//
// Copyright (c) 2000-2001 by experITy
//       All Rights Reserved
//

function Date_onChange(field)
{   
	var strInput = field.value;
	var blnResult;
	
	field.value = Date_Format(field.value);
	
	if(strInput.length > 0)
	{		
		blnResult = Date_Validate(strInput);
		
        if(blnResult == false)
        {
             HandleClientError("Invalid Date!", field);
        }        
	}
	return;
}


function Date_Validate(strInput)
{
	if(strInput.length == 0)
		return true;
	
	var month ;
	var day;
	var year;
	
	if(strInput.indexOf("/") > 0)
	{
		month = strInput.substring(0, strInput.indexOf("/"));		
		day = strInput.substring(strInput.indexOf("/") + 1, strInput.lastIndexOf("/"));
			
		if(strInput.indexOf("/") != strInput.lastIndexOf("/"))		
			year = parseInt(strInput.substring(strInput.lastIndexOf("/") + 1));				
	}
	else
	{
		if(strInput.length == 8)
		{
			month = strInput.substring(0, 2);
			day = strInput.substring(2, 4);
			year = strInput.substring(4, 8);
		}
		else
		{		
			return false;
		}		
	}
	if (month ==0 || month > 12 || day == 0 || day > getDaysInMonth(month, year))
	{		
		return false;	
	}
	return true;
}

function Date_Format(strInput)
{
    var month; 
    var day;
    var year;
    

    if(strInput.length == 0)
        return strInput;

    
    if(strInput.indexOf("/", 0) == -1) 
    {
        month = strInput.substring(0, 2) - 0;
        day = strInput.substring(2, 4) - 0;
        year = strInput.substring(4);
    }       
    else
    {
        var pos1 = strInput.indexOf("/");
        month = strInput.substring(0, pos1) - 0;
        var pos2 = strInput.lastIndexOf("/");
        day = strInput.substring(pos1 + 1, pos2) - 0;
        year = strInput.substring(pos2 + 1);
    }
    
    if (month < 10)
        month = "0" + month;
    
    if (day < 10)
        day = "0" + day;

    if (year.length < 4 && (year - 0) < 100) {
		if ((year - 0) < 10) { // For Y2k problem.
			year = (year-0) + 100;
		}
        year = (year - 0) + 1900;
    }
    return month + "/" + day + "/" + year;
}
    
function Date_onInput(inputChar, field)
{   
    var blnReturnVal = true;
    var strInput = field.value; 

	
    if(inputChar < 47 || inputChar > 57)
    {
        blnReturnVal = false;
    } 
    else if (strInput.length >= 10)
    {
		blnReturnVal = false;
    }
    else if ((inputChar == 47) && (strInput.length == 0 ))
    {   
        blnReturnVal = false;
    }
    
    return blnReturnVal;
}

function Date_Pick(butfield) 
{   
    var sfname = butfield.name;
    var sname = "dat-" + sfname;  
    var field = document.frm[sname];                
    for(index = 0; index < window.document.frm[sfname].length; index++)
    {                   
        if( window.document.frm[sfname](index) == butfield)
        {
            field = window.document.frm[sname](index);          
            break;
        }
    }               
    window.dateField = field;           
    calendar = window.open("../ClientScriptLib/NTSCP/Calendar.htm",'cal','WIDTH=225,HEIGHT=175');         
}

function getDaysInMonth(month, year)
{
    var daysInMonth= new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if (month == 2) // Test for leap year when Febrary is selected
        return ((year % 4 ==0) && (year % 100 != 0)) || (year % 400 == 0) ? 29 : 28;
    else
        return daysInMonth[month - 1];
}

