function ckbxAddRmvList(name, value, checked) {
	if (checked) {
		addValue(name, value);
	} else {
		removeValue(name, value);
	}
}

function ckbxSetFromSaved (name, ckbx_name) {
	var fields = document.getElementsByName(ckbx_name);
	for (i = 0; i < fields.length; i++) {
  		fields[i].checked = getValue(name,fields[i].value);
	}
}

function getDelimList(name, delim) {
   var list = getList(name);
   if (list.length > 0) {
   	list = list.substring(1, list.lastIndexOf("_"));
   }
   return list.replace(/_/g, delim);
}

function getCommaList(site) {
   var list = getList(site);
   if (list.length > 0) {
   	list = list.substring(1, list.lastIndexOf("_"));
   }
   return list.replace(/_/g, ",");
}

function getList(site) {
   var theCookie=""+document.cookie;
   var ind=theCookie.indexOf(site+"_oh");
   var list = "";
   
   if (ind>-1 && site.length > 0) {
      list = ReadCookie(site+"_oh");
   }
   return list;
}

function getValue(site, value) {
   var list = getList(site);
      
   if (list.indexOf("_" + value + "_") > -1) {
      return true;
   }
   return false;
}

function addValue(site, value) {
   var list = getList(site);
   
   if (list.indexOf("_" + value + "_") == -1) {
 	if (list.length > 0) {
       		list = list + value + "_";
        } else {
		list = "_" + value + "_";
	}
	   SetCookie(site, list);      
   }
   return list;
}

function removeValue(site, value) {
   var list = getList(site);
   
   var ind_value = list.indexOf("_" + value + "_");

   if (ind_value > -1) {
      list = list.substring(0, ind_value) + list.substring(ind_value+value.length + 1);
      SetCookie(site, list);      
   }
   
   return list;
}

function ReadCookie(cookieName) {
	
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	
	if (ind==-1 || cookieName=="")
		return "";
	
	var ind1 = theCookie.indexOf(';', ind);
	
	if (ind1==-1)
		ind1 = theCookie.length; 
	
	return unescape(theCookie.substring(ind+cookieName.length + 1, ind1));
}

function SetCookie(site, cookieValue) {
	
	var today = new Date();
	var expire = new Date();

	today.setHours(0);

	nDays = (8 - today.getDay()) % 7;
	
	if (nDays == null || nDays == 0) 
		nDays = 7;
	
	expire.setTime(today.getTime() + 3600000*24*nDays);
	
	var domain = document.domain;
	if (domain.indexOf("www.") == 0) {
		domain = domain.substring(domain.indexOf(".") + 1);
	}
	
	document.cookie = site+"_oh"+"="+escape(cookieValue) + "; expires="+expire.toGMTString()+"; path=/; domain="+domain+";";
}

function DeleteCookie(site) {
	
	var expire = new Date();

	expire.setTime(expire.getTime() - 1);
	
	var domain = document.domain;
	if (domain.indexOf("www.") == 0) {
		domain = domain.substring(domain.indexOf(".") + 1);
	}
	
	document.cookie = site+"_oh"+"=; expires="+expire.toGMTString()+"; path=/; domain="+domain+";";
}
