/*  
  Developed by Zeal Neo Technologies
  info@zealneotech.com 
  http://www.zealneotech.com
  You can use this script until you don't remove this comment from the script
*/ 
  
//alert('added file');

function updateState(str, targetId, TargetFieldName, filename, format, warnMsgsTrgt)
{                                   
  var xmlhttp;
  var respTargetId; //id of the element where response from server would be pushed
  var inputFieldName ; // to put the name of the select tag we would create
  var serverHandler;  // file name which would handle the AJAX request
  var formatResp;
  var warnMsgsTrgtId;
  //alert(' params '+str+ ' params '+targetId+ ' params '+TargetFieldName+ ' params '+filename);
  if(!(str && targetId && TargetFieldName && filename))
  {
    alert('Provide all inputs for AJAX to work.');
    return;
  }
  
  respTargetId = targetId;
  inputFieldName = TargetFieldName;
  serverHandler = filename;
  formatResp = format;
  warnMsgsTrgtId = warnMsgsTrgt;
   
  if (str.length==0)
  {
    document.getElementById(targetId).innerHTML="";
    //document.getElementById(targetId).focus();
    return;
  }
  xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null)
  {
    alert ("Your browser does not support XMLHTTP!");
    return;
  }
  //var url="getStatesForThisCountry.php";
  var url=serverHandler;
  url=url+"?"+str;
  url=url+"&sid="+Math.random();   //to prevent the server from using a cached file
  xmlhttp.onreadystatechange=function() //anonymous function 
    {
      //if (xmlhttp.readyState==4)
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
        //alert('resp '+ xmlhttp.responseText);
        var textToPush = '';
        if( formatResp.toLowerCase() == "select".toLowerCase() ) 
        {
          var country_selectbox = document.getElementById('prop_country');
          //var country_name = country_selectbox.options[country_selectbox.selectedIndex].text;
    
          var stateEle = document.getElementById('prop_state');
          
          if( country_selectbox && document.getElementById('prop_state') && (country_selectbox.options[country_selectbox.selectedIndex].text.toLowerCase() == 'italy') )
          {
            if( (inputFieldName.toLowerCase() =='prop_state') ) //if combo box being prepared for states
              textToPush += '<select name="'+inputFieldName+'" id="'+inputFieldName+'" autocomplete="off" class="inputfield" onchange="updateState(\'stateid=\'+document.getElementById(\'prop_state\').value, \'cityDiv\', \'prop_suggestions_city\', \'getCitiesForThisState.php\', \'select\', \'city_suggestions_warn\');">';        
            else if( (inputFieldName.toLowerCase() =='prop_suggestions_city') ) //if combo box being prepared for cities
              textToPush += '<select name="'+inputFieldName+'" id="'+inputFieldName+'" autocomplete="off" class="inputfield" onchange="javascript: document.getElementById(\'prop_city\').value=this.options[this.selectedIndex].text;" style="background-color: #eee;">';
          }
          else
          {  
            textToPush += '<select name="'+inputFieldName+'" id="'+inputFieldName+'" autocomplete="off" class="inputfield">';
          }
          textToPush += xmlhttp.responseText;
          textToPush += '</select>';
          //textToPush = textToPush.replace(/'/g, "\'");    
        }
        else
        {
          textToPush += xmlhttp.responseText;
        }
    
        if( document.getElementById(respTargetId) )
        { 
          document.getElementById(respTargetId).innerHTML = textToPush ;
                 
          if(warnMsgsTrgtId && document.getElementById(warnMsgsTrgtId))
            document.getElementById(warnMsgsTrgtId).innerHTML = '' ;
    
          if( (inputFieldName.toLowerCase() =='prop_state') )
          {
            updateState('stateid='+document.getElementById('prop_state').value, 'cityDiv', 'prop_suggestions_city', 'getCitiesForThisState.php', 'select', 'city_suggestions_warn');
          } 
        
        }
        else
          alert('Target to show result does not exist');
    
    
        //document.getElementById(respTargetId).innerHTML = xmlhttp.responseText;    
      }
      else
      {
        if(warnMsgsTrgtId && document.getElementById(warnMsgsTrgtId))
          document.getElementById(warnMsgsTrgtId).innerHTML = '<img src="img/loading.gif" title="Processing..." alt="Processing...">' ;
      }
    }
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

function GetXmlHttpObject()
{
  if (window.XMLHttpRequest)     // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
    
  if (window.ActiveXObject)     // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
    
  return null;
}

