/* itaURL - URL for updating cart Items */
var itaURL = "italia.php";

var xmlHttp = createXmlHttpRequestObject();

/* creates an XMLHttpRequest instance */
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error Item
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

//SPEDIZIONE

function get_city(inObjProvince)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
	  var ProvinceCode = inObjProvince.value;
	  if (ProvinceCode != 0){
		// create the params string
		var params = "?action=get_city&province_code=" + ProvinceCode;
		// initiate the asynchronous HTTP request
		xmlHttp.open("GET", itaURL + params, true);
		xmlHttp.onreadystatechange = handleRequestStateChange_city;
		xmlHttp.send(null);
	  }else {
		CitySelect = document.getElementById("sh_city");
		CitySelect.options.length=0;
		CitySelect.options[0] = new Option('- - -',0);
		Cap = document.getElementById("sh_cap");
		Cap.value = '';
	  }
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChange_city() 
{
  if (xmlHttp.readyState == 1 || xmlHttp.readyState == 2 || xmlHttp.readyState == 3) 
  {
	document.body.style.cursor = 'wait';
  }
  // when readyState is 4, we are ready to read the server response  
  if (xmlHttp.readyState == 4) 
  { //alert(xmlHttp.status);
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponse_city();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
    }
  }
}

// handles the response received from the server
function handleServerResponse_city()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  //catturo errori con IE e opera
  if (!xmlResponse || !xmlResponse.documentElement){
	  throw("Struttura XML non valida:\n" + xmlHttp.responseText);
  }
  //catturo gli errori con Firefox
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror"){
	  throw("Struttura XML non valida:\n" + xmlHttp.responseText);
  }
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;  
  // obtain array with city, cap, id
  idArray = xmlRoot.getElementsByTagName("id_city");
  cityArray = xmlRoot.getElementsByTagName("city");
  capArray = xmlRoot.getElementsByTagName("cap");

  // display
  CitySelect = document.getElementById("sh_city");
  CitySelect.options.length=0;
	
  CitySelect.options[0] = new Option('- - -',0);
  for (var i=0; i<idArray.length; i++)
    CitySelect.options[CitySelect.options.length] = new Option(cityArray.item(i).firstChild.data, cityArray.item(i).firstChild.data);

  Cap = document.getElementById("sh_cap");
  Cap.value = '';
  document.frmBasket.stpr.value = document.frmBasket.stpr_ajax.value;

  
  /*for(i=0;i<arrValori.length;i++) {
   document.form1.citta.options[document.form1.citta.options.length] = new Option(arrValori[i].split(”|”),arrValori[i].split(”|”));*/

  document.body.style.cursor = 'auto';
}

function get_cap(inObjCity)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
	  var City = inObjCity.value;
	  var Prov = document.getElementById('stpr').value;
	  if (City != 0){
		// create the params string
		var params = "?action=get_cap&city=" + City + "&province=" + Prov;
		// initiate the asynchronous HTTP request
		xmlHttp.open("GET", itaURL + params, true);
		xmlHttp.onreadystatechange = handleRequestStateChange_cap;
		xmlHttp.send(null);
	  }else {
		Cap = document.getElementById("sh_cap");
		Cap.value = '';
	  }
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChange_cap() 
{
  if (xmlHttp.readyState == 1 || xmlHttp.readyState == 2 || xmlHttp.readyState == 3) 
  {
	document.body.style.cursor = 'wait';
  }
  // when readyState is 4, we are ready to read the server response  
  if (xmlHttp.readyState == 4) 
  { //alert(xmlHttp.status);
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
		  handleServerResponse_cap();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
    }
  }
}

// handles the response received from the server
function handleServerResponse_cap()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  //catturo errori con IE e opera
  if (!xmlResponse || !xmlResponse.documentElement){
	  throw("Struttura XML non valida:\n" + xmlHttp.responseText);
  }
  //catturo gli errori con Firefox
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror"){
	  throw("Struttura XML non valida:\n" + xmlHttp.responseText);
  }
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;  
  // obtain array with city, cap, id
  idArray = xmlRoot.getElementsByTagName("id_city");
  cityArray = xmlRoot.getElementsByTagName("city");
  capArray = xmlRoot.getElementsByTagName("cap");

  // display
  /*CitySelect = document.getElementById("sh_city");
  CitySelect.options.length=0;
	
  CitySelect.options[0] = new Option('- - -',0);
  for (var i=0; i<idArray.length; i++)
    CitySelect.options[CitySelect.options.length] = new Option(cityArray.item(i).firstChild.data, cityArray.item(i).firstChild.data);*/

  Cap = document.getElementById("sh_cap");
  Cap.value = capArray.item(0).firstChild.data;
  document.frmBasket.stci.value = cityArray.item(0).firstChild.data
  
  /*for(i=0;i<arrValori.length;i++) {
   document.form1.citta.options[document.form1.citta.options.length] = new Option(arrValori[i].split(”|”),arrValori[i].split(”|”));*/

  document.body.style.cursor = 'auto';
}

//FATTURAZIONE

function get_city_bt(inObjProvince)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
	  var ProvinceCode = inObjProvince.value;
	  if (ProvinceCode != 0){
		// create the params string
		var params = "?action=get_city&province_code=" + ProvinceCode;
		// initiate the asynchronous HTTP request
		xmlHttp.open("GET", itaURL + params, true);
		xmlHttp.onreadystatechange = handleRequestStateChange_city_bt;
		xmlHttp.send(null);
	  }else {
		CitySelect = document.getElementById("pa_city");
		CitySelect.options.length=0;
		CitySelect.options[0] = new Option('- - -',0);
		Cap = document.getElementById("bt_cap");
		Cap.value = '';
	  }
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChange_city_bt() 
{
  if (xmlHttp.readyState == 1 || xmlHttp.readyState == 2 || xmlHttp.readyState == 3) 
  {
	document.body.style.cursor = 'wait';
  }
  // when readyState is 4, we are ready to read the server response  
  if (xmlHttp.readyState == 4) 
  { //alert(xmlHttp.status);
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponse_city_bt();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
    }
  }
}

// handles the response received from the server
function handleServerResponse_city_bt()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  //catturo errori con IE e opera
  if (!xmlResponse || !xmlResponse.documentElement){
	  throw("Struttura XML non valida:\n" + xmlHttp.responseText);
  }
  //catturo gli errori con Firefox
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror"){
	  throw("Struttura XML non valida:\n" + xmlHttp.responseText);
  }
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;  
  // obtain array with city, cap, id
  idArray = xmlRoot.getElementsByTagName("id_city");
  cityArray = xmlRoot.getElementsByTagName("city");
  capArray = xmlRoot.getElementsByTagName("cap");

  // display
  CitySelect = document.getElementById("bt_city");
  CitySelect.options.length=0;
	
  CitySelect.options[0] = new Option('- - -',0);
  for (var i=0; i<idArray.length; i++)
    CitySelect.options[CitySelect.options.length] = new Option(cityArray.item(i).firstChild.data, cityArray.item(i).firstChild.data);

  Cap = document.getElementById("bt_cap");
  Cap.value = '';
  document.frmBasket.btpr.value = document.frmBasket.btpr_ajax.value;

  document.body.style.cursor = 'auto';
}

function get_cap_bt(inObjCity)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
	  var City = inObjCity.value;
	  var Prov = document.getElementById('btpr').value;
	  if (City != 0){
		// create the params string
		var params = "?action=get_cap&city=" + City + "&province=" + Prov;
		// initiate the asynchronous HTTP request
		xmlHttp.open("GET", itaURL + params, true);
		xmlHttp.onreadystatechange = handleRequestStateChange_cap_bt;
		xmlHttp.send(null);
	  }else {
		Cap = document.getElementById("bt_cap");
		Cap.value = '';
	  }
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChange_cap_bt() 
{
  if (xmlHttp.readyState == 1 || xmlHttp.readyState == 2 || xmlHttp.readyState == 3) 
  {
	document.body.style.cursor = 'wait';
  }
  // when readyState is 4, we are ready to read the server response  
  if (xmlHttp.readyState == 4) 
  { //alert(xmlHttp.status);
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponse_cap_bt();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
    }
  }
}

// handles the response received from the server
function handleServerResponse_cap_bt()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  //catturo errori con IE e opera
  if (!xmlResponse || !xmlResponse.documentElement){
	  throw("Struttura XML non valida:\n" + xmlHttp.responseText);
  }
  //catturo gli errori con Firefox
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror"){
	  throw("Struttura XML non valida:\n" + xmlHttp.responseText);
  }
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;  
  // obtain array with city, cap, id
  idArray = xmlRoot.getElementsByTagName("id_city");
  cityArray = xmlRoot.getElementsByTagName("city");
  capArray = xmlRoot.getElementsByTagName("cap");

  Cap = document.getElementById("bt_cap");
  Cap.value = capArray.item(0).firstChild.data;
  document.frmBasket.btci.value = cityArray.item(0).firstChild.data
  
  document.body.style.cursor = 'auto';
}

//PARTE IN SVILUPPO

function doAction(inAction, inObj, inFormName){
	
	
	switch (inAction)
	{
	case 'get_city':
		switch (inFormName)
		{
		case 'st':
			
		break;

		case 'bt':

		break;
		}
	break;

	case 'get_cap':
		switch (inFormName)
		{
		case 'st':
			
		break;

		case 'bt':

		break;
		}
	break;
	}
	
}
