var inDiv = 0;
var idProducto = 0;
var letra = "";

function updateNavigator()
{
	var ajax;
	ajax = nuevoAjax(ajax);
	ajax.open("GET", "UpdateNavigator.php?navigator="+navigator.appName+"&pwidth="+screen.width+"&pheight="+screen.height, true);
	ajax.onreadystatechange=function() {
	if (ajax.readyState==4) {
		 //alert(navigator.appName);
	}
	}
	ajax.send(null);
}

function loadDirectProduct(idProd)
{	
	if ((idProd) && (idProd != ''))
	{
		callProductAJAX(idProd);
	}
}

function liOver(objLI){
	objLI.style.color="black";
}

function liOut(objLI){
	objLI.style.color="grey";
}

function updatePointer(objButton){
	if (navigator.appName=="Netscape") { 
			objButton.style.cursor='pointer'; 
	} else { 
			objButton.style.cursor='hand'; 
	} 
}

function disablePointer(objButton){
	objButton.style.cursor='default'; 
}

function enableShadow(obj) 
{ 	
	obj.style.backgroundColor= "#B40404"; 
	updatePointer(obj);
} 

function showProductsDiv(obj)
{
	var pos = getButtonPosition(obj);
	var pos2 = getButtonPosition(document.getElementById("cabecera"));
	if (navigator.appName=="Netscape") { 
		document.getElementById("div2").style.top = parseInt(pos.split("#")[0], 10) + 102 + "px";
		document.getElementById("div2").style.left = (parseInt(pos.split("#")[1], 10) + parseInt(pos2.split("#")[1], 10)) + 35+ "px";
	} else if (navigator.appName=="Opera") { 
		document.getElementById("div2").style.top = parseInt(pos.split("#")[0], 10) + 102;
		document.getElementById("div2").style.left = (parseInt(pos.split("#")[1], 10) + parseInt(pos2.split("#")[1], 10)) + 35;
	} else { 
		document.getElementById("div2").style.top = parseInt(pos.split("#")[0], 10) + 102;
		document.getElementById("div2").style.left = (parseInt(pos.split("#")[1], 10) + parseInt(pos2.split("#")[1], 10)) + 18;
	} 
	document.getElementById("div2").style.visibility = 'visible';
}

function disableShadow(obj) {

	obj.style.backgroundColor= "transparent"; 
	obj.style.cursor='default'; 
}

function hideProductsDiv() {
	if (inDiv == 0)
	{
		document.getElementById("div2").style.visibility = 'hidden';
	}
}

function getButtonPosition(obj)
{
	var top = parseInt(obj.offsetTop);
	var left = parseInt(obj.offsetLeft); 
	return top + "#" + left;
}

function updateDivStart(divObj)
{
	inDiv = 1;
	if (divObj.id == "div2") { var tdObj = document.getElementById("td2"); }
	if (divObj.style.visibility == 'hidden')
	{
		var pos = getButtonPosition(tdObj);
		var pos2 = getButtonPosition(document.getElementById("cabecera"));
		divObj.style.visibility = 'visible';
		
		if (navigator.appName=="Netscape") { 
				divObj.style.top = parseInt(pos.split("#")[0], 10) + 102 + "px";
				divObj.style.left = (parseInt(pos.split("#")[1], 10) + parseInt(pos2.split("#")[1], 10)) + 35+ "px";
		} else if (navigator.appName=="Opera") { 
				divObj.style.top = parseInt(pos.split("#")[0], 10) + 102;
				divObj.style.left = (parseInt(pos.split("#")[1], 10) + parseInt(pos2.split("#")[1], 10)) + 35;
		} else { 
				divObj.style.top = parseInt(pos.split("#")[0], 10) + 102;
				divObj.style.left = (parseInt(pos.split("#")[1], 10) + parseInt(pos2.split("#")[1], 10)) + 18;
		} 
	}
}
function updateDivEnd(divObj)
{
	inDiv = 0;
	divObj.style.visibility = 'hidden';
}


function callProduct(obj)
{
	idProducto = obj.id;
	cargarContenido("productos2.php","divG");
}

function nuevoAjax(xmlhttp){

   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 cargarContenido(pagina,destino){
   var contenedor;
   var ajax;
   
   contenedor = document.getElementById(destino);
   ajax = nuevoAjax(ajax);
   ajax.open("GET", pagina, true);
   ajax.onreadystatechange=function() {
      if (ajax.readyState==4) {
		 //var sinc = reSizeDiv(idProducto);
         contenedor.innerHTML = ajax.responseText;
		 expandProducto(idProducto);
      }
   }
   ajax.send(null);
}

function callProductAJAX(idProductoDiv)
{
	idProducto = idProductoDiv;
	if ((idProductoDiv == 9) || (idProductoDiv == 10)){
		idProductoDiv = 8;
	}	
	if ((idProductoDiv == 12) || (idProductoDiv == 13)){
		idProductoDiv = 11;
	}
	if (idProductoDiv == 15){ idProductoDiv = 14; }
	if ((idProductoDiv == 23) || (idProductoDiv == 24)){
		idProductoDiv = 22;
	}
	
	// Segunda pagina de PROTEINAS LIQUIDAS
	if (idProductoDiv == "58_2") { idProductoDiv = 100; }
	
	if (idProductoDiv == 50) { idProductoDiv = 26; }
	if  ((idProductoDiv == 45) || (idProductoDiv == 46)){
		idProductoDiv = 44;
	}
	
	// Casos especiales de Bebidas y Barritas
	if (idProductoDiv == 72){ idProductoDiv = 88; }
	if (idProductoDiv == 73){ idProductoDiv = 43; }
	if (idProductoDiv == 74){ idProductoDiv = 54; }
	if (idProductoDiv == 75){ idProductoDiv = 67; }
	if (idProductoDiv == 76){ idProductoDiv = 68; }
	if (idProductoDiv == 77){ idProductoDiv = 69; }
	if (idProductoDiv == 81){ idProductoDiv = 19; }
	if (idProductoDiv == 82){ idProductoDiv = 51; }
	if (idProductoDiv == 83){ idProductoDiv = 66; }
	if (idProductoDiv == 84){ idProductoDiv = 55; }
	if (idProductoDiv == 87){ idProductoDiv = 52; }
	// Fin casos especiales
	/*
	$.ajax({
		type: 'get',
		dataType: 'html',
		url: 'loadProducts.php',
		data: {codProducto: idProductoDiv
			  },
		success: function(htmlPagina){
			if ((htmlPagina) && (htmlPagina != ""))
			{
				document.getElementById("div2").style.visibility = 'hidden';
			}
			document.getElementById("divG").innerHTML = htmlPagina;
			cargarContenido("menuProd.php","menuProd");
			//expandProducto(idProductoDiv);
		}
	});	
	*/

	var ajax;
	ajax = nuevoAjax(ajax);
	ajax.open("GET", "LoadProducts.php?codProducto="+idProductoDiv, true);
	ajax.onreadystatechange=function() {
	if (ajax.readyState==4) {
	 document.getElementById("divG").innerHTML = ajax.responseText;
	 cargarContenido("menuProd.php","menuProd");
	 updateDivEnd(document.getElementById("div2"));
	}
	}
	ajax.send(null);
	
}

function loadPage(td)
{
	var idPagina = 0;
	if (td.id == "td1")
	{
		idPagina = 5;
	}
	else if (td.id == "td3")
	{
		idPagina = 4;
	}
	else if (td.id == "td4")
	{
		idPagina = 1;
	}
	else if(td.id == "td5")
	{
		idPagina = 2;
	}
	else if(td.id == "td6")
	{
		idPagina = 3;
	}
	$.ajax({
		type: 'get',
		dataType: 'html',
		url: 'loadPage.php',
		data: {codPagina: idPagina
			  },
		success: function(htmlPagina){
			document.getElementById("divG").innerHTML = htmlPagina;
		}
	});	
	
}

function loadMainPage(){
	window.location = "http://www.nutrisport.es";
}

// GESTION DESCARGA PDF
function descargarPDFs(idPDF){
	if (idPDF != "")
	{
		window.open("DescargaPDF.php?f="+idPDF,'','');
	}
}

function loadDietasPage(){
	url = "wwwDietas/NutriSport.php";
	/*params  = 'width='+screen.width;
	 params += ', height='+screen.height;
	 params += ', top=0, left=0'
	 params += ', fullscreen=no'
	 params += ', resizable=no';
	
	//params = 'width='+screen.width+',height='+screen.height;*/
	alert('Acceso libre por tiempo limitado');
	var params = "";
	window.open(url, '', params  );
}

function clearForm(){
	document.getElementById('txtNombre').value = '';
	document.getElementById('txtEmpresa').value = '';
	document.getElementById('txtEmail').value = '';
	document.getElementById('txtAsunto').value = '';
	
}

function sendQuery(){
	var strNombre = document.getElementById('txtNombre').value;
	var strEmpresa = document.getElementById('txtEmpresa').value;
	var strEmail = document.getElementById('txtEmail').value;
	var strAsunto = document.getElementById('txtAsunto').value;

	$.ajax({
		type: 'get',
		dataType: 'script',
		url: 'SendMail.php',
		data: { nombre: strNombre,
				empresa: strEmpresa,
				mail: strEmail,
				asunto: strAsunto
		
			  },
		success: function(valor){
			if (valor == 1){
			alert('Gracias '+strNombre+'\nLos datos han sido enviados satisfactoriamente');
			}
			else
			{
			alert('Lamentamos comunicarle que no se ha podido enviar su petición\nRogamos vuelve a intentarlo más tarde');
			}
		}
	});	
}

/***********   METODOS PARA NUEVO MENU, EN PRUEBAS  ************/
function showProductsDiv2(obj)
{
	var pos = getButtonPosition(obj);
	var pos2 = getButtonPosition(document.getElementById("cabecera"));
	var div2 = document.getElementById("div2");
	if (navigator.appName=="Netscape") { 
		div2.style.top = parseInt(pos.split("#")[0], 10) + 177 + "px";
		div2.style.left = (parseInt(pos.split("#")[1], 10) + parseInt(pos2.split("#")[1], 10)) + "px";
	} else if (navigator.appName=="Opera") { 
		div2.style.top = parseInt(pos.split("#")[0], 10) + 177;
		div2.style.left = (parseInt(pos.split("#")[1], 10) + parseInt(pos2.split("#")[1], 10));
	} else { 
		div2.style.top = parseInt(pos.split("#")[0], 10) + 177;
		div2.style.left = (parseInt(pos.split("#")[1], 10) + parseInt(pos2.split("#")[1], 10));
	} 
	if (div2.style.visibility == 'visible'){
		document.getElementById("div2").style.visibility = 'hidden';
	}
	else{
		document.getElementById("div2").style.visibility = 'visible';
	}
}
function hideProductsDiv() {
	if (inDiv == 0)	{
		document.getElementById("div2").style.visibility = 'hidden';
	}
}
function updateDivStart2(divObj)
{
	inDiv = 1;
	if (divObj.id == "div2") { var tdObj = document.getElementById("td2"); }
	if (divObj.style.visibility == 'hidden')
	{
		var pos = getButtonPosition(tdObj);
		var pos2 = getButtonPosition(document.getElementById("cabecera"));
		divObj.style.visibility = 'visible';
		
		if (navigator.appName=="Netscape") { 
				document.getElementById("div2").style.top = parseInt(pos.split("#")[0], 10) + 177 + "px";
				document.getElementById("div2").style.left = (parseInt(pos.split("#")[1], 10) + parseInt(pos2.split("#")[1], 10)) + "px";
		} else if (navigator.appName=="Opera") { 
				divObj.style.top = parseInt(pos.split("#")[0], 10) + 177;
				divObj.style.left = (parseInt(pos.split("#")[1], 10) + parseInt(pos2.split("#")[1], 10));
		} else { 
				divObj.style.top = parseInt(pos.split("#")[0], 10) + 177;
				divObj.style.left = (parseInt(pos.split("#")[1], 10) + parseInt(pos2.split("#")[1], 10));
		} 
	}
}
/*****************/


/***********   METODOS PARA DICCIONARIO ************/

function updateCursorDic(obj,action)
{
	if (action == 1)
	{
		if (navigator.appName=="Netscape") { 
				obj.style.cursor='pointer'; 
		} else { 
				obj.style.cursor='hand'; 
		} 
		if (obj != letra){
			obj.style.fontSize = '20px';
			obj.style.color='red'; 
			}
	}
	else if (action = 2)
	{
		obj.style.cursor='default'; 
		if (obj != letra){
			obj.style.fontSize = '16px';
			obj.style.color='black'; 
			}
	}
}

function selectLetter(obj)
{
	var newLetra = new Object();
	newLetra = letra;
	
	if (newLetra != '')
	{
		newLetra.style.fontSize = '16px';
		newLetra.style.color = 'black';
		newLetra.style.backgroundColor = '#D8D8D8';
	}
	
	// Actualizamos variable global letra
	letra = obj;

	obj.style.fontSize='20px'; 
	obj.style.color='white';
	obj.style.backgroundColor = '#FF0000';
	
	// Forzamos el marco de la celda	
	obj.style.borderColor="red";
	obj.style.borderWidth="medium";
	
	var ajax;
	ajax = nuevoAjax(ajax);
	ajax.open("GET", "loadLetra.php?letra="+letra.innerHTML, true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			 document.getElementById("divLetra").innerHTML = ajax.responseText;
		}
	}
	ajax.send(null);
}

/*****************/
//http://www.cssplay.co.uk/menus/ 

function loadPrevMain(idMain)
{
	cargarContenido("PrevMain/Main"+idMain+".php","contenido");
}
