function mailAjax(){
 var xmlhttp=false;
  try{
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }catch(e){
   try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }catch(E){
    xmlhttp = false;
   }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
   xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

function validarForm(){/////////funcion que valida los campos y ademas envia por POST los valores
	
	//donde se mostrará los registros
	var divContenido = document.getElementById('centro'); ////este es la capa div donde se mostrara el resultado del php que procesa los datos que enviamos por POST
	var divAlert = document.getElementById ('div_msg');
	
	var ajax=mailAjax();
	
	form = document.frmEmail ///nombre del formulario

 	//valores de los inputs
  	nombre=form.nombre.value;
	telefono=form.telefono.value;
	email=form.email.value;
	asunto=form.asunto.value;
	mensaje=form.mensaje.value;
	
	
	if(notEmpty(document.frmEmail.nombre, "Ingrese su nombre")){
		if(isNumeric(document.frmEmail.telefono, "Ingrese su numero de teléfono valido")){
			if(emailValidator(document.frmEmail.email, "Por favor ingrese un email valido")){
				if(notEmpty(document.frmEmail.asunto, "Ingrese el asunto")){
					if(notEmpty(document.frmEmail.mensaje, "cual es su mensaje?")){
						
						
						ajax.open("GET", "mail.php"+"?nombre="+nombre+"&telefono="+telefono+"&email="+email+"&asunto="+asunto+"&mensaje="+mensaje, true);
						divContenido.innerHTML= '<img src="./assets/images/ajax-loading.gif">';
						
						ajax.onreadystatechange=function() {
							if (ajax.readyState==4) {
								//mostrar resultados en esta capa
								divContenido.innerHTML = ajax.responseText
							}
						}
						
						 //como hacemos uso del metodo GET
						 //colocamos null ya que enviamos 
						 //el valor por la url ?pag=nropagina
						 ajax.send(null)
						 setTimeout("sendRequest()", 10000);
					 
 
					}
				}
			}
		}
	}
	return false;
	
	
	
	// Check each input in the order that it appears in the form!
	/*if(isAlphabet(document.frmEmail.nombre, "Ingrese solo letras")){
		if(isAlphanumeric(document.frmEmail.telefono, "Ingrese solo numeros o letras")){
			if(emailValidator(document.frmEmail.email, "Por favor ingrese un email valido")){
				if(isAlphanumeric(document.frmEmail.asunto, "Ingrese solo numeros o letras")){
					if(lengthRestriction(document.frmEmail.mensaje, 6, 8)){
						return true;
					}
				}
			}
		}
	}
	return false;*/
	

	
 
}



//____ Validaciones....
	
function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function madeSelection(elem, helperMsg){
	if(elem.value == "Please Choose"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function espaciosBlanco(elem, helperMsg){
	if (elem.indexOf(' ',0) != -1) {
		alert('El campo CONTRASENA no debe contener espacios en blanco'); 
		document.frm_pass.password1.focus ();
	
		return false;
	}
}

function caracteresEspeciales(elem, helperMsg){
	var filter = /^[a-z0-9_\-\.\[\]\(\)]+$/i;
	if (!filter.test(password1)){
		alert('El campo CONTRASENA no debe contener caracteres especiales'); 
		document.frm_pass.password1.focus ();
		return false;
	}
}
