//<!--$$Revision: 3 $-->
//<!--$$Workfile: drawDates.js $-->
// Both functions accept a (optional) parameter for day or month -
// this is the VB member of the array, so one higher than javaScript.
// Left out, date defaults to five days in the future.

function getLastDayOfMonth(intMonth)
{
	var dynMonth=intMonth;

	var MonthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31)

	var daysInMonth=MonthDays[intMonth]
	
	return daysInMonth;
	
}



function addDays(myDate, intDays) {
	var numDays=intDays;
    var adjDate= new Date(myDate.getTime() + numDays*24*60*60*1000);
	//alert("Adjusted Date =" + typeof(adjDate));
	//alert("New Date =" + adjDate.getDate());
	//alert("New Month =" + adjDate.getMonth());
	//alert("New Year =" + adjDate.getFullYear());
	return adjDate
}

function reloadDays(newDepVal,newRetDay)
{
	
	var objDep=document.all.depDay;
	var objRet=document.all.retDay;
	
	var depValue=parseInt(newDepVal);
	var retValue=parseInt(newRetDay);
	
	var startDepDay;
	var startRetDay;
	var adjustDays;
	var dateToday=new Date();
	var i;
	
	var depMonth=document.forms[0].depMonth.options[document.forms[0].depMonth.selectedIndex].value;
	var depMonthDays=getLastDayOfMonth(parseInt(depMonth.substring(0,2)-1));
	
	
	var retMonth=document.forms[0].retMonth.options[document.forms[0].retMonth.selectedIndex].value;
	var retMonthDays=getLastDayOfMonth(parseInt(retMonth.substring(0,2)-1));
	
	//get first day of the departure month
	if(parseInt(depMonth.substring(0,2)-1)==dateToday.getMonth())
	{
		if(parseInt(depMonth.substring(3,7))==dateToday.getFullYear())
		{
			startDepDay=depMonthDays -(depMonthDays-parseInt(dateToday.getDate()));
		}
		else
		{
			startDepDay=1;
		}	
		
	}
	else
	{
		startDepDay=1;
	}
	
	//clear option
	objDep.options.length =0;
	for (i=startDepDay; i<(depMonthDays+1); i++)
	 {
	 
         var optDep = new Option();
         optDep.value =i;
		 if(parseInt(i)<10)
		 {
         	optDep.text ='0'+ i;
		 }
		 else
		 {
		 	optDep.text =i;
		 }
         objDep.options[objDep.options.length] = optDep;
		 
		 if(parseInt(optDep.value)==parseInt(depValue))
		 {
			optDep.selected=true;
		 }
				
      }
	
	
	//get first day of the return month
	if(parseInt(retMonth.substring(0,2)-1)==dateToday.getMonth())
	{
		if(parseInt(retMonth.substring(3,7))==dateToday.getFullYear())
		{
			startRetDay=retMonthDays -(retMonthDays-parseInt(dateToday.getDate()));
		}
		else
		{
			startRetDay=1;
		}	
		
	}
	else
	{
		startRetDay=1;
	}
	
	
	//Clear options
	objRet.options.length =0;
	for (i=startRetDay; i<(retMonthDays+1); i++)
	 {
         var optRet = new Option();
         optRet.value =i;
		 if(parseInt(i)<10)
		 {
         	optRet.text ='0'+i;
		 }
		 else
		 {
		 	 optRet.text =i;		
		 }
         objRet.options[objRet.options.length] = optRet;
		
			if(parseInt(optRet.value)==parseInt(retValue))
			{
				optRet.selected=true;
			
			}
      }
	  
}

function drawDays(prePop)
{
	var selected = "";
	var padDate = "";
	var date = "";
	if(typeof(prePop) == "undefined"){prePop = "";}
	if(prePop != "")
	{
		date = prePop;
	}
	else
	{
		var tempDate = new Date();
		date = tempDate.getDate();
		date += 5;
		tempDate.setDate(date);
		date = tempDate.getDate();
	}

	for (var i=1; i<32; i++)
	{
		padDate = i;
		padDate = padDate.toString();
		if(padDate.length == 1){padDate = "0" + padDate;}
		if(i == date){selected = "selected";}
		document.write('<option value="'+padDate+'" '+ selected +'>'+padDate+'</option>');
		selected = '';
	}
	
}

function drawMonths(prePop)
{
	var selected = "";
	var tempMonth = "";
	var tempYear = "";
	var armonths=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	if(typeof(prePop) == "undefined"){prePop = "";}
	var curDate = new Date();
	curDate.setDate(1);
	
	if(prePop != "" && prePop.length == 4)
	{
		tempMonth = prePop.substr(0,2);
		tempMonth = Number(tempMonth);
		// Decrement to concert vb array to jscript array
		tempMonth --;
		tempMonth = tempMonth.toString();
		if(tempMonth.length == 1){tempMonth = "0" + tempMonth;}
		tempYear = "20" + prePop.substr(2,2);
	}
	else
	{
		var tempDate = new Date();
		var date = tempDate.getDate();
		date += 5;
		tempDate.setDate(date);
		tempMonth = tempDate.getMonth();
		tempYear = tempDate.getFullYear();
	}
	
	for (var i=0; i<17; i++)
	{
		var curMonth = curDate.getMonth();
		var curMonthStr = curMonth + 1;
		curMonthStr = curMonthStr.toString();
		if(curMonthStr.length == 1){curMonthStr = "0" + curMonthStr;}
		var curYear = curDate.getFullYear();
		curYear = curYear.toString();
		var curYearStr = curYear.substr(2,2);
		//alert("curYear="+curYear+",tempYear="+tempYear+",CurMonth="+curMonth+", tempMonth="+tempMonth);
		if(curYear == tempYear && curMonth == tempMonth)
		{
			selected = 'selected';
		}
		document.write('<option value="'+curMonthStr+'-'+curYear+'" ' + selected + '>'+armonths[curMonth]+' '+curYearStr+'</option>');
		curDate.setMonth(curDate.getMonth() + 1);
		selected = '';
	}
}
