/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}



function FormatearNumero(num,msg) 
{//*******************************************************************************************

var er1 = /^(\d{1,3})(\.\d{3})*(\,\d+)?$/ig;
var er2 = /^(\d+)(\,\d+)?$/ig;

var cadexp = num ;
cadena=num;

mensaje="Por favor, ingrese una cifra válida.\nUtilice punto (.) para miles y coma (,) para decimales.\n\nEjemplo: 87.544,12";

//var mensaje = new String(msg);

/*if (msg == undefined)
{
	mensaje="Por favor, ingrese una cifra válida.\nUtilice punto (.) para miles y coma (,) para decimales.\n\nEjemplo: 87.544,12";
	}
else
{ 
	mensaje=msg;
}
*/

if(!er1.test(cadexp)) {
	if(!er2.test(cadexp)) {
	       alert(mensaje);
		cadena='';
	} 
	else {
		var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');	
		var arrNumber = cadexp.split(',');
	
		arrNumber[0] += ',';
		do {
			arrNumber[0] = arrNumber[0].replace(rxSplit, '$1.$2');
		} while (rxSplit.test(arrNumber[0]));
		var cadena = arrNumber.join('');
	
		if (cadena.charAt(cadena.length-1)==',') {
			cadena=cadena.substring(0,cadena.length-1);
		}
	}
}
return cadena;
}

function FormatearNorma(num) 
{

var er1 = /^(\s*\d+)(\s*\,\s*\d+)*$/ig; // sintaxis para las ","
var er2 = /^(\s*\d+)(\s*\/\s*\d+)$/ig;	// sintaxis para los "/"

var cadena=''

num=Trim(num);

if(!er1.test(num)) 
{
	if(!er2.test(num)) 
	{
	       alert('Por favor, indique "coma" (,) para enumerar y "slash" (/) para rangos.\n\nEjemplos:\n\tN° de Orden\n\t1, 3, 5\t\t(enumeración)\n\t1/5\t\t(rango)');
//		return (false);
	} 
	else
	{
		var arrNumber = num.split('/');
		cadena=trim(arrNumber[0]);
	
		for (i=1;i<arrNumber.length;i++)
		{
			cadena=cadena+'/ '+trim(arrNumber[i]);
		}

	}

}
else
{
	var arrNumber = num.split(',');
	cadena=trim((arrNumber[0]));

	for (i=1;i<arrNumber.length;i++)
	{
		cadena=cadena+', '+trim((arrNumber[i]));
	}

}

return (cadena);
}


/*Funcion que elimina espacios*/
function Trim(strTxt)
	{
	pos1 = 0;
	pos2 = strTxt.length-1;
	for (i=0; i<strTxt.length; i++)
	{
		if (strTxt.charAt(i) == ' ') 
			{pos1 = pos1 + 1}
		else {break}
	}
	if (pos1 != (pos2+1))
		for (i=strTxt.length-1; i>0; i--)
		{
			if (strTxt.charAt(i) == ' ')
			{ pos2 = pos2 - 1}
			else {break}
		}
	return strTxt.substring(pos1,pos2+1);
	}

function IsNumero(entry)
{
	if (String(entry*1)=="NaN")
	{
		return false
	}
	return true;
}

/*Funcion que revisa dir. email*/
function isEmail(string) {

   if (!string) return false;
   var iChars = "*|,\":<>[]{}`\';()&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         {return false}
   }
   return true;
}                      
function isProper(string) {

   if (!string) return false;
   var iChars = "*|,\":<>[]{}`\';()@&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         {return false}
   }
   return true;
} 
                     
function isReady(obj1,obj2) {
    if (isEmail(obj1.value) == false) {
        /*alert("Please enter a valid email address.");*/
        return false;
    }
    if (isProper(obj2.value) == false) {
        /*alert("Please enter a valid username.");*/
        return false;
    }
    return true;
}

/*Fechas*/
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isFecha (day,month,year) {
    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        {return true}
    else
        {return false}
}

/*Function Codigo Sofofa*/
function checknaladisa(texto)
{
 texto=Trim(texto);
 largo = texto.length;
 if ((largo < 9) || (largo>10))
 {
	 return 0	
 }
 else
 {	
	if ((largo==10) && (IsNumero(texto.charAt(0)) && IsNumero(texto.charAt(1)) && IsNumero(texto.charAt(2)) && IsNumero(texto.charAt(3)) && texto.charAt(4)=='.' && IsNumero(texto.charAt(5)) && IsNumero(texto.charAt(6)) && texto.charAt(7)=='.' && IsNumero(texto.charAt(8)) && IsNumero(texto.charAt(9)) ))
	{
		return 1
	}	
	else
	{
	 if ((largo==10) && (IsNumero(texto.charAt(0)) && IsNumero(texto.charAt(1)) && texto.charAt(2)=='.' && IsNumero(texto.charAt(3)) && IsNumero(texto.charAt(4)) && texto.charAt(5)=='.' && IsNumero(texto.charAt(6)) && texto.charAt(7)=='.' && IsNumero(texto.charAt(8)) && IsNumero(texto.charAt(9))))
	 {
	    return 2
	 }
	 else
	 {
	   if ((largo==9) && ( IsNumero(texto.charAt(0)) && IsNumero(texto.charAt(1)) && IsNumero(texto.charAt(2)) && IsNumero(texto.charAt(3)) && texto.charAt(4)=='.' && IsNumero(texto.charAt(5)) && IsNumero(texto.charAt(6)) && IsNumero(texto.charAt(7)) && IsNumero(texto.charAt(8)) ))
	   {
	        return 3
		}
			else
			{
				return 0
			}
       }	
	}
  }	
}
/*Funcion que revisa que valor sea Numero o % */
function checkValor(texto)
{
 texto=Trim(texto);
 largo = texto.length;
 for (i=0; i < largo ; i++ )
 { 
  if ( !IsNumero(texto.charAt(i)) && texto.charAt(i) != "%" && texto.charAt(i) != "."&& texto.charAt(i) != ",")
	{
	    return false
	}
 }
 return true;
}

/*Funcion que revisa busca comas en textos */
function checkComa(texto)
{
 texto=Trim(texto);
 largo = texto.length;
 var sincoma = "";
 for (i=0; i < largo ; i++ )
 { 
  if ( texto.charAt(i) == "," )
	{
    sincoma = sincoma + "~";
	}
  else
  {
    sincoma = sincoma + texto.charAt(i);
  }
 }
 return sincoma;
}

function FormatoRut(texto)
{	var tmpstr = "";
	var tmpstr2 = "";
	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;

  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;
}

