function ValidDate(day, mth, yearyear) {
	if ( ((mth == "4") || (mth == "6") || (mth == "9") || (mth == "11")) && (day > 30) ) { 
		alert("Selected month has only 30 days.");
		return false;	
	} else if ((mth == "2") && (yearyear % 4 > 0) && (day > 28)) {
		alert("February for " + yearyear + " has only 28 days.");
		return false;
	} else if ((mth == "2") && (day > 29)) {
		alert("February for " + yearyear + " has only 29 days.");
		return false;
	} else {
  		return true;
	}
}

function determineCheckOutDate() {
	var iNumberOfNights = document.step_1_form.numnights.value;
	
	var INyr = document.step_1_form.INyr.options[document.step_1_form.INyr.selectedIndex].value;
	var INmo = document.step_1_form.INmo.options[document.step_1_form.INmo.selectedIndex].value;
	var INdy = document.step_1_form.INdy.options[document.step_1_form.INdy.selectedIndex].value;
	
	var oOUTyr = document.step_1_form.OUTyr;
	
	var oCheckInDate = new Date(INyr,INmo-1,INdy);
	var oCheckOutDate = new Date(INyr,INmo-1,INdy);
	
	var iCtr = 0;
	
	//set the oCheckInDate value based on the check-in date form value
	oCheckOutDate.setDate(parseInt(oCheckInDate.getDate()) + parseInt(iNumberOfNights));
	
	//set the drop-down lists selected values for Check-out date fields
	document.step_1_form.OUTmo.options.selectedIndex = oCheckOutDate.getMonth();
	document.step_1_form.OUTdy.options.selectedIndex = oCheckOutDate.getDate()-1;
	
	for(iCtr=0;iCtr<=oOUTyr.length;iCtr++) {
		if (oOUTyr.options[iCtr].value==oCheckOutDate.getFullYear()) {
			oOUTyr.options.selectedIndex = iCtr;
			break;
		}
	}
	
}

function recalculateNumberOfNights() {
	var oINyr = document.step_1_form.INyr;
	var oINmo = document.step_1_form.INmo;
	var oINdy = document.step_1_form.INdy;
	
	var oOUTyr = document.step_1_form.OUTyr;
	var oOUTmo = document.step_1_form.OUTmo;
	var oOUTdy = document.step_1_form.OUTdy;
	
	var oNumNights = document.step_1_form.numnights;
	
	var INyr = parseInt(oINyr.options[oINyr.selectedIndex].value);
	var INmo = parseInt(oINmo.options[oINmo.selectedIndex].value);
	var INdy = parseInt(oINdy.options[oINdy.selectedIndex].value);
	
	var OUTyr = parseInt(oOUTyr.options[oOUTyr.selectedIndex].value);
	var OUTmo = parseInt(oOUTmo.options[oOUTmo.selectedIndex].value);
	var OUTdy = parseInt(oOUTdy.options[oOUTdy.selectedIndex].value);
	
	var oCheckInDate = new Date(INyr,INmo-1,INdy);
	var oCheckOutDate = new Date(OUTyr,OUTmo-1,OUTdy);
	var oCurrentDate = new Date();
	
	var iDiff, iInSec, iMin, iHr, iDay;
	
	iDiff = Date.parse(oCheckOutDate.toString()) - Date.parse(oCheckInDate.toString());
	
	if (iDiff > 0) {
		iInSec = iDiff / 1000;
		iMin = iInSec / 60;
		iHr = iMin / 60;
		iDay = iHr / 24;
		
		oNumNights.value = iDay;	
	} else {
		oNumNights.value = 1;
		determineCheckOutDate();
	}
}

function ResValidate() { 
	var Submit = true;

	CurrentDate = new Date()
	Currday = CurrentDate.getDate();
	Currmonth = CurrentDate.getMonth();
	Curryear = CurrentDate.getFullYear();

	if (!isAllowedStringValue(document.step_1_form.gfirstname.value)) {
		alert('Please specify guest firstname.');
		document.step_1_form.gfirstname.focus();
		document.step_1_form.gfirstname.select();
		return false;
	}

	if (!isAllowedStringValue(document.step_1_form.glastname.value)) {
		alert('Please specify guest lastname.');
		document.step_1_form.glastname.focus();
		document.step_1_form.glastname.select();
		return false;
	}

	if (!isValidEmail(trimSpaces(document.step_1_form.email.value))) {
		document.step_1_form.email.value = trimSpaces(document.step_1_form.email.value);
		document.step_1_form.email.focus();
		document.step_1_form.email.select();
		return false;
	} else {
		document.step_1_form.email.value = trimSpaces(document.step_1_form.email.value);
	}

	if(Currmonth == 0)   	 monthname = "1";
	else if(Currmonth == 1)  monthname = "2";
	else if(Currmonth == 2)  monthname = "3";
	else if(Currmonth == 3)  monthname = "4";
	else if(Currmonth == 4)  monthname = "5";
	else if(Currmonth == 5)  monthname = "6";
	else if(Currmonth == 6)  monthname = "7";
	else if(Currmonth == 7)  monthname = "8";
	else if(Currmonth == 8)  monthname = "9";
	else if(Currmonth == 9)  monthname = "10";
	else if(Currmonth== 10)  monthname = "11";
	else if(Currmonth == 11) monthname = "12";

	//var month_in = document.step_1_form.INmo.options[document.step_1_form.INmo.selectedIndex].value;
	var month_in = document.step_1_form.INmo.selectedIndex + 1;
	Check_in_month = parseInt(month_in, 10);

	//var month_out = document.step_1_form.OUTmo.options[document.step_1_form.OUTmo.selectedIndex].value;
	var month_out = document.step_1_form.OUTmo.selectedIndex + 1;
	Check_out_month = parseInt(month_out, 10);	

	if (!ValidDate(document.step_1_form.INdy.options[document.step_1_form.INdy.selectedIndex].text,
				document.step_1_form.INmo.options[document.step_1_form.INmo.selectedIndex].value,
					document.step_1_form.INyr.options[document.step_1_form.INyr.selectedIndex].text
			) )
			     
	{
		document.step_1_form.INdy.focus();
		Submit = false;
		
	}

	else if (!ValidDate(document.step_1_form.OUTdy.options[document.step_1_form.OUTdy.selectedIndex].text,
					document.step_1_form.OUTmo.options[document.step_1_form.OUTmo.selectedIndex].value,
						document.step_1_form.OUTyr.options[document.step_1_form.OUTyr.selectedIndex].text)
				)
			     
	{
		document.step_1_form.OUTdy.focus();
		Submit = false;
		
	}



	else if (parseInt(document.step_1_form.INyr.options[document.step_1_form.INyr.selectedIndex].text) < parseInt(Curryear))
	{
		alert("Selected check in date cannot be less than current date");
		document.step_1_form.INyr.focus();
		Submit = false;

	} 

	else if(  (parseInt(document.step_1_form.INyr.options[document.step_1_form.INyr.selectedIndex].text) == Curryear) &&
				(Check_in_month < Currmonth+1) ) 
	{  
		alert("Selected check in date cannot be less than current date");
		document.step_1_form.INdy.focus();
		Submit = false;

	}

	else if((parseInt(document.step_1_form.INyr.options[document.step_1_form.INyr.selectedIndex].text) == Curryear) &&
			(Check_in_month == Currmonth+1) &&
			(parseInt(document.step_1_form.INdy.options[document.step_1_form.INdy.selectedIndex].text) < Currday) )
	{  
		alert("Selected check in date cannot be less than current date");
		document.step_1_form.INdy.focus();
		Submit = false;

	}


	else if (parseInt(document.step_1_form.OUTyr.options[document.step_1_form.OUTyr.selectedIndex].text) < parseInt(Curryear))
	{
		alert("Selected check out date cannot be less than current date");
		document.step_1_form.OUTyr.focus();
		Submit = false;

	} 

	else if(  (parseInt(document.step_1_form.OUTyr.options[document.step_1_form.OUTyr.selectedIndex].text) == Curryear) &&
				(Check_out_month < Currmonth+1) ) 
	{  
		alert("Selected check out date cannot be less than current date");
		document.step_1_form.OUTdy.focus();
		Submit = false;

	}

	else if((parseInt(document.step_1_form.OUTyr.options[document.step_1_form.OUTyr.selectedIndex].text) == Curryear) &&
			(Check_out_month == Currmonth+1) &&
			(parseInt(document.step_1_form.OUTdy.options[document.step_1_form.OUTdy.selectedIndex].text) < Currday) )
	{  
		alert("Selected check out date cannot be less than current date");
		document.step_1_form.OUTdy.focus();
		Submit = false;

	}
	 
	else if ( parseInt(document.step_1_form.INyr.options[document.step_1_form.INyr.selectedIndex].text) > parseInt(document.step_1_form.OUTyr.options[document.step_1_form.OUTyr.selectedIndex].text)) 
	          
	{
		alert("Check-In date cannot be greater than Check-Out date");
		document.step_1_form.OUTdy.focus();
		Submit = false;

	}

		else if(((parseInt(document.step_1_form.INyr.options[document.step_1_form.INyr.selectedIndex].text) == parseInt(document.step_1_form.OUTyr.options[document.step_1_form.OUTyr.selectedIndex].text)) &&
		(Check_in_month > Check_out_month)))  
	{
		alert("Check-In date cannot be greater than Check-Out date");
		document.step_1_form.OUTdy.focus();
		Submit = false;

	}

	else if(  (parseInt(document.step_1_form.INyr.options[document.step_1_form.INyr.selectedIndex].text) == parseInt(document.step_1_form.OUTyr.options[document.step_1_form.OUTyr.selectedIndex].text)) &&
		(Check_in_month == Check_out_month) &&
			(parseInt(document.step_1_form.INdy.options[document.step_1_form.INdy.selectedIndex].text) >= parseInt(document.step_1_form.OUTdy.options[document.step_1_form.OUTdy.selectedIndex].text)))
	{
		alert("Check-In date cannot be greater than or equal to Check-Out date");
		document.step_1_form.OUTdy.focus();
		Submit = false;

	}

	return Submit;
}

