function ifFullThenAdvance(thisField, nextField, e) {
    if (!e) {
        e = window.event;
    }

    keyPressed = e.keyCode;


    var goodChar = false;
    if ((keyPressed > 47 && keyPressed < 58) || (keyPressed > 64 && keyPressed < 91) || (keyPressed > 96 && keyPressed < 123)) {
        goodChar = true;
    }
    // Direction key + Tab
    if (keyPressed < 37 || keyPressed > 40 || keyPressed == 9) {
        thisField.value = CleanInput(thisField.value).replace(/[^a-zA-Z0-9]/gi, "").toUpperCase();
    }

    if (thisField.value.length == 5 && goodChar) {
        nextField.focus();
        if (nextField.type == "text") {
            nextField.select();
        }
    }
}

// Trim all leading spaces from a string.
function LTrim(InString) {
    OutString = InString;
    for (Count = 0; Count < InString.length; Count++) {
        TempChar = InString.substring(Count, Count + 1);
        if (TempChar != " ") {
            OutString = InString.substring(Count, InString.length);
            break;
        }
    }
    return (OutString);
}

// Trim all trailing spaces from a string.
function RTrim(argvalue) {
    while (1) {
        if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
            break;

        argvalue = argvalue.substring(0, argvalue.length - 1);
    }
    return argvalue;
}

// Remove dangerous characters from the input string
// i.e. "<", ">"
function CleanInput(input) {
    return input.replace(/</g, "").replace(/>/g, "").replace(/&#/g, "");
}

// Clean plain text input
function cleanPlainText(thisField) {
    thisField.value = CleanInput(LTrim(RTrim(thisField.value)));
}

//Remove unwanted space and markup tags
function removeMarkupTag(thisField) {
    thisField.value = (thisField.value).replace("  ", " ");
    thisField.value = CleanInput(thisField.value);
}

function urlCheck(thisField, args) {
    var url = args.Value;
    url = url.toLowerCase();
    var regEx = new RegExp();
    if (url.search('www.') === 0 || url.search('http://www.') === 0 || url.search('https://www.') === 0 || url.search('ftp://www.') === 8) {
        var urlSplit = url.split('www.');
        regEx.compile("^([a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}))$");
        if (regEx.test(urlSplit[1])) {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }
    else {
        regEx.compile("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.|http:\/\/|https:\/\/|ftp:){0,1}([a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}))$");
        var lastIndeoxOfDot = url.lastIndexOf('.');
        if (regEx.test(url) && lastIndeoxOfDot != url.length - 1) {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }
    var lastIndoexOfDot = url.lastIndexOf('..');
    if (lastIndoexOfDot != -1) {
        args.IsValid = false;
        return;
    }
    //regEx.compile("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
}

function checkEmailFormat(thisField, args) {
    var emailStr = args.Value;
    emailStr = emailStr.toLowerCase();
    /* The following pattern is used to check if the entered e-mail address
    fits the user@domain format.  It also is used to separate the username
    from the domain. */
    var emailPat = /^(.+)@(.+)$/;
    /* The following string represents one word in the typical username.
    For example, in john.doe@somewhere.com, john and doe are words.
    Basically, a word is either an atom or quoted string. */
    // The following pattern describes the structure of the user
    var userPat = new RegExp("^([a-zA-Z0-9]+[a-zA-Z0-9_\\-\\.]*)$");
    /* The following pattern describes the structure of a normal symbolic
    domain, as opposed to ipDomainPat, shown above. */
    var domainPat = new RegExp("^([a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}))$");


    /* Finally, let's start trying to figure out if the supplied address is
    valid. */

    /* Begin with the coarse pattern to simply break up user@domain into
    different pieces that are easy to analyze. */
    var matchArray = emailStr.match(emailPat);
    if (matchArray == null) {
        /* Too many/few @'s or something; basically, this address doesn't
        even fit the general mould of a valid e-mail address. */
        args.IsValid = false;
        return;
    }

    var user = matchArray[1];
    var domain = matchArray[2];

    // See if "user" is valid 
    if (user.match(userPat) == null) {
        // user is not valid
        args.IsValid = false;
        return;
    }

    // Domain is symbolic name
    var domainArray = domain.match(domainPat);
    if (domainArray == null) {
        args.IsValid = false;
        return;
    }

    // Check if multiple . are present in domain part for Email
    var lastIndoexOfDot = domainArray[1].lastIndexOf('..');
    if (lastIndoexOfDot != -1) {
        args.IsValid = false;
        return;
    }

    // If we've gotten this far, everything's valid!
    if (LTrim(RTrim(emailStr)).length > 0) {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }
}


// check for a valid email.
function emailCheck(thisField, args) {
    var emailStr = args.Value;
    emailStr = emailStr.toLowerCase();
    /* The following pattern is used to check if the entered e-mail address
    fits the user@domain format.  It also is used to separate the username
    from the domain. */
    var emailPat = /^(.+)@(.+)$/;
    /* The following string represents one word in the typical username.
    For example, in john.doe@somewhere.com, john and doe are words.
    Basically, a word is either an atom or quoted string. */
    // The following pattern describes the structure of the user
    var userPat = new RegExp("^([a-zA-Z0-9]+[a-zA-Z0-9_\\-\\.]*)$");
    /* The following pattern describes the structure of a normal symbolic
    domain, as opposed to ipDomainPat, shown above. */
    var domainPat = new RegExp("^([a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}))$");


    /* Finally, let's start trying to figure out if the supplied address is
    valid. */

    /* Begin with the coarse pattern to simply break up user@domain into
    different pieces that are easy to analyze. */
    var matchArray = emailStr.match(emailPat);
    if (matchArray == null) {
        /* Too many/few @'s or something; basically, this address doesn't
        even fit the general mould of a valid e-mail address. */
        args.IsValid = false;
        return;
    }

    var user = matchArray[1];
    var domain = matchArray[2];

    // See if "user" is valid 
    if (user.match(userPat) == null) {
        // user is not valid
        args.IsValid = false;
        return;
    }

    // Domain is symbolic name
    var domainArray = domain.match(domainPat);
    if (domainArray == null) {
        args.IsValid = false;
        return;
    }

    // Check if multiple . are present in domain part for Email
    var lastIndoexOfDot = domainArray[1].lastIndexOf('..');
    if (lastIndoexOfDot != -1) {
        args.IsValid = false;
        return;
    }

    // If we've gotten this far, everything's valid!
    if (LTrim(RTrim(emailStr)).length > 0) {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }

    if (args.IsValid) {
        var eMail1;
        var eMail2;

        if (null != document.getElementById('ctl00_ContentPlaceHolder_textEmailShippingFormat')) {
            eMail1 = document.getElementById('ctl00_ContentPlaceHolder_textEmailShippingFormat').value;
        }
        else if (null != document.forms[0].textEmailShippingFormat) {
            eMail1 = document.forms[0].textEmailShippingFormat.value
        }

        if (null != document.getElementById('ctl00_ContentPlaceHolder_textReEnterEmailShippingFormat')) {
            eMail2 = document.getElementById('ctl00_ContentPlaceHolder_textReEnterEmailShippingFormat').value;
        }
        else if (null != document.forms[0].textReEnterEmailShippingFormat) {
            eMail2 = document.forms[0].textReEnterEmailShippingFormat.value;
        }
        if (eMail1 != null && eMail2 != null && eMail1 != "" && eMail2 != "") {
            eMail1 = eMail1.toLowerCase();
            eMail2 = eMail2.toLowerCase();

            if (eMail1 == eMail2) {
                args.IsValid = true;
            }
            else {
                args.IsValid = false;
            }
        }
    }
    else {
        return;
    }
}

function keyCheck(thisField, args) {
    var key = document.getElementById(key1).value;
    key = key.concat(document.getElementById(key2).value);
    key = key.concat(document.getElementById(key3).value);
    key = key.concat(document.getElementById(key4).value);
    key = key.concat(document.getElementById(key5).value);
    args.IsValid = (key.length == 25);
}

// Check max length for text area fields.
function CheckMaxLength(thisField, maxlength) {
    if (thisField.value.length > maxlength) {
        thisField.value = thisField.value.substring(0, maxlength);
    }
}

function CheckPhoneOrZipCode(thisField, args) {
    var phone = args.Value;

    if (phone != null && LTrim(RTrim(phone)).length > 0) {
        // check whether the phone number contains anything except numeric fields
        if (parseInt(LTrim(RTrim(phone)), 10) == LTrim(RTrim(phone))) {
            if (isNaN(LTrim(RTrim(phone)))) {
                args.IsValid = false;
            }
            else {
                args.IsValid = true;
            }
        }
        else {
            args.IsValid = false;
        }
    }
    else {
        args.IsValid = false;
    }
}

function ReEmailCheck(obj, args) {
    args.IsValid = true;
    var email;
    if (null != document.getElementById("ctl00_ContentPlaceHolder_textEmailShippingFormat")) {
        email = document.getElementById("ctl00_ContentPlaceHolder_textEmailShippingFormat").value;
    }
    else if (null != document.forms[0].textEmailShippingFormat) {
        email = document.forms[0].textEmailShippingFormat.value;
    }
    var div = document.getElementById("divReEnterEmailShippingFormat");
    div.style.display = "none";
    if (email != "" && args.Value == "") {
        args.IsValid = false;
    }
    else if (args.Value == "") {
        div.style.display = "inline";
    }
    else {
        args.IsValid = true;
    }
}
var OnLoadFunction = window.onload;
window.onload = function() {
    var txtReEmail;
    if (null != document.getElementById("ctl00_ContentPlaceHolder_textReEnterEmailShippingFormat")) {
        txtReEmail = document.getElementById("ctl00_ContentPlaceHolder_textReEnterEmailShippingFormat");
        txtReEmail.onchange = function() { cleanPlainText(txtReEmail) };
    }
    else if (null != document.forms[0].textReEnterEmailShippingFormat) {
        txtReEmail = document.forms[0].textReEnterEmailShippingFormat;
        txtReEmail.onchange = function() { cleanPlainText(txtReEmail) };
    }
    if (typeof (OnLoadFunction) == "function") {
        OnLoadFunction();
    }
}

function ClientValidateDate(sender, args) {
    var date;
    if (null != document.getElementById("ctl00_ContentPlaceHolder_txtDate")) {
        date = document.getElementById("ctl00_ContentPlaceHolder_txtDate").value;
    }
    else if (null != document.forms[0].txtDate) {
        date = document.forms[0].txtDate.value;
    }

    if (Date.parse(date) > new Date()) {
        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }
}

//Check the reseller email format
function ResellerEmailCheck(thisField, args) {
    var emailStr = args.Value;
    emailStr = emailStr.toLowerCase();
    /* The following pattern is used to check if the entered e-mail address
    fits the user@domain format.  It also is used to separate the username
    from the domain. */
    var emailPat = /^(.+)@(.+)$/;
    /* The following string represents one word in the typical username.
    For example, in john.doe@somewhere.com, john and doe are words.
    Basically, a word is either an atom or quoted string. */
    // The following pattern describes the structure of the user
    var userPat = new RegExp("^([a-zA-Z0-9]+[a-zA-Z0-9_\\-\\.]*)$");
    /* The following pattern describes the structure of a normal symbolic
    domain, as opposed to ipDomainPat, shown above. */
    var domainPat = new RegExp("^([a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}))$");


    /* Finally, let's start trying to figure out if the supplied address is
    valid. */

    /* Begin with the coarse pattern to simply break up user@domain into
    different pieces that are easy to analyze. */
    var matchArray = emailStr.match(emailPat);
    if (matchArray == null) {
        /* Too many/few @'s or something; basically, this address doesn't
        even fit the general mould of a valid e-mail address. */
        args.IsValid = false;
        return;
    }

    var user = matchArray[1];
    var domain = matchArray[2];

    // See if "user" is valid 
    if (user.match(userPat) == null) {
        // user is not valid
        args.IsValid = false;
        return;
    }

    // Domain is symbolic name
    var domainArray = domain.match(domainPat);
    if (domainArray == null) {
        args.IsValid = false;
        return;
    }

    // Check if multiple . are present in domain part for Email
    var lastIndoexOfDot = domainArray[1].lastIndexOf('..');
    if (lastIndoexOfDot != -1) {
        args.IsValid = false;
        return;
    }

    // If we've gotten this far, everything's valid!
    if (LTrim(RTrim(emailStr)).length > 0) {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }

    if (args.IsValid) {
        var eMail3;

        if (null != document.getElementById('ctl00_ContentPlaceHolder_txtResellerEmail')) {
            eMail3 = document.getElementById('ctl00_ContentPlaceHolder_txtResellerEmail').value;
        }
        else if (null != document.forms[0].txtResellerEmail) {
            eMail3 = document.forms[0].txtResellerEmail.value;
        }
        if (eMail3 != null && eMail3 != "") {
            args.IsValid = true;
        }

    }
    else {
        return;
    }
}
