// JavaScript Document
try{
    var xmlhttpcidade = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttpcidade = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttpcidade = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttpcidade = false;
        }
    }
}

//Fila de conexões
var filacid=[];
var ifilacid=0;

//Carrega via XMLHTTP a url recebida e coloca seu valor
//no objeto com o id recebido
function carregaCidades(uf, id, vlrDefault, rootFolder) {
	if ( uf != '' ) {
		//Adiciona à fila
		filacid[filacid.length]=[id, (rootFolder ? '' : '../') + '/wp-content/themes/parador10/municipios/' + uf.toLowerCase() + '.xml', vlrDefault];
		//Se não há conexões pendentes, executa
		if((ifilacid+1)==filacid.length) ajaxRunCidade();
	} else { 
		//alert('Estado inválido.'); 
		input = document.getElementById(id);
		input.options.length = 0;
		input.options[0] = new Option(" ", '0'); 
	}
}

//Executa a próxima conexão da fila
function ajaxRunCidade(){
    //Abre a conexão
	//alert(fila[ifila][1]);
    xmlhttpcidade.open("GET",filacid[ifilacid][1],true);
    //Função para tratamento do retorno
    xmlhttpcidade.onreadystatechange=function() {
        if (xmlhttpcidade.readyState==4 && xmlhttpcidade.status == 200) {
            //Mostra o HTML recebido
            //--  retorno=unescape(xmlhttp.responseText.replace(/\+/g," "))
			var result = xmlhttpcidade.responseXML;
			var cidXml = result.getElementsByTagName("cid");
			input = document.getElementById(filacid[ifilacid][0]);
			input.options.length = 0;
			input.disabled = true;
			input.options[0] = new Option("Escolha uma cidade", ''); 

			for (var x = 0; x < cidXml.length; x++) {
				//cidXml[x].getAttribute('cod');
				input.options[x+1] = new Option(cidXml[x].firstChild.nodeValue, cidXml[x].firstChild.nodeValue); 
			}
			input.disabled = false;
			if (filacid[ifilacid][2] > 0) input.value = filacid[ifilacid][2];

            //Roda o próximo
            ifilacid++;
            if(ifilacid<filacid.length)setTimeout("ajaxRunCidade()",20);
        }
    }
    //Executa
    xmlhttpcidade.send(null);
}

function doEmail(pStr) {
	var reEmail = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;	
	if (reEmail.test(pStr)) return true;
	else return false;
}

function IsEmpty(field) {
    var total = jQuery(field).length;
    var value = jQuery(field).val();
    
    if(total == 0 || value == null || value == "") {
		jQuery(field).focus();
		return true;
	} else return false;
}

function IsEmptyTrip(field) {
    var total = jQuery(field).length;
    var value = jQuery(field).val();
    
    if(total == 0 || value == null || value == "") {
  	return true;
	} else return false;
}

function validFormTrip() {
  var email = jQuery("#email").val();
  if(!IsEmptyTrip("#nome") && !IsEmptyTrip("#email") && doEmail(email) && !IsEmptyTrip("#pais") && !IsEmptyTrip("#telefone") && !IsEmptyTrip("#mensagem")) return true;
  else {
    jQuery("#fieldErros").fadeIn("fast");
      //Erro da mensagem
      if(IsEmptyTrip("#mensagem")) {
          jQuery("#e6").fadeIn("fast");
      } else jQuery("#e6").fadeOut("fast");
      
      //Erros do email Empty e invalido
      if(IsEmptyTrip("#email")) {
          jQuery("#e5").hide();
          jQuery("#e4").fadeIn("fast");
      } else {
          if(!doEmail(email)) {
            jQuery("#e5").fadeIn("fast");
          } else jQuery("#e5").fadeOut("fast");
          jQuery("#e4").hide();
      }
      
      //Erro do telefone
      if(IsEmptyTrip("#telefone")) {
          jQuery("#e3").fadeIn("fast");
      } else jQuery("#e3").fadeOut("fast");
      
      //Erro do pais
      if(IsEmptyTrip("#pais")) {
          jQuery("#e2").fadeIn("fast");
      } else jQuery("#e2").fadeOut("fast");    
      
      //Erro do nome
      if(IsEmptyTrip("#nome")) {
          jQuery("#e1").fadeIn("fast");
      } else jQuery("#e1").fadeOut("fast");      
  
      return false;
  } //fimelse
}
