
/* SEARCH FUNCTIONS */


function CheckSimpleSearch(theForm){
	var Submit = 1
	if (theForm.quicksearch){
		theForm.quicksearch.value = ""
	}
	
	if (theForm.location.value.length==0){
		alert("Please select an area from the list")
	Submit = 0;
	}
	if (theForm.minprice && theForm.maxprice){
		if (!isNaN(theForm.minprice.value) && !isNaN(theForm.maxprice.value)){
			if (parseInt(theForm.minprice.value) >= parseInt(theForm.maxprice.value)){
				alert('Please select a maximum price higher than the minimum price selected');
				Submit = 0;
			}
		}
	}
	if(Submit==1){
		theForm.submit();
	}
}


function CheckQuickSearch(theForm){
	ResetSearchElements(theForm);
	if (theForm.quicksearch){
		if (theForm.quicksearch.value.length==0){
			alert("Please enter Road Name or Postcode")
		//return false;
		}
		else 
		{
			theForm.submit();
		}
	}
	
}


function ResetSearchElements(theForm){
	theForm.location.selectedIndex = 0
	theForm.minprice.selectedIndex  = 0
	theForm.maxprice.selectedIndex  = 0
	theForm.ListingType.selectedIndex  = 0
	theForm.bedrooms.selectedIndex  = 0
	
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function ChangeSimpleSearchOptions(Value, IndicatorField, theForm){

	//Depending on the listing type selected we display different price options
	//alert(document.location);
	var groups = IndicatorField.options.length-1;
	var group = new Array(groups);
	var group2 = new Array(groups);

	var temp = theForm.minprice;
	var temp2 = theForm.maxprice;

	for (i=0; i<groups+1; i++){
		group[i]=new Array();
		group2[i]=new Array();
	}

	group[0][0]=new Option("No min price","0");
	group[0][1]=new Option("\243100,000","100000");
	group[0][2]=new Option("\243200,000","200000"); 
	group[0][3]=new Option("\243300,000","300000"); 
	group[0][4]=new Option("\243400,000","400000"); 
	group[0][5]=new Option("\243500,000","500000");
	group[0][6]=new Option("\243750,000","750000");
	group[0][7]=new Option("\2431,000,000","1000000");
	group2[0][0]=new Option("No max price ","99999999");
	group2[0][1]=new Option("\243200,000","200000"); 
	group2[0][2]=new Option("\243300,000","300000"); 
	group2[0][3]=new Option("\243400,000","400000"); 
	group2[0][4]=new Option("\243500,000","500000");
	group2[0][5]=new Option("\243750,000","750000");
	group2[0][6]=new Option("\2431,000,000","1000000");
	group2[0][7]=new Option("\2432,000,000","2000000");
	group2[0][8]=new Option("\2435,000,000+","999999999");

	group[1][0]=new Option("No min price","0");
	group[1][1]=new Option("\243100.00","100");
	group[1][2]=new Option("\243200.00","200");
	group[1][3]=new Option("\243300.00","300"); 
	group[1][4]=new Option("\243400.00","400"); 
	group[1][5]=new Option("\243700.00","700");
	group[1][6]=new Option("\2431000.00","1000");
	group2[1][0]=new Option("No max price","99999999");
	group2[1][1]=new Option("\243200.00","200");
	group2[1][2]=new Option("\243300.00","300"); 
	group2[1][3]=new Option("\243400.00","400"); 
	group2[1][4]=new Option("\243700.00","700"); 
	group2[1][5]=new Option("\2431000.00+","999999999");
	
	
	switch (Value){
		case '6':
			RecalSimpleSearchPriceRanges(1, temp, temp2, group, group2);
			break;
		default:
			RecalSimpleSearchPriceRanges(0, temp, temp2, group, group2);
			break;
	}

}

function RecalSimpleSearchPriceRanges(x, temp, temp2, group, group2){

		for (m=temp.options.length-1;m>0;m--){
			temp.options[m]=null
		}
		for (i=0;i<group[x].length;i++){
			temp.options[i]=new Option(group[x][i].text,group[x][i].value)
		}
		for (m=temp2.options.length-1;m>0;m--){
			temp2.options[m]=null
		}
		for (i=0;i<group2[x].length;i++){
			temp2.options[i]=new Option(group2[x][i].text,group2[x][i].value)
		}
		temp.options[0].selected=true;
		temp2.options[0].selected=true;
	}
	
	
function OpenWindow(url,name,features){

	window.open(url,name,features);
}

function RequestQueryString(Key){
	//returns the querystring value of the key provided
	var queryString;
	queryString = document.location.href;
	if (queryString.toUpperCase().indexOf(Key.toUpperCase()) >= 0){
		var queryParams = queryString.split("?");
		var queryStringParams = queryParams[1].split("&");
		for (i=0;i<queryStringParams.length;i++){
			var queryParam = queryStringParams[i].split("=");
			if (queryParam[0].toUpperCase() == Key.toUpperCase()){
				return queryParam[1].replace("+", " ");
			}
		}
	}
	return "";
}

function EstateWeb_Formatting_Photos_SetDimensions(source,width, height){
	//replace height
	var heightDimension = source.substring(0, source.indexOf("h=")+2);
	heightDimension += height;
	var widthDimension = source.substring(source.indexOf("w=")+2);
	widthDimension = widthDimension.substring(widthDimension.indexOf("&")+1);
	widthDimension = heightDimension+'&w='+width+'&'+widthDimension;
	return widthDimension;
}

function EstateWeb_Document_GetObject(ID){
	if (document.getElementById){
		return document.getElementById(ID)
	}else{
		return document.all[ID];
	}
}


///############### FORM VALIDATION ###########################

function EstateWeb_RegistrationForm_Validate(theForm)
{

  if (theForm.Title_1.selectedIndex == 0)
  {
    alert("Please choose an appropriate salutation.");
    theForm.Title_1.focus();
    return (false);
  }

  if (theForm.Name_2.value == "")
  {
    alert("Please enter your name");
    theForm.Name_2.focus();
    return (false);
  }

  if (theForm.Name_2.value.length < 2)
  {
    alert("Please enter your name");
    theForm.Name_2.focus();
    return (false);
  }

  if (theForm.Name_2.value.length > 30)
  {
    alert("Please enter at most 30 characters for your name");
    theForm.Name_2.focus();
    return (false);
  }

  if (theForm.Email_Address_3.value == "")
  {
    alert("Please enter a valid email address");
    theForm.Email_Address_3.focus();
    return (false);
  }

  if (theForm.Email_Address_3.value.length < 5)
  {
    alert("Please enter a valid email address");
    theForm.Email_Address_3.focus();
    return (false);
  }

  if (theForm.Email_Address_3.value.length > 60)
  {
    alert("Please enter at most 60 characters for your email address");
    theForm.Email_Address_3.focus();
    return (false);
  }
  
	if (theForm.Email_Address_3.value.indexOf("@")==-1 || theForm.Email_Address_3.value.indexOf("@")==0 || theForm.Email_Address_3.value.indexOf("@")==theForm.Email_Address_3.value.length-1){
    alert("Please enter a valid email address");
    theForm.Email_Address_3.focus();
    return (false);
	}
	
	if (theForm.Email_Address_3.value.indexOf(".")==-1 || theForm.Email_Address_3.value.indexOf(".")==0 || theForm.Email_Address_3.value.indexOf(".")==theForm.Email_Address_3.value.length-1){
    alert("Please enter a valid email address");
    theForm.Email_Address_3.focus();
    return (false);
	}		  

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-@";
  var checkStr = theForm.Email_Address_3.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }

  if (!allValid)
  {
    alert("The email address you have entered appears to be invalid \n If you know it be correct yet this message persists \n please contact us by telephone");
    theForm.Email_Address_3.focus();
    return (false);
  }

  if (theForm.Address_4.value == "")
  {
    alert("Please enter current home address");
    theForm.Address_4.focus();
    return (false);
  }

  if (theForm.Address_4.value.length < 10)
  {
    alert("Please enter current home address");
    theForm.Address_4.focus();
    return (false);
  }

  if (theForm.Address_4.value.length > 80)
  {
    alert("Please enter at most 80 characters for your address");
    theForm.Address_4.focus();
    return (false);
  }

  if (theForm.PostCode_5.value == "")
  {
    alert("Please enter your postcode.");
    theForm.PostCode_5.focus();
    return (false);
  }

  if (theForm.PostCode_5.value.length < 5)
  {
    alert("Please enter your postcode.");
    theForm.PostCode_5.focus();
    return (false);
  }

  if (theForm.PostCode_5.value.length > 10)
  {
    alert("Please enter at most 10 characters for your postcode");
    theForm.PostCode_5.focus();
    return (false);
  }
  
  if (theForm.Telephone_Number_6.value == "")
  {
    alert("Please enter your telephone number");
    theForm.Telephone_Number_6.focus();
    return (false);
  }

  if (theForm.Telephone_Number_6.value.length < 6)
  {
    alert("Please enter your telephone number including std code\n (i.e. +44(0)1376 550010 or 01376-550010)");
    theForm.Telephone_Number_6.focus();
    return (false);
  }

  if (theForm.Telephone_Number_6.value.length > 20)
  {
    alert("Please enter at most 20 characters for your telephone number");
    theForm.Telephone_Number_6.focus();
    return (false);
  }

  var checkOK = "0123456789+-() ";
  var checkStr = theForm.Telephone_Number_6.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only numeric digits and \"+()\" characters in your telephone number");
    theForm.Telephone_Number_6.focus();
    return (false);
  }
if (theForm.Min_Price_12){
 if (theForm.Min_Price_12.value == "")
  {
    alert("Please enter your minimum price");
    theForm.Min_Price_12.focus();
    return (false);
  }
  }

  if (theForm.Min_Price_12){
	if (theForm.Min_Price_12.value.length < 2)
	{
		alert("Please enter your minimum price");
		theForm.Min_Price_12.focus();
		return (false);
	}
  }

if (theForm.Min_Price_12){
  if (theForm.Min_Price_12.value.length > 30)
  {
    alert("Please enter at most 30 characters for your minimum price");
    theForm.Min_Price_12.focus();
    return (false);
  }
  }
  
  if (theForm.Max_Price_13){
  if (theForm.Max_Price_13.value == "")
  {
    alert("Please enter your maximum price");
    theForm.Max_Price_13.focus();
    return (false);
  }
  }

  if (theForm.Max_Price_13){
  if (theForm.Max_Price_13.value.length < 2)
  {
    alert("Please enter your maximum price");
    theForm.Max_Price_13.focus();
    return (false);
  }
  }

  if (theForm.Max_Price_13){
  if (theForm.Max_Price_13.value.length > 30)
  {
    alert("Please enter at most 30 characters for your maximum price");
    theForm.Max_Price_13.focus();
    return (false);
  }
  }
	
  if (theForm.Have_you_arranged_your_mortgage_11){
	if (theForm.Have_you_arranged_your_mortgage_11.selectedIndex == 0)
	{
		alert("Please select whether you have arranged your mortgage yet");
		theForm.Have_you_arranged_your_mortgage_11.focus();
		return (false);
	}
  }
  
  if (theForm.Property_to_sell_9.selectedIndex == 0){
	theForm.Property_to_sell_9.focus();
	alert('Please select whether you have a property to sell');
	return false
  }

  if (theForm.Number_of_Bedrooms_14){
		if (theForm.Number_of_Bedrooms_14.value.length < 1)
		{
			alert("Please enter your minimum required number of bedrooms");
			theForm.Number_of_Bedrooms_14.focus();
			return (false);
		}
	  

		var checkOK = "0123456789-";
		var checkStr = theForm.Number_of_Bedrooms_14.value;
		var allValid = true;
		var validGroups = true;
		for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
			allValid = false;
			break;
			}
		}
		if (!allValid)
		{
			alert("Please enter only numeric digits in the number of bedrooms field");
			theForm.Number_of_Bedrooms_14.focus();
			return (false);
		}
  }

  return (true);
}

//##################################################################