function make_cbo(cbo,xmlx,val,text,seleccionar){


  var combo = window.document.getElementById(cbo);
  var xml = window.document.getElementById(xmlx);
  combo.innerHTML='';xml.recordset.movefirst();
  var op1 = combo.document.createElement('OPTION');op1.value='';op1.text=seleccionar;
  combo.options.add(op1);
  while(!xml.recordset.eof)
  {
     var op = combo.document.createElement('OPTION');
	 op.value = xml.recordset.Fields(val).value;op.text=xml.recordset.Fields(text).value;
	 combo.options.add(op);xml.recordset.MoveNext();
  }
}


String.prototype.trim = function()
{

    return this.replace(/(^\s*)|(\s*$)/g, "");
}


function ConvToASCII(str)
{
	var arrCode = str.split('#');
	var string = arrCode[0];
	for(var i = 1; i < arrCode.length; i++)
	{
		var f = (arrCode[i].charAt(0) == '0')?1:0;
		if (arrCode[i].length>=3)
		{
			var h = arrCode[i].substring(f,3);
			if ((!(isNaN(h))) && (arrCode[i].substring(0,1)!=" ") && (arrCode[i].substring(1,2)!=" ") && (arrCode[i].substring(2,3)!=" "))
			{
				var charASCII = String.fromCharCode(parseInt(arrCode[i].substring(f,3)));
				string += String(charASCII) + String(arrCode[i].substring(3));
			}
			else
			{
				string += "#" + h + String(arrCode[i].substring(3));
			}
		}
		else
		{
			string += "#"
			if (arrCode[i].length>0)
			{
				string += String(arrCode[i]);
			}
		}
	}

	return string;
}

/*
function getdata(mode,params){
 var dx=new ActiveXObject("Microsoft.XMLHTTP");
 dx.open('POST','../../../data/data.asp?m='+ mode + '&' + params,false);
 dx.setRequestheader('Content-type','text-html');
 dx.send();
 //alert(dx.responseText);
 return ConvToASCII(dx.responseText);
}
*/

function getdata(mode,params,raiz,nodo){
 //alert("mode:    "+mode+" params:    "+params+" raiz:    "+raiz+" nodo:    "+nodo);
 var dx=new ActiveXObject("Microsoft.XMLHTTP");
 dx.open('POST','../../../data/data.asp?m='+ mode + '&' + params,false);
 dx.setRequestheader('Content-type','text-html');
 dx.send();
 var response = dx.responseText;

  if (raiz)
 {
    var nombreRaiz = raiz + " error='0'";
    response = response.replace("data error='0'",nombreRaiz);
    response = response.replace("/data","/" + raiz);
 }

  if (nodo)
    response = response.replace(/<row /g,"<" + nodo + " ");
 return ConvToASCII(response);
}

function savedata(mode,params)
{
 var dxs=new ActiveXObject("Microsoft.XMLHTTP");
 dxs.open('POST','../../../data/savedata.asp?m='+ mode + '&' + params,false);
 dxs.setRequestheader('Content-type','text-html');
 dxs.send();

 return String(dxs.responseText);
}



function ExitDlg(){ if(event.keyCode == 27) window.close(); }



function wOpener(consulta,abrir,abrir_hidden,cols,sep,adicionalesAborrar,functAdicionalEliminar,functAdicionalPresionar)
{
	if(!sep)sep='';
	var str ='<td  colspan="' + cols + '">';
	var reseteado = '';
	if(abrir!=0){
	var vabrir = abrir.split(',');
	for(j=0;j<vabrir.length;j++)
	{
		if(j==(vabrir.length-1)) sep = '';
		var vabrir_show = vabrir[j].split('=');
		str += '<input name="txt_'+vabrir_show[0]+'"size="'+vabrir_show[1]+'"readonly>&nbsp;'+sep+'&nbsp;';
		reseteado+= "txt_" + vabrir_show[0] + ".value = \'\';"
	}
}


 if(abrir_hidden!=0){
   var vabrir_hidden = abrir_hidden.split(',');
   for(i=0;i<vabrir_hidden.length;i++)
   {
   	str += '<input name="txt_'+vabrir_hidden[i]+'"type="hidden">';
   	reseteado+= "txt_" + vabrir_hidden[i]+".value = \'\';"
   }
 }
 if (adicionalesAborrar)
 {
   if (adicionalesAborrar.split(',')[1])
   {
   	var vadicionalesAborrar = adicionalesAborrar.split(',');
	for(j=0;j<vadicionalesAborrar.length;j++)
	{
		reseteado+= vadicionalesAborrar[j] + ".value = \'\';"
	}
   }
   else
   {
   	reseteado+= adicionalesAborrar + ".value = \'\';"
   }
 }
 str += '&nbsp;<button id="btn_' + consulta + '" onclick="';
 if(functAdicionalPresionar)
 {
 	str += functAdicionalPresionar;
 }
 str += 'openData(\''+consulta+'\');">ELEGIR</button>&nbsp;&nbsp;<img id="img_' + consulta + '" ALT="QUITAR LA SELECCIÓN REALIZADA" src="../../../images/x10x10.gif" onmouseover="this.style.cursor=\'hand\';" onclick="' + reseteado;
 if (functAdicionalEliminar)
 {
 	str += functAdicionalEliminar;
 }
// por ahora este help no.
// str += '" width="10" height="10"></img>&nbsp;&nbsp;<img src="../../../images/faq.gif" onmouseover="this.style.cursor=\'hand\';" onClick="help_w();"></img></td>';
 str += '" width="10" height="10"></img></td>';
 document.write(str);
}

function help_w()
{
	window.open("../../../helps/Buscadores.htm",null, "height=600,width=640,status=yes,toolbar=no,menubar=no,location=no,scrollbars=Yes");
}

function print_div_espere()
{
	document.write('<div id="div_espere_fondo" name="div_espere_fondo" style="position:absolute;display:none;"><div id="div_espere" name="div_espere" class="div_espere_style" style="position:absolute;background-color:#FFFFFF;width:160px;height:24px;font-size:11pt;font-family:Verdana;text-align:center;vertical-align:middle;font-weight:900;border-top: #006699 1.0 solid;border-bottom: #006699 1.0 solid;border-left: #006699 1.0 solid;border-right: #006699 1.0 solid;color: #006699;">Espere, por favor.</div></div>');
}

function mostrarEspere(func_a_ejecutar,milisegundos)
{
	div_espere_fondo.style.display="block";
	var izq_c=((self.document.body.clientWidth/2)-75);
	var top_c=((self.document.body.clientHeight/2)-20);
	div_espere_fondo.style.height=self.document.body.clientHeight + "px";
	div_espere_fondo.style.width=self.document.body.clientWidth + "px";
	div_espere_fondo.style.top="0";
	div_espere_fondo.style.left="0";
	div_espere.style.left=(parseInt(izq_c));
	div_espere.style.top=(parseInt(top_c));
	/*
	alert(div_espere_fondo.style.left);
	alert(div_espere_fondo.style.top);
	alert(div_espere.style.left);
	alert(div_espere.style.top);
	*/
        if (func_a_ejecutar)
          {
        	setTimeout(func_a_ejecutar,milisegundos);
          }
        else
          {
          }
}

function ocultarEspere()
{
	div_espere_fondo.style.display="none";
}

function print_div_espere2()
{
	document.write('<div id="div_espere_fondo" name="div_espere_fondo" style="position:absolute;display:none;"><div id="div_espere" name="div_espere" class="div_espere_style" style="position:absolute;background-color:#FFFFFF;width:160px;height:24px;font-size:11pt;font-family:Verdana;text-align:center;vertical-align:middle;font-weight:900;border-top: #006699 1.0 solid;border-bottom: #006699 1.0 solid;border-left: #006699 1.0 solid;border-right: #006699 1.0 solid;color: #006699;">Espere, por favor.</div></div>');
}

function mostrarEspere2(func_a_ejecutar,milisegundos)
{
	div_espere_fondo.style.display="block";
	var izq_c=((self.document.body.clientWidth/2)-75);
	var top_c=((self.document.body.clientHeight/2)-20);
	div_espere_fondo.style.height=self.document.body.clientHeight + "px";
	div_espere_fondo.style.width=self.document.body.clientWidth + "px";
	div_espere_fondo.style.top="0";
	div_espere_fondo.style.left="0";
	div_espere.style.top=(parseInt(top_c));
	div_espere.style.left="0";
	/*
	div_espere.style.left=(parseInt(izq_c));
	div_espere.style.top=(parseInt(top_c));

	alert(div_espere_fondo.style.left);
	alert(div_espere_fondo.style.top);
	alert(div_espere.style.left);
	alert(div_espere.style.top);
	alert(div_espere.style.left);
	alert(div_espere.style.top);
	*/
	setTimeout(func_a_ejecutar,milisegundos);
}

function ocultarEspere2()
{
	div_espere_fondo.style.display="none";
}


function reemplazar(texto, que, por_que, noDistingueCase){
	if (noDistingueCase){
		var re = new RegExp(que, "gi");
	} else {
		var re = new RegExp(que, "g");
	}
	var tex = texto.replace(re, por_que);
  	return tex;
}

function rellenarConCeros(valor,longitud)
{
	if (valor)
		{
			if (valor.length < longitud)
			{
				var tope = parseInt(longitud,10) - parseInt(valor.length,10);
				for (var i=0;i<tope;i++)
					valor = "0" + valor;
			}

			return valor;
		}
	else
		return "";
}

/*
	Función: posicionarCursorEnPos
	Descripción: colocar el cursor en una posición determinada de un input
	Parámetros: id_input: ID del input text al que deseo posicionarle el cursor
				pos: posición deseada (número)
*/
function posicionarCursorEnPos(id_input,pos)
{  
    //si recibo un ID como parámetro y es del tipo conocido
    if(typeof document.selection != 'undefined' && document.selection && id_input)
    {
        //resguardo el contenido (valor)
        var tex = document.getElementById(id_input).value; 
        
        //limpio el valor y coloco el foco en el campo
        document.getElementById(id_input).value = '';  
        document.getElementById(id_input).focus(); 
        
        //creo el rango, asigno el valor y muevo el cursor
        var str = document.selection.createRange();  
        document.getElementById(id_input).value = tex; 
        str.move("character", pos);  
        str.moveEnd("character", 0);  
        str.select(); 
    } 
}  


/*
	Función: Tabulación con ENTER
	Descripción: Permite ir tabulando los distintos elementos mediante la tecla ENTER
*/
function tabularConEnter()
{
	//13: enter - 9: tab
	//si el control donde está el foco no es un botón --> simulo el tab.
	if (window.event.keyCode == 13 && document.activeElement.nodeName != "BUTTON")
	{
		return window.event.keyCode = 9;
	}
}

/*
	Función: obtenerValorRadio
	Descripción: retorna el valor del radio seleccionado
	Parámetros: campo: ID del campo html del cual se deben obtener los datos. 
*/
function obtenerValorRadio(campo)
{
	if (!campo)
		return;
		
	var valor = null;
	
	for (var i=0; i < document.getElementsByName(campo).length; i++)
	{
		if (document.getElementsByName(campo)[i].checked)
			valor = document.getElementsByName(campo)[i].value;
	}
	
	return valor;
}

/*
	Funcion para devolver un resultado con decimales.
	Ej: n=123244.3457  aplicado a n.decimal(2)  --> 123244.34
*/
Number.prototype.decimal = function(n)
{
	pot = Math.pow(10, parseInt(n));
	return parseInt(this * pot) / pot;
}


/*
	Funcion para realizar calculos de resta, suma, multiplicación y división, 
	parametros (operacion,num1,num2)
	operacion =  la operacion matematica a llevar a cabo. Por ahora cubre *, + y -
	num1 = el primer numero
	num2 = el segundo numero
	Ej: operacion= *  num1 = 321651.654  num2 = 123244.34
*/

function operarTerminos(operacion,num1,num2)
{
	//descomponemos los números
	
	num1 = num1.toString().replace(',','.');
	num2 = num2.toString().replace(',','.');
	var ent1,ent2,dec1,dec2;
	var entero,decimal;
	ent1 = parseInt(num1,10);
	ent2 = parseInt(num2,10);
	
	dec1 = 0;
	dec2 = 0;
	
	
	if ( num1.toString().indexOf(".") != -1 )
	{
		dec1 = num1.toString().substring(num1.toString().indexOf(".")+1);
		if (dec1.toString().length == 1)
			dec1 = dec1.toString() + "0";

		dec1 = parseInt(dec1,10);
	}
	if ( num2.toString().indexOf(".") != -1 )
	{
		dec2 = num2.toString().substring(num2.toString().indexOf(".")+1);
		if (dec2.toString().length == 1)
			dec2 = dec2.toString() + "0";

		dec2 = parseInt(dec2,10);
	}
	
	switch(operacion)
	{
		case "+":
			//sumamos las partes decimales. Si es > 100, dejo como resultado total-100 y sumo 1 en la parte entera
			entero = ent1+ent2;

			decimal = dec1+dec2;
			if (decimal > 99)
			{
				entero++;
				decimal = decimal - 100;
			}
		break;

		case "-":
			entero = ent1-ent2;

			decimal = dec1-dec2;
			if (decimal < 0)
			{
				entero--;
				decimal = 100 + decimal;
			}
		break;
		case "*":
			entero = ent1*ent2;
			if (dec1 == 0 && dec2 == 0)
			{

			}
			else
			{
				if(dec1 == 0)
					dec1 = 1;
				if(dec2 == 0)
					dec2 == 1;
			}
			decimal = dec1*ent2;
			while (decimal > 99)
			{
				entero++;
				decimal = decimal - 100;
			}
		break;
		case "/":
				var redondeo = 0;
				//tengo la primera parte del nro.
				entero = ent1/ent2;
				// tengo el resto de la division
				var resto = ent1%ent2;
				if(resto != 0)
					redondeo = resto*10;
					
				dec1 = dec1+redondeo;
				decimal = dec1/ent2;
				entero = parseInt(entero,10);
				decimal = parseInt(decimal,10);
		break;
		
		
		
	}
	if(operacion != '/')
	{
		if (decimal < 10)
			decimal = "0" + decimal.toString();
	}
	return parseFloat(entero.toString() + "." + decimal.toString());
}

