//Get named cookie
function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

//Set cookie
function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

//Delete cookie
function Delete_Cookie(name,path,domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
       ( (path) ? ";path=" + path : "") +
       ( (domain) ? ";domain=" + domain : "") +
       ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}

//Date manipulation to get expiration date for cookie
var today = new Date();
var zero_date = new Date(0,0,0);
today.setTime(today.getTime() - zero_date.getTime());
var cookie_expire_date = new Date(today.getTime() + (8 * 7 * 86400000));

//Set Visitor ID
function setVisitorID() {
    if (Get_Cookie('VisitorID')) {
        var VisitorID = Get_Cookie('VisitorID');
        if (!VisitorID) {
            Set_Cookie('VisitorID',Math.random(),cookie_expire_date);
       }
    }
}

//Set Session ID
function setSessionID() {
    if (!Get_Cookie('SessionID'))
        Set_Cookie('SessionID',Math.random());
}

function Configure_Cookies(cookies)
{
	var cookie_name = "";
	var cookie_value = "";

	for (cookie_name in cookies)
	{
		cookie_value = cookies[cookie_name];
		
		if(cookie_value.replace(/ /g, "").length != 0)
		{
			Set_Cookie(cookie_name,cookie_value,'','/');
		}
	}
}

function mtSetLocation(cookie_name, cookie_value, location_value)
{
  // At this time, I am only aware of ColdFusion handling UPPERCASE cookies so convert to uppercase.
  cookie_name = cookie_name.toUpperCase();
  
  Set_Cookie(cookie_name,cookie_value,'','/');

  // In case the browser has cached the page, force it to reload the page
  // again by explicitly pointing it to the page (either the default
  // page or the page specified by the calling routine).
  document.location = location_value ? location_value : "/mastery_test/";
  
  return true;
}

var loaded_script = true;
