// a list of ajax scripts
// Create an XMLHttpRequest object
function create_xml_http_request()
{
	var request = null;
	
	try
	{
		request = new XMLHttpRequest();
	}
	catch( e )
	{
		try
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(  e )
		{
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	return request;
}

function get_xml_node( xml_doc, id, index )
{
	var result = null;
	if( !index ) index = 0;
	if( xml_doc.getElementsByTagName( id ) )
	{
		if( xml_doc.getElementsByTagName( id )[ index ] )
		{
			if( xml_doc.getElementsByTagName( id )[ index ].childNodes.length > 0 ) result = xml_doc.getElementsByTagName( id )[index].childNodes[0].nodeValue;
		}
	}
	return result;
}

function get_xml_attribute( xml_doc, id, attribute_id, index )
{
	var result = null;
	if( !index ) index = 0;
	if( xml_doc.getElementsByTagName( id ) )
	{
		if( xml_doc.getElementsByTagName( id )[ index ] )
		{
			if( xml_doc.getElementsByTagName( id )[ index ].childNodes.length > 0 ) result = xml_doc.getElementsByTagName( id )[index].getAttribute( attribute_id );
		}
	}
	return result;
}

var g_request = null;

function applications_request( grid )
{
	var request = create_xml_http_request();
	var url = g_root + "/ajax/update_view.php?grid=" + grid;
	
	request.open("GET",url,true);
	request.send(null);		
}


/********************************************************************************
 * Popup: Country
 * ==============
 */

function open_popup_country()
{
	popup_show_loading();
	g_request = create_xml_http_request();
	var url = g_root + "/ajax/country_list.php";
	
	g_request.onreadystatechange = country_list_response;
	g_request.open("GET",url,true);
	g_request.send(null);	
}

function country_list_response()
{
	if( g_request.readyState == 4 )
	{
		var xml_doc = g_request.responseXML.documentElement;
		
		var ddl = Builder.node( 'select', { id: 'popup_country_id', name: 'popup_country_id', style: 'float:left; margin-bottom:0px' } );

		var countries = xml_doc.getElementsByTagName( 'country' );
		for( var i = 0; i < countries.length; i++ )
		{
			country = countries[ i ];
			var country_id = country.getAttribute( 'id' );
			var country_name = country.childNodes[ 0 ].nodeValue;
			
			ddl.options[ i ] = new Option( country_name, country_id );
			if( country.getAttribute( 'selected' ) )
			{
				ddl.options[ i ].selected = true;	
			}
		}
		
		var obj = Builder.node( 'div', [ 
			Builder.node( 'form', { id: 'form_popup', method: 'post', action: '' }, [
				ddl,
				Builder.node( 'input', { name: 'button_select', type: 'button', className: 'button button_positive', style: 'margin-bottom:0px', value: 'Select', onclick:'update_country();' } ),
				Builder.node( 'div', { className: 'clear' } )
			] )
		] );
		popup_open( obj, 'Select your country...' );
	}
}

function update_country()
{
	var ddl = document.getElementById( 'popup_country_id' );
	var country_id = ddl.options[ ddl.selectedIndex ].value;
	popup_kill();
	var url = g_root_secure + "/country_action.php?country_id=" + country_id;
	window.location = url;
}


/********************************************************************************
 * Popup: Variations
 * =================
 */
 
var g_popup_checked_variations = null;
var g_popup_variations_cancel = 0;

function open_popup_variations( product_id, stock_reminder, submit_reminder, cancel )
{
	var result = true;
	if( !is_IE6() )
	{
		var result = false;
		if( cancel )
		{
			g_popup_variations_cancel = cancel;	
		}
		popup_show_loading();
		g_request = create_xml_http_request();
		var url = g_root + "/ajax/variations_list.php?product_id=" + product_id;
		
		if( stock_reminder )
		{
			url += "&stock_reminder=" + stock_reminder;
			if( submit_reminder )
			{
				url += '&submit_reminder=1';
				if( g_popup_checked_variations.length > 0 )
				{
					url += '&checked_variations=' + escape( g_popup_checked_variations );	
					g_popup_checked_variations = null;
				}
				
				if( document.getElementById( 'popup_email' ) )
				{
					url += '&email=' + document.getElementById( 'popup_email' ).value;
				}
			}
		}
	
		g_request.onreadystatechange = variation_list_response;
		g_request.open("GET",url,true);
		g_request.send(null);
	}
	return result;
}

function variations_add_reminders( product_id )
{
	var form = document.getElementById( 'form_popup' );
	var objs = form.getElementsByTagName( 'input' );
	g_popup_checked_variations = new Array();
	
	for( var i = 0; i < objs.length; i++ )
	{
		obj = objs[ i ];
		if( obj.type == 'checkbox' && obj.checked )
		{
			g_popup_checked_variations.push( obj.value );
		}	
	}
	
	open_popup_variations( product_id, 1, 1 );
}

function variation_list_response()
{
	if( g_request.readyState == 4 )
	{
		var xml_doc = g_request.responseXML.documentElement;
		var table = Builder.node( 'table', { className: 'table_clear_header', style: 'width:698px' } );
		
		var names = xml_doc.getElementsByTagName( 'name' );
		var attr2 = xml_doc.getElementsByTagName( 'attr2' );
		var stock_reminder = parseInt( xml_doc.getElementsByTagName( 'stock_reminder' )[ 0 ].childNodes[ 0 ].nodeValue );
		var product_id = xml_doc.getElementsByTagName( 'product_id' )[ 0 ].childNodes[ 0 ].nodeValue;
		var product_name = xml_doc.getElementsByTagName( 'product_name' )[ 0 ].childNodes[ 0 ].nodeValue;
		
		var checked_variations = new Array();
		var checked_variation_ids = xml_doc.getElementsByTagName( 'checked_variation_id' );
		for( var i = 0; i < checked_variation_ids.length; i++ )
		{
			checked_variations.push( checked_variation_ids[ i ].childNodes[ 0 ].nodeValue );
		}
		
		var tr = null;
		if( attr2.length > 1 )
		{
			tr = Builder.node( 'tr', { className: 'table_no_border' } );
			
			tr.appendChild( Builder.node( 'th', '' ) );
			for( var i = 0; i < attr2.length; i++ )
			{
				var label = attr2[ i ].getElementsByTagName( 'label' )[ 0 ];
				if( label.childNodes.length > 0 ) label = label.childNodes[ 0 ].nodeValue;
				else label = "";
				tr.appendChild( Builder.node( 'th', [ Builder.node( 'div', { className: 'no_input' } ).update( label ) ] ) );
			}
			table.appendChild( tr );
		}
		
		checked = true;
		
		var show_stock_reminder = false;
		var show_add_button = false;
		
		for( var i = 0; i < names.length; i++ )
		{
			tr = Builder.node( 'tr', { className: 'narrow small' } );
			
			var color = names[ i ].getAttribute( 'color' );
			var color_dark = names[ i ].getAttribute( 'color_dark' );
			var color_light = names[ i ].getAttribute( 'color_light' );
			
			var name_obj = null;
			
			var td = Builder.node( 'td' );
			if( color )
			{
				td.appendChild( Builder.node( 'span', { className: 'swatch', style:'background:' + color + '; background:-moz-linear-gradient(bottom, ' + color + ', ' + color_light + ' ); border-color:' + color_dark + ';' } ) );
			}
			td.appendChild( Builder.node( 'b' ).update( names[ i ].childNodes[ 0 ].nodeValue ) );

			tr.appendChild( td );
			
			for( var j = 0; j < attr2.length; j++ )
			{
				var variation = attr2[ j ].getElementsByTagName( 'variation' )[ i ];
			
				var variation_id = get_xml_node( variation, 'variation_id' );
				var price = get_xml_node( variation, 'price' );
				var rrp = get_xml_node( variation, 'rrp' );
				var stock = get_xml_node( variation, 'stock' );
				if( !stock ) stock = 0;
				var reorder = get_xml_node( variation, 'reorder' );
				
				var price_obj = null;
				if( rrp )
				{
					price_obj = Builder.node( 'b', [
						Builder.node( 'span', { className: 'offer' } ).update( price ),
						' ',
						Builder.node( 'span', { className: 'rrp', style: 'text-decoration:line-through' } ).update( rrp )
					] );
				}
				else
				{
					price_obj = Builder.node( 'b' ).update( price );
				}
				
				var stock_image = 'stock_in.gif';
				var stock_description = 'In Stock';
				
				if( stock == 0 )
				{
					if( reorder == 1 )
					{
						stock_image = 'stock_waiting.gif';
						stock_description = 'On Order';
						if( !stock_reminder ) show_stock_reminder = true;
					}
					else
					{
						stock_image = 'stock_out.gif';
						stock_description = 'Discontinued';
					}
				}
				else
				{
					show_add_button = true;
					
				}
				stock_obj = Builder.node( 'span', { className: 'icon' }, [
					Builder.node( 'img', { src: g_root + '/images/' + stock_image, alt: stock_description } )
				] );
				
				var width = "130px";
				if( attr2.length == 1 )
				{
					width = "195px";	
				}
				
				if( !variation_id )
				{
					stock_obj = '';
					price_obj = Builder.node( 'b', 'N/A' );
				}
				
				var radio = '';
				
				if( stock_reminder )
				{
					if( stock == 0 && reorder == 1 )
					{
						
						checked_variation = in_array( variation_id, checked_variations );
						if( checked_variation )
						{
							radio = Builder.node( 'input', { type: 'checkbox', className: 'radio', name: 'variation_id', value: variation_id, checked: checked } );
						}
						else
						{
							radio = Builder.node( 'input', { type: 'checkbox', className: 'radio', name: 'variation_id', value: variation_id } );
						}
						
						var td = Builder.node( 'td' );
						td.style.width = width;
						
						td.appendChild( radio );
						td.appendChild( stock_obj );
						td.appendChild( price_obj );
						
						tr.appendChild( td );
					}
					else
					{
						var td = Builder.node( 'td' );
						td.style.width = width;
						
						td.appendChild(
							Builder.node( 'div', { className: 'no_input' }, [
									radio,
									stock_obj,
									price_obj
							] )
						);
						
						tr.appendChild( td );
					}
				}
				else
				{

					if( stock > 0 )
					{
						
						if( checked )
						{
							radio = Builder.node( 'input', { type: 'radio', className: 'radio', name: 'variation_id', value: variation_id, checked: checked } );
						}
						else
						{
							radio = Builder.node( 'input', { type: 'radio', className: 'radio', name: 'variation_id', value: variation_id } );
						}
						checked = false;
						// TODO: td style
						var td = Builder.node( 'td' );
						td.style.width = width;
						
						td.appendChild( radio );
						td.appendChild( stock_obj );
						td.appendChild( price_obj );
						
						tr.appendChild( td );
					}
					else
					{
						// TODO: td style
						
						var td = Builder.node( 'td' );
						td.style.width = width;
						
						td.appendChild(
							Builder.node( 'div', { className: 'no_input' }, [
								radio,
								stock_obj,
								price_obj
							] )			   
						);
						
						tr.appendChild( td );
					}
				}
			}
			table.appendChild( tr );
			
		}
		
		var cancel_str = ( g_popup_variations_cancel ) ? "popup_kill();" : "open_popup_variations( " + product_id + " );";
		
		add_button_obj = '';
		if( show_add_button )
		{
			add_button_obj = 	
				Builder.node( 'p', { className: 'right' }, [
					Builder.node( 'b', 'Choose quantity' ), 
					' ',
					Builder.node( 'span', [ 
						Builder.node( 'input', { id: 'popup_quantity', name: 'quantity', type: 'text', className: 'text qty', value: '1' } ) 
						
					] ),
					' ',
					Builder.node( 'span', [
						Builder.node( 'input', { name: 'btn_add', type: 'button', className: 'button button_positive', value: 'Add To Basket', onclick: 'basket_add();' } ) 
						
					] )
				
				] );
		}
		
		stock_reminder_obj = '';
		if( show_stock_reminder )
		{
			stock_reminder_obj = 
				Builder.node( 'p', { className: 'small' }, [
					Builder.node( 'img', { className: 'icon', src: g_root + '/images/icon_talk.png', alt: '' } ),
					Builder.node( 'a', { href: '', onclick: 'open_popup_variations( ' + product_id + ', 1 ); return false' }, [
						Builder.node( 'b', 'Contact me when back in stock' )
					] ),
				] );	
		}
		
		var stock_p_obj = '';
		var cancel_p_obj = '';
		if( stock_reminder )
		{
			cancel_p_obj = Builder.node( 'p', { className: 'small', style: 'width:200px' }, [ 
				Builder.node( 'input', { name: 'btn_cancel', type: 'button', className: 'button button_sub', value: 'Cancel', onclick: cancel_str } )
				] ); 
			
			stock_p_obj = Builder.node( 'p', { className: 'small', style: 'width:700px' }, [ 
					Builder.node( 'b', 'Please tick the products below for which you would like to be sent a stock reminder (you can select more than one). As soon as the product is back in stock we will automatically let you know via email.' ) ] );
			
			
			var email = '';
			var customer_id = null;
			if( xml_doc.getElementsByTagName( 'customer_id' )[ 0 ].childNodes.length > 0 )
			{
				customer_id = xml_doc.getElementsByTagName( 'customer_id' )[ 0 ].childNodes[ 0 ].nodeValue;
			}
			if( xml_doc.getElementsByTagName( 'email' ).length > 0 )
			{
				email = xml_doc.getElementsByTagName( 'email' )[ 0 ].childNodes[ 0 ].nodeValue;
			}
			
			var email_obj = '';
			var email_str = '';
			if( !customer_id )
			{
				email_obj = 
					Builder.node( 'span', [ 
						Builder.node( 'input', { id: 'popup_email', name: 'popup_email', type: 'text', className: 'text', value: email } )
					] );
				email_str = 'Enter your email address';
			}
			
			add_button_obj = 	
				Builder.node( 'p', { className: 'right', style: 'width:450px' }, [
					Builder.node( 'b', email_str ), 
					' ',
					email_obj,
					' ',
					Builder.node( 'span', [
						Builder.node( 'input', { name: 'btn_remind', type: 'button', className: 'button button_positive', value: 'Submit', onclick: 'variations_add_reminders( ' + product_id + ' );' } ) 
						
					] )
				
				] );
																																  
		}
		
		// get any messages
		var error_obj = basket_message_box( xml_doc.getElementsByTagName( 'error' ), 'error_box' );
		var success_obj = basket_message_box( xml_doc.getElementsByTagName( 'success' ), 'success_box' );
		var warning_obj = basket_message_box( xml_doc.getElementsByTagName( 'caution' ), 'warning_box' );
		
		var obj = Builder.node( 'form', { id: 'form_popup', name: 'form_popup', method: 'post', action: '' }, [
			error_obj,
			success_obj,
			warning_obj,
			stock_p_obj,
			Builder.node( 'div', { className: 'product', style: 'padding-top:5px' }, [
				Builder.node( 'p', { className: 'stock' }, 'Key:' ),
				Builder.node( 'p', { className: 'stock' }, [
					Builder.node( 'img', { src: g_root + '/images/stock_in.gif', alt: 'In Stock' } ), 'In Stock'
				] ),
				Builder.node( 'p', { className: 'stock' }, [
					Builder.node( 'img', { src: g_root + '/images/stock_waiting.gif', alt: 'On Order' } ), 'On Order'
				] ),
				Builder.node( 'p', { className: 'stock' }, [
					Builder.node( 'img', { src: g_root + '/images/stock_out.gif', alt: 'Discontinued' } ), 'Discontinued'
				] ),
				Builder.node( 'div', { className: 'clear' }, ' ' ),
				Builder.node( 'div', { className: 'form_box' }, [
					Builder.node( 'div', { className: 'form_box_content3' } , [
						table
					] )
				] ),
				Builder.node( 'div', { className: 'controls' }, [
					Builder.node( 'div', { className: 'top' }, [
						Builder.node( 'div', { className: 'content' }, [
							cancel_p_obj,
							stock_reminder_obj,
							add_button_obj,
							Builder.node( 'div', { className: 'clear' }, ' ' )
						] )
					] )
				] )
			] )
		] );
		
		popup_open( obj, product_name, 720 );
	}
}

function basket_add( variation_id )
{
	var result = true;
	if( !is_IE6() )
	{
		result = false;
		var quantity = 1;
		if( !variation_id )
		{
			// check to see whether we are adding from the popup or the actual prouct page itself
			
			var obj = null;
			if( document.form_popup )
			{
				obj = document.form_popup.variation_id;
				quantity = document.getElementById( 'popup_quantity' ).value;
			}
			else
			{
				obj = document.form1;
				quantity = document.getElementById( 'quantity' ).value;
			}
			
			if( typeof( obj.length ) !== "undefined" )
			{
				for( i = 0; i < obj.length; i++ )
				{
					if( obj[ i ].checked == true )
					{
						variation_id = obj[ i ].value;
					}
				}
			}
			else
			{
				variation_id = obj.value;	
			}
			
		}
		else
		{
			quantity = 	document.getElementById( 'quantity_' + variation_id ).value;
		}
		
		var query_string = '?add_to_basket=1&variation_id=' + variation_id + '&quantity=' + quantity;
		
		var filename = location.href;
		filename = filename.substring(filename.lastIndexOf('/')+1);
		
		// If we are already on the shopping basket page we cannot use ajax so need to use the url remove instead
		if( filename == "shopping_basket.php" )
		{
			popup_kill();
			window.location = g_root + '/shopping_basket_action.php' + query_string;
		}
		else
		{
			popup_show_loading();
			var url = g_root + '/ajax/basket_list.php' + query_string;
			
			g_request = create_xml_http_request();	
			g_request.onreadystatechange = basket_list_response;
			g_request.open("POST",url,true);
			g_request.send(null);	
		}
	}
	return result;
}


/********************************************************************************
 * Popup: Basket
 * =============
 */
 
var g_popup_indexes = null;

function open_popup_basket()
{
	var result = true;
	if( !is_IE6() )
	{
		result = false;
		popup_show_loading();
		g_request = create_xml_http_request();
		var url = g_root + "/ajax/basket_list.php";
		
		g_request.onreadystatechange = basket_list_response;
		g_request.open("GET",url,true);
		g_request.send(null);
	}
	return result;
}

function basket_list_response()
{
	if( g_request.readyState == 4 )
	{
		g_popup_indexes = new Array();
		var xml_doc = g_request.responseXML.documentElement;
		
		var redirect_url = get_xml_node( xml_doc, 'redirect_url' );
		if( redirect_url )
		{
			window.location = redirect_url;
			return;
		}
		
		var subtotal = xml_doc.getElementsByTagName( 'subtotal' )[ 0 ].childNodes[ 0 ].nodeValue;
		var num_items = xml_doc.getElementsByTagName( 'num_items' )[ 0 ].childNodes[ 0 ].nodeValue;
		var basket_items = xml_doc.getElementsByTagName( 'item' );
		
		var table = Builder.node( 'table', { className: 'table', style: 'width:698px;' } );
		
		var th1 = Builder.node( 'th', 'Item' );
		var th2 = Builder.node( 'th', 'Price' );
		th2.style.width = '79px';
		var th3 = Builder.node( 'th', 'Quantity' );
		th3.style.width = '79px';
		var th4 = Builder.node( 'th', 'Total' );
		th4.style.width = '79px';
		var th5 = Builder.node( 'th' );
		th5.style.width = '20px';
		
		table.appendChild( 
		Builder.node( 'tr', [
			th1, th2, th3, th4, th5
		] ) );
		
		if( basket_items.length > 0 )
		{
		
			var count = basket_items.length;
			for( var i = 0; i < count; i++ )
			{
				var basket_item = basket_items[ i ];
				
				var index = get_xml_node( basket_item, 'index' );
				var variation_id = get_xml_node( basket_item, 'variation_id' );
				var product_name = get_xml_node( basket_item, 'product_name' );
				var variation_name = get_xml_node( basket_item, 'variation_name' );
				var url = get_xml_node( basket_item, 'url' );
				var thumb = get_xml_node( basket_item, 'thumb' );
				var price = get_xml_node( basket_item, 'price' );
				var quantity = get_xml_node( basket_item, 'quantity' );
				var total = get_xml_node( basket_item, 'total' );
				
				var img = g_root + '/' + thumb
				
				g_popup_indexes[ i ] = index;

				var name_obj = '';
				if( variation_name )
				{
					name_obj = Builder.node( 'p', { className: 'checkout_item' }, [
							Builder.node( 'b', [
								Builder.node( 'a', { href: '' } ).update( product_name ),
							] ),
							Builder.node( 'br' ),
							Builder.node( 'span' ).update( variation_name )
						] );
				}
				else
				{
					name_obj = Builder.node( 'p', { className: 'checkout_item' }, [
							Builder.node( 'b', [
								Builder.node( 'a', { href: url } ).update( product_name )
							] )
						] );
				}

				var td = 
					Builder.node( 'td', [
						Builder.node( 'a', { href: 'shopping_basket_action.php?remove_index=' + index, onclick: 'basket_remove( ' + index + ' ); return false;' }, [
							Builder.node( 'img', { src: g_root + '/images/cross.gif', alt: 'Remove' } )
						] )
					] );
				
				td.style.textAlign = "center";
				
				table.appendChild(
					Builder.node( 'tr', [
						Builder.node( 'td', [
							Builder.node( 'a', { href: url }, [
								Builder.node( 'img', { className: 'checkout_item', src: img } )
							] ),
							Builder.node( 'div', { className: 'checkout_item' }, [
								name_obj
							] ),
						] ),
						Builder.node( 'td', [
							Builder.node( 'b' ).update( price )
						] ),
						Builder.node( 'td', [
							Builder.node( 'input', { className: 'text', type: 'text', id: 'popup_item_quantity_' + index, name: 'item_quantities[' + index + ']', style: 'width:50px', value: quantity, onkeyup: ' basket_update_quantity( this, ' + index + ' );' } )				 
						] ),
						Builder.node( 'td', [
							Builder.node( 'b' ).update( total )
						] ),
						td
					] )	);
			}
			
			var td1 = Builder.node( 'td', { className: 'total' }, [ Builder.node( 'strong', 'Subtotal' ) ] );
			td1.colSpan = 2;
			var td2 = Builder.node( 'td', { className: 'total' }, [ Builder.node( 'strong' ).update( subtotal ) ] );
			td2.colSpan = 2;
			
			table.appendChild( 
				Builder.node( 'tr', { className: 'blank' }, [
					Builder.node( 'td', { className: 'blank' } ),
					td1,
					td2
				] ) );
			
		}
		else
		{
			var td = Builder.node( 'td', 'There are currently no items in the basket.' );
			td.colSpan = 5;
			
			table.appendChild(
				Builder.node( 'tr', { className: 'error' }, [
					td
				] ) );
		}
		
		// get any messages
		var error_obj = basket_message_box( xml_doc.getElementsByTagName( 'error' ), 'error_box' );
		var success_obj = basket_message_box( xml_doc.getElementsByTagName( 'success' ), 'success_box' );
		var warning_obj = basket_message_box( xml_doc.getElementsByTagName( 'caution' ), 'warning_box' );
		
		var obj = Builder.node( 'form', { id: 'form_popup', name: 'form_popup', method:'post', action:'' }, [
				error_obj,
				success_obj,
				warning_obj,
				Builder.node( 'div', { className: 'form_box' }, [
					Builder.node( 'div', { className: 'form_box_content' }, [
						table
					] )
				] ),
			] );

		if( basket_items.length > 0 )
		{
			obj.appendChild( Builder.node( 'p', { className: 'notice_box' }, 'Proceed to the checkout to calculate delivery charges.' ) );
			obj.appendChild( Builder.node( 'input', { name: 'submit_checkout', className: 'button button_positive', type: 'submit', value: 'Proceed To Checkout', style: 'margin-bottom:0px', onclick: 'basket_update(\'' + g_root_secure +'/shopping_basket.php\'); return false;' } ) );
			obj.appendChild( Builder.node( 'input', { name: 'submit_update_basket', className: 'button', type: 'submit', value: 'Update Basket', style: 'margin-bottom:0px', onclick: 'basket_update(); return false;' } ) );
		}
		
		obj.appendChild(
			Builder.node( 'input', { name: 'submit_continue_shopping', className: 'button', type: 'submit', value: 'Continue Shopping', style: 'margin-bottom:0px', onclick: 'popup_close(); return false;' } )
		);

		popup_open( obj, 'Shopping Basket' );
		
		var str = ( num_items == 1 ) ? "is 1 item" : "are " + num_items + " items"; 
		
		obj = document.getElementById( 'basket_num_items' )
		if( obj )
		{
			obj.innerHTML = str;
		}
	}
}

function basket_update( redirect_url )
{
	var filename = location.href;
	filename = filename.substring(filename.lastIndexOf('/')+1);
	if( filename == "shopping_basket.php" )
	{
		document.form1.submit_update_basket.click(); 
	}
	else
	{
		var item_quantities = new Array();
		var str = "";
		
		for( var i = 0; i < g_popup_indexes.length; i++ )
		{
			index = g_popup_indexes[ i ];
			item_quantities[ index ] = document.getElementById( 'popup_item_quantity_' + index ).value;
			
			str += index + " => " + item_quantities[ index ] + ", ";
		}
		
		var url = g_root + '/ajax/basket_list.php?submit_update_basket=1&item_quantities=' + escape( item_quantities );
		if( redirect_url )
		{
			url += "&redirect_url=" + escape( redirect_url );	
		}
		
		g_request = create_xml_http_request();	
		g_request.onreadystatechange = basket_list_response;
		g_request.open("POST",url,true);
		g_request.send(null);
	}
}

function basket_remove( index )
{
	popup_show_loading();
	
	var filename = location.href;
	filename = filename.substring(filename.lastIndexOf('/')+1);
	
	// If we are already on the shopping basket page we cannot use ajax so need to use the url remove instead
	if( filename == "shopping_basket.php" )
	{
		var url = g_root + '/shopping_basket_action.php?remove_index=' + index;
		window.location = url;
	}
	else
	{
		var url = g_root + '/ajax/basket_list.php?remove_index=' + index;
		g_request = create_xml_http_request();	
		g_request.onreadystatechange = basket_list_response;
		g_request.open("POST",url,true);
		g_request.send(null);
	}
}

function basket_message_box( messages, class_name )
{
	var obj = '';
	if( messages.length > 0 )
	{
		if( messages.length == 1 )
		{
			var message = messages[ 0 ].childNodes[ 0 ].nodeValue;
			obj = Builder.node( 'p', { className: class_name } ).update( message );
		}
		else
		{
			obj = Builder.node( 'p', { className: class_name } );
			var ul = Builder.node( 'ul' );
			var count = messages.length;
			for( var i = 0; i < count; i++ )
			{
				var message = messages[ i ].childNodes[ 0 ].nodeValue;
				ul.appendChild( Builder.node( 'li' ).update( message ) );
			}
			obj.appendChild( ul );
		}
	}
	return obj;
}

function basket_update_quantity( obj, index )
{
	var div = null;

	if( div = document.getElementById( 'popup_item_quantity_' + index ) )
	{
		div.value = obj.value;
	}
	
	if( div = document.getElementById( 'item_quantity_' + index ) )
	{
		div.value = obj.value;
	}
}

/********************************************************************************
 * Popup: Background
 * =================
 */

function open_popup_background()
{
	popup_show_loading();
	g_request = create_xml_http_request();
	var url = g_root + "/ajax/background_list.php";
	
	g_request.onreadystatechange = background_list_response;
	g_request.open("GET",url,true);
	g_request.send(null);	
}

function background_list_response()
{
	if( g_request.readyState == 4 )
	{
		var xml_doc = g_request.responseXML.documentElement;
		
		var div = Builder.node( 'div', { className: 'image_scroll' } );
		
		var backgrounds = xml_doc.getElementsByTagName( 'background' );
		var num_backgrounds = backgrounds.length;
		
		if( num_backgrounds > 0 )
		{
			for( var i = 0; i < num_backgrounds; i++ )
			{
				var background = backgrounds[ i ];
				var background_id = background.getElementsByTagName( 'background_id' )[ 0 ].childNodes[ 0 ].nodeValue;
				var title = background.getElementsByTagName( 'title' )[ 0 ].childNodes[ 0 ].nodeValue;
				var thumb = background.getElementsByTagName( 'thumb' )[ 0 ].childNodes[ 0 ].nodeValue;
				var date_posted = background.getElementsByTagName( 'date_posted' )[ 0 ].childNodes[ 0 ].nodeValue;
				
				div.appendChild( Builder.node( 'div', { className: 'image_scroll_thumb' }, [
					Builder.node( 'p', [
						Builder.node( 'a', { href: g_root + '/background_action.php?background_id=' + background_id }, [ 
							Builder.node( 'img', { src: thumb, alt: title, className: 'border' } )
						] )
					] ),
					Builder.node( 'h2', title ),
					Builder.node( 'p', { className: 'small' }, [
						Builder.node( 'b', 'Posted: ' + date_posted )
					] )
				] ) );	
			}
			
			var obj = Builder.node( 'div', [
				Builder.node( 'p', { className: 'small' }, [
					Builder.node( 'b', 'Click on an image below to change your background.' ),
				] ),
				div,
				Builder.node( 'input', { name: 'submit_background', className: 'button', type: 'submit', value: 'Restore Default', style: 'margin-bottom:0px', onclick: 'window.location="' + g_root + '/background_action.php"' } )
			] );
		}
		else
		{
			var obj = Builder.node( 'p', { className: 'small' }, [
				Builder.node( 'b', 'There are currently no alternative backgrounds available.' ),
			] );
		}
		
		popup_open( obj, 'Change Background&hellip;' );
	}
}

/********************************************************************************
 * Popup: Background
 * =================
 */

function open_popup_guide( category_id )
{
	popup_show_loading();
	g_request = create_xml_http_request();
	var url = g_root + "/ajax/guide.php?category_id=" + category_id;
	
	g_request.onreadystatechange = guide_response;
	g_request.open("GET",url,true);
	g_request.send(null);	
}

function guide_response()
{
	if( g_request.readyState == 4 )
	{
		var xml_doc = g_request.responseXML.documentElement;
		
		var company_name = get_xml_node( xml_doc, 'company_name' );
		var category_name = get_xml_node( xml_doc, 'category_name' );
		var description = get_xml_node( xml_doc, 'description' );
		
		var div = Builder.node( 'div', { className: 'popup_scroll' } ).update( description );
		
		var obj = Builder.node( 'div', [
			div,
			Builder.node( 'input', { name: 'submit_close', className: 'button', type: 'submit', value: 'Close', style: 'margin-bottom:0px', onclick: 'popup_close(); return false;' } )
		] );
		
		
		popup_open( obj, company_name + "'s Product Guide : " + category_name );
	}
}

/********************************************************************************
 * Update Checkout
 * ===============
 */

function update_checkout( query_string )
{
	popup_show_loading();
	g_request = create_xml_http_request();
	var url = g_root + "/ajax/checkout_list.php";
	if( query_string ) url += '?' + query_string;

	g_request.onreadystatechange = checkout_list_response;
	g_request.open("GET",url,true);
	g_request.send(null);		
}

function checkout_list_response()
{
	if( g_request.readyState == 4 )
	{
		var xml_doc = g_request.responseXML.documentElement;
		
		var country_id = get_xml_attribute( xml_doc, 'country', 'id' );
		var has_postcodes = parseInt( get_xml_attribute( xml_doc, 'country', 'postcodes' ) );
		// update the drop down menu
		
		var sel = document.getElementById( 'country_id' );
		if( sel )
		{
			for( var i = 0; i < sel.options.length; i++ )
			{
				if( sel.options[ i ].value == country_id )
				{
					sel.selectedIndex = i;	
				}
			}
			
			var flag = get_xml_attribute( xml_doc, 'country', 'flag' );
			var country_name = get_xml_node( xml_doc, 'country' );
			
			var country_flag = document.getElementById( 'country_flag' );
			country_flag.src = flag;
			country_flag.alt = country_name;
			
			document.getElementById( 'toolbar_country_flag' ).src = flag;
			document.getElementById( 'toolbar_country_name' ).innerHTML = country_name;
		}
		
		var postcode = get_xml_node( xml_doc, 'postcode' );
		
		var postage_header = document.getElementById( 'postage_header' );
		
		var header_str = "Postage Options";
		if( postcode )
		{
			 header_str += " for " + postcode;
		}
		
		postage_header.innerHTML = header_str;
		
		var span_postcodes = document.getElementById( 'span_postcodes' );
		
		if( span_postcodes )
		{
			var display = has_postcodes ? "block" : "none";
			span_postcodes.style.display = display;
			
			var obj = null;
			var inputs = null;
			
			// clear the existing postcodes div
			var span_existing_postcode = document.getElementById( 'span_existing_postcode' );
			if( span_existing_postcode ) span_existing_postcode.style.display = "none";
			
			var existing_postcode = document.getElementById( 'existing_postcode' );
			if( existing_postcode )
			{
				existing_postcode.options.length = 1;
				existing_postcode.selectedIndex = 0;
				
				disable_inputs( 'span_existing_postcode' );
			}
			
			// clear the new postcode div
			var span_new_postcode = document.getElementById( 'span_new_postcode' );
			if( span_new_postcode )
			{
				span_new_postcode.style.display = "none";
				disable_inputs( 'span_new_postcode' );
			}
			
			var new_postcode = document.getElementById( 'new_postcode' );
			if( new_postcode ) new_postcode.value = "Specify a new postcode...";
			
			if( has_postcodes )
			{
				// show the existing postcodes
				var num_postcodes = 0;
				var existing_postcodes = xml_doc.getElementsByTagName( 'existing_postcode' );
				if( existing_postcodes )
				{
					var num_postcodes = xml_doc.getElementsByTagName( 'existing_postcode' ).length;
					if( num_postcodes )
					{
						for( var i = 1; i <= num_postcodes; i++ )
						{
							var postcode_text = get_xml_node( xml_doc, 'existing_postcode', i - 1 );
							
							existing_postcode.options[ i ]= new Option( postcode_text );
							
							if( postcode == postcode_text )
							{
								existing_postcode.selectedIndex = i;	
							}
						}
						enable_inputs( 'span_existing_postcode' );
						span_existing_postcode.style.display = "block";
					}
				}
					
				// show the new postcode
				var new_postcode_value = get_xml_node( xml_doc, 'new_postcode' );
				if( new_postcode_value )
				{
					new_postcode.value = new_postcode_value;
				}
				enable_inputs( 'span_new_postcode' );
				span_new_postcode.style.display = "block";
			}
		}
		
		// all the postage stuff goes here..
		// hide everything
		document.getElementById( 'span_postage_options' ).style.display = "none";
		disable_inputs( 'span_postage_options' );
		
		var postage_price_id = document.getElementById( 'postage_price_id' );
		postage_price_id.options.length = 1;
		
		document.getElementById( 'postage_message' ).style.display = "none";
		document.getElementById( 'postcode_message' ).style.display = "none";
		document.getElementById( 'quotation_message' ).style.display = "none";
		
		var postage_options = xml_doc.getElementsByTagName( 'postage_option' );
		var num_postage_options = postage_options.length;
		
		if( has_postcodes && !postcode )
		{
			document.getElementById( 'postcode_message' ).style.display = "block";
		}
		else if( !num_postage_options )
		{
			document.getElementById( 'quotation_message' ).style.display = "block";	
		}
		else
		{
			// add all the options to the select
			for( var i = 1; i <= num_postage_options; i++ )
			{
				var postage_text = get_xml_node( xml_doc, 'postage_option', i - 1 );
				var postage_id = get_xml_attribute( xml_doc, 'postage_option', 'id', i - 1 );
				
				postage_price_id.options[ i ]= new Option( postage_text, postage_id );
				
				if( get_xml_attribute( xml_doc, 'postage_option', 'selected', i - 1 ) )
				{
					postage_price_id.selectedIndex = i;	
				}
			}
			enable_inputs( 'span_postage_options' );
			document.getElementById( 'span_postage_options' ).style.display = "block";
			
			var postage_message = get_xml_node( xml_doc, 'postage_message' );
			if( postage_message )
			{
				document.getElementById( 'postage_message' ).innerHTML = postage_message;	
				document.getElementById( 'postage_message' ).style.display = "block";
			}
		}
		
		if( span_postcodes )
		{
			// update all of the item prices
			var items = xml_doc.getElementsByTagName( 'item' );
			for( var i = 0; i < items.length; i++ )
			{
				var order_item = items[ i ];
				var index = get_xml_attribute( xml_doc, 'item', 'index', i );
				document.getElementById( 'item_price_' + index ).innerHTML = get_xml_node( order_item, 'item_price' ); 
				document.getElementById( 'item_total_' + index ).innerHTML = get_xml_node( order_item, 'item_total' ); 
			}
			
			
			// update all of the discount prices
			var discounts = xml_doc.getElementsByTagName( 'discount' );
			for( var i = 0; i < discounts.length; i++ )
			{
				var order_discount = discounts[ i ];
				var index = get_xml_attribute( xml_doc, 'discount', 'index', i );
				document.getElementById( 'discount_price_' + index ).innerHTML = get_xml_node( order_discount, 'discount_price' ); 
				document.getElementById( 'discount_total_' + index ).innerHTML = get_xml_node( order_discount, 'discount_total' ); 
			}
		}
		
		if( document.getElementById( 'item_subtotal' ) )
		{
			document.getElementById( 'item_subtotal' ).innerHTML = get_xml_node( xml_doc, 'subtotal' );
		}
		document.getElementById( 'subtotal' ).innerHTML = get_xml_node( xml_doc, 'subtotal' );
		document.getElementById( 'postage' ).innerHTML = get_xml_node( xml_doc, 'postage' );
		
		if( document.getElementById( 'surcharge' ) )
		{
			document.getElementById( 'surcharge' ).innerHTML = get_xml_node( xml_doc, 'surcharge' );	
		}
		
		document.getElementById( 'tax' ).innerHTML = get_xml_node( xml_doc, 'tax' );
		document.getElementById( 'total' ).innerHTML = get_xml_node( xml_doc, 'total' );
		
		popup_kill();
	}
}

function disable_inputs( id )
{
	var inputs = array_merge(	document.getElementById( id ).getElementsByTagName( 'input' ),
								document.getElementById( id ).getElementsByTagName( 'select' ) );
	
	for( var i = 0; i < inputs.length; i++ )
	{
		inputs[ i ].disabled = true;
	}
}

function enable_inputs( id )
{
	var inputs = array_merge(	document.getElementById( id ).getElementsByTagName( 'input' ),
								document.getElementById( id ).getElementsByTagName( 'select' ) );
	
	for( var i = 0; i < inputs.length; i++ )
	{
		inputs[ i ].disabled = false;
	}
}





