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 moverWeb(cual,idioma){
	document.getElementById('detalles').innerHTML = "";
	document.getElementById('detalles').style.display = "none";
	
	Effect.ScrollTo(cual , { duration:'1', offset:-141 });
	
	var ap=nuevoAjax();
	ap.open("GET", "ajax/menu.php?cual="+cual+"&idioma="+idioma, true);
	ap.onreadystatechange=function(){
		if (ap.readyState==4){
			document.getElementById('opciones').innerHTML = ap.responseText;
		} 
	}
    ap.send(null);
}

function moverWebDetalle(cual){
	Effect.ScrollTo(cual , { duration:'1', offset:-141 });
	
	var ap=nuevoAjax();
	ap.open("GET", "ajax/menu.php?cual="+cual, true);
	ap.onreadystatechange=function(){
		if (ap.readyState==4){
			document.getElementById('opciones').innerHTML = ap.responseText;
		} 
	}
    ap.send(null);
}

function calcularAfegir(){
	var altoAfegir = document.documentElement.clientHeight - document.getElementById('donde').offsetHeight - 141 - 40;
	if (altoAfegir >= 0) document.getElementById('afegir').style.height = altoAfegir + "px";
}

function overMenuProductos(cual,idTexto,idIcono){
	document.getElementById(idIcono).innerHTML = "<img src='img/comun/bot_mas_o.png'>";
	document.getElementById(idTexto).style.color = "#E1000A";
}

function outMenuProductos(cual,idTexto,idIcono){
	document.getElementById(idIcono).innerHTML = "<img src='img/comun/bot_mas_n.png'>";
	document.getElementById(idTexto).style.color = "#666";
}

function overBotonDetalle(cual){
	cual.getElementsByClassName('icono')[0].innerHTML = "<img src='img/comun/bot_enviar_o.png'>";
	cual.getElementsByClassName('textoDetalle')[0].style.color = "#666";
}

function outBotonDetalle(cual){
	cual.getElementsByClassName('icono')[0].innerHTML = "<img src='img/comun/bot_enviar_n.png'>";
	cual.getElementsByClassName('textoDetalle')[0].style.color = "#E1000A";
}

function overBotonEnviar(cual,idTexto,idIcono){
	document.getElementById(idTexto).style.color = "#CCC";
	document.getElementById(idIcono).innerHTML = "<img src='img/comun/bot_enviar_o.png'>";
}

function outBotonEnviar(cual,idTexto,idIcono){
	document.getElementById(idTexto).style.color = "#FFF";
	document.getElementById(idIcono).innerHTML = "<img src='img/comun/bot_enviar_n.png'>";
}

var itv;
function verFormularioComercial(){
	clearInterval(itv);
	document.getElementById('resultado_comercial').innerHTML = "";
	document.getElementById('resultado_tecnico').innerHTML = "";
	document.getElementById('foto').style.display = "none";
	document.getElementById('formularioTecnico').style.display = "none";
	document.getElementById('formularioComercial').style.display = "block";
}

function verFormularioTecnico(){
	clearInterval(itv);
	document.getElementById('resultado_comercial').innerHTML = "";
	document.getElementById('resultado_tecnico').innerHTML = "";
	document.getElementById('foto').style.display = "none";
	document.getElementById('formularioTecnico').style.display = "block";
	document.getElementById('formularioComercial').style.display = "none";
}

function cargarServicioTecnico(idioma){
	moverWeb('contacta',idioma);
	verFormularioTecnico();
}

var textosContacto_es = new Array("Todos los campos son obligatorios.","Mensaje enviado correctamente.<br>En breve nos pondremos en contacto con usted. Gracias.");
var textosContacto_en = new Array("All field should be completed.","Message sent correctly. Thank you.");

function validarEnvioContacto(tipo,idioma){
	var tmp;
	if (idioma == "es") tmp = textosContacto_es;
	if (idioma == "en") tmp = textosContacto_en;
	
	clearInterval(itv);
	document.getElementById('resultado_comercial').innerHTML = "";
	document.getElementById('resultado_tecnico').innerHTML = "";
	var formulario;
	
	if (tipo == "comercial") formulario = document.contactoComercial;
	else formulario = document.contactoTecnico;
	
	if (formulario.nombre.value == "" || formulario.mail.value == "" || formulario.mensaje.value == ""){
		document.getElementById('resultado_'+tipo).innerHTML = tmp[0];
		return;
	}
	
	var ap = nuevoAjax();
	ap.open("POST", "ajax/envioContacto.php", true);
	ap.onreadystatechange = function(){
		if (ap.readyState == 4) {
			formulario.reset();
			document.getElementById('resultado_'+tipo).innerHTML = tmp[1];
			itv = setInterval("cerrarFormularios()",5000);
		}
	}
	
	var variables = "";
	variables += "nombre=" + formulario.nombre.value;
	variables += "&mail=" + formulario.mail.value;
	variables += "&mensaje=" + formulario.mensaje.value;
	variables += "&tipo="+tipo;
	
	ap.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ap.send(variables);
}

function cerrarFormularios(){
	clearInterval(itv);
	document.getElementById('resultado_comercial').innerHTML = "";
	document.getElementById('resultado_tecnico').innerHTML = "";
	
	document.getElementById('foto').style.display = "block";
	document.getElementById('formularioTecnico').style.display = "none";
	document.getElementById('formularioComercial').style.display = "none";
}

function abrirAviso(idioma){
	window.open("politica.php?idioma="+idioma,"aviso","width=775,height=500,menubars=0");
}

function abrirMenuProductos(){
	document.getElementById('listaServicios').style.display = "none";
	document.getElementById('listaProductos').style.display = "block";
	document.getElementById('iconoProductos').src = "img/comun/bot_down_n.png";
	document.getElementById('iconoServicios').src = "img/comun/bot_enviar_n.png";
}

function abrirMenuServicios(){
	document.getElementById('listaServicios').style.display = "block";
	document.getElementById('listaProductos').style.display = "none";
	document.getElementById('iconoServicios').src = "img/comun/bot_down_n.png";
	document.getElementById('iconoProductos').src = "img/comun/bot_enviar_n.png";
}

var listadoProductos = new Array();
var productoAbierto = "";

function addProducto(cual){
	listadoProductos.push(cual);
}

function cargarProducto(id,idioma){
	cargarMenuProductos(id,idioma);
	
	var ap=nuevoAjax();
	ap.open("GET", "include/productos/" + id + ".php?idioma="+idioma, true);
	ap.onreadystatechange=function(){
		if (ap.readyState==4){
			document.getElementById('producto').innerHTML = ap.responseText;
		}
	}
	ap.send(null);
}

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

function siguienteImagen(imagen, galeria){
	imagen++;
	
	var ap=nuevoAjax();
	ap.open("GET", "ajax/galeria.php?imagen="+imagen+"&galeria="+galeria, true);
	ap.onreadystatechange=function(){
		if (ap.readyState==4){
			document.getElementById('galeria').innerHTML = ap.responseText;
		}
	}
	ap.send(null);
}

function anteriorImagen(imagen, galeria){
	imagen--;
	
	var ap=nuevoAjax();
	ap.open("GET", "ajax/galeria.php?imagen="+imagen+"&galeria="+galeria, true);
	ap.onreadystatechange=function(){
		if (ap.readyState==4){
			document.getElementById('galeria').innerHTML = ap.responseText;
		}
	}
	ap.send(null);
}

function verOpcionSubMenu(cual){
	document.getElementById('descripcion').style.display = "none";
	document.getElementById('caracteristicas').style.display = "none";
	
	document.getElementById(cual).style.display = "block";
}

function verOpcionSubMenuModelo(){
	document.getElementById('fondoModelos').style.display = "block";
	document.getElementById('fondoModelos').style.height = document.getElementById('container').offsetHeight + "px";
	
	document.getElementById('modelos').style.display = "block";
	document.getElementById('modelos').style.left = document.getElementById('fondoModelos').offsetWidth/2 - document.getElementById('modelos').offsetWidth/2 + "px";
	document.getElementById('modelos').style.top = calcularposScroll() +  100 + "px";
}

function cerrarTableModelos(){
	document.getElementById('fondoModelos').style.display = "none";
	document.getElementById('modelos').style.display = "none";
}

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