var idioma = 0;

var textosError = Array(
							Array("Campo nombre obligatorio.","Campo mail obligatorio.","No es un mail correcto.","El repetir mail debe coincidir.","Debes seleccionar una provincia.","Debes aceptar la politica de privacidad.","El campo comentarios es obligatorio.","Debes seleccionar una foto","Debes elegir un idioma.","Campo dirección obligatorio.","Campo código postal obligatorio","Campo localizacion es obligatorio."),
							Array("Camp nom obligatori.","Camp mail obligatori.","No es un mail correcte.","El repetix mail te que coincidir.","Tens que seleccionar una provincia.","Tens que acceptar la politica de privacidad.","El camp comentaris es obligatori.","Tens que seleccionar una imatge","Tens que escollir un idioma.","Camp adreça obligatori.","Camp codi postal obligatori.","Camp localitzacio es obligatori.")
						);
						
var valoresCampos = Array(
							Array("Nombre","Repite el Email"),
							Array("Nom","Repeteix l'Email")
						 );
						 
var textoContacto = Array("Mensaje enviado correctamente.","Missatge enviat correctament.");

var textoErrorSuscripcion = Array("La subscripción no ha podido tramitarse correctamente debido a que tu email ya se encuentra en nuestra base de datos. Gracias","La suscripció no s'ha pogut tramitar degut a que el seu mail ja es troba a la nostra base de dades. Gracies.");
var textoOKSuscripcion = Array("Tu subscripción se ha tramitado correctamente. Pronto recibirás un correo con instrucciones para confirmar y activar tu alta. Gracias","La seva suscripció s'ha tramitat correctament. En breu rebrà un correu amb instruccions per confirmar i activar l'alta. Gracies.");

function cargarMapa(id,lat,long){
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById(id));
		map.setCenter(new GLatLng(lat, long), 15);
		
		var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
		
		var point = new GLatLng(41.389978160807075,2.1619462966918945);
		map.addOverlay(new GMarker(point));
		
		var point = new GLatLng(41.387820977045635,2.166581153869629);
		map.addOverlay(new GMarker(point));
		
		var point = new GLatLng(40.433686247571295, -3.715803623199463);
		map.addOverlay(new GMarker(point));
		
		map.setUIToDefault();
	}
}
	
function valEmail(valor){
    re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
    if(!re.exec(valor)){
        return false;
    }else{
        return true;
    }
}

function ponerEnBlanco(cual){
	cual.style.border = "1px solid #ababa6";
	
	if (cual.id == "nombre" && (cual.value == "Nombre" || cual.value == "Nom")) cual.value = "";
	if (cual.id == "mail" && cual.value == "Email") cual.value = "";
	if (cual.id == "mailRep" && (cual.value == "Repite el Email" || cual.value == "Repeteix l'Email")) cual.value = "";
}

function ponerNormal(cual,i){
	if (i == "es") idioma = 0;
	else idioma = 1;
	
	if (cual.id == "nombre" && cual.value == "") cual.value = valoresCampos[idioma][0];
	if (cual.id == "mail" && cual.value == "") cual.value = "Email";
	if (cual.id == "mailRep" && cual.value == "") cual.value = valoresCampos[idioma][1];
}

function enviarSuscripcion(i){
	if (i == "es") idioma = 0;
	else idioma = 1;
	
	var validado = true;
	var idiomaSus = "";
	
	document.getElementById('error').innerHTML = "";
	document.getElementById('resultado').innerHTML = "";
	
	if (document.getElementById('nombre').value == "" || document.getElementById('nombre').value == "Nombre" || document.getElementById('nombre').value == "Nom"){
		document.getElementById('error').innerHTML = textosError[idioma][0];
		return;
	}
	
	if (document.getElementById('mail').value == "" || document.getElementById('mail').value == "Email"){
		document.getElementById('error').innerHTML = textosError[idioma][1];
		return;
	}else if (!valEmail(document.getElementById('mail').value)){
		document.getElementById('error').innerHTML = textosError[idioma][2];
		return
	}
	
	if (document.getElementById('mail').value != document.getElementById('mailRep').value){
		document.getElementById('error').innerHTML = textosError[idioma][3];
		return;
	}
	
	if (document.getElementById('provincia').value == 0){
		document.getElementById('error').innerHTML = textosError[idioma][4];
		return;
	}
	
	if (!document.getElementById('castellano').checked && !document.getElementById('catala').checked){
		document.getElementById('error').innerHTML = textosError[idioma][8];
		return;
	}else{
		if (document.getElementById('castellano').checked) idiomaSus = "es";
		if (document.getElementById('catala').checked) idiomaSus = "cat";
	}
	
	if (document.getElementById('politica').checked == false){
		document.getElementById('error').innerHTML = textosError[idioma][5];
		return;
	}
	
	var suscripcion = nuevoAjax();
	suscripcion.open("GET", "ajax/suscripcion.php?idioma=" + idioma + "&nombre=" + document.getElementById('nombre').value + "&mail=" + document.getElementById('mail').value + "&provincia=" + document.getElementById('provincia').value + "&idiomaSus=" + idiomaSus, true);
	
	suscripcion.onreadystatechange = function(){
		if (suscripcion.readyState == 4) {
			if (suscripcion.responseText == "KO") document.getElementById('error').innerHTML = textoErrorSuscripcion[idioma];
			else{
				document.getElementById('resultado').innerHTML = textoOKSuscripcion[idioma];
				document.suscripcion.reset();
			} 
		}
	}
	
	suscripcion.send(null);
}

function enviarContacto(i){
	document.getElementById('resultado').innerHTML = "";
	if (i == "es") idioma = 0;
	else idioma = 1;
	
	if (document.getElementById('nombre').value == ""){
		document.getElementById('resultado').innerHTML = textosError[idioma][0];
		return;
	}
	
	if (document.getElementById('direccion').value == ""){
		document.getElementById('resultado').innerHTML = textosError[idioma][9];
		return;
	}
	
	if (document.getElementById('cp').value == ""){
		document.getElementById('resultado').innerHTML = textosError[idioma][10];
		return;
	}
	
	if (document.getElementById('mail').value == ""){
		document.getElementById('resultado').innerHTML = textosError[idioma][1];
		return;
	}else if (!valEmail(document.getElementById('mail').value)){
		document.getElementById('resultado').innerHTML = textosError[idioma][2];
		return
	}
	if (document.getElementById('comentarios').value == ""){
		document.getElementById('resultado').innerHTML = textosError[idioma][6];
		return;
	}
	
	var contacto = nuevoAjax();
	var url = "ajax/envioContacto.php?idiomaSus=" + i + "&idioma=" + idioma + "&nombre=" + document.getElementById('nombre').value + "&mail=" + document.getElementById('mail').value + "&direccion=" + document.getElementById('direccion').value + "&cp=" + document.getElementById('cp').value + "&poblacion=" + document.getElementById('poblacion').value + "&telefono=" + document.getElementById('telefono').value + "&comentarios=" + document.getElementById('comentarios').value + "&destino=" + document.getElementById('destinatario').value + "&provincia=" + document.getElementById('provincia').value
	
	if (document.contacto.suscripcion.checked) url += "&suscripcion=1";
	
	contacto.open("GET", url, true);
	
	contacto.onreadystatechange = function(){
		if (contacto.readyState == 4) {
			document.getElementById('resultado').innerHTML = "<font color='#0ea70e;'>" + textoContacto[idioma] + "</font>";
			document.contacto.reset();
		}
	}
	
	contacto.send(null);
} 

function overImagenGaleria(cual){
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		document.getElementById('foto_' + cual).parentNode.parentNode.style.filter = "alpha(opacity=100)";
	}else {
		document.getElementById('foto_' + cual).parentNode.parentNode.style.opacity = 1;	
	}
}

function outImagenGaleria(cual){
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		document.getElementById('foto_' + cual).parentNode.parentNode.style.filter = "alpha(opacity=45)";
	}else {
		document.getElementById('foto_' + cual).parentNode.parentNode.style.opacity = 0.45;	
	}
}

function enviarFoto(i){
	document.getElementById('resultado').innerHTML = "";
	if (i == "es") idioma = 0;
	else idioma = 1;
	
	if (document.getElementById('nombre').value == ""){
		document.getElementById('resultado').innerHTML = textosError[idioma][0];
		return;
	}
	if (document.getElementById('mail').value == ""){
		document.getElementById('resultado').innerHTML = textosError[idioma][1];
		return;
	}else if (!valEmail(document.getElementById('mail').value)){
		document.getElementById('resultado').innerHTML = textosError[idioma][2];
		return
	}
	if (document.getElementById('lugar').value == ""){
		document.getElementById('resultado').innerHTML = textosError[idioma][11];
		return;
	}
	if (document.getElementById('comentarios').value == ""){
		document.getElementById('resultado').innerHTML = textosError[idioma][6];
		return;
	}
	
	if (document.getElementById('fichero').value == ""){
		document.getElementById('resultado').innerHTML = textosError[idioma][7];
		return;
	}
	
	if (document.getElementById('politica').checked == false){
		document.getElementById('resultado').innerHTML = textosError[idioma][5];
		return;
	}

	document.galeria.action = "ui/envioFoto.php";
	document.galeria.submit();	
}

function verPolitica(idioma){
	window.open("include/politica.php?idioma=" + idioma,"politica","width=400,height=500,menubars=0,scrollBars=yes");
}

function verResultadoActivacion(){
	var w,h;
	
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}else{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	
	document.getElementById('resultadoActivacion').style.visibility = 'visible';
	document.getElementById('resultadoActivacion').style.top = h/2 - document.getElementById('resultadoActivacion').style.height/2 + "px";
	document.getElementById('resultadoActivacion').style.left = w/2 - 400/2 + "px";
}

function cerrarActivacion(){
	document.getElementById('resultadoActivacion').style.visibility = 'hidden';
}

function abrirMapa(cual){
	window.open("mapaViaje.php?cual="+cual,"mapa","width=650,height=550,scrollBars=yes");
}

function sumarClick(url,cual){
	var suscripcion = nuevoAjax();
	suscripcion.open("GET", "ajax/sumarClick.php?cual="+cual, true);
	
	suscripcion.onreadystatechange = function(){
		if (suscripcion.readyState == 4) {
			window.open(url,"_blank");
		}
	}
	
	suscripcion.send(null);
}
