// JavaScript Document

function toInt(num, maximo) {
  aux=parseInt(num);
  if(aux>maximo) {
   aux=maximo;	  
  } 
  return(aux);
}

function strToUpper(ascii) {
	alert(ascii);
  if(ascii>=97 && ascii<=122) {
    ascii=ascii-0x20;;	  
  }
  alert(ascii);
 return(ascii);
}

function validarSelectNoSel(validacionSelectNoSel, func) {
 var doAction = true;
  for(i=0;i<validacionSelectNoSel.length;i++) {
	  e=document.getElementById(validacionSelectNoSel[i]);
	  if(e.value==0) {
		 alert("Falta seleccionar el campo [" + e.tag + "]") ;
		 doAction=false;
		 e.focus();
		 break;
	 }
  }
  if(doAction)
    return true;
}

/***************************************************************************************************
*Nombre: validarCamposBlank
*Obj: verfica que los campos del formulario no sean nulos, retorna boolean
*$predecesor: todos los formularios que lo llamen
***************************************************************************************************/
function validarCamposBlank(validacionCamposBlank, func) {
	
// Requiere vector de validacion [validacionCamposBlank] donde estan los id de los campos que no pueden pasar vacios
// Ejemplo : 
//           var validacionCamposBlank = new Array("idcampo1","idcampo2","idcampo3");
//           validarCamposBlank(validacionCamposBlank);
	
 var doAction = true;
  for(i=0;i<validacionCamposBlank.length;i++) {
	  e=document.getElementById(validacionCamposBlank[i]);
	  if(e.value.length<=0) {
		 alert("Falta llenar el campo " + e.tag + "") ;
		 doAction=false;
         eval(func); 
		 e.focus();
		 break;
	 }
  }
  if(doAction)
	return true;
}

/***************************************************************************************************
*Nombre: validarCamposIguales
*Obj: verifica que dos campos de un mismo formulario sean iguales, Ejm: password, retorna boolean
*$predecesor: todos los formularios que lo llamen
***************************************************************************************************/
function validarCamposIguales(validacionCamposIguales) {

// Requiere vector de validacion [validarCamposIguales] donde estan los id de los campos que deben pasar iguales
// Ejemplo : 
//           var validacionCamposIguales = new Array("idcampo1","idcampo2","idcampo3");
//           validarCamposIguales(validacionCamposIguales);

 var doAction = true;
 
 if(validacionCamposIguales.length>0) 
    g=document.getElementById(validacionCamposIguales[0]);	 

  for(i=0;i<validacionCamposIguales.length;i++) {
	  e=document.getElementById(validacionCamposIguales[i]);
	  if(e.value!=g.value) {
		 alert("Los campos (" + g.tag + " y " + e.tag + ") son diferentes") ;
		 doAction=false;
         eval(func); 		 
		 e.focus();
		 break;
	 }
  }
  if(doAction)
    return true;
}

function validarCampoFecha(validacionCampoFecha, func) {
 var doAction = true;	
  for(i=0;i<validacionCampoFecha.length;i++) { 
     e=document.getElementById(validacionCampoFecha[i]);

     if (/^(\d{4}\-\d{2}\-\d{2}){1}$/.test(e.value)){	 
        a=1;     
	 }
	 else {
		doAction=false;
		alert("El campo " + e.tag + " [" + e.value + "] es incorrecto\nEl formato es AAAA-MM-DD");
         eval(func); 		
		e.focus();
		break;
 	 }
  }
  if(doAction)
    return(true);
}

function validarCampoMail(validacionCampoMail) {
	
 var doAction = true;	
 
  for(i=0;i<validacionCampoMail.length;i++) { 
     e=document.getElementById(validacionCampoMail[i]);
	 
     if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(e.value)){	 
        a=1;     
	 }
	 else {
		doAction=false;
		alert("La direccion de mail [" + e.value + "] es incorrecta");
		e.focus();
		break;
 	 }
  }
  
  if(doAction)
    return(true);
}


function validarChecks(forma, nameChecks, minChecked, tag, useTag) {
	
 // Recibe el nombre de la forma, el nombre del vector de checks, el minimo de checks q deben pasar seleccionados y el label
 // Ejemplo :
// 	validarChecks("formRegistro", "cSer[]" , 1, "servicio");
	
  numele=document.forms[forma].elements.length;
  contador=0;
  
  for(i=0;i<numele;i++) {
	 e=document.forms[forma].elements[i];
	 if(e.type == "checkbox") 	
	    if(e.name==nameChecks) 
 		   if(e.checked==true) 
		       contador++;
  }

  if(contador >= minChecked) {
    return true;
  }
  else {
	 if(useTag) {
      alert(tag);		 	 
	 }
	 else {
      alert("Debe seleccionar por lo menos " + minChecked + " " + tag);		 
	 }
  }
}

function ir(forma) {
  if(validarForm(forma))
    document.forms[forma].submit();
}



function validarForm(forma) {
  FLAG=true;
  numele=document.forms[forma].elements.length;
  
  for(i=0;i<numele;i++) {
	 if( (document.forms[forma].elements[i].type == "text") || (document.forms[forma].elements[i].type == "select-one") || (document.forms[forma].elements[i].type == "textarea") ) 
	 {
	  if(document.forms[forma].elements[i].value=="") {
		 alert("Falta llenar el campo [" + document.forms[forma].elements[i].tag + "]");  
		 document.forms[forma].elements[i].focus();
	     FLAG=false;
		 break;
	    }
	 }
  }
 if(FLAG) 
  if(confirm("Se dispone a enviar el formulario, esta seguro de los datos ?")) 
    return(FLAG);  
}




function searchArray(vector, value)
{
 var i;
 for (i=0; i < vector.length; i++) {  // Matches identical (===), not just similar (==).
    if (vector[i] === value) {
       return true;
    }
 }
return false;
}


function validarAlfaNumerico(tecla, type) {
  num=false;
  ascii=codigoascii(tecla);
  if( (ascii>=48 && ascii<=57) || (ascii>=65 && ascii<=90) || (ascii>=97 && ascii<=122) || (ascii==32) || (ascii==209) || (ascii>=44 && ascii<=46)   || (ascii==35) || (ascii==64) )
    if((ascii != 13)) {
 	   if(type=="UPPER") uppercase(); 		
 	   if(type=="LOWER") lowercase(); 			   
  	   num = String.fromCharCode(ascii);
	}
  return num;
}

function validarMail(tecla, type) {
  num=false;
  ascii=codigoascii(tecla);
  if( (ascii>=48 && ascii<=57) || (ascii>=65 && ascii<=90) || (ascii>=97 && ascii<=122) || (ascii==64) || (ascii==46) || (ascii==95) )
    if((ascii != 13)) { 
 	   if(type=="UPPER") uppercase(); 		
 	   if(type=="LOWER") lowercase(); 			   
 	   num = String.fromCharCode(ascii);
	   }
  return num;
}

function validarAlfa(tecla, type) {
  num=false;
  ascii=codigoascii(tecla);
  if( (ascii>=65 && ascii<=90) || (ascii>=97 && ascii<=122) || (ascii==32) || (ascii==209) || (ascii==45) || (ascii==44))
    if((ascii != 13)) {
 	   if(type=="UPPER") uppercase(); 
 	   if(type=="LOWER") lowercase(); 			   	   
 	      num=String.fromCharCode(ascii);
	}
  return num;
}

function validarNone(tecla) {
  return false;
}


// ValidarFecha permite [ (0-9),'/','-' ] 
function validarFecha(tecla) {
  num=false;
  ascii=codigoascii(tecla);
  if((ascii>=48 && ascii<=57) || ascii==47 || ascii==45)
    if((ascii != 13) && (ascii != 32))
 	   num = String.fromCharCode(ascii);
  return num;
}

function uppercase() {
  key = window.event.keyCode;
  if ((key > 0x60) && (key < 0x7B))
        window.event.keyCode = key-0x20;
}

function lowercase() {
  key = window.event.keyCode;
  if ((key > 0x40) && (key < 0x5B))
        window.event.keyCode = key+0x20;
}
/*
function validarNumerico(tecla) {
  num=false;
//  alert(tecla);
 // ascii=codigoascii(tecla);
  if (navigator.appName == 'Netscape'){ 
//  document.exf1.key.value=e.which + " = ("+String.fromCharCode(e.which)+")"; 
//	alert("1. "+ tecla.which);
	ascii = tecla.which;
  } 
  if (navigator.appName == 'Microsoft Internet Explorer'){ 
//  document.exf1.key.value=window.event.keyCode + " = ("+String.fromCharCode(window.event.keyCode)+")";
//	alert("2. "+ window.event.keyCode);
	ascii =	window.event.keyCode;
  }

  if( ascii>=48 && ascii<=57 )
    if((ascii != 13) && (ascii != 32))
 	   num = String.fromCharCode(ascii);
	   //alert(num);
  return num;  
}
*/
function validarNumerico(e) {
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event) // IE
	  {
	  keynum = e.keyCode;
	  }
	else if(e.which) // Netscape/Firefox/Opera
	  {
	  keynum = e.which;
	  }
	keychar = String.fromCharCode(keynum);
	//numcheck = /\d/;
	numcheck = /[0-9\x08\x7F]/;
	return numcheck.test(keychar);
}


function validarNumericoEnter(tecla) {
  num=false;
  ascii=codigoascii(tecla);
  if(ascii>=48 && ascii<=57) 
     num = String.fromCharCode(ascii);
  if(ascii==13) 
	 document.getElementById("t1_s4").focus();
  return num;
}

function validarUserPass(tecla) {
  num=false;
  ascii=codigoascii(tecla);
  if(ascii!=34 && ascii!=39 && ascii!=44 && ascii!=47 && ascii!=96 && ascii!=126 && ascii != 32)  
      if((ascii != 13))
 	     num = String.fromCharCode(ascii);
  return num;
}

 function validarCorteCadena(tecla, type) {
  num=false;
  ascii=codigoascii(tecla);
  if(ascii!=34 && ascii!=39 && ascii!=44 && ascii!=47 && ascii!=96 && ascii!=126)
    if(ascii != 13) {
 	   if(type=="UPPER") uppercase(); 		
 	   if(type=="LOWER") lowercase(); 			   
 	   num = String.fromCharCode(ascii);
	}
  return num;
 }


function validarDecimal(tecla) {
  num=false;
  ascii=codigoascii(tecla);
  if((ascii>=48 && ascii<=57) || ascii==46)
    if((ascii != 13) && (ascii != 32))
 	   num = String.fromCharCode(ascii);
  return num;
}


function codigoascii(tecla) {
 if(document.all) { // Internet Explorer
  var ascii = window.event.keyCode;

 }
 else {
  if(document.layers) { // Nestcape
   var ascii = tecla.which;
  }
 }
 
 return ascii;
}

function showhide(id) {
 if(document.getElementById(id).style.display=='inline') {
    document.getElementById(id).style.display='none'
  }
 else {
    document.getElementById(id).style.display='inline'
 }
}

function show(id) {
 document.getElementById(id).style.display='inline'	
}

function hide(id) {
 document.getElementById(id).style.display='none'	
}


function selectAll(id) {
 e=document.getElementById(id);
 for (i=0; i<e.length; i++) { 
   e.options[i].selected = true; 
 } 
}

function removeAllList(m1) {
 m1len = m1.length;	
    for ( i = (m1len-1); i>=0; i--){
            m1.options[i] = null;
    }
}

function one2two(m1, m2) {
    m1len = m1.length ;
    for ( i=0; i<m1len ; i++){
        if (m1.options[i].selected == true ) {
            m2len = m2.length;
            m2.options[m2len]= new Option(m1.options[i].text);
        }
    }

    for ( i = (m1len -1); i>=0; i--){
        if (m1.options[i].selected == true ) {
            m1.options[i] = null;
        }
    }
}

function two2one(m1, m2) {
    m2len = m2.length ;
        for ( i=0; i<m2len ; i++){
            if (m2.options[i].selected == true ) {
                m1len = m1.length;
                m1.options[m1len]= new Option(m2.options[i].text);
            }
        }
        for ( i=(m2len-1); i>=0; i--) {
            if (m2.options[i].selected == true ) {
                m2.options[i] = null;
            }
        }
}



function NumberFormat(num,numDec,decSep,thousandSep) {
  var arg;
  var Dec;
  Dec = Math.pow(10,numDec);
  if(typeof(num) == 'undefined') return;
  
  if(typeof(decSep) == 'undefined') decSep = ',';
  
  if(typeof(thousandSep) == 'undefined') thousandSep = '.';
  
  if(thousandSep == '.'){
    arg=/\./g;
  }else if(thousandSep == ','){
    arg=/\,/g;
  }

  if(typeof(arg) != 'undefined'){ 
    num = num.toString().replace(arg,'');
  }
  
  num = num.toString().replace(/,/g,'.'); 
  if(isNaN(num))
    num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*Dec+0.50000000001);
  cents = num%Dec;
  num = Math.floor(num/Dec).toString();  
  if(cents<(Dec/10))
   {
    if(cents==0)	   
	   cents = "0" + cents;  	
	else
	   cents = "0" + cents;   
   }	   
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+thousandSep+ num.substring(num.length-(4*i+3));
  if(Dec==1)
  return (((sign)?'':'-') + num);
  else {
   return (((sign)?'':'-') + num + decSep + cents);
   }
}

function QuitarFormato(number)
{
    var numero_sf ='';
    //alert (number);
    for (var i = 0; i < number.length; i++)
    {
         //alert (number.substring(i,i+1));
         if ((number.substring(i,i+1))!= ",")
         {
            numero_sf=numero_sf + number.substring(i,i+1);
         }
    }
    //alert (numero_sf);
    return numero_sf;

}

function setDataType(cValue)
  {
    var isDate = new Date(cValue);
    if (isDate == "NaN")
      {
        if (isNaN(cValue))
          {
            cValue = cValue.toUpperCase();
            return cValue;
          }
        else
          {
            var myNum;
            myNum = String.fromCharCode(48 + cValue.length) + cValue;
            return myNum;
          }
        }
  else
      {
        var myDate = new String();
        myDate = isDate.getFullYear() + " " ;
        myDate = myDate + isDate.getMonth() + " ";
        myDate = myDate + isDate.getDate(); + " ";
        myDate = myDate + isDate.getHours(); + " ";
        myDate = myDate + isDate.getMinutes(); + " ";
        myDate = myDate + isDate.getSeconds();
        return myDate ;
      }
  }
  
function sortTable(col, tableToSort)
  {
    var iCurCell = col + tableToSort.cols;
    var totalRows = tableToSort.rows.length;
    var bSort = 0;
    var colArray = new Array();
    var oldIndex = new Array();
    var indexArray = new Array();
    var bArray = new Array();
    var newRow;
    var newCell;
    var i;
    var c;
    var j;

    for (i=1; i < tableToSort.rows.length; i++)
      {
        colArray[i - 1] = setDataType(tableToSort.cells(iCurCell).innerText);
        iCurCell = iCurCell + tableToSort.cols;
      }
    for (i=0; i < colArray.length; i++)
      {
        bArray[i] = colArray[i];
      }
    colArray.sort();
    for (i=0; i < colArray.length; i++)
      { 
        indexArray[i] = (i+1);
        for(j=0; j < bArray.length; j++)
          { 
            if (colArray[i] == bArray[j])
              {  
                for (c=0; c<i; c++)
                  {
                    if ( oldIndex[c] == (j+1) )
                    {
                      bSort = 1;
                    }
                      }
                      if (bSort == 0)
                        {
                          oldIndex[i] = (j+1);
                        }
                          bSort = 0;
                        }
          }
    }
  for (i=0; i<oldIndex.length; i++)
    {
      newRow = tableToSort.insertRow();
      for (c=0; c<tableToSort.cols; c++)
        {
          newCell = newRow.insertCell();
          newCell.innerHTML = tableToSort.rows(oldIndex[i]).cells(c).innerHTML;
        }
      }
  for (i=1; i<totalRows; i++)
    {
      tableToSort.moveRow((tableToSort.rows.length -1),1);
    }
  for (i=1; i<totalRows; i++)
    {
      tableToSort.deleteRow();
    }
  }


function loadDataList(idLista, vector1, vector2) {
 m=document.getElementById(idLista);
 removeAllList(m);
  for(i=0;i<vector1.length;i++) {
      m1len = m.length;	
      m.options[m1len]=new Option(vector1[i], vector2[i]);
  }
}


function NumberFormat(num,numDec,decSep,thousandSep) {
  var arg;
  var Dec;
  Dec = Math.pow(10,numDec);
  if(typeof(num) == 'undefined') return;
  
  if(typeof(decSep) == 'undefined') decSep = ',';
  
  if(typeof(thousandSep) == 'undefined') thousandSep = '.';
  
  if(thousandSep == '.'){
    arg=/\./g;
  }else if(thousandSep == ','){
    arg=/\,/g;
  }

  if(typeof(arg) != 'undefined'){ 
    num = num.toString().replace(arg,'');
  }
  
  num = num.toString().replace(/,/g,'.'); 
  if(isNaN(num))
    num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*Dec+0.50000000001);
  cents = num%Dec;
  num = Math.floor(num/Dec).toString();  
  if(cents<(Dec/10))
   {
    if(cents==0)	   
	   cents = "0" + cents;  	
	else
	   cents = "0" + cents;   
   }	   
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+thousandSep+ num.substring(num.length-(4*i+3));
  if(Dec==1)
  return (((sign)?'':'-') + num);
  else {
   return (((sign)?'':'-') + num + decSep + cents);
   }
}

function QuitarFormato(number)
{
    var numero_sf ='';
    //alert (number);
    for (var i = 0; i < number.length; i++)
    {
         //alert (number.substring(i,i+1));
         if ((number.substring(i,i+1))!= ",")
         {
            numero_sf=numero_sf + number.substring(i,i+1);
         }
    }
    //alert (numero_sf);
    return numero_sf;

}
