/****************************

General utility functions

*****************************/

var RedirectUrl;

function checkOverlap(obj1, obj2, al) {
	var f_to_check = document.getElementById(obj1);
	var f = document.getElementById(obj2);
	if (f_to_check && f) {
		var top_to_check = parseInt(f_to_check.style.top) + 1;
		var left_to_check = parseInt(f_to_check.style.left) + 1;
		var bottom_to_check = parseInt(f_to_check.style.top) + parseInt(f_to_check.offsetHeight) - 1;
		var right_to_check = parseInt(f_to_check.style.left) + parseInt(f_to_check.offsetWidth) - 1;

		var top = parseInt(f.style.top);
		var left = parseInt(f.style.left);
		var bottom = parseInt(f.style.top) + parseInt(f.offsetHeight);
		var right = parseInt(f.style.left) + parseInt(f.offsetWidth);

		if  ( (top_to_check >= top && top_to_check <= bottom) || (bottom_to_check >= top && bottom_to_check <= bottom) ) {
			if  ( (left_to_check >= left && left_to_check <= right) || (right_to_check >= left && right_to_check <= right) ) {
				return true;
			}
		}

		//now check the other way around (if we're bigger)
		if  ( (top >= top_to_check && top <= bottom_to_check) || (bottom >= top_to_check && bottom <= bottom_to_check) ) {
			if  ( (left >= left_to_check && left <= right_to_check) || (right >= left_to_check && right <= right_to_check) ) {
				return true;
			}
		}

	}

	return false;
}

function placeElement(name, x, y) {
	var elem = document.getElementById(name);
	if (elem) {
		elem.style.left = x + 'px';
		elem.style.top = y + 'px';
	}
}

function hideElement(name) {
	var elem = document.getElementById(name);
	if (elem) {
		elem.style.visibility = 'hidden';
		elem.style.display = 'none';
	}
}

function showElement(name) {
	var elem = document.getElementById(name);
	if (elem) {
		elem.style.visibility = 'visible';
		elem.style.display = 'block';
	}
}

// format currency in dollars
function formatCurrency(num) {
	// Get rid of dollar signs, etc.
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) {num = "0";}

	// Negative or positive?
	var sign = (num == (num = Math.abs(num)));

	// Round off dollars/cents
	var curr = roundCurrency(num);
	
	// Get dollars/cents
	var dollars = Math.floor(curr);
	var cents = parseInt(Math.round(curr * 100) % 100);
	if (cents < 10) {cents = "0" + cents;}
	dollars = dollars.toString();
	
	// Add commas
	for (var i = 0; i < Math.floor((dollars.length-(1+i))/3); i++) {		
		dollars = dollars.substring(0,dollars.length-(4*i+3)) + ',' + dollars.substring(dollars.length-(4*i+3));
	}

	return (((sign)?'':'-') + '$' + dollars + '.' + cents);
}

// Round off to dollars and cents (2 places)
function roundCurrency(num) {
	if(isNaN(num)) {num = "0";}
	num = Math.floor(num*100+0.50000000001);
	var cents = num%100;
	num = Math.floor(num/100).toString();
	if (cents < 10) {cents = "0" + cents;}
	return parseFloat(num + '.' + cents);
}

function URLencode(sStr) {
    return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

function urlEncode(sStr) {
    return URLencode(sStr);
}

function array2d(x, y) {
	var a = new Array(x);
	for (var i = 0; i < x; i++) {
		a[i] = new Array(y);
	}

	return a;
}

//function $(id) {
//	return document.getElementById(id);
//}

function $n(name) {
	return document.getElementsByName(name);
}

// This is dumb, but it seems to be the only way to make IE redirect properly
function redirectTo(str) {
	RedirectUrl	= str;
	setTimeout( "window.location.href = RedirectUrl", 0 );
}

function setClass(item, cls) {
	item.className = cls;
}

function roundNumber(rnum, rlength) {
	return Math.round(rnum * Math.pow(10,rlength)) / Math.pow(10,rlength);
}

function roundNumberString(rnum) {
	rnum += '';
	rnum = rnum.replace(/\.(\d\d)\d+/gi, ".$1");
	return rnum;
}

function findAbsolutePosition(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function swapTutVid(theVid) {
	document.getElementById('tutVid').innerHTML = '<object width="618" height="355"><param name="movie" value="http://www.youtube.com/v/' + theVid + '?rel=0&color1=0x5d1f09&color2=0xb6b6b6&border=1&fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/' + theVid +'?rel=0&color1=0x5d1f09&color2=0xb6b6b6&border=1&fs=1" type="application/x-shockwave-flash" width="618" height="355" allowfullscreen="true"></embed></object> ';
}

function ucfirst(str) {
	var f = str.charAt(0).toUpperCase();
	return f + str.substr(1);
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function documentHeight() {
	var body = document.body,
		html = document.documentElement;
	
	var height = Math.max( body.scrollHeight, body.offsetHeight, 
						   html.clientHeight, html.scrollHeight, html.offsetHeight );
	return height;
}
