// JavaScript Document


function showhide_closingdate()
{
  var targ = document.getElementById('closingdate'); 
  if(targ)
 { 
  if(document.mainform.status.options[document.mainform.status.selectedIndex].value == "Closing date set")
  {
    if(targ)
	{
	  targ.style.display = 'block';
	}
  }
  else
  {
    if(targ)
	{
	  targ.style.display = 'none'; 
	}	
  }
 }
}




function checkBedrooms()
{

  if(document.mainform)
  {
	if (document.mainform.bedrooms && document.mainform.bedrooms.value == 'other')
	{
		var bedrooms = prompt("Please enter number of bedrooms");

		while(bedrooms && /[^\d]/.test(bedrooms))
		{
			bedrooms = prompt("Please enter number of bedrooms");
		}

		if (!bedrooms || bedrooms == '')
		{
			document.mainform.bedrooms.selectedIndex = 0;
		}
		else
		{
			var length = document.mainform.bedrooms.length;
			document.mainform.bedrooms.options[length] = new Option(bedrooms, bedrooms);
			document.mainform.bedrooms.selectedIndex = length;
		}
	}
 }
}

function validate_form (f)
{
  submitForm(); // Update the RTE

    var required_fields_names  = new Array ( 'street_address', 'postcode', 'price', 'price_modifier', 'type', 'bedrooms', 'status');
    var required_fields_labels = new Array ( 'street', 'postcode', 'price', 'price_modifier', 'type', 'bedrooms', 'status');
    for (var t=0; t < required_fields_names.length; ++t)
    {
        if (f[required_fields_names[t]] && !f[required_fields_names[t]].value.match(/[A-Za-z0-9]/))
	    {
	     alert('Please complete the ' + required_fields_labels[t] + ' field.');
	     return false;
        }
    }

	if(document.getElementById('furnished'))
	{
	  var targ = document.getElementById('furnished');
	  if (!targ.options[targ.selectedIndex].value.match(/[A-Za-z0-9]/))
	  {
	    alert('Please select a furnished option');
	    return false;
	  }
	}
	
	if(( document.mainform.current_price && document.mainform.current_price.value && 
	     document.mainform.current_price.value != document.mainform.price.value &&
	    Math.abs(document.mainform.original_price.value - document.mainform.price.value) > 1000) || 
	   (document.mainform.current_price_mod && document.mainform.current_price_mod.value && document.mainform.current_price_mod.value != document.mainform.price_modifier.value && document.mainform.original_price_mod.value != document.mainform.price_modifier.value))
	    {  
	      if (confirm("You have changed the price of this property. Click OK if you want to flag this up on the site and have the new price details sent out to homeseekers. Click Cancel if you don’t want to highlight the new price")) 
		  {
              document.mainform.disable.value = 'n';
          }
		  else
		  {
		      document.mainform.disable.value = 'y';
		  }
	    }
   	 
	 
	if(!f.area_id.value && !f.area)
   {
      alert('Please complete the area field.');
	  return false;
   }  
 
   if(!f.area_id.value && (f.area && !f.area.value || f.area.value == 0))
   {
      alert('Please complete the area field.');
	  return false;
   }   
	
   if(f.site_status && f.site_status.value == "" )
   {
     alert('Please select a site status.');
	  return false;
   }
	
	if(document.mainform.is_express && document.mainform.is_express.checked == true &&
	   !document.mainform.Edescription.value.match(/[A-Za-z0-9]/))
	 {
       alert('Please provide a description');
     }
    else if (!f.description.value.match(/[A-Za-z0-9]/))
     {	
       alert('Please provide a description');
	 }
	 
   f.description.value = cleanHTML( f.description.value, 1 );	
	
   if (f.description.value.length > 8000)
    {
	alert('Please reduce the length of the description field to 8000 characters or less.');
	return false;
    }
	
	// var targ = document.getElementById('superfeatured');  
	// if(targ)
    // {
      // if(f.superfeatured.checked && !f.super_featured_url.value.match(/[A-Za-z0-9]/))
	  // {
	     // alert('You have marked this property as super featured you must provide a super feature url.');
	     // return false;
	  // }
    // }	

	  cleanArea('description',true);
 
   if ( (!f.new_build || !f.new_build.checked) && ! f.postcode.value.match(/^[a-zA-Z][a-zA-Z]?[0-9]+\s*[0-9][a-zA-Z][a-zA-Z]\s*$/))
       {
	   alert('Please check the postcode and ensure it is in the correct format.');
	   f.postcode.focus();
	   return false;
       }



    return true;
}

function getOtherLocationAreas () 
{
   
    if ( window.ActiveXObject ) // if it's ie
    {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // xmlhttp is global
       xmlhttp.onreadystatechange = function ()
       {	
          if(xmlhttp.readyState == 4) writecontent(xmlhttp.responseText);
       };
    }
    else 
    {
       if (window.XMLHttpRequest) // if it's firefox etc.
       {
         xmlhttp        = new XMLHttpRequest() // xmlhttp is global
         xmlhttp.onload = function () { writecontent(xmlhttp.responseText) };
       }
       else return; // 
    }

    var locationid = "";

    if(document.stepOne)
    {
	locationid = document.stepOne.location.options[document.stepOne.location.selectedIndex].value; 
    }
    else if (document.mainform)
    {
	if (document.mainform.area_id.selectedIndex) {
	    locationid = document.mainform.area_id.options[document.mainform.area_id.selectedIndex].value;
	}
	else if (document.mainform.area_id.length && document.mainform.area_id[1].value) {
	    locationid = document.mainform.area_id[1].value;
	}
    }

    var targ = document.getElementById('locationHeader').style.display = 'block';
    if(locationid != "")
    {
	
	var url = "http://" + document.location.hostname + "/get_other_locations.cgi?locationid=" + locationid;
	
	xmlhttp.open("GET", url , true);
	xmlhttp.send(null);
    }
    else
    {
	document.getElementById('locationHeader').style.display = 'none'
    }
}

