﻿// LowerHouse 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;
}

// 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 false;
    }
    else {
        return true;
    }
}


// Submit Postback routine
// (causes a postback for page load to pick up)
function submitPostback(actionName) {
    __doPostBack('__Page', actionName);
}


// Swap Checkbox Checks routine
function swapCheckboxChecked(checkedName, otherName) {
    var checkedCheckBox = findObjWithClientId(checkedName);
    if (checkedCheckBox.checked == false) {
        return;
    }
    var newState = !checkedCheckBox.checked;
    var otherCheckBox = findObjWithClientId(otherName);
    otherCheckBox.checked = newState;
}


// Routine called when a data entry box key press occurs
function dataEntryBox_OnKeyPress(e) {
    numbersOnly(e);
}

// Routine called when a data entry box key up occurs - basically performs the multiplication
function dataEntryBox_OnKeyUp(e, uiDataEntryTextBoxName, multiplierValue, uiDataResultLabelName) {
    var characterCode = getCharacterCode(e);
    if ((characterCode == 8) || (characterCode == 46) || ((characterCode >= 48) && (characterCode <= 57))) {
        // Do the multiplication
        doMultiplication(uiDataEntryTextBoxName, multiplierValue, uiDataResultLabelName, false, '');       
    }
}

// Routine used to only allow numbers in textboxes
function numbersOnly(e) {
    //  Check if the key press is valid e.g. a number if it isn't ignore it
    var characterCode = getCharacterCode(e);
    if ((characterCode < 48) || (characterCode > 57)) {
        e.cancelBubble = true;
        e.returnValue = false;
        return false;
    }
    return true;
}


//  Gets a character code from an event
function getCharacterCode(e) {
    //  Check if the key press is valid e.g. a number if it isn't ignore it
    var characterCode;
    if (e && e.which) {
        e = e;
        characterCode = e.which;
    }
    else {
        e = event;
        characterCode = e.keyCode;
    }
    return characterCode;
}


// Converts a string value to a number (if invalid number string then return 0)
function convertStringValueToNumber(data) {
    if (trimAll(data) == '') {
        return 0;
    }
    else {
        var number = parseFloat(data);
        return number;
    }
}


// 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;
}



// Routine that performs the multiplication used in the grids
function doMultiplication(uiDataEntryTextBoxName, multiplierValue, uiDataResultLabelName) {
    // Key press is a number so do the multiplication

    //  Get the controls
    var uiDataEntryTextBox = findObjWithClientId(uiDataEntryTextBoxName);
    var uiDataResultLabel = findObjWithClientId(uiDataResultLabelName);
    
    //  Get the values from the controls
    var entryValue = convertStringValueToNumber(uiDataEntryTextBox.value);
    var multiplier = convertStringValueToNumber(multiplierValue);
      
    //  Do the multiplication and convert to a string
    var result = entryValue * multiplier;
    if (result == 0) {
        result = '';
    }
    else
    {
        result = result.toFixed(2);  
        result = '£' + result;
    }

    //  Load the result
    uiDataResultLabel.innerText = result;

}