// ~~~ OBJECT DECLARATION ~~~
function LargeBusinessRefCode()
{
	
	//default name of the URL parameter we're looking for
	this.urlParamName = 'refCode';
	
	//max chars allowed for a refcode
	this.maxParamValChars = 50;
	
	//the name of the cookie
	this.cookieName = 'LargeBusinessRefCode';
	
	//the number of seconds to keep the cookie
	this.cookieDuration = 60*60*24;
	
	//the path for the cookie
	this.cookiePath = "/";
	
	//the domain for the cookie
	this.cookieDomain = "qwest.com";
	   
} //end class LargeBusinessRefCode



// ~~~ ACCESSORS ~~~

// get() - reads from session and returns a string
LargeBusinessRefCode.prototype.get = function ()
{
	//look for the position of our cookie in the cookies for this document
	var allCookies = document.cookie;
	var searchFor = this.cookieName + '=';
	var pos = allCookies.indexOf(searchFor);
	
	//return null if that cookie is not set
	if (pos == -1) { return null; }
	
	//the cookie value starts after the cookieName and '=',
	//so count that many characters past pos
	var start = pos + this.cookieName.length + 1;
	
	//the end of the cookie value is at the ';' char or the end of the cookies' string
	var end = allCookies.indexOf(";", start);
    if (end == -1) end = allCookies.length;

	//get the substring that is the cookie value and decode it
    var value = allCookies.substring(start, end);
    value = decodeURIComponent(value);

	//return the value or null if it is an empty string
	if ((value) && (value != ""))
	{
		return value;
	}
	else
	{
		return null;
	}
	
} //end get()


// set() writes a given refCode to session
LargeBusinessRefCode.prototype.set = function ( inVal )
{
	if  ((inVal) && (inVal != ""))
	{
		
		//clean it... only allow letters and numbers...
		inVal = inVal.toString().replace(/[^A-Za-z0-9]/g, '');
		inVal = inVal.substr(0, this.maxParamValChars);
		
		
		document.cookie = this.cookieName + '=' + inVal
			+ '; max-age=' + this.cookieDuration 
			+ '; path=' + this.cookiePath
			+ '; domain=' + this.cookieDomain;
	}
	
} //end set



// ~~~ CONVENIENCE METHODS ~~~

// setFromURLParam() reads from URL and stores the parameter value in session
// name of param to read is an optional arg, otherwise the default set
// in object declaration is used
LargeBusinessRefCode.prototype.setFromURLParam = function ( inParamName )
{
	
	//default to the param name in this object
	var pName = this.urlParamName
	if ((inParamName) && (inParamName != ""))
	{
		//an alternate name was given, use it instead
		pName = inParamName;
	}
	
	
	//get URL parameters as string
	// - location
	// - remove leading and trailing whitespace
	var urlStr = location.toString().replace(/^\s+/, '').replace(/\s+$/, '');
	
	//don't bother doing anything if there are no URL params
	if (urlStr.search(/\?/) == -1) { return; }
	
	//grab the part after the ?
	var urlParamsStr = urlStr.split('?')[1];
	
	//don't bother if it's null or empty
	if ((! urlParamsStr) || (urlParamsStr == "")) { return; }
	
	//now, split on '&' delimiters to make an array, iterate over it
	var all_pvs_array = urlParamsStr.split('&');
	var refCode = null;
	for (var i=0; i< all_pvs_array.length; i++)	
	{
		
		//use each array entry, should be of form 'param=value'
		var pv_str = all_pvs_array[i];
		if ((! pv_str) || (pv_str == "")) { continue; }
		if (pv_str.search(/\=/) == -1) { continue; }
		var pv_array = pv_str.split('=');
		
		//see if we have found the param name we want
		//if so, store the refCode and break out of the loop
		if ( pv_array[0] == pName )
		{
			//leave refCode as null if it is an empty string
			if (pv_array[1] != "")
			{
				refCode = pv_array[1];
			}
			
			break;
			
		} //end if we matched the param name we're looking for
		
	} //end loop over all param value pair strings
	
	//store the refCode in the appropriate cookie if found
	if (refCode)
	{
		this.set( refCode );
	}
	
} //end storeIfPresent()

// ~~~ END OBJECT DEFINITION ~~~ 





//********************
// === MAIN ===
//********************

//store a refCode if there is one in the URL
largebusinessrefcode = new LargeBusinessRefCode();
largebusinessrefcode.setFromURLParam();
