//inicio
function init () {
	fechaActual();
	horaActual();
	document.frmdatos.dir_correo.focus();
}


//asigna fecha actual
function fechaActual() {
	var fecha = new Date();
	var dia = fecha.getDate();
	if (dia < 10)
		dia = "0" + dia;
	var mes = fecha.getMonth();
	mes = mes+1;
	if (mes < 10)
		mes = "0" + mes;
	var anio = fecha.getYear();
	if (anio < 2000) 
		anio = anio + 1900;
	fecha = dia + "/" + mes + "/" + anio;
	if ('<%=idioma%>'=='1') 
		fecha = anio + "/" + mes + "/" + dia;
	document.frmdatos.fecha_ataque.value = fecha;
	return true;
}



//asigna hora actual
function horaActual(){
	var fecha = new Date();
	var hora = fecha.getHours();
	var minuto = fecha.getMinutes();
	if (minuto < 10) 
		minuto = "0" + minuto;
	var horita = hora + ":" + minuto;
	document.frmdatos.hora_ataque.value = horita;
	return true;
} 



//obtiene el UTC de la zona horaria
function zonaHorariaActual() {
	var fecha = new Date();
	var difmin = fecha.getTimezoneOffset()*(-1);
	var minutos = difmin % 60;
	minutos = String(minutos);
	if (minutos.length==1) 
		minutos = '0' + minutos;
	var horas = difmin / 60;
    var DT = 2 * fecha.getTimezoneOffset() - (new Date(2004, 0)).getTimezoneOffset() - (new Date(2004, 6)).getTimezoneOffset();
	if (DT < 0) {
		horas--;
	}
	var utc = '0:00';
	if (horas > 0) utc = '+' + horas + ':' + minutos;
	if (horas < 0) utc = '-' + horas + ':' + minutos;
	return utc;
}



//se posiciona en la lista de la zona horaria segun el utc
function selec_zona() {
	var el_indice = zonaHorariaActual();
	for (var i=0; i<totalzonas; i++) {
		if (document.frmdatos.zona_horaria.options[i].value == el_indice) {
			document.frmdatos.zona_horaria.options[i].selected=true;
			return true;
		}
	}
	return true;
}


function desglosarIP(IP_entera) {
	var i1  = IP_entera.indexOf(".");
	var i2  = IP_entera.indexOf(".",i1+1);
	var i3  = IP_entera.indexOf(".",i2+1);
	var ip1 = IP_entera.substring(0,i1);
	var ip2 = IP_entera.substring(i1+1,i2);
	var ip3 = IP_entera.substring(i2+1,i3);
	var ip4 = IP_entera.substring(i3+1,IP_entera.length);
	var comparar = (ip1*16777216)+(ip2*65536)+(ip3*256)+(ip4*1);
	return comparar;
}


//Funcion que comprueba si la IP introducida en el formulario pertenece a Euskaltel
function IP_Ekt(mi_IP) {
	//buscamos en el rango de IP's del fich. txt
	for (var i=0; i<direcciones_IP.length; i++) {
		var IP_rango = direcciones_IP[i];
		var IP_desde = IP_rango.substring(0,IP_rango.indexOf("-"));
		var IP_hasta = IP_rango.substring(IP_rango.indexOf("-")+1,IP_rango.length);
		
		//desglosamos las IPs para compararlas... 
		var comparar_IP      = desglosarIP(mi_IP);
		var comparar_IPDesde = desglosarIP(IP_desde);
		var comparar_IPHasta = desglosarIP(IP_hasta);
		if (comparar_IPDesde<=comparar_IP && comparar_IP<=comparar_IPHasta) {
			return true;
		}
	}
	return false;
}


//Funcion que comprueba si la IP es valida
function IPValida(mi_IP) {
	var i1  = mi_IP.indexOf(".");
	var ip1 = mi_IP.substring(0,i1);
	if (0<=ip1 && ip1<=255)	{
		var i2  = mi_IP.indexOf(".",i1+1);
		var ip2 = mi_IP.substring(i1+1,i2);
		if (0<=ip2 && ip2<=255)	{
			var i3  = mi_IP.indexOf(".",i2+1);
			var ip3 = mi_IP.substring(i2+1,i3);
			if (0<=ip3 && ip3<=255)	{
				var ip4 = mi_IP.substring(i3+1,mi_IP.length);
				if (0<=ip4 && ip4<=255)	{
					return true;
				}
			}
		}
	}
	return false;
}


//Funcion de validacion al pulsar el boton Enviar
function validacion(idioma) { //gestion de campos obligatorios
	var campo = "";
	var error = "0"; // O - no hay errores  1 - hay errores
	var msg = "";
	var elCampo;
	
	//FORM ABUSE
	if (document.frmdatos.dir_correo!= null)
	{
		if(document.frmdatos.dir_correo.value == "" && error == "0") {
			error= "1";
			switch (idioma) {
				case "0": campo = "'E-mail de contacto'"; break;
				case "1": campo = "'Helbide elektronikoa'"; break;
				case "2": campo = "'E-mail'"; break;
			}
			elCampo="document.frmdatos.dir_correo";
		} else {
			if (document.frmdatos.dir_correo.value.indexOf('@', 0) == -1 || document.frmdatos.dir_correo.value.indexOf('.') == -1) {
				error= "1";
				switch (idioma) {
					case "0": msg= "La direccion de correo que ha introducido no es correcta."; break;
					case "1": msg= "Idatzi duzun helbide elektronikoa ez da zuzena."; break;
					case "2": msg= "The e-mail address indicated is not correct."; break;
				}
				elCampo="document.frmdatos.dir_correo";
			}
		}
	}

	if (document.frmdatos.descripcion!= null){
		if(document.frmdatos.descripcion.value == "" && error == "0"){
			error= "1";
			switch (idioma) {
				case "0": campo = "'Descripcion'";	 break;
				case "1": campo = "'Deskribapena'";	 break;
				case "2": campo = "'Description'";	 break;
			}
			elCampo="document.frmdatos.descripcion";
		} 
	}

	if (document.frmdatos.tipo_ataque!= null){
		if(document.frmdatos.tipo_ataque.value == "" && error == "0"){
			error= "1";
			switch (idioma) {
				case "0": campo = "'Tipo de incidente'"; break;
				case "1": campo = "'Eraso-mota'";		 break;
				case "2": campo = "'Type of incident'";	 break;
			}
			elCampo="document.frmdatos.tipo_ataque";
		}
	}

	if (document.frmdatos.fecha_ataque!= null){
		if(document.frmdatos.fecha_ataque.value == "" && error == "0"){
			error= "1";
			switch (idioma) {
				case "0": campo = "'Fecha del incidente'";	 break;
				case "1": campo = "'Gertaeraren data'";		 break;
				case "2": campo = "'Date of the incident'";	 break;
			}
			elCampo="document.frmdatos.fecha_ataque";
		} else {
			if ( error=="0"){
				//comprueba que el formato de la fecha es correcto
				var fecha = document.frmdatos.fecha_ataque.value;
				var texto_aux= fecha.substring(fecha.indexOf('/',0)+1,fecha.length);
				if (fecha.indexOf('/',0) == -1){
					error= "1";
				} else { 
					if (idioma == 0 || idioma == 2) {
						var dia = fecha.substr(0,fecha.indexOf('/'));
						var mes = texto_aux.substr(0,texto_aux.indexOf('/'));
						var anyo = texto_aux.substr(texto_aux.indexOf('/')+1,texto_aux.length);				
					} else {
						var anyo = fecha.substr(0,fecha.indexOf('/'));
						var mes = texto_aux.substr(0,texto_aux.indexOf('/'));
						var dia = texto_aux.substr(texto_aux.indexOf('/')+1,texto_aux.length);		
					}
					
					if (dia <1 || dia > 31 ) error="1";
					if (mes <1 || mes >12) error="1";
					if (anyo <2005) error="1";
				}
				if (error == "1") {
					switch (idioma) {
						case "0": msg= "La fecha que ha introducido no tiene un formato correcto o no es correcta."; break;
						case "1": msg= "Idatzi duzun dataren formatua edo data bera ez da zuzena."; break;
						case "2": msg= "The format of the date indicated is not correct, or else the date is not correct."; break;
					}
				}
				elCampo="document.frmdatos.fecha_ataque";
			}
		}
	}
	
	if (document.frmdatos.hora_ataque!= null){
		if(document.frmdatos.hora_ataque.value == "" && error == "0"){
			error= "1";
			switch (idioma) {
				case "0": campo = "'Hora del incidente'";	 break;
				case "1": campo = "'Gertaeraren ordua'";	 break;
				case "2": campo = "'Time of the incident'";	 break;
			}
			elCampo="document.frmdatos.hora_ataque";
		} else {
			if ( error=="0"){
				//comprueba que el formato de la hora es correcto
				var hora_completa = document.frmdatos.hora_ataque.value;
				if (hora_completa.indexOf(':', 0) == -1){
					error= "1";
				} else {
					var puntos = hora_completa.indexOf(':');
					var hora = hora_completa.substr(0,puntos);
					if (isNaN(parseInt(hora))) error= "1";
					if (hora.length>2 || hora=="") error= "1";
					if (hora <0 || hora > 24 ) error="1";
									
					var minutos = hora_completa.substr(puntos+1,hora_completa.length);
					if (isNaN(parseInt(minutos))) error= "1";
					if (minutos.length!=2 || minutos=="") error= "1";
					if (minutos <0 || minutos >59) error="1";
				}
				if (error == "1") {
					switch (idioma) {
						case "0": msg= "La hora que ha introducido no tiene un formato correcto o no es correcta."; break;
						case "1": msg= "Idatzi duzun orduaren formatua edo ordua bera ez da zuzena."; break;
						case "2": msg= "The format of the time indicated is not correct, or else the time is not correct."; break;
					}
				}
				elCampo="document.frmdatos.hora_ataque";
			}
		}
	}

	if (document.frmdatos.zona_horaria!= null){
		if(document.frmdatos.zona_horaria.value == "00" && error == "0"){
			error= "1";
			switch (idioma) {
				case "0": campo = "'Zona horaria'";	 break;
				case "1": campo = "'Ordu-eremua '";	 break;
				case "2": campo = "'Time zone'";	 break;
			}
			elCampo="document.frmdatos.zona_horaria";
		}
	}

	if (document.frmdatos.ip_origen!= null){
		if(document.frmdatos.ip_origen.value == "" && error == "0"){
			error= "1";
			switch (idioma) {
				case "0": campo = "'IP origen'";		break;
				case "1": campo = "'Jatorrizko IPa'";	break;
				case "2": campo = "'Source IP'";		break;
			}
			elCampo="document.frmdatos.ip_origen";
		} else {
			if (error=="0"){
				var direccion_IP = document.frmdatos.ip_origen.value;
				//si la IP introducida en el formulario es una IP valida
				if (IPValida(direccion_IP)) {
					//comprueba que pertenece al rango de IP's de Euskaltel
					if (!IP_Ekt(direccion_IP)) {
						error= "1";
					} 
				}
				else {
					error= "1";
				}
				if (error=="1") {
					switch (idioma) {
						case "0": msg= "La IP que ha introducido no corresponde a Euskaltel."; break;
						case "1": msg= "Sartu duzun IPa ez da Euskaltelena."; break;
						case "2": msg= "The IP you entered does not belong to Euskaltel."; break;
					}
				}
				elCampo="document.frmdatos.ip_origen";
			}
		}
	}

	if (document.frmdatos.evidencias!= null){
		if(document.frmdatos.evidencias.value == "" && error == "0"){
			error= "1";
			switch (idioma) {
				case "0": campo = "'Evidencias'";	 break;
				case "1": campo = "'Ebidentziak'";	 break;
				case "2": campo = "'Evidences'";	 break;
			}
			elCampo="document.frmdatos.evidencias";
		} 
	}	

	//FORM SUGERENCIAS-CONSULTAS
	if (document.frmdatos.cif!= null){
		if(document.frmdatos.cif.value == "" && error == "0"){
			error= "1";
			switch (idioma) {
				case "0": campo = "'DNI/NIF/CIF'";	 break;
				case "1": campo = "'NAN/IFK/IFZ'";	 break;
				case "2": campo = "'CIF'";	 break;
			}
			elCampo="document.frmdatos.cif";
		} 
	}	
	if (document.frmdatos.email!= null)
	{
		if(document.frmdatos.email.value == "" && error == "0") {
			error= "1";
			switch (idioma) {
				case "0": campo = "'E-mail de contacto'"; break;
				case "1": campo = "'Helbide elektronikoa'"; break;
				case "2": campo = "'E-mail'"; break;
			}
			elCampo="document.frmdatos.email";
		} else {
			if (error=="0") {
				if (document.frmdatos.email.value.indexOf('@', 0) == -1 || document.frmdatos.email.value.indexOf('.') == -1) {
					error= "1";
					switch (idioma) {
						case "0": msg= "La direccion de correo que ha introducido no es correcta."; break;
						case "1": msg= "Idatzi duzun helbide elektronikoa ez da zuzena."; break;
						case "2": msg= "The e-mail address indicated is not correct."; break;
					}
				}
				elCampo="document.frmdatos.email";
			}
		}
	}
	if (document.frmdatos.resumen!= null){
		if(document.frmdatos.resumen.value == "" && error == "0"){
			error= "1";
			switch (idioma) {
				case "0": campo = "'Descripcion'";	 break;
				case "1": campo = "'Deskribapena'";	 break;
				case "2": campo = "'Description'";	 break;
			}
			elCampo="document.frmdatos.resumen";
		}
    	if(document.frmdatos.resumen.value.indexOf('<')!=-1 || document.frmdatos.resumen.value.indexOf('>')!=-1){
			document.frmdatos.resumen.value = escape(document.frmdatos.resumen.value);
		}
		document.frmdatos.resumen.value = tildes(document.frmdatos.resumen.value);
	}	
	if (document.frmdatos.apellido!= null){
	    document.frmdatos.apellido.value = tildes(document.frmdatos.apellido.value);
	}
	
	//FORM VALORE
	/*if (document.frmdatos.rating_seven_input!= null){
    alert(document.frmdatos.rating_seven_input.value);
	    if(document.frmdatos.rating_seven_input.value == 0 && error == "0"){
	    	error= "1";
		switch (idioma) {
			case "0": campo= "'Contenido'"; break;
			case "1": campo= "'Edukia'"; break;
		}
	    	elCampo="document.frmdatos.rating_seven_input";
	    }
	}	
	if (document.frmdatos.rating_seven2_input!= null){
	    if(document.frmdatos.rating_seven2_input.value == 0 && error == "0"){
	    	error= "1";
		switch (idioma) {
			case "0": campo= "'Diseño'"; break;
			case "1": campo= "'Diseinua'"; break;
		}
   	        elCampo="document.frmdatos.rating_seven2_input";
	    }
	}
	if (document.frmdatos.rating_seven3_input!= null){
	    if(document.frmdatos.rating_seven3_input.value == 0 && error == "0"){
	    	error= "1";
		switch (idioma) {
			case "0": campo= "'Navegación'"; break;
			case "1": campo= "'Nabigazioa'"; break;
		}
	        elCampo="document.frmdatos.rating_seven3_input";
	    }
	}*/

	if (document.frmdatos.mejoras!= null){

      		document.frmdatos.rating_seven_input.value = rating_seven.value;
      		document.frmdatos.rating_seven2_input.value = rating_seven2.value;
      		document.frmdatos.rating_seven3_input.value = rating_seven3.value;

	    if(document.frmdatos.mejoras.value == "" && error == "0"){
	    	error= "1";
		switch (idioma) {
			case "0": campo= "'¿Qué mejorarías en nuestro sitio web?'"; break;
			case "1": campo= "'Zer hobetuko zenuke gure webgunean?'"; break;
			//case "2": msg= "The e-mail address indicated is not correct."; break;
		}
		elCampo="document.frmdatos.mejoras";
	    }
	    if (document.frmdatos.mejoras.value != null){ 
		// En ciertos casos, los acentos y enyes se envian mal, asi que los sustituimos por sus codigos
		document.frmdatos.mejoras.value = tildes(document.frmdatos.mejoras.value);
	    }
	    
	}	
	/*if (document.frmdatos.rating_seven5_input!= null){
	    if(document.frmdatos.rating_seven5_input.value == 0 && error == "0"){
	    	error= "1";
		switch (idioma) {
			case "0": campo= "'Valora cómo consideras que el servicio de Euskaltel.com cumple tus expectativas'"; break;
			case "1": campo= "'Baloratu Euskaltel.com-ek zer neurritan erantzuten dien zure espektatibei'"; break;
			//case "2": msg= "The e-mail address indicated is not correct."; break;
		}
		elCampo="document.frmdatos.rating_seven5_input";
	    }
	}
	if (document.frmdatos.rating_seven6_input!= null){
	    if(document.frmdatos.rating_seven6_input.value == 0 && error == "0"){
	    	error= "1";
		switch (idioma) {
			case "0": campo= "'Valora tu nivel de satisfacción global con el servicio de Euskaltel.com'"; break;
			case "1": campo= "'Baloratu Euskaltel.com-en zerbitzuak oro har zer neurritan gogobetetzen zaituen'"; break;
			//case "2": msg= "The e-mail address indicated is not correct."; break;
		}
		elCampo="document.frmdatos.rating_seven6_input";
	    }	    
	}	
	if (document.frmdatos.rating_seven7_input!= null){
	    if(document.frmdatos.rating_seven7_input.value == 0 && error == "0"){
	    	error= "1";
		switch (idioma) {
			case "0": campo= "'Valora el servicio de Euskaltel.com respecto a tu ideal de un servicio perfecto'"; break;
			case "1": campo= "'Baloratu Euskaltel.com-en zerbitzua, zure ustez zerbitzu perfektua denarekin alderatuta'"; break;
			//case "2": msg= "The e-mail address indicated is not correct."; break;
		}
		elCampo="document.frmdatos.rating_seven7_input";
	    }
	}
	if (document.frmdatos.rating_seven8_input!= null){
	    if(document.frmdatos.rating_seven8_input.value == 0 && error == "0"){
	    	error= "1";
		switch (idioma) {
			case "0": campo= "'Valora cuanto recomendarías Euskaltel.com a tus familiares y amigos'"; break;
			case "1": campo= "'Baloratu zer neurritan gomendatuko zeniekeen Euskaltel.com zure senitarteko eta lagunei'"; break;
			//case "2": msg= "The e-mail address indicated is not correct."; break;
		}
		elCampo="document.frmdatos.rating_seven8_input";
	    }
	}*/

	if(error == "0" ){
		if (document.frmdatos.hora_ataque!= null){
			var hora_c = document.frmdatos.hora_ataque.value;
			var hora = hora_c.substr(0,hora_c.indexOf(':'));
			if (hora.length==2 && hora.substring(0,1)=="0") {
				document.frmdatos.hora_ataque.value = hora_c.substring(1,hora_c.length);
			}
		}
		if (document.frmdatos.descripcion!= null){ document.frmdatos.descripcion.value = tildes(document.frmdatos.descripcion.value);}
		if (document.frmdatos.evidencias!= null){ document.frmdatos.evidencias.value = tildes(document.frmdatos.evidencias.value);}
		if (document.frmdatos.observaciones!= null){ document.frmdatos.observaciones.value = tildes(document.frmdatos.observaciones.value);}
		if (document.frmdatos.zona_horaria!= null){ 
			var sel = document.frmdatos.zona_horaria.selectedIndex;
			document.frmdatos.zona_horaria.options[sel].value = document.frmdatos.zona_horaria.options[sel].text;
		}
		document.frmdatos.submit();
	}else{ 
		if (campo !=""){
			switch (idioma) {
				case "0": msg = "Para poder enviar su consulta es necesario que rellene el campo, " + campo;	 break;
				case "1": msg = "Zure kontsulta bidaltzeko, ezinbestekoa da " + campo + " eremua betetzea.";	 break;
				case "2": msg = "In order to enable the forwarding of your complaint, please fill in the "  + campo + " field.";	 break;
			}
		}
		alert(msg);
		eval(elCampo).focus();
		return;
	}
}

function campobli(idioma) {
	var camposcompletos="1";
	var mensaje_error="";
	if (document.forms[0].email.value!="") {
		if (document.forms[0].email.value.indexOf('@', 0) == -1 || document.forms[0].email.value.indexOf('.') == -1) {
			camposcompletos="0";
			switch (idioma) {
				case "0": mensaje_error= "La direccion de correo que ha introducido no es correcta."; break;
				case "1": mensaje_error= "Idatzi duzun helbide elektronikoa ez da zuzena."; break;
				case "2": mensaje_error= "The e-mail address indicated is not correct."; break;
			}
		}
	}
	document.forms[0].resumen.value = tildes(document.forms[0].resumen.value);		
	if (camposcompletos=="0") {
		alert(mensaje_error);
	} else {
		document.forms[0].submit ();
	}
}


function tildes(campo){
	var temp = "";
	for (var i=0; i < campo.length; i++) {
		var letter = campo.charAt(i);
		switch (letter) 		{
			case 'á': temp = temp + "&aacute;"; break
			case 'é': temp = temp + "&eacute;"; break
			case 'í': temp = temp + "&iacute;"; break
			case 'ó': temp = temp + "&oacute;"; break
			case 'ú': temp = temp + "&uacute;"; break
			case 'Á': temp = temp + "&Aacute;"; break
			case 'É': temp = temp + "&Eacute;"; break
			case 'Í': temp = temp + "&Iacute;"; break
			case 'Ó': temp = temp + "&Oacute;"; break
			case 'Ú': temp = temp + "&Uacute;"; break
			case 'ü': temp = temp + "&uuml;"; break
			case 'Ü': temp = temp + "&Uuml;"; break
			case 'ñ': temp = temp + "&ntilde;"; break
			case 'Ñ': temp = temp + "&Ntilde;"; break
			case 'º': temp = temp + "&#186;"; break
			case 'ª': temp = temp + "&#170;"; break
			case '€': temp = temp + "&#8364"; break
			case '\"': temp = temp + "&quot;"; break
			case '¿': temp = temp + "&#191;"; break
			case '?': temp = temp + "&#63;"; break
			default: 
				switch (letter.charCodeAt(0)) 		{		
					//case 10: temp = temp + "<BR>"; break
					case 13: temp = temp + "<BR>"; break
					default: temp = temp + letter;
				}				
		}
	}
	return temp;
}

function mostrarMensaje(idioma) {
	$("respuesta").innerHTML = "";
	if (idioma==0)
		$("respuesta").innerHTML = "<span style='font-size:12px'>Por favor, para poder abrir una reclamación, llama al teléfono gratuito de Atención al Cliente 1717.</span>";
	else
		$("respuesta").innerHTML ="<span style='font-size:12px'>Erreklamazio bat irekitzeko, deitu Bezeroarentzako doako Arreta Zerbitzura (1717).</span>";	
}
