function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
//Add or remove the checked product from the cookie
function checkProduct(page,promo,product,productCheck) {
var cookieContents = readCookie('productSelections');
var newSelection = 'pr='+page+'pm='+promo+'prm='+product+'|';
if (productCheck.checked){
if (cookieContents) {
if (cookieContents.indexOf(newSelection) == -1) {
createCookie('productSelections',cookieContents+newSelection,0);
}
}
else {
createCookie('productSelections',newSelection,0);	
}
}
else {
if (cookieContents) {
if (cookieContents.indexOf(newSelection) > -1) {
createCookie('productSelections',cookieContents.replace(newSelection,""),0);
}
}	
}
}
//Read all the query strings into an array
var qsParm = new Array();
function qs() {
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i=0; i<parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0) {
var key = parms[i].substring(0,pos);
var val = parms[i].substring(pos+1);
qsParm[key] = val;
}
}
}
//On page load - if this is a product page, and it's got checkboxes, check 'em - fall out where ever possible to save on load time
function checkSelectedProducts() {
var cookieContents = readCookie('productSelections');				
if (cookieContents) { //no cookie - nothing to do
if (document.forms["promoSelected"]) { //on a page with product selection - if not exit
var theForm = document.forms["promoSelected"];
var thisPage = 0;
var thisPromo = 0;		
thisPage = theForm.elements[0].value; //what product page are we on - read from a hidden field
qs(); //do we have a promo over ride - read all query strings
if (qsParm['dpr']) {
thisPromo = qsParm['dpr']; //what is this pages promotion
}		
var promoParm = cookieContents.split('|');
for (i=0;i<theForm.elements.length;i++)
{		
if (theForm.elements[i].type == "checkbox")
{
if (theForm.elements[i].name.indexOf('promoSelect') != -1) {
if (thisPromo == 0) {//migt be the products default promo - so strip it from the checkbox itself
thisPromo = theForm.elements[i].name.substr(theForm.elements[i].name.indexOf('promoSelect') + 11,
theForm.elements[i].name.length - (theForm.elements[i].name.indexOf('promoSelect') + 11));
}		
for (var j=0; j<promoParm.length; j++) {
//loop through each cookie value, if there's a match check this checkbox
if (promoParm[j] == 'pr='+thisPage+'pm='+thisPromo+'prm='+theForm.elements[i].value)
theForm.elements[i].checked = true;
}
}
}
}			
}	
}
}