  
  var AreasRange = new Array(0,0,0,0);
  var head = 0;
  var MaxAreas = 10;

  function AddArea(dest) {
  var SourceString = new String(6);  
  selectfor = dest['area_source'].selectedIndex;

  if (selectfor > -1){

    for (i=0; i < dest['area_target'].length; i++ ){

      if (dest['area_source'].options[selectfor].value == dest['area_target'].options[i].value) return;
      
      if(i > (MaxAreas - 2) ){
        alert("You can select up to ten areas");
        return;
        }     

      intersection(dest['area_target'].options[i].value);
      intersection(dest['area_source'].options[selectfor].value); 
      
      if( (AreasRange[0] >= AreasRange[2] && AreasRange[0] <= AreasRange[3]) || (AreasRange[2] >= AreasRange[0] && AreasRange[2] <= AreasRange[1]) ) {
        var flag = confirm("Already selected. \n\n\tWould you like to add it anyway? [press OK to add it, or press Cancel to cancel the action]");
        if(flag) {
        } else {
          return;
        }
      }
    
    }
    var objOption = new Option(dest['area_source'].options[selectfor].text,dest['area_source'].options[selectfor].value);

    dest['area_target'].options[dest['area_target'].length] = objOption;  
    }
}


function intersection(SourceString)
{
if(head >= 3) head = 0;
if(SourceString.indexOf("-")!=-1){
  AreasRange[head++]=parseInt(SourceString.substring(0, SourceString.indexOf("-")), 10);
  AreasRange[head++]=parseInt(SourceString.substring(SourceString.indexOf("-")+1), 10);
  }
 else
  AreasRange[head++]=AreasRange[head++]=parseInt(SourceString, 10);
}


function DeleteArea(dest) {
  if (dest['area_target'].selectedIndex > -1) {
    dest['area_target'].options[dest['area_target'].selectedIndex]=null;
  }
}

function IncludeArea(dest, checkarea){
  for (i = 0; i < document.forms.SearchForm['area_source'].options.length; i++){
    if ( checkarea == document.forms.SearchForm['area_source'].options[i].value ) {
    var objOption = new Option(dest['area_source'].options[i].text,dest['area_source'].options[i].value);
    dest['area_target'].options[dest['area_target'].length] = objOption;  
    }
  }
}

function InitAreas( storedValue ){
  if ( (storedValue) && (storedValue != 0) && storedValue != ""){
    _InitAreas(storedValue);
    return;
  }
  if (document.forms["SearchForm"]["areas_defaults"]) {
    storedValue = document.forms["SearchForm"]["areas_defaults"].value;
    _InitAreas(storedValue);
  }
}

function _InitAreas( storedValue ){
  if ( (storedValue) && (storedValue != 0) && storedValue != ""){
    do {
      var w = storedValue.substring(0, storedValue.indexOf(","));
      var k = storedValue.substring(storedValue.indexOf(",")+1);
      storedValue = k;
      IncludeArea(document.forms.SearchForm,w);
    } while ( storedValue.substring(storedValue.indexOf(",")+1) != k )
    var w = k;
    IncludeArea(document.forms.SearchForm,w);
  }
}
  
function MLSSearch(par, oper) {
  if ( par == '_' ) document.forms.oper.submit();
  if ( document.forms.SearchForm['area_target'].options.length > 0 ){
    var results = document.forms.SearchForm['area_target'][0].value;
    var k = '';
    for (i = 1; i < document.forms.SearchForm['area_target'].options.length; i++){
      k = document.forms.SearchForm['area_target'].options[i].value;
      results = results + ',' + k;
    }
    document.forms['SearchForm']['area_code'].value = results;
  } else {
    document.forms['SearchForm']['area_code'].value = "";
  }
  if ( par == 'test' ) { document.forms.SearchForm['operation'].value = oper; }
  if ( par == 'save' ) { document.forms.SearchForm['operation'].value = oper; }
  document.forms['SearchForm'].submit();
}

function checkLocationFieldsAndSubmit(frm, p1, op) {
  var snameFld = document.forms['SearchForm']['search_name'];
  if (snameFld != null) {
    var val = snameFld.value;
    if (val=="") {
      alert("Search name is required");
      return;
    }
  }
  if (! (frm["active"].checked | frm["pending"].checked | frm["sold"].checked)) {
    alert("Property status is required!");
    return;
  }
  var regexp = /^[\d,]*$/g;
/*  if(frm["price_low"].value.search(regexp) < 0) {
    alert("Price should be number!");
    frm["price_low"].focus();
    return;
  } 
  if(frm["price_high"].value.search(regexp) < 0) {
    alert("Price should be number!");
    frm["price_high"].focus();
    return;
  } 
*/
  if(frm["age"].value.search(regexp) < 0) {
    alert("Age should be number!");
    frm["age"].focus();
    return;
  } 
  if(parseInt(frm["price_high"].value) < parseInt(frm["price_low"].value)) {
    alert("Min price is higher than max!");
    frm["price_h"].focus();
    return;
  } 
  if (document.forms.SearchForm['area_target'].options.length == 0) {
    if (frm["locaid_page"].value=="" & frm["locaid_horiz"].value=="" & frm["locaid_vert"].value=="") {
      if (frm["city_code"].value=="" & frm["zip_code"].value=="") {
        alert("The area, map, city or zip  is required!");
        return;
      }
    }
  }
  if ( document.forms['SearchForm']['mail_header'] ){
    if (!checkValueSize(document.forms['SearchForm']['mail_header'], 0, 490, false, true)){
     alert("Mail header may not exceed 490 characters.");
     return;
    }
  }
  
  if ( document.forms['SearchForm']['mail_footer'] ){
    if (!checkValueSize(document.forms['SearchForm']['mail_footer'], 0, 490, false, true)){
     alert("Mail footer may not exceed 490 characters.");
     return;
    }
  }
  MLSSearch(p1, op);
}
var flag = 0;