﻿//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 = "";
    }
}
//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
function ForceNumericInput(This, AllowDot, AllowMinus)
{
	if(arguments.length == 1)
	{
    	var s = This.value;
    	// if "-" exists then it better be the 1st character
    	var i = s.lastIndexOf("-");
    	if(i == -1)
        	return;
    	if(i != 0)
       		This.value = s.substring(0,i)+s.substring(i+1);
       	return;
    }

    var code = event.keyCode;
    switch(code)
    {
        case 8:     // backspace
        case 9:     // tab
        case 37:    // left arrow
        case 39:    // right arrow
        case 46:    // delete
            event.returnValue=true;
            return;
    }
    if(code == 189) // minus sign
    {
    	if(AllowMinus == false)
    	{
            event.returnValue=false;
            return;
        }

        // wait until the element has been updated to see if the minus is in the right spot
        var s = "ForceNumericInput(document.getElementById('"+This.id+"'))";
        setTimeout(s, 250);
        return;
    }
    if(AllowDot && code == 190)
    {
        if(This.value.indexOf(".") >= 0)
        {
        	// don't allow more than one dot
            event.returnValue=false;
            return;
        }
        event.returnValue=true;
        return;
    }
    // allow character of between 0 and 9
    if(code >= 48 && code <= 57)
    {
        event.returnValue=true;
        return;
    }
    event.returnValue=false;
}
        
//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('/')
}