// JavaScript Document

// Declare variables
var browserName = navigator.appName; 
var browserVer = parseInt(navigator.appVersion);

// Define string prototypes
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
String.prototype.toInteger = function() { return (isNaN(this)) ? 0 : parseInt(this); };
String.prototype.toCurrency = function() { 
	num = this;
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) return "0.00";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	return num + "." + cents;
};
String.prototype.padLeft = function(len) {
	var char = "0";
	var str = this;
	if (arguments > 1) char = arguments[1];
	for (var x = str.length; x < len; x ++) str = char + str + '';
	return str;
};
String.prototype.padRight = function(len) {
	var char = "0";
	var str = this;
	if (arguments > 1) char = arguments[1];
	for (var x = str.length; x < len; x ++) str += char + '';
	return str;
};
String.prototype.toDate = function() {
	var val = this.replace(/[-.]/g, "/");
	var tmp = Date.parse(val);
	if (isNaN(tmp)) return this;
	else {
		var yearDigits = (val.match(/\/[0-9]{2}$/) != null) ? 2 : 4;
		d = new Date(tmp);
		if (d.getFullYear() < '1950' && yearDigits == 2) d.setFullYear(d.getFullYear() + 100);
		return parseInt(d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
	}
};
Date.prototype.add = function(sInterval, iNum){
	var dTemp = this;
	if (!sInterval || iNum == 0) return dTemp;
	switch (sInterval.toLowerCase()){
		case "ms":
			dTemp.setMilliseconds(dTemp.getMilliseconds() + iNum);
			break;
		case "s":
			dTemp.setSeconds(dTemp.getSeconds() + iNum);
			break;
		case "mi":
			dTemp.setMinutes(dTemp.getMinutes() + iNum);
			break;
		case "h":
			dTemp.setHours(dTemp.getHours() + iNum);
			break;
		case "d":
			dTemp.setDate(dTemp.getDate() + iNum);
			break;
		case "mo":
			dTemp.setMonth(dTemp.getMonth() + iNum);
			break;
		case "y":
			dTemp.setFullYear(dTemp.getFullYear() + iNum);
			break;
	}
	return dTemp;
}


// Image preloader script
function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

// Sets up image rollovers
function setupRollovers() {
	// Get all of the images on this page and iterate through them to determine if any are rollovers
	var pageImages = document.getElementsByTagName("img");
	for (var x = 0; x < pageImages.length; x++) {
		// Get the base name of the current image
		var thisImage = pageImages[x].src.replace(location.protocol + '//' + location.hostname, '');
		if (thisImage.indexOf('/nav/') != -1) {
			// Preload rollover image
			var normalURL = thisImage;
			var rolloverURL = thisImage.replace('.', '_on.');
			// Setup rollover events
			addRollover(pageImages[x], normalURL, rolloverURL);
		}
	}
	// Get all of the image buttons on this page and iterate through them to determine if any are rollovers
	var inputElements = document.getElementsByTagName("input");
	for (var x = 0; x < inputElements.length; x++) {
		if (inputElements[x].getAttribute("type") == "image") {
			var thisImage = inputElements[x].src.replace(location.protocol + '//' + location.hostname, '');
			if (thisImage.indexOf('/nav/') != -1) {
				// Preload rollover image
				var normalURL = thisImage;
				var rolloverURL = thisImage.replace('.', '_on.');
				// Setup rollover events
				addRollover(inputElements[x], normalURL, rolloverURL);
			}
		}
	}
}

// Validates the contact form
function validateContactForm(form) {
	if (form) {
		var error = false;
		var strError = "Please provide us with the following:\n";
		if (form.txtName.value == '') {
			error = true;
			var strError = strError + "- Your name\n";
		}
		if (form.txtPhone.value == '' && form.txtEmail.value == '') {
			error = true;
			var strError = strError + "- Your phone OR e-mail address\n";
		}
		if (form.txtMessage.value == '') {
			error = true;
			var strError = strError + "- A brief message to us\n";
		}
		if (form.txtEmail.value != '' && !validateEmail(form.txtEmail.value)) {
			error = true;
			var strError = strError + "- The e-mail address does not appear to be valid\n";
		}
		if (error) {
			alert(strError);
			return false;
		} else return true;
	} else return true;
}

// Checks an e-mail address for validity
function validateEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1) return false;
	else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
	else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
	else if (str.indexOf(at,(lat+1))!=-1) return false;
	else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
	else if (str.indexOf(dot,(lat+2))==-1) return false;
	else if (str.indexOf(" ")!=-1) return false;
	else return true					
}

// Driving directions validation
function validateDrivingDirections(form) {
	if (form.txtStreet && form.txtCSZ) {
		if (form.txtCSZ.value.trim() == "") {
			alert('A city and state, or zip is required.');
			return false;
		} else {
			var strAddress = form.txtStreet.value;
			if (strAddress.length > 0) strAddress += ", ";
			strAddress += form.txtCSZ.value;
			form.saddr.value = strAddress;
			return true;
		}
	} else {
		alert('oops');
		return true;
	}
}

// Image button rollover/press handler handler
function addRollover(img, normalURL, rolloverURL) {
	preloadImages(rolloverURL);
	img.onmouseover = function() { img.src = rolloverURL; }
	img.onmouseout = function() { img.src = normalURL; }
}

// Pricing OnLoad directive
function globalOnLoad() {
	// Preload images
	setupRollovers();
}

if (window.addEventListener) window.addEventListener("load", globalOnLoad, false);
else if (window.attachEvent) window.attachEvent("onload", globalOnLoad);