/*
	Modern Climate
	Shawn Heller
	June 1, 2009

	Updated October 23, 2009
	
	Repsonsible for managing the Sitecore side of the WebTrac/Sitecore Integration
*/


// VARIABLES
var rootURL = "https://webtrac.threeriversparks.org/wbwsc/webtrac.wsc/";
var timeoutURL = "http://www.threeriversparks.org/KeepAlive.aspx?url=" + escape(rootURL);


// ONLOAD

// Initialize session instance, and attempt to read cookie
session.init();
if(session.isValid) {
	setHeaderHousehold();
	//keepAliveResult = setInterval("keepAlive(true);", 10000);
}


// PUBLIC FUNCTIONS

/* set the user name in the header */
function setHeaderHousehold()
{
	var lastnameObject = findObj("webtrac_name");
	if(lastnameObject != null && isNotEmpty(session.name)) {
		// set the household name
		lastnameObject.innerHTML = formatLoginName(session.name);
		
		// change login to logout
		var loginObject = findObj("webtrac_login");
		if(loginObject != null) {
			loginObject.innerHTML = "Logout";
		}
	}
}


function onEventIntegrationClick(page, activity, section)
{
	forwardToWebtrac(page, activity, section, null);
}


function onPassIntegrationClick(page, pass)
{
	forwardToWebtrac(page, null, null, pass);
}


// this forward is different then all other webtrac forwards
// because it goes directly to a webtrac page regardless of logging in first
function onRegisterIntegrationClick(page)
{
	window.location = rootURL + page + ".html";
}


function onIntegrationClick(pageName)
{
	forwardToWebtrac(pageName, null, null, null);
}


// GolfTrac integration
// forwards direct to the page because GolfTrac uses guest login
function onGolfIntegration(courseNumber, coursePhone) {
	if(courseNumber == null) {courseNumber = 0;}

	// get the current time + 1 hour as the starting search time
	var xxhour = new Date().getHours() + 1;
	var xxmin = new Date().getMinutes();
	var xxampm = "A";

	// adjust for am/pm variance
	if(xxhour == 23) {xxhour = 0};
	if(xxhour >= 12) {
		xxampm = "P";
		xxhour = xxhour - 12;
	}
	window.location = rootURL + "wbgr0210.html?wbsi=si&xxmod=10&del=yes&xxcourse=" + courseNumber + "&xxnumslot=1&xxhour=" + xxhour + "&xxmin=" + xxmin + "&xxampm=" + xxampm + "&xxdisp=list&xxinq=yes&wbp=2&phone=" + coursePhone + "&ReferPage=" + window.location;
}


function forwardToWebtrac(page, activity, section, pass)
{
	try
	{
		if (isNotEmpty(page)) 
		{
			if (session.isValid)
			{
				// if the session is valid, send them over to webtrac
				if(isNotEmpty(activity) || isNotEmpty(pass))
				{
					// append a referring url if it is an event or pass
					window.location = appendReferringURL(formatWebTracURL(page, activity, section, pass));
				} else {
					// append the complete event listing
					window.location = appendReferringEventListing(formatWebTracURL(page, activity, section, pass));
				}
			} else {
				// send them to the login page and embed everything in the URL
				var loginPage = rootURL + "wb1000.html?" + PAGEID + "=" + page;
				if(isNotEmpty(activity))
				{
					loginPage += "&" + EVENTID + "=" + activity + "&" + SECTIONID + "=" + section;
					loginPage = appendReferringURL(loginPage);
				} else if(isNotEmpty(pass)) {
					loginPage += "&" + PASSID + "=" + pass;
					loginPage = appendReferringURL(loginPage);
				} else {
					loginPage = appendReferringEventListing(loginPage);
				}
				window.location = loginPage;
			}
		} else {
			// if no page id exists
			window.location = appendReferringURL(rootURL + "wb1000.html?");
		}
	}
	catch(err)
	{
		// there is a severe problem if the user gets this far
		window.location = appendReferringURL(rootURL + "wb1000.html?");
	}
}

function appendReferringURL(url) {
	if(document.referrer != null && (window.location.search.indexOf("facId") != -1 || document.referrer.indexOf("Groups") != -1)) {
		url += "&" + SEARCHPAGE + "=" + escape(document.referrer);
	}
	return url + "&" + REFERPAGE + "=" + escape(window.location.href);
}

function appendReferringEventListing(url) {
	return url + "&" + REFERPAGE + "=" + escape("http://www.threeriversparks.org/events/complete-listing.aspx");
}