/*************************************************************************
*                         COPYRIGHT NOTICE                               *
*                                                                        *
*   The contents of this file is protected under the United States       *
*   copyright laws as an unpublished work, and is confidential and       *
*   proprietary to Planetography.  Its use or disclosure in whole or in  *
*   part without the expressed written permission of Planetography. is   *
*   prohibited.                                                          *
*                                                                        *
*   (c) Copyright 1999-2007 by Planetography. All rights reserved.       *
**************************************************************************/

// Find the value for the cookie with the tag cookie_name.
function get_cookie(cookie_name)
{
	var all_cookies = document.cookie;
//alert ("cookie name to get: "+cookie_name);
	var pos = all_cookies.indexOf(cookie_name+"=");
//alert("all_cookies is: "+all_cookies);
//alert("pos "+pos);	
	if (pos != -1) {
		var start = pos+cookie_name.length+1;
		var end = all_cookies.indexOf(";", start);
		
		if (end == -1) {
			end = all_cookies.length;
		}
		
		var value = all_cookies.substring(start,end);
		return value;
	}
	return "";
}

// Save the cookie value to the tag cookie_name.
function save_cookie (cookie_name,cookie_value,cookie_path)
{
//alert ("save_cookie("+cookie_name+", "+cookie_value+", "+cookie_path+")");
	if (!cookie_name) {
		return false;
	}
	
	var my_cookie = cookie_name+"="+cookie_value;
	

	if (cookie_path) {
		my_cookie += "; path="+cookie_path;
	}
	//alert("Saving cookie: "+my_cookie);
	document.cookie=my_cookie;
	
	if (document.cookie.length==0 && my_cookie.length > 0) {
		return false;
	}
	return true;
}


// The following variables are intended to be available throughout this set of functions. The variables 'item_parsed' and
// 'item_string are used externally to walk through the current item.
var local_cookie;
var end_index = -1;
var cookie_walker;
var item_string;
var item_parsed;
var split_pattern=new RegExp (":|,");
// Initialize the cookies for the PID tracking and item list.
function item_initialize ()
{
// Store the purchase index in the cookie.  The pid is used to make sure that if duplicate images
// are added to the shopping cart, that they have a unique id.  The pid is incremented when a new
// image is added to the shopping cart.
//
// We need the pid in the cookie to make sure that a reload does not reset the index, causing
// duplicates in the shopping cart.
//alert ("Initialize item cookie");
	var pid = get_cookie ("pid");
	if (pid == "") {
//alert ("pid cookie is ("+pid+")");
		if (!save_cookie("pid","0","/")) {
			alert("ERROR 001: Your browser is configured to not accept cookies.  Please reconfigure your browser.");
		}
	}
}


// get ready to start walking the items in the cookie
function item_start ()
{
	local_cookie = get_cookie ("images");
//alert ("cookie is "+local_cookie);
	// if the cookie is empty, indicate that there are no no values
	if (local_cookie.length == 0) {
		end_index = -1;
		return false;
	}
	end_index = 0;
	cookie_walker = 0;
//item_string = "";
	return true;
}

// Walk to the next item in the items cookie.
// Extract a string with the item (minus the '&') into item_string
// Parse the item and place in items_parsed.
function item_next ()
{
//alert ("end_index cookie_walker are "+end_index+" "+cookie_walker);
	// if end of cookie already, return the status
	if (end_index == -1) {
		return false;
	}
	end_index = local_cookie.indexOf ("&",cookie_walker);
	// Check if we found another item. If not, return that status.
	if (end_index == -1) {
//alert ("end of items detected");
		return false;
	}
	item_string = local_cookie.substring(cookie_walker,end_index)
	item_parsed = item_string.split (split_pattern);
//alert ("item string is "+item_string);
	if ((item_parsed == null) || (item_parsed.length%2 != 0)) {
		end_index = -1;
		return false;
	}
	cookie_walker = end_index + 1;
	return true;
}


// This variable is used in the add_item.js file to submit to the server for pricing determination.
var item_to_add;
function item_add (product_type,item_number,item_parameters)
{
//alert ("check item "+item_number+" parameters "+item_parameters);
	// Get the purchase index, increment it by one, and use it for adding the image to the cart. We use the purchase index to
	// make every item in the purchase list unique, even if identical items are added. Update the purchase index in the cookie
	// after incrementing it.
	var pid = get_cookie ("pid");
//alert ("pid is "+pid);
	var newpid = eval (pid)+1;
	if (!save_cookie ("pid",newpid,"/")) {
		alert("ERROR 503: Your browser is configured to not accept cookies.  Please reconfigure your browser.");
		return false;
	}
	// Check if the image is already in the shopping cart. If so, ask whether to proceed. Exit if no desire to proceed, which
	// keeps user on Detail page.
	if (item_find_product (product_type,item_number)) {
		var msg="You have already added that item to the shopping cart. Do you want to add another?";
		if (!confirm (msg)) {
			return false;
		}
	}
	// concatenate the pid, product type, item number, and item parameters
	item_to_add = "sn:"+pid+",pt:"+product_type+",in:"+item_number+",qn:1,"+item_parameters
//alert ("item to add is "+item_to_add);
	var my_item = local_cookie + item_to_add+"&";
//alert ("cookie is:" +my_item);
	if (!save_cookie ("images",my_item,"/")) {
		alert("ERROR 502: Your browser is configured to not accept cookies. Please reconfigure your browser.");
		return false;
	}
	return true;
}

function item_delete (sequence_number)
{
	var new_cookie = "";
	var found = false;

	// Initialize the cookie walker, return if there is no cookie to walk
	if (!item_start ()) {
		return false;
	}
	while (item_next ()) {
		// If the current item does not match, then add it to the new cookie.
//alert ("item parsed and sequence number "+item_get_value ("sn")+"  "+sequence_number);
		if (item_get_value ("sn") == sequence_number) {
//alert ("found the one to delete");
			found = true;
		} else {
			new_cookie += item_string + "&";
		}
	}
	if (!found) {
		return false;
	}
//alert ("Saving new cookie "+new_cookie);
	save_cookie ("images",new_cookie,"/");
	return true;
}


function item_find (sequence_number)
{
	// Initialize the cookie walker, return if there is no cookie to walk
	if (!item_start ()) {
		return false;
	}
	while (item_next ()) {
		// If the current item does not match, then add it to the new cookie.
//alert ("item parsed "+item_get_value("sn"));
		if (item_get_value ("sn") == sequence_number) {
//alert ("found a match");
			return true;
		}
	}
	return false;
}


function item_find_product (product_type,item_number)
{
	// Initialize the cookie walker, return if there is no cookie to walk
	if (!item_start ()) {
		return false;
	}
	while (item_next ()) {
		// If the current item does not match, then add it to the new cookie.
//alert ("item parsed "+item_get_value("pt")+"  "+item_get_value("in"));
		if ((item_get_value ("pt") == product_type) && (item_get_value ("in") == item_number)) {
//alert ("found a match");
			return true;
		}
	}
	return false;
}


function item_replace_values (sequence_number, new_name_values)
{
//alert ("item replace_values "+new_name_values);
	var new_cookie = "";
	var found = false;
	var parsed_name_values;
	var index;
	var new_item = "";
	
	// Initialize the cookie walker, return if there is no cookie to walk
	if (!item_start ()) {
		return false;
	}
	// Find the entry, and start making a new cookie up to the point of the new entry
	while (item_next ()) {
		// If the current item does not match, then add it to the new cookie.
//alert ("item parsed  sequence"+item_get_value ("sn")+"  "+sequence_number);
		if (item_get_value ("sn") == sequence_number) {
			found = true;
//alert ("found a match");
			break;
		}
		new_cookie += item_string + "&";
	}
	if (found == false) {
		return false;
	}
	
	// Crack open the parameters to replace
	parse_name_values = new_name_values.split (split_pattern);
	// Now build the new item. Check each name value pair and if it is one of the parameters we want to replace, then
	// replace it. Otherwise, use the existing one. Finally, append any parameters that are not replaced.
	for (i=0;i<item_parsed.length;i=i+2) {
//alert ("current name is "+item_parsed[i]);
		// strip out the existing price and shipping
		if (i != 0) {
			new_item += ",";
		}
		found = false;
		for (j=0;j<parse_name_values.length;j=j+2) {
//alert ("value token "+parse_name_values[j]);
			// Found a match, use the new values and throw out old ones.
			if (item_parsed[i] == parse_name_values[j]) {
//alert ("replace");
				new_item += parse_name_values[j]+":"+parse_name_values[j+1];
				parse_name_values[j] = "";
				found = true;
				break;
			}
		}
		if (found == false) {
//alert ("use original");
			new_item += item_parsed[i]+":"+item_parsed[i+1];
		}
	}
	// Now add the values that were not used in replacement (they are new)
	for (j=0;j<parse_name_values.length;j=j+2) {
		if (parse_name_values[j] != "") {
//alert ("add name value "+parse_name_values[j]);
			new_item += ",";
			new_item += parse_name_values[j]+":"+parse_name_values[j+1];
		}
	}
//new_item += ","+new_name_values+"&";
	new_item += "&";
//alert ("new item is "+new_item);
	new_cookie += new_item;
	// Now finish appending the rest of the old entries to this one.
	while (item_next ()) {
		new_cookie += item_string + "&";
	}
//alert ("Saving new cookie "+new_cookie);
	save_cookie ("images",new_cookie,"/");
	return true;
}

// Find the value for the keyword (name) in the parsed item name/value pairs
function item_get_value (keyword)
{
	for (var i=0;i<item_parsed.length;i=i+2) {
		if (item_parsed[i] == keyword) {
			return item_parsed[i+1];
		}
	}
	return "";
}
