// JavaScript Document
// Kris McGlashan - 070624



// Functions Definitions --------------------



	// --- Switches all the xhtml strict links on a page that have rel set to external, to pop open in a new window.
	
	function externalLinks() {
		if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
				 anchor.target = "_blank";
		}
	}
	
	
	
	// --- Contact Form Functions
	
	function isFilled(str)   { return (str != ""); }
	function isEmail(string) { return (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1); }
	function isDigital(str)	 { return (parseFloat(str,10)==(str*1)); }
	function isCurrency(val) { var re = /^(\$?\d+\$?|\$?\d+\.\d+\$?)$/; return (re.test(val)); }
	
	function ValidForm(form) {
		
		var field, i;
		var req = new Array(3);
		var email = new Array(1);
		var digits = new Array(0);
		var currs = new Array(0);
		req[0] = "r_Name";
		req[1] = "re_Email";
		req[2] = "r_Comment";
		email[0] = "re_Email";

		for (i=0;i<3;i++)	{
			var field = document.getElementById(req[i]);
			if ((field.type == 'checkbox')||(field.type == 'radio'))
			{
				var field = document.getElementsByName(req[i]);
				var chk = false;
				for(l=0;l<field.length;l++)
				{
					if (field[l].checked) chk = true;
				}
				if (!chk) {alert("Field '" + field[0].title + "' is required to be checked correctly before successful submission."); return false; break;}
			}
			else
			{
			if (!isFilled(field.value))	{
				alert("Field '" + field.title + "' is required to be filled in before successful submission.");
				field.focus();
				return false;
				break;
			}}}
		for (i=0;i<1;i++)	{
			var field = document.getElementById(email[i]);
			if (!isEmail(field.value)) {
				alert("Field '" + field.title + "' is required to be filled in with valid email addresses before successful submission.");
				field.focus();
				return false;
				break;
			}}
		for (i=0;i<0;i++)	{
			var field = document.getElementById(digits[i]);
			if (!isDigital(field.value)) {
				alert("Field " + field.title + " is required to be filled in only with digits (0-9) and decimal point before successful submission.");
				field.focus();
				return false;
				break;
			}}
		for (i=0;i<0;i++)	{
			var field = document.getElementById(currs[i]);
			if (!isCurrency(field.value)) {
				alert("Field " + field.title + " is required to be filled in only with digits (0-9) a decimal point, or a dollar sign before successful submission.");
				field.focus();
				return false;
				break;
			}}
		return true; }
		
		
// Page OnLoad Calls --------------------

	window.onload = externalLinks;
