var CurrentTabID = "tabCourseSearch";
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
function encodeString(strIn) {
	var str = escape(strIn);
	str = str.replace(/\//g,"%2F");
	str = str.replace(/\?/g,"%3F");
	str = str.replace(/=/g,"%3D");
	str = str.replace(/&/g,"%26");
	str = str.replace(/@/g,"%40");
	return str;
} 
function fnSwitchRightTab(TabID){
	var oActiveTab = document.getElementById(TabID);
	var oActiveTabContent = document.getElementById(TabID + "Content");
	if (CurrentTabID.length > 0 ){
		document.getElementById(CurrentTabID).className = "inactive";
		document.getElementById(CurrentTabID +"Content").style.display = "none";
	}
	oActiveTab.className = "active";
	oActiveTabContent.style.display = "block";
	CurrentTabID = oActiveTab.id;
	return false;
}
function fnToggleDisplay(ToggleElementId){
	var oToggleElement = document.getElementById(ToggleElementId);
	if (oToggleElement.style.display == "block") {
		oToggleElement.style.display = "none";
		if (arguments.length == 4) arguments[1].innerHTML = arguments[2];
	} else {
		oToggleElement.style.display = "block"
		if (arguments.length == 4) arguments[1].innerHTML = arguments[3];
	}
	return false;
}
function fnSetAdvancedCourseSearchDisplay(el){
	var oAdvancedSearchContainer = document.getElementById("advancedSearchContainer");
	fnToggleDisplay(oAdvancedSearchContainer.id)
	if (oAdvancedSearchContainer.style.display == "block") {
		setCookie("advancedCourseSearch", "true", 15);
		el.innerHTML = "Basic Search";
	} else {
		setCookie("advancedCourseSearch", "false", 15);
		el.innerHTML = "Advanced Search Options";
	}
	return false;
}
function fnCheckAdvancedCourseSearchDisplayCookie(){
	if ((getCookie("advancedCourseSearch") == "true") || (document.location.href.toLowerCase().indexOf("courses.aspx")) > 0){
		document.getElementById("AdvancedSearchOptions").innerHTML = "Basic Search";
		document.getElementById("advancedSearchContainer").style.display = "block";
	}
}
function getCookie(NameOfCookie) {
	if (document.cookie.length > 0){ 
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if (begin != -1) {
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); 
		}
	}
	return null;
}
function setCookie(cookieName, value, expireDays){
	var expiry = new Date ();
	expiry.setTime(expiry.getTime() + (expireDays * 24 * 3600 * 1000));
	document.cookie = cookieName +"="+ escape(value) + "; path=/; "+ ((expireDays == null) ? "" : "; expires="+ expiry.toGMTString());
}
function submitCourseSearch(oForm, BaseURL, IsPreviousCourseSearch){
	var i = 0;
	var j = 0;
	var QueryString;
	//course type
	for (i=0; i < oForm.ctOption.length; i++){
		if (oForm.ctOption[i].checked == true) j += parseInt(oForm.ctOption[i].value);
	}
	oForm.ct.value = j;
	if (IsPreviousCourseSearch == true) {
		//term
		j="";
		for (i=0; i < oForm.tOption.length; i++){
			if (oForm.tOption[i].checked == true) j += oForm.tOption[i].value +",";
		}
		oForm.t.value = j;
		QueryString = "&pc=1&t="+ oForm.t.value +"&ct="+ oForm.ct.value;
	} else {
		//course status
		j=0;
		for (i=0; i < oForm.csOption.length; i++){
			if (oForm.csOption[i].checked == true) j += parseInt(oForm.csOption[i].value);
		}
		oForm.cs.value = j;
		//delivery method
		j=0;
		for (i=0; i < oForm.dmOption.length; i++){
			if (oForm.dmOption[i].checked == true) j += parseInt(oForm.dmOption[i].value);
		}
		oForm.dm.value = j;
		QueryString = "&cs="+ oForm.cs.value +"&ct="+ oForm.ct.value +"&dm="+ oForm.dm.value +"&dtb="+ oForm.dtb.value +"&dte="+ oForm.dte.value;
	}
	if (oForm.q.value == "search for a course") oForm.q.value = "";
	document.location.href = BaseURL +"?q="+ encodeString(oForm.q.value) + QueryString;;
	return false;
}
function submitProgramSearch(oForm, BaseURL){
	var i = 0;
	var j = 0;
	var QueryString;
	//program type
	for (i=0; i < oForm.ptOption.length; i++){
		if (oForm.ptOption[i].checked == true) j += parseInt(oForm.ptOption[i].value);
	}
	oForm.pt.value = j;
	//delivery method
	/*
	j=0;
	for (i=0; i < oForm.pdmOption.length; i++){
		if (oForm.pdmOption[i].checked == true) j += parseInt(oForm.pdmOption[i].value);
	}
	oForm.dm.value = j;
	QueryString = "&pt="+ oForm.pt.value +"&dm="+ oForm.dm.value;
	*/
	QueryString = "&pt="+ oForm.pt.value;
	if (oForm.q.value == "search for a program") oForm.q.value = "";
	document.location.href = BaseURL +"?q="+ encodeString(oForm.q.value) + QueryString;;
	return false;
}
function fnSubmitEmailThis(oForm){
	if (oForm.SenderName.value.length == 0) {
		alert("Please enter your name before proceeding.");
		oForm.SenderName.focus();
		return false;
	}
	if (oForm.SenderEmail.value.length == 0) {
		alert("Please enter your email before proceeding.");
		oForm.SenderEmail.focus();
		return false;
	}
	if (oForm.RecipientEmail1.value.length == 0) {
		alert("Please enter an email address in the \"To Email\" field before proceeding.");
		oForm.RecipientEmail1.focus();
		return false;
	}
	oForm.Send.value = "Sending...";
	oForm.Send.disabled = "true";			
	var RequestURL = "./sendEmailThis.aspx";
	if (arguments[1].length > 0) RequestURL = arguments[1]
	var PostData = "SenderName="+ oForm.SenderName.value;
	PostData += "&SenderEmail="+ oForm.SenderEmail.value;
	PostData += "&SenderCC="+ oForm.SenderCC.value;
	PostData += "&RecipientEmail1="+ oForm.RecipientEmail1.value;
	PostData += "&RecipientEmail2="+ oForm.RecipientEmail2.value;
	PostData += "&RecipientEmail3="+ oForm.RecipientEmail3.value;
	PostData += "&Subject="+ oForm.Subject.value;
	PostData += "&Message="+ oForm.Message.value;
	PostData += "&Body="+ oForm.Body.value;
	if (xmlhttp) {
		PostData += "&XmlHttp=1"
		xmlhttp.open("POST", RequestURL, true); 
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
				var Response;
				//document.getElementById("XmlHttpResponse").innerHTML = xmlhttp.responseText;
				if (xmlhttp.responseText == "sent"){
					//message sent successfully
					Response = "<span style='color:green; font-weight:bold;'>Success:</span> Your email to the following address(es) has been sent:<ul><li>"+ oForm.RecipientEmail1.value +"</li>"
					if (oForm.RecipientEmail2.value.length > 0) Response += "<li>"+ oForm.RecipientEmail2.value +"</li>" 
					if (oForm.RecipientEmail3.value.length > 0) Response += "<li>"+ oForm.RecipientEmail3.value +"</li>" 
					Response += "</ul><a href='#' onclick='fnResetEmailThis();'>Create &amp; send another email</a>" 
					//hide form
					oForm.style.display = "none";
				} else {
					//message not sent successfully
					Response = "<span style='color:red;'><b>Error:</b> Your email was not sent due to the following reason:<ul><li>"+ xmlhttp.responseText +"</li></ul></span>"
					fnResetEmailThis();
				}
				document.getElementById("EmailThisSent").innerHTML =  Response;
				document.getElementById("EmailThisSent").style.display = "block";
			}
		}
		xmlhttp.send(PostData);
	} else {
		document.location.href = RequestURL +"?"+ PostData;
	}
	return false;
}
function fnInitEmailThis(){
	document.getElementById("EmailThisSubject").value = document.title;
	document.getElementById("EmailThisBody").value = document.title +"<br>"+ document.location.href
	var str = document.title +"<br />"+ document.location.href.substring(0,40);
	if (document.location.href.length > 40) str += "..."
	document.getElementById("EmailThisBodyDiv").innerHTML = str
}
function fnResetEmailThis(){
	document.getElementById("EmailThis").style.display = "block";
	document.getElementById("EmailThisSent").style.display = "none";
	document.getElementById("EmailThisSend").value = "Send the email";
	document.getElementById("EmailThisSend").disabled = "";
}