var ifidSiteCatalyst = {
	buildCookiesArray : function() {
		var cookiesArray = document.cookie.split(';');
		var cookieRexExp = /^(.*)=(.*)/;
		for(i=0,ii=cookiesArray.length; i<ii; i++){
			var cookieResult = cookieRexExp.exec(cookiesArray[i]);
			ifidSiteCatalyst.ifidCookies[cookieResult[0]]=cookieResult[1]; 
		}
	},
	getPageName : function(pName) {
		pName = pName.replace(/(^https{0,1}:\/\/.*)(\/.*)/gi,"$2")/*remove http(s) & www.domain*/.replace(/^\/(.*)/,"$1")/*remove leading '/'*/;
		pName += this.getPNameHeader('calcs');// get calcs header. 
		pName += this.getPNameHeader('financialcontent'); // get financial content header.
		return pName;
	},
	getChannel : function(channel) {
		return channel;
	},
	getHier : function(hierarchy) {
		return hierarchy;
	},
	getSearchResultsCount : function() {
		var searchRegExp = /Results\s*<b>\d*<\/b>\s*-\s*<b>\d*<\/b>\s*of\s*about\s*<b>(\d*)<\/b>/i;
		var searchResults = searchRegExp.exec(document.getElementById("search").innerHTML);
		var returnString="";
		if(searchResults != null && typeof searchResults!="undefined") {
			if(typeof searchResults[1] != "undefined"){
				returnString = searchResults[1];
			}
		}
		return returnString;
	},
	getPNameHeader : function(type) {
		var returnString="";
		if(type=="calcs") {
			if(document.forms["calculator"]){
				var testRegExp = /<h2>\s*(.*)<\/h2>/i; // assumes it is the first <h2> within the 'calculator' form.
				var testString = document.forms["calculator"].innerHTML;
			}
		}
		if(type=="financialcontent") {
			if(document.getElementById("sANDpContent")){
				var testRegExp = /<h1>\s*(.*)<\/h1>/i; // assumes it is the first <h1> within 'sANDpContent' div.
				var testString = document.getElementById("sANDpContent").innerHTML;
			}
		}
		 if(typeof testString != "undefined" && testString != null && typeof testRegExp != "undefined" && testRegExp != null){
			var resultArry = testRegExp.exec(testString);
			returnString = (resultArry != null && typeof resultArry != "undefined")?' | ' + resultArry[1]:''; 
		}
		return returnString;
	},
	getWCServerName : function(sessionid) {
		var returnVal = "none"; // default
		if(typeof ifidSiteCatalyst.ifidCookies[sessionid] != "undefined"){
			var serverid = ifidSiteCatalyst.ifidCookies[sessionid];
			var serverRegExp = /\.(.*)?/;
			var tempstring = serverRegExp.exec(serverid);
			returnVal = (tempstring != null && typeof tempstring != "undefined")?tempstring[1]:"none";
		}
		return returnVal;
	},
	getIBUserStatus : function(scibuser) {
		var returnVal = "IB non-User"; // default
		if(typeof ifidSiteCatalyst.ifidCookies[scibuser] != "undefined"){
			if(ifidSiteCatalyst.ifidCookies[scibuser]=="true") returnVal = "IB User";
		}
		return returnVal;
	},
	getIFSNum : function(ifsnum) {
		ifsnum = ifsnum.replace(/[^0-9]/gi,'');
		if(ifsnum.length<5){
			ifsnum = ifsnum.replace(/(.*)/,"0$1");
		}
		ifsnum = ifsnum.match(/.{0,5}/);
		return ifsnum;
	},
	init : function () {
		ifidSiteCatalyst.pageName = (typeof arguments[0] != "undefined")?this.getPageName(arguments[0]):this.getPageName(document.location.href);
		ifidSiteCatalyst.channel = (typeof arguments[1] != "undefined")?this.getChannel(arguments[1]):"";
		
		var cookiesArray = document.cookie.split(';');
		ifidSiteCatalyst.ifidCookies = new Array(cookiesArray.length);
		var cookieRegExp = /^(.*)=(.*)?/;
		for(i=0,ii=cookiesArray.length; i<ii; i++){
			var cookieResult = cookieRegExp.exec(cookiesArray[i]);
			if(cookieResult != null){ /* cookie has no value (null) */
				if(typeof cookieResult[1] != "undefined") {
					var key=cookieResult[1].replace(/^\s*(\S*)\s*?/,"$1");
					var value=(typeof cookieResult[2] != "undefined")?cookieResult[2].replace(/^\s*(\S*)\s*?/,"$1"):"";
					ifidSiteCatalyst.ifidCookies[key]=value;
				}
			}
		}
		
		ifidSiteCatalyst.server = ifidSiteCatalyst.getWCServerName("JSESSIONID"); 
		ifidSiteCatalyst.ibUserStatus = ifidSiteCatalyst.getIBUserStatus("SCIBUser");
		/* pulling hier out. 
			ifidSiteCatalyst.hier = (typeof arguments[2] != "undefined")?this.getHier(arguments[2]):"";
		*/
	}
}
