/*
written by Carey R. Dayrit <carey.dayrit.org>
copyright Shel Goldberg
Last update: 04-18-2008 +8GMT
*/

function check_citypostal(){
   var cmdcitypostal= document.getElementById('citypostal');
   var citypostal=cmdcitypostal.value;
   var frm=document.frmSearch;
    //check first if it has special characters
  //debug

  //get the value of the two radio buttons;
  var radio_search=frm.showme;
  if(radio_search[0].checked){
     frm.action="http://dreamrealty.com/tb_mls_search.php"
  }else{
     frm.action="http://dreamrealty.com/tb_real_estate_search.php"
  }


  if(citypostal ==""){
    msg="Please input MLS number, City name or zip code";
    open_instruction(cmdcitypostal,'crd_msg1',msg);
    return false;
  }
   var sclpattern="[!\"#$%&\'()*+,-./:;<=>?@_`\{|}~]";
   var re = new RegExp(sclpattern,"ig") ;
   var x=re.test(citypostal);
   //x has a boolean value that the citypostal contains special characters
   if(!x){
      //test first number or alphanumeric

      var sclpattern="[A-Za-z]";
      var re = new RegExp(sclpattern,"ig");
      x=re.test(citypostal);
       if(x){//this is alphanumeric
        //do the test
        var sclpattern="[0-9][A-Za-z]";
        var re = new RegExp(sclpattern,"ig") ;
        x=re.test(citypostal);
        if(x){//city postal starts with number but it is proceeded by letters
            //wrong MLS Number format
            msg="Wrong MLS number or City !" ;
            open_instruction(cmdcitypostal,'crd_msg1',msg);
        }else{
            //check if it is an MLS number or City
            //get the second of first
            if(isNaN(citypostal.substr(2,1))){
               document.frmSearch.citypostal.value='';
               document.frmSearch.mls_number.value='';
               document.frmSearch.city.value=citypostal;
            }else{
              document.frmSearch.citypostal.value='';
              document.frmSearch.city.value='';
               document.frmSearch.mls_number.value=citypostal;
            }
             return true;
            //alert('check if it is an MLS number or City');
        }
       }else{
          //this is number only
         //if it is 5 then it's a zip code
         if(citypostal.length==5){
          document.frmSearch.zipcode.value=citypostal;
         }else{
          document.frmSearch.mls_number.value=citypostal;
         }
         //alert('Success valid MLS Number digits only');
         //else if it's 10 then it's an MLS number
         return true;
       }

   }else{
    //do popup here
    msg="Please enter a city, zipcode or MLS number.";
    open_instruction(cmdcitypostal,'crd_msg1',msg);
    return false;
   }
  //alert('pogi');
   return false;
}

function open_instruction(oCur,sName,sMsg){
  var oD=document.getElementById(sName);
  var oM=document.getElementById('crd_msg_content');
  oD.style.left=getposOffset(oCur, "left")+ 120 +'px';
  //oD.style.left='200px';
  oD.style.top=(getposOffset(oCur, "top") + 26 )  +'px';
  //alert(getposOffset(oCur, "top")- 19+'px');
  oM.innerHTML=sMsg;
  oD.style.visibility='visible';

}

function close_instruction(sName){
    var oD=document.getElementById(sName);
    oD.style.visibility='hidden';

}


function getposOffset(what, offsettype){

  var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;

  var parentEl=what.offsetParent;

  while (parentEl!=null){

  totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;

    parentEl=parentEl.offsetParent;

  }

  return totaloffset;

}
