function sendMail(user,domain,tld,subject,message) {
	locationstring = "mailto:" + user + "@" + domain + "." + tld;
	if (subject) {
		locationstring += "?subject=" + encodeURIComponent(subject);
	}
	if (message) {
		locationstring += "&body=" + encodeURIComponent(message);
	}
	window.location = locationstring;
}

function openWindow(url,features) {
	var newWin = window.open(url,'popup',features);
	newWin.focus();
}

function externalLinks() { 
	if (!document.getElementsByTagName) return;
	var pattern = /external/;
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && pattern.test(anchor.getAttribute("rel"))) 
		anchor.target = "_blank"; 
	} 
}

function fixIE() {
	if (document.all && document.getElementById) {
		var navRoot = document.getElementById("nav");
		var listItems = navRoot.getElementsByTagName("LI");
		for (var i=0; i<listItems.length; i++) {
			e = listItems[i];
			e.onmouseover=function() {
				this.className+=" over";
			}
			e.onmouseout=function() {
				this.className=this.className.replace(" over","");
			}
		}
	}
}

function makeRollovers() { 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a");
	var mouseovers = new Array;
	var j = 0;
	for (var i=0; i<anchors.length; i++) {
   	var e = anchors[i]; 
   	if (/\brollover\b/.test(e.className)) {
			mouseovers[j] = new Image;
			mouseovers[j].src = e.childNodes[0].src.replace('_up','_ov');
			j++;
			e.onmouseover = function() { 
				this.childNodes[0].src = this.childNodes[0].src.replace('_up','_ov');
			}
			e.onmouseout = function() {
				this.childNodes[0].src = this.childNodes[0].src.replace('_ov','_up');
			}		
		}
	} 
}

function filesToBlank() { 
	if (!document.getElementsByTagName) return;
	var pattern = /\.pdf$/i;
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && pattern.test(anchor.href)) 
		anchor.target = "_blank"; 
	} 
}


/**
 ** FORM VALIDATION & MANIPULATION FUNCTIONS
 **/
function clearFields() {
	for (i = 0; i<arguments.length; i++) {
		e = document.getElementById(arguments[i]);
		e.value = '';
	}
}

function checkRadio(id) {
	e = document.getElementById(id);
	e.checked = true;
}

function formatNumber(id, digits) {
	var e = document.getElementById(id);
	var stripped = e.value.replace(/\D/g,'');
	if (stripped.length !== digits) {
		alert('Please enter a valid ' + digits + '-digit number.');
	}
	else {
		e.value = stripped;
	}
}

function formatPhone(id) { // Pass a form element's ID onchange
	var e = document.getElementById(id);
	var stripped = e.value.replace(/\D/g,''); // Strip out non-numerical characters
	if (e.value.match(/\w/)) { // If it's not empty...
		if (stripped.length != 10) {
			alert('Please enter a phone number with area code.');
			e.focus();
		}
		else {
			e.value = '(' + stripped.substring(0,3) + ') ' + stripped.substring(3,6) + '-' + stripped.substring(6,10);
			// formats number as (xxx) xxx-xxxx and writes to field
		}
	}
}

function formatZip(id) { // Pass a form element's ID onchange
	var e = document.getElementById(id);
	var stripped = e.value.replace(/\D/g,''); // Strip out non-numerical characters
	if (e.value.match(/\w/)) { // If it's not empty...
		if (stripped.length == 5) {
			return true;
		}
		else if (stripped.length == 9) {
			e.value = stripped.substring(0,5) + '-' + stripped.substring(5,9)
		}
		else alert('Please enter a valid 5- or 9-digit ZIP Code.');
	}
}

function checkEmail(id) { // Pass a form element's ID onchange
	var e = document.getElementById(id);
	re = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	if(!re.test(e.value) && e.value.match(/\w/)) { // Passes the Regexp test and is not empty
		alert('"' + e.value + '" does not appear to be a valid email address. Please ensure you have provided an accurate email address before submitting this form.');
		e.select();
		e.focus();
	}
}

function validate(f) {
	var msg = '';
	for (var i=0; i<f.elements.length; i++) {
		var e = f.elements[i];
		e.style.background = 'white';
		if (e.required && !e.value.match(/\w/)) {
			msg += e.title + '\n';
			e.style.background = '#fcc';
		}
	}
	if (msg != '') {
		alert('The following fields are required:\n' + msg);
		return false;
	}
}

function init() {
	fixIE();
	externalLinks();
	makeRollovers();
	filesToBlank();
}
