 /*Contents:  
* setCookie(name, value, expires, path, domain, secure) ?
* getCookie(name) ?
* deleteCookie(name, path, domain) 
* fixDate(date) 
* updateCookieChip(name, chipname, chipvalue, path, domain) 
* deleteCookieChip(name, chipname, path, domain) 
* getChip(name, chipname) 
* setNewChipCookie(name, chipname, chipvalue, path, domain) 
* multiValueChipReturn(name) 
*/
 
 
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + value +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "; path=/") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return false;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return dc.substring(begin + prefix.length, end);
}

function deleteCookie(name, path, domain) {

	var ExpYear = new Date();
	ExpYear.setFullYear(ExpYear.getFullYear()-1);
	

    document.cookie = name + "= " + 
    ((path) ? "; path=" + path : "; path=/") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=" + ExpYear.toGMTString();

}

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}


function updateCookieChip(name, chipname, chipvalue, path, domain) {

var tempvar = chipname.split("+");
chipname = tempvar.join("%2b");
tempvar = chipvalue.split("+");
chipvalue = tempvar.join("%2b");


allcookies = document.cookie;

//get back cookie values
var prefix = name + "=";
var pos = allcookies.indexOf("; " + prefix);

if (pos == -1) {
	pos = allcookies.indexOf(prefix);
	if (pos != 0) return false;
} else {
	pos += 2;
	}
var end = document.cookie.indexOf(";", pos);
if (end == -1) {
	end = allcookies.length;
}
	var value = allcookies.substring(pos + prefix.length, end);


//reconstruct string with spaces where they are intended.
var chipArray = new Array();
var spaceArray = value.split("+");

for (i = 0, j=0; i < spaceArray.length; i++, j++) {
	
	chipArray[j] = spaceArray[i];
	//remove "{"
	if (chipArray[j].charAt(0) == "{") {
		chipArray[j] = chipArray[j].split("{")[1];
		i++;

		while (spaceArray[i].charAt(spaceArray[i].length-1) != "}") {
			chipArray[j] = chipArray[j] + " " + spaceArray[i];
			i++;
				
		}
		//remove "}"
		chipArray[j] = chipArray[j] + " "+ spaceArray[i].split("}")[0];
	
	
	}

}


//create arrays for names and values and then divide chip string accordingly.
var oldname = new Array();
var oldValueArray = new Array();

for (i = 0, j = 0; i < chipArray.length; i++, j++) {
	oldname[j] = chipArray[i];
	i++;
	oldValueArray[j] = chipArray[i];
}


var addNew = 0;
//search new input name list for corresponding chip name/value and substitute in new value. Add new name if required.
		for (j = 0; j < oldname.length; j++) {
			if (chipname == oldname[j]) {
			
				oldValueArray[j] = chipvalue;			
				addNew = 1;
				
			}
		}
	if (addNew == 0) {
		oldname[oldname.length] = chipname;
		oldValueArray[oldValueArray.length] = chipvalue;
	}

for (i = 0, j = 0; i < oldValueArray.length; i++, j++) {
chipArray[j] = oldname[i];
j++;
chipArray[j] = oldValueArray[i];

}

for (i = 0; i < chipArray.length; i++) {
	var wordnumber = chipArray[i].split(" ");
	if (wordnumber.length > 1) {
		chipArray[i] = "{" + wordnumber.join("+") + "}";
	}
}

//join array with +s
value = chipArray.join("+");



//put back into cookie.
var cookieval = prefix + value; 
cookieval = cookieval + ((path) ? "; path=" + path : "; path=/") + ((domain) ? "; domain=" + domain : "");
//cookieval = cookieval + "; path=/";

document.cookie = cookieval;


return false;
}

function deleteCookieChip(name, chipname, path, domain) {


var tempvar = chipname.split("+");
chipname = tempvar.join("%2b");
tempvar = name.split("+");
name = tempvar.join("%2b");

allcookies = document.cookie;

//get back cookie values
var prefix = name + "=";
var pos = allcookies.indexOf("; " + prefix);

if (pos == -1) {
	pos = allcookies.indexOf(prefix);
	if (pos != 0) return false;
} else {
	pos += 2;
	}
var end = document.cookie.indexOf(";", pos);
if (end == -1)
	end = allcookies.length;
	var value = allcookies.substring(pos + prefix.length, end);
	
//reconstruct string with spaces where they are intended.
var chipArray = new Array();
var spaceArray = value.split("+");

for (i = 0, j=0; i < spaceArray.length; i++, j++) {
	
	chipArray[j] = spaceArray[i];
	
	if (chipArray[j].charAt(0) == "{") {
		chipArray[j] = chipArray[j].split("{")[1];
		i++;

		while (spaceArray[i].charAt(spaceArray[i].length-1) != "}") {
			chipArray[j] = chipArray[j] + " " + spaceArray[i];
			i++;
				
		}
		chipArray[j] = chipArray[j] + " " + spaceArray[i].split("}")[0];
	}

}



//create arrays for names and values and then divide chip string accordingly.
var oldname = new Array();
var oldValueArray = new Array();

for (i = 0, j = 0; i < chipArray.length; i++, j++) {
	oldname[j] = chipArray[i];
	i++;
	oldValueArray[j] = chipArray[i];
}


//delete cookie if last chip and terminate function.
if (oldname.length == 1 && oldname[0] == chipname) {
	deleteCookie(name, path, ((domain) ? domain : ""));
	return false;
}
//search new input name for corresponding chip name/value and substitute in new value.
var newNameArray = new Array();
var newValueArray = new Array();

		for (j = 0; j < oldname.length; j++) {
			if (chipname != oldname[j]) {
				newNameArray[newNameArray.length] = oldname[j];
				newValueArray[newValueArray.length] = oldValueArray[j];			
			}		
		}
		
	oldname = newNameArray;
	oldValueArray = newValueArray;



//reconstruct chip array

var chipArray = new Array();
for (i = 0, j = 0; i < oldname.length; i++, j++) {
	chipArray[j] = oldname[i];
	j++;
	chipArray[j] = oldValueArray[i];
}

for (i = 0; i < chipArray.length; i++) {
	var wordnumber = chipArray[i].split(" ");
	if (wordnumber.length > 1) {
		chipArray[i] = "{" + wordnumber.join("+") + "}";
	}
}

//join array with +s
value = chipArray.join("+");

//put back into cookie.
cookieval = prefix + value;
cookieval = cookieval + ((path) ? "; path=" + path : "; path=/") + ((domain) ? "; domain=" + domain : "");
document.cookie = cookieval;
return false;
}


function getChip(name, chipname) {


var tempvar = chipname.split("+");
chipname = tempvar.join("%2b");
tempvar = name.split("+");
name = tempvar.join("%2b");



allcookies = document.cookie;

//get back cookie values
var prefix = name + "=";
var pos = allcookies.indexOf("; " + prefix);

if (pos == -1) {
	pos = allcookies.indexOf(prefix);
	if (pos != 0) return false;
} else {
	pos += 2;
	}
var end = document.cookie.indexOf(";", pos);
if (end == -1) {
	end = allcookies.length;
}
	var value = allcookies.substring(pos + prefix.length, end);

//reconstruct string with spaces where they are intended.
var chipArray = new Array();
var spaceArray = value.split("+");

for (i = 0, j=0; i < spaceArray.length; i++, j++) {
	
	chipArray[j] = spaceArray[i];
	
	if (chipArray[j].charAt(0) == "{") {
		i++;

		while (spaceArray[i].charAt(spaceArray[i].length-1) != "}") {
			chipArray[j] = chipArray[j] + " " + spaceArray[i];
			i++;
				
		}
		chipArray[j] = chipArray[j] + " "+spaceArray[i];
	}

}

//get rid of curly braces
for (i = 0; i < chipArray.length; i++) {
	if (chipArray[i].charAt(0) == "{") {
		chipArray[i] = chipArray[i].split("{")[1];
		chipArray[i] = chipArray[i].split("}")[0];
	}


}

//create arrays for names and values and then divide chip string accordingly.
var oldname = new Array();
var oldValueArray = new Array();

for (i = 0, j = 0; i < chipArray.length; i++, j++) {
	oldname[j] = chipArray[i];
	i++;
	oldValueArray[j] = chipArray[i];
}

var addNew = 0;
//search input chipname for corresponding chip name value.
		for (j = 0; j < oldname.length; j++) {
			if (chipname == oldname[j]) {
				var chipvalue = oldValueArray[j];			
				addNew = 1;
			}
		}
	if (addNew == 0) {
		return "Chip not found."
	} else {
	tempvar = chipvalue.split("%2b");
	chipvalue = tempvar.join("+");
	return chipvalue
	}
}
function multiValueChipReturn(name) {

allcookies = document.cookie;

//get back cookie values
var prefix = name + "=";
var pos = allcookies.indexOf("; " + prefix);

if (pos == -1) {
	pos = allcookies.indexOf(prefix);
	if (pos != 0) return false;
} else {
	pos += 2;
	}
var end = document.cookie.indexOf(";", pos);
if (end == -1) {
	end = allcookies.length;
}
	var value = allcookies.substring(pos + prefix.length, end);


//reconstruct string with spaces where they are intended.
var chipArray = new Array();
var spaceArray = value.split("+");

for (i = 0, j=0; i < spaceArray.length; i++, j++) {
	
	chipArray[j] = spaceArray[i];
	
	if (chipArray[j].charAt(0) == "{") {
		i++;

		while (spaceArray[i].charAt(spaceArray[i].length-1) != "}") {
			chipArray[j] = chipArray[j] + " " + spaceArray[i];
			i++;
				
		}
		chipArray[j] = chipArray[j] + " "+spaceArray[i];
	}

}

//get rid of curly braces
for (i = 0; i < chipArray.length; i++) {
	if (chipArray[i].charAt(0) == "{") {
		chipArray[i] = chipArray[i].split("{")[1];
		chipArray[i] = chipArray[i].split("}")[0];
	}
tempvar = chipArray[i].split("%2b");
	chipArray[i] = tempvar.join("+");

}


//create arrays for names and values and then divide chip string accordingly.
var namevaluearray = new Array();
	
for (i = 0, j = 0; i < chipArray.length; i++, j++) {
	namevaluearray[j] = chipArray[i] + '=';
	i++;
	namevaluearray[j] = namevaluearray[j] + chipArray[i];
}

return namevaluearray;
}

//setNewChipCookie USAGE: IF COOKIE ALREADY EXISTS, USE updateCookieChip. ONLY USE setNewChipCookie IF COOKIE DOES NOT YET EXIST.

function setNewChipCookie(name, chipname, chipvalue, path, domain) {

var tempvar = chipname.split("+");
chipname = tempvar.join("%2b");
tempvar = chipvalue.split("+");
chipvalue = tempvar.join("%2b");

var wordnumber = chipname.split(" ");
if (wordnumber.length > 1) {
chipname = "{" + wordnumber.join("+") + "}";
}
var wordnumber = chipvalue.split(" ");
if (wordnumber.length > 1) {
chipvalue = "{" + wordnumber.join("+") + "}";
}



//set cookie
 document.cookie = name + "=" + chipname + "+" + chipvalue + ((path) ? "; path=" + path : "; path=/") + ((domain) ? "; domain=" + domain : "");
}

