// JavaScript Document
function addToCart(vid, vidt) {
	new Ajax.Updater('cartbox', '/carro_add.php', { method: 'get', parameters: {id: vid, idt: vidt} });
	return false;
}
function delFromCart(vid, vidt) {
	new Ajax.Updater('cartbox', '/carro_del.php', { method: 'get', parameters: {id: vid, idt: vidt} });
	return false;
}

// JavaScript Document
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function CorregirEntrada(entrada, mensaje) {
  alert(mensaje);
  entrada.focus();
}

function CorregirEntradaFCK(oFCKentrada, mensaje) {
  alert(mensaje);
  oFCKentrada.Focus();
}

function EsNumerico(numero) {
  return numero.match(/\b(^[\d]*$)\b/gi);
}

function EsTelefono(numero) {
  return numero.match(/^[0-9]{1,2}(-| )?[0-9]{4,7}$/);
}

function EsFecha(fecha) {
  return fecha.match(/^\d{1,2}(\-)\d{1,2}\1(\d{2}|\d{4})$/);
}

function EsFechaHora(fechahora) {
  return fechahora.match(/^\d{1,2}(\-)\d{1,2}\1(\d{2}|\d{4}) ([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])$/);
}

// Función para validar el mail
function EsEmail(email) {
  return email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.pro)|(\.name)|(\.info)|(\.aero)|(\.museum)|(\..{2,2}))$)\b/gi);
}



function ArreglaRUT(texto) {
  var tmpstr = "";
  var largo = texto.length;
  for ( i=0; i < texto.length ; i++ )
    if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
      tmpstr = tmpstr + texto.charAt(i);
  texto = tmpstr;
  largo = texto.length;
  tmpstr = "";
  for ( i=0; texto.charAt(i) == '0' ; i++ );
  for (; i < texto.length ; i++ )
     tmpstr = tmpstr + texto.charAt(i);
  texto = tmpstr;
  largo = texto.length;
  var invertido = "";
  for ( i=(largo-1),j=0; i>=0; i--,j++ )
    invertido = invertido + texto.charAt(i);
  var dtexto = "";
  dtexto = dtexto + invertido.charAt(0);
  dtexto = dtexto + '-';
  cnt = 0;
  for ( i=1,j=2; i<largo; i++,j++ )
  {
    if ( cnt == 3 )
    {
      dtexto = dtexto + '.';
      j++;
      dtexto = dtexto + invertido.charAt(i);
      cnt = 1;
    }
    else
    {
      dtexto = dtexto + invertido.charAt(i);
      cnt++;
    }
  }
  invertido = "";
  for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )
    invertido = invertido + dtexto.charAt(i);
  return invertido;
}

function checkDV( crut )
{
  largo = crut.length;
  if ( largo > 2 )
    rut = crut.substring(0, largo - 1);
  else
    rut = crut.charAt(0);
  dv = crut.charAt(largo-1);
  dv = dv + "";
  if ( dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k'  && dv != 'K')
    return false;
  if ( rut == null || dv == null )
      return 0;
  var dvr = '0';
  suma = 0;
  mul  = 2;
  for (i= rut.length -1 ; i >= 0; i--)
  {
    suma = suma + rut.charAt(i) * mul;
    if (mul == 7)
      mul = 2;
    else
      mul++;
  }
  res = suma % 11;
  if (res==1)
    dvr = 'k';
  else if (res==0)
    dvr = '0';
  else
  {
    dvi = 11-res;
    dvr = dvi + "";
  }
  if ( dvr != dv.toLowerCase() )
    return false;

  return true;
}

function EsRUTValido(texto)
{
  var tmpstr = "";
  for ( i=0; i < texto.length ; i++ )
    if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
      tmpstr = tmpstr + texto.charAt(i);
  texto = tmpstr;
  largo = texto.length;
  tmpstr = "";
  for ( i=0; texto.charAt(i) == '0' ; i++ );
  for (; i < texto.length ; i++ )
     tmpstr = tmpstr + texto.charAt(i);
  texto = tmpstr;
  largo = texto.length;
  for (i=0; i < largo ; i++ )
  {
    if ( texto.charAt(i) !="0" && texto.charAt(i) != "1" && texto.charAt(i) !="2" && texto.charAt(i) != "3" && texto.charAt(i) != "4" && texto.charAt(i) !="5" && texto.charAt(i) != "6" && texto.charAt(i) != "7" && texto.charAt(i) !="8" && texto.charAt(i) != "9" && texto.charAt(i) !="k" && texto.charAt(i) != "K" )
      return false;
  }
  if ( checkDV(texto) )
    return true;
  return false;
}

function ValidateLogin(formulario) {
  var EntradasOK;
  EntradasOK = false;
  
  if (trim(formulario.LoginRUT.value).length >= 6) formulario.LoginRUT.value = ArreglaRUT(trim(formulario.LoginRUT.value));

  if (trim(formulario.LoginRUT.value).length < 6) {
    CorregirEntrada(formulario.LoginRUT, "Por favor indica tu RUT.");
  } else if (!EsRUTValido(formulario.LoginRUT.value)) {
    CorregirEntrada(formulario.LoginRUT, "Por favor indica un RUT VÁLIDO (Ej: 12.345.678-9).");
  } else if (formulario.LoginPassword.value == "") {
    CorregirEntrada(formulario.LoginPassword, "Por favor indica tu CONTRASEÑA PARA INGRESAR.");
  } else {
    EntradasOK = true;
  }
  return EntradasOK;
}

