// Si una cadena esta vacia 
function isEmpty(inputStr)
{
  	if (inputStr=="")
   		{return true;}
  	return false;
}

// Valida digito verificador
function checkCDV( objrut, dvr )
{
  dv = dvr + "";
  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')
	{
    	alert("Debe ingresar un digito verificador valido.");
    	objrut.focus();
    	objrut.select();
    	return false;
    	}
  return true;
}

function checkDV( objrut, crut )
{
  largo = crut.length;
  if ( largo < 2 )
	{
    	alert("Debe ingresar el rut completo.")
    	objrut.focus();
    	objrut.select();
    	return false;
    	}
  if ( largo > 2 )
  	rut = crut.substring(0, largo - 1);
  else
    	rut = crut.charAt(0);
  dv = crut.charAt(largo-1);
  checkCDV(objrut, dv );

  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() )
	{
    	alert("EL rut es incorrecto.")
    	objrut.focus();
    	objrut.value = "";
    	return false;
    	}

  return true;
}


// Valida RUT - CL
function ValidaRutCL(objrut)
{
  var texto = objrut.value;
  var tmpstr = "";  
  
  if (isEmpty(texto))
	return true;
  for ( i=0; i < texto.length ; i++ )
	if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
		tmpstr = tmpstr + texto.charAt(i);
//  texto = tmpstr;

  for ( i=0; i < tmpstr.length ; i++ )
	if ( tmpstr.charAt(i) != "0")
	{
		texto = tmpstr.substring(i, tmpstr.length);
		i = 100;
	}
  largo = texto.length;

  if ( largo < 6 || largo > 9 )
	{
    	alert("Largo del rut no corresponde.")
    	objrut.focus();
    	objrut.select();
    	return false;
	}

  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" ) 
		{
		alert("El valor ingresado no corresponde a un R.U.T valido.");
		objrut.focus();
		objrut.select();
		return false;
		}
	}

  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);

  objrut.value = invertido;
  Mayusculas(objrut);
  
  if ( checkDV(objrut, texto) )
    	return true;
  return false;
}

// Valida CUIT - AR
function ValidaRutAR(objrut)
{
  var texto = objrut.value;
  var tmpstr = "";  
  
  if (isEmpty(texto))
	return true;
  for ( i=0; i < texto.length ; i++ )
	if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' && texto.charAt(i) != '/' )
		tmpstr = tmpstr + texto.charAt(i);
  texto = tmpstr;
  largo = texto.length;

  if ( largo < 2 )
	{
    	alert("Debe ingresar el rut completo.")
    	objrut.focus();
    	objrut.select();
    	return false;
	}

  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" )
		{
		alert("El valor ingresado no corresponde a un ID valido.");
		objrut.focus();
		objrut.select();
		return false;
		}
	}
	var invertido = ""
	invertido = texto

  objrut.value = invertido;
  Mayusculas(objrut);

  return true;
}

// Valida CNPJ - BR
function ValidaRutAR(objrut)
{
  var texto = objrut.value;
  var tmpstr = "";

  if (isEmpty(texto))
	return true;
  for ( i=0; i < texto.length ; i++ )
	if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' && texto.charAt(i) != '/' )
		tmpstr = tmpstr + texto.charAt(i);
  texto = tmpstr;
  largo = texto.length;

  if ( largo < 2 )
	{
    	alert("Debe ingresar el rut completo.")
    	objrut.focus();
    	objrut.select();
    	return false;
	}

  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" )
		{
		alert("El valor ingresado no corresponde a un ID valido.");
		objrut.focus();
		objrut.select();
		return false;
		}
	}

	var invertido = ""
	invertido = texto

  objrut.value = invertido;
  Mayusculas(objrut);

  return true;
}

// Valida NIT - CO
function ValidaRutAR(objrut)
{
  var texto = objrut.value;
  var tmpstr = "";  
  
  if (isEmpty(texto))
	return true;
  for ( i=0; i < texto.length ; i++ )
	if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' && texto.charAt(i) != '/' )
		tmpstr = tmpstr + texto.charAt(i);
  texto = tmpstr;
  largo = texto.length;

  if ( largo < 2 )
	{
    	alert("Debe ingresar el rut completo.")
    	objrut.focus();
    	objrut.select();
    	return false;
	}

  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" )
		{
		alert("El valor ingresado no corresponde a un ID valido.");
		objrut.focus();
		objrut.select();
		return false;
		}
	}

	var invertido = ""
	invertido = texto

  objrut.value = invertido;
  Mayusculas(objrut);

  return true;
}


// Valida RUC - PE
function ValidaRutAR(objrut)
{
  var texto = objrut.value;
  var tmpstr = "";  
  
  if (isEmpty(texto))
	return true;
  for ( i=0; i < texto.length ; i++ )
	if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' && texto.charAt(i) != '/' )
		tmpstr = tmpstr + texto.charAt(i);
  texto = tmpstr;
  largo = texto.length;

  if ( largo < 2 )
	{
    	alert("Debe ingresar el rut completo.")
    	objrut.focus();
    	objrut.select();
    	return false;
	}

  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" )
		{
		alert("El valor ingresado no corresponde a un ID valido.");
		objrut.focus();
		objrut.select();
		return false;
		}
	}

	var invertido = ""
	invertido = texto

  objrut.value = invertido;
  Mayusculas(objrut);

  return true;
}


// Valida un monto en pesos
function ValidaMonto(objmonto)
{
	var monto = objmonto.value;
	var tmpmonto;
	//Si esta vacio y puede estarlo
	if (isEmpty(monto))
		return true;	
	// Si es número entero
	if (checkField( objmonto, isInteger, false ))
		{
		// Si es no es positivo
		if ( monto < 0 )
			{
			alert("El monto no puede ser negativo");
			objmonto.focus();
			objmonto.select();
			return false;
			}
		else
			{
			// Pone separacion de miles
			tmpmonto = "";			
			for(i = monto.length-1 ,j=1 ; i >= 0 ; i--,j++)
				{
				tmpmonto = monto.charAt(i) +  tmpmonto;				
				if ((j == 3 || j == 6 || j == 9 || j == 12 || j == 15) && i != 0)
					{					
					tmpmonto = '.' + tmpmonto;
					}
				}
			objmonto.value = tmpmonto;
			return true;					
			}
		}
	else
		{
		objmonto.focus();
		objmonto.select();
		return false;
		}
}

// Quita separacion de miles a un monto (.)
function SacaPuntos(objmonto)
{
	var monto = objmonto.value;	
	var tmpmonto = "";	
	
	for (i=0; i <= monto.length-1 ; i++)
		{
		if ( monto.charAt(i) != '.')			
			tmpmonto = tmpmonto + monto.charAt(i);
		}
	objmonto.value = tmpmonto;
}
// Quita mascara de rut (.)

function LimpiaRut(objrut)
{
	var rut = objrut.value;	
	var tmprut = "";	
	
	for (i=0; i <= rut.length-1 ; i++)
		{
		if (( rut.charAt(i) != '.') & ( rut.charAt(i) != '-'))
			tmprut = tmprut + rut.charAt(i);
	 	}
	
	objrut.value = tmprut;
}

// Valida una fecha escrita en formato dd+mm+yy o dd+mm+yyyy
// y lo entrega en dd/mm/yyyy, si la fecha es valida 
function ValidaFecha(objfecha)
{
	// Valida el formato de la fecha
	var fecha = objfecha.value;
	var largo = fecha.length;
	var tmpfecha = "";
	
	// Si esta vacio y puede estarlo
	if (isEmpty(fecha))
		return true;			

	if (largo==6) //Si digito 6 caracteres ddmmyy
	    {
			// Asigna dia, mes y año
			dia = fecha.charAt(0) + fecha.charAt(1);
			mes = fecha.charAt(2) + fecha.charAt(3);
			anio = fecha.charAt(4) + fecha.charAt(5);
			if (anio >= 50) // Pone siglo, si es mayor de 50 pone 1900
				anio = '19' + anio;
			else			// Pone siglo, si es menor de 50 pone 2000
				anio = '20' + anio;
	    }
	else if (largo==8) //Si digito 8 caracteres dd/mm/yy
	    {
		if ((fecha.charAt(2) == "/" && fecha.charAt(5) == "/")  || (fecha.charAt(2) == "-" && fecha.charAt(2) == "-" ))
		{
			// Asigna dia, mes y año
			dia = fecha.charAt(0) + fecha.charAt(1);
			mes = fecha.charAt(3) + fecha.charAt(4);
			anio = fecha.charAt(6) + fecha.charAt(7);				
			if (anio >= 50) // Pone siglo, si es mayor de 50 pone 1900
				anio = '19' + anio;
			else			// Pone siglo, si es menor de 50 pone 2000
				anio = '20' + anio;
		}
		else // ddmmyyyy
		{
				// Asigna dia, mes y año
				dia = fecha.charAt(0) + fecha.charAt(1);
				mes = fecha.charAt(2) + fecha.charAt(3);
				anio = fecha.charAt(4) + fecha.charAt(5) + fecha.charAt(6) + fecha.charAt(7);
		}
	    }
	else if (largo==10) //Si digito 10 caracteres dd/mm/yyyy
		{
		// Asigna dia, mes y año
		dia = fecha.charAt(0) + fecha.charAt(1);
		mes = fecha.charAt(3) + fecha.charAt(4);
		anio = fecha.charAt(6) + fecha.charAt(7) + fecha.charAt(8) + fecha.charAt(9);
		}
	else
		{
		alert("Error en el formato de la fecha");
		objfecha.focus();
		objfecha.select();
		return false;
		}
	
	if (!isNumber(dia) || !isNumber(mes) || !isNumber(anio))
		{
		alert("Error en el formato de la fecha");
		objfecha.focus();
		objfecha.select();
		return false;
		}
	else
	{
	if (ValidaDiaMesAnio(dia,mes,anio)) // Si la fecha es valida
		{
		// Formatea fecha dd/mm/yyyy
		tmpfecha = dia + '/' + mes + '/' + anio;
		objfecha.value = tmpfecha;
		return true;
		}
	else
		{
		objfecha.focus();
		objfecha.select();
		return false;
		}
	}
}

// Falta valida año bisiesto 
function ValidaDiaMesAnio(dia, mes, anio)
{ 
	// Valida cantidad maxima de dias
	if (dia > 31) 
		{
		alert("El dia no corresponde");
		return false;
		}
	// Valida cantidad maxima de meses
	if (mes > 12)
		{
		alert("El mes no corresponde");
		return false;
		}
	// Valida los meses de 30 dias
	if ((mes==4||mes==6||mes==9||mes==11)&&(dia>30))
		{
		alert("El dia no corresponde, para el mes");
		return false;
		}
	// Valida febrero
	if (mes==2 && dia>28 && anio%4!=0) 
		{
		alert("Este año no es bisiesto");
		return false;		
		}		
	// Valida rango de año
	if (anio < 1950 || anio > 2050)
		{
		alert("El año esta fuera de rango");
		return false;
		}
	return true;
}

//Combierte a mayusculas
function Mayusculas(objtexto)
{
	var texto = objtexto.value;

	// Si esta vacio y puede estarlo
	if (isEmpty(texto))
		return true;
	objtexto.value = texto.toUpperCase();
	return true;
}

