var app_path='..'; var err_clr='#def4fc';

String.prototype.trim = function () { return this.replace(/^\s*|\s*$/,""); };
String.prototype.underscore = function () { return this.replace(/\s*/,"_"); };

function ss(txt){ window.status = txt; return true; }

function is_obj(objToTest) { // This function checks for valid objects - simpler
  var chk=(objToTest == null || objToTest == undefined)?0:1; return chk;
}

function create_xhr(){
	var obj;
	if(window.ActiveXObject){	obj = new ActiveXObject("Microsoft.XMLHTTP"); } 
	else if (window.XMLHttpRequest){ obj = new XMLHttpRequest(); }
	return obj;
}
// JavaScript Document
// Onload event listening function
function addLoadListener(fn){
	if (typeof window.addEventListener != 'undefined'){
		window.addEventListener('load', fn, false);
	} else if (typeof document.addEventListener != 'undefined'){
		document.addEventListener('load', fn, false);
	} else if (typeof window.attachEvent != 'undefined'){
		window.attachEvent('onload', fn);
	} else {
		var oldfn = window.onload;
		if (typeof window.onload != 'function'){
			window.onload = fn;
		} else {
			window.onload = function(){
				oldfn();
				fn();
			};
		}
	}
}

addLoadListener(function(){ window.defaultStatus=''; });

function attachEventListener(target, eventType, functionRef, capture) {
	if (typeof target.addEventListener != "undefined") {
		target.addEventListener(eventType, functionRef, capture);
	} else if (typeof target.attachEvent != "undefined") {
		var functionString = eventType + functionRef;
		target["e" + functionString] = functionRef;
		target[functionString] = function(event) {
			if(typeof event == "undefined"){event = window.event}; target["e" + functionString](event);
		};
		target.attachEvent("on" + eventType, target[functionString]);
	} else {
		eventType = "on" + eventType;
		if (typeof target[eventType] == "function") {
			var oldListener = target[eventType];
			target[eventType] = function() {
				oldListener();
				return functionRef();
			}
		} else {
			target[eventType] = functionRef;
		}
	}
	return true;
}

function stopDefaultAction(event) {
	event.returnValue = false; if (typeof event.preventDefault != "undefined") { event.preventDefault(); } return true; 
}

function stopEvent(event) {
	if (typeof event.stopPropagation != "undefined") { event.stopPropagation(); } else { event.cancelBubble = true; } return true;
}

function detachEventListener(target, eventType, functionRef, capture) {
	if (typeof target.removeEventListener != "undefined") {
		target.removeEventListener(eventType, functionRef, capture)
	} else if (typeof target.detachEvent != "undefined") {
		var functionString = eventType + functionRef;
		target.detachEvent("on" + eventType, target[functionString]);
		target["e" + functionString] = null;
		target[functionString] = null;
	} else {
		target["on" + eventType] = null;
	}
	return true;
}

function getEventTarget(event) {
	var targetElement = null;
	if (typeof event.target != "undefined") {
		targetElement = event.target;
	} else {
		targetElement = event.srcElement;
	}
	while (targetElement.nodeType == 3 && targetElement.parentNode != null) {
		targetElement = targetElement.parentNode;
	}
	return targetElement;
}

function getPosition(theElement) {
	var positionX = 0; var positionY = 0;
	while (theElement != null) {
		positionX += theElement.offsetLeft; positionY += theElement.offsetTop; theElement = theElement.offsetParent;
	}
	return [positionX, positionY];
}

function displayCursorPosition(event) {
	if (typeof event == "undefined") { event = window.event; }
	var scrollingPosition = getScrollingPosition();
	var cursorPosition = [0, 0];
	if (typeof event.pageX != "undefined" && typeof event.x != "undefined") {
		cursorPosition[0] = event.pageX; cursorPosition[1] = event.pageY;
	} else {
		cursorPosition[0] = event.clientX + scrollingPosition[0]; cursorPosition[1] = event.clientY + scrollingPosition[1];
	}
	var paragraph = document.getElementsByTagName("p")[0];
	paragraph.replaceChild(document.createTextNode("Your mouse is currently located at: " + cursorPosition[0] + "," + cursorPosition[1]), paragraph.firstChild);
	return true;
}


// Get Element By Attribute function
function getElementsByAttribute(attribute, attributeValue){
	var elementArray = new Array();
  var matchedArray = new Array();
	if (document.all){ elementArray = document.all; } else { elementArray = document.getElementsByTagName("*"); }
	for (var i = 0; i < elementArray.length; i++){
		if (attribute == "class"){
			var pattern = new RegExp("(^| )" + attributeValue + "( |$)");
			if (pattern.test(elementArray[i].className)){
				matchedArray[matchedArray.length] = elementArray[i];
			}
		} else if (attribute == "for") {
			if (elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for")) {
				if (elementArray[i].htmlFor == attributeValue) {
					matchedArray[matchedArray.length] = elementArray[i];
				}
			}
		} else if (elementArray[i].getAttribute(attribute) == attributeValue) {
			matchedArray[matchedArray.length] = elementArray[i];
		}
	}
	return matchedArray;
}


function addClass(target, classValue) {
	var pattern = new RegExp("(^| )" + classValue + "( |$)");
	if (!pattern.test(target.className)) {
		if (target.className == "") {
			target.className = classValue;
		} else {
			target.className += " " + classValue;
		}
	}
	return true;
}

function removeClass(target, classValue) {
	var removedClass = target.className;
	var pattern = new RegExp("(^| )" + classValue + "( |$)");
	removedClass = removedClass.replace(pattern, "$1"); removedClass = removedClass.replace(/ $/, ""); target.className = removedClass; return true;
}

function makePopup(url, width, height, overflow){
	if (width > 640) { width = 640; }
	if (height > 480) { height = 480; }
	if (overflow == '' || !/^(scroll|resize|both)$/.test(overflow)) { overflow = 'both'; }
	var win = window.open(url, '', 'width=' + width + ', height=' + height
      + ',scrollbars=' + (/^(scroll|both)$/.test(overflow) ? 'yes' : 'no')
      + ',resizable=' + (/^(resize|both)$/.test(overflow) ? 'yes' : 'no')
      + ',status=yes,toolbar=no,menubar=no,location=no');
	return win;
}

function getScrollingPosition() {
	var position = [0, 0];
	if (typeof window.pageYOffset != 'undefined') {
		position = [
			window.pageXOffset,
			window.pageYOffset
    	];
	} else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0) {
		position = [
			document.documentElement.scrollLeft,
			document.documentElement.scrollTop
		];
	} else if (typeof document.body.scrollTop != 'undefined') {
		position = [
			document.body.scrollLeft,
			document.body.scrollTop
		];
	}
	return position;
}

function getViewportSize() {
	var size = [0, 0];
	if (typeof window.innerWidth != 'undefined') {
		size = [
			window.innerWidth,
			window.innerHeight
		];
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		size = [
			document.documentElement.clientWidth,
			document.documentElement.clientHeight
		];
	} else {
		size = [
			document.getElementsByTagName('body')[0].clientWidth,
			document.getElementsByTagName('body')[0].clientHeight
		];
	}
	return size;
}


// Rounding function
// Ex. n = 3.942487; roundTo(n, 0) = 4; round(n, 3) = 3.942
function roundTo(base, precision){
	var m = Math.pow(10, precision);
	var a = Math.round(base * m) / m;
	return a;
}

// Random function
// Ex. random number between 1 and 100: random(1, 100);
function randomBetween(min, max){ return min + Math.floor(Math.random() * (max - min + 1)); }

// Format currency function
// Ex. formatTo(3.942, 2) = 3.94; formatTo(4.003, 2) = 4.00
function formatTo(base, precision){
	var a = roundTo(base, precision); var s = a.toString();
	var decimalIndex = s.indexOf(".");
	if (precision >0 && decimalIndex < 0){ decimalIndex = s.length; s += '.'; }
	while (decimalIndex + precision + 1 > s.length){ s += '0'; }
	return s;
}

// Compute ordinal function
function getOrdinal(n){
	var ord = 'th';
	if (n % 10 == 1 && n % 100 != 11){
		ord = 'st';
	} else if (n % 10 == 2 && n % 100 != 12){
		ord = 'nd';
	} else if (n % 10 == 3 && n % 100 != 13){
		ord = 'rd';
	}
	return ord;
}
	
// Array push function prototype
Array.prototype.push = function(){
	for (var i = 0; i < arguments.length; i++){
		this[this.length] = arguments[i];
	} return arguments[i - 1];
};

// Array splice function prototype
Array.prototype.splice = function(a, b){
	var tmp = [];
	for (var i = a + b; i < this.length; i++){
		tmp[tmp.length] = this[i];	
	}
	var rem = [];
	for (i = a; i < a + b; i++){
		rem[rem.length] = this[i];
	}
	this.length = a;
	for (i = 2; i < arguments.length; i++){
		this[this.length] = arguments[i];	
	}
	for (i = 0; i < tmp.length; i++){
		this[this.length] = tmp[i];	
	}
	return rem;
};


// COOKIES
function getCookie(searchName) {
	var cookies = document.cookie.split(";");
	for (var i = 0; i < cookies.length; i++) {
		var cookieCrumbs = cookies[i].split("=");
		var cookieName = cookieCrumbs[0];
		var cookieValue = cookieCrumbs[1];
		if (cookieName == searchName) {
			return cookieValue;
		}
	}
	return false;
}

// DATES
// Ex. var today = new Date(); var us = today.getDateString('%month %date%ordinal %year'); us = "June 19th 2005"
Date.prototype.getDateString = function(str) {
	var dnames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
	var mnames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'Novemeber', 'December'];
	str = str.replace('%day', dnames[this.getDay()]);
	str = str.replace('%date', this.getDate());
	str = str.replace('%ordinal', this.getDateOrdinal());
	str = str.replace('%month', mnames[this.getMonth()]);
	str = str.replace('%year', this.getFullYear());
	return str;
};

Date.prototype.getDateOrdinal = function() {
	var n = this.getDate();
	var ord = 'th';
	if (n % 10 == 1 && n % 100 != 11) { ord = 'st'; }
	else if (n % 10 == 2 && n % 100 != 12) { ord = 'nd'; }
	else if (n % 10 == 3 && n % 100 != 13) { ord = 'rd'; }
	return ord;
};

Date.prototype.getISODate = function() {
	var mth = this.getMonth() + 1;
	mth = (mth < 10 ? '0' : '') + mth;
	var date = this.getDate();
	date = (date < 10 ? '0' : '') + date;
	return this.getFullYear() + mth + date;
};

// OBJ
function get_obj(idx){ return document.getElementById(idx); }

// ROLLOVER

function ro_init(){
	for(var i=0;i<document.images.length;i++){
		dpattern = /^(ro)/;
		if(document.images[i].parentNode.tagName=="A" && dpattern.test(document.images[i].className)){ ro_setup(document.images[i]);	}
	}
}
function ro_setup(img){
	img.out = new Image(); img.out.src = img.src;	img.onmouseout = rollOut; img.onclick = rollOut;
	img.over = new Image();	img.over.src = app_path+"/gfx/"+img.alt+".a.gif"; img.onmouseover = rollOver;
}
function rollOver(){ this.src=this.over.src; }
function rollOut(){ this.src=this.out.src; }
function toggle_layer(id){ if(is_obj(id)){get_obj(id).style.display=(get_obj(id).style.display=='block')?'none':'block'; }}
function set_display(id,dis){	if(is_obj(id)){ get_obj(id).style.display=(dis=='n')?'none':'block'; }}
function set_row(id,dis){	if(is_obj(id)){ get_obj(id).style.display=(dis=='n')?'none':''; }}
function set_div(id,dis){	if(is_obj(id)){ get_obj(id).className=(dis=='n')?'hdiv':'sdiv'; }}