// JK last update 06/05/05

function setCookie( name, value, expiry )
{
	document.cookie = name + '=' + escape( value ) + ( expiry == null ? '' : '; expires=' + expiry.toGMTString() );
}

function getCookie( name )
{
	var cookies = document.cookie + ';';
	var start, end;
	
	start = cookies.indexOf( name + '=' );
	if( start == -1 )
		return '';
	start += name.length + 1;
	end = cookies.indexOf( ';', start );
	return unescape( cookies.substring( start, end ) );
}

function removeCookie( biscuit )
{
	var expiry = new Date();
	setCookie( biscuit, '', expiry );
}

function addToOrder( title, id, lang )
{
	// 'lang' parameter not used yet; all messages in English.
	var expiry;
	var cookie_value, cookie_delimited;
	var sid = id.toString();
	
	/* expiry = new Date();
	expiry.setMonth( (expiry.getMonth() + 1) % 12 );
	*/
	cookie_value = getCookie( 'films_ordered' );
	cookie_delimited = ',' + cookie_value + ',';
	if( cookie_delimited.indexOf( ',' + sid + ',' ) > -1 ) {
		// title already in order
		alert( '\'' + title + '\' is already in your basket. If you want to order multiple copies, you will be able to do so at checkout.' );
	}
	else {
		if( cookie_value != '' ) cookie_value += ',';
		cookie_value += sid;
		/* setCookie( 'films_ordered', cookie_value, expiry ); */
		setCookie( 'films_ordered', cookie_value, null );
		// now check that cookie has been accepted:
		cookie_value = getCookie( 'films_ordered' );
		cookie_delimited = ',' + cookie_value + ',';
		if( cookie_delimited.indexOf( ',' + sid + ',' ) > -1 )
			alert( '\'' + title + '\' has been added to your basket.' );
		else
			alert( 'Sorry, but our ordering system requires \'cookies\' to be enabled. Your browser does not seem to accept them.' );
	}
}

function removeFromOrder( title, id, lang )
{
	// 'lang' parameter not used yet; all messages in English.
	var expiry;
	var cookie_value, cookie_delimited, cookie_left, cookie_right;
	var sid = id.toString();
	var start, end;
	
	/* expiry = new Date();
	expiry.setMonth( (expiry.getMonth() + 1) % 12 );
	*/
	cookie_value = getCookie( 'films_ordered' );
	cookie_delimited = ',' + cookie_value + ',';
	start = cookie_delimited.indexOf( ',' + sid + ',' );
	if( start == -1 )
		alert( '\'' + title + '\' wasn\'t in your basket.' );
	else {
		cookie_delimited = replace( cookie_delimited, ',' + sid + ',', ',' );
		if( cookie_delimited == ',' )
			cookie_value = '';
		else
			cookie_value = cookie_delimited.substring( 1, cookie_delimited.length - 1 );
		/* setCookie( 'films_ordered', cookie_value, expiry ); */
		setCookie( 'films_ordered', cookie_value, null );
		alert( '\'' + title + ' has been removed from your basket. ' );
	}
}

function showBasket()
{
	var now = new Date();
	var w = window.open( 'basket.cfm?' + now.valueOf().toString(), 'basket', 'width=665,height=300,resizable=yes,scrollbars=yes' );
	w.focus();
}

function showPricing()
{
	var w = window.open( 'pricing.cfm', 'prices', 'width=665,height=565,resizable,scrollbars=yes' );
	w.focus();
}

function replace( str, oldToken, newToken )
{
	var start, result;
	
	result = '';
	do {
		start = str.indexOf( oldToken );
		if( start == -1 )
			result += str;
		else {
			result += str.substring( 0, start ) + newToken;
			str = str.substring( start + oldToken.length );
		}
	} while( start != -1 );
	return result;
}
		
function cookieFound()
{
	var found = ( getCookie( 'films_ordered' ) != '' );
	
	if( !found ) 
		alert( 'There are no films in your basket.' );
	return found;
}


function amountFormat( amt )
{
	if( amt.toFixed )
		return amt.toFixed( 2 );
	var pounds, pence;
	pounds = Math.floor( amt );
	pence = Math.round( amt * 100 ) % 100;
	return pounds.toString() + '.' + ( pence < 10 ? '0' : '' ) + pence.toString();
}

function confirmCancellation()
{
	if( confirm( 'Clicking \'OK\' will remove all data you entered from our server.' ) )
		location = 'cancelled.cfm';
}