var reset =1;
var origenX = -1;
var origenY = -1;
var finX = '';
var finY = '';

var punto;

function muestraCapa(valor, id)
{
	//valor es un boolean si es true mostramos si es false ocultamos
	if (valor == true)
		document.getElementById(id).className = 'block';
	if (valor == false)
		document.getElementById(id).className = 'hidden';
}
function muestraCamposBanner(tipo)
{
	if (tipo != '')
	{
		var index;
		document.getElementById('bannerflash').className = 'hidden';
		document.getElementById('bannerhtml').className = 'hidden';
		document.getElementById('bannerimage').className = 'hidden';
		
		if (tipo == 'flash')
		{
			index = 0;
		}
			
		if (tipo == 'html')
			index = 1;
			
		if (tipo == 'image')
			index = 2;
		
		document.getElementById('tipo').options[index].selected = true;
		document.getElementById('banner'+tipo).className = 'visible';
	}
}

function selectBox(x,y,tpunto)
{
	punto = tpunto;
	var buf = '';
		
	if (reset == 1)
	{
		resetSeleccion();
		origenX = x;
		origenY = y;
		document.getElementById('cuadro_'+x+'_'+y).style.border = '1px solid #000;';
		document.getElementById('xini').value = origenX;
		document.getElementById('yini').value = origenY;
		document.getElementById('origen').value = '('+origenX+', '+origenY+')';
		reset =0;
	}
	else
	{
		//es el punto de fin
		finX = x;
		finY = y;
		
		if (finX < origenX)
		{
			buf = origenX;
			
			origenX = finX;
			finX = buf;
			
		}
		
		if (finY < origenY)
		{
			buf = origenY;
			
			origenY = finY;
			finY = buf;
			
		}
		document.getElementById('xini').value = origenX;
		document.getElementById('yini').value = origenY;
		document.getElementById('origen').value = '('+origenX+', '+origenY+')';
		
		for (i=origenY;i<=finY;i++)
		{
			for (j=origenX;j<=finX;j++)
			{
				document.getElementById('cuadro_'+j+'_'+i).style.border = '1px solid #FF0000;';
				document.getElementById('cuadro_'+j+'_'+i).style.backgroundColor = '#FF0000';
			}
		}
		
		document.getElementById('fin').value = '('+finX+', '+finY+')';
		document.getElementById('dimensiones').value = ((finX-origenX)+1)*punto+' x '+((finY-origenY)+1)*punto+ 'px';
		
		
		document.getElementById('anchol').value = ((finX-origenX)+1)*punto;
		
		
		document.getElementById('altol').value= ((finY-origenY)+1)*punto;
		reset = 1;
	}
}

function resetSeleccion()
{
	var i;
	var j;
	
	if (origenX != -1)
	{
		for (i=origenY;i<finY+1;i++)
		{
			for (j=origenX;j<finX+1;j++)
			{
				document.getElementById('cuadro_'+j+'_'+i).style.backgroundColor = '#FFF';
				document.getElementById('cuadro_'+j+'_'+i).style.border = '1px solid #E0E0E0;';	
			}
		}
	}
}

function SeleccionaBanner(banneri, bannerplinea, width, height, seleccionado, tipo)
{
	//alert('BannerpLinea: '+id+' Dimensiones: '+width+'x'+height);
	//Creamos un ventanator para sacar los banners que coincidan con las dimensiones d bannerplinea
	
	var params = '&banneri='+banneri+'&idlinea='+bannerplinea+'&width='+width+'&height='+height+'&sel='+seleccionado+'&tipo='+tipo;;
	myconfirm('misc/seleccionabanners.php',params);
}

function emptyinput(id, matchtext)
{
	if (document.getElementById(id).value.toUpperCase() == matchtext.toUpperCase()) 
		document.getElementById(id).value = '';
}

function resetinput(id, matchtext)
{
	if (document.getElementById(id).value == '') 
	{
		document.getElementById(id).value = matchtext;
		//document.getElementById(id).type = 'text';
	}
}

function loadswf(swf, divdestino, ancho, alto)
{
	var so = new SWFObject(swf, "sotester", ancho, alto, "7", "#FFFFFF");
	
	if (ancho == 550 && alto == 310)
	{
		so.addParam("wmode", "transparent");
	}
	
	so.useExpressInstall('expressinstall.swf');
	so.write(divdestino);
}

function checkHijos(object)
{
	var contenedor = document.getElementById('menucheck');
	var inputCol = contenedor.getElementsByTagName('input');
	var lCol = inputCol.length;
	var j=0;
	var arr = new Array();
	var salir = false;
	var entrar = false;
	var nivel ='';
	var chk;
	var yo;
	var minivel;
	var ultimopadre;
	
	for (var i=0;i<lCol;i++)
	{
		if (inputCol[i].type == 'checkbox')
		{
			arr[j] = inputCol[i];
			j++;
		}
	}
	
	i=0;
	while (i<j && salir == false)
	{
		nivel = document.getElementById('nivel'+i).value;
		
		if (object.name == 'menu'+i)
		{
			yo = 'menu'+i;
			minivel = document.getElementById('nivel'+i).value;
			entrar = true;
		}
		if (document.getElementById('nivel'+i).value == 1)
			ultimopadre = 'menu'+i
		
		if (entrar == true)
		{
			if (minivel == 1)
			{
				if (nivel == 1 && object.name != 'menu'+i)
					salir = true;
				else
				{
					if (object.name == 'menu'+i)
					{
						chk = object.checked;
					}
					document.getElementById('menu'+i).checked = chk;
				}
			}
			else
			{
				document.getElementById(ultimopadre).checked = true;
				salir = true;
			}
		}
		i++;
	}
}
/*
function LoadPage(page,param1,param2)
{
	var params = '&id='+param1+'&p='+param2;
	doAjax(page, params, 'PrintPage', 'get', 0);
}

function PrintPage(t)
{
	document.getElementById('divdataext').innerHTML = t;
}

function updateImagePVenta(url)
{
	var html = "<img alt='Imagen del producto' src='lib/resize.php?imagen=" + url + "&ancho=200&alto=200'/>";
	document.getElementById('imagenprincipal').innerHTML = html;
}

function addToChart(pventa, unidades)
{
	var params = '&p='+pventa+'&u='+unidades;
	doAjax('misc/compras/addtochart.php', params, 'addedToChart', 'get', 0);
	app.ventanator.prototype.cerrar();
	
	return true;
}

function addedToChart(t)
{
	var arr = t.split('|');
	var chart = arr[0];
	var linea = 'totallinea'+arr[1];
	var newtotal = arr[2];
	var totalbase = arr[3];
	
	document.getElementById('chart').innerHTML = chart;
	
	if (document.getElementById(linea) != null)
	{
		document.getElementById(linea).innerHTML = newtotal;
	}
	
	if (document.getElementById('basecompra') != null)
	{
		totalbase = "Base: <b>" + totalbase + " &euro;";
		document.getElementById('basecompra').innerHTML = totalbase;
	}
}
*/

/*
function LoadImage(url)
{
	var params = '&i='+url;
	doAjax('adm/productos/loadimage.php', params, 'updImagenPrincipal', 'get', 0);
}

function updImagenPrincipal(t)
{
	var pos = t.indexOf('|');
	var t1=t.substr(0,pos);
	var t2=t.substr(pos+1);
	
	document.getElementById('activo'+t1).innerHTML = t2;
}
*/

function textos(clave,pagina)
{
	var params = '&clave='+clave;
	doAjax('menu/textospagina.php', params, 'setTextos', 'get', 0);
}

function setTextos(t)
{
	document.getElementById('traductor').innerHTML = t;
}

// FUNCIONES AJAX PARA VALIDAR QUE SI EL ELEMENTO DE MENÚ
// ESTÁ EN LA ZONA PRIVADA DEL MENÚ
// SE DESHABILITA LA OPCIÓN DE NAVEGABLE
// SI ES PUBLICO SE SELECCIONA EL CHECK "PUBLICO"
	function validateNavegable(menu)
	{
		var params= '&id='+menu;
		doAjax('menu/validatenavegable.php', params, 'setNavegable', 'get', 0);
	}
	
	function setNavegable(t)
	{
		var tbln = false;
		var msg = '';
		
		if (t == 'true')
			tbln = true;
			
		if (document.getElementById('navegable').checked != tbln)
		{
			msg = 'Se ha cambiado el valor del campo "Navegable" a ' + tbln;
			document.getElementById('navegable').checked = tbln;
		}
		
		if (document.getElementById('publico').checked != tbln)
		{
			document.getElementById('publico').checked = tbln;
			msg = msg + "\r\n" + 'Se ha cambiado el valor del campo "Público" a ' + tbln;
		}
		
		if (msg != '')
			alert(msg);
	}
// FIN AJAX VALIDATENAVEGABLE

// FUNCIONES AJAX PARA ACTUALIZACIONES

	function updateMenu(value)
	{
		/*
		// Sólo para setbatch2
		if (value == undefined)
			value = 0;
		var p = 'v='+value;*/
		doAjax('menu/setbatch.php', 'v=1', 'updateMenuBack', 'get', 0);
		//doAjax('menu/setbatch2.php', p, 'updateMenuBackAlt', 'get', 0);
	}

	function updateMenuBack(t)
	{
		document.getElementById('updateMenuBack').innerHTML = t;
	}

	// en desuso. asociado a setbatch2.
	// este sistema es extremadamente lento
	function updateMenuBackAlt(t)
	{
		var pos = t.indexOf('MORE');
		
		if (pos != -1)
		{
			var vini = t.indexOf('|');
			var v = t.substring(pos + 5, vini);
			var max = t.substring(vini+1+4);
			//alert(v);
			//alert(max);
			
			document.getElementById('extra').innerHTML = '<br />Porcentaje completado: <b>' + Math.round(v*100/max, 10) + '%</b>';
			updateMenu(v);
		}
		else
			document.getElementById('updateMenuBack').innerHTML = t;
	}
	
	function updateNoticia()
	{
		doAjax('noticias/setbatch.php', '', 'updateNoticiaBack', 'get', 0);		
	}
	
	function updateNoticiaBack(t)
	{
		document.getElementById('updateNoticiaBack').innerHTML = t;
	}
	
	function updateAnalytics()
	{
		doAjax('analytics/setbatch.php', '', 'updateAnalyticsBack', 'get', 0);		
	}
	
	function updateAnalyticsBack(t)
	{
		document.getElementById('updateAnalyticsBack').innerHTML = t;
	}
// FIN FUNCIONES AJAX PARA ACTUALIZACIONES BATCH

// FUNCION AJAX PARA VALIDAR LA POSICION DEL MENU
function checkPositionMenu(parent, position,id)
{
	var params= '&parent='+parent+'&position='+position+'&id='+id;
	doAjax('menu/checkpositionmenu.php', params, 'checkPositionMenuBack', 'get', 0);
}

function checkPositionMenuBack(t)
{
	document.getElementById('positionok').innerHTML = t;
}

function showPlantilla(name)
{
	var params="&file="+name;
	doAjax('banner/showPlantilla.php', params, 'showPlantillaBack', 'get', 0);
}

function showPlantillaBack(t)
{
	document.getElementById('divcontbanner').innerHTML = '<textarea style="width:320px;height:200px" id="contenido_banner" name="contenido_banner">'+t+'</textarea>';
}

function testQuery(name)
{
	while (name.indexOf('+') != -1)
		name = name.replace('+', '·');
	var params="&sql="+name;
	doAjax('banner/testQuery.php', params, 'testQueryBack', 'post', 0);
}

function testQueryBack(t)
{
	document.getElementById('divcontbanner').innerHTML = t;
}

function saveSQL(sql, name)
{
	while (sql.indexOf('+') != -1)
		sql = sql.replace('+', '·');
	var params="&sql="+sql+'&name='+name;
	doAjax('banner/savesql.php', params, 'saveSQLBack', 'post', 0);
}

function saveSQLBack(t)
{
	document.getElementById('sqlgrabadas').innerHTML = t;
}

function showSQL(sql)
{
	var params="&sql="+sql;
	doAjax('banner/showquery.php', params, 'showSQLBack', 'get', 0);	
}

function showSQLBack(t)
{
	document.getElementById('divsql').innerHTML = '<textarea style="width:500px;height:300px" id="sql" name="sql" class="textarea">' + t + '</textarea><br />';	
}

function formatFecha(fecha)
{
	var tmp = '';
	if (fecha == '1900-01-01 00:00:00')
	{
		tmp = '';
	}
	else
	{
		if (fecha.substring(4,5) == '-')
		{
			tmp = fecha.substring(8, 10) + '/' + fecha.substring(5, 7) + '/' + fecha.substring(0, 4);
		}
		
		if (fecha.substring(11, 19) != '00:00:00')
		{
			tmp = tmp + ' ' + fecha.substring(11, 19);
		}
	}
	
	return tmp;
}


function muestraOcultaHijos(padre, accionalt)
{
	var trs = document.getElementsByTagName('tr');
	var nCol = trs.length;
	var elid;
	var arr;
	var tmp = '';
	var accion;
	
	for (var i = 0; i < nCol; i++)
	{
		elid = trs[i].id;
		arr=elid.split('_');
		tmp = tmp + ' ' + arr[1];
		
		if (arr[1] == padre)
		{
			if ((trs[i].style.display == '' && accionalt == null) || accionalt == 'C')
			{
				trs[i].style.display = 'none';
				document.getElementById('pm'+padre).src='../img/plus.gif';
				accion = 'C';
			}
			else
			{
				trs[i].style.display = '';
				document.getElementById('pm'+padre).src='../img/minus.gif';
				accion = 'O';
			}
		}
	}
	
	return accion;
}

function AbrirCerrarMenuGrouped(elementos)
{
	var elementos = elementos.split(',');
	var opt;
	
	opt = muestraOcultaHijos(elementos[0]);	
	for (var i = 1; i < elementos.length; i++)
		muestraOcultaHijos(elementos[i], opt);	
}

function muestraOculta(div)
{
	if (document.getElementById(div).style.display == 'block')
		document.getElementById(div).style.display = 'none';
	else
		document.getElementById(div).style.display = 'block';
	//checkAbrirTodo();
}

function checkAbrirTodo(id)
{
	if (document.getElementById(id).innerHTML == 'Expandir')
	{
		document.getElementById(id).innerHTML = 'Contraer';
	}
	else
	{
		document.getElementById(id).innerHTML = 'Expandir';
	}	
}

function abrirTodo(label, id)
{
	var i;
	var modo = '';
	
	if (document.getElementById(label).innerHTML == 'Expandir')
	{
		modo = 'block';
		document.getElementById(label).innerHTML = 'Contraer';
	}
	else
	{
		modo = 'none';
		document.getElementById(label).innerHTML = 'Expandir';
	}
	
	document.getElementById(id).style.display= modo;
}

function init()
{
	if (document.forms[0])
	{
		var init;
		var salir = false;
		var thelength = document.forms[0].elements.length;
		
		for (init=0;init<thelength;init++)
		{
			if (document.forms[0].elements[init].tagName == 'INPUT' && salir == false)
			{
				if (document.forms[0].elements[init]['type'] == 'text')
				{
					document.forms[0].elements[init].focus();
					salir = true;
				}
			}
			
			if (document.forms[0].elements[init].tagName == 'SELECT' && salir == false)
			{
				document.forms[0].elements[init].focus();
				salir = true;
			}
			
			if (document.forms[0].elements[init].tagName == 'TEXTAREA' && salir == false)
			{
				document.forms[0].elements[init].focus();
				salir = true;
			}
		}
	}
}

function confirmacion()
{
	if (window.confirm("Está seguro de querer eliminar el elemento?\r\nPara restablecer el elemento será necesario contactar con Estudio Seijo.\r\nSi desea desactivar el elemento de forma temporal utilice el campo 'Activo'"))
	{
		//return true;
	}
	else
		return false;
}

function externalLinks() 
{
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) 
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
			anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
	}
}

function updateClaveMenu()
{
	doAjax('menu/setbatchclave.php', '', 'updateClaveMenuBack', 'get', 0);		
}

function updateClaveMenuBack(t)
{
	document.getElementById('updateClaveMenuBack').innerHTML = t;
}

function buscarlocalizador(cp)
{
	params = '&destino=tpvProx&cp=' + cp;
	doAjax('zona_disa_v2/scriptajax.php', params, 'rbuscarlocalizador', 'get', 0);
//		doAjax('searchpdisa.php', params, 'rbuscarlocalizador', 'get', 0);
}

function rbuscarlocalizador(t)
{
	document.getElementById('resultados').innerHTML = t;
}

function setComision(index)
{
	var divs = document.getElementsByTagName('span');
	var nCol = divs.length;
	var elid;
	
	for (init=0; init<nCol; init++)
	{
		elid = divs[init].id;
		
		if (elid.substring(0,5) == 'produ')
			if (elid.substring(0,7) == 'produ_'+index)
				document.getElementById(divs[init].id).style.display = '';
			else
				document.getElementById(divs[init].id).style.display = 'none';
	}
}
