var nav4 = window.Event ? true : false;

function validateNumber(evt)
{ 
  // NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57 
  var key = nav4 ? evt.which : evt.keyCode; 
  return (key <= 13 || (key >= 48 && key <= 57));
}

function validarLetraKey(evt)
{

  // NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57 
  var key = nav4 ? evt.which : evt.keyCode; 
  return ( key <= 13 || (key >= 65 && key <= 90)  || (key >= 97 && key <= 122) || (key == 32) || (key == 209) || (key == 241)  );
}

function validarAlfanumericoKey(evt)
{
 
  // NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57, '209' = ń, '241' = Ń , '47' = /  
  var key = nav4 ? evt.which : evt.keyCode; 
  return ( key <= 13 || (key >= 65 && key <= 90)  || (key >= 97 && key <= 122) || (key >= 48 && key <= 57) || (key == 32) || (key == 209) || (key == 241) || (key == 38) || (key == 47));
  
}

function validateEmail(control ) {
  if( control.value == "" )
    return (true);
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(control.value)){
   return (true)
  } else {
control.focus();
   alert("Invalid Email, correct format juanperez@yahoo.com") 
   return (false);
  }
 }

function validateZipCode(control ) {
  control.value = control.value.toUpperCase();

  if( control.value == "" )
    return (true);
  if (/^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$/.test(control.value)){
   return (true)
  } else {
control.focus();
   alert("Invalid Zip Code correct format For US 5 digits like 85123,for Canada a format like R2G 1T9") 
   return (false);
  }
 }

function validarLongitudCadena( control, longitud ) {

  if( parseInt(control.value.length) < parseInt(longitud) )
    return (true);
  else
    return (false);
  
}

function lenIdentificacion( pais, tipoId )
{
  if( pais == "CHILE" )
  {
   	if( tipoId == "R" ) 
	  return 10;
	if( tipoId == "P" ) 
	  return 14;
  }
  
  if( pais == "ECUADOR" )
  {
   	if( tipoId == "C" ) 
	  return 10;
	if( tipoId == "R" ) 
	  return 13;
	if( tipoId == "P" ) 
	  return 14;
  }

  if( pais == "PARAGUAY" )
  {
   	if( tipoId == "C" ) 
	  return 11;
	if( tipoId == "R" ) 
	  return 11;
	if( tipoId == "P" ) 
	  return 14;
  }

  if( pais == "BOLIVIA" )
  {
   	if( tipoId == "C" ) 
	  return 14;
	if( tipoId == "R" ) 
	  return 14;
	if( tipoId == "P" ) 
	  return 14;
  }

  return 14;
}

function esIdentificacionValida( pais, tipoId, identificacion )
{
  

  if( pais == "ECUADOR" )
  {
    if( tipoId == "C" ) 
      return esCedulaValidaEcuador( identificacion );
  }

  if( pais == "PARAGUAY" )
  {
    if( tipoId == "C" ) 
      return esCedulaValidaParaguay( identificacion );
  }
  
  if( pais == "CHILE" )
  {
      //if( tipoId == "C" ) 
      return esIdentificacionValidaChile( identificacion );
  }
  
  return true;
}


function esCedulaValidaEcuador( cedula )
{
	if( cedula == "" )
		return true;

	var digitoControl = 0;
	var digito = 0;
	var sumando = 0; 
	var sumatoria = 0;
		
	// Obtiene la sumatoria de los 9 primeros digitos
	for( i = 0 ; i < 9 ; i++ )
	{
		digito = parseInt( cedula.substring(i,i+1) );
			
		if( i % 2 != 0 )
			sumando = digito;
		else
		{
			sumando = 2 * digito;
			if( sumando >= 10 )
				sumando = sumando - 9;
		}
		sumatoria += sumando;
	}
		
	// Obtiene el digito de control
	if( sumatoria % 10 == 0 )
		digitoControl = 0;
	else
		digitoControl = 10 - ( sumatoria % 10 );
				
	// Comrueba el digito de control con el decimo digito de la cedula
	if( digitoControl != parseInt( cedula.substring(9,10) ) )
	{
		alert( "La cédula ingresada no es válida." );
		return false;
	}

	return true;
}


function esCedulaValidaParaguay( cedula )
{
	if( cedula == "" )
		return true;

	var lenCedula = lenIdentificacion( "PARAGUAY", "C" );

	if ( cedula.length < lenCedula )
	{
		alert( "La cédula ingresada no es válida. Debe tener una longitud de " + lenCedula );
		return false;	
	}
		
	// Obtiene la sumatoria de los 9 primeros digitos
	for( i = 0 ; i < 4 ; i++ )
	{
		if ( ! isNaN( cedula.substring(i,i+1) ) )
		{
			alert( "La cédula ingresada no es válida. Formato Ej: MOTI0004567" );
			return false;	
		}
		//break;
	}

	for( i = 4 ; i < 10 ; i++ )
	{
		if ( isNaN( cedula.substring(i,i+1) ) )
		{
			alert( "La cédula ingresada no es válida. Formato Ej: MOTI0004567" );
			return false;	
		}
		//break;
	}
	
	return true;
}

function esIdentificacionValidaChile( identificacion )
{
	var tmpstr = "";
	var intlargo = identificacion
	 if (intlargo.length > 0){ 	
		
			crut = identificacion 
			largo = crut.length;
		
		if ( largo < 2 )
		{
			alert('rut inválido')
			//Objeto.focus()
			return false;
		}
		for ( i=0; i < crut.length ; i++ )
					if ( crut.charAt(i) != ' ' && crut.charAt(i) != '.' && crut.charAt(i) != '-' )
					{
					tmpstr = tmpstr + crut.charAt(i);
					}
				rut = tmpstr;
		crut=tmpstr;
		largo = crut.length;
	
		if ( largo > 2 )
			rut = crut.substring(0, largo - 1);
		else
			rut = crut.charAt(0);
	
		dv = crut.charAt(largo-1);
	
		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 Ingreso es Invalido')
		//Objeto.focus()
		return false;
		}
		//alert('El Rut Ingresado es Correcto!')
		//Objeto.focus()
		return true;
	  }   
}


function IsNumeric(expression) {
	
	var nums = "0123456789";
	
	if (expression.length==0)return(false);
	
	for (var n=0; n < expression.length; n++){
	
	if(nums.indexOf(expression.charAt(n))==-1)return(false);
	
	
	}
	
	return(true);

}
