/*
==================================================================
checkDocument(CI) : Checks the validaty of the given document
==================================================================
*/
function checkDocument(CI) {
	value = replace(replace(replace(CI,".",""),"-",""),"/","");
	if ((value.length < 7) || (isNumeric(value) == false))
		return(false);
		
	chkDgt = value.substr(value.length-1,1);
	chkDgt2 = Math.floor((value % 100) / 10) * 4;
	chkDgt2 = chkDgt2 + Math.floor((value % 1000) / 100) * 3;
	chkDgt2 = chkDgt2 + Math.floor((value % 10000) / 1000) * 6;
	chkDgt2 = chkDgt2 + Math.floor((value % 100000) / 10000) * 7;
	chkDgt2 = chkDgt2 + Math.floor((value % 1000000) / 100000) * 8;
	chkDgt2 = chkDgt2 + Math.floor((value % 10000000) / 1000000) * 9;
	chkDgt2 = chkDgt2 + Math.floor((value % 100000000) / 10000000) * 2;
	chkDgt2 = (chkDgt2 % 10);
    	
	if (chkDgt2 != 0)
		chkDgt2 = 10 - chkDgt2
    		
	if ("'" + chkDgt + "'" != "'" + chkDgt2 + "'")
		return(false);
	else
		return(true);
}

/*
==================================================================
replace(data, searchstr, newstr) : Replaces in the given string
all occurrences of searchstr with newstr
==================================================================
*/
function replace(data, searchstr, newstr) {
	// same as replace in vb
	var newtext = "";
	for (i=0; i < data.length; i++) {
		caracter = data.substring(i,i+1);
		if (caracter == searchstr) {
			newtext = newtext + newstr;
		} else {
			newtext = newtext + caracter;
		}
	}
	return(newtext);
}

/*
==================================================================
isNumeric(valor): Checks if the value is a valid number
==================================================================
*/
function isNumeric(valor) {
// Funcion que controla que 'valor' sea numerico
	for (z=0; z < valor.length; z++) {
		caracter = valor.substr(z,1);
		if ((caracter != "0") && (caracter != "1") &&
			(caracter != "2") && (caracter != "3") &&
			(caracter != "4") && (caracter != "5") &&
			(caracter != "6") && (caracter != "7") &&
			(caracter != "8") && (caracter != "9") &&
			(caracter != ".") && (caracter != ",")) {
				return(false);
		}
	}
	return(true);
}	

/*
==================================================================
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));
}

