// JavaScript Document
function f_cambiar(e){
	for(i=1;i<4;i++){
		document.getElementById("paso"+i).className ="";
		document.getElementById("div"+i).style.display = "none";
	}
	 if(e<3 || (e==3 & f_valida())){
		document.getElementById("paso"+e).className ="sel";
		document.getElementById("div"+e).style.display = "block";
		window.scrollTo(0,0);
	}
}
var cont = 1;
function f_aniadir(){
	cont++;
	var dl = document.getElementById("nf");	
	var read = document.getElementById("pp");	
	var dd = document.getElementById("nfdd");
	var inp = document.createElement("input");
	inp.type = "file";
	inp.className = "sizeXXL";
	inp.id = "control"+cont;
	inp.name = document.getElementById("control" + (cont-1)).name;
	var br = document.createElement("br");
	dd.appendChild(br);
	dd.appendChild(inp);
	read.appendChild(br);
	var inn = document.createElement("input");
	inn.type = "text";
	inn.readonly = true;
	inn.className = "sizeXXL resumen";
	inn.id = "tddocumento"+cont;
	read.appendChild(inn);
}

function elige(p){
	var quien=p;
	var f = document.forms[0];
	if ((p=='0')||(p=='1')) {
		document.getElementById("nombre").innerHTML="Nombre* ";
		document.getElementById("apellidos").innerHTML="Apellidos* ";
		f.reclamacion_apellidos.disabled=false;
	}if (p=='2') {
		document.getElementById("nombre").innerHTML="Razón social* ";
		document.getElementById("apellidos").innerHTML="";
		f.reclamacion_apellidos.disabled=true;
	}
}

function f_valida(){
	var f = document.forms[0];
	var tipoDocu=(f.tipodocu[0].checked)?0:(f.tipodocu[1].checked)?1:2;
		if(esVacio(f.reclamacion_NIF.value)){
				alert("Debes indicar un valor en el campo NIF/NIE/CIF");
				f_cambiar(1);
				f.reclamacion_NIF.select();
		return false;}

		if((tipoDocu==0)||(tipoDocu==1)){
			if(!ValidaNifNie(tipoDocu)){
			if(tipoDocu==0)	alert("El N.I.F. no es correcto.");else alert("El N.I.E. no es correcto.");
			f_cambiar(1);
			f.reclamacion_NIF.select();
		return false;}
		}else{
			if(!esCif(f.reclamacion_NIF.value)){
			alert("El C.I.F.no es correcto.");
			f_cambiar(1);
			f.reclamacion_NIF.select();
			return false;}
	}document.getElementById("tdnif").value = f.reclamacion_NIF.value;

	if(esVacio(f.reclamacion_nombre.value)){
		alert("El nombre es obligatorio");
		f_cambiar(1);
		f.reclamacion_nombre.focus();
		return false;
	}document.getElementById("tdnombre").value = f.reclamacion_nombre.value;

	if((esVacio(f.reclamacion_apellidos.value) && (tipoDocu!=2))){
		alert("El apellido es obligatorio");
		f_cambiar(1);
		f.reclamacion_apellidos.focus();
		return false;
	}document.getElementById("tdapellidos").value = f.reclamacion_apellidos.value;

	if(esVacio(f.reclamacion_domicilio.value)){
		alert("Debes indicar tu domicilio");
		f_cambiar(1);
		f.reclamacion_domicilio.focus();
		return false;
	}document.getElementById("tddomi").value = f.reclamacion_domicilio.value;
	if(esVacio(f.reclamacion_numero.value)){
		alert("Debes indicar el número de tu calle");
		f_cambiar(1);
		f.reclamacion_numero.select();
		return false;
	}document.getElementById("tdnro").value = f.reclamacion_numero.value;
	if(!esNumerico(f.reclamacion_codpostal.value) || f.reclamacion_codpostal.value.length !=5){
		alert("El código postal es obligatorio, y debe tener 5 dígitos");
		f_cambiar(1);
		f.reclamacion_codpostal.select();
		return false;
	}document.getElementById("tdcpostal").value = f.reclamacion_codpostal.value;
	if(esVacio(f.reclamacion_localidad.value)){
		alert("Debes indicar tu población");
		f_cambiar(1);
		f.reclamacion_localidad.select();
		return false;
	}document.getElementById("tdlocalidad").value = f.reclamacion_localidad.value;
	if(f.reclamacion_provincia.selectedIndex ==0){
		alert("Debes seleccionar una provincia");
		f_cambiar(1);
		f.reclamacion_provincia.focus();
		return false;
	}if(f.reclamacion_codpostal.value.substring(0,2)!= f.reclamacion_provincia.value){
		alert("El código postal no coincide con la provincia seleccionada");
		f_cambiar(1);
		f.reclamacion_codpostal.select();
		return false;
	}document.getElementById("tdprovincia").value = f.reclamacion_provincia.options[f.reclamacion_provincia.selectedIndex].text;
	if(!f.reclamacion_declaro2.checked){
		alert("Debes aceptar las declaraciones");
		f_cambiar(2);
		f.reclamacion_declaro2.focus();
		return false;
	}
	if(!f.reclamacion_acepto.checked){
		alert("Debes aceptar la cláusula de protección de datos");
		f_cambiar(2);
		f.reclamacion_acepto.focus();
		return false;
	}
	document.getElementById("dtdoc").innerHTML =(tipoDocu==0)?"N.I.F.":(tipoDocu==1)?"N.I.E.":"C.I.F.";
	document.getElementById("dtapell").innerHTML =(tipoDocu==2)?"":"Apellidos:";
	document.getElementById("tdtel").value = f.reclamacion_telefono.value;
	document.getElementById("tdtel").style.visibility =(esVacio(f.reclamacion_telefono.value))?"hidden":"visible";
	document.getElementById("tdfax").value = f.reclamacion_fax.value;
	document.getElementById("tdfax").style.visibility =(esVacio(f.reclamacion_fax.value))?"hidden":"visible";
	document.getElementById("tdemail").value = f.reclamacion_email.value;
	document.getElementById("tdemail").style.visibility =(esVacio(f.reclamacion_email.value))?"hidden":"visible";
	document.getElementById("tdtoficina").value = f.reclamacion_departamento.value;
	document.getElementById("tdtoficina").style.visibility =(esVacio(f.reclamacion_departamento.value))?"hidden":"visible";
	document.getElementById("tdmotivo").value = f.reclamacion_sugerencia.value;
	document.getElementById("tdmotivo").style.visibility =(esVacio(f.reclamacion_sugerencia.value))?"hidden":"visible";
	document.getElementById("tdcuestiones").value = f.reclamacion_pronunciarme.value;
	document.getElementById("tdcuestiones").style.visibility =(esVacio(f.reclamacion_pronunciarme.value))?"hidden":"visible";
	var files = document.getElementById("pp").getElementsByTagName("input");
	for(var i=0;i<files.length;i++){
		var j = i+1;
		document.getElementById("tddocumento"+j).value = document.getElementById("control"+j).value;
		document.getElementById("tddocumento"+j).style.visibility =(esVacio(document.getElementById("control"+j).value))?"hidden":"visible";
	}
	return true;
}
function esVacio(cadena){
	cadena = cadena.replace(/\s/g,"");
	cadena = (cadena =="")? true:false;
	return cadena;
}
function ValidaNifNie(qq){
	var letras=new Array("T","R","W","A","G","M","Y","F","P","D","X","B","N","J","Z","S","Q","V","H","L","C","K","E");
	var validacion=(qq==0)?"NIF":"NIE";
	var dato=document.forms[0].reclamacion_NIF.value;
	var filtro=(qq==0)?/(\d){2,8}[a-z]/i:/x(\d){7}[a-z]/i;	
	if(dato.search(filtro)<0) return false;
	var numero=dato.substring(qq,dato.length-1);
	var letra=dato.substring(dato.length-1).toUpperCase();
	var tmp=parseInt(numero/23)*23;
	tmp=numero-tmp;
	if(letras[tmp]==letra) return true;
	else return false;
}
function esCif(cadena){
	if(esVacio(cadena))return false;
	cadena = cadena.replace(/\s/g,"").toUpperCase();
	if(cadena.length!=9)return false;
	var digitosC = ['Z','A','B','C','D','E','F','G','H','I','J']
	var digitos = ['A','B','C','D','E','F','G','H','K','L','M','N','P','Q','S']
	var letra = cadena.substring(0,1);
	var tf = false;
	for(i=0;i<15;i++)
		if(letra==digitos[i])tf=true;
	if(!tf)return false;
	var numero = cadena.substring(1,cadena.length-1);
	var dc = cadena.substring(cadena.length-1);
	if(numero.match(/\D/)!=null || letra.match(/[^A-Z]/)!=null)return false;
	var a=0;
	for(i=0; i<numero.length;i++){
		var n = parseInt(numero.substring(i,i+1),10);
		if(i%2!=0)a+=n;
		else{
			n = String(n*2);
			for(x=0;x<n.length;x++)a += parseInt(n.substring(x,x+1));
		}
	}
	var n =String(a).substring(1,2);
	n=10-parseInt(n,10);
	if(dc!= digitosC[n] && dc!=n)return false;	
	return true;
}
function esNumerico(cadena){
	if(esVacio(cadena))return false;
	cadena = cadena.match(/\D/);
	cadena = (cadena == null)?true:false;	
	return cadena;
}

function esEmail(cadena){
	if(esVacio(cadena))return false;
	var cadenas = cadena.split("@");
	if(cadenas.length!=2)return false;
	if(esVacio(cadenas[0]) || esVacio(cadenas[1]))return false;
	var cadenas1 = cadenas[1].split(".")
	if(cadenas1.length<2)return false;
	for(c=0;c<cadenas1.length;c++)
		if(esVacio(cadenas1[c]))return false;
	if(cadenas1[cadenas1.length-1].length>3 ||cadenas1[cadenas1.length-1].length<2)return false;
	return true;
}
function f_aceptar(){
	var t = document.forms[0];
	if(f_valida()){
		t.submit();	
	}
}
