/*
esta funcion puede devolver:
es
*/
function getLanguage() {
	var res = navigator.userLanguage;
	//IE
	if (res==null) res = navigator.systemLanguage;
	//IE
	if (res==null) res = navigator.browserLanguage;
	//MOZILLA
	if (res==null) res = navigator.language;
	return res;
}

function getMainLanguage() {
	var res="EN";
	var lang = getLanguage();
	if (lang==null) lang ="en";
	if (lang.length>=2) {
		res = lang.substring(0,2);
	}
	res = res.toLowerCase();
	return res;
}

function getIdFromLanguage(lang) {
	var res = 1;
	if (lang=="es") res=1;
	if (lang=="en") res=2;
	if (lang=="ca") res=3;
	if (lang=="de") res=4;
	if (lang=="pt") res=5;
	if (lang=="it") res=6;
	if (lang=="fr") res=7;
	return res;
}

function abrirLang() {
	//alert(navigator.browserLanguage +";" + navigator.systemLanguage +";" + navigator.userLanguage );
	var lang = getMainLanguage();
	var idLang = getIdFromLanguage(lang);
	window.location="/view/default.aspx?lang="+idLang;
}

// Funciones para establecer Cookies mediante JS 
function FixCookieDate (date) {
	var base = new Date(0);
	var skew = base.getTime(); // dawn of (Unix) time - should be 0
	if (skew > 0) // Except on the Mac - ahead of its time
	date.setTime (date.getTime() - skew);
}

function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
	var j = i + alen;
	if (document.cookie.substring(i, j) == arg)
	return getCookieVal (j);
	i = document.cookie.indexOf(" ", i) + 1;
	if (i == 0) break; 
	}
	return null;
}

// ********************* 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 getXml(url) {
    var xmlDoc;
	try {
	    xmlDoc = new ActiveXObject("MSXML.DOMDocument")
	} catch (e) {
		xmlDoc = new ActiveXObject("MSXML2.DOMDocument")
	}
    //xmlDoc.async=false;

    //xmlDoc.load(url);
	var nodo = getElement("xmlisland");
	nodo.src=url;

    return xmlDoc;
}

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 = 700;
	if (alto==null) stralto = 575;
	window.open(url,"","resizable=yes,status=1,scrollbars=1,width="+strancho+",height="+stralto,false);
}

function openWindowAdmin(url, ancho, alto) {
	strancho=ancho;
	stralto=alto;
	if (ancho==null) strancho = 700;
	if (alto==null) stralto = 575;
	window.open(url,"ADMINISTRADOR","resizable=yes,status=1,scrollbars=1,width="+strancho+",height="+stralto,false);
}

function openWindowAdmin2(url, ancho, alto) {
	strancho=ancho;
	stralto=alto;
	if (ancho==null) strancho = 700;
	if (alto==null) stralto = 575;	
	if (window.opener.top!=null) {
		window.opener.top.open(url,"_self","resizable=yes,status=1,scrollbars=1,width="+strancho+",height="+stralto,false);
	} else {
		window.open(url,"ADMINISTRADOR","resizable=yes,status=1,scrollbars=1,width="+strancho+",height="+stralto,false);
	}
}

function esVacio(cadena) {
	if (trim(cadena)=="") return true;
	else return false;
}

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 popUp(URL) {
	day = new Date();
	id = day.getTime();
	window.open("" + URL + "", "" + id + "", "width=780, height=400, scrollbars=yes, menubar=no, location=no, resizable=yes");
}


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, ndoc) {
	var element;
	if (ndoc==null) doc=document;
	else doc=ndoc;
	if (doc.getElementById==null) {
		element = doc.all[id];
	} else {
		element =doc.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 getElementDoc( ndoc, id) {
	var doc;
	doc = ndoc;
	if (doc==null) doc = document;
	var element;
	if (doc.getElementById==null) {
		element = doc.all[id];
	} else {
		element =doc.getElementById(id);
	}
	return element;
}

/** Para mostrar n elementos */
	var idCampos=2;

function nuevoCampo(nodo,strTitle,nuevoTexto ) {
	var res = nuevoTexto;
	var re;
	re = new RegExp('<([^>]*='+strTitle+'_)([0-9]*)','g'); 
	res = res.replace(re, '<$1'+idCampos);
	re = new RegExp('<([^>]*="'+strTitle+'_)([0-9]*)','g'); 
	res = res.replace(re, '<$1'+idCampos);

	re = new RegExp('Campo ([0-9]*):','g'); 
	res = res.replace(re, 'Campo '+idCampos);
	
	var strSpan = nodo.id.substring(0, nodo.id.length-4 ) + "span";
	var span = getElement( strSpan );
	elem = document.createElement("div");
	elem.id = strTitle+"_" + idCampos+ "_span";
	idCampos++; 
	elem.innerHTML = res;
	//alert(res);
	span.parentNode.appendChild( elem );
	return idCampos-1;
}

function eliminarCampo(nodo) {
	var nodoPadre = getPadreConId(nodo);
	if (confirm('¿Seguro que desea eliminar este elemento?')) {
		var nodo = nodoPadre;
		nodo.parentNode.removeChild( nodo );		
	}
	return false;
}

	function getIdentificadorPadre(nodo) {
		var nodoPadre = getPadreConId(nodo);
		var res=nodoPadre.id;
		return res;
	}

	function getIdentificadorPadreIndice(nodo) {
		var nodoPadre = getPadreConId(nodo);
		var v = nodoPadre.id.split("_");
		var res="";
		res = v[1];
		return res;
	}

	function getPadreConId(nodo) {
		var res=null;
		//alert(nodo);
		if (nodo.id!=null) {
			if (nodo.id!="") {
				res = nodo;
			}
		}
		if (res==null) res = getPadreConId(nodo.parentNode);
		return res;
	}

	function eliminarCampo(nodo,strTitle) {
		var nodoPadre = getPadreConId(nodo);
		if (confirm('¿Seguro que desea eliminar este elemento?')) {
			var nodo = nodoPadre;
			nodo.parentNode.removeChild( nodo );		
		}
		return false;
	}

function construyeQuery( url, nombre, valor) {
	var res, resini;
	//var re = new RegExp('(?|&)'+nombre+'=[^&]*','g');
	try
	{
		var v1 = url.split('?');
		var res="";
		var v2 = v1[1].split('&');
		var i;
		inicio = false;
		for (i=0;i<v2.length;i++) {
			var v3 = v2[i].split("=");
			if (res!="") {
			} else {
				res = v1[0]+"?";
				resini=res;
				inicio = true;
			}
			if ( (v3[0]!=nombre) &&(v3[0]!="") ) {
				if (!inicio) {
					res +="&";
				} else {
					inicio = false;
				}
				res += v3[0]+"="+v3[1];
			} else {
			}
		}
		if (res==resini) {
			res += nombre + "=" + valor;
		} else {
			res += "&" + nombre + "=" + valor;
		}
	}
	catch (e)
	{
		res = url+"?"+nombre+"="+valor;
	}
	return res;
}

function aniadirQuery(url, resto) {
	var res;
	if (url.indexOf('?')==-1) {
		res = url +'?'+resto;
	} else {
		res = url +'&'+resto;
	}
	return res;
}


function construyeUrlMenosMas(url,lista,itemsMas) {
	var res,urlBase,resto,w,x,i,atributo,valor,nresto;
	var v = url.split("?");
	if (v.length==1) {
		res = url+"?"+itemsMas;
	} else {
		nresto="";
		urlBase = v[0];
		resto = v[1];
		w = resto.split("&");
		for (i=0;i<w.length;i++) {
			if (w[i]!=""){
				x = w[i].split("=");
				atributo = x[0];
				valor="";
				if (x.length>0) valor = x[1];
				if (valor!="") {
					if (!perteneceLista(atributo,lista)) {
						nresto = nresto + "&" + atributo + "=" + valor;
					}
				}
			}
		}
		nresto += "&" + itemsMas;
		if (nresto!="") nresto=nresto.substring(1);
		res = urlBase + "?" + nresto;
	}
	return res;
}


function selectAll(f, valor) {
	var res, patron;
	res="";
	patron="set_";
	for(i=0;i<f.elements.length;i++) {	
		var campo = f[i];
		if (campo!=null) {
			var nombreCampo = campo.name;
			if (nombreCampo.substring(0, patron.length)==patron) {
				campo.checked = valor;
			}
		}
	}
}

function selectAllActivos(f, valor) {
	var res, patron;
	res="";
	patron="set_";
	for(i=0;i<f.elements.length;i++) {	
		var campo = f[i];
		if (campo!=null) {
			if( !campo.disabled ){
				var nombreCampo = campo.name;
				if (nombreCampo.substring(0, patron.length)==patron) {
					campo.checked = valor;
				}
			}
		}
	}
}

function selectAlguno(f) {
	var res, patron, isSelect;
	res = "";
	patron = "set_";
	isSelect = false;
	for(i=0;i<f.elements.length;i++) {	
		var campo = f[i];
		if (campo!=null) {
			var nombreCampo = campo.name;
			if (nombreCampo.substring(0, patron.length)==patron) {
				isSelect = isSelect || campo.checked;
			}
		}
	}
	return isSelect;
}

function selectInversa(f) {
	var res, patron;
	res="";
	patron="set_";
	for(i=0;i<f.elements.length;i++) {	
		var campo = f[i];
		if (campo!=null) {
			var nombreCampo = campo.name;
			if (nombreCampo.substring(0, patron.length)==patron) {
				campo.checked = !campo.checked;
			}
		}
	}
}


function copiarValidaciones(texto) {
    clipboardData.setData( "Text", "VALIDACIONES="+texto );
}

function pegarValidaciones() {
	var texto,patron,numero,patron,patron2;
	texto = clipboardData.getData("Text");
	patron="VALIDACIONES=";
	if ((texto.substring(0, patron.length )==patron) ) {
		if (confirm("¿Seguro que desea pegar las validaciones?")) {
			var f = document.formulario;
			texto = texto.substring(patron.length);
			patron="campo_";
			patron2="_href";
			numero="1";
			for (i=0;i<f.all.length;i++) {
				nombre = f.all[i].id;
				if ((nombre.substring(0, patron.length )==patron) ) {					
					if ((nombre.substring(nombre.length - patron2.length )==patron2) ) {
						numero = nombre.substring(patron.length, nombre.length - patron2.length  );
					}
				}
			}
			numero = parseInt(numero);
			if (f["campo_"+ (numero) +""].value==null) {

			} else if (f["campo_"+ (numero) +""].value=="") {
				

			} else {				
				document.getElementById("campo_"+ (numero) +"_href").fireEvent("onclick");
				numero++;
			}
			alert(numero);
			v = texto.split(";");
			for (i=0;i<v.length;i++) {
				v2 = v[i].split(":");
				//alert(f["campo_"+ (i+numero) +"_surtido"]);
				f["campo_"+ (i+numero) +"_surtido"].value = v2[0];
				f["campo_"+ (i+numero) +""].value = v2[1];
				f["campo_"+ (i+numero) +"_tipo"].value = v2[2];
				f["campo_"+ (i+numero) +"_incidencias"].value = v2[3];
				if (i!=v.length-1) {
					document.getElementById("campo_"+ (i+numero) +"_href").fireEvent("onclick");
				}
			}
			
		}
	} else {
		alert('No hay validaciones que pegar');
	}
}



	function recargaProvinciasPais() {
		var i;
		var f = document.formulario;
		if (f.pais.value!="") {
			var url = "xml_get_provincias.asp?pais="+f.pais.value;
			var dom = getXml(url);
			var nodes = dom.selectNodes("//provincia");
			var res="";
			res = res + "<OPTION VALUE=''>--- Seleccione provincia ---</OPTION>\n";
			for (i=0;i<nodes.length ;i++) {
				var nodo = nodes.item(i);
				res = res + "<OPTION VALUE='"+nodo.getAttribute("id")+"'>"+nodo.getAttribute("id")+"|"+nodo.getAttribute("nombre")+"</OPTION>\n";
			}
			f.provincia.outerHTML = "<select class='inputclass' name='provincia' onchange='recargaMunicipiosProvincia()'>"+res+"</select>";
			res = "<OPTION VALUE=''>--- Seleccione municipio ---</OPTION>\n";
			f.municipio.outerHTML = "<select class='inputclass' name='municipio'>"+res+"</select>";
		}
	}

	function recargaMunicipiosProvincia() {
		var i;
		var f = document.formulario;
		var res="";
		res = res + "<OPTION VALUE=''>--- Seleccione municipio ---</OPTION>\n";
		if (f.provincia.value!="") {
			var url = "xml_get_localidades.asp?provincia="+f.provincia.value;
			var dom = getXml(url);
			var nodes = dom.selectNodes("//localidad");			
			for (i=0;i<nodes.length ;i++) {
				var nodo = nodes.item(i);
				res = res + "<OPTION VALUE='"+nodo.getAttribute("id")+"'>"+nodo.getAttribute("id")+"|"+nodo.getAttribute("nombre")+"</OPTION>\n";
			}			
		} else {

		}
		f.municipio.outerHTML = "<select class='inputclass' name='municipio'>"+res+"</select>";
	}

	function recargaInspectores() {
		var i;
		var f = document.formulario;
		var res="";
		res = res + "<OPTION VALUE=''>--- Seleccione inspector ---</OPTION>\n";
		if (f.codcoord.value!="") {
			var url = "xml_get_inspectores.asp?codcoord="+f.codcoord.value;
			var dom = getXml(url);
			var nodes = dom.selectNodes("//inspector");			
			for (i=0;i<nodes.length ;i++) {
				var nodo = nodes.item(i);
				res = res + "<OPTION VALUE='"+nodo.getAttribute("id")+"'>"+nodo.getAttribute("id")+"|"+nodo.getAttribute("nombre")+"</OPTION>\n";
			}
		} else {

		}
		f.codinspector.outerHTML = "<select class='inputclass' name='codinspector'>"+res+"</select>";
	}

	function recargaInspectoresMuestras() {
		var i;
		var f = document.formulario;
		var res="";
		res = res + "<OPTION VALUE=''>--- Seleccione inspector ---</OPTION>\n";
		if (f.codcoord.value!="") {
			var url = "xml_get_inspectores.asp?codcoord="+f.codcoord.value;
			var dom = getXml(url);
			var nodes = dom.selectNodes("//inspector");			
			for (i=0;i<nodes.length ;i++) {
				var nodo = nodes.item(i);
				res = res + "<OPTION VALUE='"+nodo.getAttribute("id")+"'>"+nodo.getAttribute("id")+"|"+nodo.getAttribute("nombre")+"</OPTION>\n";
			}
		} else {

		}
		//Recorro todos los elementos que sean inspectores
		patron="codinspector_muestra_";
		for (i=0;i<f.length;i++) {
			if ((f[i].name.substring(0, patron.length )==patron) ) {
				f[i].outerHTML = "<select class='inputclass' name='"+ f[i].name +"' style='display:"+f[i].style.display+"'>"+res+"</select>";
				
			}
		}	
		
	}



	function moverElemento( elemento, valor ) {
		if (valor<0) {			
			var prev = elemento.previousSibling;
			if (prev!=null) {			
				var texto = elemento.innerHTML;
				elemento.innerHTML = prev.innerHTML;
				prev.innerHTML = texto;
			}
		} else {
			var next = elemento.nextSibling;
			if (next!=null) {
				var texto = elemento.innerHTML;
				elemento.innerHTML = next.innerHTML;
				next.innerHTML = texto;
			}
		}
		
	}

	function rellena_orden() {
		var filas, i, patron, nombre, res,f;
		var fila = getElement("datos_orden");
		patron="orden_";
		res="";
		filas = fila.children;	
		for (i=0;i<filas.length ;i++ ) {
			try {
				nombre = filas[i].children[0].id;
				if ((nombre.substring(0, patron.length )==patron) ) {	
					res = res + "," + nombre.substring(patron.length);
				}
			}
			catch (e)
			{
			}
		}
		if (res!="") res = res.substring(1);
		f = document.formulario;
		f.orden.value=res;
	}


function eliminarIncidencia(strid) {
	var nodo = getElement("mostrar_relacionadas_"+strid);
	nodo.outerHTML="";

	var valorPrevio = document.formulario.relacionadas.value;
	var v = valorPrevio.split(",");
	var res="";
	var i;
	for (i=0;i<v.length;i++) {
		if (v[i]!="") {
			if (v[i]!=strid) {
				res = res +","+v[i];
			}
		}
	}
	if (res!="") res=res.substring(1);
	document.formulario.relacionadas.value = res;
}


function sinImagen(imagen) {
	var nodo;
	nodo = getElement("ubicacion_"+imagen);
	if (nodo!=null) nodo.innerHTML="-- Sin imágen --";
	nodo = getElement("imagen_"+imagen);
	if (nodo!=null) nodo.src="img/punto_claro.gif";
	nodo = getElement(imagen+"_bancoimagenes");
	if (nodo!=null) nodo.value="-1";
		
}

function sinDocumento(doc) {
	var nodo;
	nodo = getElement("ubicacion_"+doc);
	if (nodo!=null) {
		nodo.innerHTML="-- Sin documento --";
		nodo.href="#"
	}
	nodo = getElement(doc+"_eliminar");
	if (nodo!=null) nodo.value="S";		
}

function DateAddDays( date, numDays ) {
	res = date;
	res.setDate(res.getDate() + numDays );
	return res;
}

function bbdd2date( datebbdd ) {
	var valor = datebbdd;
	res = new Date();
	strDay=eliminaCerosNoSignificativos( valor.substring(6,8) );
	strMonth=eliminaCerosNoSignificativos( valor.substring(4,6) );
	strYr=eliminaCerosNoSignificativos( valor.substring(0,4) );
//	alert(bbdd2date + ":" + strMonth);

	day=parseInt(strDay);
	month=parseInt(strMonth);
//	alert("bbdd2date. parseInt :"+strMonth + ";" + month);
	year=parseInt(strYr);
	res.setDate( day );
	res.setMonth( month );
	res.setFullYear( year );

//	alert(valor+"-"+day+"-"+strMonth+"-"+year);

//	alert("bbdd2date:" + datebbdd + " ; " + res );
	return res;
}

function date2bbdd( date ) {
	var res="";
	res = "" + date.getFullYear() + dosCeros( date.getMonth() ) + dosCeros( date.getDate() );
	return res;
}

function date2bbddAncillares( date ) {
	var res="";
	res = "" + date.getFullYear() + dosCeros( date.getMonth()+1 ) + dosCeros( date.getDate() );
	return res;
}

function bbdd2str( datebbdd ) {
	var res="";
	valor=datebbdd;
	if (valor.length>=8) {
		res = valor.substring(6,8) + "/" + valor.substring(4,6) + "/" + valor.substring(0,4);
	}
	return res;
}

function date2str( date ) {
	var res="";
	res = "" +  dosCeros( date.getDate() ) +"/"+ dosCeros( date.getMonth()+1 ) +"/"+  date.getFullYear();
	return res;
}

/* Dada una cadena como 1/1/05 te devuelve 20050101 */
function str2bbdd( valor) {
	var dia="", mes="", anio="", path;
	if (valor=="") return "";
	path = valor.split("/");
	if (path.length==1) path = valor.split("-");
	if (path.length>=1) dia=path[0];
	if (path.length>=2) mes=path[1];
	if (path.length>=3) anio=path[2];
	//Fuerza un año empezando por digito>0
	if (anio.length < 4 ) {
		res = numDigitos(anio,4) + dosCeros(mes) + dosCeros(dia);
		res = "2"+res.substring(1);
	} else {
		res = numDigitos(anio,4) + dosCeros(mes) + dosCeros(dia);
	}
	return res;
}

/*Dada una fecha 'aaaammdd' te dice si es valida o no*/
function compruebaFecha(strFecha) {
	var res=false;
	var mmddaaaa = strFecha.substring(4,6) +"/"+ strFecha.substring(6,8) +"/"+ strFecha.substring(0,4);	
	if (isDate(mmddaaaa)) {
		res=true;
	} else {

	}
	return res;
}

/*Dada una fecha 'dd/mm/aaaa' te dice si es valida o no*/
function compruebaFecha2(strFecha) {
	var res=false;
	if ( strFecha.length==10 )
	{
		var mmddaaaa = strFecha.substring(3,5) +"/"+ strFecha.substring(0,2) +"/"+ strFecha.substring(6,10);	
		if (isDate(mmddaaaa)) {
			res=true;
		} else {

		}
	}
	return res;
}

function numDigitos(numero, size) {
	var i;
	var res=numero;
	for (i=0;i<size;i++) res="0"+res;
	return right(res,size);
}

function right(cadena, size) {
	res = cadena.substring(cadena.length-size);
	return res;
}

function eliminaCerosNoSignificativos( cadena ) {
	var res;
	if (cadena=="") res="";
	else if (cadena.substring(0,1)=="0") res = eliminaCerosNoSignificativos( cadena.substring(1) );
	else res = cadena;
	return res;
}

function dosCeros(numero) {		
	return numDigitos(numero,2);
}

/*** FUNCIONES DE FECHA ***/
/*** FUNCIONES DE FECHA ***/
/*** FUNCIONES DE FECHA ***/

var dtCh= "/";
var minYear=1000;
var maxYear=3000;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function isInteger2(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "1") || (c > "5"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

function p(i) {
	var nodo = getElement("progreso");
	nodo.innerHTML=i;
}

function getValor(item) {
		var res;
		res="";
		if (item!=null) {
			if (item.value==null) {
				for (i=0;i<item.length;i++) {
						if (item[i].checked==true) {
								res=item[i].value;
						}
				}
			} else {
					res = item.value;
			}
		}
		return res;
}

// COOKIES
// Funciones para establecer Cookies mediante JS 
function FixCookieDate (date) {
	var base = new Date(0);
	var skew = base.getTime(); // dawn of (Unix) time - should be 0
	if (skew > 0) // Except on the Mac - ahead of its time
	date.setTime (date.getTime() - skew);
}

function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
	var j = i + alen;
	if (document.cookie.substring(i, j) == arg)
	return getCookieVal (j);
	i = document.cookie.indexOf(" ", i) + 1;
	if (i == 0) break; 
	}
	return null;
}

//Horas.Minutos

//Formato: HH.MM  (17.30)
function esMayor(hora1, hora2){
	var h1 = parseInt(hora1.split(".")[0]);
	var m1 = parseInt(hora1.split(".")[1]);
	var h2 = parseInt(hora2.split(".")[0]);
	var m2 = parseInt(hora2.split(".")[1]);
	//alert(h1+"."+m1+" > "+h2+"."+m2);
	var res = false;
	if (h1>h2){
		//alert(h1+" > "+h2);
		res = true;
	}else if(h1==h2){
		if (m1>m2){
			//alert(h1+" = "+h2+" & "+m1+" > "+m2);
			res = true;
		}
	}
	return res;
}

function esMayorDif(hora1, hora2, diferencia){
	var h2 = parseInt(hora2.split(".")[0]);
	var m2 = parseInt(hora2.split(".")[1]);
	var dif1 = parseInt(diferencia.split(".")[0]);
	var dif2 = parseInt(diferencia.split(".")[1]);
	h2 = h2 + dif1;
	m2 = m2 + dif2;
	while(m2>=60){
		h2 = h2 + 1;
		m2 = m2 - 60;
	}
	//alert(hora1+" > "+h2+"."+m2)
	return esMayor(hora1, h2+"."+m2);
}