// Return the XmlHttpObject
function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		 xmlHttp=new XMLHttpRequest();
	  } catch (e) {
		  try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		  } catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
	  }
	return xmlHttp;
}

var xmlHttpObj;

function getSearchData(query,section,datalink){
	if(query.Length != 0){
		xmlHttp = GetXmlHttpObject();
		if(xmlHttp != null){
		  if(query == "LocationSales"){
		  	  document.getElementById('loader-loc').style.display = '';
			  removeOptions(document.SearchForm.Location);
		      var request ="Include/SearchData.php?Type=Location&Section="+section+"&DataLink="+datalink+"&PropertyType="+document.SearchForm.PropertyType.options[document.SearchForm.PropertyType.selectedIndex].value; 
		      xmlHttp.onreadystatechange=returnLocationSales;
		      xmlHttp.open("GET",request,true);
		      xmlHttp.send(null);
		  } 
		  if(query == "LocationLet"){
		  	  document.getElementById('loader-loc').style.display = '';
			  removeOptions(document.SearchForm.Location);
		      var request ="Include/SearchData.php?Type=Location&Section="+section+"&DataLink="+datalink+"&PropertyType="+document.SearchForm.PropertyType.options[document.SearchForm.PropertyType.selectedIndex].value; 
		      xmlHttp.onreadystatechange=returnLocationLet;
		      xmlHttp.open("GET",request,true);
		      xmlHttp.send(null);
		  } 
		  if(query == "LocationComLet"){
		  	  document.getElementById('loader-loc').style.display = '';
			  removeOptions(document.SearchForm.Location);
		      var request ="Include/SearchData.php?Type=Location&Section="+section+"&DataLink="+datalink+"&PropertyType="+document.SearchForm.PropertyType.options[document.SearchForm.PropertyType.selectedIndex].value; 
		      xmlHttp.onreadystatechange=returnLocationLetCom;
		      xmlHttp.open("GET",request,true);
		      xmlHttp.send(null);
		  } 
		  if(query == "LocationComSale"){
		  	  document.getElementById('loader-loc').style.display = '';
			  removeOptions(document.SearchForm.Location);
		      var request ="Include/SearchData.php?Type=Location&Section="+section+"&DataLink="+datalink+"&PropertyType="+document.SearchForm.PropertyType.options[document.SearchForm.PropertyType.selectedIndex].value; 
		      xmlHttp.onreadystatechange=returnLocationSalesCom;
		      xmlHttp.open("GET",request,true);
		      xmlHttp.send(null);
		  } 
		}
	  }else{
	     alert("Please review your browser setting");	
	  }
   }
   
function  returnLocationSales(){
	if(xmlHttp.readyState == 4){ 
		var requestedData = new String(xmlHttp.responseText);
		var objOption;
		objOption = new Option("Any", "Any");
        document.getElementById('Location').options.add(objOption);
		if(requestedData.indexOf("|") >= 0 ){ 
		    var data = requestedData.split("|");  
		  for(var i = 0; i < data.length; i++ ){
		     objOption = new Option(data[i], data[i]);
             document.getElementById('Location').options.add(objOption);
		  }
		}else{
			 objOption = new Option(requestedData, requestedData);
             document.getElementById('Location').options.add(objOption);
		}
		  document.getElementById('loader-loc').style.display = 'none';
		  document.getElementById('Location').disabled = false;

		 returnBedroomsSale();
		  
     }
}

function  returnLocationLet(){
	if(xmlHttp.readyState == 4){ 
		var requestedData = new String(xmlHttp.responseText);
		var objOption;
		objOption = new Option("Any", "Any");
        document.getElementById('Location').options.add(objOption);
		if(requestedData.indexOf("|") >= 0 ){ 
		    var data = requestedData.split("|");  
		  for(var i = 0; i < data.length; i++ ){
		     objOption = new Option(data[i], data[i]);
             document.getElementById('Location').options.add(objOption);
		  }
		}else{
			 objOption = new Option(requestedData, requestedData);
             document.getElementById('Location').options.add(objOption);
		}
		  document.getElementById('loader-loc').style.display = 'none';
		  document.getElementById('Location').disabled = false;

		 returnBedroomsLet();
		  
     }
}
function  returnLocationSalesCom(){
	if(xmlHttp.readyState == 4){ 
		var requestedData = new String(xmlHttp.responseText);
		var objOption;
		objOption = new Option("Any", "Any");
        document.getElementById('Location').options.add(objOption);
		if(requestedData.indexOf("|") >= 0 ){ 
		    var data = requestedData.split("|");  
		  for(var i = 0; i < data.length; i++ ){
		     objOption = new Option(data[i], data[i]);
             document.getElementById('Location').options.add(objOption);
		  }
		}else{
			 objOption = new Option(requestedData, requestedData);
             document.getElementById('Location').options.add(objOption);
		}
		  document.getElementById('loader-loc').style.display = 'none';
		  document.getElementById('Location').disabled = false;

		 getComPrice();
		  
     }
}
function  returnLocationLetCom(){
	if(xmlHttp.readyState == 4){ 
		var requestedData = new String(xmlHttp.responseText);
		var objOption;
		objOption = new Option("Any", "Any");
        document.getElementById('Location').options.add(objOption);
		if(requestedData.indexOf("|") >= 0 ){ 
		    var data = requestedData.split("|");  
		  for(var i = 0; i < data.length; i++ ){
		     objOption = new Option(data[i], data[i]);
             document.getElementById('Location').options.add(objOption);
		  }
		}else{
			 objOption = new Option(requestedData, requestedData);
             document.getElementById('Location').options.add(objOption);
		}
		  document.getElementById('loader-loc').style.display = 'none';
		  document.getElementById('Location').disabled = false;

		 getComPriceLet();
		  
     }
}
  
 function  returnBedroomsSale(){
var objOption;
		  removeOptions(document.SearchForm.MinimumRooms);
		  objOption = new Option("Any", "");
          document.getElementById('MinimumRooms').options.add(objOption);
		  		  
		  var maxRooms = 9;
		  for(j = 1; j < maxRooms; j++){
		  	objOption = new Option(j, j);
		  	document.getElementById('MinimumRooms').options.add(objOption);
		  }
		  document.getElementById('loader-rooms-min').style.display = 'none';
		  document.getElementById('MinimumRooms').disabled = false;

		  //set the minimum price
		   removeOptions(document.SearchForm.MinimumPrice);
		  objOption = new Option("[ no minimum ]", "");
          document.getElementById('MinimumPrice').options.add(objOption);
          
          objOption = new Option("£35,000", "35000");
          document.getElementById('MinimumPrice').options.add(objOption);
          
          objOption = new Option("£40,000", "40000");
          document.getElementById('MinimumPrice').options.add(objOption);
          
          objOption = new Option("£45,000", "45000");
          document.getElementById('MinimumPrice').options.add(objOption);
          
		  for(j = 50000; j <= 600000; j+=25000){
		  	objOption = new Option( formatCurrency(j), j);
		  	document.getElementById('MinimumPrice').options.add(objOption);
		  }
		  for(j = 700000; j <= 1500000; j+=100000){
		  	objOption = new Option( formatCurrency(j), j);
		  	document.getElementById('MinimumPrice').options.add(objOption);
		  }
		  objOption = new Option("[ no minimum ]", "");
          document.getElementById('MinimumPrice').options.add(objOption);
		  
          
          
		   //set the maximum price
		   removeOptions(document.SearchForm.MaximumPrice);
		   objOption = new Option("[ no maxinum ]", "");
          document.getElementById('MaximumPrice').options.add(objOption);
          
          objOption = new Option("£35,000", "35000");
          document.getElementById('MaximumPrice').options.add(objOption);
          
          objOption = new Option("£40,000", "40000");
          document.getElementById('MaximumPrice').options.add(objOption);
          
          objOption = new Option("£45,000", "45000");
          document.getElementById('MaximumPrice').options.add(objOption);
          
		  for(j = 50000; j <= 600000; j+=25000){
		  	objOption = new Option( formatCurrency(j), j);
		  	document.getElementById('MaximumPrice').options.add(objOption);
		  }
		  
		  for(j = 700000; j <= 1500000; j+=100000){
		  	objOption = new Option( formatCurrency(j), j);
		  	document.getElementById('MaximumPrice').options.add(objOption);
		  }
		  
		  objOption = new Option("[ no maxinum ]", "");
          document.getElementById('MaximumPrice').options.add(objOption);
          
          
		  //reset all the values
		  document.getElementById('loader-price-min').style.display = 'none';
	      document.getElementById('loader-price-max').style.display = 'none';
		  document.getElementById('loader-rooms-min').style.display = 'none';
		  document.getElementById('MinimumRooms').disabled = false;
		  document.getElementById('MinimumPrice').disabled = false;
		  document.getElementById('MaximumPrice').disabled = false;
		  
}  


function  returnBedroomsLet(){

		var requestedData = new String(xmlHttp.responseText);
		var objOption;
		
		  document.getElementById('MinRooms').disabled = false;
		  document.getElementById('MinPrice').disabled = false;
		  document.getElementById('MaxPrice').disabled = false;
		
		  // the default selection
		  
		  objOption = new Option("Any", "");
          document.getElementById('MinRooms').options.add(objOption);
		  var maxRooms = 8;
		  for(j = 1; j < maxRooms; j++){
		  	objOption = new Option(j, j);
		  	document.getElementById('MinRooms').options.add(objOption);
		  }  
		  
		 
		
		   //set the minimum price
		    removeOptions(document.SearchForm.MinPrice);
		  objOption = new Option("[ no mininum ]", "");
          document.getElementById('MinPrice').options.add(objOption);
          
		  for(j = 400; j <= 2000; j+=100){
		  	objOption = new Option( formatCurrency(j), j);
		  	document.getElementById('MinPrice').options.add(objOption);
		  }
		  
		  objOption = new Option("[ no mininum ]", "");
          document.getElementById('MinPrice').options.add(objOption);
          
		   //set the maximum price
		   removeOptions(document.SearchForm.MaxPrice);
		  objOption = new Option("[ no maxinum ]", "");
          document.getElementById('MaxPrice').options.add(objOption);
          
		  for(j = 400; j <= 2000; j+=100){
		  	objOption = new Option( formatCurrency(j), j);
		  	document.getElementById('MaxPrice').options.add(objOption);
		  }
		  
		  objOption = new Option("[ no maxinum ]", "");
          document.getElementById('MaxPrice').options.add(objOption);
		  
		  
		  //reset all the values
		 // document.getElementById('loader-price-min').style.display = 'none';
	     // document.getElementById('loader-price-max').style.display = 'none';
		 // document.getElementById('loader-rooms-min').style.display = 'none';
    }




/* Add you the users property list */
function setUserList(refid) {
	if(refid.Length != 0){
		xmlHttp = GetXmlHttpObject();
		if(xmlHttp != null){
		      var request ="Include/MyListData.php?Type=Add&Ref=" + refid;
		      xmlHttp.onreadystatechange=returnUserList;
		      xmlHttp.open("GET",request,true);
		      xmlHttp.send(null);
	    }
	}
 }
function returnUserList(){
	if(xmlHttp.readyState == 4){ 
	   var requestedData = new String(xmlHttp.responseText);
	   document.getElementById('counter').innerHTML = requestedData;
	   document.getElementById("MyListLink").style.display = 'none';
	   document.getElementById("MylistAdded").style.display = '';
  }
}

/* Add you the users property list */
function removeUserList(refid) {
	if(refid.Length != 0){
		xmlHttp = GetXmlHttpObject();
		if(xmlHttp != null){
		      var request ="Include/MyListData.php?Type=Remove&Ref=" + refid;
		      xmlHttp.onreadystatechange=returnRemoveList;
		      xmlHttp.open("GET",request,true);
		      xmlHttp.send(null);
	    }
	}
 }
function returnRemoveList(){
	if(xmlHttp.readyState == 4){ 
	   var requestedData = new String(xmlHttp.responseText);
	   document.getElementById('counter').innerHTML = requestedData;
	   if(requestedData == "0"){
	   	  document.getElementById("NoList").style.display = '';
	   	  document.getElementById("SaveListBtn").style.display = 'none';
	   }
	   document.getElementById("MyListLink").style.display = '';
	   document.getElementById("MylistAdded").style.display = 'None';
   }
}


function getComPrice(){
	      //mini 
	      removeOptions(document.SearchForm.MinCommericalPriceSales);
		  objOption = new Option("[ no minimum ]", "");
          document.getElementById('MinCommericalPriceSales').options.add(objOption);
          
          objOption = new Option("£35,000", "35000");
          document.getElementById('MinCommericalPriceSales').options.add(objOption);
          
          objOption = new Option("£40,000", "40000");
          document.getElementById('MinCommericalPriceSales').options.add(objOption);
          
          objOption = new Option("£45,000", "45000");
          document.getElementById('MinCommericalPriceSales').options.add(objOption);
		  for(j = 50000; j <= 200000; j+=10000){
		  	objOption = new Option(formatCurrency(j), j);
		  	document.getElementById('MinCommericalPriceSales').options.add(objOption);
		  }
		  for(j = 225000; j <= 550000; j+=25000){
		  	objOption = new Option(formatCurrency(j), j);
		  	document.getElementById('MinCommericalPriceSales').options.add(objOption);
		  } 
		   for(j = 700000; j <= 1500000; j+=100000){
		  	objOption = new Option(formatCurrency(j), j);
		  	document.getElementById('MinCommericalPriceSales').options.add(objOption);
		  } 
		  
		  objOption = new Option("[ no minimum ]", "");
          document.getElementById('MinCommericalPriceSales').options.add(objOption);
          
		  
		  //maxi
		  removeOptions(document.SearchForm.MaxCommericalPriceSales);
		   objOption = new Option("[ no maximum ]", "");
          document.getElementById('MaxCommericalPriceSales').options.add(objOption);
          
          objOption = new Option("£35,000", "35000");
          document.getElementById('MaxCommericalPriceSales').options.add(objOption);
          
          objOption = new Option("£40,000", "40000");
          document.getElementById('MaxCommericalPriceSales').options.add(objOption);
          
          objOption = new Option("£45,000", "45000");
          document.getElementById('MaxCommericalPriceSales').options.add(objOption);
		  for(j = 50000; j <= 200000; j+=10000){
		  	objOption = new Option( formatCurrency(j), j);
		  	document.getElementById('MaxCommericalPriceSales').options.add(objOption);
		  }
		  for(j = 225000; j <= 550000; j+=25000){
		  	objOption = new Option( formatCurrency(j), j);
		  	document.getElementById('MaxCommericalPriceSales').options.add(objOption);
		  }
		   for(j = 700000; j <= 1500000; j+=100000){
		  	objOption = new Option( formatCurrency(j), j);
		  	document.getElementById('MaxCommericalPriceSales').options.add(objOption);
		  }
		  
		  objOption = new Option("[ no maximum ]", "");
          document.getElementById('MaxCommericalPriceSales').options.add(objOption);
		  
		  document.getElementById('MinCommericalPriceSales').disabled = false;
		  document.getElementById('MaxCommericalPriceSales').disabled = false;
}

function getComPriceLet(){
	      objOption = new Option("[ no maximum ]", "");
	      removeOptions(document.SearchForm.CommercialPriceLet);
          document.getElementById('CommercialPriceLet').options.add(objOption);
		  for(j = 400; j <= 2000; j+=100){
		  	objOption = new Option( formatCurrency(j), j);
		  	document.getElementById('CommercialPriceLet').options.add(objOption);
		  }
		   objOption = new Option("[ no mininum ]", "");
          document.getElementById('CommercialPriceLet').options.add(objOption);
		  
		  objOption = new Option("[ no maximum ]", "");
          document.getElementById('CommercialPriceLet').options.add(objOption);
		  for(k = 3000; k <= 5000; k+=1000){
		  	objOption = new Option( formatCurrency(k), k);
		  	document.getElementById('CommercialPriceLet').options.add(objOption);
		  }
		  objOption = new Option("[ no maximum ]", "");
          document.getElementById('CommercialPriceLet').options.add(objOption);
          
		  document.getElementById('CommercialPriceLet').disabled = false; 
}



/* Get the next image in the list */
function forwardImage(refid) {
	if(refid.Length != 0){
		xmlHttp = GetXmlHttpObject();
		if(xmlHttp != null){
		      var request ="Include/ImageData.php?Navigate=Forward&ref=" + refid;
		      xmlHttp.onreadystatechange=returnForwardImage;
		      xmlHttp.open("GET",request,true);
		      xmlHttp.send(null);
	    }
	}
 }
function returnForwardImage(){
	if(xmlHttp.readyState == 4){ 
	   var requestedData = new String(xmlHttp.responseText);
	   document.getElementById('PropertyImage').innerHTML = requestedData;
   }
}

/* Get the previous image in the list */
function rewindImage(refid) {
	if(refid.Length != 0){
		xmlHttp = GetXmlHttpObject();
		if(xmlHttp != null){
		      var request ="Include/ImageData.php?Navigate=Rewind&ref=" + refid;
		      xmlHttp.onreadystatechange=returnRewindImage;
		      xmlHttp.open("GET",request,true);
		      xmlHttp.send(null);
	    }
	}
 }
function returnRewindImage(){
	if(xmlHttp.readyState == 4){ 
	   var requestedData = new String(xmlHttp.responseText);
	   document.getElementById('PropertyImage').innerHTML = requestedData;
   }
}


/* Remove drop down options from the listing */
function removeOptions(id) {
       while (id.hasChildNodes()){
          id.removeChild(id.childNodes[0]);
    }
}

function removeMyList(ref){
	document.getElementById(ref).style.display = 'none';
}


function formatCurrency(num) {
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '£' + num);
}
   