function carga(rellena,lista,cuery,campos){
campo0=campo1=campo2=campo3=campo4="";
cadena=campos.split("&"); 
for (var i=0;i<cadena.length;i++){
   switch (i){ 
      case 0:
	     campo0=cadena[i];
		 break;
	  case 1:
	     campo1=cadena[i]; 
	     break;
	  case 2: 
	     campo2=cadena[i]; 
	     break;
	  case 3:
	     campo3=cadena[i]; 
	     break;
	  case 4: 
	     campo4=cadena[i]; 
	     break;
   }
}                               

if(campo0!=0){
   ajax=nuevoAjax();
   ajax.open("GET", "servercarga.php?cuery="+cuery+'&lista='+lista+'&campo0='+campo0+'&campo1='+campo1+'&campo2='+campo2+'&campo3='+campo3+'&campo4='+campo4, true);
   ajax.onreadystatechange=function(){ 
      if (ajax.readyState==1){
		 combo=document.getElementById(lista);
         combo.length=0;
         var nuevaOpcion=document.createElement("option"); 
         nuevaOpcion.value=0; 
		 nuevaOpcion.innerHTML="Cargando...";
		 combo.appendChild(nuevaOpcion); 
		 // combo.disabled=true;	
	  }
	  if (ajax.readyState==4){ 
         //alert(ajax.responseText);
         document.getElementById(rellena).innerHTML=ajax.responseText;
	  } 
   }  
   ajax.send(null);
}
}

function validavacio(formulario,campos,valida){
   cadena=campos.split("&");
   for (var i=0;i<cadena.length;i++){
      if (eval('document.'+formulario+'.'+cadena[i]+'.value')=='' || eval('document.'+formulario+'.'+cadena[i]+'.value')==0){
         alert('Debe completar el siguiente campo con datos')
         eval('document.'+formulario+'.'+cadena[i]+'.focus()');
         return false;
	  };
   }
   if (valida!=""){
      return eval(valida);
   }else{
      return true;  
   }
}

function mover(contenedor,campo,separador){
   if (document.getElementById(contenedor).value==''){
      document.getElementById(contenedor).value=document.getElementById(campo).value;
   }else{
      document.getElementById(contenedor).value=document.getElementById(contenedor).value+separador+document.getElementById(campo).value;
   }
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function numerodecimal(valor) { 
if (/^\d+\.?\d*$/.test(valor.value)){
   valor.value=redondeo(valor.value,2);
}else{
   valor.focus();}
return 
} 

function formatodecimal(fld, milSep, decSep, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;  // Enter
key = String.fromCharCode(whichCode);  // Get key value from key code
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '';
if (len == 1) fld.value = '0'+ decSep + '0' + aux;
if (len == 2) fld.value = '0'+ decSep + aux;
if (len > 2) {
aux2 = '';
for (j = 0, i = len - 3; i >= 0; i--) {
if (j == 3) {
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
fld.value = '';
len2 = aux2.length;
for (i = len2 - 1; i >= 0; i--)
   fld.value += aux2.charAt(i);
   fld.value += decSep + aux.substr(len - 2, len);
}
return false;
}

//function redondeo(value, precision){
//value = "" + value //convert value to string
//precision = parseInt(precision);

//var whole = "" + Math.round(value * Math.pow(10, precision));
//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 redondeo(cantidad, decimales) {
   var cantidad = parseFloat(cantidad);
   var decimales = parseFloat(decimales);
   decimales = (!decimales ? 2 : decimales);
return Math.round(cantidad * Math.pow(10, decimales)) / Math.pow(10, decimales);
}


function IsNumeric(valor) 
{ 
var log=valor.length; var sw="S"; 
for (x=0; x<log; x++) 
{ v1=valor.substr(x,1); 
v2 = parseInt(v1); 
//Compruebo si es un valor numérico 
if (isNaN(v2)) { sw= "N";} 
} 
if (sw=="S") {return true;} else {return false; } 
} 

var primerslap=false; 
var segundoslap=false; 
function formateafecha(fecha)
{ 
var long = fecha.length; 
var dia; 
var mes; 
var ano; 

if ((long>=2) && (primerslap==false)) { dia=fecha.substr(0,2); 
if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) { fecha=fecha.substr(0,2)+"/"+fecha.substr(3,7); primerslap=true; } 
else { fecha=""; primerslap=false;} 
} 
else 
{ dia=fecha.substr(0,1); 
if (IsNumeric(dia)==false) 
{fecha="";} 
if ((long<=2) && (primerslap=true)) {fecha=fecha.substr(0,1); primerslap=false; } 
} 
if ((long>=5) && (segundoslap==false)) 
{ mes=fecha.substr(3,2); 
if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) { fecha=fecha.substr(0,5)+"/"+fecha.substr(6,4); segundoslap=true; } 
else { fecha=fecha.substr(0,3);; segundoslap=false;} 
} 
else { if ((long<=5) && (segundoslap=true)) { fecha=fecha.substr(0,4); segundoslap=false; } } 
if (long>=7) 
{ ano=fecha.substr(6,4); 
if (IsNumeric(ano)==false) { fecha=fecha.substr(0,6); } 
else { if (long==10){ if ((ano==0) || (ano<1900) || (ano>2100)) { fecha=fecha.substr(0,6); } } } 
} 

if (long>=10) 
{ 
fecha=fecha.substr(0,10); 
dia=fecha.substr(0,2); 
mes=fecha.substr(3,2); 
ano=fecha.substr(6,4); 
// Año no viciesto y es febrero y el dia es mayor a 28 
if ( (ano%4 != 0) && (mes ==02) && (dia > 28) ) { fecha=fecha.substr(0,2)+"/"; } 
} 
return (fecha); 
} 

function AbrirSinScroll( url ) {
  web = url
  alto = 200
  ancho = 300
  izq = (screen.width - ancho) / 2
  arr = ((screen.height - alto) / 2) - 15
  popupWin = window.open(web, "_blank", "scroll='yes',width=" + ancho + ",height=" + alto + ",top=" + arr + ",left=" + izq)
}

function ventana()
{
open("graba_ingreso.php","grabar","width=300,height=350,scrollbars=no,toolbar=no");
}

function nuevoAjax() {
	if (window.XMLHttpRequest)
		oAjax = new XMLHttpRequest();
	else if (window.ActiveXObject)
		oAjax = new ActiveXObject("Microsoft.XMLHTTP");
	if (oAjax) return(oAjax);
}

function nuevoAjaxx()
{ 
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false; 
	try 
	{ 
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			// Creacion del objet AJAX para IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); } 

	return xmlhttp; 
}

function queradio(ctrl){
   for(i=0;i<ctrl.length;i++)
      if(ctrl[i].checked) return ctrl[i].value;
   return ('');
}

function verificador(numero){
   var auxiliar='1';
   var suma=0;
   var loop=(numero.length/4)+1;
   for(i=0;i<loop;i++)
      auxiliar=auxiliar+'3579';
   for(i=0;i<numero.length;i++)
      suma=suma+(numero.charAt(i)*auxiliar.charAt(i));
   suma=String(parseInt(suma/2));
   return suma.charAt(suma.length-1);
}

function diferenciafechas (CadenaFecha1,CadenaFecha2) {

   //Obtiene dia, mes y año
   var fecha1 = new separadorfecha( CadenaFecha1 )
   var fecha2 = new separadorfecha( CadenaFecha2 )

   //Obtiene objetos Date
   var miFecha1 = new Date( fecha1.anio, fecha1.mes, fecha1.dia )
   var miFecha2 = new Date( fecha2.anio, fecha2.mes, fecha2.dia )

   //Resta fechas y redondea
   var diferencia = miFecha1.getTime() - miFecha2.getTime()
   var dias = Math.floor(diferencia / (1000 * 60 * 60 * 24))
   var segundos = Math.floor(diferencia / 1000)
   //alert ('La diferencia es de ' + dias + ' dias,\no ' + segundos + ' segundos.')

   return dias
}

function separadorfecha( cadena ) {

   //Separador para la introduccion de las fechas
   var separador = "/"

   //Separa por dia, mes y año
   if ( cadena.indexOf( separador ) != -1 ) {
        var posi1 = 0
        var posi2 = cadena.indexOf( separador, posi1 + 1 )
        var posi3 = cadena.indexOf( separador, posi2 + 1 )
        this.dia = cadena.substring( posi1, posi2 )
        this.mes = cadena.substring( posi2 + 1, posi3 )
        this.anio = cadena.substring( posi3 + 1, cadena.length )
   } else {
        this.dia = 0
        this.mes = 0
        this.anio = 0
   }
}

function ceros(campo, longitud){
var diferencia = longitud - campo.length;
var resultado = "";
if (diferencia > 0 && diferencia < longitud){
   for(i=0; i<diferencia; i++){
      resultado = resultado + "0";
   }
   return (resultado + campo);
}
}


