/***************************
 * SITE-SPECIFIC FUNCTIONS *
 ***************************/
 
 
/*********************
 * UTILITY FUNCTIONS *
 *********************/
 
function sendMail(user,domain,tld,subject,message) {
	if (!user) user = 'info';
	if (!domain) domain = 'mortonplumbing';
	if (!tld) tld = 'net';
	locationstring = 'mailto:' + user + '@' + domain + '.' + tld;
	if (subject) locationstring += '?subject=' + encodeURIComponent(subject);
	if (message) locationstring += '&body=' + encodeURIComponent(message);
	window.location = locationstring;
}

function sendExternalMail(subject,message,cc) {
	locationstring = 'mailto:';
	if (subject) locationstring += "?subject=" + encodeURIComponent(subject);
	if (message) locationstring += "&body=" + encodeURIComponent(message);
	if (cc) locationstring += "&cc=" + encodeURIComponent(cc);
	window.location = locationstring;
}

function openWindow(url,features) {
	var newWin = window.open(url,'popup',features);
	newWin.focus();
}

function closeWindow() {
	if (window.opener) {
		self.close();	
	}
}

function printPage() {
	if (window.print != null) {
		window.print();
	}
	else {
		alert("To print this page, please select Print from your browser's File menu.");
	}
}

function preload() {
	var path = '/images/';
	var images = new Array('');
	var preload = new Array('');
	var j = images.length;
	for (var i=0; i<images.length; i++) {
		preload[j] = new Image;
		preload[j++].src = path + images[i];
	}
}

function externalLinks() {
	$('a[rel*=external]').attr('target','_blank');	
}

function filesToBlank() {
	$('a[href$=.pdf],a[href$=.PDF]').attr('target','_blank');//.append(' [PDF]');
	$('a[href$=.doc],a[href$=.DOC]').attr('target','_blank');//.append(' [DOC]');
	$('a[href$=.ppt],a[href$=.PPT]').attr('target','_blank');//.append(' [PPT]');
	$('a[href$=.pps],a[href$=.PPS]').attr('target','_blank');//.append(' [PPS]');
}

function makeRollovers(){
	$('a.rollover').each(function() {
		var img = new Image();
		var upimage = $(this).children().eq(0).attr('src');
		img.src = upimage.replace('_up','_ov');
	});
	$('a.rollover').hover(
		function(){
			var overimage = $(this).children().eq(0).attr('src').replace('_up','_ov');
			$(this).children().eq(0).attr('src',overimage);
		},
		function(){
			var upimage = $(this).children().eq(0).attr('src').replace('_ov','_up');
			$(this).children().eq(0).attr('src',upimage);
		}
	);
}

function actionLinks() {
	$('a.action').append(' &#187;');
}

function init() {
	externalLinks();
	filesToBlank();
	makeRollovers();
	actionLinks();
	$('.sf-menu').superfish();
}

/********************************************
 * FORM VALIDATION & MANIPULATION FUNCTIONS *
 ********************************************/
 
function clearFields() {
	for (i = 0; i<arguments.length; i++) {
		var 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 checkValue(id, minimum, maximum) {
	if (!minimum) minimum = 0;
	if (!maximum) maximum = Number.MAX_VALUE;
	var e = document.getElementById(id);
	if (parseFloat(e.value) < minimum || parseFloat(e.value) > maximum) {
		alert('Please enter a number between ' + minimum + ' and ' + maximum + '.');	
	}
}

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 formatSSN(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 != 9) {
			alert('Please enter a valid Social Security Number.');
			e.focus();
		}
		else {
			e.value = stripped.substring(0,3) + '-' + stripped.substring(3,5) + '-' + stripped.substring(5,9);
			// formats number as xxx-xx-xxxx and writes to field
		}
	}
}

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 checkURL(id) { // Pass a form element's ID onchange
	var e = document.getElementById(id);
	re = /^((http|https|ftp):\/\/)?([a-z0-9\-\.])+(\.)([a-z]){2,4}([a-z0-9\/\+=%&_\.~?\-])*$/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 URL (website address). Please check for accuracy.');
		e.select();
		e.focus();
	}
}

function validate(f,buttontext,waitmessage) {
	var msg = '';
	if (!buttontext) buttontext = 'Submit';
	if (!waitmessage) waitmessage = 'Please Wait';
	f.elements['submit'].disabled = true;
	f.elements['submit'].value = waitmessage;

	for (var i=0; i<f.elements.length; i++) {
		var e = f.elements[i];
		var p = f.elements[i].parentNode;
		p.className = p.className.replace(/\berror\b/,'');
		if (e.required && e.type != 'select' && !e.value.match(/\w/)) {
			msg += e.title + '\n';
			p.className += ' error';
		}
		else if (e.required && e.type == 'select' && e[e.selectedIndex].value == '') {
			msg += e.title + '\n';
			p.className += ' error';
		}
	}
	if (msg != '') {
		alert('The following fields are required:\n' + msg);
		f.elements['submit'].disabled=false;
		f.elements['submit'].value=buttontext;
		return false;
	}
}

function formInit(id) {
	var e = document.createElement("input");
	e.name = "submitted";
	e.type = "hidden";
	e.value = "1";
	document.getElementById(id).appendChild(e);
}