var DEBUG = false;

function cookieFieldDelim () { return "'" } // previously ","

function getCookie (cookieName) {
	var cookieValue = null;
	var index = document.cookie.indexOf(cookieName + '=');
	if ( index >= 0 ) {
		var startIndex = index + (cookieName + '=').length;
		var endIndex = document.cookie.indexOf(';', startIndex);
		if ( endIndex == -1 ) {
			endIndex = document.cookie.length;
		}
		cookieValue = document.cookie.substring(startIndex, endIndex);
	}
	return cookieValue;
}

function setCookie (cookieName, cookieValue, cookiePath) {
	var temp = cookieName + '=' + cookieValue;
	if ( cookiePath ) { temp += '; path=' + cookiePath }
	document.cookie =  temp;
	return document.cookie.indexOf(cookieName + '=' + cookieValue) >= 0;
}


function getField (cookieName, fieldName, defaultValue) {
	var cookieValue = getCookie(cookieName);
	if ( ! cookieValue ) { return defaultValue }
	cookieValue = cookieValue.replace(/,/g, cookieFieldDelim()); // upgrade old cookies
	
	var fieldStartIndex = cookieValue.indexOf(cookieFieldDelim() + fieldName + ':');
	if ( fieldStartIndex < 0 ) { return defaultValue }
	var fieldEndIndex = cookieValue.indexOf(cookieFieldDelim(), fieldStartIndex + 1);
	if ( fieldEndIndex < 0 ) {
		if ( DEBUG ) { 
			alert("No terminating '" + cookieFieldDelim() + "' was found for field " + fieldName + " in cookie " + cookieName + ":\n" + cookieValue);
		}
		return defaultValue;
	}
	if ( DEBUG ) {
		alert("Found field " + fieldName + " in cookie " + cookieName + ": " + cookieValue.substring(fieldStartIndex + 1, fieldEndIndex));
	}
	return unescape(cookieValue.substring(fieldStartIndex + (',' + fieldName + ':').length, fieldEndIndex));
}


function setField (cookieName, fieldName, fieldValue) {
	// handle multiple fieldName-fieldValue pairs???
	// return status: true means success.
	var cookieValue = getCookie(cookieName);
	if ( ! cookieValue ) { cookieValue = cookieFieldDelim() }
	cookieValue = cookieValue.replace(/,/g, cookieFieldDelim()); // upgrade old cookies
	
	var fieldStartIndex = cookieValue.indexOf(cookieFieldDelim() + fieldName + ':');
	if ( fieldStartIndex >= 0 ) { // remove old field if present
		var fieldEndIndex = cookieValue.indexOf(cookieFieldDelim(), fieldStartIndex + 1);
		if ( fieldEndIndex < 0 ) {
			if ( DEBUG ) { 
				alert("No terminating '" + cookieFieldDelim() + "' was found for field " + fieldName + " in cookie " + cookieName + ":\n" + cookieValue);
			}
			return null;
		}
		if ( DEBUG ) {
			alert("Found field " + fieldName + " in cookie " + cookieName + ": " + cookieValue.substring(fieldStartIndex + 1, fieldEndIndex));
		}
		cookieValue = cookieValue.substring(0, fieldStartIndex) + cookieValue.substring(fieldEndIndex, cookieValue.length); 
	}
	cookieValue += fieldName + ':' + escape(fieldValue) + cookieFieldDelim();
	return setCookie(cookieName, cookieValue, '/');
}


function deleteField (cookieName, fieldName) {
	// handle multiple fieldNames???
	var cookieValue = getCookie(cookieName);
	if ( ! cookieValue ) { return }
	cookieValue = cookieValue.replace(/,/g, cookieFieldDelim()); // upgrade old cookies
	
	var fieldStartIndex = cookieValue.indexOf(cookieFieldDelim() + fieldName + ':');
	if ( fieldStartIndex >= 0 ) { // remove old field if present
		var fieldEndIndex = cookieValue.indexOf(cookieFieldDelim(), fieldStartIndex + 1);
		if ( fieldEndIndex < 0 ) {
			if ( DEBUG ) { 
				alert("No terminating '" + cookieFieldDelim() + "' was found for field " + fieldName + " in cookie " + cookieName + ":\n" + cookieValue);
			}
			return;
		}
		if ( DEBUG ) {
			alert("Found field " + fieldName + " in cookie " + cookieName + ": " + cookieValue.substring(fieldStartIndex + 1, fieldEndIndex));
		}
		cookieValue = cookieValue.substring(0, fieldStartIndex) + cookieValue.substring(fieldEndIndex, cookieValue.length); 
	}
	return setCookie(cookieName, cookieValue, '/');
}


function setInCart (newQuantity, skuNumber, skuLabel) {
	newQuantity = parseInt('' + newQuantity, 10);
	if ( isNaN(newQuantity) ) {
		alert("Please enter an integer number of items desired.");
		return false;
	}
	
	if (newQuantity < 0) {
		alert('Please enter the number of items desired, or zero for none.');
		return false;
	}
	var status;
	if ( newQuantity == 0 ) { status = deleteField('TheCart', skuNumber) }
	else { status = setField('TheCart', skuNumber, newQuantity) }
	if ( status ) {
		if ( skuLabel ) {
			alert('Thanks! Your cart now has ' + newQuantity + ' of ' + skuLabel);
		}
	}
	else {
		alert('Sorry, there is a problem saving your cart.  Have you disabled cookies for this site?  Cookies are required to order online.');
	}
}


function addToCart (addQuantity, skuNumber, skuLabel) {
	// Fetch current cart cookie.
	// Find skuNumber in current cart, if any.
	// If found, replace (or increment?) the sku quantity; else add skuNumber and addQuantity.
	// Rewrite cart cookie.
	// Display new cart quantity.
	
	addQuantity = parseInt('' + addQuantity, 10);
	if ( isNaN(addQuantity) ) {
		alert("Please enter an integer number of items desired.");
		return false;
	}
	
	if (addQuantity <= 0) {
		alert('Please enter the number of items desired.');
		return false;
	}
	
	var oldQuantity = getField('TheCart', skuNumber, '0');
	if ( DEBUG ) { alert("Found TheCart sku " + skuNumber + " old quantity " + oldQuantity) }
	oldQuantity = parseInt(oldQuantity, 10);
	if ( isNaN(oldQuantity) ) {
		alert("Sorry, there is a problem with your cart:  Non-integer old quantity for sku " + skuNumber + " in:\n" + cartCookieValue);
		return false;
	}
	var newQuantity = oldQuantity + addQuantity; // increment
	if ( setField('TheCart', skuNumber, newQuantity) ) {
		alert('Thanks! Your cart now has ' + newQuantity + ' of ' + skuLabel);
	}
	else {
		alert('Sorry, there is a problem saving your cart.  Have you disabled cookies for this site?  Cookies are required to order online.');
	}
	return false;
}
