///////////////////////////////////////////////////////////////////////////////
// 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 = "https://wellsfargo.sharebuilder.com/wellsfargo/";
	}
    else
    {
		baseUrl = "https://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 = "https://wellsfargo.sharebuilder.com/wellsfargo/";
	}
    else
    {
		baseUrl = "https://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: 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";
			}
		
	}
}