/**
 * Form validation functions.
 * Last Updated: 05-16-2008
 *
 * All updates must be made in such a way to ensure backwards compatability.
 *
 *
 * Example use of validation:
 *     if (!isValid(isCurrency(getString("potential")),"Potential purchases")) return false;
 *
 *
 * Updates:
 *     01-21-2008 : Added the isBlank() function.  This one is different from isEmpty() since
 *                  this one checks for spaces while isEmpty() would return false on a space.
 *     05-16-2008 : Added the isNumeric() function.  Tests if string is numeric allowing for 
 *                  decimals and commas only.
 */

/** Add trim to the String object's prototype.
 *  Trims the leading and trailing white spaces from the String.
 *
 */
String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g, "");
}


/**
 * Test for string being currency or not.
 * Currency format allows the $ on top of normal number form.
 * @param test The string to be tested 
 * @returns true if str is in currency format
 */
function isCurrency(str) {
	if (isEmpty(str)) return false;
	str = str.replace('$','');
	str = str.replace(/,/g,'');
	if (isNaN(str))	return false;
	return true;
}


/**
 * Test for a non empty string.
 * In this case anything is accepted except an empty string.
 * @param test The string to be tested 
 * @returns true if str is in currency format
 */
function isString(str) {
	if (isEmpty(str)) return false;
	return true;
}


/**
 * Trims the string getting rid of leading and trailing spaces. 
 * @param str The string to trim
 * @returns The trimmed string or an empty string if str is not a valid string. 
 */
function myTrim(str) {
	// needs work isn't doing a normal trim
	// if first and last character is a space then trim the space
	str = str.replace(/ /g,'');
	return str;
}


/**
 * Return mysql decimal format version of string.
 * Strips $ , from the string.
 * @param str The string to edit.
 * @returns mysql formatted decimal string
 */
function mysqlDecimalFormat(str) {
	str = str.replace('$','');
	str = str.replace(/,/g,'');
	str = str.replace(/ /g,'');
	return str;
}


/**
 * Tests if the string is a valid integer.
 * @param str The string to test.
 * @returns true if it is an integer (only numeric characters).
 */
function isInt(str) {
      var i;
	  str = str.replace(',','');
      if (isEmpty(str)) return false;  //uncertain of this part
      //if (isInt.arguments.length == 1) return 0;  uncertain of this part
      //else return (isInt.arguments[1] == true);   uncertain of this part
      for (i = 0; i < str.length; i++) {
         var c = str.charAt(i);
         if (!isDigit(c)) return false;
      }
      return true;
}


/**
 * Tests if the string is empty.
 * @param str The string to test.
 * @returns true if not an empty string.
 */
function isEmpty(str) {
  return ((str == null) || (str.length == 0));
}

/**
 * Tests if the string is empty.
 * @param str The string to test.
 * @returns true if not an empty string.
 */
function isBlank(str) {
	//alert("before trim str = "+str);
	str = str.trim();
	//alert("after trim str = "+str);
	if ((str == null) || (str.length == 0))
		return true;
  return false;
}


/**
 * Tests for character being a digit.
 * @param c The character to test.
 * @returns true if is digit 0-9.
 */
function isDigit (c) {
  return ((c >= "0") && (c <= "9"));
}


/**
 * A routine to display the not valid message.
 * @param bool valid
 * @param String message User friendly description of the field
 *     that's not valid.
 * @returns true if is valid, else will display the message and return false.
 */
function isValid(valid, message) {
	if (!valid) {
	  alert(message+" is not valid.");
	  return false;
	}
	return true;
}


/**
 * Gets the value of the element.
 * @param String element The element id to get the value of.
 * @returns String value of the element.
 */
function getString(element) {
	return document.getElementById(element).value;
}


// Need to add validation to accept proper phone number input.
function isPhone(str) {
		
	return true;
}

/**
 * Is numeric tests for the string being a number allowing ',' or '.'
 * @param String the string to be tested.
 * @returns true if is numeric
 */
function isNumeric(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;
}


// A lot of ideas about how to validate, best seems to be to use regular expressions, below are some examples.
//	var re = /(^-?[1-9](\d{1,2}(\,\d{3})*|\d*)|^0{1})$/;
//	re = /^[\$]?[0-9]+/;
//	if (re.test(str)) return true;
//	return false
	//myRegExp = /[0-9]+/g
//myNumber = new String("93932")

//if(myRegExp.test(myNumber))
//{
//there are numbers in myNumber
//}
//else
//{
//there are no numbers in myNumber
//}