/*
	Wolfmotell
	Shawn Heller
	June 1, 2009
	
	Responsible for the common code between Sitecore and WebTrac
*/


// VARIABLES
var session = new Session();
var keepAliveXMLHttp;
var keepAliveResult;
var keepAliveInterval = 300000;


// CONSTANTS
var EVENTID = "EventID";
var PAGEID= "PageID";
var SECTIONID = "SectionID";
var PASSID = "PassID";
var SESSIONID = "SessionID";
var USERNAME = "UserName";
var REFERPAGE = "ReferPage";
var SEARCHPAGE = "SearchPage";


// PUBLIC FUNCTIONS

/* username formatter */
function formatLoginName(webtrac_name) {
	return "Not in the " + session.name + " household?";
}


/* WebTrac URL Formatter */
function formatWebTracURL(page, activity, section, pass) 
{
	var url = rootURL;
	url += page + ".html?";

	// modify the session id for golftrac
	if(page.indexOf("wbgr0210") == -1)
	{
		url += "wbsi=" + session.id + "&";
	}

	if(page.indexOf("wbar0305") != -1 && isNotEmpty(activity) && isNotEmpty(section))
	{
		url += "xxActvNumb=" + activity + "&";
		url += "xxsection=" + section;
	} else if(page.indexOf("wbpm0305") != -1 && isNotEmpty(pass)) {
		url += "xxpasstype=" + pass;
	} else if(page.indexOf("wbsa0100") != -1 || page.indexOf("wbsa0110") != -1) {
		// fyi. registering a new account does not use this.
		url += "xxac=c";
	} else if(page.indexOf("wbsa0260") != -1) {
		url += "xxbaldue=no";
	}
	return url;
}


/* Session Class */
function Session() 
{
	this.id = "";
	this.isValid = false;
	this.name = "";

	this.init = function()
	{
		this.id = getCookie(SESSIONID);
		if (this.id!="") {
			this.isValid = true;
		} else {
			this.isValid = false;
		}
		this.name = getCookie(USERNAME);
	}
}


/* session keepalive */
function keepAlive(toEncode) {
	toEncode == null ? false : toEncode;
	if(keepAliveXMLHttp == null)
	{
		if (window.XMLHttpRequest)
  		{
  			// code for all new browsers
  			keepAliveXMLHttp = new XMLHttpRequest();
  		} else if (window.ActiveXObject) {
  			// code for IE5 and IE6
  			keepAliveXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
  		}
  	}

	if (keepAliveXMLHttp != null && session.isValid)
	{
		var url = timeoutURL + (toEncode ? escape("wbar0225.html?wbsi=" + session.id) : "wbar0225.html?wbsi=" + session.id);
  		keepAliveXMLHttp.open("GET", timeoutURL, true);
  		keepAliveXMLHttp.send(null);
	}
}


// UTILITY FUNCTIONS

/* Find an object in the DOM by id */
function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  return foundObj;
} 


/* Set the innerHTML of an object */
function setInnerHTML(objName, newStr)
{
  obj = findObj(objName);
  if (obj.innerHTML)
  {
    obj.innerHTML = newStr;
  }
}


/* Read Cookie */
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}


/* Get param by name from querystring */
function getParameter(name, queryString) {
  if(queryString == null) { queryString = window.location.search.substring(1); }

  var parameters = new Array();
  parameters = queryString.split('&');

  for(var i = 0; i < parameters.length; i++) {
    if (parameters[i].toLowerCase().indexOf(name.toLowerCase())>=0) {
      var parameterValue = new Array();
      parameterValue = parameters[i].split('=');
      return parameterValue[1];
    }
  }
  return "";
}


/* Check for empty strings */
function isNotEmpty(stringvar)
{
	return stringvar != null && stringvar != "";
}
