var xmlhttp;

function fieldCheck (personNameStr, personActivityStr, addressStr, phoneStr, emailStr, textmsgStr) {

	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		document.getElementById("errorMessage").innerHTML = "<p>Tu direcci&oacute;n de correo no aparece o es incorrecta (comprueba @ y .'s)</p>";
		return false;
	}

	var user=matchArray[1];
	var domain=matchArray[2];
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			document.getElementById("errorMessage").innerHTML = "<p>Tu direcci&oacute;n de correo contiene caracteres no validos. </p>";
			return false;
		}
	}

	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			document.getElementById("errorMessage").innerHTML = "<p>El dominio de la direcci&oacute;n introducida contiene caracteres no validos.</p>";
			return false;
		}
	}

	if (user.match(userPat)==null) {
		document.getElementById("errorMessage").innerHTML = "<p>La direcci&oacute;n de correo parece incorrecta, por favor compruebalo.</p>";
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				document.getElementById("errorMessage").innerHTML = "<p>La direcci&oacute;n IPInternet Protocol de destino no es correcta!</p>";
				return false;
			}
	}
	
	return true;
	
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			document.getElementById("errorMessage").innerHTML = "<p>La direcci&oacute;n de correo parece incorrecta, por favor compruebalo, incluyendo el uso incorrecto de signos de puntuaci&oacute;n, comas , o puntos [.] al final de la direcci&oacute;n.</p>";
			return false;
		}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 &&
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		document.getElementById("errorMessage").innerHTML = "<p>Tu direcci&oacute;n de correo debe terminar en un dominio o dos letras " + "país.</p>";
		return false;
	}

	if (len<2) {
		document.getElementById("errorMessage").innerHTML = "<p>Falta el nombre del host en tu direcci&oacute;n de correo - compruebalo. O debes de haber añadido un espacio en blanco al final de la direcci&oacute;n - corrije el error e intentalo de nuevo.</p>";
		return false;
	}
	
	errStr = '';

	if (personNameStr.length == 0) {
		errStr += "<p>El campo Nombre y Apellido es obligatorio.  </p>"; 
	}
	
	if (personActivityStr.length == 0) {
		errStr += "<p>El campo Actividad que Desempeña es obligatorio. </p>"; 
	}
	
	if (addressStr.length == 0) {
		errStr += "<p>El campo Lugar de Referencia es obligatorio. </p>"; 
	}
	
	if (phoneStr.length == 0) {
		errStr += "<p>El campo Teléfono es obligatorio. </p>"; 
	}
	
	if (textmsgStr.length == 0) {
		errStr += "<p>El campo Interés es obligatorio. </p>"; 
	}
	
	if (errStr.length != 0) {
		document.getElementById("errorMessage").innerHTML = errStr;
		return false;
	}

	return true;
}

function printEmailResult(strMessage) {
	document.getElementById('errorMessage').innerHTML = strMessage;
}

function enviarFormulario() {
	try {
	if (!fieldCheck(document.forms.item(0).personName.value, document.forms.item(0).personActivity.value, document.forms.item(0).address.value, document.forms.item(0).phone.value, document.forms.item(0).email.value, document.forms.item(0).textmsg.value)) {
		return;
	}

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }

	strValues = '';

	for (i=0; i<document.getElementsByTagName("input").length; i++) {
		strValues += "&"+document.getElementsByTagName("input").item(i).name + "=" + document.getElementsByTagName("input").item(i).value;
	}
	
	for (i=0; i<document.getElementsByTagName("textarea").length; i++) {
		strValues += "&"+document.getElementsByTagName("textarea").item(i).name + "=" + document.getElementsByTagName("input").item(i).value;
	}
	
	
	var url;  
	var values = "";
	url="Scripts/send.php?id=1";
	url+=strValues;
	xmlhttp.onreadystatechange=stateChangedEmail;
	xmlhttp.open("POST",url,true);

	xmlhttp.send();
	
	} catch(e) {alert(e);}


}

function stateChangedEmail()
{
	if (xmlhttp.readyState==4)	{	
		document.getElementById("errorMessage").innerHTML = xmlhttp.responseText;
	}
}

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

