// ValidNumber(vntValue, blnDecimalAllowed, blnNegativeAllowed)
// - Checks for valid integer or floating point numbers
//   blnDecimalAllowed - Boolean indicating whether a single
//                       decimal should be allowed
//   blnNegativeAllowed - Boolean indicating whether the number
//                        may be negative or not
function ValidNumber(vntValue, blnDecimalAllowed, blnNegativeAllowed) {
	var strValue = vntValue.toString();
	var strOneChar;
	var blnDecimal = false;
	var blnValid = true;
	var intDecimals = 0;
	// Loop through each character of string looking for 
	// numbers and decimals
	for (var i = 0; i < strValue.length; i++) {
		// Retrieve single character
		strOneChar = strValue.charAt(i);
		// Validate for negative possibility
		if (strOneChar == "-") {
			if (i == 0 && blnNegativeAllowed) {
				continue;
			} else {
				blnValid = false;
			}
		}
		// If we find a decimal add one to decimal count.
		if (strOneChar == ".") {
			intDecimals++;
			continue;
		}
		// For all other cases, make sure that the character
		// is a number - otherwise mark invalid.
		if (strOneChar < "0" || strOneChar > "9") {
			blnValid = false;
		}
	}
	// If number has more than one decimal, it's invalid 
	// regardless
	if (intDecimals > 1) {
		blnValid = false;
	// Otherwise, ensure that decimals are allowed and 
	// make sure that no more than one is found.
	} else if (intDecimals > 0 && !blnDecimalAllowed) {
		blnValid = false;
	}
	// Return result
	return blnValid;
}

// ValidDate(inputVal) - 5/26/2001
// - Checks for valid date value
function ValidDate(inputVal) {
	var strValidChars = '0123456789/-';
	var intCharacterPosition;
	var intSlashCount = 0;
	var intDashCount = 0;
	var intMonth = 0;
	var intDay = 0;
	var intYear = 0;
	var arrSplitDate;
	var blnFoundBadChar;
	var i;
	// First check to see if there is something in the
	// passed string
	if (!isEmpty(inputVal)) {
		// Now, count the number of slashes in the 
		// passed string and validate only good characters
		blnFoundBadChar = false;
		for (i = 0; i < inputVal.length; i++) {
			if (strValidChars.indexOf(inputVal.charAt(i)) == -1) {
				blnFoundBadChar = true;
			}
			if (inputVal.charAt(i) == '/') {
				intSlashCount++;
			}
			if (inputVal.charAt(i) == '-') {
				intDashCount++;
			}
		}
		// If we look good so far, split into 
		// month/day/year values
		if ( ((intSlashCount == 2 && intDashCount == 0) || (intSlashCount == 0 && intDashCount == 2)) &&
				!blnFoundBadChar ) {
			// Split values up
			if (intSlashCount == 2) {
				arrSplitDate = inputVal.split('/');
			} else {
				arrSplitDate = inputVal.split('-');
			}
			// Populate fields
			intMonth = arrSplitDate[0];
			intDay = arrSplitDate[1];
			intYear = arrSplitDate[2];
			// Validate these fields
			if (intMonth < 1 || intMonth > 12) {
				return false;
			} else if (intDay < 1 || intDay > 31) {
				return false;
			} else if (intYear < 1 || intYear > 2999 || (intYear > 99 && intYear < 1850)) {
				return false;
			} else {
				// Completely valid format.  Try checking for late days in months
				if ( (intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && intDay > 31) { return false; }
				if ( (intMonth == 2) && intDay > 28 ) { return false; }
				if ( (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && intDay > 30) { return false; }
				// If we got this far, it's valid
				return true;
			}
		} else {
			return false;
		}
	} else {
		return false;
	}
}

// isEmpty(vntValue)
// - Checks for empty/null value
function isEmpty(vntValue) {
	if (vntValue == null || vntValue == "" || vntValue == " ") {
		return true;
	}
	return false;
}

// isEmail(string)
// - Checks for proper e-mail address format using RegEx
function isEmail(string) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		return true;
	} else {
		return false;
	}
}

// CleanNA(vntValue)
// - Removes "NA", "N/A", and "None" from a value and 
//   returns it
function CleanNA(strValue) {
	var strNewValue = '';
	if (strValue.length > 0) { strNewValue = strValue.toUpperCase(); }
	
	if ( (strNewValue == "NA") || 
			(strNewValue == "N/A") || 
			(strNewValue == "NONE") ) {
		strNewValue = '';
	} else {
		strNewValue = strValue;
	}
	
	return strNewValue;
}

// CleanLength(vntValue)
// - Removes apostrophe's and other things from Length fields 
//   commonly input by end-users.
function CleanLength(strValue) {
	var strNewValue = strValue.replace("'", "");

	return strNewValue;
}

// TrimString(strText)
// - Removes leading and trailing spaces from an input string 
//   and returns it to the calling code
function TrimString(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1, strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

	return strText;
}

// Checks for at least one radio button in a set
// is selected
function radioNotSet(strFormName, strFormField) {
	var itemCount = eval("document." + strFormName + "." + strFormField + ".length");

	// Support radio sets with only a single item (look like a checkbox)
	if (isNaN(itemCount)) {
		var thisChecked = eval("document." + strFormName + "." + strFormField + ".checked");
		if (thisChecked == true) {
			return false;
		}
	} else {
		for (var i=0; i < itemCount; i++) {
			var thisChecked = eval("document." + strFormName + "." + strFormField + "[" + i + "].checked");

			if (thisChecked == true) {
				// Found one that's checked - return false
				return false;
			}
		}
	}

	// Did not encounter a selection, return true
	return true;
}

// Gets the selected Radio Button's value
function radioValue(strFormName, strFormField) {
	// Make sure it's set first
	if (radioNotSet(strFormName, strFormField)) {
		return '';
	} else {
		var itemCount = eval("document." + strFormName + "." + strFormField + ".length");
		// Find the selected value
		for (var i=0; i < itemCount; i++) {
			var thisChecked = eval("document." + strFormName + "." + strFormField + "[" + i + "].checked");
			if (thisChecked == true) {
				// Found the selected one, return it's value
				return eval("document." + strFormName + "." + strFormField + "[" + i + "].value");
			}
		}
	}
}

// Validate a credit card number
function ValidCreditCard(s) {
  var i, n, c, r, t;

  // First, reverse the string and remove any non-numeric characters.
  r = "";
  for (i = 0; i < s.length; i++) {
    c = parseInt(s.charAt(i), 10);
    if (c >= 0 && c <= 9) { r = c + r; }
  }

  // Check for a bad string.
  if (r.length <= 1) { return false; }

  // Now run through each single digit to create a new string. Even digits
  // are multiplied by two, odd digits are left alone.
  t = "";
  for (i = 0; i < r.length; i++) {
    c = parseInt(r.charAt(i), 10);
    if (i % 2 != 0)
      c *= 2;
    t = t + c;
  }

  // Finally, add up all the single digits in this string.
  n = 0;
  for (i = 0; i < t.length; i++) {
    c = parseInt(t.charAt(i), 10);
    n = n + c;
  }

  // If the resulting sum is an even multiple of ten (but not zero), the
  // card number is good.
  if (n != 0 && n % 10 == 0) { return true; } else { return false; }
}
