//******************************************************
// Sheffield.edu Cookie Code
// Implemented on 10/15/09
//******************************************************


// Check for code in URL
var v_code = gup("code");
if(v_code!="" && !getCookie("code")) {
 // #### SET CODE COOKIE AND REF COOKIE ONLY IF NONE EXISTS
 setCookie('code',v_code,60);
 setCookie('ref',location.href,60);

 // Check for kw in URL
 v_kw = gup("kw");
 if(v_kw!="" && !getCookie("keyword"))
  setCookie('keyword',v_kw,60);

 // Check for aff in URL
 v_aff = gup("aff");
 if(v_aff!="" && !getCookie("aff"))
  setCookie('aff',v_aff,60);
}




// *********************************************************
function IsURLValuePresent(key) {
	urlKey = key  + "=";
	currentLocation = document.location.toString();
	keyIndex = currentLocation.indexOf(urlKey);
	
	if(keyIndex != "-1") {
		return true;
	} else {
		return false;
	}
}

function gup(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return results[1];
}

// ### COOKIE STUFF
function getCookie(NameOfCookie) {
 // First we check to see if there is a cookie stored.
 // Otherwise the length of document.cookie would be zero.  
 
 if (document.cookie.length > 0) { 
  // Second we check to see if the cookie's name is stored in the 
  // "document.cookie" object for the page.

  begin = document.cookie.indexOf(NameOfCookie+"="); 
  if (begin != -1) { 
   // Our cookie was set. 
   // The value stored in the cookie is returned from the function.

   begin += NameOfCookie.length+1; 
   end = document.cookie.indexOf(";", begin);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(begin, end)); 
  } 
 }
 // Our cookie was not set. 
 // The value "null" is returned from the function.
 return null;
}

function setCookie(NameOfCookie, value, expiredays) {

   // Three variables are used to set the new cookie. 
   // The name of the cookie, the value to be stored,
   // and finally the number of days until the cookie expires.
   // The first lines in the function convert 
   // the number of days to a valid date.

   var ExpireDate = new Date ();
   ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

   // The next line stores the cookie, simply by assigning 
   // the values to the "document.cookie" object.
   // Note the date is converted to Greenwich Mean time using
   // the "toGMTstring()" function.


   // alert( NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()) );
   var cookieString=NameOfCookie + "=" + escape(value) + "; path=/" + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
   if(window.location.hostname.indexOf("sheffield.edu")!=-1)
    cookieString += "; domain=.sheffield.edu";
   document.cookie = cookieString;
}

function delCookie (NameOfCookie) {

   // The function simply checks to see if the cookie is set.
   // If so, the expiration date is set to Jan. 1st 1970.

   if (getCookie(NameOfCookie)) {

      document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";

   }
}

function SetCookie(name) {
   setCookie('code',name,60);
}
  
function GetURLValue(key) {
        urlKey = key  + "=";
        currentLocation = document.location.toString();
        keyIndex = currentLocation.indexOf(urlKey);
        if(keyIndex != "-1") {
                remainingLocationString = currentLocation.substr(keyIndex, currentLocation.length);
                amperIndex = remainingLocationString.indexOf("&");
                hashIndex = remainingLocationString.indexOf("#");

                if(amperIndex != "-1") {
                        value = remainingLocationString.substr(urlKey.length, (amperIndex - urlKey.length));
                } else {
                        if(hashIndex != "-1") {
                                value = remainingLocationString.substr(urlKey.length, (hashIndex - urlKey.length));
                        } else {
                                value = remainingLocationString.substr(urlKey.length, remainingLocationString.length);
                        }
                }

        } else {
                return null;
        }

        //one last tweak decode a URL encoded value
        value = unescape(value.replace(/\+/g,  " "));
        return value;
} 
