var IE = (document.all && !window.opera) ? true:false;
var cubretodo;
var semi; 
var allowornot; 
var cerrar;
var allowme;
var browser = navigator.appName;
var http = createRequestObject();

function createRequestObject() {
	var ro;    
	if(IE)
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	else
		ro = new XMLHttpRequest();
	return ro;
}

function CrearElementosParaMensajeError() {
	cubretodo = document.createElement('div'); //un div que cubre todo
	cubretodo.setAttribute('id', 'cubretodo');
	cubretodo.style.position = "fixed";
	cubretodo.style.top = "0px";
	cubretodo.style.left = "0px";
	cubretodo.style.height = "100%";
	cubretodo.style.width = "100%";
	cubretodo.style.zIndex = 200;
	cubretodo.style.display = "none";
	document.body.appendChild(cubretodo);

	semi = document.createElement('div'); //un div que es semi transparente
	semi.setAttribute('id', "semi");
	semi.style.position = "absolute";
	semi.style.top = "0px";
	semi.style.left = "0px";
	semi.style.height = "100%";
	semi.style.width = "100%";
	semi.style.background = "#000";
	if(IE) { 
		semi.style.filter = "alpha(opacity=70);"; 
	} else {
		semi.style.opacity = 0.7;
	}
	cubretodo.appendChild(semi);

	allowornot = document.createElement('div'); //el div cuadro de dialogo
	allowornot.setAttribute('id', 'allowornot');
	allowornot.style.position = "absolute";
	allowornot.style.top = "50%";
	allowornot.style.left = "50%";
	allowornot.style.height = "200px";
	allowornot.style.width = "350px";
	allowornot.style.marginLeft = "-175px";
	allowornot.style.marginTop = "-100px";
	allowornot.style.background = "#fff";
	allowornot.style.color = "#000";
	allowornot.style.fontFamily = "arial, sans";
	allowornot.style.fontSize = "small";
	cubretodo.appendChild(allowornot);

	cerrar = document.createElement('input'); //el boton cerrar
	cerrar.setAttribute('id', 'cerrar');
	cerrar.setAttribute('name', 'cerrar');
	cerrar.setAttribute('type', 'submit');	
	cerrar.setAttribute('value', 'cerrar');	
	cerrar.style.position = "absolute";
	cerrar.style.bottom = "20px";
	cerrar.style.left = "50%";
	cerrar.style.width = "100px";
	cerrar.style.marginLeft = "-50px";
	allowornot.appendChild(cerrar);
	cerrar.onclick = function(){hide_errormsg();};

	allowme = document.createElement('p'); //el texto de error
	allowme.setAttribute('id', 'allowme');
	allowme.style.position = "absolute";
	allowme.style.top = "50%";
	allowme.style.left = "0px";
	allowme.style.width = "100%";
	allowme.style.height = "15px";
	allowme.style.textAlign= "center";
	allowme.style.marginTop = "-7px";
	allowme.style.color = "black";
	allowornot.appendChild(allowme);
	return;
}

CrearElementosParaMensajeError();

function ValidateForm() {    
	var formd = document.getElementById('form');	
	var cuadro = document.getElementById('cuadroMensaje');

	if(formd.mensaje.value == "") { 
		document.getElementById('cubretodo').style.display = "block";
		allowme.innerHTML = 'Es necesario escribir un mensaje';
		aparece(allowornot);
		cerrar.focus(); 
		return false; 
	} 


	var confirma_email = (formd.lastName.value) ? formd.lastName.value : formd.email2.value;
	if (formd.email.value != confirma_email ) { 
		document.getElementById('cubretodo').style.display = "block";
		allowme.innerHTML = 'El email y la confirmacion no coinciden';
		aparece(allowornot);
		cerrar.focus(); 
		return false; 
	} 
	if (!formd.email.value.match('[a-zA-Z][0-9a-zA-Z._-]*@[a-zA-Z][0-9a-zA-Z_-]*\..+[^.]$')) {
		document.getElementById('cubretodo').style.display = "block";
		allowme.innerHTML = 'El email no es valido';
		aparece(allowornot);
		//cerrar.value ="cerrar";
		cerrar.focus(); 
		return false; 
	} 
	var confirma_email = (formd.lastName.value) ? "&lastName="+formd.lastName.value : "&email2="+formd.email2.value;
	http.open('GET', 'contactar.php?email='+formd.email.value+ confirma_email +'&nombre='+formd.nombre.value+'&empresa='+formd.empresa.value+'&mensaje='+formd.mensaje.value);
	http.onreadystatechange = handleResponse;
	document.body.style.cursor = "wait";
	desaparece(cuadro);
	http.send(null);	
	return false;

}

function handleResponse() {
	if(http.readyState == 4){
		var response = http.responseText;

		var cuadro = document.getElementById('cuadroMensaje');
		var itsok = document.getElementById('itsok');	
		document.body.style.cursor = "default";	
		var recibidoText = document.createElement('p');
		recibidoText.innerHTML = "Mensaje enviado con exito";
		itsok.appendChild(recibidoText);
		aparece(itsok);
	}
}



function hide_errormsg(){	
	document.getElementById('cubretodo').style.display='none';
	document.getElementById('allowornot').style.display='none';
}
