/*////////////////////////////////////////
------------Private Functions-------------
////////////////////////////////////////*/

// function to bring focus to a field, and to select that field
function _selectAndFocus(field) {
	field.focus();
	field.select();
}

// function to strip the "invalid" characters out of "input"
function stripChars(input, invalid) {
	var newString = "";
	for (i = 0;  i < input.length; i++) {
    	myChar = input.charAt(i);
        if (invalid.indexOf(myChar) == -1)
        	newString += myChar;
	}
	return newString;
}

/*////////////////////////////////////////
------Private  Validation Functions-------
////////////////////////////////////////*/

// function that returns whether or not an email address is actually potentially valid
function _validate_email(address) {
		var atPosition = address.indexOf('@');
		var dotPosition = address.lastIndexOf('.');
		var addressLength = address.length;

		if (addressLength < 5)  // definitely bad here!
			return false;
		else if (atPosition < 1)
			return false;
		else if ((dotPosition < 3) ||
				(dotPosition <= (atPosition + 1)))
			return false;

		return true;
}

// this function checks to see if an entry is a valid numeric and positive
function _validate_numeric_positive(input) {
	if (!_validate_characters(input, '0123456789.'))
		return false;

	// make sure there is not more than one decimal
	if (input.split('.').length > 2)
		return false;

	return true;
}

// this function checks to see if an entry is a valid numeric
function _validate_numeric(input) {
	if (!_validate_characters(input, '0123456789.-'))
		return false;

	// make sure there is not more than one decimal
	if (input.split('.').length > 2)
		return false;

	// make sure there is not more than one negative sign
	if (input.split('-').length > 2)
		return false;

	// make sure if there is a negative, that it's the first character
	if (input.indexOf('-') != -1 && input.indexOf('-') != 0) {
		return false;
	}

	return true;
}

// determines if the input is a valid currency amount
function _validate_currency(input) {
	if (!_validate_characters(input, '0123456789.,$'))
		return false;

	if (input.split('.').length > 2)
		return false;

	if (input.split('$').length > 2)
		return false;

	if (input.indexOf('$') != -1 && input.indexOf('$') > 0) {
		return false;
	}

	return true;
}

// determines if the input string contains only the characters specified in "valid"
function _validate_characters(input, valid) {
	// loop once for each character in the input string
	for (i = 0; i < input.length; i++) {
		if (valid.indexOf(input.substring(i, i+1)) == -1) {
			return false;
		}
	}
	return true;
}

// function to determine of the values passed are a possible date
function _validate_date(tDay, tMonth, tYear) {
	// create some base arrays , don't need variables below for now,
	// leaving in case I need them later
	// weekdays = Array ("Sunday", "Monday", "Tuesday",
	// "Wednesday", "Thursday", "Friday", "Saturday");
	// months = Array (0,"January", "February", "March", "April",
	// "May", "June", "July", "August", "September", "October",
	// "November", "December");
	// monthDays = Array (0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	var testDate = new Date(tYear, tMonth - 1, tDay);

	/* if the numbers passed don't make sense,
	   then the returns won't equal them! */
	if (testDate.getMonth() + 1 != tMonth) {
		return false;
	}
	if (testDate.getDate() != tDay) {
		return false;
	}
	return true;

}

// function to determine if the value passed is a valid Canadian postal code
function _validate_postalCode_Canada(input) {
	var validLetter = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var validNumber = '1234567890';

	if (input.length == 7) {
		// they must be putting a space in their postal code...
		// make sure the 4 character is a space
		if (input.substring(3, 4) != ' ') { // we have a problem!
			return false
		}
		else {
			if (validLetter.indexOf(input.substring(0,1)) == -1) return false;
			if (validNumber.indexOf(input.substring(1,2)) == -1) return false;
			if (validLetter.indexOf(input.substring(2,3)) == -1) return false;
			if (validNumber.indexOf(input.substring(4,5)) == -1) return false;
			if (validLetter.indexOf(input.substring(5,6)) == -1) return false;
			if (validNumber.indexOf(input.substring(6,7)) == -1) return false;
		}
	}
	else if (input.length != 6) {
		return false;
	}
	else {
		if (validLetter.indexOf(input.substring(0,1)) == -1) return false;
		if (validNumber.indexOf(input.substring(1,2)) == -1) return false;
		if (validLetter.indexOf(input.substring(2,3)) == -1) return false;
		if (validNumber.indexOf(input.substring(3,4)) == -1) return false;
		if (validLetter.indexOf(input.substring(4,5)) == -1) return false;
		if (validNumber.indexOf(input.substring(5,6)) == -1) return false;
	}
	return true;
}

