// ********************* Funciones Para los menus que siempre te salgan en pantalla con el nombre tabla_desplazar
function findScrollTop() {
	if (window.pageYOffset != null)
		return window.pageYOffset;
	if (document.body.scrollWidth != null)
		return document.body.scrollTop;
	return null;
}
var offsetTablaInicial = null;

function miOnScroll() {
	var top;
	//alert("scroll");
	if ( offsetTablaInicial==null) {
		offsetTablaInicial = document.all["tabla_desplazar"].offsetTop;
	} else {
		top = findScrollTop();
		if (top!=null) {
			if (top>offsetTablaInicial) {
				document.all["tabla_desplazar"].style.top = top;				
			} else {
				document.all["tabla_desplazar"].style.top = offsetTablaInicial;
			}
		}				
	}
}


function eliminarEntradaAsiento(numero) {
	if (confirm("¿Seguro que desea eliminar esta linea?")) {
		eval("document.formulario.importe_"+numero+".value='';");
		eval("document.formulario.cuenta_"+numero+".value='';");
	}
	return;
}

function changeDisplay(id) {
	//Permite ocultar o ver un elemento:
	var nodo=document.all[id];
	if (nodo.style.display=="none") {
		nodo.style.display="block";
	} else {
		nodo.style.display="none";
	}
}

function openWindow(url, ancho, alto) {
	strancho=ancho;
	stralto=alto;
	if (ancho==null) strancho = 600;
	if (alto==null) stralto = 360;
	window.open(url,"","resizable=yes,status=1,scrollbars=1,width="+strancho+",height="+stralto,false);
}

function esVacio(cadena) {
	if (trim(cadena)=="") return true;
	else return false;
}

function comprobarTabla(form) {
	var res;
	res = true;
	if (form.op.value=="guardar_cliente") {
		if (trim(form.empresa.value)=="") {
			alert("El nombre de la empresa no puede ser vacio");
			res = false;
		}
	} else if (form.op.value=="guardar_contacto") {
		if (trim(form.nombre.value)+trim(form.apellidos.value)=="") {
			alert("El nombre o los apellidos del Contacto no puede ser Vacio");
			res = false;
		}
	} else if (form.op.value=="guardar_centro") {
		if (trim(form.centro.value)=="") {
			alert("El nombre del Centro no puede ser Vacio");
			res = false;
		}
	} else if (form.op.value=="guardar_consumo") {
		if (trim(form.idproducto.value)=="") {
			alert("El nombre del Producto no puede ser Vacio");
			res = false;
		}
	}
	return res;
}

function openLogin() {
	var url="popup_login.php";
	window.open(url,"","resizable=0,status=0,scrollbars=0,width=200,height=140",false);
	//window.open(url,"","",false);
}

function openBigWindow(url) {
	window.open(url,"","resizable=yes,status=1,scrollbars=1,width=540,height=480",false);
}

function setEmpresa(nombre, id) {
	//Si la nueva empresa es distinta, borro el codigo del centro
	if (window.opener.document.formulario.idempresa.value != id) {
		window.opener.document.formulario.idcentro.value="";
	}
	window.opener.document.formulario.idempresa.value=id;
	window.opener.document.formulario.empresa.value=nombre;
	window.close();
}

function setProducto(id, codigo, nombre) {
	window.opener.document.formulario.idproducto.value=id;
	window.opener.document.formulario.producto.value=nombre;
	window.opener.document.formulario.codigo.value=codigo;
	window.close();
}

function setEmpresaFromCentro(nombre, id) {
	window.opener.document.formulario.idempresa.value=id;
	window.opener.document.formulario.empresa.value=nombre;
	window.close();
}

function setCentro(nombre, id, nombre_empresa, idempresa) {
	window.opener.document.formulario.idempresa.value=idempresa;
	window.opener.document.formulario.empresa.value=nombre_empresa;
	window.opener.document.formulario.idcentro.value=id;
	window.opener.document.formulario.centro.value=nombre;
	window.close();
}

function setDireccion( form ) {
	var documentForm = window.opener.document.formulario;
	if (documentForm.direccion!=null) documentForm.direccion.value=form.direccion.value;
	if (documentForm.poblacion!=null) documentForm.poblacion.value=form.poblacion.value;
	if (documentForm.cp!=null) documentForm.cp.value=form.cp.value;
	if (documentForm.idprovincia!=null) documentForm.idprovincia.value=form.idprovincia.value;
	if (documentForm.pais!=null) documentForm.pais.value=form.pais.value;
	if (documentForm.telefono!=null) documentForm.telefono.value=form.telefono.value;
	if (documentForm.fax!=null) documentForm.fax.value=form.fax.value;
	if (documentForm.direccion_matiz!=null) documentForm.direccion_matiz[1].checked=true;
	window.close();
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function resaltarGuardar() {
	var element = document.getElementById("boton_guardar");
	if (element!=null) {
		element.innerHTML ="* Guardar *";
	}
}

function abrirMail() {
	if (event.ctrlKey) {
		event.returnValue=false;
		window.open('http://www.varaderoweb.com/cgi-bin/sqwebmail');
		return false;
	}
}

function getElement( id ) {
	var element;
	if (document.getElementById==null) {
		element = document.all[id];
	} else {
		element =document.getElementById(id);
	}
	return element;
}

function toggleOcultacion(partner) {
	var table = getElement(partner);
	if (table!=null) {
		if (table.style.display=="none") table.style.display="block";
		else table.style.display="none";
	}
}

function mostrarOcultacion(partner) {
	var table = getElement(partner);
	table.style.display="block";	
}

function ocultarOcultacion(partner) {
	var table = getElement(partner);
	table.style.display="none";	
}

function onchange_data() {
	var f = document.form_q;
	var seccion=f.seccion.options[f.seccion.selectedIndex].value;
	var proveedor=f.proveedor.options[f.proveedor.selectedIndex].value;
	var marca=f.marca.options[f.marca.selectedIndex].value;
	var modelo=f.modelo.value;
	window.location.href="cm_ls_productos.php?seccion="+seccion+"&proveedor="+proveedor+"&marca="+marca+"&modelo="+modelo;
}

function urlencode(ch) {
   ch = escape(ch);
   ch = ch.replace(/[+]/g,"%2B");
   return  ch;
}

	function confirmar(nombre) {
		return confirm("This action will delete this subject and all its messages. Do you want to continue?");
	}

	function confirmarEliminacion() {
		return confirm("¿Está usted seguro que desea eliminar este elemento?");
	}

	function confirmarEliminarMensaje() {
		return confirm("Are you sure to delete the message?");
	}

	function confirmarEliminarTema(numeroMensajes) {
		return confirm("The issue has "+numeroMensajes+" associated messages.\nAre you sure to delete the issue?\nAssociated messages will be lost.");
	}

	function confirmarEliminarSeccion(categoria, nombre, numeroMensajes, numeroSecciones) {
		if (categoria=="Categoria")	{
			return confirm("La Categoria "+ nombre + " tiene "+ numeroSecciones +" subcategorias y "+numeroMensajes+" mensajes asociados.\n¿Desea realmente eliminar la " + categoria + "?\nSe perderán todas las subcategorias y los mensajes asociados");		
		} else {
			return confirm(numeroMensajes + " messages in the subject.\nAre you sure to delete the "+ categoria  +"?\nAssociated messages will be lost.");
		}
	}


/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}
