// JavaScript Document

//validamos los formularios
function valida_envia (form){
	if (form.departamento){
		if (form.departamento.value == 0)
		{
		   //alert ("Debe seleccionar un departamento");
		   form.departamento.focus();
		   form.departamento.style.backgroundColor="rgb(251, 208, 208)";
		   return false;
		}
		else{
			form.departamento.style.backgroundColor="rgb(238, 238, 238)";
		}
	}
	if (form.nombre){
		if (form.nombre.value == ""){
			form.nombre.focus();
			form.nombre.style.backgroundColor="rgb(251, 208, 208)";
			return false;
		}
		else{
			form.nombre.style.backgroundColor="rgb(238, 238, 238)";
		}
	}
	if (form.apellidos){
		if (form.apellidos.value == ""){
			form.apellidos.focus();
			form.apellidos.style.backgroundColor="rgb(251, 208, 208)";
			return false;
		}
		else{
			form.apellidos.style.backgroundColor="rgb(238, 238, 238)";
		}
	}
	if (form.email){
		if (!emailCheck (form.email.value))
		{
			form.email.focus();
			form.email.style.backgroundColor="rgb(251, 208, 208)";
			return false;
		}
		else{
			form.email.style.backgroundColor="rgb(238, 238, 238)";
		}
	}
	if (form.tlf){
		if( !(/^\d{9}$/.test(form.tlf.value)) ) {
			form.tlf.focus();
			form.tlf.style.backgroundColor="rgb(251, 208, 208)";
			return false;
		}
		else{
			form.tlf.style.backgroundColor="rgb(238, 238, 238)";
		}
	}
	if (form.curriculum){
		if (form.curriculum.value == ""){
			form.curriculum.focus();
			form.curriculum.style.backgroundColor="rgb(251, 208, 208)";
			return false;
		}
		else{
			form.curriculum.style.backgroundColor="rgb(238, 238, 238)";
		}
	}
	if (form.categoria){
		if (form.categoria.value == 0){
			form.categoria.focus();
			form.categoria.style.backgroundColor="rgb(251, 208, 208)";
			return false;
		}
		else{
			form.curriculum.style.backgroundColor="rgb(238, 238, 238)";
		}
	}
	if (form.acepto){
		if (!form.acepto.checked)
		{
			alert("La casilla de conformidad con la pol"+String.fromCharCode(237)+"tica de tratamiento de datos debe estar marcada");
			form.acepto.focus();
			form.acepto.style.backgroundColor="rgb(251, 208, 208)";
			return false;
		}
		else{
			form.email.style.backgroundColor="rgb(238, 238, 238)";
		}
	}
	
	
	return true;
}

function emailCheck (emailStr) {
    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) {
    	//alert("El campo 'Email' está vacio o es incorrecto");
    	return false;
    }
    
	var user=matchArray[1];
    var domain=matchArray[2];
    
	for (i=0; i<user.length; i++) {
    	if (user.charCodeAt(i)>127) {
    		//alert("El campo 'Email' contiene caracteres no validos");
    		return false;
       }
    }
	
    for (i=0; i<domain.length; i++) {
    	if (domain.charCodeAt(i)>127) {
    		//alert("El campo 'Email' contiene caracteres no validos");
    		return false;
       }
    }

	if (user.match(userPat)==null) {
    	//alert("El campo 'Email' es incorrecta");
    	return false;
    }
    
	var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
    	for (var i=1;i<=4;i++) {
    		if (IPArray[i]>255) {
    			//alert("La dirección IPInternet Protocol de destino no es correcta!");
    			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) {
    		//alert("El campo 'Email' es incorrecto, por favor compruebalo, incluyendo el uso incorrecto de signos de puntuación, comas , o puntos [.] al final de la dirección.");
    		return false;
       	}
    }
	
    if (checkTLD && domArr[domArr.length-1].length!=2 &&
    domArr[domArr.length-1].search(knownDomsPat)==-1) {
    	//alert("El campo 'Email' debe terminar en un dominio o dos letras " + "país.");
    	return false;
    }
    
	if (len<2) {
    	//alert("Falta el nombre del host en tu dirección de correo - compruebalo. O debes de haber anadido un espacio en blanco al final de la dirección - corrije el error e intentalo de nuevo.");
    	return false;
    }
    
	return true;
}

function num_decimal_valido(n){
	
	var expr = /^[0-9]*(\.[0-9]+)?$/;
	if (expr.test(n))
		return true;
	else (expr.test(n))
		return false;
}

function valida_form1_simulador(form){
	
	if (form.precio_energia.value == "" || form.precio_energia.value <= 0 || !num_decimal_valido(form.precio_energia.value) ){
	   //alert ("Debe seleccionar un departamento");
	   form.precio_energia.focus();
	   form.precio_energia.style.backgroundColor="rgb(251, 208, 208)";
	   return false;
	}
	else{
		form.precio_energia.style.backgroundColor="rgb(238, 238, 238)";
	}
	
	if (form.importe_potencia.value == "" || form.importe_potencia.value <= 0 || !num_decimal_valido(form.precio_energia.value) ){
	   //alert ("Debe seleccionar un departamento");
	   form.importe_potencia.focus();
	   form.importe_potencia.style.backgroundColor="rgb(251, 208, 208)";
	   return false;
	}
	else{
		form.importe_potencia.style.backgroundColor="rgb(238, 238, 238)";
	}
	
	if (form.importe_energia.value == "" || form.importe_energia.value <= 0|| !num_decimal_valido(form.precio_energia.value) ){
	   //alert ("Debe seleccionar un departamento");
	   form.importe_energia.focus();
	   form.importe_energia.style.backgroundColor="rgb(251, 208, 208)";
	   return false;
	}
	else{
		form.importe_energia.style.backgroundColor="rgb(238, 238, 238)";
	}
	
	if (form.alquiler_equipo.value == "" || form.alquiler_equipo.value <= 0 || !num_decimal_valido(form.precio_energia.value) ){
	   //alert ("Debe seleccionar un departamento");
	   form.alquiler_equipo.focus();
	   form.alquiler_equipo.style.backgroundColor="rgb(251, 208, 208)";
	   return false;
	}
	else{
		form.alquiler_equipo.style.backgroundColor="rgb(238, 238, 238)";
	}
	
	if (form.horas_func.value == "" || form.horas_func.value <= 0 || !num_decimal_valido(form.precio_energia.value) ){
	   //alert ("Debe seleccionar un departamento");
	   form.horas_func.focus();
	   form.horas_func.style.backgroundColor="rgb(251, 208, 208)";
	   return false;
	}
	else{
		form.horas_func.style.backgroundColor="rgb(238, 238, 238)";
	}
	
	if (form.dias_func.value == "" || form.dias_func.value <= 0 || !num_decimal_valido(form.precio_energia.value) ){
	   //alert ("Debe seleccionar un departamento");
	   form.dias_func.focus();
	   form.dias_func.style.backgroundColor="rgb(251, 208, 208)";
	   return false;
	}
	else{
		form.dias_func.style.backgroundColor="rgb(238, 238, 238)";
	}
	
	return true;
}

function reemplazar_coma(e){
	e.value = e.value.replace(",",".");
}
function pasa_euro(e){
	e.value = e.value / 100;
}

function valida_form2_simulador(form){
	
	var aFl = document.getElementsByName('fl[]');
	var aDw = document.getElementsByName('dw[]');
	var aHa = document.getElementsByName('ha[]');
	var aBo = document.getElementsByName('bo[]');
	
	var todo_ceros = true;
	var cont = 1;
	var id = "";
	
	id = "fl_";
	for(i=0;i<aFl.length;i++){
		id_aux = id + cont;
		inp = document.getElementById(id_aux);
		if (isNaN(aFl[i].value)){
			inp.focus();
			inp.style.backgroundColor = "rgb(251, 208, 208)";
			return false;
		}
		else if (aFl[i].value != 0)
			todo_ceros = false;
		else
			inp.style.backgroundColor = "rgb(238, 238, 238)";
		
		cont++;
	}
	
	cont = 1;
	id = "dw_";
	for(i=0; i<aDw.length; i++){
		id_aux = id + cont;
		inp = document.getElementById(id_aux);
		if (isNaN(aDw[i].value)){
			inp.focus();
			inp.style.backgroundColor = "rgb(251, 208, 208)";
			return false;
		}
		else if (aDw[i].value != 0)
			todo_ceros = false;
		else
			inp.style.backgroundColor = "rgb(238, 238, 238)";
			
		cont++;
	}
	
	
	cont = 1;
	id = "ha_";
	for(i=0; i<aHa.length; i++){
		id_aux = id + cont;
		inp = document.getElementById(id_aux);
		if (isNaN(aHa[i].value)){
			inp.focus();
			inp.style.backgroundColor = "rgb(251, 208, 208)";
			return false;
		}
		else if (aHa[i].value != 0)
			todo_ceros = false;
		else
			inp.style.backgroundColor = "rgb(238, 238, 238)";
			
		cont++;
	}
	
	cont = 1;
	id = "bo_";
	for(i=0; i<aBo.length; i++){
		id_aux = id + cont;
		inp = document.getElementById(id_aux);
		if (isNaN(aBo[i].value)){
			inp.focus();
			inp.style.backgroundColor = "rgb(251, 208, 208)";
			return false;
		}
		else if (aBo[i].value != 0)
			todo_ceros = false;
		else
			inp.style.backgroundColor = "rgb(238, 238, 238)";
			
		cont++;
	}
	
	if ( todo_ceros ){
		alert ("Por favor, inserte el número de unidades luminarias utilizadas en función del tipo y potencia de las mismas");
		return false;
	}
	
	return true;
}
