

//Actualizamos la altura del div de carga (output)
function updatePreloadDiv(){
	height = GetImageLocation(document.myImg, 'y');	
	document.loading_gif.src = 'i/web/loading.gif';
	document.getElementById("loading").style.height = height+'px';
	//document.getElementById("output").style.left = GetImageLocation(document.getElementById("caca"), 'x')+'px';
	//alert(GetImageLocation(document.getElementById("caca"), 'x'))
	
	//alert(getY(document.getElementById("output")));
	//document.getElementById("output").style.left = document.body.clientWidth / 2 - 100+'px';
}


//Cambiamos la imagen de las estadisticas por otra
function changeStatGraph(tipo){
	
	document.images.stat_img.style.visibility = 'visible';
	document.images.stat_img.style.display = 'block';
	document.getElementById('stats_innerhtml').style.visibility = 'hidden';
	document.getElementById('stats_innerhtml').style.display = 'none';	
	
	switch(tipo){
		case "monthly":
			document.images.stat_img.src = 'img_stats.php?type=monthly&x=473&y=450';
			break;
		case "daily":
			document.images.stat_img.src = 'img_stats.php?type=daily&limit=7&x=473&y=450';
			break;
		case "familias":
			document.images.stat_img.src = 'img_stats.php?type=familias&x=473&y=500';
			break;
		case "marcas":
			document.images.stat_img.src = 'img_stats.php?type=marcas&x=473&y=500';
			break;
		case "ping":
			document.images.stat_img.style.visibility = 'hidden';
			document.images.stat_img.style.display = 'none';
			document.getElementById('stats_innerhtml').style.visibility = 'visible';
			document.getElementById('stats_innerhtml').style.display = 'block';				
			llamarasincrono('ping');
			break;
		case "ping_send":
			document.images.stat_img.style.visibility = 'hidden';
			document.images.stat_img.style.display = 'none';
			document.getElementById('stats_innerhtml').style.visibility = 'visible';
			document.getElementById('stats_innerhtml').style.display = 'block';				
			llamarasincrono('ping_send');
			break;
	}
}


function doCheckAll(formulario, marcado){
	if(marcado)
		checkAll(formulario)
	else
		uncheckAll(formulario)
}

function checkAll(formulario){

	for(i=0;i<formulario.length;i++) {
		c = formulario[i].elements;
		for(j=0;j<c.length;j++) {
			if(c[j].getAttribute("type") == "checkbox")
				c[j].checked = true ;
		}
	}
}

function uncheckAll(formulario){
	for(i=0;i<formulario.length;i++) {
		c = formulario[i].elements;
		for(j=0;j<c.length;j++) {
			if(c[j].getAttribute("type") == "checkbox")
				c[j].checked = false ;
		}
	}
}


//Extraemos las coordenadas en página de un elemento
function getY(oElement){
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function printImageTrc(trc){
	//document.write('<img src="http://tracker.servehttp.com/track.php?image='+ trc +'" width="1" height="1" border="0">');
}

//Movemos el output simultáneamente con el scroll
function JSFX_FloatDiv(id, sx, sy){
	//alert("test");
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};
	el.flt=function(){
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		this.sP(this.cx, this.cy);
		setTimeout(this.id + "_obj.flt()", 40);
	}
	return el;
}

function GetImageLocation(image, tipo) {
	var x = (document.layers) ? image.x : image.offsetLeft;
	var y = (document.layers) ? image.y : image.offsetTop;
	//alert(x + "," + y);
	
    switch(tipo){
        case "x": return(x);
        case "y": return(y);
        default:  return(0);
    }		
}

function ventanaSecundaria (URL){ 
	window.open(URL,"ventana1","width=505,height=380,scrollbars=NO") 
}
function openVisor(ID){
	w = 800;
	h = 600;
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;	
	document.location = 'index.php?w=i&id=' + ID,'imgvisor','width=800,height=600,top='+wint+',left='+winl+'resizable=YES,scrollbars=YES';
	//imgvisor = window.open('index.php?w=i&id=' + ID,'imgvisor','width=800,height=600,top='+wint+',left='+winl+'resizable=YES,scrollbars=YES');
	//imgvisor.focus();
}
function ventana (URL, HEIGHT){ 
	window.open(URL,"addimg","width=380,height=" + HEIGHT + ",scrollbars=NO") 
} 
function NewWindow(u,n,w,h,f,p,x,y){
	var win=(ws=window.screen?1:0,m=Math,C='center',R='random',M='custom',sw=screen.availWidth,sh=screen.availHeight,T=(p==C&&ws)?(sh-h)/2:(p==R&&ws)?(m.floor(m.random()*(sh-h))):(p==M)?y:100,L=(p==C&&ws)?(sw-w)/2:(p==R&&ws)?(m.floor(m.random()*(sw-w))):(p==M)?x:100,s='width='+w+',height='+h+',top='+T+',left='+L,s+=(!f||f=='')?'':','+f,window.open(u,n,s));
	if(win.focus){
		win.focus();
	}
}
function findObj(n, d) { 
	var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function Layers() { 
	var i,p,v,obj,args=Layers.arguments;
	for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) { v=args[i+2];
	if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
	obj.visibility=v; }
}
//function init(){
//	//alert("ESTADO: " + document.htmlareajs.readyState);
//	//alert("ESTADO: " + document.getElementById('funcionesjs').readyState);
//	timer();
//	initScrollLayer();
//	clearpage();
//}

function toggleDiv(id, param){
	var elm = document.getElementById(id);
    
    if (!elm || elm.length == 0) return;
    var d = elm.style.display;
        //alert(d);
    if (d && (d != "none" || d == "")){
        if(id == 'user_extras'){
	        elm.style.display = "block";
	        elm.style.visibility = "visible";
        	document.getElementById('user_extras_txt').innerHTML = USER_EXTRAS_ON;
        }
        if(id == 'lightbox'){
        	if($('footer').style.height == '175px')
				$('footer').style.height = '40px';
			else
				$('footer').style.height = '175px';
        }
    	else{
	        elm.style.display = "none";
	        elm.style.visibility = "hidden";
	    }
    }
    else{
        elm.style.display = "block";
        elm.style.visibility = "visible";
        if(id == 'user_extras'){
	        elm.style.display = "none";
	        elm.style.visibility = "hidden";
        	document.getElementById('user_extras_txt').innerHTML = USER_EXTRAS_OFF;
        }
        if(id == 'lightbox'){
        	$('footer').style.height = '175px';
        }
    }
}

function toggleTXT(id_objeto, var1, var2){
	var contenido = document.getElementById(id_objeto).innerHTML;
	if(contenido == var1)
		document.getElementById(id_objeto).innerHTML = var2;
	else if(contenido == var2)
		document.getElementById(id_objeto).innerHTML = var1;
}


function mostrarCapa(idcapa, tipo){
	if (tipo == 'formulario'){
		bg2 = "url(i/skin/borne_boton.gif)";
		bg1 = "url(i/skin/borne_boton_off.gif)";

		if(document.getElementById('td_datos')){
			document.getElementById('td_datos').style.backgroundImage = bg1;
			document.getElementById('datos').style.display = 'none';
			document.getElementById('datos_content').style.display = 'none';
			document.getElementById('a_datos').style.color = '#8E9595';
		}
		
		if(document.getElementById('td_stats')){
			document.getElementById('td_stats').style.backgroundImage = bg1;
			document.getElementById('stats').style.display = 'none';
			document.getElementById('stats_content').style.display = 'none';
			document.getElementById('a_stats').style.color = '#8E9595';
		}
		
		if(document.getElementById('td_import')){
			document.getElementById('td_import').style.backgroundImage = bg1;
			document.getElementById('import').style.display = 'none';
			document.getElementById('import_content').style.display = 'none';
			document.getElementById('a_import').style.color = '#8E9595';
		}
		
		//------------------------------------------------------------------------
		document.getElementById('td_' + idcapa).style.backgroundImage = bg2;
		document.getElementById(idcapa).style.visibility = 'visible';
		document.getElementById(idcapa).style.display = 'block';
		document.getElementById(idcapa + '_content').style.visibility = 'visible';
		document.getElementById(idcapa + '_content').style.display = 'block';
		document.getElementById('a_'+ idcapa).style.color = '#364644';
	}
	
//	if((idcapa == 'login') || (idcapa == 'lost') || (idcapa == 'nuevo')){
//		document.getElementById('login').style.visibility = 'hidden';
//		document.getElementById('login').style.display = "none";
//		document.getElementById('lost').style.visibility = 'hidden';
//		document.getElementById('lost').style.display = "none";
//		document.getElementById('nuevo').style.visibility = 'hidden';
//		document.getElementById('nuevo').style.display = "none";
//		
//		//------------------------------------------------------------------------
//		document.getElementById(idcapa).style.visibility = 'visible';
//		document.getElementById(idcapa).style.display = "block";		
//	}
		
	
	
	return;
}

function createCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	//document.cookie = name+"="+value+expires+"; path=/encuadro/; domain=baladron.es";
}

function dieForm(url_actual){
	var indice = document.of_form.order.selectedIndex;
	var selOrden = document.of_form.order.options[indice].value;
	var indice2 = document.of_form.sentido.selectedIndex;
	var selSentido = document.of_form.sentido.options[indice2].value;	
	
	document.location = 'index.php'+url_actual+'&orden='+selOrden+'&sentido='+selSentido;
}

function actualizaLocation(){
	var indice = document.form_buscador.env_ciudad.selectedIndex;
	var selCiudad = document.form_buscador.env_ciudad.options[indice].value;
	createCookie('location',selCiudad,7);
	
	if(document.URL.indexOf("op=bus") > 0)
		document.form_buscador.submit();
	else if(document.URL.indexOf("op=logout") > 0)
		document.location = 'index.php';
	else
		document.location.reload(false);		
}

function actualizaMoneda(){
	var indice = document.fl_list.moneda.selectedIndex;
	var selCoin = document.fl_list.moneda.options[indice].value;
	createCookie('moneda',selCoin,7);
	
	if(document.URL.indexOf("op=logout") > 0)
		document.location = 'index.php';
	else
		document.location.reload(false);		
}

function actualizaIdioma(idioma){
	createCookie('idioma',idioma,7);	
	document.location.reload(false);		
}



//----------------------------------------------------------------------
//Funciones DASHBOARD
//----------------------------------------------------------------------
var menuHeight=0;var growHeight=-100;var growMax=0;var displayMenu=true;

function openwidgets(id) {
	if (displayMenu) { openMenu(id);	} else { closeMenu(id);}
}
function openMenu(id) {
	document.getElementById("showCalendar").style.zIndex = 1;
	document.getElementById("board").style.display = 'block';
	growHeight+=10;	
	if(growHeight<=growMax) {		
		document.getElementById("board").style.top=growHeight+"px";		
		setTimeout("openMenu("+id+")",20)	
	} 
	else {		
		document.getElementById("board").style.top="0px";		
		displayMenu=false;
		createCookie('hidePanel',true,7);
		
		//if(id)
		//	remoteurl(id);
	}
	
}
function closeMenu(id) {
	growHeight-=10;
	//document.getElementById("lineas_eventos").style.display = 'none';
	if(growHeight>=-100) {		
		document.getElementById("board").style.top=growHeight+"px";		
		setTimeout("closeMenu()",20)
		createCookie('hidePanel',false,7);	
	} 
	else {		
		document.getElementById("board").style.top="-258px";
		displayMenu=true;
		document.getElementById("showCalendar").style.zIndex = 11;
		var cal_evento = document.getElementById("cal_evento");
		//gsearch.innerHTML = '<h4>Cargando evento...</h4><p>Un momento por favor.</p>';
		//cal_evento.innerHTML = '<table class="calendar" align="left" style="margin-left:4px;margin-top:4px" border="0" cellspacing="1" cellpadding="1"><tr><td height="16" bgcolor="#D5DEEA" align="center" valign="middle"></td></tr><tr><td height="118" bgcolor="white" align="center" valign="middle"></td></tr></table>';
	}
}

function remoteurl(id) {
	//var search = document.getElementById("igoogle").value;
	var search = id;
	var idLayer = document.getElementById("cal_evento");
	if(search != "") {
		//idLayer.innerHTML = '<h4>Cargando evento...</h4><p>Un momento por favor.</p>';
		//idLayer.innerHTML = '';
        var qgoogle = function (oXML) { idLayer.innerHTML = oXML.responseText; };
        query("w=cl&op=l&datevents="+escape(search),qgoogle);
	}
}
function updateCalendar(id) {
	//var search = document.getElementById("igoogle").value;
	//openMenu();
	var search = id;
	var idLayer = document.getElementById("showCalendar");
	if(search != "") {
		idLayer.innerHTML = '<table align="center" style="margin-top:44px;" border="0" cellspacing="1" cellpadding="1"><tr><td height="40" align="center" valign="middle">Actualizando...<br><br><img src="i/web/loading.gif"></td></tr></table>';
		//idLayer.innerHTML = '';
        var qgoogle = function (oXML) { idLayer.innerHTML = oXML.responseText; };
        query("w=cl&op=l&updfecha="+search,qgoogle);
	}
}
function query(params,funcion) {
	var myConn = new dXHConn();
    if (!myConn) alert("XMLHTTP no disponible. Actualice su navegador.");
    //myConn.connect("inc/dashboard/ajax.dashboard.php", "GET", params, funcion);
    myConn.connect("index.php", "GET", params, funcion);
}


/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function dXHConn(){
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone){
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET"){
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else{
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete){
          bComplete = true;
          fnDone(xmlhttp);

        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


function getSelected(frm_multiple, url_location){
	
	var doc_array 		= '';
	var hay_checkeds 	= '';
	var targetFolder	= '';
	var myArray 		= [];
	
	//Llamamos a cada uno de los registros
	if(frm_multiple){
		id = 0;
		
		for (i = 0; i < frm_multiple.length; i++){
			if(frm_multiple[i].type == 'checkbox' && frm_multiple[i].value != 'on')
				if(frm_multiple[i].checked){
					hay_checkeds++;
				}
		}
		if(hay_checkeds){
			//Si confirma la acción, procedemos
			for (i = 0; i < frm_multiple.length; i++){
				if(frm_multiple[i].type == 'checkbox' && frm_multiple[i].value != 'on')
					if(frm_multiple[i].checked)
						doc_array = doc_array + '&cest%5B'+i+'%5D=' + frm_multiple[i].value;
			}
			//url = "index.php?op=dxconn&menoscesta=1"+doc_array+"&cuantos=todos";
			return doc_array;
		}
		else			
			return false;
	}
}


window.defaultStatus = dominio;
