var formPosted=false;
var searchBase=null;

function searchChange(s)
{
  document.getElementById('qs_pt').style.display=s.selectedIndex==0 ? '' : 'none';
  document.getElementById('qs_b').style.display=s.selectedIndex==0 ? '' : 'none';
  document.getElementById('qs_m').style.display=s.selectedIndex==1 ? '' : 'none';
}

function advancedSearch()
{
  var t;
  if (formPosted)
    return;
  var f=document.forms['as'];
  if (f.suburb.value=="") {
    alert('Please enter the suburb of interest.');
    f.suburb.focus();
    return;
  }
  if (f.state.selectedIndex <= 0) {
    alert('Please select the state the suburb is located in.');
    f.state.focus();
    return;
  }
  if (f.r.value != "" && (isNaN(parseInt(f.r.value)) || parseInt(f.r.value) < 1 || parseInt(f.r.value) > 500)) {
    alert('Please enter only positive whole numbers between 1 and 500 for the radius.');
    f.r.focus();
    return;
  }
  if (f.t.selectedIndex==0) {
    // bedroom dropdowns.....
    if (f.bf.selectedIndex > 0 && f.bt.selectedIndex > 0 && f.bt.selectedIndex < f.bf.selectedIndex) {
      t=f.bt.selectedIndex;
      f.bt.selectedIndex=f.bf.selectedIndex;
      f.bf.selectedIndex=t;
    }
  }
  else {
  }
  if (f.pf.selectedIndex > 0 && f.pt.selectedIndex > 0 && f.pt.selectedIndex < f.pf.selectedIndex) {
      t=f.pt.selectedIndex;
      f.pt.selectedIndex=f.pf.selectedIndex;
      f.pf.selectedIndex=t;
  }
  formPosted=true;
  
  f.submit();
}

function quickSearch()
{
  var t;
  if (formPosted)
    return;
  var f=document.forms['qs'];
  if (f.suburb.value=="") {
    alert('Please enter the suburb of interest.');
    f.suburb.focus();
    return;
  }
  if (f.state.selectedIndex <= 0) {
    alert('Please select the state the suburb is located in.');
    f.state.focus();
    return;
  }
  if (f.r.value != "" && (isNaN(parseInt(f.r.value)) || parseInt(f.r.value) < 1 || parseInt(f.r.value) > 500)) {
    alert('Please enter only positive whole numbers between 1 and 500 for the radius.');
    f.r.focus();
    return;
  }
  formPosted=true;
  
  f.submit();
}

function re_search(url,suburb)
{
  document.location=url+'&suburb='+encodeURI(document.getElementById('suburb').value);
}

function page(p)
{
  document.location=searchBase+'&p='+p;
}

function sort(url,s)
{
  document.location=url+'&s='+s.options[s.selectedIndex].value;
}

function makeOffer(u)
{
  window.open(u,'','width=620,height=600,scrollbars=yes');
}

function showGallery(p,i)
{
  window.open('gallery.php?p='+p+'&index='+i,'','width=810,height=650');
}

function validate_enquiry(theForm)
{
  if (formPosted)
    return true;
  if (theForm.name.value=="") {
    alert("Please enter your name.");
    theForm.name.focus();
    return false;
  }
  if (theForm.phone.value=="") {
    alert("Please enter your contact number.");
    theForm.phone.focus();
    return false;
  }
  if (theForm.email.value=="" || !validateEmailv2(theForm.email.value)) {
    alert("Please enter a valid email address.");
    theForm.email.focus();
    return false;
  }
  if (theForm.message.value=="") {
    alert("Please enter your message/enquiry regarding the above property.");
    theForm.message.focus();
    return false;
  }
  return formPosted=true;
}
