var http = getHTTPObject();
var callback = '';
/*************************************************************/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
/*************************************************************/
function setStatus(obj){
	objTarget=obj.parentNode.parentNode.lastChild;
	(objTarget.style.display=='')?objTarget.style.display='none':objTarget.style.display='';
}
/*************************************************************/
   function switchMenu(obj, flag) {
	    var el = document.getElementById(obj);
	    el.style.display = flag;
   }
/*************************************************************/
   function getHTTPObject() {
     var xmlhttp;
     if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
          xmlhttp = new XMLHttpRequest();
        } catch (e) {
          xmlhttp = false;
        }
     }else if(typeof window.ActiveXObject != "undefined"){
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     return xmlhttp;
   }
/*************************************************************/
   function handleHttpResponse() {
	   if (http.readyState == 4) {
		    results = http.responseText;
		    results = results.split("*");
		    if (results[0] == "location") {
			   for (x=1; x<results.length; x++){
				   tmpStr = results[x].split('|');
				   document.units.country.options[x]=new Option(tmpStr[1], tmpStr[0], true, false);
				   switchMenu('state1', '');
				   switchMenu('state2', 'none');
			   }
			} else {
			   results_length = results.length;
			   if (results_length > 1 && results[1]!='') {
				  switchMenu('state1', 'none');
				  document.units.state1.value = "";
				  switchMenu('state2', '');
				  for (x=1; x<results_length; x++){
					  tmpStr = results[x].split('|');
					  document.units.state2.options[x]=new Option(tmpStr[1], tmpStr[0], true, false);
				  }
			   } else {
				  switchMenu('state2', 'none');
				  document.units.state2.options.length=0;
				  switchMenu('state1', '');
			   }
			}
			if (callback!=''){
				eval(callback);
			}
		}
   }   
/*************************************************************/
   function doprocess(theValue,theId,callback2) {
      var send = theId+'&value='+theValue;
  	  if (theId == "location") {
         document.units.country.options.length=0;
         document.units.state2.options.length=0;
      } else {
         document.units.state2.options.length=0;
      }
	  //alert(url+send);
      http.open("POST", url + send, true);
      isWorking = true;
	  callback=callback2; // set local var to global.
      http.onreadystatechange = handleHttpResponse;
      http.send(null);
   }
/*************************************************************/
	function updateDateTo(){
		arrDaysinMonths=new Array('0','31','28','31','30','31','30','31','31','30','31','30','31');
		arrDateFrom=gv('ava_date_from').split('-');
		d=Number(arrDateFrom[2])+7;
		m=Number(arrDateFrom[1]);
		y=Number(arrDateFrom[0]);
		if (d>arrDaysinMonths[m]){d=d-arrDaysinMonths[m];m++;}
		if (m>12){m=1;y++;}
		//document.getElementById('ava_date_to').value=d+'-'+m+'-'+y;
		document.getElementById('ava_date_to').value=y+'-'+twoDigit(m)+'-'+twoDigit(d);
	}
/*************************************************************/
	function DoSubmit(){		
		bCont=true;
		if (bCont && isNaN(gv('num_beds'))) {
			alert('You can only enter numbers in Bedrooms Field');
			bCont=false;
		}
		if (bCont){
			from=gv('ava_date_from').split('-');
			from=parseFloat(from[0]*12)+parseFloat(from[1]);
			//alert(from);
			to=gv('ava_date_to').split('-');
			to=parseFloat(to[0]*12)+parseFloat(to[1]);
			//alert(parseFloat(from)+'-'+parseFloat(to));
			if((parseFloat(to)-parseFloat(from))>1) {
				alert('You can only search for at most 60 days availablity.')
				bCont=false;
			}
		}
		if (document.getElementById('price_min') && bCont && isNaN(gv('price_min'))) {
			alert('You can only enter numbers in Minimum Price Field');
			bCont=false;
		}
		if (document.getElementById('price_max') && bCont && isNaN(gv('price_max'))) {
			alert('You can only enter numbers in Maximum Price Field');
			bCont=false;
		}
		if (ge('selectBy') && ge('selectBy').checked){
			if (ge('zip_code').value==''){
				alert('You must type in your Zip Code.');
				bCont=false;
			}
		}else{
			if(document.getElementById('country').value==''){
				alert('You must select a country to initiate search function.');
				bCont=false;
			}
		}
		if (bCont){
			from=gv('ava_date_from').split('-');
			to=gv('ava_date_to').split('-');
			document.getElementById('ava_date_from2').value=from[2]+'-'+from[1]+'-'+from[0];
			document.getElementById('ava_date_to2').value=to[2]+'-'+to[1]+'-'+to[0];
		}
		return bCont;
	}
/*************************************************************/
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
/*************************************************************/
function twoDigit(m){return ((m<10)?'0'+m:m)}
function ge(id){return(document.getElementById(id));}
function gv(id){return(ge(id).value);}
function sf(id){ge(id).focus();}


$(function(){
	lcsc_prefix='';
	$('#btnLocReq').click(function(){GoLocReq2();});
});
//```````````````````````````````````````````````````````````
function GoLocReq2(){
	lnk=js_dir+'search/request.php?ln='+$('#cab_location > #'+lcsc_prefix+'location').val()+'&co='+$('#cab_country > #'+lcsc_prefix+'country').val()+'&st='+$('#cab_state > #'+lcsc_prefix+'state').val()+'&ci='+$('#cab_city > #'+lcsc_prefix+'city').val()+'&unit_type_id='+$('#unit_type_id').val();
	$('#btnLocReq').attr('href',lnk);
	$('#btnLocReq').attr('target','_parent');
	return(true);
}
//```````````````````````````````````````````````````````````
