

function validateSearchForm(theForm){
	var dtStartDate = theForm.optStartYear.value + "-" + theForm.optStartMonth.options[theForm.optStartMonth.selectedIndex].value + "-" + theForm.optStartDay.value;
	var msg = "";
	
	if (dtStartDate != "--") {
		if (!isValidDate(dtStartDate)) {return false;}
		
		if (msg!=""){
			alert(msg);
			return false;
		}
	}
	return true;		
}

function addDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}

function populateDateControls(theForm){
	//populates the date dropdowns
	
	//do pitch date
	var dtCurrentDate = addDays(new Date(),1);


	populateDaysControl(theForm.optStartDay,dtCurrentDate);
	
	populateMonthsControl(theForm.optStartMonth,dtCurrentDate);
	
	populateYearsControl(theForm.optStartYear,dtCurrentDate);
	
}

function EnableLengthOfStayControl(theForm, blnEnable){
	theForm.optLengthOfStay.selectedIndex = 0;
	theForm.optLengthOfStay.disabled = !blnEnable;
}

function CheckEnableLengthOfStayControl(theForm){
	if (theForm.optStartDay.selectedIndex!=0&&theForm.optStartMonth.selectedIndex!=0&&theForm.optStartYear.selectedIndex!=0) {
		if (theForm.optLengthOfStay.disabled == true){
			EnableLengthOfStayControl(theForm,true);
		}
	}
	else {
		EnableLengthOfStayControl(theForm,false);
	}
}

function EnableMaxWeeklyPriceControl(theForm, blnEnable){
	theForm.optMaxWeeklyPrice.selectedIndex = 0;
	theForm.optMaxWeeklyPrice.disabled = !blnEnable;
}

function CheckEnableMaxWeeklyPriceControl(theForm){
	if (theForm.optLengthOfStay.selectedIndex!=0&&theForm.optLengthOfStay.options[theForm.optLengthOfStay.selectedIndex].value!=7&&theForm.optLengthOfStay.options[theForm.optLengthOfStay.selectedIndex].value!=14&&theForm.optLengthOfStay.options[theForm.optLengthOfStay.selectedIndex].value!=21) {
		EnableMaxWeeklyPriceControl(theForm,false);
	}
	else {
		if (theForm.optMaxWeeklyPrice.disabled == true){
			EnableMaxWeeklyPriceControl(theForm,true);
		}
	}
}

function populateDaysControl(theControl,selectedDate){
	var iNumDays = 31;
	var i;
	theControl.options.length = iNumDays+1;
	theControl.options[0].text = '';
	theControl.options[0].value = '';
	for(i=0;i<iNumDays;i++){
		theControl.options[i+1].text = i+1;
		theControl.options[i+1].value = i+1;
		if (i % 2==1) theControl.options[i+1].className ='alt';
	}
	//theControl.options.selectedIndex = selectedDate.getDate(selectedDate)-1;
}

function populateMonthsControl(theControl,selectedDate){
	var iNumMonths = 12;
	var i;
	theControl.options.length = iNumMonths+1;
	theControl.options[0].text = '';
	theControl.options[0].value = '';
	for(i=0;i<iNumMonths;i++){
		theControl.options[i+1].text = getShortMonthFromNumber(i);
		theControl.options[i+1].value = i+1;
		if (i % 2==1) theControl.options[i+1].className ='alt';
	}
	//theControl.options.selectedIndex = selectedDate.getMonth();
}

function populateYearsControl(theControl,selectedDate){
	var iNumYears = 2;
	var i;
	theControl.options.length = iNumYears+1;
	theControl.options[0].text = '';
	theControl.options[0].value = '';

	for(i=0;i<iNumYears;i++){
		
			theControl.options[i+1].text = selectedDate.getFullYear()+i;
			theControl.options[i+1].value = selectedDate.getFullYear()+i;
			if (i % 2==1) theControl.options[i+1].className ='alt';
		
	}
	//theControl.options.selectedIndex = 0;
}

function getShortMonthFromNumber(iMonthNumber){
	switch(iMonthNumber){
		case 0:
			return "Jan";
			break;
		case 1:
			return "Feb";
			break;
		case 2:
			return "Mar";
			break;
		case 3:
			return "Apr";
			break;
		case 4:
			return "May";
			break;
		case 5:
			return "Jun";
			break;
		case 6:
			return "Jul";
			break;
		case 7:
			return "Aug";
			break;
		case 8:
			return "Sep";
			break;
		case 9:
			return "Oct";
			break;
		case 10:
			return "Nov";
			break;
		case 11:
			return "Dec";
			break;
	}
	return "Month out of range 0-11";
}

function isValidDate(dateStr) {
	// Checks for the following valid date formats:
	// YY/MM/DD   YYYY/MM/DD   YY-MM-DD   YYYY-MM-DD
	// Also separates date into month, day, and year variables

	var datePat = /^(\d{2}|\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) {
		alert("Date is not in a valid format.")
		return false;
	}
	
	month = matchArray[3]; // parse date into variables
	day = matchArray[5];
	year = matchArray[1];

	
	if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn't have 31 days")
		return false
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		
		if (day>29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn't have " + day + " days");
			return false;
		}
	}

	return true;  // date is valid
}



function validatenewsletterform(form){
	var re = new RegExp('^[-a-z0-9_][-a-z0-9_.]*\@[-a-z0-9]+(\.[-a-z0-9]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|[a-z][a-z])$');
	if(form.getournewsletteremailcontrol.value == "")
	{
		alert("Please enter your email address.");
		form.getournewsletteremailcontrol.focus();
		return false;
	}
	if(!form.getournewsletteremailcontrol.value.match(re))
	{
		alert("Please complete a valid email address.");
		form.getournewsletteremailcontrol.focus();
		return false;
	}
	return true;
}