function nuevoAjax(){ 
	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(); }

	return xmlhttp;
}

function preloadImage(cual){
	var image = new Image();
	image.src = cual;
}

function calcularAlturaPantalla(){
	if (document.getElementById('container').offsetHeight <= calcularAltoPantalla()) return calcularAltoPantalla();
	else return document.getElementById('container').offsetHeight;
}

function calcularAltoPantalla(){
	if (self.innerHeight) {
		altoPantalla = parseInt(self.innerHeight);
	}else if (document.documentElement && document.documentElement.clientHeight) {
		altoPantalla = parseInt(document.documentElement.clientHeight);
	}else if (document.body.clientHeight) {
		altoPantalla = parseInt(document.body.clientHeight);
	}
	
	return altoPantalla;
}

function calcularAnchoPantalla(){
	 if (self.innerWidth) {
		anchoPantalla = parseInt(self.innerWidth);
	}else if (document.documentElement && document.documentElement.clientWidth) {
		anchoPantalla = parseInt(document.documentElement.clientWidth);
	}else if (document.body.clientWidth) {
		anchoPantalla = parseInt(document.body.clientWidth);
	}
	
	if (navigator.appVersion.indexOf("MSIE 8") != -1) anchoPantalla = anchoPantalla + 20
	
	return anchoPantalla;
}

function calcularposScroll(){
	if (navigator.appVersion.indexOf("MSIE") != -1) return document.body.parentNode.scrollTop;
	else return window.scrollY;
}

function calcularposScrollX(){
	if (navigator.appVersion.indexOf("MSIE") != -1) return document.body.parentNode.scrollLeft;
	else return window.scrollX;
}

function cerrarAviso(){
	document.getElementById('fondoAviso').style.height = "10px";
	document.getElementById('fondoAviso').style.visibility = "hidden";
	document.getElementById('aviso').innerHTML = "";
	document.getElementById('aviso').style.top = "0px";
}

function overMas(cual){
	document.getElementById(cual).src = "img/botones/bot_mas_o.png";
}

function outMas(cual){
	document.getElementById(cual).src = "img/botones/bot_mas_n.png";
}

function cargarNoticia(idNoticia,idioma){
	cargarBotoneraNoticia(idNoticia,idioma);
	cargarContenidoNoticia(idNoticia,idioma);
}

function cargarBotoneraNoticia(idNoticia,idioma){
	var ap=nuevoAjax();
	ap.open("POST", "ajax/home/botoneraNoticias.php?idNoticia="+idNoticia+"&idioma="+idioma, true);
	ap.onreadystatechange=function(){
		if (ap.readyState==4){
			document.getElementById('botoneraNoticias').innerHTML = ap.responseText;
		} 
	}
	
	ap.send(null);
}

function cargarContenidoNoticia(idNoticia,idioma){
	var ap=nuevoAjax();
	ap.open("POST", "ajax/home/noticia.php?idNoticia="+idNoticia+"&idioma="+idioma, true);
	ap.onreadystatechange=function(){
		if (ap.readyState==4){
			document.getElementById('noticia').innerHTML = ap.responseText;
		} 
	}
	
	ap.send(null);
}

function ponerLabelNormal(cual,color){
	document.getElementById(cual).style.color = color;
}

var textoOk = new Array("Mensaje enviado correctamente.<br>En breve nos pondremos en contacto con usted. Gracias.","Email correctly sent.<br>We will contact you soon. Thank you.","Message envoyé correctement.<br>Nous vous contacterons dès que possible. Merci.","Mitteilung wurde korrekt versendet.<br>Wir werden Sie so schnell wie möglich kontaktieren, Vielen Dank.");

function enviarContacto(idioma){
	var idm = 0;
	if (idioma == "es") idm = 0;
	if (idioma == "en") idm = 1;
	if (idioma == "fr") idm = 2;
	if (idioma == "de") idm = 3;
	
	var validado = true;
	
	if (document.contacto.empresa.value == ""){
		document.getElementById('labelEmpresa').style.color = "#F00";
		validado = false;
	}
	if (document.contacto.nombre.value == ""){
		document.getElementById('labelNombre').style.color = "#F00";
		validado = false;
	}
	if (document.contacto.telefono.value == ""){
		document.getElementById('labelTelefono').style.color = "#F00";
		validado = false;
	}
	if (document.contacto.mail.value == ""){
		document.getElementById('labelMail').style.color = "#F00";
		validado = false;
	}
	if (document.contacto.comentario.value == ""){
		document.getElementById('labelComentarios').style.color = "#F00";
		validado = false;
	}
	
	if (!document.contacto.politica.checked){
		document.getElementById('politica').style.color = "#F00";
		validado = false;
	}
	
	if (validado) {
		var ap = nuevoAjax();
		ap.open("POST", "ajax/contacto/envioContacto.php", true);
		ap.onreadystatechange = function(){
			if (ap.readyState == 4) {
				document.contacto.reset();
				document.getElementById('resultado').innerHTML = "<span style='color:#436b33;'>" + textoOk[idm] + "</span>";
			}
		}
		
		var variables = "";
		variables += "empresa=" + document.contacto.empresa.value;
		variables += "&nombre=" + document.contacto.nombre.value;
		variables += "&telefono=" + document.contacto.telefono.value;
		variables += "&mail=" + document.contacto.mail.value;
		variables += "&comentario=" + document.contacto.comentario.value;
		
		ap.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ap.send(variables);
	}
}

function abrirPoliticaPrivacidad(idioma){
	window.open("popUpPolitica.php?idioma="+idioma,"politica","width=600,height=500,menubars=0,scrollbars=yes")
}

function pedirUsuario(idioma){
	document.getElementById('fondoAviso').style.height = calcularAlturaPantalla() + 40 + "px";
	document.getElementById('fondoAviso').style.visibility = "visible";
	
	document.getElementById('aviso').style.left = calcularAnchoPantalla()/2 - 435/2 + "px";
	document.getElementById('aviso').style.top = calcularposScroll() + calcularAltoPantalla()/2 - 187/2 + "px";
	document.getElementById('aviso').style.visibility = "visible";
	
	var ap=nuevoAjax();
	ap.open("GET", "ajax/descargas/pedidrUsuario.php?idioma="+idioma, true);
	ap.onreadystatechange=function(){
		if (ap.readyState==4){
			document.getElementById('aviso').innerHTML = ap.responseText;
		} 
	}
	
	ap.send(null);
}

function validarUsuario(idioma){
	var validado = true;
	
	if (document.login.usuario.value == ""){
		document.login.usuario.style.border = "1px solid #F00";
		validado = false;
	}
	
	if (document.login.contrasenya.value == ""){
		document.login.contrasenya.style.border = "1px solid #F00";
		validado = false;
	}
	
	if (validado) {
		var ap = nuevoAjax();
		ap.open("POST", "ajax/descargas/validarUsuario.php", true);
		ap.onreadystatechange = function(){
			if (ap.readyState == 4) {
				if (ap.responseText == "KO"){
					document.login.usuario.style.border = "1px solid #F00";
					document.login.contrasenya.style.border = "1px solid #F00";
				}else if (ap.responseText == "OK"){
					window.open("img/descargas/pdf/"+idioma+"/presto/UnionesARPOL V 1.3.bc3.zip","_blank");
					cerrarAviso();
				}
			}
		}
		
		var variables = "";
		variables += "usuario="+document.login.usuario.value;
		variables += "&contrasenya="+document.login.contrasenya.value;
		
		ap.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ap.send(variables);
	}
}

function ponerCampoUsuarioNormal(cual){
	cual.style.border = "1px solid #e2e3dd";
}

