﻿String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}

function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}

function Replicate(s, n) {
    var r = '';
    while (n) {
        if (n & 1) { r += s; }
        s += s;
        n >>= 1;
    }
    return r;
}

function disableEnterKey(evt) {
    var key = (window.Event) ? evt.which : evt.keyCode;
    if (key == 13 || key == 8) return false;
    return true;
}

function getCookie(name) {
    var pos
    var token = name + "=";
    var tnlen = token.length;
    var cklen = document.cookie.length;
    var i = 0;
    var j;
    while (i < cklen) {
        j = i + tnlen;
        if (document.cookie.substring(i, j) == token) {
            pos = document.cookie.indexOf(";", j);
            if (pos == -1)
                pos = document.cookie.length;
            return unescape(document.cookie.substring(j, pos));
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

function setCookie(name, value) {
    document.cookie = name + "=" + escape(value)

}

function deleteCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function isCookiesAllowed() {
    var gCookieName;
    var gCookieValueRet;
    gCookieName = "AVCOOKIETEST";
    gCookieValue = "AV_TESTING_COOKIE";
    setCookie(gCookieName, gCookieValue);
    gCookieValueRet = getCookie(gCookieName);
    deleteCookie(gCookieName);
    if (gCookieValueRet !== null) {
        return true;
    }
    else {
        return false;
    }
}

function bookmarksite(title, url) {
    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
    else if (window.opera && window.print) { // opera
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    }
    else if (document.all)// ie
        window.external.AddFavorite(url, title);
}

function showClock(displayId) {
    var newdate = new Date();
    var day = "";
    var month = "";
    var myweekday = "";
    var houradjust = -1;
    var year = "";
    var mydate = new Date();
    var dston = new Date('March 14, 1999 2:59:59');
    var dstoff = new Date('November 7, 1999 2:59:59');
    dston.setFullYear(newdate.getFullYear());
    dstoff.setFullYear(newdate.getFullYear());

    var myzone = newdate.getTimezoneOffset();

    var zone = -5;
    if (zone <= 0) {
        zone = -zone;
    }

    newtime = newdate.getTime();

    var newzone = (zone * 60 * 60 * 1000);
    newtimea = newtime + (myzone * 60 * 1000) - newzone;

    mydate.setTime(newtimea);
    myday = mydate.getDay();
    mymonth = mydate.getMonth();
    myweekday = mydate.getDate();
    myyear = mydate.getYear();
    year = myyear;

    if (year < 2000) year = year + 1900;
    myhours = mydate.getHours();


    var mm = "am";
    if (myhours > 11 + houradjust)
        mm = "pm";
    if (myhours > 12 + houradjust)
        myhours -= 12;
    if (myhours == 0) myhours = 12;


    myminutes = mydate.getMinutes();

    if (myminutes < 10) {
        mytime = "0" + myminutes;
    }
    else {
        mytime = "" + myminutes;
    };

    myseconds = mydate.getSeconds();

    if (myseconds < 10) {
        myseconds = "0" + myseconds;
    } else {
        myseconds = "" + myseconds;
    };

    arday = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
    armonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
    ardate = new Array("0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th", "31st");


    var time = ("" + arday[myday] + "," + " " + armonth[mymonth] + " " + ardate[myweekday] + "," + " " + year + " " + "" + "" + myhours + ":" + mytime + ":" + mm + "");

    document.getElementById(displayId).innerHTML = time;

    setTimeout("showClock('" + displayId + "')", 1000)

}

function extractNumber(obj, decimalPlaces, allowNegative) {
    var temp = obj.value;
    var reg0Str = '[0-9]*';
    if (decimalPlaces > 0) {
        reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
    } else if (decimalPlaces < 0) {
        reg0Str += '\\.?[0-9]*';
    }
    reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
    reg0Str = reg0Str + '$';
    var reg0 = new RegExp(reg0Str);
    if (reg0.test(temp)) return true;

    // first replace all non numbers
    var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
    var reg1 = new RegExp(reg1Str, 'g');
    temp = temp.replace(reg1, '');

    if (allowNegative) {
        // replace extra negative
        var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
        var reg2 = /-/g;
        temp = temp.replace(reg2, '');
        if (hasNegative) temp = '-' + temp;
    }

    if (decimalPlaces != 0) {
        var reg3 = /\./g;
        var reg3Array = reg3.exec(temp);
        if (reg3Array != null) {
            // keep only first occurrence of . 
            //  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
            var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
            reg3Right = reg3Right.replace(reg3, '');
            reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
            temp = temp.substring(0, reg3Array.index) + '.' + reg3Right;
        }
    }
    obj.value = temp;
}

function blockNonNumbers(obj, e, allowDecimal, allowNegative) {
    var key;
    var isCtrl = false;
    var isShift = false;
    var keychar;
    var reg;

    if (window.event) {
        key = e.keyCode;
        isCtrl = window.event.ctrlKey;
        isShift = window.event.shiftKey;
    }
    else if (e.which) {
        key = e.which;
        isCtrl = e.ctrlKey;
        isShift = e.shiftKey;
    }

    if (isNaN(key)) return true;

    keychar = String.fromCharCode(key);

    // check for backspace or delete, or if Ctrl was pressed
    if (key == 8) {
        return true;
    }
    reg = /\d/;
    var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
    var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;

    return isFirstN || isFirstD || reg.test(keychar);
}

function blockNonDate(obj, e) {
    var key;
    var isCtrl = false;
    var isShift = false;
    var keychar;
    if (window.event) {
        key = e.keyCode;
        isCtrl = window.event.ctrlKey;
        isShift = window.event.shiftKey;
    }
    else if (e.which) {
        key = e.which;
        isCtrl = e.ctrlKey;
        isShift = e.shiftKey;
    }
    if (isNaN(key)) return true;
    keychar = String.fromCharCode(key);
    // check for backspace or delete, or if Ctrl was pressed
    if (key == 8) {
        return true;
    }
    var reg0 = new RegExp("[0-9]|[/]");
    return reg0.test(keychar);
}

function convertDateNoFormat(fld) {
    var strToCheck = fld.value.trim();
    var uryear = 0;
    var urmonth = 0;
    var urday = 0;
    var MinimumYearToConsider = 10;
    var InvalidDateMsg = "Invalid Date Entry \n (mm/dd/yyyy)";
    var newDate = new Date();
    if (strToCheck.length == 0) {
        return true;
    }
    var reg0 = new RegExp("[0-9]|[/]");
    if (!reg0.test(strToCheck)) {
        alert(InvalidDateMsg);
        fld.focus();
        return;
    }
    if (strToCheck.indexOf("/") >= 0) {
        urmonth = validateDateGetDatePart(strToCheck, "Month")
        urday = validateDateGetDatePart(strToCheck, "Day")
        uryear = validateDateGetDatePart(strToCheck, "Year")
        var newDate1 = new Date(strToCheck);
        if (isNaN(newDate1)) {
            alert(InvalidDateMsg);
            fld.focus();
            return;
        }
        if (!validateDateisValidMonthDayYear(urmonth - 1, urday, uryear)) {
            alert(InvalidDateMsg);
            fld.focus();
            return;
        }
        if (!validateDateisValidMonthDayYear(newDate1.getMonth(), newDate1.getDate(), newDate1.getFullYear())) {
            alert(InvalidDateMsg);
            fld.focus();
            return;
        }
        else {
            newDate1.setFullYear(uryear, urmonth - 1, urday);
            fld.value = (newDate1.getMonth() + 1) + "/" + newDate1.getDate() + "/" + newDate1.getFullYear();
        }
        return;
    }
    if (strToCheck.length < 4) {
        alert(InvalidDateMsg);
        fld.focus();
        return;
    }
    if (strToCheck.length == 5 && strToCheck.substr(1, 1) > "3") {
        alert(InvalidDateMsg);
        fld.focus();
        return;
    }
    if (strToCheck.length == 7 && strToCheck.substr(1, 1) > "3") {
        alert(InvalidDateMsg);
        fld.focus();
        return;
    }
    if (strToCheck.length == 8 && strToCheck.substr(0, 1) > "1") {
        alert(InvalidDateMsg);
        fld.focus();
        return;
    }
    if (strToCheck.length == 8 && strToCheck.substr(0, 1) == "1" && strToCheck.substr(1, 1) > "2") {
        alert(InvalidDateMsg);
        fld.focus();
        return;
    }
    if (strToCheck.length == 8 && strToCheck.substr(0, 1) > "1") {
        alert(InvalidDateMsg);
        fld.focus();
        return;
    }
    if (strToCheck.length == 8 && strToCheck.substr(2, 1) > "3") {
        alert(InvalidDateMsg);
        fld.focus();
        return;
    }
    if (strToCheck.length == 4) {
        urmonth = parseInt(strToCheck.substr(0, 1), 10);
        urday = parseInt(strToCheck.substr(1, 1), 10);
        uryear = parseInt(strToCheck.substr(2, 2), 10);
        if (uryear <= 10)
        { uryear = uryear + 2000; }
        else
        { uryear = uryear + 1900; }
        if (!validateDateisValidMonthDayYear(urmonth - 1, urday, uryear)) {
            alert(InvalidDateMsg);
            fld.focus();
            return;
        }
        newDate.setFullYear(uryear, urmonth - 1, urday);
        fld.value = (newDate.getMonth() + 1) + "/" + newDate.getDate() + "/" + newDate.getFullYear();
        return;
    }
    if (strToCheck.length == 5 && strToCheck.substr(1, 1) <= "3") {
        urmonth = parseInt(strToCheck.substr(0, 1), 10);
        urday = parseInt(strToCheck.substr(1, 2), 10);
        uryear = parseInt(strToCheck.substr(3, 2), 10);
        if (uryear <= 10)
        { uryear = uryear + 2000; }
        else
        { uryear = uryear + 1900; }
        if (!validateDateisValidMonthDayYear(urmonth - 1, urday, uryear)) {
            alert(InvalidDateMsg);
            fld.focus();
            return;
        }
        newDate.setFullYear(uryear, urmonth - 1, urday);
        fld.value = (newDate.getMonth() + 1) + "/" + newDate.getDate() + "/" + newDate.getFullYear();
        return;
    }
    if (strToCheck.length == 6 && (strToCheck.substr(0, 1) > "1" && strToCheck.substr(1, 1) > "2")) {
        urmonth = parseInt(strToCheck.substr(0, 1), 10);
        urday = parseInt(strToCheck.substr(1, 1), 10);
        uryear = parseInt(strToCheck.substr(2, 4), 10);
        if (!validateDateisValidMonthDayYear(urmonth - 1, urday, uryear)) {
            alert(InvalidDateMsg);
            fld.focus();
            return;
        }
        newDate.setFullYear(uryear, urmonth - 1, urday);
        fld.value = (newDate.getMonth() + 1) + "/" + newDate.getDate() + "/" + newDate.getFullYear();
        return;
    }
    if (strToCheck.length == 6 && strToCheck.substr(0, 1) <= "1") {
        urmonth = parseInt(strToCheck.substr(0, 2), 10);
        urday = parseInt(strToCheck.substr(2, 2), 10);
        uryear = parseInt(strToCheck.substr(4, 2), 10);
        if (uryear <= 10)
        { uryear = uryear + 2000; }
        else
        { uryear = uryear + 1900; }
        if (!validateDateisValidMonthDayYear(urmonth - 1, urday, uryear)) {
            alert(InvalidDateMsg);
            fld.focus();
            return;
        }
        newDate.setFullYear(uryear, urmonth - 1, urday);
        fld.value = (newDate.getMonth() + 1) + "/" + newDate.getDate() + "/" + newDate.getFullYear();
        return;
    }
    if (strToCheck.length == 6) {
        urmonth = parseInt(strToCheck.substr(0, 2), 10);
        urday = parseInt(strToCheck.substr(2, 2), 10);
        uryear = parseInt(strToCheck.substr(4, 2), 10);
        if (uryear <= 10)
        { uryear = uryear + 2000; }
        else
        { uryear = uryear + 1900; }
        if (!validateDateisValidMonthDayYear(urmonth - 1, urday, uryear)) {
            alert(InvalidDateMsg);
            fld.focus();
            return;
        }
        newDate.setFullYear(uryear, urmonth - 1, urday);
        fld.value = (newDate.getMonth() + 1) + "/" + newDate.getDate() + "/" + newDate.getFullYear();
        return;
    }
    if (strToCheck.length == 7) {
        urmonth = parseInt(strToCheck.substr(0, 1), 10);
        urday = parseInt(strToCheck.substr(1, 2), 10);
        uryear = parseInt(strToCheck.substr(3, 4), 10);
        if (!validateDateisValidMonthDayYear(urmonth - 1, urday, uryear)) {
            alert(InvalidDateMsg);
            fld.focus();
            return;
        }
        newDate.setFullYear(uryear, urmonth - 1, urday);
        fld.value = (newDate.getMonth() + 1) + "/" + newDate.getDate() + "/" + newDate.getFullYear();
        return;
    }
    urmonth = parseInt(strToCheck.substr(0, 2), 10);
    urday = parseInt(strToCheck.substr(2, 2), 10);
    uryear = parseInt(strToCheck.substr(4, 4), 10);
    if (!validateDateisValidMonthDayYear(urmonth - 1, urday, uryear)) {
        alert(InvalidDateMsg);
        fld.focus();
        return;
    }
    newDate.setFullYear(uryear, urmonth - 1, urday);
    fld.value = (newDate.getMonth() + 1) + "/" + newDate.getDate() + "/" + newDate.getFullYear();
    return;
}

function validateDateisValidMonthDayYear(urmonth, urday, uryear) {
    if (isNaN(urmonth) || isNaN(urday) || isNaN(uryear)) {
        return false;
    }
    if (urday < 0 || urday > 31) {
        return false;
    }
    if (urmonth < 0 || urmonth > 11) {
        return false;
    }
    if (uryear <= 1900 || uryear > 2100) {
        return false;
    }
    if (validateDateHas31Days(urmonth) && urday > 31) {
        return false;
    }
    if (validateDateHas30Days(urmonth) && urday > 30) {
        return false;
    }
    if (urmonth == 1 && validateDateLeapYear(uryear) && urday > 29) {
        return false;
    }
    if (urmonth == 1 && !validateDateLeapYear(uryear) && urday > 28) {
        return false;
    }
    return true;
}

function validateDateGetDatePart(strDate, strDatePart) {
    var pos1 = strDate.indexOf("/");
    var pos2 = strDate.indexOf("/", pos1 + 1);
    var urmonth = parseInt(strDate.substr(0, pos1), 10);
    var urday = parseInt(strDate.substr(pos1 + 1, (pos2 - pos1) - 1), 10);
    var uryear = parseInt(strDate.substr(pos2 + 1), 10);
    uryear = validateDateGetYear(uryear);
    if (strDatePart == "Day") { return urday; }
    if (strDatePart == "Month") { return urmonth; }
    if (strDatePart == "Year") { return uryear; }
    return NaN;
}

function validateDateGetYear(uryear) {
    var MinimumYearToConsider = 10;

    if (uryear >= 1000) { return uryear; }

    if (uryear <= 10)
    { uryear = uryear + 2000; }

    else

    { uryear = uryear + 1900; }

    return uryear;
}

function validateDateHas31Days(urmonth) {
    var arrMonths = [0, 2, 4, 6, 7, 9, 11];
    for (i = 0; i < arrMonths.length; i++) {
        if (arrMonths[i] == urmonth) { return true; }
    }
    return false;
}

function validateDateHas30Days(urmonth) {
    var arrMonths = [3, 5, 8, 10];
    for (i = 0; i < arrMonths.length; i++) {
        if (arrMonths[i] == urmonth) { return true; }
    }
    return false;
}

function validateDateLeapYear(dateYear) {
    var dateObj = new Date(dateYear, 1, 29);
    if (dateObj.getDate() == 29) {
        return true;
    }
    else { return false; }
}

function ChangeToProperFormat(fld) {
    var strToCheck = fld.value.trim();
    fld.value = strToCheck.substr(0, 1).toUpperCase() + strToCheck.substr(1);
}

function auto_currency(fld) {
    fld = fld.formatCurrency(this.value);
}

function FormatPhone(PhoneField, format) {
    //Validate phone number for 10 digit US numbers.
    //phoneField - The HTML input field containing the phone number to validate.
    //format - Integer value that defines how to format the text field.

    var num = PhoneField.value.replace(/[^\d]/g, '');
    if (num.length != 10) {
        if (num.length == 0) {
            PhoneField.value = '';
            return;
        }
        //Alert the user that the phone number entered was invalid.
        alert('Invalid phone number. Expected Format: (999) 999-9999');
        PhoneField.focus();
    }
    else {
        //If format type is set, format the Phone to the desired style.
        switch (format) {
            case '0': //Format (xxx)xxx-xxxx
                PhoneField.value = "(" + num.substring(0, 3) + ") " +
                       num.substring(3, 6) + '-' + num.substring(6);
                break;
            case '1': //Format xxx-xxx-xxxx, in case we want in this format
                PhoneField.value = num.substring(0, 3) + "-" +
                       num.substring(3, 6) + "-" + num.substring(6);
                break;
            case '2': //Format xxx.xxx.xxxx, in case we want in this format
                PhoneField.value = num.substring(0, 3) + "." +
                       num.substring(3, 6) + "." + num.substring(6);
                break;
            default: //Format xxxxxxxxxx, in case we don't want in any format
                PhoneField.value = num;
                break;
        }
    }
}

/* Format phone number function */
function formatPhoneExt(phoneObj) {
    phoneObj.value = formatPhoneStr(phoneObj.value);
}

/* Returns a formatted phone number */
function formatPhoneStr(phoneNumber) {
    var tempPhone = phoneNumber.replace(/[^0-9xX]/g, "");
    tempPhone = tempPhone.replace(/[xX]/g, "x");
    var extension = "";
    if (tempPhone.indexOf("x") > -1) {
        extension = " " + tempPhone.substr(tempPhone.indexOf("x"));
        tempPhone = tempPhone.substr(0, tempPhone.indexOf("x"));
    }
    switch (tempPhone.length) {
        case (10):
            return tempPhone.replace(/(...)(...)(....)/g, "($1) $2-$3") + extension;
        case (11):
            if (tempPhone.substr(0, 1) == "1") {
                return tempPhone.substr(1).replace(/(...)(...)(....)/g, "($1) $2-$3") + extension;
            }
            break;
        default:
    }
    return phoneNumber;
}