// JavaScript Document
// Reference functions to use when validating forms
// Created 3-Jan-2007


// Check that the number of characters in a string is between a given maximum and minimum value
function isValidLength(string, min, max) {
	if (string.length < min || string.length > max) 
		return false;
  else 
		return true;
}

// Check that a date is valid - use only with 3 drop down menus (month,day,year) because it can't handle strings
function isDate(month,day,year) {
	if(!isNumeric(month) || !isNumeric(day) || !isNumeric(year))
		return false;
	if (month == 2) { // check for february 29th, 30th or 31st
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap))
			return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) // check for other months that don't allow 31st
		 return false;
	return true; // date is valid
}

// Check that a credit card number is in a valid format (not necessarily a valid number)
function isValidCreditCard(number) {
	if ((number.search(/^([4]{1})([0-9]{12,15})$/) != -1) || // visa
			(number.search(/^([51|52|53|54|55]{2})([0-9]{14})$/) != -1) || // mc
			(number.search(/^([34|37]{2})([0-9]{13})$/) != -1) || // amex
			(number.search(/^([6011]{4})([0-9]{12})$/) != -1) || // discover
			(number.search(/^([30|36|38]{2})([0-9]{12})$/) != -1) // diners club
		 ){
		return true;
	}
	else
		return false;
}

// Check an email address conforms to standard email format (will allow blanks and addresses like postmaster@localhost)
function isValidEmail(address) {
	if (address != '' && address.search) {
		if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
		else return false;
	}
	// allow empty strings to return true - screen these with either a 'required' test or a 'length' test
	else return true;
}

// Check that an email address conforms to strict email format (will NOT allow blanks or addresses like postmaster@localhost)
function isValidEmailStrict(address) {
	if (isValidEmail(address) == false) return false;
	var domain = address.substring(address.indexOf('@') + 1);
	if (domain.indexOf('.') == -1) return false;
	if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1) return false;
	return true;
}

// Check that a US zip code is in a valid format (not necessarily an active zip code)
function isValidZipCode(zipcode) {
	zipcode = removeSpaces(zipcode);
	if (!(zipcode.length == 5 || zipcode.length == 9 || zipcode.length == 10)) return false;
	if ((zipcode.length == 5 || zipcode.length == 9) && !isNumeric(zipcode)) return false;
	if (zipcode.length == 10 && zipcode.search && zipcode.search(/^\d{5}-\d{4}$/) == -1) return false;
	return true;
}

// Check that a Canadian postal code is in a valid format (not necessarily an acitve postal code)
function isValidPostalCode(postalcode) {
	if (postalcode.search) {
		postalcode = removeSpaces(postalcode);
		if (postalcode.length == 6 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return true;
		else if (postalcode.length == 7 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]-\d[a-zA-Z]\d$/) != -1) return true;
		else return false;
	}
	return true;
}

// Check that a US or Canadian phone number is in a valid format (not necessarily an active phone number)
function isValidUSPhoneNumber(areaCode, prefixNumber, suffixNumber) {
	if (arguments.length == 1) {
		var phoneNumber = arguments[0];
		phoneNumber = phoneNumber.replace(/\D+/g, '');
		var length = phoneNumber.length;
		if (phoneNumber.length >= 7) {
			var areaCode = phoneNumber.substring(0, length-7);
			var prefixNumber = phoneNumber.substring(length-7, length-4);
			var suffixNumber = phoneNumber.substring(length-4);
		}
		else return false;
	}
	else if (arguments.length == 3) {
		var areaCode = arguments[0];
		var prefixNumber = arguments[1];
		var suffixNumber = arguments[2];
	}
	else return true;
	
	if (areaCode.length != 3 || !isNumeric(areaCode) || prefixNumber.length != 3 || !isNumeric(prefixNumber) || suffixNumber.length != 4 || !isNumeric(suffixNumber)) return false;
	return true;
}

// Check that a string contains only letters and numbers
function isAlphanumeric(string, ignoreWhiteSpace) {
	if (string.search) {
  	if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1))
			return false;
  }
  return true;
}

// Check that a string contains only letters
function isAlphabetic(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)) return false;
	}
	return true;
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
	}
	return true;
}

// Remove characters that might cause security problems from a string 
function removeBadCharacters(string) {
	if (string.replace) {
		string.replace(/[<>\"\'%;\)\(&\+]/, '');
	}
	return string;
}

// Remove all spaces from a string
function removeSpaces(string) {
	var newString = '';
	for (var i = 0; i < string.length; i++) {
		if (string.charAt(i) != ' ') newString += string.charAt(i);
	}
	return newString;
}

// Remove leading and trailing whitespace from a string
function trimWhiteSpace(string) {
	var newString  = '';
	var substring  = '';
	beginningFound = false;
	// copy characters over to a new string
	// retain whitespace characters if they are between other characters
	for (var i = 0; i < string.length; i++) {
		// copy non-whitespace characters
		if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {
			// if the temporary string contains some whitespace characters, copy them first
			if (substring != '') {
				newString += substring;
				substring = '';
			}
			newString += string.charAt(i);
			if (beginningFound == false) 
				beginningFound = true;
		}
		// hold whitespace characters in a temporary string if they follow a non-whitespace character
		else if (beginningFound == true) 
			substring += string.charAt(i);
	}
	return newString;
}

// Returns a checksum digit for a number using mod 10
function getMod10(number) {
	// convert number to a string and check that it contains only digits
	// return -1 for illegal input
	number = '' + number;
	number = removeSpaces(number);
	if (!isNumeric(number)) return -1;
	// calculate checksum using mod10
	var checksum = 0;
	for (var i = number.length - 1; i >= 0; i--) {
		var isOdd = ((number.length - i) % 2 != 0) ? true : false;
		digit = number.charAt(i);
		
		if (isOdd) checksum += parseInt(digit);
		else {
			var evenDigit = parseInt(digit) * 2;
			if (evenDigit >= 10) checksum += 1 + (evenDigit - 10);
			else checksum += evenDigit;
		}
	}
	return (checksum % 10);
}
