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 cerrarAviso(){
	document.getElementById('fondoAviso').style.height= "10px";
	document.getElementById('fondoAviso').style.display = "none";
	document.getElementById('aviso').style.width = "10px";
	document.getElementById('aviso').style.height = "10px";
	document.getElementById('aviso').innerHTML = "";
	document.getElementById('aviso').style.display = "none";
	document.getElementById('aviso').style.top = "0px";
	document.getElementById('aviso').style.left = "0px";
}


function abrirPolitica(idioma){
	ancho = 617;
	alto = 400;
	
	document.getElementById('fondoAviso').style.height = calcularAlturaPantalla() + "px";
	document.getElementById('fondoAviso').style.display = "block";
	
	document.getElementById('aviso').style.width = ancho + "px";
	document.getElementById('aviso').style.height = alto + "px";
	document.getElementById('aviso').style.top = calcularposScroll() + calcularAltoPantalla()/2 - alto/2 + "px";
	document.getElementById('aviso').style.left = calcularAnchoPantalla()/2 - ancho/2 + "px";
	
	var ap=nuevoAjax();
	ap.open("GET", "popUpPolitica.php?idioma="+idioma, true);
	ap.onreadystatechange=function(){ 
		if (ap.readyState==4){ 
			document.getElementById('aviso').innerHTML = ap.responseText;
			document.getElementById('aviso').style.display = "block";
		} 
	}
	ap.send(null);
}

function validarMail(valor){
	ingreso = new String()
	ingreso = valor
	
	if (!ingreso.match("@") || ingreso.indexOf(".") == -1){ 
		return false;
	}
	
	return true
} 

function preloadImages(img){
	image = new Image();
	image.src = img;
}

function cargarBotonesMenu(idioma){
	preloadImages("img/" + idioma + "/menu/01estudi_a.gif");
	preloadImages("img/" + idioma + "/menu/01estudi.gif");
	preloadImages("img/" + idioma + "/menu/02serveis_a.gif");
	preloadImages("img/" + idioma + "/menu/02serveis.gif");
	preloadImages("img/" + idioma + "/menu/03productes_a.gif");
	preloadImages("img/" + idioma + "/menu/03productes.gif");
	preloadImages("img/" + idioma + "/menu/04contactar_a.gif");
	preloadImages("img/" + idioma + "/menu/04contactar.gif");
	preloadImages("img/" + idioma + "/menu/05pressupost_a.gif");
	preloadImages("img/" + idioma + "/menu/05pressupost.gif");
}

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 sendToDesactivar(){
	document.menuProyectos.sendToDesactivar();
}

function cargarApartado(cual,idioma){
	sendToDesactivar();
	cambiarMenu(cual,idioma);
	
	var ap=nuevoAjax();
	ap.open("GET", "ajax/" + cual + ".php?idioma=" + idioma, true);
	ap.onreadystatechange=function(){ 
		if (ap.readyState==4){ 
			document.getElementById('contenido').innerHTML = ap.responseText;
		} 
	}
	ap.send(null);
	
	document.menuProyectos.width = "290px";
}

function cambiarMenu(cual,idioma){
	var ap=nuevoAjax();
	ap.open("GET", "include/menu.php?opcion=" + cual + "&idioma="+idioma, true);
	ap.onreadystatechange=function(){ 
		if (ap.readyState==4){ 
			document.getElementById('menu').innerHTML = ap.responseText;
		} 
	}
	ap.send(null);
}

function cambioServicios(cual,idioma){
	cambioMenuServicios(cual,idioma);
	
	var ap=nuevoAjax();
	ap.open("GET", "ajax/servicios/" + cual +".php?idioma="+idioma, true);
	ap.onreadystatechange=function(){ 
		if (ap.readyState==4){ 
			document.getElementById('intro').innerHTML = ap.responseText;
		} 
	}
	ap.send(null);
}

function cambioMenuServicios(cual,idioma){
	var ap=nuevoAjax();
	ap.open("GET", "ajax/servicios/menu.php?opcion=" + cual + "&idioma="+idioma, true);
	ap.onreadystatechange=function(){ 
		if (ap.readyState==4){ 
			document.getElementById('opciones').innerHTML = ap.responseText;
		} 
	}
	ap.send(null);
}

function cambioProductos(cual,idioma){
	cambioMenuProductos(cual,idioma);
	
	var ap=nuevoAjax();
	ap.open("GET", "ajax/productos/" + cual +".php?idioma="+idioma, true);
	ap.onreadystatechange=function(){ 
		if (ap.readyState==4){ 
			document.getElementById('intro').innerHTML = ap.responseText;
		} 
	}
	ap.send(null);
}

function cambioMenuProductos(cual,idioma){
	var ap=nuevoAjax();
	ap.open("GET", "ajax/productos/menu.php?opcion=" + cual + "&idioma="+idioma, true);
	ap.onreadystatechange=function(){ 
		if (ap.readyState==4){ 
			document.getElementById('opciones').innerHTML = ap.responseText;
		} 
	}
	ap.send(null);
}

function cargarProyectos(cual,idioma){
	cambiarMenu("",idioma)
	document.getElementById('contenido').innerHTML = "";
	
	document.menuProyectos.width = "1209px";
}

function cargarDestacado(cual,idioma){
	document.menuProyectos.sendToProyectoDestacado(cual,idioma);
}

function ponerEnVerde(cual,id){
	if (cual.value != "") document.getElementById(id).style.backgroundColor = "#73ff74";
	else document.getElementById(id).style.backgroundColor = "#434343";
}

var politica = false;

function marcarPolitica(){
	if (!politica){
		document.getElementById('botonPolitica').src = "img/es/formularis/vist_ok.gif";
		politica = true;
	}else{
		document.getElementById('botonPolitica').src = "img/es/formularis/vist.gif";
		politica = false;
	}
}

function ponerNormal(){
	if (document.contacto.nombre.value != "") document.getElementById('obligatorioNombre').style.backgroundColor = "#73ff74";
	if (document.contacto.telefono.value != "") document.getElementById('obligatorioTelefono').style.backgroundColor = "#73ff74";
	if (document.contacto.mail.value != "") document.getElementById('obligatorioMail').style.backgroundColor = "#73ff74";
	if (document.contacto.mailConfirmar != null && document.contacto.mailConfirmar.value != "") document.getElementById('obligatorioMailConfirmar').style.backgroundColor = "#73ff74";
	if (document.contacto.consulta.value != "") document.getElementById('obligatorioConsulta').style.backgroundColor = "#73ff74";
}

function enviarContacto(idioma){
	var validado = true;
	
	if (document.contacto.nombre.value == ""){
		validado = false;
		document.getElementById('obligatorioNombre').style.backgroundColor = "#fd605b";
	}
	if (document.contacto.telefono.value == ""){
		validado = false;
		document.getElementById('obligatorioTelefono').style.backgroundColor = "#fd605b";
	}
	if (document.contacto.mail.value == ""){
		validado = false;
		document.getElementById('obligatorioMail').style.backgroundColor = "#fd605b";
	}else if (!validarMail(document.contacto.mail.value)){
		validado = false;
		document.getElementById('obligatorioMail').style.backgroundColor = "#fd605b";
	}
	if (document.contacto.consulta.value == ""){
		validado = false;
		document.getElementById('obligatorioConsulta').style.backgroundColor = "#fd605b";
	}
	
	if (!politica){
		validado = false;
		document.getElementById('botonPolitica').src = "img/es/formularis/vist_ko.gif";
	}
	
	if (validado){
		var ap=nuevoAjax();
		ap.open("POST", "ajax/contacto/envioContacto.php", true);
		ap.onreadystatechange=function(){ 
			if (ap.readyState==4){ 
				if (idioma == "es") document.getElementById('resultado').innerHTML = "<span style='color:#73ff74'>Mensaje enviado correctamente.</span>";
				else document.getElementById('resultado').innerHTML = "<span style='color:#73ff74'>Missatge enviat correctament.</span>";
				document.contacto.reset();
			} 
		}
		
		var variables = "";
		variables += "nombre="+document.contacto.nombre.value;
		variables += "&empresa="+document.contacto.empresa.value;
		variables += "&telefono="+document.contacto.telefono.value;
		variables += "&mail="+document.contacto.mail.value;
		variables += "&texto="+document.contacto.consulta.value;
		
		ap.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ap.send(variables);
	}
}

function enviarPresupuesto(){
	var validado = true;
	
	if (document.contacto.nombre.value == ""){
		validado = false;
		document.getElementById('obligatorioNombre').style.backgroundColor = "#fd605b";
	}
	if (document.contacto.mail.value == ""){
		validado = false;
		document.getElementById('obligatorioMail').style.backgroundColor = "#fd605b";
	}else if (!validarMail(document.contacto.mail.value)){
		validado = false;
		document.getElementById('obligatorioMail').style.backgroundColor = "#fd605b";
	}
	if (document.contacto.mail.value != document.contacto.mailConfirmar.value) {
		validado = false;
		document.getElementById('obligatorioMailConfirmar').style.backgroundColor = "#fd605b";
	}
	if (document.contacto.consulta.value == ""){
		validado = false;
		document.getElementById('obligatorioConsulta').style.backgroundColor = "#fd605b";
	}
	
	if (!politica){
		validado = false;
		document.getElementById('botonPolitica').src = "img/es/formularis/vist_ko.gif";
	}
	
	if (validado){
		document.contacto.action = "ajax/presupuesto/envioPresupuesto.php";
		document.contacto.submit();
	}
}

