function zip_codes(target_state_id, target_city_id)
{
	this.target_state_id   	= target_state_id;
	this.target_city_id   	= target_city_id;
	this.projecroot		 	= "";
	this.zip_code           = "";
	this.country            = "";
	this.load			    = zip_loadState;
	this.getData            = zip_getServerData;
}	

function zip_loadState(country, zip_code)
{
  this.country = country;
  this.zip_code = zip_code;
  
  var criteria = 	"state_id="+this.target_state_id +
					"&city_id="+this.target_city_id +
					"&zip_code="+this.zip_code +
					"&country="+this.country;
					

  // disabilita i campetti
  //document.getElementById(this.target_state_id).value = 'loading...';
  //document.getElementById(this.target_city_id).value = 'loading...';
  
  this.getData(criteria);
  
}	

function zip_getServerData(psCriteria) {


  document.body.style.cursor='wait';
   if(document.all){
   	var sURL = this.projectroot + "pages/serverdata/zip_code.php?" + psCriteria
      oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );
	}else{
		var sURL = this.projectroot + "pages/serverdata/zip_code.php?" + psCriteria
		oXMLHTTP = new XMLHttpRequest
	}

    oXMLHTTP.open( "POST", sURL, true );
    oXMLHTTP.onreadystatechange = zip_manageStateChange;

    try {
         oXMLHTTP.send(null);
    }
    catch (e) {
         alert("Server is not available at this time to process your request.");
    }
}


function zip_manageStateChange() {

     switch (oXMLHTTP.readyState) {

          case 4:     // --- RESPONSE FROM SERVER HAS COMPLETED ---

              document.body.style.cursor='auto';

              //document.getElementById("divProgress").style.display = "none";

               var list = oXMLHTTP.responseText;
               
               //alert (strCommaDelimList);
               populateFields(list);

               oXMLHTTP = null

               break;
    }
}

function populateFields(list)
{
	//alert ("*"+list+"*");
	var returnData = list.split('#');
	//alert (returnData.length)
	if (returnData.length == 2)
	{
		var state = returnData[0].split('=');
		var city  = returnData[1].split('=');
		
		if (state.length == 2 && state[1] != "")
		{
			//alert ('ho lo stato in' +  state[0]);
			// imposta lo stato
   			 document.getElementById(state[0]).value = state[1];
		}	
		if (city.length == 2 && city[1] != "")
		{
			//alert ('ho la cittą');
			// imposta lo stato
   			 document.getElementById(city[0]).value = city[1];
		}
	}	
	else
	{
			// nothing to do
	}	
	
}	
