﻿//Handle redisplaying modal popups
var launch = "";
function launchModal(e)
{
    launch = e;
}

function pageLoad()
{
    if (launch != "") {
        var popups = launch.split(",");
        for (i = 0; i < popups.length; i++){
	        $find(popups[i]).show();
        }
        launch = "";
    }
}

//cursor functions
function showPleaseWait(e) {
    try {
        document.getElementById(e).innerHTML = "Loading contents, please stand by..."
        }
    catch(err) { }
}

//cursor functions
function cursor_wait() {
    document.body.style.cursor = 'wait';
}

function cursor_default() {
    document.body.style.cursor = 'default';
}

//don't allow checkbox change
function disableClick(_chkBox){
    _chkBox.checked == true ? _chkBox.checked = false : _chkBox.checked = true;
}

//set default submit button
function clickButton(e, id) {
    var evt = e ? e : window.event;
    var _id = $$(id).attr("id");
    var bt = document.getElementById(_id)
    if (bt) {
        if (evt.keyCode == 13) {
            bt.click();
            return false;
        }
    }
}

//fade control In
function fadeIn(ctrl)
{
    $("#"+ctrl).fadeIn("slow");
}

//fade control out
function fadeOut(ctrl)
{
    $("#"+ctrl).fadeOut("slow");
}

//trim blanks
function trim(sString) { 
    while (sString.substring(0,1) == ' ') { 
        sString = sString.substring(1, sString.length); 
    } 
    while (sString.substring(sString.length-1, sString.length) == ' ') { 
        sString = sString.substring(0,sString.length-1); 
    } 
    return sString; 
} 
//find client Id   
function $$(id, context) {
    var el = $("#" + id, context);
    if (el.length < 1)
        el = $("[id$=_" + id + "]", context);
        
    return el;
}

//check for a valid date
function isDate(dateStr, dateName) {

    var _error = "";
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?
 
    if (matchArray == null) {
        _error = dateName + " format is invalid";
        return _error;
    }
    
    // parse date into variables
    month = matchArray[1]; 
    day = matchArray[3];
    year = matchArray[5];
    
    // check month range
    if (month < 1 || month > 12) { 
        _error = dateName + " month must be between 1 and 12";
        return _error;
    }

    if (day < 1 || day > 31) {
        _error = dateName + " day must be between 1 and 31";
        return _error;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        _error = dateName + " month " + month + " doesn't have 31 days";
        return _error;
    }

    // check for february 29th
    if (month == 2) { 
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            _error = dateName + " February " + year + " doesn`t have " + day + " days";
            return false;
        }
    }
    // date is valid
    return _error; 
}

//Numbers only (all browsers)
// onkeypress="return CheckNumericInput(event.keyCode, event.which, false, false);"
function CheckNumericInput($char, $mozChar, AllowDot, AllowMinus) {
    if ($mozChar != null) {	// Look for a Mozilla-compatible browser

        if (($mozChar >= 48 && $mozChar <= 57) || ($mozChar == 0) || ($char == 8) || ($mozChar == 13) || (AllowDot == true && $mozChar == 46) || (AllowMinus == true && $mozChar == 45)) $RetVal = true;
        else {
            $RetVal = false;
        }
    }
    else {	 // Must be an IE-compatible Browser
        if (($char >= 48 && $char <= 57) || ($char == 13) || (AllowDot == true && $char == 46) || (AllowMinus == true && $char == 45)) $RetVal = true;
        else {
            $RetVal = false;
        }
    }
    return $RetVal;
}
    
//Set selected text in list
function dpFindString(id, val)
{
    var _id = $$(id).attr("id");
    var ctl = document.getElementById(_id)

    for( var i=0, limit=ctl.options.length; i < limit; ++i )
    {
        if( ctl.options[i].innerText==val )
	        ctl.options[i].selected=true;
    }
}

//Set selected value in list
function dpFindValue(id, val)
{
    var _id = $$(id).attr("id");
    var ctl = document.getElementById(_id)

    for( var i=0, limit=ctl.options.length; i < limit; ++i )
    {
        if( ctl.options[i].value==val )
	        ctl.options[i].selected=true;
    }
}

//format date to MM/DD/YYYY
Date.prototype.toMMDDYYYYString = function () 
{   return isNaN (this) ? 'NaN' 
        : [this.getMonth() > 8 ? this.getMonth() + 1 : '0' + (this.getMonth() + 1), this.getDate() > 9 ? this.getDate() 
        : '0' + this.getDate(), this.getFullYear()].join('/')
}
