var ddMonth = false;
var ddDay = false;
var ddYear = false;
var hidIndia = false;

var dt = new Date();
DaysOfMonth = new Array(12);
DaysOfMonth[0] = 31; // January			
DaysOfMonth[1] = (dt.getFullYear() % 4 == 0) ? 29 : 28; // February
DaysOfMonth[2] = 31; // March
DaysOfMonth[3] = 30; // April
DaysOfMonth[4] = 31; // May
DaysOfMonth[5] = 30; // June
DaysOfMonth[6] = 31; // July
DaysOfMonth[7] = 31; // August
DaysOfMonth[8] = 30; // September
DaysOfMonth[9] = 31; // October
DaysOfMonth[10] = 30; // November
DaysOfMonth[11] = 31; // December	

/* Get Handles to Dropdowns in Local Variables. */
function GetHandles() {
    try {
        var dropdowns = document.getElementsByTagName("SELECT");
        for(i=0; i < dropdowns.length; i++) {
            if (dropdowns[i].id.indexOf("ddlMonth") > 0) {
                ddMonth = dropdowns[i];
            }
            else if (dropdowns[i].id.indexOf("ddlDay") > 0) {
                ddDay = dropdowns[i];                
            }
            else if (dropdowns[i].id.indexOf("ddlYear") > 0) {
                ddYear = dropdowns[i];
            }
        }    
    }
    catch (e) {
        alert('Error in loadGetHandle: ' + e);
    }    
}
function Month_onChange() {				
	var index;				
	var iDay;
	
	/* Get DropDown Handles. */
	GetHandles();	
		
	/* If Month/Day selects are valid, then continue...*/
	if ((ddMonth != false) && (ddDay != false)) {
	    try {	        
		    iDay = ddDay.options[ddDay.selectedIndex].text;							    
		    index = ddMonth.selectedIndex;	
		    				
		    /* If Month selection is valid and DayDropDown.Length is other 
			    than the number in the new position of the Month array then ...*/
		    if ((index > 0) && ((ddDay.length-1)!=DaysOfMonth[index-1])) {			
			    /* Remove all entries in the DayDropDown. */
			    for(i=ddDay.length; i >= 0; i--) {
				    ddDay.remove(i);
			    }
			    /* Add the first blank Day Entry. */
			    ddDay.options[ddDay.options.length] = new Option("---", 0);
    			
			    /* Loop until the Integer stored in the Month Array is reached.
			       Along the way, if we encounter the previously selected day,
			       then select it again.*/
			    
			    for (i=0; i < DaysOfMonth[index -1]; i++) {
				    ddDay.options[ddDay.options.length] = new Option(i+1, i+1);				
				    if (iDay == i+1) {
					    ddDay.selectedIndex = iDay;
				    }
			    }			
		    }
		 }
		 catch (e) {
		    alert('Error in ddMonth_onChange1: ' + e);
		 }
	}
}
function Day_onChange() {			
	var index;	
	try {
	    if (ddDay != undefined) {
		    index = ddDay.selectedIndex;
		    if (index != -1) {
			    return verifyDate();
		    }
	    }
    }
    catch (e) {
        alert('Error in ddDay_onChange: ' + e);
    }
}
function Year_onChange() {	
	var index;
	try {
	    if (ddYear != undefined) {
		    index = ddYear.selectedIndex;
		    if (index != -1) {
			    DaysOfMonth[1] = (ddYear.options(index).text%4==0) ? 29 : 28; //[Re]set February
			    return verifyDate();
		    }
	    }
    }
    catch (e) {
        alert('Error in ddYear_onChange: ' + e);
    }
}
function verifyDate() {
    return true;
}

/*
FormatDateTime(datetime, FormatType) : Returns an expression formatted
                                       as a date or time
========================================================================
	 FomatType takes the following values
		1 - General Date = Friday, October 30, 1998
		2 - Typical Date = 10/30/98
		3 - Standard Time = 6:31 PM
		4 - Military Time = 18:31
*/
function FormatDateTime(datetime, FormatType)
{
	var strDate = new String(datetime);

	if (strDate.toUpperCase() == "NOW") {
		var myDate = new Date();
		strDate = String(myDate);
	} else {
		var myDate = new Date(datetime);
		strDate = String(myDate);
	}


	// Get the date variable parts
	var Day = new String(strDate.substring(0, 3));
//	if (Day == "Sun") Day = "Sunday";
//	if (Day == "Mon") Day = "Monday";
//	if (Day == "Tue") Day = "Tuesday";
//	if (Day == "Wed") Day = "Wednesday";
//	if (Day == "Thu") Day = "Thursday";
//	if (Day == "Fri") Day = "Friday";
//	if (Day == "Sat") Day = "Saturday";	
    
	
	var Month = new String(strDate.substring(4,7)), MonthNumber = 0;
	if (Month == "Jan") { Month = "January"; MonthNumber = 1; }
	if (Month == "Feb") { Month = "February"; MonthNumber = 2; }
	if (Month == "Mar") { Month = "March"; MonthNumber = 3; }
	if (Month == "Apr") { Month = "April"; MonthNumber = 4; }
	if (Month == "May") { Month = "May"; MonthNumber = 5; }
	if (Month == "Jun") { Month = "June"; MonthNumber = 6; }
	if (Month == "Jul") { Month = "July"; MonthNumber = 7; }
	if (Month == "Aug") { Month = "August"; MonthNumber = 8; }
	if (Month == "Sep") { Month = "September"; MonthNumber = 9; }
	if (Month == "Oct") { Month = "October"; MonthNumber = 10; }
	if (Month == "Nov") { Month = "November"; MonthNumber = 11; }
	if (Month == "Dec") { Month = "December"; MonthNumber = 12; }
	
	var curPos = 11;
	var MonthDay = new String(strDate.substring(8,10));
	if (MonthDay.charAt(1) == " ") {
		MonthDay = "0" + MonthDay.charAt(0);
		curPos--;
	}	
	
	var MilitaryTime = new String(strDate.substring(curPos, curPos + 5));
	
	var Year = new String(strDate.substring(strDate.length - 4, strDate.length));	
	
	// Format Type decision time!
	if (FormatType == 1)
		strDate = Day + ", " + Month + " " + MonthDay + ", " + Year;
	else if (FormatType == 2)
		strDate = MonthNumber + "/" + MonthDay + "/" + Year.substring(2,4);
	else if (FormatType == 3) {
		var AMPM = MilitaryTime.substring(0,2) >= 12 && MilitaryTime.substring(0,2) != "24" ? " PM" : " AM";
		if (MilitaryTime.substring(0,2) > 12)
			strDate = (MilitaryTime.substring(0,2) - 12) + ":" + MilitaryTime.substring(3,MilitaryTime.length) + AMPM;
		else {
			if (MilitaryTime.substring(0,2) < 10)
				strDate = MilitaryTime.substring(1,MilitaryTime.length) + AMPM;
			else
				strDate = MilitaryTime + AMPM;
		}
	}	
	else if (FormatType == 4) {
		strDate = MilitaryTime;
    }
    else if (FormatType == 5) {
        strDate = MonthNumber + "/" + MonthDay + "/" + Year.substring(2,4) + " " + MilitaryTime;
    }

	return strDate;
}
