function es_email(email) 
{
  /* Validar mail: comprobación de @, un punto después de la @ y dos letras mínimo después del último punto */
	if (email != '')
	{
		var posArroba = email.indexOf('@',0);
		if ((posArroba == -1) || (email.lastIndexOf('@') != posArroba) ||
			(email.charAt(0)== '@') || (email.charAt(email.length-1) == '@'))
		{
			return false;
		}
		var dominio = email.substr(posArroba + 1);
		var posPunto = dominio.indexOf('.',0);
		if ((posPunto == -1) || 	(dominio.charAt(0) == '.') || (dominio.charAt(dominio.length-1) == '.'))
		{
			return false;
		}
		var subdominio = dominio.substr(posPunto + 1);
		if (subdominio.length < 2)
		{
			return false;
		}
		var valido ="'ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz.-";
		var ok = true;
		var letras = 0
		for (i = 0; i < subdominio.length; i++)
		{
			ch = subdominio.charAt(i);
			for (j = 0; j < valido.length; j++)
			{
				if (ch == valido.charAt(j))
				{
					if ((ch != '.') || (ch != '-'))
					{
						letras = letras + 1
					}
					break;
				}
			}
			if (j == valido.length)
			{
				ok = false;
				break;
			}
		}
		if ((!ok) || (letras < 2))
		{
			return false;
		}
		return true;
	}
}





			/*function hexdigit(v) {
			   symbs = "0123456789ABCDEF";
			   return symbs.charAt(v & 0x0f);
			}

			function hexval(v) {
			 //  return hexdigit(v >>> 12) + hexdigit(v >>> 8) + hexdigit(v >>> 4) + hexdigit(v);
			   return hexdigit(v >>> 4) + hexdigit(v);
			}*/

			function myesc(val) {
			   if (val == 10) return "\\n"
			   else if (val == 13) return "\\r"
			   else if (val == 92) return "\\\\"
			   else if (val == 34) return "\\\""
			  // else if (val < 32 || val > 126) return "%"+hexval(val);
			   else return String.fromCharCode(val);
			}



			function myescape(uni) {
			   lit = '';
			   for (i = 0; i < uni.length; i++) {
			      v = uni.charCodeAt(i);
			      lit = lit + myesc(v);

			   }

			   return lit;
			}
			
			
			function fixemup(registro) {
				document.enqs.address1.value=myescape(document.enqs.address1d.value);
				document.enqs.firstname.value=myescape(document.enqs.firstnamed.value);
				document.enqs.lastname2.value=myescape(document.enqs.lastname2d.value);
				document.enqs.city.value=myescape(document.enqs.cityd.value);
				document.enqs.company.value=myescape(document.enqs.companyd.value);
				document.enqs.state.value=myescape(document.enqs.stated.value);
				document.enqs.postcode.value=myescape(document.enqs.postcoded.value);
				document.enqs.job_title.value=myescape(document.enqs.job_titled.value);
				document.enqs.country.value=myescape(document.enqs.countryd.value);
				document.enqs.otherorigin.value=myescape(document.enqs.otherorigind.value);				
				
				if(registro){
					document.enqs.authorisation.value=myescape(document.enqs.authorisationd.value);	
				
					if(document.enqs.invaddsupplied.value=="true"){
						document.enqs.nameinv1.value=myescape(document.enqs.nameinv1d.value);
						document.enqs.company2.value=myescape(document.enqs.company2d.value);
						document.enqs.addressinv1.value=myescape(document.enqs.addressinv1d.value);
						document.enqs.city2.value=myescape(document.enqs.city2d.value);
						document.enqs.state2.value=myescape(document.enqs.state2d.value);
						document.enqs.postcodeinv.value=myescape(document.enqs.postcodeinvd.value);
						document.enqs.countryinv.value=myescape(document.enqs.countryinvd.value);					
					}
				}
			}
			
			
			function Validator(thisForm,registro) {								
				
				
				if(registro){
					//Codigo del curso
					if(thisForm.coursecode.value=="") {
						alert("Por favor seleccione um curso.");
						thisForm.coursecode.focus();
						return (false);
					}
				
					//Fecha de inicio
					if(thisForm.sdate.value=="") {
						alert("Por favor preencha o campo data de início.");
						thisForm.sdate.focus();
						return (false);
					}
				}
				
				//Genero
				if ((thisForm.sr[0].checked==false)&& (thisForm.sr[1].checked==false)){
					alert("Deve seleccionar o género.");
					return (false);
				}
								
				//Nombre
				if(thisForm.firstname.value=="") {
					alert("Por favor preencha o campo Nome.");
					thisForm.firstnamed.focus();
					return (false);
				}
				if(thisForm.firstname.value.slice(0,1)==" ") {
					alert("O campo Nome não deve começar com espaços em branco.");
					thisForm.firstnamed.focus();
					return (false);
				}
				if(thisForm.firstname.value.length >50) {
					alert("O campo Nome não deve conter mais de 50 caracteres.");
					thisForm.firstnamed.focus();
					return (false);
				}
				if(thisForm.firstname.value.length ==1) {
					alert("O campo Nome deve conter, pelo menos, dois caracteres.");
					thisForm.firstnamed.focus();
					return (false);
				}				
								
				
				//Apellidos
				if(thisForm.lastname2.value=="") {
					alert("Por favor, preencha o campo Sobrenomes.");
					thisForm.lastname2d.focus();
					return (false);
				}
				if(thisForm.lastname2.value.slice(0,1)==" ") {
					alert("O campo Sobrenomes não deve começar com espaços em branco.");
					thisForm.lastname2d.focus();
					return (false);
				}
				if(thisForm.lastname2.value.length >50) {
					alert("O campo Sobrenomes não deve conter mais de 50 caracteres.");
					thisForm.lastname2d.focus();
					return (false);
				}
				if(thisForm.lastname2.value.length ==1) {
					alert("O campo Sobrenomes deve conter, pelo menos, dois caracteres.");
					thisForm.lastname2d.focus();
					return (false);
				}
												
				
				//Empresa
				if(thisForm.company.value=="") {
					alert("Por favor preencha o campo Empresa.");
					thisForm.companyd.focus();
					return (false);
				}
				if(thisForm.company.value.slice(0,1)==" ") {
					alert("O campo Empresa não deve começar com espaços em branco.");
					thisForm.companyd.focus();
					return (false);
				}
				if(thisForm.company.value.length >255) {
					alert("O campo Empresa não deve conter mais de 255 caracteres.");
					thisForm.companyd.focus();
					return (false);
				}
				if(thisForm.company.value.length ==1) {
					alert("O campo Empresa deve conter, pelo menos, dois caracteres.");
					thisForm.companyd.focus();
					return (false);
				}
				
				
				//CIF
				if(thisForm.cif.value.slice(0,1)==" ") {
					alert("O campo CIF não deve começar com espaços em branco.");
					thisForm.cif.focus();
					return (false);
				}
				if(thisForm.cif.value.length >255) {
					alert("O campo CIF não deve conter mais de 255 caracteres.");
					thisForm.cif.focus();
					return (false);
				}
				if(thisForm.cif.value.length ==1) {
					alert("O campo CIF deve conter, pelo menos, dois caracteres.");
					thisForm.cif.focus();
					return (false);
				}
				
								
				//Cargo
				if(thisForm.job_title.value=="") {
					alert("Por favor preencha o campo Cargo.");
					thisForm.job_titled.focus();
					return (false);
				}
				if(thisForm.job_title.value.slice(0,1)==" ") {
					alert("O campo Cargo não deve começar com espaços em branco.");
					thisForm.job_titled.focus();
					return (false);
				}
				if(thisForm.job_title.value.length >255) {
					alert("O campo Cargo não deve conter mais de 255 caracteres.");
					thisForm.job_titled.focus();
					return (false);
				}
				if(thisForm.job_title.value.length ==1) {
					alert("O campo Cargo deve conter, pelo menos, dois caracteres.");
					thisForm.job_titled.focus();
					return (false);
				}
				
							
				//Email
				if(thisForm.email.value=="") {
					alert("Por favor preencha o campo Email.");
					thisForm.email.focus();
					return (false);
				}
				if(thisForm.email.value.slice(0,1)==" ") {
					alert("O campo Email não deve começar com espaços em branco.");
					thisForm.email.focus();
					return (false);
				}
				if (es_email(thisForm.email.value)==false){
					alert("Email incorrecto")
					thisForm.email.focus();
					return (false);
				}
				if(thisForm.email.value.length >255) {
					alert("O campo Email não deve conter mais de 255 caracteres.");
					thisForm.email.focus();
					return (false);
				}
				
				
				//Direccion
				if(thisForm.address1.value=="") {
					alert("Por favor preencha o campo Morada.");
					thisForm.address1d.focus();
					return (false);
				}
				if(thisForm.address1.value.slice(0,1)==" ") {
					alert("O campo Morada não deve começar com espaços em branco.");
					thisForm.address1d.focus();
					return (false);
				}
				if(thisForm.address1.value.length >255) {
					alert("O campo Email não deve conter mais de 255 caracteres.");
					thisForm.address1d.focus();
					return (false);
				}
				if(thisForm.address1.value.length ==1) {
					alert("O campo Morada deve conter, pelo menos, dois caracteres.");
					thisForm.address1d.focus();
					return (false);
				}
				
				
				//Numero
				if(thisForm.numero.value.slice(0,1)==" ") {
					alert("O campo Número não deve começar com espaços em branco.");
					thisForm.numero.focus();
					return (false);
				}
				if(thisForm.numero.value.length >3) {
					alert("O campo Número não deve conter mais de 3 caracteres.");
					thisForm.numero.focus();
					return (false);
				}
				if((thisForm.numero.value!="")) {
						if(isNaN(thisForm.numero.value)) {
							alert("O campo Número somente deve conter caracteres numéricos.");
							thisForm.numero.focus();
							return (false);
						}
				}

				
				//Piso
				if(thisForm.piso.value.slice(0,1)==" ") {
					alert("O campo Andar não deve começar com espaços em branco.");
					thisForm.piso.focus();
					return (false);
				}
				if(thisForm.piso.value.length >255) {
					alert("O campo Andar não deve conter mais de 255 caracteres.");
					thisForm.piso.focus();
					return (false);
				}
				
				
				//Ciudad/Población
				if(thisForm.city.value=="") {
					alert("Por favor preencha o campo Cidade.");
					thisForm.cityd.focus();
					return (false);
				}
				if(thisForm.city.value.slice(0,1)==" ") {
					alert("O campo Cidade não deve começar com espaços em branco.");
					thisForm.cityd.focus();
					return (false);
				}
				if(thisForm.city.value.length >255) {
					alert("O campo Cidade não deve conter mais de 255 caracteres.");
					thisForm.cityd.focus();
					return (false);
				}
				if(thisForm.city.value.length ==1) {
					alert("O campo Cidade deve conter, pelo menos, dois caracteres.");
					thisForm.cityd.focus();
					return (false);
				}
				
				
				//Estado/Provincia
				if(thisForm.state.value=="") {
					alert("Por favor preencha o campo Estado.");
					thisForm.stated.focus();
					return (false);
				}
				if(thisForm.state.value.slice(0,1)==" ") {
					alert("O campo Estado não deve começar com espaços em branco.");
					thisForm.stated.focus();
					return (false);
				}
				if(thisForm.state.value.length >255) {
					alert("O campo Estado não deve conter mais de 255 caracteres.");
					thisForm.stated.focus();
					return (false);
				}
				if(thisForm.state.value.length ==1) {
					alert("O campo Estado deve conter, pelo menos, dois caracteres.");
					thisForm.stated.focus();
					return (false);
				}
				
				//Codigo Postal
				if(thisForm.postcode.value=="") {
					alert("Por favor preencha o campo Código Postal.");
					thisForm.postcoded.focus();
					return (false);
				}
				if(thisForm.postcode.value.slice(0,1)==" ") {
					alert("O campo Código Postal não deve começar com espaços em branco.");
					thisForm.postcoded.focus();
					return (false);
				}
				if(thisForm.postcode.value.length ==1) {
					alert("O campo Código Postal deve conter, pelo menos, dois caracteres.");
					thisForm.postcoded.focus();
					return (false);
				}
				
				
				//Pais
				if(thisForm.country.value=="") {
					alert("Por favor preencha o campo País.");
					thisForm.countryd.focus();
					return (false);
				}
				if(thisForm.country.value.slice(0,1)==" ") {
					alert("O campo País não deve começar com espaços em branco.");
					thisForm.countryd.focus();
					return (false);
				}
				if(thisForm.country.value.length >255) {
					alert("O campo País não deve conter mais de 255 caracteres.");
					thisForm.countryd.focus();
					return (false);
				}
				if(thisForm.country.value.length ==1) {
					alert("O campo País deve conter, pelo menos, dois caracteres.");
					thisForm.countryd.focus();
					return (false);
				}
				
				
				
				
				
				//Telefono
				if(thisForm.tel_full.value=="") {
					alert("Por favor preencha o campo Telefone.");
					thisForm.tel_full.focus();
					return (false);
				}
				if((thisForm.tel_full.value!="")) {
					var tempfax=thisForm.tel_full.value;
					tempfax=tempfax.replace(/ /g,"");
					thisForm.tel_full.value=tempfax;
					var tempfax=thisForm.tel_full.value;
					tempfax=tempfax.replace(/\+/g,"");
					thisForm.tel_full.value=tempfax;
						
					if((thisForm.tel_full.value!="")) {
						if(isNaN(thisForm.tel_full.value)) {
							alert("O campo Telefone somente deve conter caracteres numéricos.");
							thisForm.tel_full.focus();
							return (false);
						}
					}
				}
				if(thisForm.tel_full.value.length ==1) {
					alert("O campo Telefone deve conter, pelo menos, dois caracteres.");
					thisForm.tel_full.focus();
					return (false);
				}
				
				
				
				
				
				if(registro){
					if(thisForm.invaddsupplied.value=="true"){
						
						//Destinatario
						if(thisForm.nameinv1.value.slice(0,1)==" ") {
							alert("O campo do destinatário não deve começar com espaços em branco.");
							thisForm.nameinv1d.focus();
							return (false);
						}
						if(thisForm.nameinv1.value.length >255) {
							alert("O campo destinatário não deve conter mais de 255 caracteres.");
							thisForm.nameinv1d.focus();
							return (false);
						}
						if(thisForm.nameinv1.value.length ==1) {
							alert("O campo destinatário deve conter, pelo menos, dois caracteres.");
							thisForm.nameinv1d.focus();
							return (false);
						}
						
						
						//Empresa
						if(thisForm.company2.value.slice(0,1)==" ") {
							alert("O campo Empresa não deve começar com espaços em branco.");
							thisForm.company2d.focus();
							return (false);
						}
						if(thisForm.company2.value.length >255) {
							alert("O campo Empresa não deve conter mais de 255 caracteres.");
							thisForm.company2d.focus();
							return (false);
						}
						if(thisForm.company2.value.length ==1) {
							alert("O campo Empresa deve conter, pelo menos, dois caracteres.");
							thisForm.company2d.focus();
							return (false);
						}
						
						
						//CIF
						if(thisForm.cif2.value.slice(0,1)==" ") {
							alert("O campo CIF não deve começar com espaços em branco.");
							thisForm.cif2.focus();
							return (false);
						}
						if(thisForm.cif2.value.length >255) {
							alert("O campo CIF não deve conter mais de 255 caracteres.");
							thisForm.cif2.focus();
							return (false);
						}
						if(thisForm.cif2.value.length ==1) {
							alert("O campo CIF deve conter, pelo menos, dois caracteres.");
							thisForm.cif2.focus();
							return (false);
						}
					
					
						//Direccion
						if(thisForm.addressinv1.value=="") {
							alert("Por favor preencha o campo Morada.");
							thisForm.addressinv1d.focus();
							return (false);
						}
						if(thisForm.addressinv1.value.slice(0,1)==" ") {
							alert("O campo Morada não deve começar com espaços em branco.");
							thisForm.addressinv1d.focus();
							return (false);
						}	
						if(thisForm.addressinv1.value.length >255) {
							alert("O campo Morada não deve conter mais de 255 caracteres.");
							thisForm.addressinv1d.focus();
							return (false);
						}
						if(thisForm.addressinv1.value.length ==1) {
							alert("O campo Morada deve conter, pelo menos, dois caracteres.");
							thisForm.addressinv1d.focus();
							return (false);
						}
						
						
						//Numero
						if(thisForm.numero2.value.slice(0,1)==" ") {
							alert("O campo Número não deve começar com espaços em branco.");
							thisForm.numero2.focus();
							return (false);
						}
						if(thisForm.numero2.value.length >3) {
							alert("O campo Número não deve conter mais de 3 caracteres.");
							thisForm.numero2.focus();
							return (false);
						}
						if((thisForm.numero2.value!="")) {
								if(isNaN(thisForm.numero2.value)) {
									alert("O campo Número somente deve conter caracteres numéricos.");
									thisForm.numero2.focus();
									return (false);
								}
						}
		
						
						//Piso
						if(thisForm.piso2.value.slice(0,1)==" ") {
							alert("O campo Andar não deve começar com espaços em branco.");
							thisForm.piso2.focus();
							return (false);
						}
						if(thisForm.piso2.value.length >255) {
							alert("O campo Andar não deve conter mais de 255 caracteres.");
							thisForm.piso2.focus();
							return (false);
						}
						
						
						//Ciudad/Población
						if(thisForm.city2.value=="") {
							alert("Por favor preencha o campo Cidade.");
							thisForm.city2d.focus();
							return (false);
						}
						if(thisForm.city2.value.slice(0,1)==" ") {
							alert("O campo Cidade não deve começar com espaços em branco.");
							thisForm.city2d.focus();
							return (false);
						}
						if(thisForm.city2.value.length >255) {
							alert("O campo Cidade não deve conter mais de 255 caracteres.");
							thisForm.city2d.focus();
							return (false);
						}
						if(thisForm.city2.value.length ==1) {
							alert("O campo Cidade deve conter, pelo menos, dois caracteres.");
							thisForm.city2d.focus();
							return (false);
						}
						
						
						//Estado/Provincia
						if(thisForm.state2.value=="") {
							alert("Por favor preencha o campo Estado.");
							thisForm.state2d.focus();
							return (false);
						}
						if(thisForm.state2.value.slice(0,1)==" ") {
							alert("O campo Estado não deve começar com espaços em branco.");
							thisForm.state2d.focus();
							return (false);
						}
						if(thisForm.state2.value.length >255) {
							alert("O campo Estado não deve conter mais de 255 caracteres.");
							thisForm.state2d.focus();
							return (false);
						}
						if(thisForm.state2.value.length ==1) {
							alert("O campo Estado deve conter, pelo menos, dois caracteres.");
							thisForm.state2d.focus();
							return (false);
						}
						
						
						//Código Postal
						if(thisForm.postcodeinv.value=="") {
							alert("Por favor preencha o campo Código Postal.");
							thisForm.postcodeinvd.focus();
						return (false);
						}
						if(thisForm.postcodeinv.value.slice(0,1)==" ") {
							alert("O campo Código Postal não deve começar com espaços em branco.");
							thisForm.postcodeinvd.focus();
							return (false);
						}
						if(thisForm.postcodeinv.value.length ==1) {
							alert("O campo Código Postal deve conter, pelo menos, dois caracteres.");
							thisForm.postcodeinvd.focus();
							return (false);
						}
						
																						
						//País
						if(thisForm.countryinv.value=="") {
							alert("Por favor preencha o campo País.");
							thisForm.countryinvd.focus();
							return (false);
						}
						if(thisForm.countryinv.value.slice(0,1)==" ") {
							alert("O campo País não deve começar com espaços em branco.");
							thisForm.countryinvd.focus();
							return (false);
						}
						if(thisForm.countryinv.value.length >50) {
							alert("O campo País não deve conter mais de 50 caracteres.");
							thisForm.countryinvd.focus();
							return (false);
						}
						if(thisForm.countryinv.value.length ==1) {
							alert("O campo País deve conter, pelo menos, dois caracteres.");
							thisForm.countryinvd.focus();
							return (false);
						}					
											
					}
				
				
				//Autorización
					if(thisForm.authorisation.value=="") {
						alert("Por favor preencha o campo Autorização.");
						thisForm.authorisationd.focus();
						return (false);
					}
					if(thisForm.authorisation.value.slice(0,1)==" ") {
						alert("O campo Autorização não deve começar com espaços em branco.");
						thisForm.authorisationd.focus();
						return (false);
					}
					if(thisForm.authorisation.value.length >255) {
						alert("O campo Autorização não deve conter mais de 255 caracteres.");
						thisForm.authorisationd.focus();
						return (false);
					}
					if(thisForm.authorisation.value.length ==1) {
						alert("O campo Autorização deve conter, pelo menos, dois caracteres.");
						thisForm.authorisationd.focus();
						return (false);
					}
				}
				
				//Origen
				if(thisForm.origin_id.value=="") {
					alert("Por favor indique como conheceu a ESI.");
					thisForm.origin_id.focus();
					return (false);
				}
				if((thisForm.origin_id.value=="Por otros medios") & (thisForm.otherorigin.value=="")) {
					alert("Por favor indique qual outra fonte serviu para que conhecesse a ESI.");
					thisForm.otherorigind.focus();
					return (false);
				}

				
			//return true;
			}
			
			
			
			//Funciones para borrar las cookies
			
			function Caduca(dias) {
				var hoy = new Date()					//coge la fecha actual
				var msEnXDias = eval(dias) * 24 * 60 * 60 * 1000	//pasa los dias a mseg.

				hoy.setTime(hoy.getTime() + msEnXDias)			//fecha de caducidad: actual + caducidad
				return (hoy.toGMTString())
			}
			function BorrarCookie(nombre) {
	//para borrar la cookie, se le pone una fecha del pasado mediante Caduca(-1)
	document.cookie = nombre + "=; expires=" + Caduca(-1) + "; path=/"
			}
			
			function limpiar(){
				BorrarCookie("usuario");
				BorrarCookie("conocer");
				BorrarCookie("opciones");
				BorrarCookie("newsletter");

			}			
			
			
			
			
			
			
			
			
			
			

