﻿// Acumen Website JavaScript Library

// Go to anchor function
function goToAnchor(pageName, anchorName) {
    location.href = pageName + "#" + anchorName;
}

// Finds a server control on the client side by id
function findObjWithClientId(Id) {
    var ctrls = document.getElementsByTagName("body")[0].getElementsByTagName("*");
    for (var count = 0; count < ctrls.length; count++) {
        var index = ctrls[count].id.indexOf(Id);
        if (index != -1) {
            if ((ctrls[count].id.length - index) == Id.length) {
                return ctrls[count];
            }
        }
    }
    return null;
}

// Finds and disables / enables a link button with the given name
function disableLinkButton(buttonName, disable) {
    var theButton = findObjWithClientId(buttonName);
    disableLinkButtonByControl(theButton, disable);
}

// Trim routine for strings
function trimAll(stringToTrim) {
    while (stringToTrim.substring(0, 1) == ' ') {
        stringToTrim = stringToTrim.substring(1, stringToTrim.length);
    }
    while (stringToTrim.substring(stringToTrim.length - 1, stringToTrim.length) == ' ') {
        stringToTrim = stringToTrim.substring(0, stringToTrim.length - 1);
    }
    return stringToTrim;
}


// Checks if a email address is valid
function isEmailAddressValid(emailAddress) {
    emailAddress = trimAll(emailAddress);
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(emailAddress)) {
        return false;
    }
    else {
        return true;
    }
}

// Set control class
function setMultiTabControlClass(theControlName, className) {
    var theControl = findObjWithClientId(theControlName);
    if (theControl != null) {
        var currentClass = theControl.getAttribute("class");
        if (currentClass == null) {
            currentClass = '';
        }
        if (currentClass.length == 0) {
            currentClass = theControl.getAttribute("className");
            if (currentClass == null) {
                currentClass = '';
            }
        }
        if (currentClass.indexOf('last') != -1) {
            theControl.setAttribute("class", 'last ' + className);
            theControl.setAttribute("className", 'last ' + className);
        }
        else {
            theControl.setAttribute("class", className);
            theControl.setAttribute("className", className);            
        }       
    }
}

// Show Multi Tab Content
function showMultiTabContent(theTabNo) {

    hideDiv('uiMultiTabContent1');
    hideDiv('uiMultiTabContent2');
    hideDiv('uiMultiTabContent3');
    hideDiv('uiMultiTabContent4');
    hideDiv('uiMultiTabContent5');

    setMultiTabControlClass('uiMultiTabListItem1', '');
    setMultiTabControlClass('uiMultiTabListItem2', '');
    setMultiTabControlClass('uiMultiTabListItem3', '');
    setMultiTabControlClass('uiMultiTabListItem4', '');
    setMultiTabControlClass('uiMultiTabListItem5', '');

    // Show the correct tab content based on the tab click
    switch (theTabNo) {
        case 1: // Tab 1
            showDiv('uiMultiTabContent1');
            setMultiTabControlClass('uiMultiTabListItem1', 'active');
            break;
        case 2: // Tab 2
            showDiv('uiMultiTabContent2');
            setMultiTabControlClass('uiMultiTabListItem2', 'active');
            break;
        case 3: // Tab 3
            showDiv('uiMultiTabContent3');
            setMultiTabControlClass('uiMultiTabListItem3', 'active');
            break;
        case 4: // Tab 4
            showDiv('uiMultiTabContent4');
            setMultiTabControlClass('uiMultiTabListItem4', 'active');
            break;
        case 5: // Tab 5
            showDiv('uiMultiTabContent5');
            setMultiTabControlClass('uiMultiTabListItem5', 'active');
            break;
    }    
}

//  Initialise Multi Tab Content
function initialiseMultiTabContent() {

    showDiv('uiMultiTabContent1');
    setMultiTabControlClass('uiMultiTabListItem1', 'active');
    hideDiv('uiMultiTabContent2');
    hideDiv('uiMultiTabContent3');
    hideDiv('uiMultiTabContent4');
    hideDiv('uiMultiTabContent5');
}

// Show Multi Tab Content
function showDiv(controlName) {
    var theDiv = findObjWithClientId(controlName);
    theDiv.style.display = "block";  
}

// Hide Multi Tab Content
function hideDiv(controlName) {
    var theDiv = findObjWithClientId(controlName);
    theDiv.style.display = "none";
}


// Set Default Text on Textbox On Focus Routine
function setDefaultTextOnTextBoxOnFocus(textBoxName, defaultText) {
    // Find the text box
    var theTextBox = findObjWithClientId(textBoxName);
    if (theTextBox != null) {
        // Check if its value is set to the default
        if (theTextBox.value == defaultText) {    
            // Clear the text box         
            theTextBox.value = "";
        }
    }

}

// Set Default Text on Textbox On Blur Routine
function setDefaultTextOnTextBoxOnBlur(textBoxName, defaultText) {
    // Find the text box
    var theTextBox = findObjWithClientId(textBoxName);
    if (theTextBox != null) {
        // Get the value from text box
        var textValue = theTextBox.value;
        if (textValue == defaultText || textValue.length == 0) {
            theTextBox.className = "textWaterMarkOn";
            theTextBox.value = defaultText;
        }      
    }
}


//   Show video player routine
function showVideoPlayer(videoPlayerUrl, width, height, title) {    
    var specs = 'width=' + width + ',height=' + height + ',resizable=yes';
    window.open(videoPlayerUrl, 'machineVideoDialog', specs);
}


// Gets the current RadWindow
function getRadWindow() {
    var oWindow = null;
    if (window.radWindow)
        oWindow = window.radWindow;
    else if (window.frameElement.radWindow)
        oWindow = window.frameElement.radWindow;
    return oWindow;
}

// Closes the current RadWindow
function closeRadWindow() {
    var oWindow = getRadWindow();
    oWindow.argument = null;
    oWindow.close();
}

// Routine for the show terms &  conditions dialog
function showTermsConditionsDialog() {
    // Show the terms & conditions popup
    var url = "termsconditionsdialog.aspx";
    var newwindow;
    newwindow = window.open(url, 'termsConditionsDialog', 'left=300, top=300, height=450, width=550, status=no, resizable=no, scrollbars=yes, toolbar=no,location=no, menubar=no');
    if (window.focus) { newwindow.focus() }
}


// Checks for enter key and forces a postback to occur with the target event argument to be the same as the actionName parameter
function performPostbackOnEnter(e, actionName) {
    var characterCode;

    if (e && e.which) {
        e = e;
        characterCode = e.which;
    }
    else {
        e = event;
        characterCode = e.keyCode;
    }

    if (characterCode == 13) {
        __doPostBack('__Page', actionName);
        return true;
    }
    else {
        return false;
    }
}


// Submit Postback routine
// (causes a postback for page load to pick up)
function submitPostback(actionName) {
    __doPostBack('__Page', actionName);
}


// Get character code routine
function getCharacterCode(e) {
    if (e && e.which) {
        e = e;
        characterCode = e.which;
    }
    else {
        e = event;
        characterCode = e.keyCode;
    }
    return characterCode;
}


// Used to handle the onkeypress for the quick stock search text box
function performDoStockSearch(e) {
    var characterCode = getCharacterCode(e);
    if (characterCode == 13) {
        // Return key pressed do the validation
        var theTextBox = findObjWithClientId('uiStockQuickSearchTextbox');
        if (trimAll(theTextBox.value).length == 0) {
            alert('Please enter a search term in the Stock Search!');
            return false;
        }
        if (theTextBox.value == 'Keyword or Stock No...') {
            alert('Please enter a search term in the Stock Search!');
            return false;
        }

        // Validation done force the post back
        var actionName = 'doStockSearch';
        submitPostback(actionName);
        return true;
    }
    else {
        return false;
    }

}



// Used to handle the onkeypress for the join mailing list text box
function performDoJoinMailingList(e) {
    var characterCode = getCharacterCode(e);
    if (characterCode == 13) {
        // Return key pressed do the validation
        var theTextBox = findObjWithClientId('uiJoinEmailListTextbox');
        if (trimAll(theTextBox.value).length == 0) {
            alert('Please enter an email address!');
            return false;
        }
        if (isEmailAddressValid(theTextBox.value) == false) {
            alert('Please enter a valid email address!');
            return false;
        }

        // Validation done force the post back
        var actionName = 'doJoinMailingList';
        submitPostback(actionName);
        return true;
    }
    else {
        return false;
    }

}


// Used to handle the onkeypress for the login side menu control
function performDoLoginSideControl(e) {
    var characterCode = getCharacterCode(e);
    if (characterCode == 13) {
        // Return key pressed do the validation
        var theEmailTextBox = findObjWithClientId('uiEmailAddressTextBox');
        var thePasswordTextBox = findObjWithClientId('uiPasswordTextBox');
        if (trimAll(theEmailTextBox.value).length == 0) {
            alert('Please enter your email address!');
            return false;
        }
        if (trimAll(thePasswordTextBox.value).length == 0) {
            alert('Please enter your password!');
            return false;
        }

        // Validation done force the post back
        var actionName = 'doLoginSideControl';        
        submitPostback(actionName);
        return true;
    }
    else {
        return false;
    }

}


// Used to handle the onkeypress for the my account login page
function performDoLoginMyAccount(e) {
    var characterCode = getCharacterCode(e);
    if (characterCode == 13) {
        // Return key pressed do the validation
        var theEmailTextBox = findObjWithClientId('uiMyAccountEmailAddressTextBox');
        var thePasswordTextBox = findObjWithClientId('uiMyAccountPasswordTextBox');
        if (trimAll(theEmailTextBox.value).length == 0) {
            alert('Please enter your email address!');
            return false;
        }
        if (trimAll(thePasswordTextBox.value).length == 0) {
            alert('Please enter your password!');
            return false;
        }

        // Validation done force the post back
        var actionName = 'doLogin';
        submitPostback(actionName);
        return true;
    }
    else {
        return false;
    }

}

