function emailCheck (emailStr) {

	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */

	var checkTLD=1;

	/* The following is the list of known TLDs that an e-mail address must end with. */

	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */

	var emailPat=/^(.+)@(.+)$/;

	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */

	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/

	var validChars="\[^\\s" + specialChars + "\]";

	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */

	var quotedUser="(\"[^\"]*\")";

	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */

	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

	/* The following string represents an atom (basically a series of non-special characters.) */

	var atom=validChars + '+';

	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */

	var word="(" + atom + "|" + quotedUser + ")";

	// The following pattern describes the structure of the user

	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */

	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	/* Finally, let's start trying to figure out if the supplied address is valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */

	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		alert("Debe ingresar un Email válido.");
		return false;
	}

	return true;
}


function verifydigit(texto) {
  largo = texto.length;
  for (i=0; i < largo ; i++ )  {    
      if ( 	texto.charAt(i) == '<'  || texto.charAt(i) == '>' || 
	  		texto.charAt(i) == '#'  || texto.charAt(i) == '$' ||  
			texto.charAt(i) == '\\' || texto.charAt(i) == '/' ) 
		  		return false;    
  }				     
  				return true;
}

function checkForm() {

	var f = $("contacto");

	if(f.fpNombre.value != "") {
	  if (!verifydigit(f.fpNombre.value)) {
	  	alert("Existen caracteres no validos en el Nombre (<,>,\\,/,$,#). ");
	  	f.fpNombre.focus();
		return false;
	  }
	} else {
		alert("Debe ingresar su Nombre");
		f.fpNombre.focus();
		return false;
	}


	if(f.fpApellidoPat.value != "") {
	  if (!verifydigit(f.fpApellidoPat.value)) {
	     alert("Existen caracteres no validos en el Apellido Paterno (<,>,\\,/,$,#). ");
			return false;
	  }
	} else {
		alert("Debe ingresar su Apellido Paterno");
		f.fpApellidoPat.focus();
		return false;
	}

	if(f.fpApellidoMat.value != "") {
	  if (!verifydigit(f.fpApellidoMat.value)) {
	     alert("Existen caracteres no validos en el Apellido Materno (<,>,\\,/,$,#). ");
			return false;
	  }
	} else {
		alert("Debe ingresar su Apellido Materno");
		f.fpApellidoMat.focus();
		return false;
	}

	if(!emailCheck(f.fpEmail.value)) {
		f.fpEmail.focus();
		return false;
	}

	if(f.fpComentario.value == ""){
		alert("Debe ingresar su Mensaje");
		f.fpComentario.focus();
		return false;
	}


	if( !verifydigit(f.fpNombre.value) ||
		!verifydigit(f.fpApellidoPat.value) ||
		!verifydigit(f.fpApellidoMat.value)) {
			alert("Existen caracteres no validos (<,>,\\,/,$,#). ");
			return false;
	}

	return true;

}

function submitForm(content) {

	var reset = $("resetContainer");
	var submit = $("submitContainer");

	if(checkForm()) {

		var f = $("contacto");
		var destinatario = "";

		for(i = 0; i < f.fpDestinatario.length; i++) {
			if(f.fpDestinatario[i].checked) {
				destinatario = f.fpDestinatario[i].value;
			}
		}

		var url = '/app/application';
		var params = "action=contactForm&nombre="+f.fpNombre.value
						+"&paterno="+f.fpApellidoPat.value
						+"&materno="+f.fpApellidoMat.value
						+"&comentario="+f.fpComentario.value
						+"&email="+f.fpEmail.value
						+"&destinatario="+destinatario
						+"&reset="+reset.innerHTML
						+"&submit="+submit.innerHTML;

		submit.innerHTML = content;
		reset.innerHTML = "&nbsp";

		new Ajax.Request(url,{	
			method:'POST',
			parameters: params,
			encoding: 'UTF-8',
			onSuccess : callBackSuccess,
			onFailure: callBackSuccess
		});

	}

	return false;

}

function callBackSuccess(resp) {

	var f = $("contacto");
	var reset = $("resetContainer");
	var submit = $("submitContainer");
	var textReturn = resp.responseText || "no response text";
	var data = resp.responseText.evalJSON();
	var msg = "";

	switch(parseInt(data.error)) {
		case 0:
			f.reset();
			msg = "<h1>" + data.successMsg + "</h1>";
			break;
		default:
			msg = "<h1>" + data.errorMsg + "</h1>";
			break;
	}

	if ($("response")) {
		$("response").innerHTML = msg;
	}
	reset.innerHTML = data.reset;
	submit.innerHTML = data.submit;

}
