// JavaScript Document
function validarEmail(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
	    return true
	} else {
	   return false;
	}
}


// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresearch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// And thanks to everyone else who has provided comments and suggestions.
// ====================================================================
function URLEncode( ){
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = document.URLForm.F1.value;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

    document.URLForm.F2.value = encoded;
    document.URLForm.F2.select();
	return false;
}

function URLDecode(cadena){
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = cadena;  // document.URLForm.F2.value; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   //document.URLForm.F1.value = plaintext;
   //document.URLForm.F1.select();
   return plaintext;
}



function valida_envio(){
	if($("#id_nombre").val()==""){
	   alert("Ingresa el nombre");
	   $("#id_nombre").focus();
	   return false;
	}
	if($("#id_telefono").val()==""){
	   alert("Ingresa el teléfono");
	   $("#id_telefono").focus();
	   return false;
	}
	if($("#id_mail").val()==""){
	   alert("Ingresa el email");
	   $("#id_mail").focus();
	   return false;
	}else if(!validarEmail($("#id_mail").val())){
	   alert("El email es incorrecto");
	   $("#id_mail").focus();
	   return false;	
	}
	if($("#id_residencia").val()==""){
	   alert("Selecciona el estado");
	   $("#id_residencia").focus();
	   return false;
	}
	if($("#id_servicio").val()==""){
	   alert("Ingrese el servicio que requiere");
	   $("#id_servicio").focus();
	   return false;
	}
	if($("#id_lugar_tramite").val()==""){
	   alert("Selecciona el lugar donde necesita su trámite");
	   $("#id_lugar_tramite").focus();
	   return false;
	}
	if($("#id_tiempo").val()==""){
	   alert("Selecciona el tiempo de consideración");
	   $("#id_tiempo").focus();
	   return false;
	}
}



function valida_contacto(){
	if($("#id_nombre").val()==""){
	   alert("Ingresa tu nombre");
	   $("#id_nombre").focus();
	   return false;
	}
	if($("#id_telefono").val()==""){
	   alert("Ingresa tu teléfono");
	   $("#id_telefono").focus();
	   return false;
	}
	if($("#id_mail").val()==""){
	   alert("Ingresa tu email");
	   $("#id_mail").focus();
	   return false;
	}else if(!validarEmail($("#id_mail").val())){
	   alert("El email es incorrecto");
	   $("#id_mail").focus();
	   return false;	
	}
	if($("#id_pais").val()==""){
	   alert("Ingresa tu país");
	   $("#id_pais").focus();
	   return false;
	}
	if($("#id_estado").val()==""){
	   alert("Ingresa tu estado");
	   $("#id_estado").focus();
	   return false;
	}
	if($("#id_comentario").val()==""){
	   alert("Ingresa tu comentario");
	   $("#id_comentario").focus();
	   return false;
	}
	return true;
}




function valida_contacto_franquicia(){
	if($("#id_nombre").val()==""){
	   alert("Ingresa tu nombre");
	   $("#id_nombre").focus();
	   return false;
	}
	if($("#id_ciudad").val()==""){
	   alert("Ingresa tu ciudad");
	   $("#id_ciudad").focus();
	   return false;
	}
	if($("#id_telefono").val()==""){
	   alert("Ingresa tu teléfono");
	   $("#id_telefono").focus();
	   return false;
	}
	if($("#id_email").val()==""){
	   alert("Ingresa tu email");
	   $("#id_email").focus();
	   return false;
	}else if(!validarEmail($("#id_email").val())){
	   alert("El email es incorrecto");
	   $("#id_email").focus();
	   return false;	
	}
	return true;
}


$(document).ready(function(){ 
  $("#maestra").click(function(){ 
	  $("#detalle_maestra").show();
	  $("#detalle_prefesionista").hide();
  });//change
});//ready

$(document).ready(function(){ 
  $("#profesionista").click(function(){ 
	  $("#detalle_prefesionista").show();
	  $("#detalle_maestra").hide();
  });//change
});//ready


function verifica(){

	if($("#idpais_compra").val()=="") {
	   alert("Debes seleccionar el país");
	   $("#idpais_compra").focus();
	   return false;
	}
	
	if(!document.forma1.politicas.checked){
	   alert("Debes de aceptar las políticas de la empresa para poder continuar");	
	   return false;
	}
    return true;
}


function valida_seleccion(total){
	 selecciona=false;
	 if(total>1){
		 for(i=0; i<total;i++) {
		   if(document.forma1.producto[i].checked){
			   selecciona=true;
		   }
		 }
	 }else{
		if( document.forma1.producto.checked){
			selecciona=true;
		}
	 }
	 if(!selecciona){
		alert("Selecciona un producto de la lista");
		return false;
	 }
	 
	 return true;
}


function valida_paso3(){
   if($("#id_estado_solicita").val()=="") {
	   alert("Selecciona el estado");	
	   $("#id_estado_solicita").focus();
	   return false;
   }
   if($("#id_municipio").val()=="") {
	   alert("Selecciona el municipio");	
	   $("#id_municipio").focus();
	   return false;
   }else if($("#id_municipio").val()=="Otro") {
	  if($("#id_otro").val()==""){
	      alert("Ingresa el municipio");	
	      $("#id_otro").focus();
	      return false;
	  }
   }
   return true;
}

function carga_opciones_usa(){
	   if($("#id_copias").val()==2){
		   $("#idContentCondado").show();
		   
	   }else{
		   $("#idContentCondado").hide(); 
		   document.forma1.tipo_acta[0].checked=false;
		   document.forma1.tipo_acta[1].checked=false;
		   $("#idContentPostilla").html('<select name="tramite_postilla" id="id_tramite_postilla"><option value="8">Solo el acta</option><option value="9">Solo el apostillamiento</option><option value="10">Ambos</option></select>');
	   }
}


function verifica_estado_tramite(){
	if($("#id_estado_solicita").val()=="33") {
	    $("#id_envios").val(2);
	}else{
		$("#idContentEnvio").hide();
		$("#id_envios").val(1);
	}
}

function muestra_municipio(req){
	var resoponseDatos = eval(req.xhRequest.responseText);
	combo = URLDecode(resoponseDatos[0]);
    $('#idContentMunicipio').html(""); 
    $('#idContentMunicipio').html(combo); 
}

function carga_municipio_tramite(){
	  sURL= "../carga_municipio.php?opcion=carga_municipio&id_estado="+$("#id_estado_solicita").val();
	  //---->oculto el campo de otro en caso de haberlo descubierto
	  $("#idOtroMunicipio").hide();
	  Spry.Utils.loadURL("GET", sURL, true, muestra_municipio, { postData: ""});
}


function valida_tramite(){
	if($("#id_nombre").val()=="") {
	   alert("Ingresa el nombre");
	   $("#id_nombre").focus();
	   return false;
	}
	if($("#id_apellido").val()=="") {
	   alert("Ingresa el apellido");
	   $("#id_apellido").focus();
	   return false;
	}
	if($("#id_mail").val()!="") {
	   if(!validarEmail($("#id_mail").val())){
	      alert("El email es incorrecto");
	      $("#id_mail").focus();
	      return false;
	   }
	}
	if($("#id_telefono").val()=="") {
	   alert("Ingresa el teléfono");
	   $("#id_telefono").focus();
	   return false;
	}
	if($("#id_pais").val()=="") {
	   alert("Selecciona el país");
	   $("#id_pais").focus();
	   return false;
	}
	if($("#id_estado").val()=="") {
	   alert("Selecciona el estado");
	   $("#id_estado").focus();
	   return false;
	}
	if($("#id_municipio").val()=="") {
	   alert("Selecciona el municipio");
	   $("#id_municipio").focus();
	   return false;
	}else if($("#id_municipio").val()=="Otro") {
		if($("#id_otro").val()=="") {
		    alert("Ingresa el municipio");
	        $("#id_otro").focus();
	        return false;
		}
	}
	
	if($("#id_calle").val()=="") {
	   alert("Ingresa la calle");
	   $("#id_calle").focus();
	   return false;
	}
	if($("#id_colonia").val()=="") {
	   alert("Ingresa la colonia");
	   $("#id_colonia").focus();
	   return false;
	}
	if($("#id_cp").val()=="") {
	   alert("Ingresa el CP");
	   $("#id_cp").focus();
	   return false;
	}else if(!/^\d+(d)?$/.test($("#id_cp").val())){
		alert("el CP debe de ser numerico");
	    $("#id_cp").focus();
	    return false;
	}
	return true;	
}


function muestra_estado(req){
	var resoponseDatos = eval(req.xhRequest.responseText);
	combo = URLDecode(resoponseDatos[0]);
	combo_compra = URLDecode(resoponseDatos[1]);
	document.getElementById('idContentEstado').innerHTML =""; 
    document.getElementById('idContentEstado').innerHTML = combo; 
	
	$('#idPaisCompra').html(""); 
	$('#idPaisCompra').html(combo_compra); 
}

function carga_estado_combo(){
	 sURL= "../carga_municipio.php?opcion=carga_estado&id_pais="+$("#id_pais").val();
	 Spry.Utils.loadURL("GET", sURL, true, muestra_estado, { postData: ""});
}


function carga_municipio(){
	  sURL= "../carga_municipio.php?opcion=carga_municipio&id_estado="+$("#id_estado").val();
	  //---->oculto el campo de otro en caso de haberlo descubierto
	  $("#idOtroMunicipio").hide();
	  Spry.Utils.loadURL("GET", sURL, true, muestra_municipio, { postData: ""});
}

function verifica_otro(){
	if($("#id_municipio").val()=="Otro") {
	   document.getElementById("idOtroMunicipio").style.display="";
	}else{
	  document.getElementById("idOtroMunicipio").style.display="none";
	}
}


function muestra_mensaje(req){
	var resoponseDatos = eval(req.xhRequest.responseText);
	mensaje = URLDecode(resoponseDatos[0]);
    $('#idContentMensaje').html(""); 
    $('#idContentMensaje').html(mensaje); 
}

function carga_aviso(){
	 sURL= "../carga_municipio.php?opcion=carga_aviso&id_pais="+$("#idpais_compra").val();
	 Spry.Utils.loadURL("GET", sURL, true, muestra_mensaje, { postData: ""});
}

