
// whitespace characters
var whitespace = " \t\n\r";

//Validation functions

//function suppressquote - suppresses quote keystrokes
function suppressquote(e) {
	var charCode = (navigator.appName == "Netscape") ? e.which:e.keyCode
	if (charCode == 34 || charCode == 39) {
		return false;
	}
	else {
		return true;
	}
}


//function suppresszip - suppresses non-numbers but allows dashes (for ZIP codes)
function suppresszip(e) {
	var charCode = (navigator.appName == "Netscape") ? e.which:e.keyCode
	if (charCode == 45) {
		return true;
	}
	else {
		if (charCode < 48 || charCode > 57) {
			return false;
		}
		else {
			return true;
		}
	}
}


//function suppressphone - suppresses non-numbers but allows dashes, spaces, and parentheses (for phone numbers)
function suppressphone(e) {
	var charCode = (navigator.appName == "Netscape") ? e.which:e.keyCode
	if (charCode == 45 || charCode == 32 || charCode == 40 || charCode == 41) {
		return true;
	}
	else {
		if (charCode < 48 || charCode > 57) {
			return false;
		}
		else {
			return true;
		}
	}
}


//function suppressletter - suppresses letter keystrokes
//second argument (boolean) indicates whether decimals (".") are allowed.
//onKeyPress="return suppressletter(event, true);"
function suppressletter(e, blndecimal) {
	var charCode = (navigator.appName == "Netscape") ? e.which:e.keyCode
	if (charCode == 46 && blndecimal) {
		return true;
	}
	else {
		if (charCode < 48 || charCode > 57) {
			return false;
		}
		else {
			return true;
		}
	}
}


// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag) {
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++) {   
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}


// Removes all characters which do not appear in string bag from string s.
function stripCharsNotInBag (s, bag) {
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++) {   
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) != -1) returnString += c;
	}
	return returnString;
}


//function to see if a character is a digit.
function isDigit (c) {
	return ((c >= "0") && (c <= "9"))
}


//function to see if a string is a number.
function isFloat (s) {
	var i;
	var seenDecimalPoint = false;
	var decimalpointdelimiter = ".";

// doesn't work - dfp
//	if (isEmpty(s)) {
	if (s == "") {
		return true;
	}

	if (s == decimalpointdelimiter) return false;

	// Search through string's characters one by one
	// until we find a non-numeric character.
	// When we do, return false; if we don't, return true.

	for (i = 0; i < s.length; i++) {   
		// Check that current character is number.
		var c = s.charAt(i);

		if ((c == decimalpointdelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
		else if (!isDigit(c)) return false;
	}

	// All characters are numbers.
	return true;
}


//function evaluates a string to see if it's a valid password.
//PWs must be at least six characters in length and contain numbers, letters, "@", ".", or "-".
//Approach is to strip out non-valid characters and see if we still have our original string.
function isPW (pwField, blnRequired) {
	var strPW = pwField.value;
	var strPWcheck = stripCharsNotInBag(strPW, "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@.-")
	
	if (!(blnRequired) && strPWcheck.length == 0) {
		pwField.value = strPWcheck;
		return true;
	}
	
	if (strPW.length >= 6 && strPW == strPWcheck) {
		return true;
	}
	else {
		alert ("The password you have entered is invalid.  Passwords must be at least six characters long and contain letters and numbers.");
		pwField.focus();
		return false;
	}
}


//function evaluates a string to see if it's a valid user ID.
//User IDs must be at least six characters in length and cannot contain several characters.
//Approach is to strip out non-valid characters and see if we still have our original string.
function isUserID (userIDField, blnRequired) {
	var strUserID = userIDField.value;
	var strUserIDcheck = stripCharsInBag(strUserID, "\()*%_/+=;,|#<>[]&")
	var strUserIDcheck = stripCharsInBag(strUserIDcheck, " ")
	
	if (!(blnRequired) && strUserIDcheck.length == 0) {
		userIDField.value = strUserIDcheck;
		return true;
	}
	
	if (strUserID.length >= 6 && strUserID == strUserIDcheck) {
		return true;
	}
	else {
		alert ("The user ID you have entered is invalid.  User IDs must be at least six characters long and contain letters and/or numbers. Blank spaces are not permitted.");
		userIDField.focus();
		return false;
	}
}

//function evaluates a string to see if it's a valid ##### or #####-#### US zip code.
//The general approach is to strip out all non-numeric characters, ensure the length is either 5 or 9 digits,
//and then reformat the string.
function isZIPCode (zipField, strDesc, blnRequired) {
	var strZip = zipField.value;
	var strDigits = stripCharsNotInBag (strZip, "1234567890");
	
	if (!(blnRequired) && strDigits.length == 0) {
		zipField.value = "";
		return true;
	}
	
		if (strDigits.length == 5) {
			zipField.value = strDigits;
			return true;
		}
		else if (strDigits.length == 9) {
			zipField.value = strDigits.substring (0, 5) + "-" + strDigits.substring (5, 9);
			return true;
		}
		else {
			alert ("The " + strDesc + " field does not contain a valid ZIP code.");
			zipField.focus();
			return false;
		}
}

//function evaluates a string to see if it's a valid four digit year.
//The general approach is to strip out all non-numeric characters, 
//and ensure the length is 4 digits.
function isValidYear (yearField, strDesc, blnRequired) {
	var strYear = yearField.value;
	var strDigits = stripCharsNotInBag (strYear, "1234567890");
	
	if (!(blnRequired) && strDigits.length == 0) {
		yearField.value = "";
		return true;
	}
	
	if (strDigits.length == 4) { //nest1
		if (strYear.length == 4) { //nest2
			if (strYear.substring (0,1) == 1 || strYear.substring (0,1) == 2) { //nest3
			yearField.value = strYear;
			return true;
			}
			else {
				alert ("The " + strDesc + " field does not contain a valid year.");
				yearField.focus();
				return false;
			} //nest3
		}
		else {
			if (strDigits.substring (0,1) == 1 || strDigits.substring (0,1) == 2) { //nest4
			yearField.value = strDigits;
			return true;
			}
			else {
				alert ("The " + strDesc + " field does not contain a valid year.");
				yearField.focus();
				return false;
			} //nest4
		} //nest2
	}
	else {
		alert ("The " + strDesc + " field does not contain a valid year.");
		yearField.focus();
		return false;
	} //nest1
} // end function

//function evaluates a string to see if it's a valid phone number (XXX) XXX-XXXX
//The general approach is to strip out all non-numeric characters, ensure the length is either 7 or 10 digits,
//and then reformat the string.
function isPhoneNumber (phoneField, strDesc, blnRequired) {
	var strPhone = phoneField.value;
	var strDigits = stripCharsNotInBag(strPhone, "1234567890");

	if (!(blnRequired) && strDigits.length == 0) {
		phoneField.value = "";
		return true;
	}

	if (strDigits.length == 10) {
		phoneField.value = "(" + strDigits.substring (0, 3) + ") " + strDigits.substring (3, 6) + "-" + strDigits.substring (6, 10);
		return true;
	}
	else {
		alert ("The " + strDesc + " field does not contain a valid phone number. Area code is required.");
		phoneField.focus();
		return false;
	}
}

//function evaluates a field to see if it contains a valid email address.
// Email address must be of form a@b.c.
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
function isEmail (emailField, strDesc, blnRequired) {
	var strEmail = stripCharsInBag(emailField.value, " ");

	if (!(blnRequired) && strEmail.length == 0) {
		emailField.value = strEmail;
		return true;
	}
	
	// there must be >= 1 character before @, so we
	// start looking at character position 1 
	// (i.e. second character)
	var i = 1;
	var sLength = strEmail.length;

	// look for @
	while ((i < sLength) && (strEmail.charAt(i) != "@")) {
		i++
	}

	if ((i >= sLength) || (strEmail.charAt(i) != "@")) {
		alert("The " + strDesc + " field does not contain a valid email address (i.e. name@domain.extension).");
		emailField.focus();
		return false;
	}
	else {
		i += 2;
	}

	// look for .
	while ((i < sLength) && (strEmail.charAt(i) != ".")) {
		i++
	}

	// there must be at least one character after the .
	if ((i >= sLength - 1) || (strEmail.charAt(i) != ".")) {
		alert("The " + strDesc + " field does not contain a valid email address (i.e. name@domain.extension).");
		emailField.focus();
		return false;
	}
	else {
		emailField.value = strEmail;
		return true;
	}
}


//function to see if a string is all whitespace.  An all-whitespace string is blanked out.
//This function will return true if the field is required and either blank or all whitespace.
//It will return false if it is either optional or not all whitespace.
function isWhiteSpace(sField, strDesc, blnRequired) {
	var str = sField.value;
	
	if (str.length > 0) {
		for (var i = 0; i < str.length; i++) {
			// Check that current character isn't whitespace.
			var c = str.charAt(i);
			if (whitespace.indexOf(c) == -1) return false;
		}
	}

	// All characters are whitespace.
	sField.value = "";
	if (blnRequired) {
		alert("The " + strDesc + " field is required.");
		sField.focus();
		return true;
	}
	else {
		return false;
	}
}

//function to see if a string is all whitespace.  An all-whitespace string is blanked out.
//This function will return true if the field is empty and false if it is not

function isEmpty(sField) {
	var str = sField.value;
	
	if (str.length > 0) {
 	    for (var i = 0; i < str.length; i++) {
	         // Check that current character isn't whitespace.
	         var c = str.charAt(i);
	         if (whitespace.indexOf(c) == -1) return false;
	         }
	 }

	// All characters are whitespace.
	return true;
}


//function will check to see if a year is a LeapYear
//It will return true if the year is a leap year and false if it is not.
function isLeapYear(year){
	return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

//function will check to see if the date is valid
//It will return true if the date is valid and false if it is not.
function isDate(year, mnth, day)
{
	if (!isFloat(day) || !isFloat(mnth) || !isFloat(year)) return false;   // all values must be numeric 	
	if (!day || !mnth || !year) return false;   // no values can be 0 
	if (mnth > 12 || mnth < 1)  return false;   // there's only 12 month
	if (day  > 31 || day  < 1)  return false;   // and 31 day

	if ((day == 31)                             // not each month has 31 days
	&& (mnth != 1 && mnth != 3 && mnth != 5
	&&  mnth != 7 && mnth != 8 && mnth != 10
	&&  mnth != 12)) return false;

	if (mnth == 2){                               // february 
		if (day > 29) return false;           // has maximum 29 days
		if (day == 29 && !isLeapYear(year))   // and only if it's a leap year
			return false;
	}
	return true; 
}

//function will check to see if the company is a member
//It will return true if company is a member and false if it is not.
function isMember(memberType)
{
    // Members Include the Following Member Types	
    // 1 - Corporate Member
    // 2 - Academic Member
    // 3 - Associate Member
    // 4 - Affiliate Member    
    
      if (memberType == 1 || memberType == 2 || memberType == 3 || memberType == 4)
         {return true;}
      else
         {return false;} 
}
