var HttpReq = null;
var from_combo = null;
var dest_combo = null;
var set_combo = null;
window.ajax_testOK = false;
var from_text = null;
var current_value = null;
var HttpReq_piva = null;
var HttpReq_return = 'no';
function ajaxComboBox(comboin, url, comboBox, preset, sync)
{
 from_combo = comboin;
 dest_combo = comboBox;
 set_combo = preset;
 var indice = document.getElementById(from_combo).selectedIndex;
 var sigla = document.getElementById(from_combo).options[indice].getAttribute('value');
 url = url + '&uf=' + sigla;
 if (document.getElementById) 
 { //Verifica se o Browser suporta DHTML.
  if (window.XMLHttpRequest) 
  {
   HttpReq = new XMLHttpRequest();
   HttpReq.onreadystatechange = XMLHttpRequestChange;
   HttpReq.open("GET", url, sync);
   HttpReq.send(null);
  } 
  else if (window.ActiveXObject) 
  {
   HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
   if (HttpReq) 
   {
    HttpReq.onreadystatechange = XMLHttpRequestChange;
    HttpReq.open("GET", url, sync);
    HttpReq.send();
   }
  }
 }
}

function ajaxText(textin, url, sync)
{
 from_text = textin;
 var sigla = document.getElementById(textin).value;
 url = url + '&uf=' + sigla;
 current_value = sigla;
 if (document.getElementById) 
 { //Verifica se o Browser suporta DHTML.
  if (window.XMLHttpRequest)
  {
   HttpReq = new XMLHttpRequest();
   HttpReq.onreadystatechange = XMLHttpRequestUname;
   HttpReq.open("GET", url, sync);
   HttpReq.send(null);
  } 
  else if (window.ActiveXObject) 
  {
   HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
   if (HttpReq) 
   {
    HttpReq.onreadystatechange = XMLHttpRequestUname;
    HttpReq.open("GET", url, sync);
    HttpReq.send();
   }
  }
 }
}

function XMLHttpRequestUname()
{
 if (HttpReq.readyState == 4 && HttpReq.status == 200)
 {
  var result = HttpReq.responseXML;
  var cidades = result.getElementsByTagName('nome').item(0);
  if (cidades.firstChild.data == 'none' && current_value.length > 3)
  {
   document.getElementById(from_text).className = 'OKHilite';
   ajax_testOK = true;
  }
  else
  {
   document.getElementById(from_text).className = 'errHilite';
   ajax_testOK = false;
  }
 }
}

function XMLHttpRequestChange() 
{
 if (HttpReq.readyState == 4 && HttpReq.status == 200)
 {  //Verifica se o arquivo foi carregado com sucesso.
  var result = HttpReq.responseXML;
  var cidades = result.getElementsByTagName("nome");
  document.getElementById(dest_combo).innerHTML = "";
  for (var i = 0; i < cidades.length; i++) 
  {
   new_opcao = create_opcao(cidades[i]);
   document.getElementById(dest_combo).appendChild(new_opcao);
  }
 }
}

function create_opcao(cidade) 
{ //Cria um novo elemento OPTION.
 //return opcao.cloneNode(true);
 var new_opcao = document.createElement("option"); //Cria um OPTION.
 var texto = document.createTextNode(cidade.childNodes[0].data); //Cria um texto.
 new_opcao.setAttribute("value",cidade.getAttribute("id")); //Adiciona o atributo de valor a nova opção.
 if (set_combo == cidade.getAttribute("id") && set_combo != '') new_opcao.setAttribute("selected",true);
 new_opcao.appendChild(texto); //Adiciona o texto a OPTION.
 return new_opcao; // Retorna a nova OPTION.
}

function set_first(comboname)
{
 document.getElementById(comboname).selectedIndex = 0;
}

function set_piva_code(from_combo,piva_code_id,currentlanguage)
{
 var pasei_cee = { 'Austria':'AT','Belgium':'BE','Bulgaria':'BG','Cyprus':'CY','Czech Republic':'CZ','Denmark':'DK','Estonia':'EE',
 	'Finland':'FI','France':'FR','Germany':'DE','Greece':'EL','Hungary':'HU','Ireland':'IE','Italy':'IT','Latvia':'LV','Lithuania':'LT','Luxembourg':'LU',
 	'Malta':'MT','Netherlands':'NL','Poland':'PL','Portugal':'PT','Romania':'RO','Slovakia':'SK','Slovenia':'SI','Spain':'ES','Sweden':'SE',
 	'United Kingdom':'UK' };
 var indice_piva_code = { 'AT':1,'BE':2,'BG':3,'CY':4,'CZ':5,'DK':6,'EE':7,'FI':8,'FR':9,'DE':10,'EL':11,'HU':12,'IE':13,'IT':14,'LV':15,'LT':16,'LU':17,'MT':18,'NL':19,'PL':20,'PT':21,'RO':22,'SK':23,'SI':24,'ES':25,'SE':26,'UK':27 }
 var indice = document.getElementById(from_combo).selectedIndex;
 var nazione = document.getElementById(from_combo).options[indice].getAttribute('value');
 if (pasei_cee[nazione] != '')
 {
 	document.getElementById(piva_code_id).selectedIndex = indice_piva_code[pasei_cee[nazione]];
 	if (nazione == 'Italy')
 	{
 	 document.getElementById('codicefiscale').alt= 'piva_cee|needed|'+currentlanguage;
 	 //document.getElementById('matricola').alt= 'cf|needed';
 	 document.getElementById('matricola').alt= 'blank';
 	 document.getElementById('matricola').style.textTransform = 'uppercase';
 	 if (currentlanguage == 'italian') document.getElementById('matricola').emsg= 'Per piacere inserisci un Codice Fiscale corretto!';
 	 else document.getElementById('matricola').emsg= 'Please, insert a correct ID Card Number!';
  }
  else
  {
   document.getElementById('codicefiscale').alt= 'piva_cee|xxx|'+currentlanguage;
   document.getElementById('matricola').alt= '';
  }
 }
 else document.getElementById(piva_code_id).selectedIndex = 0;	
 //document.getElementById('codicefiscale').value = '';	
}

function ajaxTextAudio(url, sync)
{
 url = url+'?time='+(new Date()).valueOf();
 if (document.getElementById) 
 { //Verifica se o Browser suporta DHTML.
  if (window.XMLHttpRequest) 
  {
   HttpReq = new XMLHttpRequest();
   HttpReq.onreadystatechange = XMLHttpRequestAudio;
   HttpReq.open("GET", url, sync);
   HttpReq.send(null);
  } 
  else if (window.ActiveXObject) 
  {
   HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
   if (HttpReq) 
   {
    HttpReq.onreadystatechange = XMLHttpRequestAudio;
    HttpReq.open("GET", url, sync);
    HttpReq.send();
   }
  }
 }
}

function XMLHttpRequestAudio()
{
 if (HttpReq.readyState == 4 && HttpReq.status == 200)
 {
  var result = HttpReq.responseXML;
  var cidades = result.getElementsByTagName('nome').item(0);
  dest_combo = cidades.firstChild.data;
 }
}


