/// <reference path="jquery-1.6.2-vsdoc.js" />
/// <reference path="jquery-ui-1.8.14.custom.min.js" />
/// <reference path="jquery.hoverIntent.js" />

/////////////////////////////////////////
// Older Utility Methods - Will clean up going forward
////////////////////////////////////////


function showHideElement(elementId, show) {
	if (show)
		$("#" + elementId).show();
	else
		$("#" + elementId).hide();
}

function toggleElementDisplay(elementId) {
	if (document.getElementById(elementId)) {
		if (document.getElementById(elementId).style.display == "block")
			document.getElementById(elementId).style.display = "none";
		else
			document.getElementById(elementId).style.display = "block";
	}
}

function loadPopup(url) {
	openWindow(url);
}

function mapLink(url) {
	openWindow(url);
}

function hotelLink(url) {
	openWindow(url);
}

function openWindow(url) {
	window.open(url);
}

function focusFirstElement(formIndex) {
	try {
		if (document.forms[formIndex] && document.forms[formIndex].elements) {
			for (x = 0; x < document.forms[formIndex].elements.length; x++) {
				if (document.forms[formIndex].elements[x].type != "hidden") {
					document.forms[formIndex].elements[x].focus();
					return;
				}
			}
		}
	}
	catch (e) {
	}

}

function isValidEmailAddress(email) {
	AtPos = email.indexOf("@");
	StopPos = email.lastIndexOf(".");
	if (email == "") {
		return false;
	}

	if (AtPos == -1 || StopPos == -1) {
		return false;
	}

	if (StopPos < AtPos) {
		return false;
	}

	if (StopPos - AtPos == 1) {
		return false;
	}

	return true;
}


function submitSubscriptionRequest(submit) {
	document.getElementById('subscribeformerror').innerHtml = '';
	document.getElementById('subscribeformerror').style.display = '';
	if (!isValidEmailAddress(document.getElementById('emailaddress').value)) {
		document.getElementById('subscribeformerror').style.display = 'block';
		document.getElementById('subscribeformerror').innerHTML = 'Please enter a valid email address<br/>';
		return false;
	}
	if (submit) {
		document.getElementById('subscribeform').submit();
	}
	return true;
}


function getWindowWidth() {
	var myWidth = 0, myHeight = 0;
	if (typeof (window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return myWidth;
}

function getWindowHeight() {
	var myWidth = 0, myHeight = 0;
	if (typeof (window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return myHeight;
}


function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if (typeof (window.pageYOffset) == 'number') {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [scrOfX, scrOfY];
}

function switchBlock(prefix, max) {
	try {
		var blobcat = Math.floor((max) * Math.random());
		elements = document.getElementsByTagName("*");
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].id.toString().indexOf(prefix) == 0) {
				elements[i].style.display = "none";
			}
		}
		try {
			if (document.getElementById(prefix + blobcat))
				document.getElementById(prefix + blobcat).style.display = "block";
		} catch (e) { }
		try {
			if (document.getElementById(prefix))
				document.getElementById(prefix).style.display = "block";
		} catch (e) { }
	} catch (e) { }

}

function removeFromArray(array, item) {
	var tempArray = new Array();
	for (var x = 0; x < array.length; x++) {
		if (array[x] != item) {
			tempArray[tempArray.length] = item;
		}
	}
	return tempArray;
}

// prefer dm.courses.switchTabs[Ex](), this global version is only for back compat.

function switchTabs(activeTab) {
	for (i = 0; i < tabIds.length; i++) {
		var cssId = "#" + tabIds[i];
		if (i == activeTab)
			$(cssId).show();
		else
			$(cssId).hide();
	}
}

