/********************************
 * cookie utils for ascolta.com
 ****************************************************************************************************
 *																									
 * the ascolta.com shopping cart is stored in a cookie on the client in the form:
 *	
 *			ascolta=eid:sn:q:d:ln:l:discount|eid:sn:q:d:ln:l:discount
 *
 * where | is the separator beteween items ordered, and : is the seperator between attributes
 *		eid	= event ID
 *		sn		= course Short Name (for display in cart)
 *		q		= quantity
 *		d		= date
 *		ln		= course's LongName (for display in alt-tag in cart)
 *		l		= location																	
 *		discount= Discount to be used for total calculation
 *
 ****************************************************************************************************/


var cookieArray = new Array();
var retreivedCookie;

var cu = new Image(); cu.src = '/images/cart-empty.gif';
var cd = new Image(); cd.src = '/images/cart-active.gif';

function findItem(eid) {
	var flag = -1;
	for (var z = 0; z < cookieArray.length; z++) {
		if (cookieArray[z][0] == eid) {
			flag = z;
		}
	}
	return flag;
}



function getArray() {

	retreivedCookie = document.cookie + "";
	if (retreivedCookie.indexOf("ascoltaCart") != -1) {

		var start, end;
		start = retreivedCookie.indexOf("ascoltaCart") + 13;
		end = retreivedCookie.indexOf(";", start);	
		if (end == -1) end = retreivedCookie.length;
		retreivedCookie = unescape(retreivedCookie.substring(start,end));							

		if (retreivedCookie.length > 0) {
			cookieArray = retreivedCookie.split("|");											
			for (var x = 0; x < cookieArray.length; x++) {
				cookieArray[x] = cookieArray[x].split(":");										
				for (var y = 0; y < cookieArray.length; y++) cookieArray[x][y] = unescape(cookieArray[x][y]);
			}
		} else {
			cookieArray = new Array();
		}
	}
}




function saveArray() {
	var ascoltacart = "ascoltaCart=";
	for (var a = 0; a < cookieArray.length; a++) {
		for (var b = 0; b < cookieArray[a].length; b++) cookieArray[a][b] = escape(cookieArray[a][b]);
		if (cookieArray[a][0]) cookieArray[a] = cookieArray[a].join(":");
	}
	ascoltacart += cookieArray.join("|");
	document.cookie = ascoltacart + ";path=/";
	getArray();
}




function removeFromArray(i) {

// i: the index in the top-level array that corresponds to the item we want to remove from the cart.

	var ascoltacart = "ascoltaCart="
	var tempArray = new Array();

	for (var x=0; x<cookieArray.length; x++) {
		for (var y = 0; y < cookieArray[x].length; y++) cookieArray[x][y] = escape(cookieArray[x][y]);
		if (x != i) tempArray[tempArray.length] = cookieArray[x].join(":");
	}

	ascoltacart += tempArray.join("|");
	document.cookie = ascoltacart + ";path=/";
	getArray();
}



function makeChangesInCatalog(eid,sn,d,ln,l,discount) {
	
	var index = findItem(eid)
	
	if (index == -1) { // if adding an item to the cart...
	
		// add item to the array
		var len = cookieArray.length
		cookieArray[len] = new Array();
		cookieArray[len][0] = eid;
		cookieArray[len][1] = sn;
		cookieArray[len][2] = 1;
		cookieArray[len][3] = d;
		cookieArray[len][4] = ln;
		cookieArray[len][5] = l;
		cookieArray[len][6] = discount;
		
		saveArray();
		
		
	} else {
		
		// remove the item from the array
		removeFromArray(index);
	}
}



function makeChangesInCheckout(eid, nq) {

	var i = findItem(eid)
	
	if (i == -1) return false; // item isn't in cart
	else
	{
		if (nq == 0) { // remove item from cart
			removeFromArray(i);

		} else {
			cookieArray[i][2] = nq;
			saveArray();
			getArray();
		}
	}
return true;
}



function deleteCart() {
	// this is for testing...
	// type javascript:deleteCart() into the URL to delete all things from the cart
	// and clear the cookie... if there's errors this can help in debugging.
	var lastyear = new Date(); 
	lastyear.setFullYear(lastyear.getFullYear() - 1); 
	document.cookie='ascoltaCart=ddd;path=/;expires=' + lastyear.toGMTString();
	cookieArray.length = 0;
}


getArray();




// of course, there are no bugs in this script ;-)
// but if by some wierd chance, something seems to be wrong, these functions may prove quite utilitous.
//
// is that a word?? ;-)



// slice the raw cookie up and show what SHOULD be in the array.
function report() {
	var a = "complete cookie:\n" + document.cookie + "\n\n";
	var retreivedCookie = document.cookie;															// get the cookie
	var start, end;
	start = retreivedCookie.indexOf("ascoltaCart=") + 12;										// look for the ascolta cart cookie in the list
	end = retreivedCookie.indexOf("; ", start);
	if (end == -1) end = retreivedCookie.length;
	retreivedCookie = unescape(retreivedCookie.substring(start,end));					// slice the ascoltacart cookie out of the list

	a += "retreivedCookie:\n" + retreivedCookie + "\n\n";

	var newCookiesArray = retreivedCookie.split("|");											// create a new array of all the items in the cart
	for (var x = 0; x < newCookiesArray.length; x++) {											// and for each item in the array, make a new array
		newCookiesArray[x] = newCookiesArray[x].split(":");									// containing the original three properties
		a += '[' + x + '][0]: ' + newCookiesArray[x][0] + '\n'; 
		a += '[' + x + '][1]: ' + newCookiesArray[x][1] + '\n'; 
		a += '[' + x + '][2]: ' + newCookiesArray[x][2] + '\n'; 
		a += '[' + x + '][3]: ' + newCookiesArray[x][3] + '\n'; 
		a += '[' + x + '][4]: ' + newCookiesArray[x][4] + '\n';
		a += '[' + x + '][5]: ' + newCookiesArray[x][5] + '\n';
		a += '[' + x + '][6]: ' + newCookiesArray[x][6] + '\n';
	}
	alert(a);
}

// show what is actually in the array
// hint: this and the above should show the same thing...
function reportArray() {
	var alertmessage = "";
	for (var j = 0; j < cookieArray.length; j++) {
		alertmessage += "[" + j + "][0]: " + cookieArray[j][0] + "\n";
		alertmessage += "[" + j + "][1]: " + cookieArray[j][1] + "\n";
		alertmessage += "[" + j + "][2]: " + cookieArray[j][2] + "\n";
		alertmessage += "[" + j + "][3]: " + cookieArray[j][3] + "\n";
		alertmessage += "[" + j + "][4]: " + cookieArray[j][4] + "\n";
		alertmessage += "[" + j + "][5]: " + cookieArray[j][5] + "\n";
		alertmessage += "[" + j + "][6]: " + cookieArray[j][6] + "\n";
	}
	alert(alertmessage);
}