var DOM = (document.getElementById) ? 1 : 0;
var NS4 = (document.layers) ? 1 : 0;
var Opera5 = (navigator.userAgent.indexOf("Opera 5") > -1 || navigator.userAgent.indexOf("Opera/5") > -1) ? 1 : 0;
var ns6=document.getElementById && !document.all;
var qsParm = new Array();

/**
 *	@short forward browser to this location
 *	@param string address to go to
 */
function go( xx ) {
	document.location.href = xx;
}

/**
 *	@short cross browser, ruturns div id of element requested
 *	@param string div id name
 *	@return div id
 */
function getobj( x ) {
	return (document.all) ? document.all[x] : document.getElementById? document.getElementById(x) : "";
}

/**
 *	@short reverse logic of if in debug mode or not
 */
function switchDebug() {
	debug = ( debug==false ) ? true : false;
}

/**
 *	@short tests if value is empty
 *	@param mixed
 *	@return bool
 */
function not_null( xx ) {
	try {
		if ( xx == '' || xx == null || (typeof xx)=='undefined') {
			return false;
		}
	} catch (e) {
		handleError( 'not_null', e );
	}
	return true;
}

/**
 *	Position Helpers
 */
/**
 *	@short attempts to place one div on top of another
 *	@param div id
 *	@param div id
 */
function overlapDiv( top_div, bottom_div ) {
	try {
		if (!not_null( top_div ) || !not_null( bottom_div )) {
			return;
		}
		
		setTop( top_div, findPosTop( bottom_div ) );
		setLeft( top_div, findPosLeft( bottom_div ) );
	} catch (e) {
		handleError( 'overlapDiv', e );
	}
}

/**
 *	@short set the position of the Y coordinate of a div
 *	@param div id
 *	@param int Y coordinate
 */
function setLeft( object, x ) {
	try {
		var layer = object.id;
		if (DOM && !Opera5) {
			document.getElementById(layer).style.left = x + "px";
		} else if (Opera5) {
			document.getElementById(layer).style.left = x;
		} else if (NS4) {
			document.layers[layer].left = x;
		} else {
			document.all[layer].style.pixelLeft = x;
		}
	} catch (e) {
		handleError( 'setLeft', e );
	}
}

/**
 *	@short set the position of the Y coordinate of a div
 *	@param div id
 *	@param int Y coordinate
 */
function setTop( object, y ) {
	try {
		var layer = object.id;
		if (DOM && !Opera5) {
			document.getElementById(layer).style.top = y + "px";
		} else if (Opera5) {
			document.getElementById(layer).style.top = y;
		} else if (NS4) {
			document.layers[layer].top = y;
		} else {
			document.all[layer].style.pixelTop = y;
		}
	} catch (e) {
		handleError( 'setTop', e );
	}
}

/**
 *	@short find the Y coordinate of the div object
 *	@param div id
 *	@return int
 */
function findPosTop( object ) {
	try {
		var value = 0;
		if (DOM) {	// Mozilla, Konqueror >= 2.2, Opera >= 5, IE
			value = object.offsetTop;
			while (object.tagName != "BODY" && object.offsetParent) {
				object = object.offsetParent;
				value += object.offsetTop;
			}
		} else if (NS4) {
			value = object.pageY;
		} else {	// IE4 IS SIMPLY A BASTARD !!!
			value = object.offsetTop;
			while (object.tagName != "BODY") {
				object = object.offsetParent;
				value += object.offsetTop;
			}
		}
	} catch (e) {
		handleError( 'findPosTop', e );
	}

	return value;
}

/**
 *	@short find the X coordinate of the div object
 *	@param div id
 *	@return int
 */
function findPosLeft( object ) {
	try {
		var value = 0;
		if (DOM) {	// Mozilla, Konqueror >= 2.2, Opera >= 5, IE
			value = object.offsetLeft;
			while (object.tagName != "BODY" && object.offsetParent) {
				object = object.offsetParent;
				value += object.offsetLeft;
			}
		} else if (NS4) {
			value = object.pageX;
		} else {
			value = object.offsetLeft;
			while (object.tagName != "BODY") {
				object = object.offsetParent;
				value += object.offsetLeft;
			}
		}
	} catch (e) {
		handleError( 'findPosLeft', e );
	}
	return value;
}

/**
 *	@short find the position of the mouse cursor on the page.
 *	@param event
 *	@return array
 */
function findCursorPos( e ) {
	try {
		var cur = new Array();
		var pos = new Array();

		if ( !e )	e = window.event;
		
		if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
			pos['X'] = document.body.scrollLeft;
			pos['Y'] = document.body.scrollTop;
		} else {
			pos['X'] = document.documentElement.scrollLeft;
			pos['Y'] = document.documentElement.scrollTop;
		}

		if (e.pageX || e.pageY) {
			cur['X'] = e.pageX;
			cur['Y'] = e.pageY;
		} else if (e.clientX || e.clientY) {
			cur['X'] = e.clientX+pos['X'];
			cur['Y'] = e.clientY+pos['Y'];
		} else if (e.x || e.y) {
			cur['X'] = e.x+pos['X'];
			cur['Y'] = e.y+pos['Y'];
		}

	} catch (err) {
		handleError( 'findCursorPos', err );
	}

	return cur;
}

function myonlygetnum(x){
	var t = "";
	for (i=0; i<x.length; i++){
		if (x.charAt(i)=="1" || x.charAt(i)=="2" || x.charAt(i)=="3" || x.charAt(i)=="4" || x.charAt(i)=="5" || x.charAt(i)=="6" || x.charAt(i)=="7" || x.charAt(i)=="8" || x.charAt(i)=="9" || x.charAt(i)=="0"){
			t = t+""+x.charAt(i);
		}
	}
	return t;
}

/**
 *	@short breaks the URL query string into an array for js to handle.
 */
function qs() {
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
}

/**
 *	@param string needle
 *	@param array haystack
 *	@ret bool
 */
function inArray( needle, haystack ) {
	try {
		for( i=0, n=haystack.length; i<n; i++ ) {
			if (haystack[ i ]==needle) {
				return true;
			}
		}
		
	} catch (e) {
		handleError( 'inArray', e );
	}
	return false;
}

/**
 *	@short a single function to facilitate notifying the user of errors.
 *	@param string function name of error origin
 */
function handleError( func, e ) {
	if ( DEBUG ) {
		//alert( func+'() ['+e.name+'] '+e.message );
		var thediv = getobj( 'debug_window' );
		if (not_null( thediv )) {
			thediv.innerHTML = func+'() ['+e.name+'] '+e.message+'<br />';
		}
	}
}

/**
 *	@short change the background and text values for the given div
 *	@param div id
 *	@param string background color
 *	@param string text color
 */
function thisBG( xx, bg, txt ) {
	try {
		xx.style.backgroundColor = bg;
		xx.style.color = txt;
	} catch (e) {
		handleError( 'alternatingRows', e );
	}
}

/**
 *	@short empty the select field
 *	@param div id
 */
function resetSelect( xx ) {
	try {
		if ( !not_null( xx ) ) { return; }

		for( var i=xx.options.length; i>0; i-- ) {
			xx.options[ i ] = null;
		}
	} catch (e) {
		handleError( 'resetSelect', e );
	}
}

/**
 *	@short get the value of a select field
 *	@param div id
 */
function getSelectValue( xx ) {
	try {
		if ( !not_null( xx ) )	return false;

		return xx.options[ xx.selectedIndex ].value;
	} catch (e) {
		handleError( 'getSelectValue', e );
	}
	return false;
}

/**
 *	@short test if value is integer
 *	@param mixed
 *	@return bool
 */
function isInteger( xx ) {
	try {
		if (!not_null( xx )) { return false; }
		var i, n, c;
		for( i=0, n=xx.length; i<n; i++ ) {
			c = xx.charAt( i );
			if ( ((c < '0') || (c > '9')) ) return false;
		}
		return true;
	} catch (e) {
		handleError( 'isInteger', e );
	}
	return false;
}

/**
 *	@short returns the value of the radio button group
 *	@param string the name of the form
 *	@param string the name of the radio group
 *	@return string value of the checked radio button
 */
function radioValue( formName, id ) {
	try {
		if (!not_null( formName ) || !not_null( id ))	return false;
		
		var theform = getobj( formName );
		var value=0;

		if (theform.elements) {
			var n = theform.elements.length;
			for( var i=0; i<n; i++ ) {
				var d = theform.elements[ i ];
	
				if ((d.type == 'radio') && (d.name == id) && d.checked) {
					value = d.value;
				}
			}
		}

		return value;
	} catch (e) {
		handleError( 'radioValue', e );
	}
	return false;
}


/**
 *	@short return the div id of the element on the form|not useful with duplicate named elements (ie radio or checkboxes)
 *	@param string form name
 *	@param element name
 *	@return divID
 */
function getID( formName, id ) {
	try {
		if (!not_null( formName ) || !not_null( id ))	return false;

		var theform = getobj( formName );
		var value=0;

		if (theform.elements) {
			var n = theform.elements.length;
			for( var i=0; i<n; i++ ) {
				var d = theform.elements[ i ];

				if (d.name == id) {
					if ( not_null( getobj( d.id ) ) ) {
						return getobj( d.id );
					}

					
					d.id = d.name;
					return getobj( d.id );
				}
			}
		}

		return false;
	} catch (e) {
		handleError( 'getID', e );
	}
	return false;
}


function asc( a, b ) {
	return a-b;
}

function desc( a, b ) {
	return b-a;
}

/**
 *	@short this is kind of reverse logic but: supply div id and bool stating if or if-not to hide the div
 *	@param divID
 *	@param bool
 *	@return bool
 */
var openForms = new Array();
function hideDiv( xx, hideit ) {
	try {
		if (!not_null( xx ))	return false;
	
		var thediv = getobj( xx );
		if (hideit == true) {
			thediv.style.display = 'none';
			openForms[ xx ] = false;
		} else {
			thediv.style.display = 'block';
			openForms[ xx ] = true;
		}
	
		return true;
	} catch (e) {
		handleError( 'hideDiv', e );
	}

	return false;
}

function toggleRow(id){
	var obj = document.getElementById(id);
	if (obj.style.visibility=="hidden"){
		showobjbyid(id);
	} else {
		hideobjbyid(id);
	}
}

function hideobjbyid(id){
	document.getElementById(id).style.visibility = 'hidden';
	document.getElementById(id).style.display = 'none';
}

function showobjbyid(id){
	document.getElementById(id).style.visibility = 'visible';
	document.getElementById(id).style.display = 'block';
}

function formatAmount(num){
	num = parseFloat(num);
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	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;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

function setSelectOptions(sid, ops){
	var sobj = document.getElementById(sid);
	if (sobj.type.substring(0, 6)=="select"){
		sobj.options.length = 0;
		for (x in ops){
			sobj.options[sobj.options.length] = new Option(ops[x], x);
		}
	} 
}


function confirmAndGo(msg, gurl){
	if (confirm(msg)) go(gurl);
}

var DEBUG = false;
