// JavaScript Document

// **************************************************************************************************
// Validation script adapted from "Form Validation with JavaScript" at www.devshed.com,
// by Nariman K, Melonfire (http://www.devshed.com/c/a/JavaScript/Form-Validation-with-JavaScript/)
// **************************************************************************************************

// Create form validator object ---------------------------------------------------------------------
function FormValidator() {
	// set up array to hold error messages
	this.errorList = new Array;

	// set up object methods
	this.IsBlank = IsBlank;
	this.IsNumeric = IsNumeric;
	this.IsAlphabetic = IsAlphabetic;
	this.IsAlphaNumeric = IsAlphaNumeric;
	this.IsWithinRange = IsWithinRange;
	this.IsValidEmail = IsValidEmail;
	this.IsValidPhone = IsValidPhone;
	this.IsChecked = IsChecked;
	this.IsValidTime = IsValidTime;
	//this.IsValidCC = IsValidCC;
	this.IsValidExpiration = IsValidExpiration;
	this.RaiseError = RaiseError;
	this.NumErrors = NumErrors;
	this.DisplayErrors = DisplayErrors;
}

// Form validator methods ---------------------------------------------------------------------------
// Check if input is whitespace or empty
function IsBlank(val) {
	if (val.match(/^\s+$/) || val == "") {
		return true;
	}
	else { return false; } 
}

// Check if input is numeric
function IsNumeric(val) {
	if (isNaN(val)) { return false; }
	else { return true; } 
}

// Check if input is alphabetic
function IsAlphabetic(val) {
	if (val.match(/^[a-zA-Z]+$/)) {
		return true;
	}
	else { return false; } 
}

// Check if input is alphanumeric
function IsAlphaNumeric(val) {
	if (val.match(/^[a-zA-Z0-9]+$/)) {
		return true;
	}
	else { return false; } 
}

// Check if value is within range
function IsWithinRange(val, min, max) {
	if (val >= min && val <= max) {
		return true;
	}
	else { return false; } 
}

// Check if input is valid email address
function IsValidEmail(val) {
	if (val.match(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/)) {
		return true;
	}
	else { return false; } 
}

// Very simple (international) phone validation
function IsValidPhone(val) {
	if (val.match(/^[a-zA-Z0-9\-\.\s]+$/)) {
		return true;
	}
	else { return false; } 
}

// Check if value is checked
function IsChecked(obj) {
	if (obj.checked) { return true; }
	else { return false; } 
}

// Check if time is valid (HH:MM)
function IsValidTime(val) {
	var timePat = /^(\d{1,2}):(\d{2})$/;
	var matchArray = val.match(timePat);
	if (matchArray == null) { return false; }
	hour = matchArray[1];
	minute = matchArray[2];
	if (hour < 1  || hour > 12) { return false; }
	if (minute < 0 || minute > 59) { return false; }
	return true;
}

// Check if CC expiration is valid
function IsValidExpiration(val) {
  if (val.match(/^(\d{2})\/(\d{4})$/)) {
		return true;
	}
	else { return false; }
}

// Display all errors
// iterate through error array and print each item
function DisplayErrors(title) {
	var errorMsg = title;
	for (i=0; i<this.errorList.length; i++) {
		errorMsg += this.errorList[i] + "\n";
	}
	alert(errorMsg);
}

// Add an error to error list
function RaiseError(msg) {
	this.errorList[this.errorList.length] = msg;
}

// return number of errors in error array
function NumErrors() {
	return this.errorList.length;
}

