// Some global vars
var gstrCourseAnchorId;
var gstrClassAnchorId;
var gstrPracAreaID;
var gstrDummyButtonID; 
var gstrLogOffButtonID;
var oTimer = null;

function openElearning(url){
	openSeminar(url);
	// When the eLearning window closes we need to reload the parent page to reflect new LMS status
	oTimer = setInterval("CheckChildWindow()", 1000);
}

function openSeminar(url){
	if (BrowserDetect.init() != 'Explorer'){
		// show E-Seminar only if IE is available
		myAshurst_Popup(getLanguagePhrase("strBrowserCompatibiltyTitle"),getLanguagePhrase("strBrowserCompatibiltyMessage"),getLanguagePhrase("strInterfaceOK"),"","");
	}else{
		// Use setTimeout to catch any popup blocker issues here rather than later.
		window.setTimeout("popWindow('" + url + "')", 150);
	}
}

function CheckChildWindow()
{
	if (objEseminarWindow.closed)
	{
		window.clearInterval(oTimer);
		document.location.href = window.location.protocol + "//" + window.location.host + window.location.pathname;
	}
}

function LMS_bookCoursePopup()
{
	myAshurst_clearPopupTable();

	// Display Popup with message	
	var strTitleText = getLanguagePhrase("strBookingSummaryTitle"); 
	var strMessageText = getLanguagePhrase("strBookingSummaryMessage");
	
	// Add the selected courses to the table in the pop-up
	var lobj =	Object();
	lobj =	document.getElementsByName("course");
	var checkCount = 0;
	for	(i = 0;	i <	lobj.length; i++)
	{
		if (lobj[i].checked == true)
		{	
			if (BrowserDetect.init() == 'Firefox'){
				LMS_addRowDOM('bookingsRec',lobj[i].parentNode.parentNode.childNodes[2].childNodes[0].innerHTML, lobj[i].parentNode.parentNode.childNodes[1].innerHTML);				
			}else{
				LMS_addRowDOM('bookingsRec',lobj[i].parentNode.parentNode.children.item(0).innerHTML, lobj[i].parentNode.parentNode.children.item(1).children.item(0).innerHTML);		
			} 
			checkCount+=1;
		}
	}
	
	if	(checkCount==0){
		LMS_confirmationPopup(getLanguagePhrase("strNoEventsSelectedTitle"),getLanguagePhrase("strNoEventsSelectedMessage"),0)
	}else{
		myAshurst_Popup(strTitleText,strMessageText,getLanguagePhrase("strInterfaceCancel"),getLanguagePhrase("strInterfaceConfirm"),"LMS_bookSelectedCourse();");
	}
}

function LMS_bookSingleCoursePopup()
{
	myAshurst_clearPopupTable();

	// Display Popup with message	
	var strTitleText = getLanguagePhrase("strBookingSummaryTitle");
	var strMessageText = getLanguagePhrase("strBookingSummaryMessage");
	
	var lobjTitle = Object();
	lobjTitle = document.getElementById("courseTitle");

	var lobjDate = document.getElementById("courseStartDate");
	var lstrDateInner;
	if (lobjDate != null)
	{
		lstrDateInner = lobjDate.innerHTML;
	}else{
		lstrDateInner = "";
	}
	// Add course to the table in the pop-up
	LMS_addRowDOM('bookingsRec',lstrDateInner, lobjTitle.innerHTML);
	
	// Show popup
	myAshurst_Popup(strTitleText,strMessageText,getLanguagePhrase("strInterfaceCancel"),getLanguagePhrase("strInterfaceConfirm"),"LMS_bookSingleCourse();");
}

function LMS_cancelCoursePopup(vstrCourseId)
{
	myAshurst_clearPopupTable();

	// Display Popup with message	
	var strTitleText = getLanguagePhrase("strCancellationTitle");
	var strMessageText = getLanguagePhrase("strCancellationEventMessage");
	myAshurst_Popup(strTitleText,strMessageText,getLanguagePhrase("strInterfaceNo"),getLanguagePhrase("strInterfaceYes"),"LMS_cancelSelectedCourse();");

	// Add the course details of the course they are about to cancel
	gstrCourseAnchorId = vstrCourseId;
	var lobj =	Object();
	lobj = document.getElementById(gstrCourseAnchorId);
	LMS_addRowDOM('bookingsRec',lobj.parentElement.parentElement.children.item(0).innerHTML,lobj.parentElement.parentElement.children.item(1).children.item(0).innerHTML);
}

function LMS_cancelClassPopup(vstrClassId)
{
	myAshurst_clearPopupTable();

	// Display Popup with message	
	var strTitleText = getLanguagePhrase("strCancellationTitle");
	var strMessageText = getLanguagePhrase("strCancellationESeminarMessage");
	myAshurst_Popup(strTitleText,strMessageText,getLanguagePhrase("strInterfaceNo"),getLanguagePhrase("strInterfaceYes"),"LMS_cancelSelectedClass();");

	// Add the class details to cancel
	gstrClassAnchorId = vstrClassId;
	var lobj =	Object();
	lobj = document.getElementById(gstrClassAnchorId);

	var lobjDate =	Object();
	if (lobj.parentElement.parentElement.children.item(1).children.item(0))
	{
		lobjDate = lobj.parentElement.parentElement.children.item(1).children.item(0).innerHTML;
	}else{
		lobjDate = lobj.parentElement.parentElement.children.item(2).innerHTML; 
	}
	LMS_addRowDOM('bookingsRec',lobj.parentElement.parentElement.children.item(0).innerHTML,lobjDate);
}

function LMS_cancelSingleCoursePopup(vstrCourseId)
{
	myAshurst_clearPopupTable();

	// Display Popup with message	
	var strTitleText = getLanguagePhrase("strCancellationTitle");
	var strMessageText = getLanguagePhrase("strCancellationEventMessage");

	var lobjTitle = Object();
	lobjTitle = document.getElementById("courseTitle");
	var lobjDate = Object();
	lobjDate = document.getElementById("courseStartDate");

	// Add course to the table in the pop-up
	LMS_addRowDOM('bookingsRec',lobjDate.innerHTML, lobjTitle.innerHTML);
	
	// Show popup
	myAshurst_Popup(strTitleText,strMessageText,getLanguagePhrase("strInterfaceCancel"),getLanguagePhrase("strInterfaceConfirm"),"LMS_cancelSingleCourse();");

}

function LMS_confirmationPopup(strTitleText,strMessageText,intTimeDelaySecs)
{
    intTimeDelaySecs = intTimeDelaySecs * 1000;

    // A delay isn't required but if needed (e.g. to allow page elements to load) a delay can be added here
    var t=setTimeout("LMS_fireDelayedPopup('" + strTitleText + "','" + strMessageText + "')",intTimeDelaySecs);
}

function LMS_fireDelayedPopup(strTitleText,strMessageText)
{
	myAshurst_clearPopupTable();
	
	// Display Popup with message	
	myAshurst_Popup(strTitleText,strMessageText,getLanguagePhrase("strInterfaceOK"),"","");
}


////////////////////////// ANALYTICS EVENTS /////////////////////////////////
function LMS_bookSelectedCourse() 
{
	document.body.style.cursor = 'wait';
	var lobj = Object();
	var lstr = String("");
	lobj = document.getElementsByName("course");
	for (i = 0; i < lobj.length; i++)
	{
		if (lobj[i].checked)
		{
			lstr = lstr + '|' + lobj[i].id;
		}
	}
	__doPostBack(gstrDummyButtonID,lstr);
	AnalyticsTrack("/WhatsOn/BookCourses");
}

function LMS_bookSingleCourse()
{
	document.body.style.cursor = 'wait';
	// Just pass book as command argument rest is on the calling page
	__doPostBack(gstrDummyButtonID,"Book");
	AnalyticsTrack("/WhatsOn/BookCourse");
}

function LMS_cancelSelectedCourse() 
{
	document.body.style.cursor = 'wait';
	__doPostBack(gstrDummyButtonID,gstrCourseAnchorId+'CancelCourse');
	AnalyticsTrack("/Events/Cancel");
}

function LMS_cancelSelectedClass() 
{
	document.body.style.cursor = 'wait';
	__doPostBack(gstrDummyButtonID,gstrClassAnchorId+'CancelClass');
	AnalyticsTrack("/WhatsOn/Cancel");
}

function LMS_cancelSingleCourse()
{
	document.body.style.cursor = 'wait';
	// Just pass book as command argument rest is on the calling page
	__doPostBack(gstrDummyButtonID,"Cancel");
	AnalyticsTrack("/Events/Cancel");
}
////////////////////////// ANALYTICS EVENTS /////////////////////////////////


////////////////////////// HELPER FUNCTIONS /////////////////////////////////



function LMS_addRowDOM (tableID) 
{ 
	var table =  document.getElementById(tableID);
	if (arguments.length > 1) 
	{
		var row = table.insertRow(table.rows.length);
		row.className = "spsma-eventgridtablerow";
		if (document.all)
		{
			for (var i = 1; i < arguments.length; i++) 
			{
				var cell = row.insertCell(i - 1);
				cell.innerHTML = arguments[i];
				if (i==1) cell.style.whiteSpace = "nowrap";
			}
		}
		else if (document.getElementById)
		{ 
			for (var i = arguments.length - 1; i >= 1; i--) 
			{		
				var cell = row.insertCell(arguments.length - 1 - i);
				if (i==2) cell.style.whiteSpace = "nowrap";
				cell.appendChild(document.createTextNode(arguments[i]));
			}
		}
	}
}


//clean whitespace on the whole document onload if this is Firefox, as that inteferes with DOM
var notWhitespace = /\S/;
function cleanWhitespace(node) {
	for (var x = 0; x < node.childNodes.length; x++) {
		var childNode = node.childNodes[x]
		if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
			// that is, if it's a whitespace text node
			node.removeChild(node.childNodes[x])
			x--
			}
		if (childNode.nodeType == 1) {
			// elements can have text child nodes of their own
			cleanWhitespace(childNode)
			}
	}
}

////////////////////////// HELPER FUNCTIONS /////////////////////////////////
