var whichDate;	

/* launch calendar popup */
function datePicker(dateField){
    whichDate = dateField;
    popUp('calendar.mdlx',500,180,false);
}
			
/* Set datefield - called by calendar popup */
function setDateOfListener(newDate){
    dateListener = newDate;

    if (whichDate=="startDate"){
        document.forms["search"].startDate.value=newDate;
    }else{
        document.forms["search"].endDate.value=newDate;			
    }
}
			
/* search form validation */
function searchValidate(oForm){
	
	//Ensure date has a month, day, and year specified.
	if (oForm.startDate.value.split("/").length!=3){
		alert("The start and end dates must be of a mm/dd/yy format.");
		return false;
	}else if (oForm.endDate.value.split("/").length!=3){
		alert("The start and end dates must be of a mm/dd/yy format.");
		return false;
	}
	
	//Ensure date is properly formatted.
	oForm.startDate.value = enforceShortDateFormat(oForm.startDate.value);
	oForm.endDate.value = enforceShortDateFormat(oForm.endDate.value);
	
    var startDate = new Date(oForm.startDate.value).getTime();
    var endDate = new Date(oForm.endDate.value).getTime();
	var fiveDays = 86400000*5;

	var whichCity = new String(oForm.city.value).toLowerCase();
	
	if (whichCity.indexOf("washington")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "DC");
	}else if (whichCity.indexOf("las vegas")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "NV");
	}else if (whichCity.indexOf("lasvegas")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "NV");
	}else if (whichCity.indexOf("vegas")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "NV");
	}else if (whichCity.indexOf("miami")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "FL");
	}else if (whichCity.indexOf("atlanta")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "GA");
	}else if (whichCity.indexOf("boston")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "MA");
	}else if (whichCity.indexOf("denver")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "CO");
	}else if (whichCity.indexOf("nashville")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "TN");
	}else if (whichCity.indexOf("memphis")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "TN");	
	}else if (whichCity.indexOf("knoxville")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "TN");	
	}else if (whichCity.indexOf("austin")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "TX");
	}else if (whichCity.indexOf("portland")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "OR");
	}else if (whichCity.indexOf("hollywood")>=0 && oForm.state.value=="") {
		setDropDownValue(oForm.name, "state", "CA");	
	}else if (whichCity.indexOf("manhattan")>=0 && oForm.state.value=="") {
		setFieldValue(oForm.name,"city", "new york");
		setDropDownValue(oForm.name, "state", "NY");
	}
		
    if(oForm.startDate.value==""){oForm.startDate.value=getShortDate(7);}
    if(oForm.endDate.value==""){oForm.startDate.value=getShortDate(9);}
    if (oForm.city.value==""){
        alert("Oops! You must specify a city or a town to search.");
        return(false);
	}else if (!validDate(oForm.startDate.value)) {
		alert("Oops! The date you provided for a check-in was invalid.  Please provide a valid date in the following format [mm/dd/yy].");			
        return(false);
	}else if (!validDate(oForm.endDate.value)) {
		alert("Oops! The date you provided for a check-out was invalid.  Please provide a valid date in the following format [mm/dd/yy].");	
        return(false);
    }else if(startDate > endDate){
        alert("Oops! Your checkout date is a date that preceeds your checkin date.");			
        return(false);
    }
	
	if ((oForm.searchClass[1].checked) && ((startDate+fiveDays) >= endDate)){
		myPrompt = confirm("Most Vacation Condos require a 5 night minimum stay. \n You may not find as much availability with the dates \n you are searching. Would you like to continue?");
		return(myPrompt);
	}	

    return(true);
}

function validDate(value){
//assumes a format of MM/dd/yyyy or MM/dd/yy

	flag = false;	
    aValue = value.split("/");
    iValue = aValue[2] + aValue[0] + aValue[1];

	if (Number(iValue)>0) {
		if (aValue[0].length>=1 && aValue[0].length<=2 ) {
			if (aValue[1].length>=1 && aValue[1].length<=2 ) {
				if (aValue[2].length==2 || aValue[2].length==4) {
					flag=true;
				}
			}
			
		}
	}
	return flag;
}
