// Global Namespace

var AppGlobal;
if (!AppGlobal) AppGlobal = {};

// Declare global variables
AppGlobal.BASE_URL = '/acp/';
AppGlobal.CLASSPATH = 'js/classes/';

// Declare global variables
var win = null;

// Define prototypes
Number.prototype.NaN0 = function() { return isNaN(this)?0:this; }
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();
	}
};

// Sets up image rollovers
AppGlobal.setupRolloverImages = function() {
	// 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 && thisImage.indexOf('_dis.') == -1) {
			// Preload rollover image
			var normalURL = thisImage;
			var rolloverURL = thisImage.replace('.', '_on.');
			// Setup rollover events
			AppGlobal.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 && thisImage.indexOf('_dis.') == -1) {
				// Preload rollover image
				var normalURL = thisImage;
				var rolloverURL = thisImage.replace('.', '_on.');
				// Setup rollover events
				AppGlobal.addRollover(inputElements[x], normalURL, rolloverURL);
			}
		}
	}
}

// Image button rollover/press handler handler
AppGlobal.addRollover = function(img, normalURL, rolloverURL) {
	(new Image()).src = rolloverURL;
	img.onmouseover = function() { img.src = rolloverURL; }
	img.onmouseout = function() { img.src = normalURL; }
	if (arguments.length > 3) {
		var hitURL = arguments[3];
		(new Image()).src = hitURL;
		img.onmousedown = function() { img.src = hitURL; }
		img.onmouseup = function() { img.src = rolloverURL; }
	}
}

// Performs basic form validation
AppGlobal.validateContactForm = function(form) {
	var vNameError = false;
	var vPhoneEmailError = false;
	var vMessageError = false;
	var vLocationError = false;
	if (form.txtName.value == "") vNameError = true;
	if (form.txtPhone.value == "" && form.txtEmail.value == "") vPhoneEmailError = true;
	if (form.txtMessage.value == "") vMessageError = true;
	if (form.ddlLocation.selectedIndex == 0) vLocationError = true;
	if (vNameError || vPhoneEmailError || vMessageError || vLocationError) {
		var vError = "The following item(s) are missing:";
		if (vNameError) vError += "\n- Name";
		if (vPhoneEmailError) vError += "\n- Phone or Email Address";
		if (vMessageError) vError += "\n- Your Message";
		if (vLocationError) vError += "\n- Select the appropriate office to send this to";
		alert(vError);
		return false;
	} else return true;
}

// OnLoad directive
AppGlobal.onLoad = function() {
	// Sets up image rollovers
	AppGlobal.setupRolloverImages();
}

// Assign onload parameter
if (window.addEventListener) window.addEventListener("load", AppGlobal.onLoad, false);
else if (window.attachEvent) window.attachEvent("onload", AppGlobal.onLoad);
