function ft(xUrl,xform,campo1,campo2) {
var xform=xform
var c1=campo1
var c2=campo2
if (document.getElementById(xform).ft.value=="no" && document.getElementById(c2).value!="")  {
					  var xmlhttp = false;
					  try {
						xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
					   } catch (e) {
						  try {
							xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
						  } catch (E) {
							xmlhttp = false;
						  }
					   }
					  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
						xmlhttp = new XMLHttpRequest();
					  }
				xUrl=xUrl + '?n=1'
				   for (i = 0; i < document.getElementById(xform).elements.length; i++){
						xUrl=xUrl + '&' + document.getElementById(xform).elements[i].name + '=' + document.getElementById(xform).elements[i].value
					}
					  xmlhttp.open("GET", xUrl,true);
					  xmlhttp.send(null)
}
}

function mnie(){
	if ((document.formu.pais.value=='España') && (document.formu.nacionalidad.value=='Fuera de España')) {
		alert('Si resides en España pero tu nacionalidad no es Española o Comunitaria (CEE), el campo NIE es obligatorio');
		document.getElementById("cnie").style.visibility='visible';
		document.formu.nie.style.visibility='visible';
		document.formu.nie.style.backgroundColor = "#E14E29";
	}
	else {
		document.getElementById("cnie").style.visibility='hidden';
		document.formu.nie.style.visibility='hidden';
	}
}
function okhomeceac(){
if (document.formu.nombre.value=="" || document.formu.apellidos.value=="" || document.formu.email.value=="" || document.formu.pais.value=="" || document.formu.direccion.value=="" || document.formu.poblacion.value=="" || document.formu.provincia.value=="" || document.formu.tel.value=="" || document.formu.cp.value=="" || document.formu.sexo.value=="") {
	alert('Por favor, rellene todos los campos correctamente');
	return false;
	}
if ((document.formu.pais.value=='España') && (document.formu.provincia.value=='Fuera de España')) {
	alert('Si has elegido España en Pais, debes seleccionar una provincia válida');
	return false;
	}
if ((document.formu.pais.value!='España') && (document.formu.provincia.value!='Fuera de España')) {
	alert('Si has elegido una provincia de España, en Pais debes seleccionar España');
	return false;
	}	
if (isNaN(document.formu.tel.value))  {
		alert ('El teléfono debe tener SOLO números sin espacios ni otros caracteres');
		document.formu.tel.focus();
		return false;
		}
var vv=document.formu.tel.value.substr(0,1);
var vvv=document.formu.tel.value;

if (document.formu.provincia.value!="Fuera de España")	{
		if (document.formu.tel.value.length!=9 ) {
			alert ('El teléfono debe tener 9 cifras sin espacios ni otros caracteres');
			document.formu.tel.focus();
			return false;
			}
		if (vv!=9 && vv!=6 && vv!=8 ) {
				alert ('El teléfono NO es correcto. No es un teléfono de España');
				document.formu.tel.focus();
				return false;
				}
		if (isNaN(document.formu.cp.value) || document.formu.cp.value.length!=5)  {
				alert ('El Código Postal NO es correcto. No es un C.P. de España');
				document.formu.cp.focus();
				return false;
				}
}
if (vvv==000000000 || vvv==999999999 || vvv==666666666 || vvv==888888888 ) {
	alert ('El teléfono NO es correcto');
	document.formu.tel.focus();
	return false;
	}


if (document.formu.direccion.value=="" ) {
			alert ('El campo dirección es obligatorio');
			document.formu.direccion.focus();
			return false;
			}	
if (document.formu.num.value=="" ) {
			alert ('El campo número es obligatorio');
			document.formu.num.focus();
			return false;
			}	
if (document.formu.nacionalidad.value=="" ) {
			alert ('El campo nacionalidad es obligatorio');
			document.formu.nacionalidad.focus();
			return false;
			}
if ((document.formu.pais.value=='España') && (document.formu.nacionalidad.value=='Fuera de España')) {
	if (document.formu.nie.value=='') {
		alert('Si resides en España pero tu nacionalidad no es Española o Comunitaria (CEE) el campo NIE es obligatorio');
		return false;
	}
	if ( document.formu.nie.value.length!=9) {
		alert('El campo NIE tiene que tener 9 caracteres');
		return false;
		}
	var vvv=document.formu.nie.value.substr(0,1);		
	if ((vvv!="X") && (vvv!="x") && (vvv!="Y") && (vvv!="y")) {
		alert('El campo NIE debe comenzar por la letra X o Y');
		return false;
	}
	var vvvv=document.formu.nie.value.substr(1,7);		
	if (isNaN(vvvv)) {
		alert('Los caracteres desde el 2 al 8 ambos incluidos del campo NIE deben ser números');
		return false;
	}			
	
}				
if (isDate(document.formu.edad.value)==false ) {
	document.formu.edad.focus();
	return false;
	}	
texto=document.formu.email.value
   var mailres = true; 
    var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-"; 
    var arroba = texto.indexOf("@",0); 
    if ((texto.lastIndexOf("@")) != arroba) arroba = -1; 
    var punto = texto.lastIndexOf("."); 
 
     for (var contador = 0 ; contador < texto.length ; contador++){ 
        if (cadena.indexOf(texto.substr(contador, 1),0) == -1){ 
            mailres = false; 
            break; 
     } 
    } 

    if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1)) 
     mailres = true; 
    else 
     mailres = false; 
  
    if (mailres==false) {
			alert('La dirección de email es errónea. Comprueba que está escrita correctamente y sin espacios');
			 return false; 
			 }
document.formu.ft.value="si";
return true;
}

var dtCh= "/";
var minYear=1030;
var maxYear=2020;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("La fecha de nacimiento debe ser en formato : dd/mm/aaaa")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Por favor, escriba un mes correcto en la fecha de nacimiento")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Por favor, escriba un día correcto en la fecha de nacimiento")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Por favor, escriba un año correcto en la fecha de nacimiento")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Por favor, escriba correcatamente la fecha de nacimiento. Debe ser dd/mm/aaaa")
		return false
	}
return true
}

function Left(s, n){
// Devuelve los n primeros caracteres de la cadena
	if(n>s.length)
		n=s.length;					
		return s.substring(0, n);
}					
function UCase(s){
// Devuelve la cadena convertida a mayúsculas
	return s.toUpperCase();
}				
function InStr(n, s1, s2){
	// Devuelve la posición de la primera ocurrencia de s2 en s1
	// Si se especifica n, se empezará a comprobar desde esa posición
	// Sino se especifica, los dos parámetros serán las cadenas
	var numargs=InStr.arguments.length;					
	if(numargs<3)
		return n.indexOf(s1)+1;
	else
		return s1.indexOf(s2, n)+1;
}						
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) 
			break; 
		}
	return null;
}
function cargadatos(){
		if (GetCookie("nombre")!=null) {
			var re =new RegExp("\\+","g");
			if (GetCookie("nombre")!=null) {
					document.formu.nombre.value=GetCookie("nombre").replace(re," ");
					document.getElementById("nombre").style.backgroundColor = "#FFFFCC";
			}
			if (GetCookie("apellidos")!=null) {
				document.formu.apellidos.value=GetCookie("apellidos").replace(re," ");
				document.getElementById("apellidos").style.backgroundColor = "#FFFFCC";
				if (InStr(UCase(GetCookie("nombre")), UCase(GetCookie("apellidos")))!=0) {
					document.formu.nombre.value=Left(GetCookie("nombre").replace(re," "),InStr(UCase(GetCookie("nombre")), UCase(GetCookie("apellidos")))-1)
				}									
			}
			if (GetCookie("telefono")!=null) {
				document.formu.tel.value=GetCookie("telefono").replace(re," ");
				document.getElementById("tel").style.backgroundColor = "#FFFFCC";
			}
			if (GetCookie("direccion")!=null) {	
				document.formu.direccion.value=GetCookie("direccion").replace(re," ");
				document.getElementById("direccion").style.backgroundColor = "#FFFFCC";
			}	
			if (GetCookie("numero")!=null) {	
				document.formu.num.value=GetCookie("numero").replace(re," ");
				document.getElementById("num").style.backgroundColor = "#FFFFCC";
				if (InStr(UCase(GetCookie("direccion")), UCase(GetCookie("numero")))!=0) {
					document.formu.direccion.value=Left(GetCookie("direccion").replace(re," "),InStr(UCase(GetCookie("direccion")), UCase(GetCookie("numero")))-1)
				}									
			}	
			if (GetCookie("poblacion")!=null) {				
				document.formu.poblacion.value=GetCookie("poblacion").replace(re," ");
				document.getElementById("poblacion").style.backgroundColor = "#FFFFCC";		
			}
			if (GetCookie("amail")!=null) {
				document.formu.email.value=GetCookie("amail");
				document.getElementById("email").style.backgroundColor = "#FFFFCC";	
			}
			if (GetCookie("provincia")!=null) {
				var newElem = document.createElement("option");
				pro=GetCookie("provincia").replace(re," ")
				newElem.text = pro;
				newElem.value = pro;
				document.formu.provincia.options.add(newElem,0);
				document.formu.provincia.options[0].selected=true;
				document.getElementById("provincia").style.backgroundColor = "#FFFFCC";	
			}
			if (GetCookie("cp")!=null) {
				document.formu.cp.value=GetCookie("cp");
				document.getElementById("cp").style.backgroundColor = "#FFFFCC";	
			}							
		}
}
