//***********  getQueryVariable = Gets a Query String variable *********
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0].toLowerCase() == variable.toLowerCase()) {
      return pair[1];
    }
  } 
  return "";
}

//***********  getQueryStringCookies = Sets a Cookie for EACH Query String variable *********
function setQueryStringCookies()
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++)
  {
    var pair = vars[i].split("=");
    setCookie(pair[0],pair[1],null);
  } 
}

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.
		
		// Since more than one cookie can be set on a
		// single page it is possible that our cookie
		// is not present, even though the "document.cookie" object
		// is not just an empty text.
		// If our cookie name is not present the value -1 is stored
		// in the variable called "begin".
		begin = document.cookie.indexOf(NameOfCookie+"="); 
		if (begin != -1) // Note: != means "is not equal to"
		{ 
			// 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)); 
		} 
	}
	return null; 	
	// Our cookie was not set. 
	// The value "null" is returned from the function.
} 

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 parameter expiredays may be null to create a "session" 
	// cookie which expires with the browser session.
	
	// 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.	
	document.cookie = NameOfCookie + "=" + escape(value) + 
	((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

/* Recursive JS 1.0 function for string replacement
   **   Supports older browsers
   **    s - string to be changed
   **    t - token to find
   **    u - token to replace with
   **    r - fully recursive where each change is evaluated against t for additional replacement
*/
   function replace(s, t, u, r) {
     i = s.indexOf(t);
     str = "";
     if (i == -1) return s;
     if (r) {
         str += s.substring(0,i);
         s = u + s;
     }
     else {
         str += s.substring(0,i) + u;
     }
     if ( i + t.length < s.length)
       str += replace(s.substring(i + t.length, s.length), t, u, r);
     return str;
     }

/* URL Handling Routines
    ** AO doens't like doubled slahses or URLs that don't end in a slash if not
    ** terminated already by "webpage.extension" like index.html.
*/

    /* Replaced doubled forward slashes */
    modified_pathname = replace(window.location.pathname, '//', '/', true);

    if (modified_pathname.length == 0)
        modified_pathname = "/";

    /* If the path doesn't end in a "/" and if we may have a filename specified,
    ** we need to verify that the filename comes after the final slash if one is
    ** present.
    ** Otherwise, we don't need to do anything because there are no slashes at all
    ** or the path is already terminated by one.
    */
    if (    (modified_pathname.charAt(modified_pathname.length - 1) != "/")
         && (modified_pathname.lastIndexOf(".") < modified_pathname.lastIndexOf("/"))) {

        modified_pathname += "/";
    }

    /* Now check to see if made any material changes requiring a client-side redirect.
    ** If so, we need to preserve the original referrer as part of the existing query
    ** string or create a new one, but if a user happened to get here twice,
    ** for some weird reason, then we should keep the first omniReferrer
    ** in the URL query string.
    ** So we only, add omniReferrer if the current href does NOT have it.
    */
    if (modified_pathname != window.location.pathname) {
        modified_search = (window.location.search) ? window.location.search : "";

        if (   document.referrer
            && document.referrer.length > 0
            && modified_search.indexOf("omniReferrer=") == -1) {

            modified_search = (window.location.search.length > 1) ? (window.location.search + "&") : "?";
            modified_search += "omniReferrer=" + escape(document.referrer);
        }
        window.location.href = modified_pathname + modified_search + window.location.hash;
    }

