function init(){
    document.frmASearch.destination.focus();
    if(document.frmASearch.searchby[1].checked)
        disableMileSDD();
}

//disable the miles dropdown
function disableMileSDD() {
    document.frmASearch.miles.disabled=true;
}

//enbles the miles dropdown
function enableMileSDD(){
    document.frmASearch.miles.disabled=false;
}

//checks the field is empty or not
function isBlank(val) {
    if(val==null){return true;}
    for(var i=0;i<val.length;i++)
        if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r"))
            return false;
    return true;
}

function compareDates (value1, value2) {
    var date1, date2;
    var month1, month2;
    var year1, year2;

    month1 =Number( value1.substring (0, value1.indexOf ("/")));
    date1 = Number(value1.substring (value1.indexOf ("/")+1, value1.lastIndexOf ("/")));
    year1 = Number(value1.substring (value1.lastIndexOf ("/")+1, value1.length));

    month2 = Number(value2.substring (0, value2.indexOf ("/")));
    date2 = Number(value2.substring (value2.indexOf ("/")+1, value2.lastIndexOf ("/")));
    year2 = Number(value2.substring (value2.lastIndexOf ("/")+1, value2.length));

    if (year1 > year2)
        return 1;
    else if (year1 < year2)
        return -1;
    else if (month1 > month2)
        return 1;
    else if (month1 < month2)
        return -1;
    else if (date1 > date2)
        return 1;
    else if (date1 < date2)
        return -1;
    else
        return 0;
}

function BasicSearchValidation() {
    if(!(isBlank(document.frmASearch.fdate.value)) || !(isBlank(document.frmASearch.tdate.value))) {
        /*
		if(isBlank(document.frmASearch.fdate.value)) {
            alert("Please enter arrival date ");
            document.frmASearch.fdate.focus();
            return 0;
        }
		*/

        /*
		if(isBlank(document.frmASearch.tdate.value)) {
            alert("Please enter departure date ");
            document.frmASearch.tdate.focus();
            return 0;
        }
		*/

		if(!isBlank(document.frmASearch.fdate.value)) {

			if(isDate(document.frmASearch.fdate.value)== false) {
				alert("Arrival date must be in the MM/DD/YYYY format. \nPlease reenter an arrival date to continue.");
				document.frmASearch.fdate.focus();
				return 0;
			}

			if(compareDates(document.frmASearch.fdate.value,document.frmASearch.cdate.value) == -1){
				alert("From date is not to be a date in the past. \nPlease enter today's date or a future date in the From date field.");
				document.frmASearch.fdate.focus();
				return 0;
			}
		}

		!(isBlank(document.frmASearch.tdate.value))

			if(!isDate(document.frmASearch.tdate.value)) {
				alert("Departure date must be in the MM/DD/YYYY format. \nPlease reenter a departure date to continue.");
				document.frmASearch.tdate.focus();
				return 0;
			}
			

			if(compareDates(document.frmASearch.tdate.value,document.frmASearch.cdate.value) == -1){
				alert("To date is not to be a date in the past.\nPlease enter today's date or a future date in the To date field.");
				document.frmASearch.tdate.focus();
				return 0;
			}
		}

		if(!(isBlank(document.frmASearch.fdate.value)) || !(isBlank(document.frmASearch.tdate.value))) {
			if(compareDates(document.frmASearch.tdate.value,document.frmASearch.fdate.value) == -1){
				alert("From date must be before the To Date. \nPlease reenter a To date to continue.");
				document.frmASearch.tdate.focus();
				return 0;
			}
		}
	//}
    return 1;
}


function validate() {
    if(!BasicSearchValidation())
        return;

    if(document.frmASearch.fname) {
        if(isBlank(document.frmASearch.fname.value)){
            alert("Please enter first name");
            document.frmASearch.fname.focus();
            return;
        }

        if(isBlank(document.frmASearch.lname.value)){
            alert("Please enter last name");
            document.frmASearch.lname.focus();
            return;
        }

        if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.frmASearch.email.value))) {
            alert("Please enter a valid email address");
            document.frmASearch.email.focus();
            return;
        }

        if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.frmASearch.cfemail.value))) {
            alert("Please enter a valid email address");
            document.frmASearch.cfemail.focus();
            return;
        }

        if(document.frmASearch.cfemail.value != document.frmASearch.email.value) {
            alert("Please check the email addresses entered. They do not match");
            document.frmASearch.cfemail.focus();
            return;
        }

        if(isBlank(document.frmASearch.phone.value)) {
            alert("Please enter phone number");
            document.frmASearch.phone.focus();
            return;
        }

        if(!IsPhoneNumber(document.frmASearch.phone)) {
            alert("The phone number must be numeric.\n Please correct the phone number to continue.");
            document.frmASearch.phone.focus();
            return;
        }

        if(document.frmASearch.agreed_policy.checked == false){
            alert("You must accept the owner email policy to utilize the advanced search option");
            document.frmASearch.agreed_policy.focus();
            return;
        }
    }

    document.frmASearch.submit();
}

//validates the valid phone no
function IsPhoneNumber(fld){
    var ValidChars = "0123456789(-)";
    var IsNum=true;
    var Char;
    sText=fld.value;

    for (i = 0; i < sText.length && IsNum == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1)
            IsNum = false;
    }
    return IsNum;
}

//Declaring valid date character
var dtCh= "/";
function isInteger(s){
    var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9")))
            return false;
    }

    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1)
            returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31;
        if (i==4 || i==6 || i==9 || i==11)
            this[i] = 30;
        if (i==2)
            this[i] = 29;
    }
    return this;
}

function isDate(dtStr){
    var daysInMonth = DaysArray(12);
    var dtCh= "/";
    var pos1=dtStr.indexOf(dtCh);
    var pos2=dtStr.indexOf(dtCh,pos1+1);
    var strMonth=dtStr.substring(0,pos1);
    var strDay=dtStr.substring(pos1+1,pos2);
    var strYear=dtStr.substring(pos2+1);

    strYr=strYear;
    if (strDay.charAt(0)=="0" && strDay.length>1)
        strDay=strDay.substring(1);
    if (strMonth.charAt(0)=="0" && strMonth.length>1)
        strMonth=strMonth.substring(1);
    for (var i = 1; i <= 3; i++)
        if (strYr.charAt(0)=="0" && strYr.length>1)
            strYr=strYr.substring(1);

    month=Number(strMonth);
    day=Number(strDay);
    year=Number(strYr);

    if (pos1==-1 || pos2==-1)
        return false;

    if (strMonth.length<1 || month<1 || month>12){
        alert("Please enter a valid month");
        return false;
    }

    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
        alert("Please enter a valid day");
        return false;
    }

    if (strYear.length != 4) {
        alert("Please enter a valid 4 digit year");
        return false;
    }

    if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
        alert("Please enter a valid date");
        return false;
    }

    return true;
}

function checkStatus(index) {
    var e=document.getElementsByName("prtype[]");

    if(index == 0)
        for(i=1;i<e.length;i++)
            e[i].checked = false;

    if(index > 0)
        e[0].checked=false;
}

function movepic(img_name,img_src) {
    document[img_name].src=img_src;
}

var calWin = null;
var calendarField;
function popCalendar( field,dt,choice ) {
    calendarField = field;
    window.open( pathinfo + '/property/popcalendar.php?dialog=1&callback=setCalendar&date='+dt+"&choice="+choice,'calwin', 'top=250,left=250,width=200,height=155,scollbars=false' );
}

function setCalendar( idate,fdate ){
    dt=idate.substr(4,2)+"/"+idate.substr(6,2)+"/"+idate.substr(0,4);
    fld_fdate = eval( 'document.frmASearch.' + calendarField );
    fld_fdate.value = dt;
    fld_fdate.focus();
}

function replaceChars(entry) {
    out = "/"; // replace this
    add = "-"; // with this
    temp = "" + entry; // temporary holder
    while (temp.indexOf(out)>-1) {
        pos= temp.indexOf(out);
        temp = "" + (temp.substring(0, pos) + add +
        temp.substring((pos + out.length), temp.length));
    }
    return temp;
}

function basicsearch(){
    if(BasicSearchValidation()){
        var destination=document.frmASearch.destination.value;
        if(document.frmASearch.searchby[0].checked)
            var searchby="area";
        else
            var searchby="location";
        var miles=document.frmASearch.miles[document.frmASearch.miles.selectedIndex].value;
        var fdate=replaceChars(document.frmASearch.fdate.value);
        var tdate=replaceChars(document.frmASearch.tdate.value);
        var mbudget=document.frmASearch.mbudget[document.frmASearch.mbudget.selectedIndex].value;
        if(fdate.length > 0 )
            window.location.href = pathinfo + "/search/basicsearch/"+destination+"/"+searchby+"/"+miles+"/"+fdate+"/"+tdate+"/"+mbudget+/search/;
        else
            window.location.href = pathinfo + "/search/basicsearch/"+destination+"/"+searchby+"/"+miles+"/"+mbudget+/search/;
    }
}

var xmlhttp;
function loadXMLDoc(){
    //code for Mozilla, etc.
    stateid=document.frmASearch.state[document.frmASearch.state.selectedIndex].value;

    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange=state_Change;
        xmlhttp.open("GET",'../getcitydetails.php?stateid=' +stateid,true)
        xmlhttp.send(null);
    }

    // code for IE
    else if (window.ActiveXObject){
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp) {
            xmlhttp.onreadystatechange=state_Change;
            xmlhttp.open("GET",'../getcitydetails.php?stateid=' +stateid,true)
            xmlhttp.send();
        }
    }
}

function state_Change(){
    var obj_form = document.frmASearch;

    // if xmlhttp shows "loaded"
    if (xmlhttp.readyState==4) {
        // if "OK"
        if (xmlhttp.status==200) {
            result = xmlhttp.responseText;
            result_arr = result.split(":");
            city_id = result_arr[0];
            cityname = result_arr[1];
            city_id = city_id.split("|");
            cityname = cityname.split("|");

            for (var i=obj_form.destination.options.length; i>0 ; i--)
                obj_form.destination.options[i] = null;

            obj_form.destination.options[0] = new Option("Select a City","");

            if(cityname[0]!="")
                for(var i=0; i<cityname.length; i++)
                    obj_form.destination.options[i] = new Option(cityname[i],city_id[i]);
        }
        else
            alert("Problem retrieving data")
    }
}

var xmlhttpSet;

function loadXMLDocCity(){
    //code for Mozilla, etc.
    cityid=document.frmASearch.destination[document.frmASearch.destination.selectedIndex].value;

    if (window.XMLHttpRequest) {
        xmlhttpSet = new XMLHttpRequest();
        xmlhttpSet.onreadystatechange=city_Change;
        xmlhttpSet.open("GET",'../getneighbordetails.php?cityid='+cityid,true)
        xmlhttpSet.send(null);
    }

    // code for IE
    else if (window.ActiveXObject){
        xmlhttpSet = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttpSet) {
            xmlhttpSet.onreadystatechange=city_Change;
            xmlhttpSet.open("GET",'../getneighbordetails.php?cityid='+cityid,true)
            xmlhttpSet.send();
        }
    }

}

function city_Change(){
    var obj_form = document.frmASearch;

    // if xmlhttp shows "loaded"

        // if "OK"
        if (xmlhttpSet.status==200) {
            result = xmlhttpSet.responseText;
            result_arr = result.split(":");
            nh_city_id = result_arr[0];
            nhcityname = result_arr[1];
            nh_city_id = nh_city_id.split("|");
            nhcityname = nhcityname.split("|");
			
		
            for (var i=obj_form.nhcity.options.length; i>0 ; i--)
                obj_form.nhcity.options[i] = null;

            obj_form.nhcity.options[0] = new Option("Select a Neighborhood","");

            if(nhcityname[0]!=""){
                for(var i=0; i<nhcityname.length; i++){
                    obj_form.nhcity.options[i] = new Option(nhcityname[i],nh_city_id[i]);
					//alert( obj_form.nhcity.options[i] );
				}
			}
        }
        else
            alert("Problem retrieving data")

}




