/*-----------------------------------------------------------------
Standard internet scripts
1) keyPress - to make sure that the currency input (ave holiday pay) and the integer input (days and ops) are in the right format
param : e - the event to allow Firefox and IE to work
param : textType - I for integer, C for currency, AN - for AlphaNumeric, NAME = Alpha, full stop, hyphen, spaces and apostrophe AP = Alpha and some puncutation, PN = Phone Numbers, ANP = Alpha, numeric and punctuation
2) roundValue - rounds value to currency (ie 2 dp)
param : valueToRound - amount to round
3) validate email - validates whether an email is correctly formed or not (validates approx 99.6% according to the website I nicked it from)
4) trapKp - fires a button off if return is pressed 
5) validatePhoneNum - validates a phone num ... all chars must be integer or blank, 
first char must be 0, 
if second char is not 0 (National num) then length must be 10 or 11 chars, 
if second char is 0 (internation nums) then length must be > 10
------------------------------------------------------------------*/

function keyPress(e, textType) {
    //get the value of the keypress depending on whether it's IE or not
    var key = window.event ? e.keyCode : e.which;

    //get the name of the control depending upon whether it's IE or not
    var sender = window.event ? e.srcElement : e.target;

    switch (textType) {
        case "I": //INTEGER

            if (parseInt(key) == 45 && sender.value.indexOf("-") > -1) //allow hyphens
            {
                //e.stopPropagation works only in Firefox.
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                else {
                    e.returnValue = 0;
                }

                break;
            }

            if (//8 = backspace, 45 = hyphen
                    (parseInt(key) < 48 || parseInt(key) > 57) && parseInt(key) !== 8 && parseInt(key) !== 45
                ) {
                //e.stopPropagation works only in Firefox.
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                else {
                    e.returnValue = 0;
                }
                break;
            }
            break;

        case "DT": //date
            if ((parseInt(event.keyCode) < 47 || parseInt(event.keyCode) > 57) && parseInt(event.keyCode) !== 8 && parseInt(event.keyCode) !== 13 && parseInt(event.keyCode) !== 13) {
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                else {
                    e.returnValue = 0;
                }
            }


            break;

        case "AN": //ALPHA NUMERIC
            if ((parseInt(key) < 48 || parseInt(key) > 57) && (parseInt(key) < 65 || parseInt(key) > 90) && (parseInt(key) < 97 || parseInt(key) > 122)) {
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                else {
                    e.returnValue = 0;
                }
                break;
            }
            break;

        case "NAME": // Alpha and other chars used in names (namely apostrophes, hypens, full stops and spaces)
            if ((parseInt(key) < 65 || parseInt(key) > 90) && (parseInt(key) < 97 || parseInt(key) > 122) && parseInt(key) != 32
                && parseInt(key) != 46 && parseInt(key) != 45 && parseInt(key) != 39) {
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                else {
                    e.returnValue = 0;
                }
                break;
            }
            break;

        case "NI": //AANNNNNN[A-D]

            var senderLength = sender.value.length;
            if (senderLength == 0 || senderLength == 1 || senderLength == 8) {
                //Letters
                if (senderLength == 8 && !((parseInt(key) >= 65 && parseInt(key) <= 68) || (parseInt(key) >= 97 && parseInt(key) <= 100))) {
                    //e.stopPropagation works only in Firefox.
                    if (e.stopPropagation) {
                        e.stopPropagation();
                        e.preventDefault();
                    }
                    else {
                        e.returnValue = 0;
                    }
                    break;
                }
                else if (!((parseInt(key) >= 65 && parseInt(key) <= 90) || (parseInt(key) >= 97 && parseInt(key) <= 122))) {
                    //e.stopPropagation works only in Firefox.
                    if (e.stopPropagation) {
                        e.stopPropagation();
                        e.preventDefault();
                    }
                    else {
                        e.returnValue = 0;
                    }
                    break;
                }

                else {
                    if (parseInt(key) >= 97) {
                        e.keyCode = parseInt(key) - 32;
                    }
                }

            }
            else if (senderLength > 8) //Toll long
            {
                //e.stopPropagation works only in Firefox.
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                else {
                    e.returnValue = 0;
                }
                break;
            }
            else //NUmbers
            {
                if (//8 = backspace, 45 = hyphen
                    (parseInt(key) < 48 || parseInt(key) > 57) && parseInt(key) !== 8
               		 ) {
                    //e.stopPropagation works only in Firefox.
                    if (e.stopPropagation) {
                        e.stopPropagation();
                        e.preventDefault();
                    }
                    else {
                        e.returnValue = 0;
                    }
                    break;
                }
                break;
            }
            break;


        case "AP": //Alpha, punctuation (space, full stop, hyphen, ampersand, comma,  open and close brackets and return)
            if ((parseInt(key) < 65 || parseInt(key) > 90) && (parseInt(key) < 97 || parseInt(key) > 122) && parseInt(key) != 32
                            && parseInt(key) != 45 && parseInt(key) != 38 && parseInt(key) != 46 && parseInt(key) != 44 &&
                                    parseInt(key) != 40 && parseInt(key) != 41 && parseInt(key) != 46 && parseInt(key) != 13) {
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                else {
                    e.returnValue = 0;
                }
                break;
            }
            break;

        case "ANP": //Alpha, numeric and punctuation (space, full stop, hyphen, ampersand, comma,  open and close brackets and return)
            if ((parseInt(key) < 48 || parseInt(key) > 57) &&
                (parseInt(key) < 65 || parseInt(key) > 90) && (parseInt(key) < 97 || parseInt(key) > 122) && parseInt(key) != 32
                            && parseInt(key) != 45 && parseInt(key) != 38 && parseInt(key) != 46 && parseInt(key) != 44 &&
                                    parseInt(key) != 40 && parseInt(key) != 41 && parseInt(key) != 46 && parseInt(key) != 13) {
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                else {
                    e.returnValue = 0;
                }
                break;
            }
            break;

        case "PH": //PHONE
            if ((parseInt(key) < 48 || parseInt(key) > 57) && parseInt(key) !== 8 && parseInt(key) !== 32) {
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                else {
                    e.returnValue = 0;
                }
                break;
            }
            break;

        case "C": //Currency
            if (parseInt(key) == 46 && sender.value.indexOf(".") > -1) //only allow one decimal point
            {
                //e.stopPropagation works only in Firefox.
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                else {
                    e.returnValue = 0;
                }
                break;
            }
            if ((parseInt(key) < 48 || parseInt(key) > 57) && parseInt(key) !== 8 && parseInt(key) !== 46) {
                //e.stopPropagation works only in Firefox.
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                else {
                    e.returnValue = 0;
                }
                break;
            }

            if (textType == "C") //only allow 2 dp
            {
                idxDot = sender.value.indexOf(".");
                if (idxDot != -1 && sender.value.length > idxDot + 2) {
                    //e.stopPropagation works only in Firefox.
                    if (e.stopPropagation) {
                        e.stopPropagation();
                        e.preventDefault();
                    }
                    else {
                        e.returnValue = 0;
                    }
                    break;
                }
            }
            break;
    }
}


function roundValue(valueToRound) {
    var negPos = String(valueToRound).indexOf("-");

    //remove the negative symbol whilst working out dps and comma positions
    if (negPos == 0) {
        valueToRound = -1 * valueToRound;
    }


    valueToRound = (Math.round(100 * valueToRound)) / 100;

    //add decimal places
    if (String(valueToRound).indexOf(".") == -1)
    { valueToRound += ".00"; }
    if (String(valueToRound).length - String(valueToRound).indexOf(".") == 2)
    { valueToRound += "0"; }

    //put in commas
    var length = valueToRound.length;

    if (length > 6 && length < 10) {
        valueToRound = valueToRound.substring(0, length - 6) + ',' + valueToRound.substring(length - 6);

    }

    if (length >= 10) {
        valueToRound = valueToRound.substring(0, length - 9) + ',' + valueToRound.substring(length - 9, 3) + ',' + valueToRound.subsing(length - 6);
    }

    valueToRound = '£' + String(valueToRound);

    if (negPos == 0) {
        valueToRound = '-' + valueToRound;
    }

    return valueToRound;
}




function validateEmail(email) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(email))
        return true;
    else
        return false;
}

function validatePhoneNum(phoneNum) {
    phoneNum = phoneNum.replace(' ', '');

    if (phoneNum.length < 10) {
        return false;
    }
    else {
        //rule 1 : accept either national or internationl numbers
        //National nums will be 10 or 11 chars, international > 10 chars
        if (!((phoneNum.substring(0, 1) == "0" && phoneNum.substring(1, 1) != "0" && (phoneNum.length == 10 || phoneNum.length == 11))
                 || (phoneNum.substring(0, 2) == "00" && phoneNum.length > 10))) {
            return false;
        }
        else {
            //make sure all chars are integers ...
            for (i = 0; i < phoneNum.length; i++) {
                var asciiValue = phoneNum.charCodeAt(i);

                if (asciiValue < 48 || asciiValue > 57) {
                    return False;
                }
            }
        }
    }

    //if got here then the phone number is honky dorey
    return true;
}

function trapKP(e, btnName) {

    //check to see if ie or not
    var key = window.event ? e.keyCode : e.which;

    //get firing control
    var sender = window.event ? e.srcElement : e.target;

    var btn = document.getElementById(btnName);


    //Fire the button if return is pressed
    if (e.keyCode == 13) {
        //check for firefox
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }
        else {
            e.returnValue = 0;
            e.cancelBubble = true;
        }

        btn.click();
    }
}

/*  trap a textbox carriage return and postback 
btnID = submit button, destination = postback destintation page
this script works in IE & FF */
function trapCRAndPostBack(e, btnID, destination) {
    e = e || window.event;
    var unicode = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    if (unicode == 13)
        WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(btnID, "", false, "", destination, false, false));
}
