var char_set = '$%^NOZ1&PQR(./~`"CDEFG!@STUVWZghij}:<pHB*#8uvwx>?[]\',ef34590qrklmnoIJ)_+{st67 abcdAyzKLM2YñÑX-';

function ComillasComa(cad)
{
  var a = 0;
  var cadlen = cad.length;
  var letra = '';
  var total = '';

  while (a <= cadlen) {
    numreg = '';
    letra = cad.substr(a, 1);
    while ( (letra != ',') && (a <= cadlen) ) {
      letra = cad.substr(a,1);
      if (letra != ',') numreg = numreg + letra;       
      a++;
    }
    total = total + "'" + Trim(numreg) + "',";
  }
	total = total.substr(0, total.length - 1);
	return total;
}

function FormatearVentana(vHeight,vWidth)
{
  
  LeftPosition = (screen.width) ? (screen.width - vWidth)/2 : 0;
  TopPosition = (screen.height) ? (screen.height - vHeight)/2 : 0;
  settings ='height=' + vHeight + ',width=' + vWidth + ',top=' + TopPosition + ',left=' + LeftPosition + ',bar=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1';
  
  return(settings);
}
function FormatearVentanaExcel(vHeight,vWidth)
{
  
  LeftPosition = (screen.width) ? (screen.width - vWidth)/2 : 0;
  TopPosition = (screen.height) ? (screen.height - vHeight)/2 : 0;
  settings ='height=' + vHeight + ',width=' + vWidth + ',top=' + TopPosition + ',left=' + LeftPosition + ',bar=0,toolbar=1,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1';
  
  return(settings);
}
function FormatearVentanaSinScroll(vHeight,vWidth)
{
  
  LeftPosition = (screen.width) ? (screen.width - vWidth)/2 : 0;
  TopPosition = (screen.height) ? (screen.height - vHeight)/2 : 0;
  settings ='height=' + vHeight + ',width=' + vWidth + ',top=' + TopPosition + ',left=' + LeftPosition + ',bar=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0';
  
  return(settings);
}
function chkNumero(vNumero, vFlag, vTamanioMax, vAlert) 
{  
  Entero = "0";
  Decimal = "00";
  
  PtoDecimal = vNumero.indexOf(".");
  if (PtoDecimal != -1)
  {
    if (vNumero.indexOf(".",PtoDecimal+1) != -1)
    {
      if (vAlert == 1)
        alert("Valor no válido");
      return("x");
    }
    if (vNumero.indexOf(",",PtoDecimal+1) != -1)
    {
      if (vAlert == 1)
        alert("Valor no válido");
      return("x");
    }
    if (vNumero.indexOf("'",PtoDecimal+1) != -1)
    {
      if (vAlert == 1)
        alert("Valor no válido");
      return("x");
    }
    if (vFlag==0) 
    {
      if (vAlert == 1)
        alert("Solo se aceptan valores enteros");
      return("x");
    }
    if (PtoDecimal== 0)
    {
      Entero="0";
    }
    else
    {
      Entero=vNumero.substr(0,PtoDecimal);
    }
    if (PtoDecimal== vNumero.length-1)
    {
      Decimal="00";
    }
    else
    {
      Decimal=vNumero.substr(PtoDecimal+1);
    }        
  }
  else
  {
    Entero = vNumero;
  }
  
  for (i = 0; i <=Entero.length - 1 ; i++)
  {
    if (Entero.length > 1)
    {
    
      if (Entero.substr(i,1)!="0" && Entero.substr(i,1)!="," && Entero.substr(i,1)!="'")
      {
        break;
      }
       else
      {
        Entero = Entero.substr(i+1)
        i = i-1
      }
    }
  }
  
  contador = 0;
  
  for (i = Entero.length - 1; i >=0 ; i--)
  {
    contador = contador + 1;
    valor = Entero.substr(i,1);
    if (valor != "0" && valor != "1" && valor != "2" && valor != "3" && valor != "4" && valor != "5" &&    
        valor != "6" && valor != "7" && valor != "8" && valor != "9")
    {
      if (contador%8 == 0 && valor != "," && valor != "'")
      {
        if (vAlert == 1)
          alert("Valor no válido");
        return("x");
      }
      else if (contador%8 == 0 && (valor == ","  || valor == "'"))
      {
        Entero=Entero.substr(0,i)+"'"+Entero.substr(i+1)        
      }
      else if (contador%4 == 0 && valor != ",")
      {
        if (vAlert == 1)
          alert("Valor no válido");
        return("x");
      }
      else if (contador%4!=0 && (valor == "," || valor == "'"))
      {
        if (vAlert == 1)
          alert("Valor no válido");
        return("x");
      }
      else if (valor != "," && valor != "'" )
      {
        if (vAlert == 1)
          alert("Valor no válido");
        return("x");
      }   
    }
    else
    {
      if (contador%8==0)
      {
        Entero = Entero.substr(0,i+1) + "'" + Entero.substr(i+1)
        i = i + 1
      }
      else if (contador%4==0)
      {
        Entero = Entero.substr(0,i+1) + "," + Entero.substr(i+1)
        i = i + 1
      }         
    }
  }

  if (Entero.length > vTamanioMax)
  {
    if (vAlert == 1)
       alert("Valor ingresado es muy grande");
    return("x");
  }
   
  if (vFlag == 0)
  { 
    return(Entero);
  }
  else
  {
   
    Decimal.substr(0,2)
    Decimal = Decimal + "00";
    Decimal = Decimal.substr(0,2);
      
    return(Entero + "." + Decimal);
 }
}

function ReemplazarComilla(Valor)
{
  Cadena=Valor;
  NuevaCadena="";

  if (Cadena != "")
  { 
    i = 0;
  
    while (i >= 0)
    {
      var temp=/\'/i;
      i = Cadena.search(temp);
      if (i < 0)
      {
        NuevaCadena = NuevaCadena + Cadena;
        break;
      }
      else
      {
         NuevaCadena = NuevaCadena + Cadena.substr(0,i) + "\'";
         Cadena = Cadena.substr(i+1);
      }
    }
   
    Cadena=NuevaCadena;
    NuevaCadena="";
    i = 0;
 
    while (i >= 0)
    {
      var temp=/"/i;
      i = Cadena.search(temp);
      if (i < 0)
      {
        NuevaCadena = NuevaCadena + Cadena;
        break;
      }
      else
      {
         NuevaCadena = NuevaCadena + Cadena.substr(0,i) + "\'";
         Cadena = Cadena.substr(i+1);                  
      }
    }
    Cadena=NuevaCadena;
 }  
 return(Cadena);
}

function chkDate(vYear, vMonth, vDay) 
{  
  var monthDays = new montharr(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  
  if (vYear.length < 4) 
  {
    alert("Fecha no válida. " + vDay+"/"+vMonth+"/"+vYear);
    return false; 
  }

  if (vDay >= 1 && vDay <= 31) 
  {
    if (vMonth < 1 || vMonth > 12) 
    {
      alert("Fecha no válida. " + vDay+"/"+vMonth+"/"+vYear);
      return false; 
    }
  }
  else 
  {
    alert("Fecha no válida." + vDay+"/"+vMonth+"/"+vYear);
    return false;
  }

  if (((vYear % 4 == 0) && (vYear % 100 != 0)) || (vYear % 400 == 0))
    monthDays[1] = 29;
  
  nDays = monthDays[vMonth-1];

  if (vDay > nDays) 
  {
    alert("Fecha no válida." + vDay+"/"+vMonth+"/"+vYear);
    return false;
  }
  return true;
}

function chkDateFrmt(vDate) 
{  
  if (vDate.length < 8 || vDate.length >10){
    return false;
  }
  else
  {  
    pos1 = vDate.indexOf("/");
    pos2 = vDate.lastIndexOf("/");
    if (pos1 > 0 || pos2 > 0) {
        vDay = vDate.substr(0, pos1);
        vMonth = vDate.substr(pos1+1, pos2-pos1-1);
        vYear = vDate.substr(pos2+1, vDate.length);
    }
    else {
        return false;
    }
	return true;
  }
}

function montharr (m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11) 
{
  this[0]  = m0;
  this[1]  = m1;
  this[2]  = m2;
  this[3]  = m3;
  this[4]  = m4;
  this[5]  = m5;
  this[6]  = m6;
  this[7]  = m7;
  this[8]  = m8;
  this[9]  = m9;
  this[10] = m10;
  this[11] = m11;
}

function ShowCal(form, pType)
{      
  addWindow = window.open("../../Tools/Calendar.php?SFormName=" + form.name +
                          "CDateType=" + pType, "Calendar",
                          "width=275,height=180,resizable=0,status=0,menubar=0,scrollbars=0,fullscreen=0");
  addWindow.focus(); 
}

function chkTime(vHor, vMin) 
{  
  if (vHor >= 0 || vHor <= 23) 
  {
    if (vMin < 0 || vMin > 59) 
    {
      alert("Hora no válida. " + vHor + ":" + vMin);
      return false; 
    }
  }
  else 
  {
    alert("Hora no válida." + vHor + ":" + vMin);
    return false;
  }
  return true;
}

function encrypt_data(value) 
{
  var input = value;
  var output = "";
  var char_code;

  //get algorithm from pull down select
  var algorithm = 4 //window.input.shift_select.selectedIndex;
  algorithm++;

  var alpha_length = char_set.length - algorithm;
  var space;

  //begin for loop to cycle through input
  for (loop=0; loop<input.length; loop++) {

    //if conditional detects unknown characters
    if (char_set.indexOf(input.charAt(loop)) == -1) {
      alert("Program Error: Unknown Character!");
    }

    //search char_set string for character and set char_code variable...
    char_code = char_set.indexOf(input.charAt(loop));

    //actual text encoding algorithm goes here
    if (char_code + algorithm > char_set.length)
    {
      space = char_set.length - char_code;
      char_code = algorithm - space;
    }
    else
    {
      char_code += algorithm;
    }

    //set output variable in accordance to char_set
    output += char_set.charAt(char_code);
  }
  //dump contents of output variable into textarea
  return output;
}

function decrypt_data(value)
{
  var input = value;
  var output = "";
  var char_code;

  //get algorithm from pull down select
  var algorithm = 4 //window.input.shift_select.selectedIndex;
  algorithm++;

  var alpha_length = char_set.length - algorithm;
  var space;

  //begin for loop to cycle through input
  for (loop=0; loop<input.length; loop++) {

    //if conditional detects unknown characters
    if (char_set.indexOf(input.charAt(loop)) == -1) {
      alert("Program Error: Unknown Character!");
    }

    //search char_set string for character and set char_code variable...
    char_code = char_set.indexOf(input.charAt(loop));

    //oppisite of encrypt algorithm goes here
    if (char_code - algorithm < 0)
    {
      space = algorithm - char_code;
      char_code = char_set.length - space;
    }
    else
    {
      char_code -= algorithm;
    }

//set output variable in accordance to char_set
    output += char_set.charAt(char_code);
  }
  
  //dump contents of output variable into textarea
  return(output);
}

function EsNumero(txt)
{ 
  for (i = txt.value.length - 1; i >=0 ; i--)
  {
    valor = txt.value.substr(i,1);
    if (valor != "0" && valor != "1" && valor != "2" && valor != "3" && valor != "4" && 
        valor != "5" && valor != "6" && valor != "7" && valor != "8" && valor != "9") 
    {
      alert("Valor no válido");
      txt.focus();
      break;
    }
  }
  return;
}

function IsNumber(Cadena)
{ 
  for (i = Cadena.length - 1; i >=0 ; i--)
  {
    valor = Cadena.substr(i,1);
    if (valor != "0" && valor != "1" && valor != "2" && valor != "3" && valor != "4" && 
        valor != "5" && valor != "6" && valor != "7" && valor != "8" && valor != "9") 
    {
      return false;
      break;
    }
  }
  return true;
}

function OnlyNum(Entero)
{ 
  cRtn = "";
  for (i = 0; i < Entero.length; i++)
  {
    valor = Entero.substr(i, 1);
    if (valor == "0" || valor == "1" || valor == "2" || valor == "3" || valor == "4" ||
        valor == "5" || valor == "6" || valor == "7" || valor == "8" || valor == "9") {
      cRtn = cRtn + valor;
    }
  }
  return(cRtn);
}

function IsNum(Entero)
{ 
  cRtn = "";
  alert(Entero);
  alert(Entero.length);
  for (i = Entero.length - 1; i >=0 ; i--)
  {
    valor = Entero.substr(i,1);
    alert(valor);
    if (valor != "0" && valor != "1" && valor != "2" && valor != "3" && valor != "4" && 
        valor != "5" && valor != "6" && valor != "7" && valor != "8" && valor != "9") 
    {
      alert("Valor no válido");
      cRtn = "x";
      break;
    }
  }
  return(cRtn);
}

function ValidarDate(txt)
{ 
  if ( IsDate(txt.value) == 'x' ) {
    txt.focus();
    return;
  }
}

function IsDate(Entero)
{ 
  cRtn = "";
  for (i = Entero.length - 1; i >=0 ; i--)
  {
    valor = Entero.substr(i,1);
    if (valor != "0" && valor != "1" && valor != "2" && valor != "3" && valor != "4" && 
        valor != "5" && valor != "6" && valor != "7" && valor != "8" && valor != "9" && 
        valor != "/" ) 
      {
      alert("Valor no válido");
      cRtn = "x";
      break;
    }
  }
  return(cRtn);
}

function ValidarTime(txt)
{ 
  if ( IsTime(txt.value) == 'x' ) {
    txt.focus();
    return;
  }
}

function IsTime(Entero)
{ 
  cRtn = "";
  for (i = Entero.length - 1; i >=0 ; i--)
  {
    valor = Entero.substr(i,1);
    if (valor != "0" && valor != "1" && valor != "2" && valor != "3" && valor != "4" && 
        valor != "5" && valor != "6" && valor != "7" && valor != "8" && valor != "9" && 
        valor != ":" ) 
    {
      alert("Valor no válido");
      cRtn = "x";
      break;
    }
  }
  return(cRtn);
}

function CCA(CB, noHL)
{
  if (!noHL)
    if (CB.checked) 
      hL(CB);
    else
      dL(CB);
}

function CCD(CB)
{
  dL(CB);
}

function CCE(CB)
{
  hL(CB);
}

function hL(E)
{
  while (E.tagName != "TR")
  { E = E.parentElement;}

  E.className="H";
}

function dL(E)
{
  while (E.tagName != "TR")
  {E = E.parentElement;}
  
  E.className = "";
}


function CambiarMayuscula(txt) 
{ 
  sValor = txt.value;
  txt.value = sValor.toUpperCase();
  return;
}

function ValidarTamanio(txt,tamanio) 
{ 
  if (txt.value.length > tamanio) {
    txt.value=txt.value.substr(0,tamanio)
    txt.focus();
    alert("Solo puede ingresar como máximo " + tamanio + " caracteres" );
  }
}

function TraerValor(valor)
{
  var temp = /\|/i;
  
  i = valor.search(temp);
  
  if (i != 0)
    valor = valor.substr(0, i);
  else
    valor = "";

  return(valor);
}

function Formatear(txt)
{
  NumeroExpediente = txt.value;
  NumeroExpediente = (NumeroExpediente.replace(/^\s*/g,'').replace(/\s+$/g,''));
  
  if (NumeroExpediente != '') {
    NumeroExpediente = '000000' + NumeroExpediente
    txt.value = NumeroExpediente.substr(NumeroExpediente.length-6,6);
  }
}

function Trim(s) 
{
  // Remove leading spaces and carriage returns  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function DeshabilitarControl(control)
{
  control.style.backgroundColor = '#D3D3D3';
  control.style.Color = '#000000';
  control.readOnly = true;
  control.disabled = false;
}

function HabilitarControl(control)
{
  control.style.backgroundColor = '#FFFFFF';
  control.style.Color = '#000000';
  control.readOnly = false;
  control.disabled = false;
}

function LlenarTexto(cbo,txt) 
{  
  i=cbo.options.selectedIndex;
  valor=cbo.options(i).value
  valor = valor.substring(0,1);
  txt.value=valor;
}
function validarCorreo(EMail){
	if (EMail.length != 0)
    {
      var temp=/@/i;
      i = EMail.search(temp);
      if (i <= 0 || i == EMail.length-1 )
      {        
        return false;
      }
      var temp=/\./i;
      i = EMail.search(temp);
      if (i <= 0 || i == EMail.length-1 )
      {        
        return false;
      }
	  return true;
    }
	else
		return false;
}
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}