// 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;
}

// Set up object for AJAX requests
var request = null;
try {
	request = new XMLHttpRequest();
} catch (trymicrosoft) {
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (othermicrosoft) {
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (failed) {
			request = null;
		}
	}
}

// Returns the value of an XML node
function getXMLValue(node) {
	return (node && node.firstChild) ? node.firstChild.nodeValue : ""
}

// Replaces node content with text
function replaceText(elem, newtext) {
	while (elem.firstChild != null) elem.removeChild(elem.firstChild);
	elem.appendChild(document.createTextNode(newtext));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function FixCookieDate (date) {
	var base = new Date(0);
	var skew = base.getTime(); // dawn of (Unix) time - should be 0
	if (skew > 0)  // Except on the Mac - ahead of its time
		date.setTime (date.getTime() - skew);
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

// 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);
			}
		}
	}
}

// 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; }
}

// 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 return true;
}

// Displays and hides the loader progress wheel
function showLoader(val) {
	var id = (arguments.length > 1) ? arguments[1] : "progresswheel";
	var progress = document.getElementById(id);
	if (progress) progress.style.visibility = (val) ? 'visible' : 'hidden';
}

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

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