// JavaScript Document

function cargarAJAX(){
	var xmlhttp=false;
	try{
		// Crea objeto AJAX para navegadores diferentes a Internet Explorer
		xmlhttp=new ActiveXObject("Msxml12.XMLHTTP");	
	}catch(e){
		try{
			// Crea objeto AJAX para navegadores de Internet Explorer
			xmlhttp=new ActiveXObject("Microsoft:XMLHTTP");	
		}catch(E){
			if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
				xmlhttp=new XMLHttpRequest();	
			}
		}
	}
	return xmlhttp;
}

function simularTrim(parametro){
	var texto=parametro.replace(/^\s+|\s+$/gi, "");
	return texto;
}

function validarCorreo(id){
	var correo = document.getElementById(id);
	if(correo.value!=""){
		var filter=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		if (correo.value.length == 0 ){
			return true;
		}
		if (filter.test(correo.value)){
			return true;
		}	
		return false;
	}
}

function validarFormularioDeCorreo(iddestino){
	if(document.getElementById(iddestino)!=null){
		var destino=document.getElementById(iddestino);	
	}else{
		return false;	
	}
	if(document.getElementById('razon')!=null && document.getElementById('idnombre')!=null){
		if(document.getElementById('idemail')!=null && document.getElementById('ciudad')!=null){
			if(document.getElementById('comentario')!=null){
				var a1=document.getElementById('razon');
				var a2=document.getElementById('idnombre');
				var a3=document.getElementById('idemail');
				var a4=document.getElementById('ciudad');
				var a5=document.getElementById('comentario');
				if(a1.value==0){	
					destino.innerHTML="Seleccione una raz&oacute;n de contacto";	
					a1.focus(); 	return false;
				}
				if(simularTrim(a2.value)==""){	
					destino.innerHTML="Complete su nombre";	
					a2.focus();  	return false;
				}
				if(!validarCorreo('idemail')){
					destino.innerHTML="Complete correctamente su correo";	
					a3.value="";
					a3.focus();  	return false;
				}
				if(a4.value==0){				
					destino.innerHTML="Seleccione su ciudad";	
					a4.focus();  	return false;
				}
				if(simularTrim(a5.value)==""){				
					destino.innerHTML="Complete su comentario";	
					a5.focus();  	return false;
				}
				a1=a1.options[a1.selectedIndex].text;
				a4=a4.options[a4.selectedIndex].text;
				var parametros="a1="+a1+"&a2="+a2.value+"&a3="+a3.value+"&a4="+a4+"&a5="+a5.value;
				var ajax=cargarAJAX();
				ajax.open("POST","wp-content/plugins/contactenos/enviarcorreo.php",true);
				ajax.onreadystatechange=function(){
					if(ajax.readyState==4){
						destino.innerHTML="Correo Enviado";
						document.getElementById('razon').value=0;
						a2.value="";
						a3.value="";
						document.getElementById('ciudad').value=0;
						a5.value="";
					}else{
						destino.innerHTML="Enviando Correo...";	
					}
				}
				ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				ajax.send(parametros);
				return true;
			}else{
				return false;	
			}	
		}else{
			return false;	
		}
	}else{
		return false;	
	}

}

				
