/****************************

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, '%2C').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> ';
}