// cookies.js
// Derived from the Bill Dortch code at http://www.hidaho.com/cookies/cookie.txt

var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
	}

function getCookieValPlain (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return document.cookie.substring(offset, endstr);
	}

function GetCookie (name, escaped) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return((escaped) ? getCookieValPlain(j) : getCookieVal(j));
			}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
		}
	return null;
	}

function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "; path=/") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	}

function SetCookie (name,value,expires,path,domain,secure) {
 document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "; path=/") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
	}

function SetCookiePlain (name,value,expires,path,domain,secure) {
 document.cookie = name + "=" + value +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "; path=/") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
	}

function CookieExists (name) {
 return(GetCookie(name) ? true : false)
 }

function GetSubCookie(cookiename,item) {
 if(SubCookieExists(cookiename,item) == false) {
  return null;
 }
 var cookie = GetCookie(cookiename,-1);
 var cookiearray = cookie.split("&");
 for (var i = 0; i < cookiearray.length; i++) {
  var string2check = cookiearray[i].split("=")
  if (unescape(string2check[0]) == item.toUpperCase()) {
   return(unescape(string2check[1]))
   }
  }
	return null;
 }

function DeleteSubCookie(cookiename,item) {
 if(SubCookieExists(cookiename,item)==false) {
  return;
  }
 var cookie = GetCookie(cookiename,-1);
 var cookiearray = cookie.split("&");
 var newcookie = "";
 for (var i = 0; i < cookiearray.length; i++) {
  var string2check = cookiearray[i].split("=");
  //--->> Changed so that tested against uppercase only
  var strtest = "";
  strtest = unescape(string2check[0])
  if (strtest.toUpperCase() == item.toUpperCase()) {
   }
  else {
   if (cookiearray[i] != "") {
    newcookie = newcookie +  cookiearray[i] + "&";
    }
   }
  }
  newcookie = newcookie.substr(0, newcookie.length -1)
  SetCookiePlain(cookiename, newcookie);
 }

function SetSubCookie(cookiename,item,newvalue) {
 var cookie = GetCookie(cookiename,-1);
 var newcookie = "";
 if(cookie) {
  var cookiearray = cookie.split("&");
  var cookiefound = false;
  for (var i = 0; i < cookiearray.length; i++) {
   var string2check = cookiearray[i].split("=")
   if (unescape(string2check[0]) == item.toUpperCase()) {
    cookiearray[i] = string2check[0] + "=" + escape(newvalue);
    cookiefound = true;
    }
   }
  if (cookiefound == false) {
   cookiearray[cookiearray.length] = escape(item.toUpperCase()) + "=" + escape(newvalue)
   }
  for (var i = 0; i < cookiearray.length; i++) {
   if (cookiearray[i] != "") {
    newcookie = newcookie +  cookiearray[i] + "&"		
    }
   }
   newcookie = newcookie.substr(0, newcookie.length -1)
   SetCookiePlain(cookiename, newcookie)
  }
 else {
  newcookie = escape(item.toUpperCase()) + "=" + escape(newvalue);
  SetCookiePlain(cookiename, newcookie)
  }
 }

function SubCookieExists(cookiename,item) {
 if(CookieExists(cookiename)==false) {
  return false;
  }
 var cookie = GetCookie(cookiename,-1);
 var cookiearray = cookie.split("&");
 for (var i=0; i<cookiearray.length; i++) {
  var string2check = cookiearray[i].split("=")
  if (unescape(string2check[0]) == item.toUpperCase()) {
   return(true)
   }
  }
	return false;
 }

function limitTextBox(txtob,len) {
 if (txtob.value.length >= len) txtob.value = txtob.value.substr(0,len-1);
 }

function ClearInput(obj) {
	var str = "";
	var substitute = "";
	var rExp, rExp2;
	str = obj.value;
	rExp = /'/gi;
	rExp2 = /"/gi;
	str = str.replace(rExp,substitute);
	str = str.replace(rExp2,substitute);
	obj.value = str;
}
