var READY_STATE_UNINITISLIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;
var peticion_http;
var httpUrl='ejecuta.php';

function cargaContenido(url,metodo,funcion,datoPOST){
	peticion_http=inicializa_xhr();
if(peticion_http){
	peticion_http.onreadystatechange=funcion;
	peticion_http.open(metodo,url,true);
        peticion_http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	peticion_http.send(datoPOST);
 }
}

function inicializa_xhr() {
	if(window.XMLHttpRequest){
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function respuestaHttp(){
if(peticion_http.readyState==READY_STATE_COMPLETE){
		if(peticion_http.status==200){
		datos=getDatos(peticion_http.responseText);
		if(datos[0]=="ok"){
                 alert("Su mensaje fue enviado correctamente")
                 location.href="index.php"; 
                }else alert("Error en el envio");
		}
	}
}

function valida(){
    if(trim(el('nombre').value)!="" && trim(el('mensaje').value)!="" && correo(el('mail').value)){
    cargaContenido(httpUrl,"POST",respuestaHttp,"Nombre="+el('nombre').value+"&Email="+el('mail').value+"&Mensaje="+el('mensaje').value);
    }else
        alert("Debe completar el formulario.\nTodos los campos son obligatorios.");
}

function el(objeto) {
	return (document.getElementById(objeto));
}

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

}

function trim(dato) {
try{
	return dato.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
catch(eer){
alert(eer);
}
}