//Global variable set at start of script
var emptyString = " field is blank. Please enter a "

//BEGIN Validate String
//non-empty, non-null with no other restrictions
function validateString(myfield, s) {
	if (notNull(myfield.value)&& notBlank(myfield.value)) 
		return true
	else {
		myfield.focus()
		alert("The " + s + emptyString + s)
		return false
	}
}
//End Validate String

//BEGIN Validate Price
//non-empty, non-null, numeric
function validatePrice(myfield) {
	if (notNull(myfield.value) && notBlank(myfield.value) && isNumber(myfield.value))
		return true
	else {
		myfield.focus()
		alert("Invalid Price.")
		return false
	}
}
//END Validate Price
		
		

// BEGIN FUNCTION validateCCNumber()function validateCCNumber(myfield) {
	if (notNull(myfield.value)) {
		newstring = stripNonDigits(myfield.value)
		if (isSize(newstring,16) || isSize(newstring, 16)) 
			return true
	}
	myfield.focus()
	alert("Invalid CC Number.")
	return false
}
// END FUNCTION validateCCNumber()

//BEGIN Validate Password
//non-empty, non-null with no other restrictions
function validatePassWord(form) {
pw1 = form.Password.value;
pw2 = form.Password2.value;

if (pw1 != pw2) {
alert ("You did not enter the same password twice. Please re-enter your password.")
return false;
}
else return true;
}

//End Validate Password



//BEGIN Checks when Username is invalid
function validateUsername() {
	alert ("Your Username is already in use. Please go back and choose a new username.")
	return false
}
//END Checks when nothing at all has been placed



//BEGIN Checks when nothing at all has been placed
//in a text box.
function notNull(str) {
	if (str.length == 0 )
		return false
	else 
		return true
}
//END Checks when nothing at all has been placed

//BEGIN Checks for strings that are input as just spaces
//If this function finds ANY characters besides 
//spaces it will return true and therefore validate.
function notBlank(str) {
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) != " ")
			return true
	}
	return false
}
//END Checks for strings that are input as just spaces

//BEGIN Checks that the length of the field is correct
function isSize(str, size) {
	if (str.length == size) 
		return true
	else
		return false
}


//END Checks that the length of the field is correct

//BEGIN validates that all characters are digits
function isDigits(str) {
	var i
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if (mychar < "0" || mychar > "9")
			return false
	}
	return true
}
//END validates that all characters are digits

//BEGIN validates that all characters are numbers
function isNumber(str) {
	numdecs = 0
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if ((mychar >= "0" && mychar <= "9") || mychar 
			== ".") {
			if (mychar == ".")
				numdecs++
		}
		else 
			return false
	}
	if (numdecs > 1)
		return false	
return true
}
//END validates that all characters are numbers

//BEGIN validates that all characters are within range
function isInRange(str, num1, num2) {
	var i = parseInt(str)
	return((i >= num1) && (i <= num2))

}
//END validate number within range

//BEGIN strips non-digits
function stripNonDigits(str) {
	var i
	var newstring = ""
	for (i = 0;  i < str.length; i++) {
		mychar = str.charAt(i)
		if (isDigits(mychar)) 
			newstring += mychar
	}
	return newstring
}
//END strips non-digits

//BEGIN strips characters
function stripChars(str, chars) {
	var i
	var newstring = ""
	for (i = 0;  i < str.length; i++) {
		mychar = str.charAt(i)
		if (chars.indexOf(mychar) == -1)
			newstring += mychar
	}
	return newstring
}
//END strips characters

//BEGIN Validation for state and zip codes
var STATECODES = "AL/AK/AZ/AR/CA/CO/CT/DE/DC/FL/GA/HI/ID/IL/IN/IA/KS/LA/ME/MD/MA/MI/MN/MS/MO/MT/NV/NH/NJ/NM/NY/NC/ND/OH/OK/OR/PA/PR/RI/SC/TN/TX/UT/VT/VA/WA/WV/WI/WY"

function isStateCode (str) {
	var newstring = str.toUpperCase()
	if (STATECODES.indexOf(newstring) != -1 && str.indexOf("/") == -1)
		return true
	else 
		return false
}

function validateState(myfield) {
	if (notNull(myfield.value) && isSize(myfield.value, 2) && isStateCode(myfield.value))
		return true
	else {
		myfield.focus()
		alert("Invalid state code. Please enter 2-letter state postal abbreviation.")
		return false
	}
}

function validateZip(myfield) {
	if (notNull(myfield.value)) {
		newstring = stripNonDigits(myfield.value)
		if (isSize(newstring,5) || isSize(newstring, 9)) 
			return true
	}
	myfield.focus()
	alert("Invalid zip code. Please enter 5-digit or 9-digit zip code.")
	return false
}
//END Validation for state and zip codes