var ajax=null;
var panel = new Array(null);

window.onresize = function()
{
	mover_flotante();
	document.getElementById("flotante_selector_ciudad_contenedor").style.top=(document.getElementById("flecha_selector").offsetTop+25)+"px";
	document.getElementById("flotante_selector_ciudad_contenedor").style.left=(document.getElementById("flecha_selector").offsetLeft+10-document.getElementById("flotante_selector_ciudad_contenedor").offsetWidth+4)+"px";
}

function mover_flotante()
{
	var alto,alto_flotante;
	
	if(document.getElementById("flotante"))
	{
		alto=getWindowData("alto");
		if(document.getElementById("flotante_alto")){alto_flotante=parseInt(document.getElementById("flotante_alto").style.height);}else{alto_flotante=486;}
		if(document.getElementById("flotante")){document.getElementById("flotante").style.marginTop=(((alto-alto_flotante)/2)+getWindowData("altoscroll"))+"px";	}
	}
	if(document.getElementById("flotante2"))
	{
		alto=getWindowData("alto");
		if(document.getElementById("flotante_alto2")){alto_flotante=parseInt(document.getElementById("flotante_alto2").style.height);}else{alto_flotante=486;}
		if(document.getElementById("flotante2")){document.getElementById("flotante2").style.marginTop=(((alto-alto_flotante)/2)+getWindowData("altoscroll"))+"px";	}
	}
}

function crear_ajax()
{	//Cargamos codigo para boton compartir de facebook
	//incluir_javascript("http://static.ak.fbcdn.net/connect.php/js/FB.Share");
	//Cargamos codigo para boton compartir de Twitter
	//incluir_javascript("http://platform.twitter.com/widgets.js");
try
{
ajax=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
//Si no, utilizar el antiguo objeto ActiveX.
try
{
ajax=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
//Si no, debemos estar utilizando un navegador que no es IE.
ajax=false;
}
}
//Si no estamos usando IE, creamos una instancia JavaScript del objeto.
if(!ajax && typeof XMLHttpRequest!="undefined")
{
ajax=new XMLHttpRequest();
}
}

//Procesar peticiones AJAX
function procesar_ajax(pagina,objeto,getorpost,datos)
{
	if (ajax==null)
	{
		objeto=document.getElementById(objeto);
		crear_ajax();
	if(getorpost=="get")
	{
		ajax.open("GET",pagina);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=ISO-8859-1");
		ajax.onreadystatechange=function(){
		if(ajax.readyState==4 && ajax.status==200)
		{
			if(objeto){objeto.innerHTML=ajax.responseText;}
			ajax=null;
		}
		}
		ajax.send(null);
	}else{
		ajax.open("POST",pagina);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=ISO-8859-1");
		ajax.onreadystatechange=function(){
		if(ajax.readyState==4 && ajax.status==200)
		{
			if(objeto){objeto.innerHTML=ajax.responseText;}
			ajax=null;
		}
		}
		ajax.send(datos);
	}
	}else{    
	setTimeout("procesar_ajax('"+pagina+"','"+objeto+"','"+getorpost+"','"+datos+"')",10);
	}
}

function obtener_datos_formulario(formulario)
{
	var str="";
	var valor="";
	
	for(var i=0;i<formulario.elements.length;i++)
	{
		if(formulario.elements[i].type=="radio")
		{
			if(formulario.elements[i].checked==true){str+=formulario.elements[i].name + "=" + escape(formulario.elements[i].value) + "&";}
		}else if(formulario.elements[i].type=="checkbox"){
			if(formulario.elements[i].checked==true){str+=formulario.elements[i].name + "=" + escape(formulario.elements[i].value) + "&";}
		}else if(formulario.elements[i].type!="button" && formulario.elements[i].type!="submit"){
		if(escape(formulario.elements[i].value)==""){valor=" ";}else{valor=escape(formulario.elements[i].value);}
		str+=formulario.elements[i].name + "=" + valor + "&";
		}
	}
	return str;
}

function getWidth(){ 
    var widthViewport,heightViewport,xScroll,yScroll,widthTotal,heightTotal; 
    if (typeof window.innerWidth != 'undefined'){ 
        widthViewport= window.innerWidth-17; 
        heightViewport= window.innerHeight-17; 
    }else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0){ 
        widthViewport=document.documentElement.clientWidth; 
        heightViewport=document.documentElement.clientHeight; 
    }else{ 
        widthViewport= document.getElementsByTagName('body')[0].clientWidth; 
        heightViewport=document.getElementsByTagName('body')[0].clientHeight; 
    } 
    xScroll=self.pageXOffset || (document.documentElement.scrollLeft+document.body.scrollLeft);
    yScroll=self.pageYOffset || (document.documentElement.scrollTop+document.body.scrollTop);
    widthTotal=Math.max(document.documentElement.scrollWidth,document.body.scrollWidth,widthViewport);
    heightTotal=Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,heightViewport);
    //return [widthViewport,heightViewport,xScroll,yScroll,widthTotal,heightTotal];
	return widthViewport;
} 

function panel_gestion(objeto,e)
{
	var i,cadena="";
	
	if(window.event)keyCode=window.event.keyCode;
	else if(e) keyCode=e.which;
	
	var caracter=String.fromCharCode(keyCode).toLowerCase();
	
	for(i=0;i<5;i++)
	{
		if(i<4 && panel[i]!=null){panel[i]=panel[i+1];}else{panel[i]=caracter;}
		cadena=cadena+String(panel[i]);
	}

	if(cadena=="admin" && !document.getElementById("flotante")){mostrar_panel();}
	if(keyCode==27){cerrar_formulario();}
}

function mostrar_panel()
{
	var altura;
	//if(document.body.scrollHeight){altura=window.document.documentElement.scrollTop;}else{altura=window.pageYOffset;}
	//alert(altura);
	altura=getWindowData("alto");

	procesar_ajax("admin.php?alto="+getWindowData("alto")+"&scroll="+getWindowData("altoscroll")+"","formularios","get");
	document.getElementById("fondo_formularios").style.height=getWindowData("altototal")+"px";
	document.getElementById("fondo_formularios").style.visibility="visible";
	document.getElementById("formularios").style.visibility="visible";
}

function getWindowData(opcion){ 
    var widthViewport,heightViewport,xScroll,yScroll,widthTotal,heightTotal; 
    if (typeof window.innerWidth != 'undefined'){ 
        widthViewport= window.innerWidth-17; 
        heightViewport= window.innerHeight-17; 
    }else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0){ 
        widthViewport=document.documentElement.clientWidth; 
        heightViewport=document.documentElement.clientHeight; 
    }else{ 
        widthViewport= document.getElementsByTagName('body')[0].clientWidth; 
        heightViewport=document.getElementsByTagName('body')[0].clientHeight; 
    } 
    xScroll=self.pageXOffset || (document.documentElement.scrollLeft+document.body.scrollLeft);
    yScroll=self.pageYOffset || (document.documentElement.scrollTop+document.body.scrollTop);
    widthTotal=Math.max(document.documentElement.scrollWidth,document.body.scrollWidth,widthViewport);
    heightTotal=Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,heightViewport);
    //return [widthViewport,heightViewport,xScroll,yScroll,widthTotal,heightTotal];
	if(opcion=="altototal"){return heightTotal;}
	if(opcion=="alto"){return heightViewport;}
	if(opcion=="altoscroll"){return yScroll;}
}

function control_panel()
{
	var control=false;
	
	if(document.getElementById("panel_menu") && document.getElementById("panel_cuerpo"))
	{
		if(parseInt(document.getElementById("panel_control").style.width)<800)
		{
			document.getElementById("panel_control").style.width=(parseInt(document.getElementById("panel_control").style.width)+10)+"px";
			document.getElementById("flotante").style.width=(parseInt(document.getElementById("flotante").style.width)+10)+"px";
			document.getElementById("flotante_alto").style.width=(parseInt(document.getElementById("flotante_alto").style.width)+10)+"px";
			if(parseInt(document.getElementById("panel_control").style.width)==800){control=true;}
		}
		if(parseInt(document.getElementById("flot_sup").style.width)<800)
		{
			document.getElementById("flot_sup").style.width=(parseInt(document.getElementById("flot_sup").style.width)+10)+"px";
		}
		if(parseInt(document.getElementById("flot_inf").style.width)<800)
		{
			document.getElementById("flot_inf").style.width=(parseInt(document.getElementById("flot_inf").style.width)+10)+"px";
		}
		if(parseInt(document.getElementById("flotante_alto").style.height)<400)
		{
			document.getElementById("flotante_alto").style.height=(parseInt(document.getElementById("flotante_alto").style.height)+10)+"px";
			document.getElementById("flotante").style.marginTop=(parseInt(document.getElementById("flotante").style.marginTop)-5)+"px";
		}	
	}
	if(control==true)
	{
		document.getElementById("panel_menu").innerHTML="<img src='cargando.gif'><span class='letra4' style='color:#fe5301'>Cargando...</span>";
		document.getElementById("panel_cuerpo").innerHTML="<img src='cargando.gif'><span class='letra4' style='color:#fe5301'>Cargando...</span>";
		procesar_ajax("admin.php?opcion=menu","panel_menu","get");
		procesar_ajax("admin.php?opcion=cuerpo","panel_cuerpo","get");
	}
	
	setTimeout(function (){control_panel();},20);
}

function cerrar_formulario()
{
	//document.getElementById("formularios").style.height=document.body.clientHeight;
	document.getElementById("formularios").innerHTML="";
	document.getElementById("formularios").style.visibility="hidden";
	document.getElementById("fondo_formularios").style.visibility="hidden";
	//document.getElementById("login").style.zIndex="10";
	//document.body.style.overflow="scroll";
	document.body.focus();
}

function iniciar_sesion()
{
	var control=false;
	var str="";

	document.getElementById("Eadmin").innerHTML="";

	if(document.admin.Auser.value=="" || document.admin.Apass.value=="")
	{
		document.getElementById("Eadmin").innerHTML="<span class='letra1' style='color:red'>Introduce usuario y contrase&ntilde;a</span>";
		control=true;
	}

	if(control==false)
	{
	str=obtener_datos_formulario(document.admin);
	procesar_ajax("admin.php","panel_control","post",str);
	}
}

function cerrar_admin()
{
	str=obtener_datos_formulario(document.cerrar);
	procesar_ajax("admin.php","panel_control","post",str);
	cerrar_formulario();
}

function gestionar_noticias(opcion)
{
	document.getElementById("panel_cuerpo").innerHTML="<img src='cargando.gif'><span class='letra4' style='color:#fe5301'>Cargando...</span>";
	if(opcion=="nada"){procesar_ajax("gestionar_noticias.php?opcion=nada","panel_cuerpo","get");}
}

function insertar_noticia()
{
	control=false;
	
	document.getElementById("Err").innerHTML="";
	
	if(document.nuevo_proveedor.Ntitulo.value==""){control=true;}
	if(document.nuevo_proveedor.Ndescripcion.value==""){control=true;}
	//if(document.nuevo_proveedor.Ndia.value==""){control=true;}
	//if(document.nuevo_proveedor.Nhorario.value==""){control=true;}
	
	if(document.nuevo_proveedor.Nimagen1.value!="")
	{
	extension=document.nuevo_proveedor.Nimagen1.value.split(".");
	extension=extension[extension.length-1].toLowerCase();
	if(extension!="jpg" && extension!="jpeg")
	{
		document.getElementById("Err").innerHTML+="La imagen debe ser jpg.";
		control=true;
	}
	}else{
		control=true;
	}
	
	if(document.nuevo_proveedor.Nimagen2.value!="")
	{
	extension=document.nuevo_proveedor.Nimagen2.value.split(".");
	extension=extension[extension.length-1].toLowerCase();
	if(extension!="jpg" && extension!="jpeg")
	{
		document.getElementById("Err").innerHTML+="La imagen debe ser jpg.";
		control=true;
	}
	}
	
	if(control==true){document.getElementById("Err").innerHTML+=" Faltan campos por rellenar.";}
	
	if(control==false)
	{
		document.nuevo_proveedor.submit();
		//str=obtener_datos_formulario(document.nuevo_proveedor);
		//procesar_ajax("gestionar_proveedores.php?insertado=correcto","zona_categorias","post",str);
		document.getElementById("bot").innerHTML="<img src='cargando.gif'><span class='letra4' style='color:#fe5301'>Guardando...</span>";
	}	
}

function noticia_insertada()
{
	document.getElementById("panel_cuerpo").innerHTML="<img src='cargando.gif'><span class='letra4' style='color:#fe5301'>Cargando...</span>";
	procesar_ajax("gestionar_noticias.php?opcion=nada&insertado=correcto","panel_cuerpo","get");
}

function noticia_modificada()
{
	document.getElementById("panel_cuerpo").innerHTML="<img src='cargando.gif'><span class='letra4' style='color:#fe5301'>Cargando...</span>";
	procesar_ajax("gestionar_noticias.php?opcion=nada&modificado=correcto","panel_cuerpo","get");
}

function eliminar_noticia(id,cat)
{
	if (!confirm('Seguro que desea eliminar la noticia "'+cat+'"?'))
	{ 
		return false;
	}else{
		procesar_ajax("gestionar_noticias.php?eliminar_provincia="+id+"","zona_categorias","get");
		subir_div("seccion_categorias");
	}
}

function subir_div(elemento)
{
		if(document.getElementById(elemento).scrollTop!=0)
		{
			document.getElementById(elemento).scrollTop=document.getElementById(elemento).scrollTop-(document.getElementById(elemento).scrollTop*0.2);
			setTimeout(function(){subir_div(elemento);},10);
		}
}

function modificar_noticia(id)
{
	subir_div("seccion_categorias");
	procesar_ajax("gestionar_noticias.php?modificar="+id+"","nueva_modificar","get");
}

function cancelar_modificar_proveedor()
{
	procesar_ajax("gestionar_noticias.php?cancelar_modificar=true","nueva_modificar","get");
}

function modificar_not()
{
	control=false;
	
	document.getElementById("Err").innerHTML="";
	
	if(document.nuevo_proveedor.Ntitulo2.value==""){control=true;}
	if(document.nuevo_proveedor.Ndescripcion.value==""){control=true;}
	//if(document.nuevo_proveedor.Ndia.value==""){control=true;}
	//if(document.nuevo_proveedor.Nhorario.value==""){control=true;}
	
	if(document.nuevo_proveedor.Nimagen1.value!="")
	{
	extension=document.nuevo_proveedor.Nimagen1.value.split(".");
	extension=extension[extension.length-1].toLowerCase();
	if(extension!="jpg" && extension!="jpeg")
	{
		document.getElementById("Err").innerHTML+="La imagen debe ser jpg.";
		control=true;
	}
	}
	
	if(document.nuevo_proveedor.Nimagen2.value!="")
	{
	extension=document.nuevo_proveedor.Nimagen2.value.split(".");
	extension=extension[extension.length-1].toLowerCase();
	if(extension!="jpg" && extension!="jpeg")
	{
		document.getElementById("Err").innerHTML+="La imagen debe ser jpg.";
		control=true;
	}
	}
	
	if(control==true){document.getElementById("Err").innerHTML+=" Faltan campos por rellenar.";}
	
	if(control==false)
	{
		document.nuevo_proveedor.submit();
		//str=obtener_datos_formulario(document.nuevo_proveedor);
		//procesar_ajax("gestionar_proveedores.php?insertado=correcto","zona_categorias","post",str);
		document.getElementById("bot").innerHTML="<img src='cargando.gif'><span class='letra4' style='color:#fe5301'>Guardando...</span>";
	}	
}
