function trimString(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}
function validate() {
	var errmsg = "";
	var errhdr = "";
	var errftr = "";

	if (trimString(document.forms.getElementById('chkAvailabilityForm').dateStart.value) == '') {
		errmsg = errmsg +  "   > You must enter a Arrival Date\n";
	}
	if (trimString(document.forms.getElementById('chkAvailabilityForm').dateEnd.value) == '') {
		errmsg = errmsg +  "   > You must enter a Departure Date\n";
	}
	

	if (parseFloat(document.forms.getElementById('chkAvailabilityForm').children.value-0)+(document.forms.getElementById('chkAvailabilityForm').adults.value-0) > '12') {
		errmsg = errmsg +  "   > The maximum number of guests for this chalet is 12 \n";
	}
	if (errmsg != "") {
		errhdr = "The following fields are required:\n\n";
		errftr = "\nClick OK to complete the form.";
		errmsg = errhdr + errmsg + errftr;
		alert(errmsg);
		return false;
	} else {
		return true;
	}
}
function sendToReservation()
		{
			if ( validateForm() )
			{
			 	var beFilterName = document.getElementById('chkAvailabilityForm').beFilterName.value;
			 	var beFilterValue = document.getElementById('chkAvailabilityForm').beFilterVal.value;			 	

				if(document.getElementById('chkAvailabilityForm').openInNewWindow.value == "1")
				{
					if (document.getElementById('chkAvailabilityForm').adults.value != null)
						mine = window.open("http://clients.innroad.com/"+ document.getElementById('chkAvailabilityForm').clientCode.value +"/logon.aspx?adults="+document.getElementById('chkAvailabilityForm').adults.value +"&children="+document.getElementById('chkAvailabilityForm').children.value+"&dateStart="+document.getElementById('chkAvailabilityForm').dateStart.value+"&dateEnd="+document.getElementById('chkAvailabilityForm').dateEnd.value+"&"+beFilterName+"="+beFilterValue);
					else
						mine = window.open("http://clients.innroad.com/"+ document.getElementById('chkAvailabilityForm').clientCode.value);

					window.document.location.replace("index.html");
				}
				else
				{
				  if (document.getElementById('chkAvailabilityForm').useIFrame.value == "1")
				  {
					document.getElementById('chkAvailabilityForm').submit();
				  }
				  else
				  {
					// If iFrame is not exist than opening in the same window.
					var mine;

					if (document.getElementById('chkAvailabilityForm').adults.value != null)
						window.document.location.replace("http://clients.innroad.com/"+document.getElementById('chkAvailabilityForm').clientCode.value+"/logon.aspx?adults="+document.getElementById('chkAvailabilityForm').adults.value +"&children="+document.getElementById('chkAvailabilityForm').children.value+"&dateStart="+document.getElementById('chkAvailabilityForm').dateStart.value+"&dateEnd="+document.getElementById('chkAvailabilityForm').dateEnd.value+"&"+beFilterName+"="+beFilterValue);
					else
						window.document.location.replace("http://clients.innroad.com/"+document.getElementById('chkAvailabilityForm').clientCode.value);

				  }
				}
			}
		}

		function validateForm()
		{		
		  if (document.getElementById('chkAvailabilityForm').beFilterVal.value == '')
		  {
		  	if (document.getElementById('chkAvailabilityForm').beFilterConfig.value == 'town')
		  		alert("Please select a Town");
		  	if (document.getElementById('chkAvailabilityForm').beFilterConfig.value == 'property')
				alert("Please select a Property");
		  	if (document.getElementById('chkAvailabilityForm').beFilterConfig.value == 'roomclass')
				alert("Please select a Room Class");
		  	
		  	document.getElementById('chkAvailabilityForm').beFilterVal.focus();
		    	return false;
		  }
		  
		  if ( !validateNotEmpty(trim(document.getElementById('chkAvailabilityForm').dateStart.value)) )
		  {
		    alert("Please enter Checkin Date");
		    document.getElementById('chkAvailabilityForm').dateStart.focus();
		    return false;
		  }

		  if ( !validateNotEmpty(trim(document.getElementById('chkAvailabilityForm').dateEnd.value)) )
		  {
		    alert("Please enter Checkout Date");
		    document.getElementById('chkAvailabilityForm').dateEnd.focus();
		    return false;
		  }

		  if ( validateDateOffset() )
		  {
			if ( document.getElementById('chkAvailabilityForm').adults.value == '0' )
			{
			    alert("Please select adults");
			    document.getElementById('chkAvailabilityForm').adults.focus();
			    return false;
			}
			else
			    return true;
		  }
		  else
			return false;		
		}

		function validateDateOffset()
		{
		  var returnVal = true;
		  var startDateTime = new Date(document.getElementById('chkAvailabilityForm').dateStart.value).getTime();
		  var endDateTime = new Date(document.getElementById('chkAvailabilityForm').dateEnd.value).getTime();
		  var curDate = new Date();

		  var y = curDate.getYear();

		  if(navigator.appName=='Netscape')
		  {
			y = y + 1900;   
		  }
		  curDate = new Date(y, curDate.getMonth(), parseInt(curDate.getDate()) );	
		  var curDateTime = curDate.getTime();

		  var one_day = 1000*60*60*24;		

		  if (startDateTime >= endDateTime )
		  {
			    alert("Please enter valid Check Out date");
			    returnVal = false;
		  }
		  else
		  {
			   if (document.getElementById('chkAvailabilityForm').daysOffset.value > 0)
			   {
				    if (startDateTime > curDateTime)
				    {
					     if ( Math.ceil((startDateTime - curDateTime)/(one_day)) < document.getElementById('chkAvailabilityForm').daysOffset.value )
					     {
						alert("Please enter Check In date which is greater than " + document.getElementById('chkAvailabilityForm').daysOffset.value + " from now");
						returnVal = false;
					     }
				    }
				    else
				    {
					alert("Please enter Check In date which is greater than " + document.getElementById('chkAvailabilityForm').daysOffset.value + " from now");
					returnVal = false;
				    }
			   }
		  } 
		  return returnVal;
		}

		function populateDates()			
		{
		  var curDate = new Date();
		  var y = curDate.getYear();
		  
		  if(navigator.appName == 'Netscape')
		  {
			y = y + 1900;   
		  }

		  curDate = new Date(y, curDate.getMonth(), parseInt(curDate.getDate())+parseInt(document.getElementById('chkAvailabilityForm').daysOffset.value) );				  
		  document.getElementById('chkAvailabilityForm').dateStart.value = formatDate(curDate, "MM/dd/yyyy");
		  curDate = new Date(y, curDate.getMonth(), parseInt(curDate.getDate())+1 );
		  document.getElementById('chkAvailabilityForm').dateEnd.value = formatDate(curDate, "MM/dd/yyyy");
		  setNights();  
		}

		function setCheckoutDate()
		{
			setCheckoutDateParams(document.getElementById('chkAvailabilityForm').dateStart,document.getElementById('chkAvailabilityForm').noOfNights,document.getElementById('chkAvailabilityForm').dateEnd);
		}

		function setCheckoutDateParams(checkinDate, noOfNights, checkOutDate)
		{
			if (checkinDate.value != '' && isNumericVal(noOfNights.value, 0) && noOfNights.value != '')
			{
				var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", 
										"Sep", "Oct", "Nov", "Dec");

				var myDate=new Date(checkinDate.value)
				var nights = noOfNights.value;
				myDate.setDate(myDate.getDate()+parseInt(nights))
				var month=(myDate.getMonth()+1)
				
					if (month==1) monthName=("01");
					else if (month==2) monthName=("02");
					else if (month==3) monthName=("03");
					else if (month==4) monthName=("04");
					else if (month==5) monthName=("05");
					else if (month==6) monthName=("06");
					else if (month==7) monthName=("07");
					else if (month==8) monthName=("08");
					else if (month==9) monthName=("09");
					else if (month==10) monthName=("10");
					else if (month==11) monthName=("11");
					else monthName=("12");						
				
				checkOutDate.value = monthName + "/" + myDate.getDate() + "/" + myDate.getFullYear();

			}
		}

		function isNumericVal(c, varTest) 
		{
			//allows alhabetic and space characters
			var letters="-0123456789";
			var LETTERS="-0123456789";

			var index1 = c.indexOf("-");
			var index2 = c.indexOf("-", index1 + 1);
			var index3 = c.indexOf("-", index2 + 1);

			if (varTest == 1)
			{
				if (!((index1 == 3) && (index2 == 7) && (index3 == -1)))
				{
					return false;
				}
			}

			if(c.length>1) 
			{
				for(j=0;j<c.length;j++) 
				{
				// call isAlphaCell recursively for each character
				alpha=isNumericVal(c.substring(j,j+1), 2);	// 2nd time entry
				if(!alpha) return alpha;
				}
				return alpha;
			}
			else 
			{
				// if c is alpha return true
				if(letters.indexOf(c)>=0 || LETTERS.indexOf(c)>=0) return true;
				return false;
			}
		}

		function setNights()
		{
			return setNightsParams(document.getElementById('chkAvailabilityForm').dateStart,document.getElementById('chkAvailabilityForm').noOfNights,document.getElementById('chkAvailabilityForm').dateEnd);
		}

		function setNightsParams(checkinDate, noOfNights, checkOutDate)
		{
			var one_day=1000*60*60*24
			if (checkinDate.value != '' && checkOutDate.value != '')
			{

				var totNights = new Number();
				var zero = 0;

				var stDate = new Date(checkinDate.value);
				var endDate = new Date(checkOutDate.value);

				stDate.setHours(0);
				stDate.setMinutes(0);
				stDate.setSeconds(0);

				stDate.setHours(0);
				endDate.setMinutes(0);
				endDate.setSeconds(0);

				totNights=Math.round((endDate.getTime() - stDate.getTime())/one_day);
				
				if(totNights < 2)
				{				
					alert('Minimum 2 night stay required');
					return false;
				}
				else
				{
					noOfNights.value = totNights ;
					return true;
				}
			}
		}		
		
		function fillForm()
		{
			if (document.getElementById('chkAvailabilityForm').beFilterConfig.value == 'town')
			{
				document.getElementById('chkAvailabilityForm').beFilterName.value="townID";
				document.getElementById("beFilterLabel").innerHTML="Town:";
			}

			if (document.getElementById('chkAvailabilityForm').beFilterConfig.value == 'property')
			{
				document.getElementById('chkAvailabilityForm').beFilterName.value="propertyID";
				document.getElementById("beFilterLabel").innerHTML="Property:";
			}

			if (document.getElementById('chkAvailabilityForm').beFilterConfig.value == 'roomClass')
			{
				document.getElementById('chkAvailabilityForm').beFilterName.value="roomClassID";
				document.getElementById("beFilterLabel").innerHTML="Room Class:";
			}			

			if (document.getElementById('chkAvailabilityForm').showHideBEFilter.value == 'hide')
			{
				document.getElementById("beFilterTR").style.display = 'none';
			}

			if (document.getElementById('chkAvailabilityForm').showHideBEFilter.value == 'show')
			{
				document.getElementById("beFilterTR").style.display = '';
			}			

			if (document.getElementById('chkAvailabilityForm').showHideChildren.value == 'hide')
			{
				document.getElementById("beChildrenTD").style.display = 'none';
				document.getElementById("beChildrenHelpTD").style.display = 'none';				
			}
			if (document.getElementById('chkAvailabilityForm').showHideChildren.value == 'show')
			{
				document.getElementById("beChildrenTD").style.display = '';
				document.getElementById("beChildrenHelpTD").style.display = '';
			}

			if (document.getElementById('chkAvailabilityForm').showHideNights.value == 'hide')
			{
				document.getElementById("beNightsTR").style.display = 'none';
			}
			if (document.getElementById('chkAvailabilityForm').showHideNights.value == 'show')
			{
				document.getElementById("beNightsTR").style.display = '';
			}			
		}		
