/*
	VERSION:
	3/13/2008	1.0.1	Updated baseDir to designate the level of the base directory.
				1.0.0	Begin versioning.
*/
function domain() {
	//returns the base url
	var url = window.location.protocol + "//" + window.location.host + "/";
	return url;
}

function baseDir(level) {
	//returns base url and the top level directory the current file resides in
	// example: if calling file resides in "http://www.test.com/files/test/index.htm"
	//		baseDir(0) returns "http://www.test.com/"
	//		baseDir(1) returns "http://www.test.com/files/"
	//		baseDir(2) returns "http://www.test.com/files/test/"
	
	var level = (typeof(level) != "number" || level < 0 ? 0 : level);	
	if(level == 0) { return domain(); }
	var url = window.location.pathname.split("/");
	url = url.slice(1, level+1);
	return domain() + url.join("/") + "/";
};

//-------------------------------------------------------------------------------
function go(url) { window.location.href=url; };

//-------------------------------------------------------------------------------
function dw(str) { document.write(str); };

//-------------------------------------------------------------------------------
function refresh() { window.location.href = window.location.href; };
//-------------------------------------------------------------------------------
function isEnterPress(evt) {
	/*
		this function is handy if you want a text field
		to do something other than submit the form
		when enter is pressed while focused
		
		USAGE EXAMPLE:
		document.forms.myTextField.onkeypress = function(event) {
			if(isEnterPress(event) {
				//do something
			}
		}
	
	*/
    var evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode :
        ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13 || charCode == 3) {
        return true;
    } else {
        return false;
    }
};

//-------------------------------------------------------------------------------
function getQS(varName) {
	var qs = window.location.search;
	var rexp = new RegExp("("+varName+"=)","i");
	if ( varName == 0 ) { return qs; } // returns entire query string starting with ?
	else if ( qs.search(rexp) == -1 ) { return false; } // can't find this variable name in query string
	else {
		rexp.compile("([\?|&]"+varName+"=)([^&]*)","gi");
		var arVal = qs.match(rexp); // returns an array of matching name=value pairs, i.e. ["test=1","test=2","test=3"]
		var thisStr = "";
		for (i in arVal) { // loop through previous compiled array
			if(typeof(arVal[i]) != "string") {
				continue;
			}
			arVal[i] = arVal[i] + "";
			thisStr = arVal[i].split("="); // split each name=value pair at =
			thisStr[1] = thisStr[1].replace(/\+/gi, ' '); //unescape doesn't recognize the + sign as a space
			arVal[i] = unescape(thisStr[1]); // reassign only the value to this spot in array
		}
		return arVal;
	}
};

//-------------------------------------------------------------------------------
function setCookie(cookieName, cookieValue, expireDays, cookiePath) {
	var exdate = new Date();
	if(!isNothing(expireDays) && !isNaN(expireDays)) {
		var d = expireDays * 24; //days to hours
			d *= 60; //hours to minutes
			d *= 60; //minutes to seconds
			d *= 1000; //seconds to milliseconds
			exdate = new Date(exdate.valueOf() + d);
	}
	document.cookie = cookieName + "=" + escape(cookieValue) +
	";expires=" + exdate.toGMTString() +
	";path=" + (isNothing(cookiePath) ? "/" : cookiePath);
};

function getCookie(cookieName){
	if(document.cookie.length > 0) {
		c_start=document.cookie.indexOf(cookieName + "=")
		if (c_start != -1) { 
			c_start=c_start + cookieName.length + 1;
			c_end=document.cookie.indexOf(";", c_start);
			if (c_end == -1) { c_end = document.cookie.length; }
			return unescape(document.cookie.substring(c_start, c_end));
		} 
	}
	return null;
};


//-------------------------------------------------------------------------------
/*
	A simple event handler class that takes
	a string of javascript code as it's parameter
	and stores it until an event, usually window.onload,
	calls the execute method. Executes the scripts
	in the order they are attached.
*/
function EventHandler() {
	this.arEvents = new Array();
	this.attach = function(executable) {
		//any string of valid javascript code
		this.arEvents.push(executable);
	}
	this.execute = function() {
		//to be called from the main event
		//such as window.onload
		//or as an inline script
		//such as after a form loads
		var i;
		var n = this.arEvents.length;
		for(i = 0; i < n; i++) {
			eval(this.arEvents[i]);
		};
		this.clearEvents(); //so it can't accidently be called twice
	}
	this.clearEvents = function() {
		this.arEvents.length = 0;
	}
};
//-------------------------------------------------------------------------------
function getDocumentHeight() {
	var docHeight;
	if (typeof document.height != 'undefined') {
		docHeight = document.height;
	} else if (document.compatMode && document.compatMode != 'BackCompat') {
		docHeight = document.documentElement.scrollHeight;
	} else if (document.body && typeof document.body.scrollHeight != 'undefined') {
		docHeight = document.body.scrollHeight;
	}
	return docHeight;
};
