var oldOnload = window.onload;
window.onload = init;
function init() {	
	if(oldOnload) {
		oldOnload();
	}
	bookingClassInfo();
	upgradeClassInfo();	
	checkForm();
}

function checkForm() {
	var i, j, theForms = document.forms;
	for(i=0,j=theForms.length; i<j; i++) {
		if(theForms[i].action.match(/.*earnfind\.jsp$/) || theForms[i].action.match(/.*burnfind\.jsp$/) ) {
			theForms[i].onsubmit = checkIt;
		}
	}
}

function checkIt() {
	var from = this.from;
	var to = this.to;
	from.className = '';
	to.className = '';
	if(from.value === '') {
		from.className = 'empty';
		from.focus();
		return false;
	}
	if(to.value === '') {
		to.className = 'empty';
		to.focus();
		return false;
	}
	return true;
}

function bookingClassInfo() {
	var i, j, infoBlock = document.getElementById('b');
	var theLinks = document.links;
	if(infoBlock !== null ) {
		infoBlock.className += ' infoblock';
		//infoBlock.innerHTML += '<a href="#" onclick="return showBookingClassInfo()">Close</a>';
	}
	for(i=0,j=theLinks.length; i<j; i++) {
		if(theLinks[i].href.match(/.*#b$/)) { // check if the href ends with #b
			theLinks[i].onclick = showBookingClassInfo;
		}
	}
}

function showBookingClassInfo() {
	var infoBlock = document.getElementById('b');
	if(infoBlock !== null ) {
		if(infoBlock.style.display === 'block') {
			infoBlock.style.display = 'none';
		} else {
			var y = getElementLeftPosition(this);
			var x = getElementTopPosition(this);
			infoBlock.style.left = y+'px';
			infoBlock.style.top = x+20+'px';
			infoBlock.style.display = 'block';
			//infoBlock.style.width = '450px';
			if(document.getElementById('u') != null && document.getElementById('u').style.display === 'block') {
				showUpgradeClassInfo()
			}
		}
	}
	if (this !== window) {
		this.blur();
	}
	return false; // stop the link from doing anything
}

function upgradeClassInfo() {
	var i, j, infoBlock = document.getElementById('u');
	var theLinks = document.links;
	if(infoBlock !== null ) {
		infoBlock.className += ' infoblock';
		infoBlock.innerHTML += '<a href="#" onclick="return showUpgradeClassInfo()">Close</a>';
	}
	for(i=0,j=theLinks.length; i<j; i++) {
		if(theLinks[i].href.match(/.*#u$/)) { // check if the href ends with #b
			theLinks[i].onclick = showUpgradeClassInfo;
		}
	}
}

function showUpgradeClassInfo() {
	var infoBlock = document.getElementById('u');
	if(infoBlock !== null ) {
		if(infoBlock.style.display === 'block') {
			infoBlock.style.display = 'none';
		} else {
			var y = getElementLeftPosition(this);
			var x = getElementTopPosition(this);
			infoBlock.style.left = y-161+'px';
			infoBlock.style.top = x+20+'px';
			infoBlock.style.display = 'block';
			if(document.getElementById('b') != null && document.getElementById('b').style.display === 'block') {
				showBookingClassInfo()
			}
		}
	}
	if (this !== window) {
		this.blur();
	}
	return false; // stop the link from doing anything
}

// http://www.highdots.com/forums/t681226-attribute-like-offsettop-but-showing-the-absolute-distance.html
/**
* returns the absolute left location of an element.
* param: e: element
* return: an integer representing the offset from left.
*/
function getElementLeftPosition(e){
	var x=0;
	while(e){
		x+=e.offsetLeft;//+e.clientLeft;
		e=e.offsetParent;
	}
	return x;
}

/**
* returns the absolute top location of an element.
* param: e: element
* return: an integer representing the offset from top.
*/
function getElementTopPosition(e){
	var y=0;
	while(e){
		y+=e.offsetTop;//+e.clientTop;
		e=e.offsetParent;
	}
	return y;
}
