// ZDNet UK Product Comparison functions

var COMPARE_BASE_URL = '/zdnetuk/reviews/compare.htm';
var COMPARE_COOKIE_NAME = 'zdnetcompare';
var COMPARE_INITIAL_PRODUCT;

var compare_checkbox_ids = new Array();

function compare_do(prod_cat_id) {

	var product_ids = get_compare_list(prod_cat_id);

	if(COMPARE_INITIAL_PRODUCT > 0) {
		product_ids.unshift(COMPARE_INITIAL_PRODUCT);
	}

	if( product_ids.length < 2 ) {
		alert("You must select at least two products to compare");
		return;
	}

	var url = COMPARE_BASE_URL +'?compare_ids='+ escape( product_ids.join(' ') );

	location.href = url;
}

// handler for when a checkbox is selected or unselected
function compare_checkbox_click(elem,prod_cat_id) {

	if(! elem) {
		return;
	}

	// keep track of all the checkboxes
	compare_checkbox_ids[elem.value] = elem.id;

	if( elem.checked ) {
		compare_add_product(elem.value,prod_cat_id);
	} else {
		compare_remove_product(elem.value,prod_cat_id);
	}

	compare_update_checkboxes(prod_cat_id);
}

function compare_update_checkboxes(prod_cat_id) {

	var product_ids = get_compare_list(prod_cat_id);

	var lookup = new Array();

	for(var i=0;i<product_ids.length;i++) {
		lookup[ product_ids[i] ] = true;
	}

	for(var product_id in compare_checkbox_ids) {

		var elem = document.getElementById(compare_checkbox_ids[product_id]);

		if( elem ) {
			if( lookup[product_id] ) {
				elem.checked = true;
			} else {
				elem.checked = false;
			}
		}
	}

}


// remove a product from the list of compare items
function compare_remove_product(product_id,prod_cat_id) {

	// get product ids
	var old_product_ids = get_compare_list(prod_cat_id);

	var product_ids = new Array();

	for(var i=0;i<old_product_ids.length;i++) {
		if( old_product_ids[i] != product_id ) {
			product_ids.unshift( old_product_ids[i] );
		}
	}

	store_compare_list(prod_cat_id,product_ids);
}

// adds a product to the list of items to compare
function compare_add_product(product_id,prod_cat_id) {

	// get product ids
	var product_ids = get_compare_list(prod_cat_id);

	var user_count = 4;

	if( COMPARE_INITIAL_PRODUCT > 0 ) {
		user_count --;
	}

	if( product_ids.length >= user_count ) {
		alert("You can select a maximum of "+ user_count +" products to compare");
		return false;
	}

	// add our new product id to the list
	product_ids.unshift(product_id);

	// make sure we don't have more than 4 items in the list
	if( product_ids.length > user_count) {
		product_ids = product_ids.slice(0,user_count);
	}

	// now store the compare list in the cookie
	store_compare_list(prod_cat_id,product_ids);

}

// saves a list of compare products
function store_compare_list(prod_cat_id,product_ids) {

	var cookie_name = COMPARE_COOKIE_NAME +'_'+ prod_cat_id;

	// serialize the id list
	var cookie_value = product_ids.join(' ');

	// store the info in a cookie
	setCookie(cookie_name,cookie_value,false,'/','.zdnet.co.uk',false);
}

// returns the current list of compare products as an array
function get_compare_list(prod_cat_id) {

	var cookie_name = COMPARE_COOKIE_NAME +'_'+ prod_cat_id;
	var cookie_val = getCookie(cookie_name);

	var product_ids = new Array();
	if( cookie_val ) {
		var product_ids = cookie_val.split(' ');
	}

	return product_ids;
}

// deletes the current list of compare products
function clear_compare_list(prod_cat_id) {

	var cookie_name = COMPARE_COOKIE_NAME +'_'+ prod_cat_id;
	deleteCookie(cookie_name,'/','.zdnet.co.uk');
}


/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
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 null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

	