/**
  *Filename  :update_profile.js
  *Purpose :This page holds javascript validations used in the update_profile.php,update_job_details.php,update_service_details.php.
  *Tables:none  
**/

/** used to trim spaces**/ 
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

//performs the necessary validation on personal details radio buttons
function checkRadio()
{
  var individual= document.updateprofileform.personal[0];
  var business= document.updateprofileform.personal[1];
  if(individual.checked) //disable business fields
  {
    document.getElementById('bus_name').value="";   
  }
  else if(business.checked) //disable individual fields
  {
   
    document.getElementById('fname').value="";
    document.getElementById('lname').value="";   
  }  
}

//This function 'checks' the respective radio based on the input box in which text is entered	
function markRadio(id)
{
  var individual= document.updateprofileform.personal[0];
  var business= document.updateprofileform.personal[1];
  //alert(id)
  if(id=='fname' || id=='lname') //disable business fields
  {
     individual.checked=true;
     business.checked=false;
     document.getElementById('bus_name').value="";   
  }
  else if(id=='bus_name') //disable individual fields
  {
    individual.checked=false;
    business.checked=true;
    document.getElementById('fname').value="";
    document.getElementById('lname').value="";   
  }  
}

//ajax function to obtain all the states based on the chosen country and keep the user's state selected      
  function getUpStates(cmbcountry,cmbstate,cmbsuburb,cmbpostcode)
  {
    var xmlHttp; 
    var j = document.getElementById(cmbcountry).selectedIndex;
    var sel_country = document.getElementById(cmbcountry).options[j].value;
    //reset the suburb and postcode combo boxes
    if(j>0){
        var cmbstate=document.getElementById(cmbstate).id;
        clearCombo(cmbstate);  
        clearCombo(cmbsuburb);
        clearCombo(cmbpostcode);
        if(sel_country > 0)
        {
          var strUrl= "cmblocation_code.php?country_id="+sel_country;
          
           xmlHttp=createRequestObject();   
           xmlHttp.open("GET",strUrl,true);       
           xmlHttp.onreadystatechange=function() {
                  if(xmlHttp.readyState==4) {         
                          if(xmlHttp.status==200)
                          {
                              //alert(xmlHttp.responseText);                    
                              eval("var response=("+xmlHttp.responseText+")"); 
                               for(i=0;i < response.states.list.length; i++) {
                                    var y=document.createElement('option');
                                    y.text=response.states.list[i].state;
                                    y.value=response.states.list[i].id; 
                                    var x=document.getElementById(cmbstate);
                                    if(y.value>0)       
                                        try{
                                         x.add(y,null); // standards compliant                                            
                                         } catch(ex){ x.add(y); // IE only
                                         } 
                                }       
                                           
                          }
                  }
          }
          xmlHttp.send(null); 
         }
      }
      else if(j==0)
      {
        clearCombo(cmbsuburb);
        clearCombo(cmbpostcode);
      }
  }
  
  
  
  
  
  //ajax function to obtain all the suburbs based on the chosen state and keep the user's suburb selected        
  function getUpSubUrb(cmbstate,cmbsuburb,cmbpostcode)
  {
    var xmlHttp; 
    var j = document.getElementById(cmbstate).selectedIndex;
    var sel_state = document.getElementById(cmbstate).options[j].value;
     //reset the suburb and postcode combo boxes
    if(j>0){
      var cmbsuburb=document.getElementById(cmbsuburb).id;
      clearCombo(cmbsuburb); 
      clearCombo(cmbpostcode);     
      if(sel_state > 0)
      {
         var strUrl= "cmblocation_code.php?sub_sid="+sel_state;
         xmlHttp=createRequestObject();    
         xmlHttp.open("GET",strUrl,true);       
         xmlHttp.onreadystatechange=function() {
                if(xmlHttp.readyState==4) {         
                        if(xmlHttp.status==200)
                        {
                            //alert(xmlHttp.responseText);                    
                            eval("var response=("+xmlHttp.responseText+")"); 
                             for(var i=0;i < response.SubUrb.list.length; i++)
                              {
                                  //create a combo box for suburbs
                                  var y=document.createElement('option');
                                  y.text=response.SubUrb.list[i].suburb;
                                  y.value=response.SubUrb.list[i].subid; 
                                  var a=document.getElementById(cmbsuburb);
                                  if(y.value>0)   //for suburbs    
                                      try{
                                       a.add(y,null); // standards compliant                                            
                                       } catch(ex){ a.add(y); // IE only
                                       } 
                              }
                        }
                     }
                  }
        xmlHttp.send(null); 
       }
     }
      else if(j==0)
      {
        clearCombo(cmbsuburb);
        clearCombo(cmbpostcode);
      }
  }
 
 
//ajax function to obtain all the postcodes based on the chosen state and keep the user's postcode selected  
   function getUpPostcode(cmbsuburb,cmbpostcode)
   {
      var xmlHttp; 
      var j = document.getElementById(cmbsuburb).selectedIndex;
      var sel_suburb = document.getElementById(cmbsuburb).options[j].value;
      var cmbpostcode=document.getElementById(cmbpostcode).id;
      clearCombo(cmbpostcode);
      if(sel_suburb > 0)
      {
         var strUrl= "cmblocation_code.php?post_subid="+sel_suburb;
         xmlHttp=createRequestObject();    
         xmlHttp.open("GET",strUrl,true);       
         xmlHttp.onreadystatechange=function() {
                if(xmlHttp.readyState==4) {         
                        if(xmlHttp.status==200)
                        {
                            //alert(xmlHttp.responseText);                    
                            eval("var response=("+xmlHttp.responseText+")"); 
                             for(var k=0;k < response.Postcode.list.length; k++) 
                             {
                                  //create a combo box for postcodes
                                var z=document.createElement('option');
                                z.text=response.Postcode.list[k].postcode;
                                z.value=response.Postcode.list[k].postid; 
                                var b=document.getElementById(cmbpostcode);
                                if(z.value>0)       //for postcodes
                                    try{
                                     b.add(z,null); // standards compliant                                            
                                     } catch(ex){ b.add(z); // IE only
                                     }     
                            }   
                         }
                      }
                  }
        xmlHttp.send(null); 
    }
  }
  

//validates the form fields
function validateform()
{
  var if_error=false;
  var objerrdiv=document.getElementById('errdiv');
   if(document.updateprofileform.personal[0].checked)
   {
      if(trim(document.getElementById('fname').value)=="") 
      {  
         var strmssg1=no_fname;
         objerrdiv.innerHTML=strmssg1;       
         return false;
      }
      else if(trim(document.getElementById('lname').value)=="") 
      {  
         var strmssg1=no_lname;
         objerrdiv.innerHTML=strmssg1;       
         return false;
      }  
   }
   else if(document.updateprofileform.personal[1].checked)
   {
      if(trim(document.getElementById('bus_name').value)=="") 
      {  
         var strmssg1=no_busname;
         objerrdiv.innerHTML=strmssg1;       
         return false;
      }      
   }
    if(document.getElementById('cmbcountry').selectedIndex==0)
    {
     if_error=true; 
     var strmssg1=no_country;
    }
    else if(document.getElementById('cmbstate').value=="")//hidden
    {
     if_error=true; 
     var strmssg1=no_state;
    }
    else if(document.getElementById('cmbsuburb').value=="")//hidden
    {
     if_error=true; 
     var strmssg1=no_suburb;
    }
    else if(document.getElementById('cmbpostcode').value=="" && trim(document.getElementById('cmbpostcode_html').value)=="" )//hidden or entered by user
    {
     if_error=true; 
     var strmssg1=no_postcode;
    }
    else if(document.getElementById('email').value=="")
    {
     if_error=true; 
     var strmssg1=no_email;
    }
   
    //checks for further email validation 
    if(!if_error)
    {
       if(CheckEmail(trim(document.getElementById('email').value))==false) //checks for valid email address
       {  
          var strmssg1=valid_email; 
          document.getElementById('email').focus();             
          if_error=true;
       }   
    }
      
        //checks for special characters in last name
      if(document.getElementById('lname').value!="")
      {
        if(chkSpecial('lname')==false)
        { 
           var strmssg1=valid_lname;
           if_error=true;
        } 
      }   
       
     //checks for special characters in first name
      if(document.getElementById('fname').value!="")
      {
        if(chkSpecial('fname')==false)
        { 
           var strmssg1=valid_fname;
           if_error=true;
        } 
      }  
      
      
      //checks for valid mobile no
      if(document.getElementById('mobile').value!="")
        {
          if(PhoneValidation('mobile')==false)
          {
             var strmssg1=valid_mobileno;
             if_error=true;
          }
        }
        
        //checks for valid alt phone no
       if(document.getElementById('altphone').value!="")  
        {
          if(PhoneValidation('altphone')==false)
          {
             var strmssg1=valid_altphoneno;
             if_error=true;
          }
        } 
       
  
  if(if_error)
  {  
    objerrdiv.innerHTML=strmssg1;       
    return false;
  }
  else 
  {    
    objerrdiv.innerHTML="";
    document.getElementById("update_mode").value=1;
    document.updateprofileform.submit(); //submit the form
  }   
}


  //ajax function to obtain the questions based on the chosen combination of industry and service    
  function getQuestions(num)
  {
      var xmlHttp; 
      var len;
      var i = document.getElementById("cmbindustry").selectedIndex;
      var sel_industry = document.getElementById("cmbindustry").options[i].value;
      var j = document.getElementById("cmbservices").selectedIndex;
      var sel_service = document.getElementById("cmbservices").options[j].value;
      if(sel_industry > 0)
      {
         var strUrl= "cmblocation_code.php?service_id="+sel_service+"&ind_id="+sel_industry+"&is_js="+num;
         xmlHttp=createRequestObject();    
         xmlHttp.open("GET",strUrl,true);       
         xmlHttp.onreadystatechange=function() 
         {
            if(xmlHttp.readyState==4) {         
                    if(xmlHttp.status==200)
                    {
                     //alert(xmlHttp.responseText);    
                      eval("var response=("+xmlHttp.responseText+")");
                      var is_no=response.is_id; 
                      if(is_no!=0){
                          document.getElementById("hdn_is_no").value=is_no; //put is_no in a hidden variable
                          if(response.questions.length >= 1){ //if there are questions, display them
                             if(document.getElementById("mainQuest").style.display="none")
                             {
                              document.getElementById("mainQuest").style.display="block";
                             }
                               //clear questions
                              clearQuest();
                              if(document.getElementById("quesdiv").style.display="none")
                                {
                                  document.getElementById("quesdiv").style.display="block";
                                }
                              len=document.getElementById('quest_tab').rows.length;   //no of rows
                              if(len>=1){
                                  for(var rowno=0;rowno<len; rowno++){
                                        document.getElementById('quest_tab').deleteRow(0);
                                  }
                              }
                            for(var k=0;k < response.questions.length; k++) {
                                  var q_no=response.questions[k].id;
                                  var q=response.questions[k].ques;
                                  len=document.getElementById('quest_tab').rows.length;   //no of rows 
                                  var a=document.getElementById('quest_tab').insertRow(len);  //insert a tr
                                  var b=a.insertCell(0); //insert a td
                                  var c=a.insertCell(1);
                                  var d=a.insertCell(2);
                                  
                                  c.vAlign="top";
                                  d.vAlign="top";
                                  
                                  b.innerHTML=q;
                                  c.innerHTML="&nbsp;&nbsp;Yes<input type='radio' class='noBorder' name='ques_"+q_no+"' value='Yes'>";
                                  d.innerHTML="&nbsp;&nbsp;No<input type='radio' class='noBorder' name='ques_"+q_no+"' value='No' checked>";
                            }     
                          }
                          else{ // if there are no questions, hide the div
                              clearQuest();
                              document.getElementById("mainQuest").style.display="none";
                          }
                        }//end is_no!=0
                      else{   // if no job services listed clear the question div
                        clearQuest();
                        document.getElementById("mainQuest").style.display="none";
                      }
                    }
                  else
                    {
                      document.getElementById("mainQuest").style.display="none";
                    }
                  }
         }
        xmlHttp.send(null); 
      }
  }


 //function to clear the questions div
  function clearQuest(){
    len=document.getElementById('quest_tab').rows.length;   //no of rows
    if(len>=1){
        for(var rowno=0;rowno<len; rowno++){
              document.getElementById('quest_tab').deleteRow(0);
        }
    }
  }
  
  
  //function to delete a row in the service/job grid
  function deleteRow(r,gridname,divname,msg)
  {
    var i=r.parentNode.parentNode.rowIndex;
    var len=r.parentNode.parentNode.parentNode.rows.length;
    document.getElementById(gridname).deleteRow(i);
    // if the table has only one row(besides the header) and we are deleting that row, hide the div.
    if(i==1 && len==2) 
    {
      document.getElementById(gridname).style.display="none";
      document.getElementById(divname).innerHTML=msg;
    }
  }
  
  
  
  //function to delete a job
  function deleteJob(jobid,idx){
  //alert(jobid);    
      var strURL="update_profile_code.php?jobdelete=1&jid="+jobid;
      if(!confirm(DEL_JOB))
        return false;
      else {  
        var xmlHttp;            
        xmlHttp=createRequestObject();    
        xmlHttp.onreadystatechange=function() {
                if(xmlHttp.readyState==4) {         
                        if(xmlHttp.status==200){  
                             var response=xmlHttp.responseText;
                             //alert(trim(response));
                                 if(response==1)
                                 {
                                    deleteRow(idx,"jobgrid","jobdiv",no_job_adverised);
                                 }
                              }
                            }
                          }
       xmlHttp.open("GET",strURL+"&bustcache="+new Date().getTime(),true);   
       xmlHttp.send(null);   
       }    
  }
  
  
 //function to delete a service
  function deleteService(serviceid,idx){
      var strURL="update_profile_code.php?servicedelete=1&sid="+serviceid;
      if(!confirm(DEL_SERVICE))
        return false;
      else {  
        var xmlHttp;            
        xmlHttp=createRequestObject();    
        xmlHttp.onreadystatechange=function() {
                if(xmlHttp.readyState==4) {         
                        if(xmlHttp.status==200){  
                             var response=xmlHttp.responseText;
                             //alert(trim(response));
                                 if(response==1)
                                 {
                                    deleteRow(idx,"servicegrid","servicediv",no_service_adverised);
                                 }
                              }
                            }
                          }
       xmlHttp.open("GET",strURL+"&bustcache="+new Date().getTime(),true);   
       xmlHttp.send(null);   
       }    
  }
  
  //This function checks for valid phone number onkeypress & onblur events
  function ValidPhone(element,strmsg)
  {
    if(PhoneValidation(element)==false)
      {
         document.getElementById('errdiv').innerHTML=strmsg;
         return false;
      }
      else
      {
         document.getElementById('errdiv').innerHTML='';     
         return true;   
      }       
  }
  


//----------------------------------autocomplete-----------------------------------------------

/**clears the values in combo box**/ 
function clearCombo(cmbid){
	   
      var x=document.getElementById(cmbid);
      var i;
      for (i=x.length;i>=0;i--){
        x.remove(i);
      }
  }
  
  // when country changes shld clear all values dependent on it
  function clearValue_Country()
  {  
    document.getElementById('cmbstate_html').value='';
    document.getElementById('cmbsuburb_html').value='';
    document.getElementById('cmbpostcode_html').value='';
    clearCombo('sel_sname');
    clearCombo('sel_suburbname');
    clearCombo('sel_postcodename');
    document.getElementById('cmbpostcode').value='';//hidden
    document.getElementById('cmbsuburb').value='';//hidden
    document.getElementById('cmbstate').value='';//hidden  
    document.getElementById("div_noMatchState").innerHTML=AUTO_CONST_STATE;
//    document.getElementById('div_noMatchPostcode').innerHTML=PINCODE_TIP;
  }
  
  // On focus on  state all other divs and values must disappear
  function clear_OnFocusState()
  {
    clearCombo('sel_suburbname');
    clearCombo('sel_postcodename');
    document.getElementById("sel_suburbname").style.display="none";
    document.getElementById("sel_postcodename").style.display="none";
  }
  
  // On focus on  state all other divs and values must disappear
  function clear_OnFocusSuburb()
  {
    clearCombo('sel_sname');
    clearCombo('sel_postcodename');
    document.getElementById("sel_sname").style.display="none";
    document.getElementById("sel_postcodename").style.display="none";
  }
  
  //  On focus on   state all other divs and values must disappear
  function clear_OnFocusPostcode()
  {
    clearCombo('sel_sname');
    clearCombo('sel_suburbname');
    document.getElementById("sel_sname").style.display="none";
    document.getElementById("sel_suburbname").style.display="none";
  }
  
  
  // when change in value of  state all other divs and values must disappear
  function clear_OnChangeValState(e)
  {
    var key;                                  
    if(window.event)
    key = window.event.keyCode;     //IE
    else
    key = e.which;     //firefox
    if(key!=13)// not on enter
    {  
      document.getElementById("div_noMatchSub").innerHTML=AUTO_CONST_SURB;
      document.getElementById('cmbsuburb_html').value='';
      document.getElementById('cmbpostcode_html').value='';
      clearCombo('sel_suburbname');
      clearCombo('sel_postcodename');
      document.getElementById('cmbstate').value='';//hidden
      document.getElementById('cmbsuburb').value='';//hidden
      document.getElementById('cmbpostcode').value='';//hidden
    }
  }
  
  // when change in value of  state all other divs and values must disappear
  function clear_OnChangeValSuburb(e)
  {
    var key;                                  
    if(window.event)
    key = window.event.keyCode;     //IE
    else
    key = e.which;     //firefox
    if(key!=13)// not on enter
    {    
      document.getElementById('cmbpostcode_html').value='';
      clearCombo('sel_postcodename');
      document.getElementById('cmbsuburb').value='';//hidden
      document.getElementById('cmbpostcode').value='';//hidden
    } 
  }
  
  // when change in value of  state all other divs and values must disappear
  function clear_OnChangeValPostcode(e)
  {
    var key;                                  
    if(window.event)
    key = window.event.keyCode;     //IE
    else
    key = e.which;     //firefox
    if(key!=13)// not on enter
    {   
      document.getElementById('cmbpostcode').value='';//hidden
    }
  }


// to put the selected value in the drop down list into the textbox
  function fill_st(e)
  {
    var key;                                  
    if(window.event)
    key = window.event.keyCode;     //IE
    else
    key = e.which;     //firefox
    
   // alert(key);
    var browser=navigator.appName;//for IE since IE click event is 0 (moz is 0 for scroll),scroll has no code
    if(browser=="Microsoft Internet Explorer")
    {
     if(key==0) key=1;// on click
    }
     
    if(key!=0)// if besides down/up arrow
    {  
      var pos=document.getElementById('sel_sname').selectedIndex;
      var id=document.getElementById('sel_sname').options[pos].value;
      var str=document.getElementById('sel_sname').options[pos].text;
      document.getElementById('cmbstate_html').value=str;
      document.getElementById('cmbstate').value=id;
      document.getElementById('cmbstate_html').focus();
      clearCombo('sel_sname');
      document.getElementById("sel_sname").style.display="none";
    }
    document.getElementById("div_noMatchState").innerHTML=AUTO_CONST_STATE;
  }

    
//ajax function to obtain all the states based on the chosen country and keep the user's state selected      
  function show_statename(str,e)
  {
     //IE
     var key; 
     key = e.keyCode;
     if(key<0) key = e.which;     //firefox 
     
    if(key!=13)// on enter key
    { 
      var xmlHttp; 
      var j = document.getElementById('cmbcountry').selectedIndex;
      var sel_country = document.getElementById('cmbcountry').options[j].value;
      //reset the suburb and postcode combo boxes
      if(trim(str)!=""){
      if(j>0){
        if(sel_country > 0)
        {
           document.getElementById('errdiv').innerHTML="";//if country selected
           var strUrl= "autocomplete_ajaxcode.php?country_id="+sel_country+"&ch="+str;
          
           xmlHttp=createRequestObject();   
           xmlHttp.open("GET",strUrl,true);       
           xmlHttp.onreadystatechange=function() {
                  if(xmlHttp.readyState==4) {         
                      if(xmlHttp.status==200)
                      {                     
                        if(xmlHttp.responseText!="")
                        {                          
                          document.getElementById("sel_sname").style.display="inline";
                          clearCombo('sel_sname');
                          //alert(xmlHttp.responseText);                    
                          eval("var response=("+xmlHttp.responseText+")"); 
                          for(i=0;i < response.states.list.length; i++) {                              
                            var y=document.createElement('option');
                            y.text=response.states.list[i].state;
                            y.value=response.states.list[i].id;                                  
                            
                            var x=document.getElementById('sel_sname');
                            if(y.value>0) 
                              try{
                              x.add(y,null); // standards compliant                                            
                              } catch(ex){ x.add(y); // IE only
                              } 
                            
                            if(i==0){y.selected=true;} // keep first one selected  
                            if(key==40) // if down arrow
                            {document.getElementById('sel_sname').focus();}                                  
                          }
                        
                         //To display msg if no auto complete drop down is shown
                         if(trim(document.getElementById("cmbstate_html").value)!="")
                         { 
                          //document.getElementById("div_noMatchState").innerHTML="";
                         }                                                
                        }
                        else  // no response
                        {
                           clearCombo('sel_sname');
                           document.getElementById("sel_sname").style.display="none";
                          
                        
                        //To display msg if no auto complete drop down is shown
                         if(trim(document.getElementById("cmbstate_html").value)!="")
                         { 
                          document.getElementById("div_noMatchState").innerHTML=AUTO_NO_STATE_FOUND;
                         } 
                         if(trim(document.getElementById("cmbstate_html").value)=="")
                         { 
                          document.getElementById("div_noMatchState").innerHTML=AUTO_CONST_STATE;
                         }
                        
                        }                            
                      }
                  }
          }
          xmlHttp.send(null); 
         }        
      }  
      else //no country selected
     {
      document.getElementById('div_noMatchState').innerHTML=AUTO_NO_COUNTRY_SEL;     
     }
     }else{
     document.getElementById("div_noMatchState").innerHTML=AUTO_CONST_STATE;
     }
    }//end of key!=13     
  }  
  
  
// to put the selected value in the drop down list into the textbox
  function fill_sub(e)
  {
    var key;                                  
    if(window.event)
    key = window.event.keyCode;     //IE
    else
    key = e.which;     //firefox
    
    var browser=navigator.appName;//for IE since IE click event is 0 (moz is 0 for scroll),scroll has no code
    if(browser=="Microsoft Internet Explorer")
    {
     if(key==0) key=1;// on click
    }
     
    if(key!=0)// if besides down/up arrow
    {  
      var pos=document.getElementById('sel_suburbname').selectedIndex;
      var id=document.getElementById('sel_suburbname').options[pos].value;
      var str=document.getElementById('sel_suburbname').options[pos].text;
      document.getElementById('cmbsuburb_html').value=str;
      document.getElementById('cmbsuburb').value=id;
      document.getElementById('cmbsuburb_html').focus();
      clearCombo('sel_suburbname');
      document.getElementById("sel_suburbname").style.display="none";
    } 
    document.getElementById("div_noMatchSub").innerHTML=AUTO_CONST_SURB;
  }
  
  //ajax function to obtain all the suburbs based on the chosen state and keep the user's suburb selected        
  function show_suburbname(str,e)
  {
     //IE
     var key; 
     key = e.keyCode;
     if(key<0) key = e.which;     //firefox 
    
    if(key!=13)// on enter key
    { 
      var xmlHttp; 
      var sel_state = document.getElementById('cmbstate').value;
       //reset the suburb and postcode combo boxes
    if(trim(str)!=""){
      if(sel_state>0){
      // if state is selected
          document.getElementById('errdiv').innerHTML="";
          var strUrl= "autocomplete_ajaxcode.php?sub_sid="+sel_state+"&ch="+str;
          
           xmlHttp=createRequestObject();   
           xmlHttp.open("GET",strUrl,true);       
           xmlHttp.onreadystatechange=function() {
                  if(xmlHttp.readyState==4) {         
                    if(xmlHttp.status==200)
                    {
                      if(xmlHttp.responseText!="")
                      {
                        document.getElementById("sel_suburbname").style.display="inline";
                        clearCombo('sel_suburbname');
                        //alert(xmlHttp.responseText);                    
                        eval("var response=("+xmlHttp.responseText+")"); 
                        for(var i=0;i < response.SubUrb.list.length; i++)
                          {
                            //create a combo box for suburbs
                            var y=document.createElement('option');
                            y.text=response.SubUrb.list[i].suburb;
                            y.value=response.SubUrb.list[i].subid;                                 
                            
                            var x=document.getElementById('sel_suburbname');
                            if(y.value>0) 
                              try{
                              x.add(y,null); // standards compliant                                            
                              } catch(ex){ x.add(y); // IE only
                              } 
                            
                            if(i==0){y.selected=true;} // keep first one selected  
                            
                            if(key==40) // if down arrow
                            {document.getElementById('sel_suburbname').focus();}                                  
                        }
                         //To display msg if no auto complete drop down is shown
                         if(trim(document.getElementById("cmbsuburb_html").value)!="")
                         { 
                         // document.getElementById("div_noMatchSub").innerHTML="";
                         }                        
                      }
                      else
                      {
                         clearCombo('sel_suburbname');
                         document.getElementById("sel_suburbname").style.display="none";
                         
                        //To display msg if no auto complete drop down is shown
                         if(trim(document.getElementById("cmbsuburb_html").value)!="")
                         { 
                          document.getElementById("div_noMatchSub").innerHTML=AUTO_NO_SUBURB_FOUND;
                         } 
                         if(trim(document.getElementById("cmbsuburb_html").value)=="")
                         { 
                          document.getElementById("div_noMatchSub").innerHTML=AUTO_CONST_SURB;
                         }                         
                      }                            
                    }
                  }
          }
          xmlHttp.send(null); 
       } 
       else //if no state is selected
       {
        document.getElementById('div_noMatchSub').innerHTML=AUTO_NO_STATE_SEL;
       }
       }
       else
       {
        document.getElementById("div_noMatchSub").innerHTML=AUTO_CONST_SURB;
       }
     }      
  } 
  

  
  // to put the selected value in the drop down list into the textbox
  function fill_postcode(e)
  {
    var key;                                  
    if(window.event)
    key = window.event.keyCode;     //IE
    else
    key = e.which;     //firefox
    
    var browser=navigator.appName;//for IE since IE click event is 0 (moz is 0 for scroll),scroll has no code
    if(browser=="Microsoft Internet Explorer")
    {
     if(key==0) key=1;// on click
    }
     
    if(key!=0)// if besides down/up arrow
    {  
      var pos=document.getElementById('sel_postcodename').selectedIndex;
      var id=document.getElementById('sel_postcodename').options[pos].value;
      var str=document.getElementById('sel_postcodename').options[pos].text;
      document.getElementById('cmbpostcode_html').value=str;
      document.getElementById('cmbpostcode').value=id;
      document.getElementById('cmbpostcode_html').focus();
      clearCombo('sel_postcodename');
      document.getElementById("sel_postcodename").style.display="none";      
    } 
  }
  
  
  //ajax function to obtain all the postcodes based on the chosen state and keep the user's postcode selected  
  function show_postcodename(str,e)
  {
     //IE
     var key; 
     key = e.keyCode;
     if(key<0) key = e.which;     //firefox 
    
    if(key!=13)// on enter key
    { 
      var xmlHttp; 
      var xmlHttp; 
      var sel_suburb = document.getElementById('cmbsuburb').value;
    if(trim(str)!=""){
      if(sel_suburb > 0)
      {
          document.getElementById('errdiv').innerHTML="";
          var strUrl= "autocomplete_ajaxcode.php?post_subid="+sel_suburb+"&ch="+str;
          
           xmlHttp=createRequestObject();   
           xmlHttp.open("GET",strUrl,true);       
           xmlHttp.onreadystatechange=function() {
                  if(xmlHttp.readyState==4) {         
                    if(xmlHttp.status==200)
                    {
                      if(xmlHttp.responseText!="")
                      {
                        document.getElementById("sel_postcodename").style.display="inline";
                        clearCombo('sel_postcodename');
                        //alert(xmlHttp.responseText);                    
                        eval("var response=("+xmlHttp.responseText+")"); 
                         for(var i=0;i < response.Postcode.list.length; i++) 
                         {
                              //create a combo box for postcodes
                            var y=document.createElement('option');
                            y.text=response.Postcode.list[i].postcode;
                            y.value=response.Postcode.list[i].postid;                                 
                            
                            var x=document.getElementById('sel_postcodename');
                            if(y.value>0) 
                              try{
                              x.add(y,null); // standards compliant                                            
                              } catch(ex){ x.add(y); // IE only
                              } 
                            
                            if(i==0){y.selected=true;} // keep first one selected 
                            
                            if(key==40) // if down arrow
                            {document.getElementById('sel_postcodename').focus();}                                  
                        }                       
                      }
                      else
                      {
                         clearCombo('sel_postcodename');
                         document.getElementById("sel_postcodename").style.display="none";                                             
                      }                            
                    }
                  }
          }
          xmlHttp.send(null);        
      }
      var j = document.getElementById('cmbcountry').selectedIndex;
      // if country is selected( since it can be user entered also for that country)
      if(j>0)
      {
        document.getElementById('errdiv').innerHTML="";
      }
      else
      {
//        document.getElementById('div_noMatchPostcode').innerHTML=AUTO_NO_POSTCODE_SEL;
      }
      }
      else
      {
//        document.getElementById('div_noMatchPostcode').innerHTML=PINCODE_TIP;
      }
     }       
  } 



    var TAB = 9;//some random value
    // function to detect tab
    function catchTAB(evt,elem)
    {
    //alert(evt.keyCode);    
      var keyCode;
      var tab_ff;
      if ("which" in evt)
      {// NN4 & FF &amp; Opera
        keyCode=evt.which;
      } else if ("keyCode" in evt)
      {// Safari & IE4+
        keyCode=evt.keyCode;
        tab_ff=1; //to detect tab for firefox
      } else if ("keyCode" in window.event)
      {// IE4+
        keyCode=window.event.keyCode;
      } else if ("which" in window.event)
      {
        keyCode=evt.which;
      } else  {    alert("the browser don't support");  }
    
      if (keyCode == TAB)
      {        
         if(elem=="0")//suburb
        {
          var pos=document.getElementById('sel_suburbname').selectedIndex;
          var id=document.getElementById('sel_suburbname').options[pos].value;
          var str=document.getElementById('sel_suburbname').options[pos].text;
          document.getElementById('cmbsuburb_html').value=str;
          document.getElementById('cmbsuburb').value=id;
          if(tab_ff==1){  // tab for ff was not working 
            document.getElementById('cmbsuburb_html').focus();
          }
          clearCombo('sel_suburbname');
          document.getElementById("sel_suburbname").style.display="none"; 
          document.getElementById("div_noMatchSub").innerHTML=AUTO_CONST_SURB;          
        }
        else if(elem=="1")//state
        {
          //put in value tht is selectedwith tab
          var pos=document.getElementById('sel_sname').selectedIndex;
          var id=document.getElementById('sel_sname').options[pos].value;
          var str=document.getElementById('sel_sname').options[pos].text;           
          document.getElementById('cmbstate_html').value=str;
          document.getElementById('cmbstate').value=id;
          if(tab_ff==1){  // tab for ff was not working 
          document.getElementById('cmbstate_html').focus();
          }
          clearCombo('sel_sname');
          document.getElementById("sel_sname").style.display="none";        
          document.getElementById("div_noMatchState").innerHTML=AUTO_CONST_STATE;          
        }
        else //postcode
        {
          var pos=document.getElementById('sel_postcodename').selectedIndex;
          var id=document.getElementById('sel_postcodename').options[pos].value;
          var str=document.getElementById('sel_postcodename').options[pos].text;
          document.getElementById('cmbpostcode_html').value=str;
          document.getElementById('cmbpostcode').value=id;
          if(tab_ff==1){  // tab for ff was not working 
          document.getElementById('cmbpostcode_html').focus();
          }
          clearCombo('sel_postcodename');
          document.getElementById("sel_postcodename").style.display="none";
        }
      
      }
    }

