//=============== Simple Button Roll Event Handlers ===================================
function doButtonRollOn(theEventObject){
	var theTmpID = null;
	if((Event) && (Event.element)){
		theTmpID = Event.element(theEventObject).id;
		if(theTmpID){
			$(theTmpID).style.cursor = 'pointer';
		}
	}
}
function doButtonRollOff(theEventObject){
	var theTmpID = null;
	if((Event) && (Event.element)){
		theTmpID = Event.element(theEventObject).id;
		if(theTmpID){
			$(theTmpID).style.cursor = 'default';
		}
	}
}
//============== Scroll ElementName to top of viewport ========================
function doScrollElementToTop(theScrollingElementName){
	if($(theScrollingElementName)){
		$(theScrollingElementName).scrollTo();
	}
}
//============== get and set the current Y scroll position ==========================
var	scrOfY = 0;
function saveScrollY() {
	if(document.body &&
		(document.body.scrollLeft ||
		 document.body.scrollTop)){
			//DOM compliant
		scrOfY = (document.body.scrollTop);
	} else if(document.documentElement &&
			  (document.documentElement.scrollLeft ||
			   document.documentElement.scrollTop)){
			//IE6 standards compliant mode
		scrOfY = (document.documentElement.scrollTop);
	}
}
function getScrollYAbs() {
	if(document.body &&
		(document.body.scrollLeft ||
		 document.body.scrollTop)){
			//DOM compliant
		return(document.body.scrollTop);
	} else if(document.documentElement &&
			  (document.documentElement.scrollLeft ||
			   document.documentElement.scrollTop)){
			//IE6 standards compliant mode
		return(document.documentElement.scrollTop);
	}
	return(null);
}
function restoreScrollY() {
	if(document.body &&
		(document.body.scrollLeft ||
		 document.body.scrollTop)){
			//DOM compliant
		document.body.scrollTop = scrOfY;
	} else if(document.documentElement &&
			  (document.documentElement.scrollLeft ||
			   document.documentElement.scrollTop)){
			//IE6 standards compliant mode
		document.documentElement.scrollTop = scrOfY;
	}
}
function setScrollYAbs(yScrollNum) {
	if(document.body &&
		(document.body.scrollLeft ||
		 document.body.scrollTop)){
			//DOM compliant
		document.body.scrollTop = yScrollNum;
	} else if(document.documentElement &&
			  (document.documentElement.scrollLeft ||
			   document.documentElement.scrollTop)){
			//IE6 standards compliant mode
		document.documentElement.scrollTop = yScrollNum;
	}
}
//============== getCurrentDate in mySQL format yyyy-mm-dd ====================
function getCurrentDateMySQLFmt(){
	theDate = new Date();
	theYear = theDate.getFullYear();
	theMonth = ((theDate.getMonth() + 1.0) < 10) ?
					('0' + (theDate.getMonth() + 1.0)) :
					(theDate.getMonth() + 1.0);
	theDay = (theDate.getDate() < 10) ?
					('0' + theDate.getDate()) :
					theDate.getDate();
	return(theYear + '-' + theMonth + '-' + theDay);
}
//========== global arrays and access for date strings =========================
gDaysArray = new Array(
						"Sunday","Monday","Tuesday","Wednesday",
						"Thursday","Friday","Saturday","Sunday");
gMonthsArray = new Array(
						"January", "February", "March", "April",
						"May", "June", "July", "August",
						"September", "October", "November", "December");
function getDayString(theDayIndex){
	return(gDaysArray[theDayIndex]);
}
function getMonthString(theMonthIndex){
	return(gMonthsArray[theMonthIndex]);
}
//==================== convertYmdToMdy ========================================
// Convert:
//   0000-00-00 Date format to
//   January x, xxxx format
function convertYmdToMdy(dateString){
	var tmpString = null;
	var dateArray = null;
	dateArray = dateString.split('(')[1].split(' ')[0].split('-');
	tmpString = gMonthsArray[dateArray[1] - 1] +
				'<br>' +
				dateArray[2] +
				', ' +
				dateArray[0];
	return(tmpString);
}
//========== UTF-8 data encode / decode ==========================================
// usage:
//	var encodedData = Utf8.encode('string to encode');
//	var decodedData = Utf8.decode('string to encode');
var Utf8Codec = {
		// public method for url encoding
    encode : function(string){
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
        for(var n = 0; n < string.length; n++){
            var c = string.charCodeAt(n);
            if(c < 128){
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)){
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else{
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    },
		// public method for url decoding
    decode : function(utftext){
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if(c < 128){
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)){
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else{
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
};
//========== FireBug console ======================================================
function cout(theString){
	if(window.console) {
		window.console.log(theString);
	}
}
function cdout(theObject){
	if(window.console) {
		window.console.dir(theObject);
	}
}
//============== debug ============================================================
function dout(theString){
	debugOut(theString);
}
function debugOut(theString){
	tmpString = new String(theString);
	if($("debug_div")){
		new Insertion.Bottom($("debug_div"), tmpString + '<br>');
		$("debug_div").scrollTop = $("debug_div").scrollHeight;
	}
	if($("debug_text")){
		$("debug_text").value += tmpString.replace(/<br>/gi, "\n") + "\n";
		$("debug_text").scrollTop = $("debug_text").scrollHeight;
	}
}
function debugClear(){
	if($("debug_div") && $("debug_text")){
		Element.update($("debug_div"), '');
		$("debug_text").value = "";
	}
}
function debugCreate(){
	var theDivElement = $('debug_div');
	if (!theDivElement) {
		theDivElement = document.createElement('div');
		theDivElement.id = 'debug_div';
		theDivElement.style.position = 'absolute';
		theDivElement.style.right = '5px';
		theDivElement.style.top = '5px';
		theDivElement.style.width = '200px';
		theDivElement.style.height = '750px';
		theDivElement.style.overflow = 'auto';
		theDivElement.style.backgroundColor = '#f0f0f0';
		theDivElement.style.border = '1px solid gray';
		theDivElement.style.textAlign = 'left';
		theDivElement.style.fontFamily = 'ariel';
		theDivElement.style.fontSize = '9pt';
		theDivElement.style.padding = '5px';
		theDivElement.style.zIndex = 777;
		document.body.appendChild(theDivElement);
	}
	var theTextElement = $('debug_text');
	if (!theTextElement) {
		theTextElement = document.createElement('textarea');
		theTextElement.id = 'debug_text';
		theTextElement.style.position = 'absolute';
		theTextElement.style.left = '5px';
		theTextElement.style.top = '5px';
		theTextElement.style.width = '200px';
		theTextElement.style.height = '750px';
		theTextElement.style.overflow = 'auto';
		theTextElement.style.backgroundColor = '#f0f0f0';
		theTextElement.style.border = '1px solid gray';
		theTextElement.style.textAlign = 'left';
		theTextElement.style.fontFamily = 'ariel';
		theTextElement.style.fontSize = '10pt';
		theTextElement.style.padding = '5px';
		theTextElement.style.zIndex = 777;
		document.body.appendChild(theTextElement);
	}
	var theDebugClearElement = $('debug_clear');
	if (!theDebugClearElement) {
		theDebugClearElement = document.createElement('div');
		theDebugClearElement.id = 'debug_clear';
		theDebugClearElement.style.position = 'absolute';
		theDebugClearElement.style.right = '4px';
		theDebugClearElement.style.top = '13px';
		theDebugClearElement.style.zIndex = 779;
		document.body.appendChild(theDebugClearElement);
	}
	var theDebugClearBkgndElement = $('debug_clear_background');
	if (!theDebugClearBkgndElement) {
		theDebugClearBkgndElement = document.createElement('div');
		theDebugClearBkgndElement.id = 'debug_clear_background';
		theDebugClearBkgndElement.className='div_control';
		theDebugClearBkgndElement.style.zIndex = 778;
		Element.update(theDebugClearBkgndElement, 'Clear');
		theDebugClearElement.appendChild(theDebugClearBkgndElement);
	}
	Event.observe('debug_clear_background',
				  'click',
				  debugClear);
	Event.observe('debug_clear_background',
				  'mouseover',
				  debugClearEventRollOn);
	Event.observe('debug_clear_background',
				  'mouseout',
				  debugClearEventRollOff);
	debugClear();
}
function debugClearEventRollOn(){
	$('debug_clear_background').style.cursor = 'pointer';
}
function debugClearEventRollOff(){
	$('debug_clear_background').style.cursor = 'default';
}
function debugToggle(){
	if($('debug_toggle').innerHTML == 'TEXT'){
		theDebugHTMLDiv.toggle();
		theDebugTextDiv.hide();
		Element.update($('debug_toggle'), 'HTML');
	}else{
		theDebugTextDiv.toggle();
		theDebugHTMLDiv.hide();
		Element.update($('debug_toggle'), 'TEXT');
	}
}
//=======shift(), unshift(), pop(), push() prototypes, if needed=========================
if(!Array.prototype.shift) { // if this method does not exist..

alert('defining shift');

	Array.prototype.shift = function(){
		firstElement = this[0];
		this.reverse();
		this.length = Math.max(this.length-1,0);
		this.reverse();
		return firstElement;
	};
}
if(!Array.prototype.unshift){ // if this method does not exist..

alert('defining unshift');

	Array.prototype.unshift = function(){
		this.reverse();
		for(var i=arguments.length-1;i>=0;i--){
			this[this.length]=arguments[i];
		}
		this.reverse();
		return this.length;
	};
}
// pop
// Removes the last element of an array and
// returns that element.
if(!Array.prototype.pop){ // if this method does not exist..

alert('defining pop');

	Array.prototype.pop = function(){
		var lastItem = this[this.length-1];
		this.length--;
		return lastItem;
	};
}
// push
// Adds an element to the end of an array and
// returns the new length of the array
if(!Array.prototype.push){ // if this method does not exist..

alert('defining push');

	Array.prototype.push = function(pushItem){
		this[this.length] = pushItem;
		return this.length;
	};
}
//============================================================

/* insert an element after a particular node */
function insertAfter(parent, node, referenceNode) {
	if(referenceNode.nextSibling){
		parent.insertBefore(node, referenceNode.nextSibling);
	}else{
		parent.appendChild(node);
	}
}

/* Array prototype, matches value AND type in array: returns bool */
Array.prototype.inArray = function (value) {
	var i;
	for (i=(this.length-1); i >=0; i--) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

/* Array prototype, matches value in array: returns the index or -1 if not found */
Array.prototype.inArrayAt = function (value) {
	var i;
	for (i=(this.length-1); i >=0; i--) {
		if (this[i] === value) {
			return(i);
		}
	}
	return(-1);
};

/* get, set, and delete cookies */
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ){
		return null;
	}
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ){
		end = document.cookie.length;
	}
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ){
		document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}
