/*
 *
 * File: validate.js
 *
 * Performs JavaScript validations in client side
 *
 * Developed by:
 *     DMR Consulting
 *     Vitacura 2939 - Piso 7 - Las Condes
 *     Santiago - Chile
 *     Phone: 56-2-4215300, Fax: 56-2-4214311
 *     www.dmr-consulting.cl
 *
 */


// Validates a string that contains a date with format p_format
function validate_date( p_objDate, p_format ) {

    var strDate = p_objDate.value;

    if (p_objDate.disabled) {
        return true;
    }

	if ( p_format == "MM/yyyy" ) {

		// Analyzing format
		if (strDate == null || strDate == "" || strDate.length != 7) {
			return validate_returnFalse(p_objDate);
		}

		if (strDate.charAt(2) != '/') {
			return validate_returnFalse(p_objDate);
		}

		if ( !validate_isInt(strDate.substring(0,2)) ) {
			return validate_returnFalse(p_objDate);
		}

		if ( !validate_isInt(strDate.substring(3,7)) ) {
			return validate_returnFalse(p_objDate);
		}

		// Analyzing ranges
		var year = Number(strDate.substring(3,7));
		var month = Number(strDate.substring(0,2) - 1);
		var date = new Date( year, month, 1 );

		if ( year != date.getFullYear() ) {
			return validate_returnFalse(p_objDate);
		}

		if ( month != date.getMonth() ) {
			return validate_returnFalse(p_objDate);
		}

		return true;
	}

	if ( p_format == "dd/MM/yyyy" ) {

		// Analyzing format
		if (strDate == null || strDate == "" || strDate.length != 10) {
			return validate_returnFalse(p_objDate);
		}

		if (strDate.charAt(2) != '/' || strDate.charAt(5) != '/') {
			return validate_returnFalse(p_objDate);
		}

		if ( !validate_isInt(strDate.substring(0,2)) ) {
			return validate_returnFalse(p_objDate);
		}

		if ( !validate_isInt(strDate.substring(3,5)) ) {
			return validate_returnFalse(p_objDate);
		}

		if ( !validate_isInt(strDate.substring(6,10)) ) {
			return validate_returnFalse(p_objDate);
		}

		// Analyzing ranges
		var year = Number(strDate.substring(6,10));
		var month = Number(strDate.substring(3,5) - 1);
		var day = Number(strDate.substring(0,2));
		var date = new Date( year, month, day );

		if ( day != date.getDate() ) {
			return validate_returnFalse(p_objDate);
		}

		if ( month != date.getMonth() ) {
			return validate_returnFalse(p_objDate);
		}

		if ( year != date.getFullYear() ) {
			return validate_returnFalse(p_objDate);
		}

		return true;
	}

	return false;
}


// Compares two dates with format p_format
function validate_isDateLess(p_strDate1, p_strDate2, p_format)
{
    var date1, date2;

	if ( p_format == "MM/yyyy" ) {
    	date1 = new Date(p_strDate1.substring(3,7), p_strDate1.substring(0,2) - 1 , 1);
	    date2 = new Date(p_strDate2.substring(3,7), p_strDate2.substring(0,2) - 1 , 1);
    	return (date1.getTime() < date2.getTime());
    }

	if ( p_format == "dd/MM/yyyy" ) {
    	date1 = new Date(p_strDate1.substring(6,10), p_strDate1.substring(3,5) - 1 , p_strDate1.substring(0,2));
    	date2 = new Date(p_strDate2.substring(6,10), p_strDate2.substring(3,5) - 1 , p_strDate2.substring(0,2));
    	return (date1.getTime() < date2.getTime());
    }

    return false;

}

// Compares two dates with format p_format
function validate_isDateLessEqual(p_strDate1, p_strDate2, p_format)
{
    var date1, date2;

	if ( p_format == "MM/yyyy" ) {
    	date1 = new Date(p_strDate1.substring(3,7), p_strDate1.substring(0,2) - 1 , 1);
	    date2 = new Date(p_strDate2.substring(3,7), p_strDate2.substring(0,2) - 1 , 1);
    	return (date1.getTime() <= date2.getTime());
    }

	if ( p_format == "dd/MM/yyyy" ) {
    	date1 = new Date(p_strDate1.substring(6,10), p_strDate1.substring(3,5) - 1 , p_strDate1.substring(0,2));
    	date2 = new Date(p_strDate2.substring(6,10), p_strDate2.substring(3,5) - 1 , p_strDate2.substring(0,2));
    	return (date1.getTime() <= date2.getTime());
    }

    return false;

}

// Compares two dates with format p_format
function validate_isDateGreater(p_strDate1, p_strDate2, p_format)
{
    var date1, date2;

    if ( p_format == "MM/yyyy" ) {
        date1 = new Date(p_strDate1.substring(3,7), p_strDate1.substring(0,2) - 1 , 1);
        date2 = new Date(p_strDate2.substring(3,7), p_strDate2.substring(0,2) - 1 , 1);
        return (date1.getTime() > date2.getTime());
    }

    if ( p_format == "dd/MM/yyyy" ) {
        date1 = new Date(p_strDate1.substring(6,10), p_strDate1.substring(3,5) - 1 , p_strDate1.substring(0,2));
        date2 = new Date(p_strDate2.substring(6,10), p_strDate2.substring(3,5) - 1 , p_strDate2.substring(0,2));
        return (date1.getTime() > date2.getTime());
    }

    return false;

}


// Compares dates with format p_format, that should be inside a range of dates
function validate_isBetweenDates(p_strDate1, p_strDate2, p_strDate3, p_format)
{
    var date1, date2, date3;

	if ( p_format == "MM/yyyy" ) {
    	date1 = new Date(p_strDate1.substring(3,7), p_strDate1.substring(0,2) - 1 , 1);
	    date2 = new Date(p_strDate2.substring(3,7), p_strDate2.substring(0,2) - 1 , 1);
			date3 = new Date(p_strDate3.substring(3,7), p_strDate3.substring(0,2) - 1 , 1);
    	return (date1.getTime() >= date2.getTime() && date3.getTime() >= date1.getTime());
    }

	if ( p_format == "dd/MM/yyyy" ) {
    	date1 = new Date(p_strDate1.substring(6,10), p_strDate1.substring(3,5) - 1 , p_strDate1.substring(0,2));
    	date2 = new Date(p_strDate2.substring(6,10), p_strDate2.substring(3,5) - 1 , p_strDate2.substring(0,2));
    	date3 = new Date(p_strDate3.substring(6,10), p_strDate3.substring(3,5) - 1 , p_strDate3.substring(0,2));
    	return (date1.getTime() >= date2.getTime() && date3.getTime() >= date1.getTime());
    }
    return false;
}

// Validates that the object p_objField is an integer
function validate_isAnyInteger(p_objField) {

    if ( !validate_isAnyInt(p_objField.value) ) {
        return validate_returnFalse(p_objField);
    }

    return true;

}


// Validates that the parameter p_str is an integer
function validate_isAnyInt(p_str)
{
	var i;
	var len = p_str.length;

	if ( (p_str.charAt(0) > '9' || p_str.charAt(0) < '0') && p_str.charAt(0) != '-' ) {
		return false;
	}

	for (i=1; i<len; i++) {
		if ( p_str.charAt(i) > '9' || p_str.charAt(i) < '0' ) {
			return false;
		}
	}

	return true;
}

// Validates that the object p_objField is a positive integer
function validate_isInteger(p_objField) {

    if ( !validate_isInt(p_objField.value) ) {
        return validate_returnFalse(p_objField);
    }

    return true;

}


// Validates that the parameter p_str is a positive integer
function validate_isInt(p_str)
{
	var i;
	var len = p_str.length;

	for (i=0; i<len; i++) {
		if ( p_str.charAt(i) > '9' || p_str.charAt(i) < '0' ) {
			return false;
		}
	}

	return true;
}

// Validates that the parameter p_str is a positive number
function validate_isNumber(p_str)
{
	var i;
	var len = p_str.length;
	var numSeparDec = 0;

    if (len<1) {
        return false;
    }

	for (i=0; i<len; i++) {
		if ( p_str.charAt(i) > '9' || p_str.charAt(i) < '0' ) {
			if ( p_str.charAt(i) == ',' || p_str.charAt(i) == '.') {
				numSeparDec++;
				if ( numSeparDec > 1 ) {
                    return false;

				}
			} else {
				return false;
			}
		}
	}

	return true;
}

function validate_isNumberDecimal(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


// Validates that the object p_objField is a positive with decimal and less 100%
function validate_isPercentage(p_objField) {

    var strNum = p_objField.value;
    var str;
    var ch;
    var len = strNum.length;

    if ( !validate_isNumberDec(p_objField.value) ) {
        return validate_returnFalse(p_objField);
    }

    strNum = strNum.replace(",",".");
    p_objField.value = strNum;

    if ((strNum.length == 1 )&& (strNum.indexOf(".") == 0) ){
        p_objField.value = 0;
        return true;
    }

    if (Number(strNum) > 100)  {
        return validate_returnFalse(p_objField);
    }

    if ( strNum.indexOf(".") != -1 && strNum.length - strNum.indexOf(".") > 3 ) {
        return validate_returnFalse(p_objField);
    }

    return true;


}

// Validates that the object p_objField is amount a positive with decimal
function validate_isAmount(p_objField) {

    var strNum = p_objField.value;

    if ( !validate_isNumberDec(p_objField.value) ) {
        return validate_returnFalse(p_objField);
    }

    strNum = strNum.replace(",",".");
    p_objField.value = strNum;

    if ((strNum.length == 1 )&& (strNum.indexOf(".") == 0) ){
        p_objField.value = 0;
        return true;
    }

    if (Number(strNum) >= 100000000)  {
        return validate_returnFalse(p_objField);
    }

    if ( strNum.indexOf(".") != -1 && strNum.length - strNum.indexOf(".") > 5 ) {
        return validate_returnFalse(p_objField);
    }

    return true;

}

// Validates that the object p_objField is a positive with decimal
function validate_isDouble(p_objField) {

    if ( !validate_isNumberDec(p_objField.value) ) {
        return validate_returnFalse(p_objField);
    }

    return true;

}

// Validates that the parameter p_str is a decimal number
function validate_isNumberDec(p_str)
{
	var i;
	var len = p_str.length;
	var numSeparDec = 0;

    if (len<1) {
        return false;
    }

/*    if (len=1 && (p_str == '.' || p_str == ',')) {
        return false;
    }*/

	var isNegative = (p_str.charAt(0) == '-');

	for (i=((isNegative) ? 1:0); i<len; i++) {
		if ( p_str.charAt(i) > '9' || p_str.charAt(i) < '0' ) {
			if ( p_str.charAt(i) == '.'  || p_str.charAt(i) == ',') {
				numSeparDec++;
				if ( numSeparDec > 1 ) {
      	  return false;
				}
			} else {
				return false;
			}
		}
	}

	return true;
}




// Changes the enable state of a button
function validate_enableButton(p_objButton, p_state) {

	if (p_objButton.type != "button") {
		return;
	}

	p_objButton.disabled = !p_state;
	if (p_state) {
		p_objButton.className = "NORMALBUTTON";
	} else {
		p_objButton.className = "DISABLEDBUTTON";
	}
}

// Validates that a field is not empty
function validate_notEmpty( p_objField ) {

	if (p_objField.length == 0) {
		return validate_returnFalse(p_objField);
	}

	return true;
}

// Validates that a field (Textarea) is not empty
function validate_notEmptyText( p_objField ) {

	if (p_objField.value == "") {
		return validate_returnFalse(p_objField);
	}

	return true;
}

// Validates that a field (select) is selected
function validate_select( p_objField ) {

	if (p_objField.value == -1 || p_objField.value == "") {
		return validate_returnFalse(p_objField);
	}

	return true;
}

// Validates that all characters of a field are in a valid char set
function validate_validChars( p_objField ) {

	var strValids = "0123456789ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚÀÈÌÒÙÄËÏÖÜÂÊÎÔÛÇabcdefghijklmnñopqrstuvwxyzáéíóúàèìòùäëïöüâêîôûç -_/@$%&()+#*'{}.,";

	return validate_chars( p_objField, strValids );
}

// Validates that all characters of a field are in a valid char set for a file name
function validate_validFileName( p_objField ) {

	var strValids = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.[]$(){}|";

  if ( !validate_chars(p_objField, strValids) ) {
      return validate_returnFalse(p_objField);
  }

	var strFileName = p_objField.value;

	var OPEN_CHARS = "{(";
	var CLOSE_CHARS = ")}";
	var strNameValids = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.";
	var keywords = ["$operation", "$folio", "$date", "$user"];

  var cont = true;

	var indexOpen = strFileName.indexOf(OPEN_CHARS)
	var indexClose = -1;
	// Si se abre las llaves, debe cerrarse

	if (indexOpen == -1) {
		return true;	
	}

	// Revisando los caracteres antes de las llaves
  if ( !validate_charsInString(strFileName.substring(0,indexOpen), strNameValids) ) {
      return validate_returnFalse(p_objField);
  }	

  var currIndex = indexOpen;

  while (cont) {
    indexOpen = strFileName.indexOf(OPEN_CHARS, currIndex);

    if (indexOpen == -1) {
      cont = false;
    } else {

			// Revisando los caracteres fuera de las llaves
		  if ( indexClose != -1) {
		  	if ( !validate_charsInString(strFileName.substring(indexClose+CLOSE_CHARS.length,indexOpen), strNameValids) ) {
		      return validate_returnFalse(p_objField);
		    }
		  }	

      indexClose = strFileName.indexOf(CLOSE_CHARS, indexOpen);

			// La llave no se cierra
      if (indexClose == -1) {
        return validate_returnFalse(p_objField);
      } else {
      	// Que contenga una de las llaves
      	var anyKeyword = false;
      	var inString = strFileName.substring(indexOpen+OPEN_CHARS.length, indexClose);
      	for (var j=0; j<keywords.length; j++) {
      		if (inString.indexOf(keywords[j]) != -1) {
      			anyKeyword = true;
      		}
      	}
      	
		  	if ( !anyKeyword ) {
		      return validate_returnFalse(p_objField);
		    }      	
      }

      currIndex = indexClose + CLOSE_CHARS.length;
    }
  }

	// Revisando los caracteres después de las llaves
  if ( !validate_charsInString(strFileName.substring(indexClose+CLOSE_CHARS.length), strNameValids) ) {
      return validate_returnFalse(p_objField);
  }	


	return true;
}

//  Validates that all characters of a field are letters or numbers
function validate_alphanumeric( p_objField ) {

	var strValids = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	return validate_chars( p_objField, strValids );

}

// Valida que los caracteres de un campo sean el nombre de una persona
function validate_personName( p_objField ) {

	var strValids = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚÀÈÌÒÙÄËÏÖÜÂÊÎÔÛÇabcdefghijklmnñopqrstuvwxyzáéíóúàèìòùäëïöüâêîôûç' ";
	return validate_chars( p_objField, strValids );

}

// Valida que los caracteres de un campo sean válidos para una password
function validate_password( p_objField ) {

	var strValids = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' .,;:-_?/@$%&()=+!#";
	return validate_chars( p_objField, strValids );

}

// Validates that p_str is an identifier (first char is a letter, and rest is alphanumeric
function validate_isIdentifier(p_objField)
{
	var i;
	var len = p_objField.value.length;

  if (len<1) {
      return validate_returnFalse(p_objField);;
  }

	var strValidsField = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";
	var validField = validate_chars( p_objField, strValidsField );

	if (!validField) {
		return validate_returnFalse(p_objField);
	}

	var firstChar = p_objField.value.charAt(0);

	if ((firstChar < 'a' || firstChar > 'z') &&
      (firstChar < 'A' || firstChar > 'Z') &&
       firstChar != "_") {
		return validate_returnFalse(p_objField);
	}

	return true;
}

function validate_url( p_objField ) {

  var strField = p_objField.value;

	if (strField.length < 7) {
    return validate_returnFalse( p_objField);
	}

	if (strField.indexOf("http://") != 0 && strField.indexOf("https://") != 0) {
    return validate_returnFalse( p_objField);
	}

  var strValids = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,;:-_?/@$%&()=+!#{}[]~";
  return validate_chars( p_objField, strValids );


}

//  Validates that all characters of a field are letters or numbers
function validate_listCodes( p_objField ) {

    var strField = p_objField.value;

    // Validating comma position
    if ( strField.indexOf(",,") != -1 || strField.charAt(0) == "," || strField.charAt(strField.length-1) == ",") {
        return validate_returnFalse( p_objField);
    }

    // Clean spaces into field
    strField = strField.replace(" ", "");
    p_objField.value = strField;

    // Validating repeated values
    if (!validate_areRepeated( strField )) {
        return validate_returnFalse( p_objField);
    }

    var strValids = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-,";
    return validate_chars( p_objField, strValids );

}

function validate_areRepeated( p_strField ) {

    var beginIndex = -1;
    var endIndex = p_strField.indexOf(",");

    if ( endIndex == -1 ) {
        return true;
    }

    do {

        var item = p_strField.substring(beginIndex+1, endIndex);

        if ( p_strField.indexOf( item ) != p_strField.lastIndexOf( item ) ) {
            return false;
        }

        beginIndex = p_strField.indexOf(",", endIndex );
        endIndex = p_strField.indexOf(",", beginIndex + 1 );
        if (endIndex == -1) {
            endIndex = p_strField.length;
        }
    } while ( beginIndex != -1 )

    return true;
}


//  Validates that all characters of a field are letters
function validate_alpha( p_objField ) {

	var strValids = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚÀÈÌÒÙÄËÏÖÜÂÊÎÔÛÇabcdefghijklmnñopqrstuvwxyzáéíóúàèìòùäëïöüâêîôûç ";
	return validate_chars( p_objField, strValids );

}

// Validates that all characters of a field are separated by comma and are letters, numbers or spaces
function validate_commaSeparatedStrings( p_objField ) {

	var strField = p_objField.value;

	// The field could be empty
	if (strField == "") {
		return true;
	}

	if ( strField.indexOf(",,") != -1 || strField.charAt(0) == "," || strField.charAt(strField.length-1) == ",") {
        return validate_returnFalse( p_objField);
    }


	var strValidsField = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,_ ";
	var validField = validate_chars( p_objField, strValidsField );

	if (!validField) {
		return validate_returnFalse(p_objField);
	}



	var arrStrField = p_objField.value.split(",");

	// Validates that all substring separated by comma are not repeats
	if (arrStrField.length > 1) {
		for (var i=0;i<arrStrField.length-1; i++) {
			for(var j=i+1; j<arrStrField.length; j++) {
				if ( validate_trim(arrStrField[i]) == validate_trim(arrStrField[j]) ) {
					return false;
				}
			}
		}
	}


	// Validates that all characters of a substring are in a valid char set
	var strValidsField = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";

	for (var i=0; i< arrStrField.length; i++) {

		var strTrim = validate_trim(arrStrField[i]);

		var validString = validate_validCharsInString(strTrim,strValidsField);

		if (!validString) {

			return validate_returnFalse(p_objField);
		}
	}
 return true;
}

// Trims for the right
function validate_rTrim( p_str )
{
	var str = "";
	var len = p_str.length;
	var i=len-1;

	while ( i>=0 && p_str.charCodeAt(i) == 32)
	{
		i--;
	}

	var str = p_str.substring( 0, i+1 );

	var strTrim = validate_lTrim( str );

	return strTrim;
}

// Trims for the left
function validate_lTrim( p_str )
{
	var str = "";
	var len = p_str.length;
	var i=0;

	while ( i<len && p_str.charCodeAt(i) == 32)
	{
		i++;
	}

	var str = p_str.substring( i, len );
	return str;
}

// Trims the object's value
function validate_trim( p_strField )
{
	var strTrim = validate_rTrim( p_strField );
	return strTrim;
}


// Validates that all characters of a field are in the char set p_chars
function validate_chars( p_objField, p_chars ) {

	var i;
	var strField = p_objField.value;
	var len = strField.length;

	if (strField == null || strField == "") {
			return validate_returnFalse(p_objField);
	}

	for (i=0; i<len; i++) {
		if ( p_chars.indexOf( strField.charAt(i) ) == -1 ) {

				return validate_returnFalse(p_objField);
		}
	}

	return true;
}

// Validates that all characters of a string are in the char set p_chars
function validate_charsInString( p_strField, p_chars ) {

	var i;
	var len = p_strField.length;

	if (p_strField == null || p_strField == "") {
			return true;
	}

	for (i=0; i<len; i++) {
		if ( p_chars.indexOf( p_strField.charAt(i) ) == -1 ) {

				return false;
		}
	}

	return true;
}

// Validates that all characters of a string are in the char set p_chars
function validate_validCharsInString( p_strField, p_chars ) {

	var i;
	var len = p_strField.length;

	// Si el string empieza con un numero
	var numbers = "0123456789";
	if (numbers.indexOf(p_strField.charAt(0)) != -1) {
		return false;
	}


	for (i=0; i<len; i++) {
		if ( p_chars.indexOf( p_strField.charAt(i) ) == -1 ) {
			return false;
		}

	}

	return true;
}

// Valida que una opción del combo box sea seleccionada
function validate_select( p_objSel ) {

	// Checks that object is a select
	if (p_objSel.type != "select-one") {
        p_objSel.focus();
    	return;
	}

	// Validates that user has selected any option
	if ( p_objSel.selectedIndex == 0 ) {
        p_objSel.focus();
		return false;
	}

	return true;

}

// Valida que una opción de los radio buttons sea seleccionada
function validate_radio( p_objField ) {

  // Si no tiene opciones, cumple
  if (p_objField.length == 0) {
  	return true;
  }

  // Si tiene una opción, no es un arreglo
  if (p_objField.length == 1) {
		if (p_objField.checked) {
    	return true;
		}
    p_objField.focus();
    return false;
  }
  // Si tiene más de una opción
  else {
    for (var i=0; i<p_objField.length; i++) {
    	if (p_objField[i].checked) {
        return true;
      }
    }
    p_objField[0].focus();
    return false;
  }

	return true;

}


function validate_returnFalse( p_objField ) {

    if (p_objField.type != "hidden" && p_objField.style && p_objField.style.display != 'none') {
        p_objField.focus();
        p_objField.select();
    }

    return false;
}


// Count number the rows of a table (not the headers)
function validate_numberRows( p_text ) {

    var text = p_text;
    var result = 0;
    var indexFirst = text.indexOf("<TR>");
    var beginIndex = text.indexOf("<TR>", indexFirst + 1) ;
    var endIndex;

    // If there are rows to delete
    if (beginIndex != -1) {
        result = 1;
    }

    return result;

}

//Compares two dates like string
function validate_isStringLess(p_str, p_strDate, p_format ) {

    if ( p_format == "dd/MM/yyyy" ) {

        var year = p_strDate.substring(6,10);
        var month = p_strDate.substring(3,5);
    }

    str = year + month;

    if (p_str < str ) {
        return false;
    }

    return true;

}

//Compares two dates like string
function validate_isStringGreater(p_str, p_strDate, p_format ) {

    if ( p_format == "dd/MM/yyyy" ) {

        var year = p_strDate.substring(6,10);
        var month = p_strDate.substring(3,5);
    }

    str = year + month;

    if (p_str > str ) {
        return false;
    }

    return true;

}

// Validates the Id of Chile (RUT)
function validate_ID_CL( p_objId ) {

    var strUId = p_objId.value;

    if ( strUId == null || strUId == "" || strUId.length < 2 ) {
        return validate_returnFalse(p_objId);
    }

    var strRol = strUId.substring(0, strUId.length - 1);
    var strDv = strUId.charAt(strUId.length - 1);

	if ( !validate_isInt(strRol) ) {
		return validate_returnFalse(p_objId);
	}

	if ( !validate_isInt(strRol) && strRol.toUpperCase() != "K" ) {
		return validate_returnFalse(p_objId);
	}

	var dvr = '0';
	var suma = 0;
	var mul  = 2;

	for(var pos = (strRol.length - 1) ; pos >= 0; pos--) {
        suma += strRol.charAt(pos) * mul;
        if(mul == 7) {
		    mul = 2;
        } else {
			mul++;
        }
    }

	var res = suma % 11;
	if (res == 1) {
		dvr = 'K';
	} else if (res == 0) {
        dvr = '0';
	} else {
        dvr = 11-res + "";
    }

	if( dvr != String(strDv).toUpperCase() ) {
        return validate_returnFalse(p_objId);
    }

    return true;
}


// Validates the Id of Bolivia (RUT)
function validate_ID_BO( p_objId ) {

    p_objId.value = p_objId.value.toUpperCase().replace("  ", " ");

    var strId = p_objId.value;

    if ( strId == null || strId == "" || strId.length < 4 ) {
        return validate_returnFalse(p_objId);
    }

    var strValidsField = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
    var validField = validate_chars( p_objId, strValidsField );

    if (!validField) {
      return validate_returnFalse(p_objId);
    }

		// Analizando números (izquierdo)
		var indexNum=0;
    while ('0' <= strId.charAt(indexNum) && strId.charAt(indexNum) <= '9' && indexNum<strId.length) {
    	indexNum++;
    }

		// Analizando letras (lado derecho)
		var indexAlpha=strId.length-1;
    while ('A' <= strId.charAt(indexAlpha) && strId.charAt(indexAlpha) <= 'Z' && indexAlpha>=0) {
    	indexAlpha--;
    }

    // Buscando espacio
		var indexSpace = strId.indexOf(" ");

		// Si no se encuentra, letras y números deben ser adyacentes, y se agrega espacio
  	if (indexSpace == -1) {
    	if (indexAlpha - indexNum > 1) {
    		return validate_returnFalse(p_objId);
    	}
      strId = strId.substring(0, indexNum) + " " + strId.substring(indexAlpha+1);
      indexAlpha++;
      p_objId.value = strId;
  	} else {
    	if (indexAlpha - indexNum > 2 || indexSpace > indexAlpha || indexSpace < indexNum) {
    		return validate_returnFalse(p_objId);
    	}
  	}

    // Validando que la parte alpha sea uno de los siguientes valores:
    // LP, CB, SC, PO, OR, TJ, BE, CH, PD, NA
    var alphaStr = strId.substring(indexAlpha+1);

    if (alphaStr != "LP" &&
        alphaStr != "CB" &&
        alphaStr != "SC" &&
        alphaStr != "PO" &&
        alphaStr != "OR" &&
        alphaStr != "TJ" &&
        alphaStr != "BE" &&
        alphaStr != "CH" &&
        alphaStr != "PD" &&
        alphaStr != "NA") {
    		return validate_returnFalse(p_objId);
    }

    return true;
}




