var desplegada = 0;
var openAviso = 0;
var popup;

var ventana;
var abierto=0;


function viewPhoto(foto)
{
	var ancho=600;
	var alto=450;
	var titulo = 'Gestinorte Inmobiliaria Gallega';
	if(openAviso==1){ventana_aviso.close();ventana_aviso=null;openAviso=0}
	ventana_aviso=window.open('','ventana','width=10,height=10,toolbar=no,resizable=no,scrollbars=no,status=0')
	ventana_aviso.document.write('<html><head><title>'+titulo+'</title><style media="print">.noprint {display:none}</style></head><body style="background:white;overflow:hidden" marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" scroll="no" onUnload="opener.abierto=0">');
	ventana_aviso.document.write('<img src="'+foto+'">');
	ventana_aviso.document.write('<div style="border-top:1px solid white;height:25px;background:#ff9933;vertical-align:top"><table style="width:100%;height:100%" cellpadding="0" cellspacing="0"><tr><td style="vertical-align:top;padding-left:8px;">');
	ventana_aviso.document.write('<a class="noprint" style="font-family:verdana,arial;font-size:11px;text-decoration:none;color:#ffffff" onmouseover="this.style.color=\'#000000\'" onmouseout="this.style.color=\'#ffffff\'" href="#" onclick="print()"><img style="border:none;vertical-align:middle" src="img/print.gif" /> Imprimir </a>');
	ventana_aviso.document.write('</td><td style="text-align:right;padding-right:8px;vertical-align:top"><a class="noprint" style="font-family:verdana,arial;font-size:11px;text-decoration:none;color:#ffffff" onmouseover="this.style.color=\'#000000\'" onmouseout="this.style.color=\'#ffffff\'" href="#" onclick="self.close()"><img style="border:none;vertical-align:middle" src="img/close.gif" /> Cerrar esta ventana</a></td></tr></table></div>');
	ventana_aviso.document.close();	
	ventana_aviso.resizeTo(ancho+10,alto+82);
	ventana_aviso.moveTo((screen.width-ancho)/2,(screen.height-(alto+82))/2);
	openAviso++;
}


function openPopup(url)
{
	var ancho;
	var alto;
	
	ancho=700;
	alto=600;
	
	if(desplegada==1){popup.close();popup=null;desplegada=0}
	popup=window.open(url,'','width='+ancho+',height='+alto+',resizable=no,status=no,scrollbars=yes,toolbars=no');
	desplegada++;
}

function openPopup2(url,ancho,alto)
{
	if(desplegada==1){popup.close();popup=null;desplegada=0}
	popup=window.open(url,'','width='+ancho+',height='+alto+',resizable=no,status=no,scrollbars=yes,toolbars=no');
	desplegada++;
}

function viewDetalle(url,ancho,alto)
{
	if(desplegada==1){popup.close();popup=null;desplegada=0}
	popup=window.open(url,'','width='+ancho+',height='+alto+',resizable=no,status=no,scrollbars=yes,toolbars=no');
	popup.resizeTo(ancho+10,alto+59);
	popup.moveTo((screen.width-ancho)/2,(screen.height-(alto+59))/2);
	desplegada++;
}

function borrar_backup(nombre, nombre_real)
{
	if(confirm('¿ Está usted seguro de que desea eliminar la ' + nombre + ' ?'))
		location.href('admin.php?type=cs&delbackup=' + nombre_real);
}

function ajustar(contenedor,foto)
{
	var maximo = 100;
	var objeto = new getObj(contenedor);
	var producto = new Image();
	producto.src = foto;
	
	while(producto.height==0 || producto.width==0)
		producto.src = foto;
		
	if(producto.height > maximo)
	{
		producto.width = parseInt(producto.width * maximo / producto.height); 
		producto.height = maximo;
	}
	
	if(producto.width > maximo)
	{
		producto.height = parseInt(producto.height * maximo / producto.width); 
		producto.width = maximo;
	}
	
	objeto.obj.src=producto.src;	
	objeto.obj.height=producto.height;
	objeto.obj.width=producto.width;
	
}


function checkPrecio(control)
{
	var objeto = new getObj(control);
	if(objeto.obj.value>0)
		return true;
	else 
		return false;
}

function abrirNuevo()
{
	var detalle = new getObj('detalle');
	openPopup(detalle.obj.src);
}


function showImage(nombre, referencia, image_name, refFabricante)
{
	var control = new getObj('viewImage');
	var html = "<hr>";
	
	html+="<div id='prodname'><b>Ref:</b> "+refFabricante+"<br>";
	html+="<b>Artículo:</b> "+nombre+"</div>";
	html+= "<img src='"+image_name+"'></img>";
	html+="<div style='margin-top:10px'><form action='?type=gi&cimg&delImage' method='post'>";
	html+="<input type='hidden' name='refUMD' value='"+referencia+"'>";
	html+="<input type='hidden' name='imagen' value='"+image_name+"'>";
	html+="<a href='#' onclick='if(confirm(\"¿ Desea realmente eliminar la imagen asociada a este producto ?\")) submit()'>";
	html+="<img style='vertical-align:middle;margin-right:5px' src='img/error2.gif'>Eliminar imagen</a>";
	html+="</form></div>";
	
	control.obj.innerHTML = html;
}

function copiar(text)
{
	if (window.clipboardData) 
   	{
   		window.clipboardData.setData("Text", text);
  	}
}

function asignImage(nombre, referencia, refFabricante)
{

	var control = new getObj('viewImage');
	var html = "<hr>";
	html+= "<script>valido=false;</script>";
	html+="<div id='prodname'><b>Ref: </b>";
	html+="<span onmouseout='window.status=\"\"' onmouseover='window.status=\"Pulse para copiar el código de referencia al portapapeles\"' onclick='copiar(this.innerText)' style='cursor:hand'>"+refFabricante+"</span><br>";
	html+="<b>Artículo: </b>";
	html+="<span onmouseout='window.status=\"\"' onmouseover='window.status=\"Pulse para copiar el nombre del producto al portapapeles\"' onclick='copiar(this.innerText)' style='cursor:hand'>"+nombre+"</span></div>";
	html+="<form action='?type=gi&simg&asignImage' method='post'><input type='hidden' name='refUMD' value='"+referencia+"'>";
	html+="<input onChange='var extension=this.value.substring(this.value.length-3,this.value.length).toLowerCase();if(extension!=\"gif\" && extension!=\"jpg\"){ alert(\"La imagen debe tener formato JPEG o GIF.\");valido=false;} else valido=true;' type='file' name='imagen'>";
	html+="<div style='margin-top:10px'>";
	html+="<a href='#' onclick='if(valido==true) submit(); else alert(\"No se puede procesar el archivo que ha seleccionado. \\nIntroduzca una imagen JPEG / GIF válida.\");'>";
	html+="<img style='vertical-align:middle;margin-right:5px' src='img/ok.gif'>Asignar imagen</a>";
	html+="</form></div>";
	
	control.obj.innerHTML = html;
}


function getCSSbyBrowser(css)
{
	var nombre = navigator.appName;
	if (nombre == "Microsoft Internet Explorer")
		document.write("<LINK rel='stylesheet' type='text/css' href='css/"+css+".css'>");
	else
		document.write("<LINK rel='stylesheet' type='text/css' href='css/"+css+"_firefox.css'>");
}

function roundOff(value, precision)
{
		value = Math.round(value*Math.pow(10,precision));
		whole = "" + value;
        var decPoint = whole.length - precision;

        if(decPoint != 0)
        {
                result = whole.substring(0, decPoint);
                result += ".";
                result += whole.substring(decPoint, whole.length);
        }
        else
        {
                result = 0;
                result += ".";
                result += whole.substring(decPoint, whole.length);
        }
        return result;
}

function progreso(obj) {
	var txt = new getObj(obj);
	var texto = '<div id="progreso">Proceso en curso ... <span id="crono"></span></div><hr>';
	txt.obj.innerHTML=texto;
}

function progreso2(obj) {
	var txt = new getObj(obj);
	var texto = '<div id="progreso">Proceso en curso ... <span id="crono"></span></div>';
	txt.obj.innerHTML=texto;
}


function delChecked(maximo)
{
	var url;
	url="compra.php?type=cc";
	
	for(x=0;x<maximo;x++)
		if(document.body.all['checkbox'+x].checked==true)
			url+="&delID[]="+x;
	
	location.href=url;
}

function getObj(name)
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if (document.layers)
	{
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

function showsubmenu (submenu)
{
	for(x=0;x<countcats;x++)
	{
		var temp = new getObj('smenu'+categorias[x]);
		temp.style.display='none';
	}
	
	var menu = new getObj(submenu);	
	if (menu.style.display=='block')
		menu.style.display='none';
	else
		menu.style.display='block';
	
	var sel=document.getElementById('categorias');
	if(sel.options[0].value == '-1')
		sel.options[0] = null;
		
}

function show(elemento)
{
	var objeto= new getObj(elemento);
	objeto.style.display='block';
}

function hide(elemento)
{
	var objeto= new getObj(elemento);
	objeto.style.display='none';
}

function insertValue(id,texto)
{
	var objeto = new getObj(id);
	objeto.obj.innerText=texto;
}

