﻿/* From forms.js - delete keypress supression... */
var tmrEventLoop;

function setKeyPress() {
    if (document.body != null) {
        if (navigator.appName == 'Netscape') {
            window.captureEvents(Event.KEYPRESS);
            window.onkeypress = keyPressEvent;
        }
        else {
            document.body.onkeypress = Function('keyPressEvent(null);');
            document.body.onkeydown = Function('keyPressEvent(null);');
            document.body.onkeyup = Function('keyPressEvent(null);');
        }
    }
    else
        tmrEventLoop = setTimeout('setKeyPress()', 100);
}
function keyPressEvent(e) {
    var evt = e || window.event;
    var ele = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);

    if ((evt.keyCode == 13) || ((evt.keyCode == 8) && (ele.type != 'text') && (ele.type != 'textarea'))) {
        if (evt.preventDefault)
            evt.preventDefault();
        else {
            evt.returnValue = false;
            evt.cancel = true;
        }
    }
}

tmrEventLoop = setTimeout('setKeyPress()', 100);

/* From forms.js - common functions */
function highlight(ctl) {
    var sImg = ctl.src;
    if (sImg.length > 8) {
        if (sImg.substring(sImg.length - 8).toLowerCase() == '_off.gif')
            ctl.src = sImg.substring(0, sImg.length - 8) + '_over.gif';
    }
}
function highlightById(sId) {
    highlight(document.getElementById(sId));
}

function restore(ctl) {
    var sImg = ctl.src;
    if (sImg.length > 9) {
        if (sImg.substring(sImg.length - 9).toLowerCase() == '_over.gif')
            ctl.src = sImg.substring(0, sImg.length - 9) + '_off.gif';
    }
}
function restoreById(sId) {
    restore(document.getElementById(sId));
}

function showPopup(sPage, sWin, sOptions) {
    var oWin = window.open(sPage, sWin, sOptions);
    oWin.focus();
}

function setClass(sEle, sClass) {
    var ele = document.getElementById(sEle);
    if (ele) ele.className = sClass;
}

/* From cookies.js - cookie functions */
function setCookie(cookie_name, cookie_value, cookie_life, cookie_path) {
    var today = new Date()
    var expiry = new Date(today.getTime() + cookie_life * 24 * 60 * 60 * 1000)
    if (cookie_value != null && cookie_value != "") {
        var cookie_string = cookie_name + "=" + escape(cookie_value)
        if (cookie_life) { cookie_string += "; expires=" + expiry.toGMTString() }
        if (cookie_path) { cookie_string += "; path=" + cookie_path }
        document.cookie = cookie_string
    }
}

function getCookie() {
    var name = getCookie.arguments[0];
    var index = document.cookie.indexOf(name + "=")
    if (index == -1) { return getCookie.arguments.length == 2 ? getCookie.arguments[1] : null; }
    index = document.cookie.indexOf("=", index) + 1
    var end_string = document.cookie.indexOf(";", index)
    if (end_string == -1) { end_string = document.cookie.length }
    return unescape(document.cookie.substring(index, end_string))
}

function deleteCookie(cookie_name, cookie_path) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);  // This cookie is history
    if (cookie_path == null)
        document.cookie = cookie_name + "=; expires=" + exp.toGMTString();
    else
        document.cookie = cookie_name + "=; expires=" + exp.toGMTString() + "; path=" + cookie_path;
}


/* From util.js - common text/date functions */
var arrMonths = new Array(12);
arrMonths[0] = 'January';
arrMonths[1] = 'February';
arrMonths[2] = 'March';
arrMonths[3] = 'April';
arrMonths[4] = 'May';
arrMonths[5] = 'June';
arrMonths[6] = 'July';
arrMonths[7] = 'August';
arrMonths[8] = 'September';
arrMonths[9] = 'October';
arrMonths[10] = 'November';
arrMonths[11] = 'December';

var arrWeekdays = new Array(7);
arrWeekdays[0] = 'Sunday';
arrWeekdays[1] = 'Monday';
arrWeekdays[2] = 'Tuesday';
arrWeekdays[3] = 'Wednesday';
arrWeekdays[4] = 'Thursday';
arrWeekdays[5] = 'Friday';
arrWeekdays[6] = 'Saturday';

function txtToDate(txt) {
    if (txt.value.length > 0) {
        txt.value = toDateStr(txt.value);
    }
}

function toDate() {
    switch (toDate.arguments.length) {
        case 1:
            return strToDate(toDate.arguments[0]);
        case 3:
            return dmyToDate(toDate.arguments[0], toDate.arguments[1], toDate.arguments[2]);
        default:
            return null;
    }
}

function strToDate(sValue) {
    var nDateParts = toDateParts(sValue);

    return toDate(nDateParts[0], nDateParts[1], nDateParts[2]);
}
function toDateStr(sValue) {
    var nDateParts = toDateParts(sValue);

    if ((nDateParts[0]) && (nDateParts[1]) && (nDateParts[2]) && (isDate(nDateParts[0], nDateParts[1], nDateParts[2])))
        return nDateParts[0] + '/' + nDateParts[1] + '/' + nDateParts[2];
    else
        return 'INVALID';
}

function toDateParts(sValue) {
    sValue = sValue.replace(/\\|\/| |,|\||#|-|_/g, '.');
    var sChar;
    var sPart = '';
    var nPart = 0;
    var nDateParts = [null, null, null];
    var bUsePart;
    //var bIgnore = false; 

    for (var nn = 0; nn < sValue.length; nn++) {
        sChar = sValue.substring(nn, nn + 1);
        if (sChar == '.')
            bUsePart = (sPart.length > 0);
        else {
            sPart = sPart + sChar;
            if (isNaN(sChar)) {
                if (nn < sValue.length - 1) {
                    var sChar2 = sValue.substring(nn + 1, nn + 2);
                    if (!isNaN(sChar2)) bUsePart = true;
                }
                else
                    bUsePart = true;
            }
            else if ((nPart < 2) && (sPart.length == 2))
                bUsePart = true;
        }

        if (bUsePart) {
            var bIgnore = false;

            if (isNaN(sPart)) {
                sPart = sPart.toLowerCase();

                // ignores a weekday part - eg thu
                for (var nDay = 0; (nDateParts[nPart] == null) && (nDay < arrWeekdays.length); nDay++) {
                    if (sPart == arrWeekdays[nDay].toLowerCase().substring(0, sPart.length))
                        bIgnore = true;
                }

                if (!bIgnore) {
                    for (var nMonth = 0; (nDateParts[nPart] == null) && (nMonth < arrMonths.length); nMonth++) {
                        if (sPart == arrMonths[nMonth].toLowerCase().substring(0, sPart.length))
                            nDateParts[1] = nMonth + 1;
                    }
                }
            }
            else
                nDateParts[nPart] = sPart;

            // Move to next unpopulated part 
            while ((nPart < nDateParts.length) && (nDateParts[nPart] != null))
                nPart++;

            sPart = '';
            bUsePart = false;
        }
    }

    if (sPart.length > 0) nDateParts[nPart] = sPart;

    // Set day and month values to be of 2 length!
    if ((nDateParts[0]) && (nDateParts[0].length < 2)) nDateParts[0] = '0' + nDateParts[0];
    if ((nDateParts[1]) && (nDateParts[1].length < 2)) nDateParts[1] = '0' + nDateParts[1];

    if (!nDateParts[2]) {
        var dtNow = new Date();
        nDateParts[2] = dtNow.getFullYear();
    }
    else if (nDateParts[2].length < 4) {
        var dtNow = new Date();
        nDateParts[2] = new String(dtNow.getFullYear()).substring(0, 4 - nDateParts[2].length) + nDateParts[2];
    }

    return nDateParts;

}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isDate(day, month, year) {
    // checks if date passed is valid
    // will accept dates in following format:
    // isDate(dd,mm,ccyy), or
    // isDate(dd,mm) - which defaults to the current year, or
    // isDate(dd) - which defaults to the current month and year.
    // Note, if passed the month must be between 1 and 12, and the
    // year in ccyy format.

    var today = new Date();
    year = ((!year) ? y2k(today.getFullYear()) : year);
    month = ((!month) ? today.getMonth() : month - 1);
    if (!day) return false
    var test = new Date(year, month, day);
    if ((y2k(test.getFullYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()))
        return true;
    else
        return false
}

function dmyToDate(day, month, year) {
    // checks if date passed is valid
    // will accept dates in following format:
    // toDate(dd,mm,ccyy), or
    // toDate(dd,mm) - which defaults to the current year, or
    // toDate(dd) - which defaults to the current month and year.
    // Note, if passed the month must be between 1 and 12, and the
    // year in ccyy format.

    var today = new Date();
    year = ((!year) ? y2k(today.getYear()) : year);
    month = ((!month) ? today.getMonth() : month - 1);
    if (!day) return false
    var test = new Date(year, month, day);
    if ((y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()))
        return test;
    else
        return null;
}

function ifNotBlank(oValue, sPrefix, sSuffix) {
    if ((oValue) && (oValue != ''))
        return sPrefix + oValue + sSuffix;
    else
        return '';
}
function ifBlank(oValue, sReturn) {
    if ((oValue) && (oValue != ''))
        return oValue;
    else
        return sReturn;
}

function findParentWin(oWin, sTitle) {
    var oWin = top.opener;

    // recurse up looking for the top window which (hopefully) will hold the cache!
    while ((oWin) && (oWin.document.title != sTitle)) {
        oWin = oWin.top.opener;
    }

    return oWin;
}

function formatAsMoney(mnt) {
    mnt -= 0;
    // do *1000 then / 10 because javascript seemed to be converting x.xx5 to xx.xx4999999999995!!!
    mnt = (Math.round((mnt * 1000) / 10)) / 100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00'
				: ((mnt * 10 == Math.floor(mnt * 10)) ?
							mnt + '0' : mnt);
}
function validate(sText, sValidationChars, bAllow) {
    if (bAllow) {
        for (nn = 0; nn < sText.length; nn++) {
            if (sValidationChars.indexOf(sText.charAt(nn)) < 0) return false;
        }
    }
    else {
        for (nn = 0; nn < sText.length; nn++) {
            if (sValidationChars.indexOf(sText.charAt(nn)) >= 0) return false;
        }
    }

    return true;
}
function isEmail(sAddress) {
    return sAddress.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
}
function isWholeNumber(sText) {
    return sText.match(/^\d+$/);
}
function isDecimalNumber(sText) {
    return sText.match(/[-+]?([0-9]*\.[0-9]+|[0-9]+)/);
}
//function isMoney(sText) {
// todo return sText.match(/^\d+$/);
//}

// funtion to check if a value is between two other values
// if null value it automatically returns false
// if nulls provided for either min or max looks for which ever can be matched and returns the value suits the min and max
function isBetween(value, min, max) {
    if (!value) return false;
    if (min && max) return ((value >= min) && (value <= max));
    if (min) return (value >= min);
    if (max) return (value >= max);
    return true;
}

/* From search_box.js - common control functions */
function comboNonBlankText(cbo, oDefault) {
    if ((cbo == null) || (cbo.selectedIndex == -1))
        return oDefault;
    else
        return (cbo.options[cbo.selectedIndex].value == '' ? oDefault : cbo.options[cbo.selectedIndex].text);
}
function comboNonBlankValue(cbo, oDefault) {
    if ((cbo == null) || (cbo.selectedIndex == -1))
        return oDefault;
    else
        return (cbo.options[cbo.selectedIndex].value == '' ? oDefault : cbo.options[cbo.selectedIndex].value);
}