/*******************************************************************
 * Utility.js
 * This page contains generic client-side scripting functions. Do
 * not add functions to this page unless they can be re-used in
 * any website.
 ******************************************************************/

//==========
// Variables
//==========
/*

/^
\w+									User name
(?:
	[-.]\w+
)*
@
(?:									Host (mail server)
	(?:									Domain name
		(?:									Sub-domain (optional)
			[a-zA-Z][a-zA-Z0-9]*
			(?:
				-[a-zA-Z0-9]+
			)*
			\.
		)?
		[a-zA-Z][a-zA-Z0-9]*							Label
		(?:
			-[a-zA-Z0-9]+
		)*
		\.[a-zA-Z]{2,4}						Top-level domain
	)
	|
	(?:								IP address enclosed in sqaure brackets
		\[
		(?:
			[12]?[012345]?\d\.
		){3}
		[12]?[012345]?\d\]
	)
	|
	(?:								IP address w/o square brackets
		(?:
			[12]?[012345]?\d\.
		){3}
		[12]?[012345]?\d
	)
)
$/

*/
// var EmailRegExp = /^\w+(?:[-.]\w+)*@(?:(?:(?:[a-zA-Z][a-zA-Z0-9]*(?:-[a-zA-Z0-9]+)*\.)?[a-zA-Z][a-zA-Z0-9]*(?:-[a-zA-Z0-9]+)*\.[a-zA-Z]{2,4})|(?:\[(?:[12]?[012345]?\d\.){3}[12]?[012345]?\d\])|(?:(?:[12]?[012345]?\d\.){3}[12]?[012345]?\d))$/;
/* use version w/o non-capturing groups for Mac IE */
var EmailRegExp = /^\w+([-.]\w+)*@((([a-zA-Z][a-zA-Z0-9]*(-[a-zA-Z0-9]+)*\.)?[a-zA-Z][a-zA-Z0-9]*(-[a-zA-Z0-9]+)*\.[a-zA-Z]{2,4})|(\[([12]?[012345]?\d\.){3}[12]?[012345]?\d\])|(([12]?[012345]?\d\.){3}[12]?[012345]?\d))$/;

/*
The following is from RFC-1035:
<domain> ::= <subdomain> | " "
<subdomain> ::= <label> | <subdomain> "." <label>
<label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
<let-dig-hyp> ::= <let-dig> | "-"
<let-dig> ::= <letter> | <digit>
<letter> ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case
<digit> ::= any one of the ten digits 0 through 9


/^
(?:							Protocol declaration (optional)
	http\:\/\/
)?
(?:							Host
	(?:							Domain
		[a-zA-Z][a-zA-Z0-9]*
		(?:
			-[a-zA-Z0-9]+
		)*
		\.
	)?
	[a-zA-Z][a-zA-Z0-9]*
	(?:
		-[a-zA-Z0-9]+
	)*
	\.[a-zA-Z]{2,4}
	|						IP Address
	(?:
		[012]?[012345]?\d\.
	){3}
	[012]?[012345]?\d
)
(?:\/[\w.]+)*				optional path (allows decimal in folder name which is interchangable with file name w/ extension)
\/?							optional trailing slash
$/
*/

//var UrlRegExp = /^(?:http\:\/\/)?(?:(?:[a-zA-Z][a-zA-Z0-9]*(?:-[a-zA-Z0-9]+)*\.)?[a-zA-Z][a-zA-Z0-9]*(?:-[a-zA-Z0-9]+)*\.[a-zA-Z]{2,4}|(?:[012]?[012345]?\d\.){3}[012]?[012345]?\d)(?:\/[\w.]+)*\/?$/
// use w/o non-capturing groups for IE Mac
var UrlRegExp = /^(http\:\/\/)?(([a-zA-Z][a-zA-Z0-9]*(-[a-zA-Z0-9]+)*\.)?[a-zA-Z][a-zA-Z0-9]*(-[a-zA-Z0-9]+)*\.[a-zA-Z]{2,4}|([012]?[012345]?\d\.){3}[012]?[012345]?\d)(\/[\w.]+)*\/?$/
			
var PostalCodeRegExpUSA = /^\d{5}(-\d{4})?$/;
var PostalCodeRegExpCanada = /^[a-zA-Z]\d[a-zA-Z][- ]?\d[a-zA-Z]\d$/;
var PostalCodeRegExpArray = [PostalCodeRegExpUSA, PostalCodeRegExpCanada];

/*
 * For use with the validatePostalCode method
 */
var POSTAL_CODE_USA = 1;
var POSTAL_CODE_CANADA = 2;
var POSTAL_CODE_ALL = 255;

var DateRegExp = /^(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])\/(19|20)[0-9]{2}$/;

//==========
// Functions
//==========
function clearFormField(field) {
	if (field.tagName) {
		switch (field.tagName) {
			case "SELECT":
				field.selectedIndex = -1;
				break;
			case "OPTION":
				if (field.parentElement) {
					field.parentElement.selectedIndex = -1;
				}
				break;
			case "INPUT":
				switch (field.type) {
					case "text":
					case "password":
						field.value = "";
						break;
					case "checkbox":
					case "radio":
						field.checked = false;
						break;
				}
				break;
		}
	}
	else {
		if (field.value!=null) {
			switch (field.type) {
				case "text":
				case "password":
					field.value = "";
					break;
				case "checkbox":
				case "radio":
					field.checked = false;
					break;
			}
		}
		else {
			field.selectedIndex = -1;
		}
	}
}

function clearFormFields(form) {
	for (var i=0; i<form.elements.length; i++) {
		var e = form.elements[i];
		clearFormField(e);
		e = null;
	}
}

function confirmDelete(type, item) {
	var s;
	if (item==null) {
		s = "Are you sure you want to delete the selected " + type + "?";
	}
	else {
		s = "You are about to delete the following " + type + ":\n\n" + 
			item + "\n\n" + 
			"Do you want to continue?";
	}
	return confirm(s);
}

function getOffset(property, elem) {
	if (elem.offsetParent) {
		return elem[property] + getOffset(property, elem.offsetParent);
	}
	else {
		return elem[property];
	}
}

function getPropertiesHTML(obj, recursive) {
	var s = "<ol>";
	if (recursive==null) {
		recursive = false;
	}
	for (var prop in obj) {
		if ((typeof(obj[prop])=="object") && (recursive==true)) {
			s += "<li>" + prop + "=" + getPropertiesHTML(obj[prop], false) + "</li>";
		}
		else {
			s += "<li>" + prop + "=" + obj[prop] + "</li>";
		}
	}
	s += "</ol>";
	return s;
}

function getPropertiesText(obj, recursive) {
	var s = "";
	if (recursive==null) {
		recursive = false;
	}
	for (var prop in obj) {
		if ((typeof(obj[prop])=="object") && (recursive==true)) {
			s += "\n" + prop + "=" + getPropertiesHTML(obj[prop], false);
		}
		else {
			s += "\n" + prop + "=" + obj[prop];
		}
	}
	return s;
}

function getSelectedCheckboxValues(ctl) {
	if (ctl==null) {
		return null;
	}
	var values = new Array();
	if (ctl.length==null) {
		if (ctl.checked) {
			values.push(ctl.value);
		}
	}
	else {
		for (var i=0; i<ctl.length; i++) {
			if (ctl[i].checked) {
				values.push(ctl[i].value);
			}
		}
	}
	return values;
}

function getSelectedOptionValue(ctl) {
	if (ctl==null) {
		return null;
	}
	if (ctl.length==null) {
		if (ctl.checked) {
			return ctl.value;
		}
	}
	else {
		for (var i=0; i<ctl.length; i++) {
			if (ctl[i].checked) {
				return ctl[i].value;
			}
		}
	}
	return null;
}

function selectListItem(list, value) {
	for (var i=0; i<list.options.length; i++) {
		if (list.options[i].value==value) {
			list.selectedIndex = i;
			return;
		}
	}
	list.selectedIndex = -1;
}

function setImageSource(imgName, source) {
	var img = document.images[imgName];
	if (img) {
		img.src = source;
	}
}

function showProperties(obj, recursive) {
	var features = "scrollbars=yes," +
			"resizable=yes," + 
			"width=400," + 
			"height=400";
	var w = window.open("", "Utility", features, null);
	w.document.write("<html><head><title>Object Properties</title></head><body>" + 
			getPropertiesHTML(obj, recursive) + 
			"</body></html>");
}

function stripPhone(number) {
	var s = "",mask="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", t;
	for (var i=0; i<number.length; i++) {
		t = number.substr(i,1).toUpperCase();
		if (mask.indexOf(t)!=-1) {
			s += t;
		}
	}
	return s;
}

function validateDate(date) {
	return DateRegExp.test(date);
}

function validateEmail(email) {
	return EmailRegExp.test(email);
}

/*
 * context is a bit mask specifying which country's postal code
 * rules to apply. Use the variables defined at the top of
 * this page
 */
function validatePostalCode(postalCode, context) {
	for (var i=0; i<PostalCodeRegExpArray.length; i++) {
		if (context & (Math.pow(2, i))) {
			if (PostalCodeRegExpArray[i].test(postalCode)) {
				return true;
			}
		}
	}
	return false;
}

function validateUrl(url) {
	return UrlRegExp.test(url);
}

//=====
// Body
//=====
// The following code allows the site to be used with the revision tool
if (document.domain.indexOf("richardsi.com")!=-1) {
	document.domain = "richardsi.com";
}

// The following code block works with the BrowserInfo Backend function
var isWin = (window.navigator.platform.indexOf("Win")!=-1)
var isMac = (window.navigator.platform.indexOf("Mac")!=-1)
if (document.cookie.indexOf("browser=")==-1) {
	document.cookie = "browser=" + 
		"layers=" + (document.layers!=null) +
		"&getElementById=" + (document.getElementById!=null) +
		"&all=" + (document.all!=null) + 
		"&isMac=" + isMac + 
		"&isWin=" + isWin;
	if (document.layers) {
		if (isMac) {
			window.location.href = "/Home.asp";
		}
		else {
			window.setTimeout("window.location.reload();", 1000);
		}
	}
}

