///////////////////////////////////////////////////////////////////////////////
// FILE: UtilityScripts.js
// DESC: Contains various utility functions that can be used for a variety of 
//       purposes, including query string reading, cookie reading and writing, 
//       etc.
// CHANGE LOG:
//       DATE       NAME    COMMENTS
//       ======================================================================
//       12/01/05   PG      Original. 
//       04/07/06   PG      Added cobrandSet function.
///////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////
// FUNC: queryStringValue
// DESC: Returns a query string parameter value, given the parameter name.
// ARGS: name - the name of the query string parameter
///////////////////////////////////////////////////////////////////////////////
function queryStringValue(name)
{
    var queryString = "";
    var startIndex = 0;
    var endIndex = 0;
    var value = "";
    
    name = name.toLowerCase();
    queryString = location.search.toLowerCase() + "&";
    
    if (queryString.length > 0)
    {
        if(queryString.indexOf(name + "=") > -1)
        {
            startIndex = queryString.indexOf(name + "=") + name.length + 1;
            endIndex = queryString.indexOf("&",startIndex);
            value = queryString.substring(startIndex,endIndex);        
        }
        return value;
    }
    
} //end function queryStringValue


///////////////////////////////////////////////////////////////////////////////
// FUNC: cobrandSet
// DESC: Sets the varb 'cobrand' from the query string varb 'Cobrand' or as the 
//       value of the entire query string (ex: ?becu).  Also sets base href tag
// ARGS: -
///////////////////////////////////////////////////////////////////////////////
var cobrand = "";
var baseUrl = "";

function cobrandSet()
{
    //if there is a cobrand variable
    if (queryStringValue("cobrand") != "")
    {
        cobrand = queryStringValue("cobrand");    
    }    
    if (cobrand == "" && location.search.length > 0)
    {
        cobrand = location.search.substring(1).toLowerCase();
    }
    if (cobrand == "" || cobrand == "www")
    {
        cobrand = "sharebuilder";
    }
	if (cobrand == "wellsfargo")
	{
		baseUrl = "//wellsfargo.sharebuilder.com/wellsfargo/";
	}
    else
    {
		baseUrl = "//www.sharebuilder.com/" + cobrand + "/";
	}

    document.write("<base href='" + baseUrl + "' target='_top'>");
    
}//end function cobrandSet()
///////////////////////////////////////////////////////////////////////////////
// FUNC: cobrandBasicSet
// DESC: Sets the varb 'cobrand' from the query string varb 'Cobrand' or as the 
//       value of the entire query string (ex: ?becu).
// ARGS: -
///////////////////////////////////////////////////////////////////////////////

function cobrandBasicSet()
{
    //if there is a cobrand variable
    if (queryStringValue("cobrand") != "")
    {
        cobrand = queryStringValue("cobrand");    
    }    
    if (cobrand == "" && location.search.length > 0)
    {
        cobrand = location.search.substring(1).toLowerCase();
    }
    if (cobrand == "" || cobrand == "www")
    {
        cobrand = "sharebuilder";
    }
	if (cobrand == "wellsfargo")
	{
		baseUrl = "//wellsfargo.sharebuilder.com/wellsfargo/";
	}
    else
    {
		baseUrl = "//www.sharebuilder.com/" + cobrand + "/";
	}

    
}//end function cobrandSet()

///////////////////////////////////////////////////////////////////////////////
// FUNC: cookieWrite
// DESC: Writes a cookie using javascript.
// ARGS: name - the name of the cookie
//       value - the value of the cookie
//       daysCount - the days till expiration
///////////////////////////////////////////////////////////////////////////////
function cookieWrite(name,value,daysCount)
{
	if (daysCount)
	{
		var expiresDate = new Date();
		expiresDate.setTime(expiresDate.getTime()+(daysCount*24*60*60*1000));
		var expiresText = "; expires=" + expiresDate.toGMTString();
	}
	else 
	{
	    var expiresText = "";
	}    
	document.cookie = name + "=" + value + expiresText + "; path=/";
} //end function cookieWrite

///////////////////////////////////////////////////////////////////////////////
// FUNC: cookieWriteDomain
// DESC: Writes a cookie using javascript.
// ARGS: name - the name of the cookie
//       value - the value of the cookie
//       daysCount - the days till expiration
//			 domainName - the domain the cookie is visible to
///////////////////////////////////////////////////////////////////////////////
function cookieWriteDomain(name,value,daysCount,domainName)
{
	if (daysCount)
	{
		var expiresDate = new Date();
		expiresDate.setTime(expiresDate.getTime()+(daysCount*24*60*60*1000));
		var expiresText = "; expires=" + expiresDate.toGMTString();
	}
	else 
	{
	    var expiresText = "";
	}    
	document.cookie = name + "=" + value + expiresText + "; path=/" + "; domain=" + domainName;
} //end function cookieWriteDomain

///////////////////////////////////////////////////////////////////////////////
// FUNC: cookieRead
// DESC: Reads a cookie using javascript.
// ARGS: name - the name of the cookie
// RETV: the value of the cookie
///////////////////////////////////////////////////////////////////////////////
function cookieRead(name)
{
	var nameText = name + "=";
	var chars = document.cookie.split(';');
	for(var i=0;i < chars.length;i++)
	{
		var c = chars[i];
		while (c.charAt(0)==' ')
		{
		    c = c.substring(1,c.length);
		}
		if (c.indexOf(nameText) == 0) 
		{
		    return c.substring(nameText.length,c.length);
		}
	}
	return null;
} //end function cookieRead

///////////////////////////////////////////////////////////////////////////////
// FUNC: MMCCookieWrite
// DESC: Reads the query string for MMC parameter and writes a 
//       cookie with the value
///////////////////////////////////////////////////////////////////////////////
function MMCCookieWrite()
{
    var mmc = unescape(queryStringValue("cm_mmc"));
    if (mmc != "") 
    {
        if (cookieRead("MMC") == null)
                cookieWrite("MMC",mmc,365);
    }
} //function MMCCookieWrite


///////////////////////////////////////////////////////////////////////////////
// FUNC: trackingIframe
// DESC: Creates an iframe with the appropriate url and query string
///////////////////////////////////////////////////////////////////////////////
function trackingIframe(trackingMode)
{
    var queryString = location.search + "xx=1"
    queryString = queryString.substring(1)
    var iframe = "<iframe src='/401k/Utility/Tracking.asp?"
               + queryString + "&Referrer=" + escape(document.referrer) + "&Mode=" + trackingMode + "'"
               + " scrolling='no' width='10' height='10' frameborder='0'></iframe>";
    document.write(iframe);             
}

///////////////////////////////////////////////////////////////////////////////
// FUNC: currentDate
// DESC: Returns the current date as a string in the format: 1/2/2003
// RETV: The current date as a string
///////////////////////////////////////////////////////////////////////////////
function currentDate()
{
    var today = new Date();
    var month = today.getMonth() + 1;
    var day = today.getDate();
    var year = today.getFullYear();
    var slash = "/";
    return month + slash + day + slash + year;
    
}// end currentDate

///////////////////////////////////////////////////////////////////////////////
// FUNC: adActivityCookieWrite
// DESC: Writes a cookie called "AdActivity" with sub cookie names AdCodes, 
//       AdTypes, and ClickDates.  
// ARGS: adCode - a 5 char code that describes what it was that was seen. 
///////////////////////////////////////////////////////////////////////////////
function adActivityCookieWrite(adCode)
{    
    var adActivityValue = "";
    
    adActivityValue = "AdCodes=" + escape(adCode) 
                    + "&AdTypes=VIEW&ClickDates=" 
                    + escape(currentDate());
       
    cookieWrite("AdActivity",adActivityValue,365);
    
}// end adActivityCookieWrite

//cobrandCookieReadWrite
function cobrandCookieReadWrite()
{
	if (queryStringValue("cobrand") != "")
	{
		cobrandBasicSet();
		cookieWrite("cobrand", cobrand, "");
	}
	else
	{
		cobrand=cookieRead("cobrand");
		//cobrandBasicSet();
			if (cobrand=="" || cobrand==null)
			{
				cobrand="sharebuilder";
			}
		
	}
}
////////////////////////////////////////////////////////////////////////////////////
// FUNC: writeHeader
// DESC: Writes the header with logos with special rules for bank cobrands with a variable
//       for the page width
/////////////////////////////////////////////////////////////////////////////////////
<!--
function writeHeader()
//Put this into htm after <body>
//<script language="javascript" type="text/javascript">
//var rightHead;
//var leftHead;
//var width = "780";
//writeHeader();
//document.write("<center><div style='width:" + width + "px'><div style='float:right;'>" + rightHead + "</div><div align='left'>" + leftHead + "</div></div></center>")
///script>
	{
		cobrandAddress = "<a href='//www.sharebuilder.com/" + cobrand + "/?&intcmp=10000047'><img src='//content.sharebuilder.com/Content/JumpCobrandLogos/" + cobrand + "Logo.gif' border='0'></a>";
		comboAddress = "<a href='//www.sharebuilder.com/" + cobrand + "/?&intcmp=10000046'><img src='//content.sharebuilder.com/Content/JumpCobrandLogos/ShareBuilderLogo.gif' border='0'></a>";
			if (cobrand == "delta")
			{
				rightHead = cobrandAddress;
				leftHead = comboAddress;
			}
			else if (cobrand == "continental")
			{
				rightHead = cobrandAddress;
				leftHead = comboAddress;
				}
			else if (cobrand == "suntrust")
			{
				rightHead = "";
				leftHead = cobrandAddress;
				}
			else if (cobrand == "wellsfargo")
			{
				rightHead = "";
				leftHead = cobrandAddress;
			}
			else if (cobrand == "costco")
			{
				rightHead = comboAddress;
				leftHead = cobrandAddress;
			}
			else if (cobrand == "walmart")
			{
				rightHead = comboAddress;
				leftHead = cobrandAddress;
			}
			else if (cobrand == "walmartassociates")
			{
				rightHead = comboAddress;
				leftHead = cobrandAddress;
			}
			else {((cobrand != "delta") || (cobrand != "continental") || (cobrand != "suntrust") || (cobrand != "wellsfargo") || (cobrand != "costco") || (cobrand != "walmart") || (cobrand != "walmartassociates"))
			
				rightHead = "<a href='//www.sharebuilder.com/" + cobrand + "'><img src='//content.sharebuilder.com/Content/JumpCobrandLogos/ShareBuilderLogo-small.gif' border='0'></a>";
				leftHead = "<a href='//www.sharebuilder.com/" + cobrand + "'><img src='//content.sharebuilder.com/Content/CobrandLogos/" + cobrand + "Logo.gif' border='0'></a>";		
	}
}
//-->