﻿/*********************************************************************
COMMON FORM FUNCTIONS
**********************************************************************/

function SubmitAction(sInput) {

    oInput = document.getElementById(sInput);
    if (oInput != null) {
        oInput.disabled = false;
    }

    if (document.forms[0].submit) {
        document.forms[0].submit();
    }

    return false;
}

function Submit() {
    if (document.forms[0].submit)
        document.forms[0].submit();

    return false;
}

function SetScroll(id) {
    if (document.getElementById(id) != null) {
        var y = 0;
        if (document.documentElement.scrollTop != null) {
            y = document.documentElement.scrollTop;
        }
        document.getElementById(id).value = y;
    }
}

function ScrollPage(id) {
    if (document.getElementById(id) != null) {
        if (document.documentElement.scrollTop != null) {
            document.documentElement.scrollTop = document.getElementById(id).value;
            document.getElementById(id).value = 0;
        }
    }
}

function SetScrollElement(inputId, elementId) {
    if (document.getElementById(inputId) != null) {
        var y = 0;
        if (document.getElementById(elementId).scrollTop != null) {
            y = document.getElementById(elementId).scrollTop;
        }
        document.getElementById(inputId).value = y;
    }
}

function ScrollElement(inputId, elementId) {
    if (document.getElementById(inputId) != null) {
        if (document.getElementById(elementId).scrollTop != null) {
            document.getElementById(elementId).scrollTop = document.getElementById(inputId).value;
            document.getElementById(inputId).value = 0;
        }
    }
}

function SubmitSelection(sInput, sField, sValue) {
    document.getElementById(sField).value = sValue;
    SubmitAction(sInput);
}



/*********************************************************************
KEY PRESS EVENTS
**********************************************************************/

function GetKeyNum(e) {
    if (!e) var e = window.event;
    if (typeof (e.which) == 'number') //NS 4, NS 6+, Mozilla 0.9+, Opera
        keyNum = e.which;
    else if (typeof (e.keyCode) == 'number') //IE, NS 6+, Mozilla 0.9+
        keyNum = e.keyCode;
    else if (typeof (e.charCode) == 'number') //also NS 6+, Mozilla 0.9+
        keyNum = e.charCode;
    else keyNum = -1;
    return keyNum;
}

function GetKeyChar(e) {
    keyNum = GetKeyNum(e);
    if (keyNum == -1) return '';
    return String.fromCharCode(keyNum);
}

function FieldInt(e) {
    keyChar = GetKeyChar(e);
    reg = /[^0-9]/;
    return !reg.test(keyChar) || IsSpecialKey(e);
}

function FieldFloat(e) {
    keyChar = GetKeyChar(e);
    reg = /[^0-9.]/;
    return !reg.test(keyChar) || IsSpecialKey(e);
}

function FieldAlpha(e) {
    keyChar = GetKeyChar(e);
    reg = /[^A-Za-z\' \-]/;
    return !reg.test(keyChar) || IsSpecialKey(e);
}

function OnEnter(e, button) {
    if (GetKeyNum(e) == 13) {
        oButton = document.getElementById(button);
        if (oButton != null) {
            oButton.click();
        }
    }
    return true;
}

function IsKeyEnter(e) {
    return (GetKeyNum(e) == 13);
}

//tests whether the string given is an integer
function IsInteger(n) // 3425234
{
    var anum = /(^(\d)+$)/;
    return anum.test(n);
}

//tests whether the string given is a decimal
// 435 or 32.3242
function IsDecimal(n) {
    var anum = /(^\d+(\.\d+)?$)/; //  34534.234, .234 is optional
    return anum.test(n);
}

// keys that can be used within fields on the form
function IsSpecialKey(e) {
    keyNum = GetKeyNum(e);
    // is the key pressed a special/function key
    return (keyNum == 0 || keyNum == 8 || keyNum == 9 || keyNum == 13 || keyNum == 16 || keyNum == 17 || keyNum == 18
     || keyNum == 19 || keyNum == 20 || keyNum == 27 || (keyNum >= 33 && keyNum <= 40) || keyNum == 45);
}

// keys that will not cause the form to be dirtied
function IsNonDirtyKey(e) {
    keyNum = GetKeyNum(e);
    // is the key pressed a special/function key
    return (keyNum == 0 || keyNum == 9 || keyNum == 13 || keyNum == 16 || keyNum == 17 || keyNum == 18
     || keyNum == 19 || keyNum == 20 || keyNum == 27 || (keyNum >= 33 && keyNum <= 40) || keyNum == 45);
}

function CancelEvent(e) {
    if (e.returnValue != null && e.returnValue != 'undefined') e.returnValue = false;
    if (e.cancelBubble != null && e.cancelBubble != 'undefined') e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}

function FormatCurrency(e) {

    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;

    var strValue = targ.value;
    if ((isNaN(strValue)) || (strValue.length == 0)) {
        return;
    }
    else {
        strValue = strValue.toString().replace(/\$|\,/g, '');
        dblValue = parseFloat(strValue);

        blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
        dblValue = Math.floor(dblValue * 100 + 0.50000000001);
        intCents = dblValue % 100;
        strCents = intCents.toString();
        dblValue = Math.floor(dblValue / 100).toString();
        if (intCents < 10)
            strCents = "0" + strCents;
        for (var i = 0; i < Math.floor((dblValue.length - (1 + i)) / 3); i++)
            dblValue = dblValue.substring(0, dblValue.length - (4 * i + 3)) + ',' +
		dblValue.substring(dblValue.length - (4 * i + 3));
        $(targ).val(((blnSign) ? '' : '-') + dblValue + '.' + strCents);
    }

}

function StripCurrency(e) {

    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    var strValue = targ.value;

    strValue = strValue.toString().replace(/\$|\,/g, '');
    $(targ).val(strValue);
    return strValue;
}


function ToggleEnabledField(id) {
    elem = document.getElementById(id);
    if (elem != null) {
        elem.disabled = !elem.disabled;
    }
}

function EnableField(id, flag) {
    elem = document.getElementById(id);
    if (elem != null) {
        elem.disabled = flag;
    }
}

function SerializeCollectionAndAppend(URL, collection, splitChar) {
    if (collection.length == 0) return URL;

    var i = 0;
    while (i < (collection.length - 1)) {
        URL = URL + collection[i] + splitChar;
        i++;
    }
    //append the last value (this time without a splitting character e.g. comma)
    URL = URL + collection[i];

    return URL;
}