// Shop JavaScript Document

var xmlReqs = new Array();
var elementTarget;
var targetDiv = new Array();
var xmlReqsID;

var shopDir = "shop";
var basketDir = shopDir+"/basket";
var quickBasketDir = shopDir+"/quick-basket";

/**
* Handles a general HTTP response
*/
function handleRequestStateChange() {
	
	for (i = 0; i < xmlReqs.length; i++) {
		
		if (xmlReqs[i]) {
			// get the document element
			myDiv = document.getElementById(targetDiv[i]);
			
			// Display the status of the request
			if (xmlReqs[i].readyState == 4) {
				try {
					// Read the message from the server
					response = xmlReqs[i].responseText;
					
					// Display the message
					myDiv.innerHTML = response;
				}
				
				catch(e) {}
			}
		}
	}
}



/**
* Creates an XMLHttpRequest instance
*/
function createXMLHttpRequestObject() {
	// Will store the reference to the XMLHttpRequest object
	var xmlHttp;
	
	// Not for IE6 and older
	try {
		// Try to create the object
		xmlHttp = new XMLHttpRequest();
	}
	
	catch(e) {
		// Assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		
		// Try every prog id until one works
		for (var i=0; i < XmlHttpVersions.length && !xmlHttp; i ++) {
			try {
				// Try to create the object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			
			catch(e) {}
		}
	}
	
	// Return the created object or display an error message
	if (!xmlHttp) {
		alert ("Error creating the XMLHttpRequest object");
	}
	
	else {
		return xmlHttp;
	}
}



/**
* updates the quick search box
*/
function quick_search(search_string) {
	xmlReqs = new Array();
	xmlReqs[0] = createXMLHttpRequestObject();
	
	document.getElementById('search-details').style.display = 'block';
	
	// Only continue if xmlHttp isn't void
	if (xmlReqs[0]) {
		// Try to connect to the server
		try {
			// Initiate retreival of the data
			targetDiv[0] = 'search-details';
			xmlReqs[0].open("GET", "/"+shopDir+"/search/index.php?action=quick-search&search="+search_string, true);
			xmlReqs[0].onreadystatechange = handleRequestStateChange;
			xmlReqs[0].send(null);
		}
		
		catch(e) {}
	}
}



/**
* Handles a general HTTP response for updating a cart then displays the new cart total
*/
function handleCartUpdate() { 
	
	for (i = 0; i < xmlReqs.length; i++) {
		// get the document element
		myDiv = document.getElementById(targetDiv[i]);
		
		// Display the status of the request
		if (xmlReqs[i].readyState == 4) {
			try {
				// Read the message from the server
				response = xmlReqs[i].responseText;
				
				// Display the message
				myDiv.innerHTML = response;
				
				myDiv.style.display = 'block';
				myDiv.className = 'basket-actions-hover';
				
				// Display the new cart total
				xmlReqs[1] = createXMLHttpRequestObject();
				
				targetDiv[1] = 'basket-total';
				xmlReqs[1].open("GET", "/"+basketDir+"/templates/header-cart-details.php", true);
				xmlReqs[1].onreadystatechange = handleRequestStateChange;
				xmlReqs[1].send(null);
				
				// Move to the top of the screen to display basket message and highlight
				window.location = "#top";
				basket = document.getElementById('shopping-basket');
				basket.style.background = "#0054a4 url('/assets/images/header/basket-bg.jpg') no-repeat top left";
			}
			
			catch(e) {}
		}
	}
}


/**
* adds an item to the shopping cart
*/
function add_to_cart(brand_url, item_url, quantity) {	
	// Set the querystring based on the product selections
	var queryString = "";
	var errorCount = false;
	
	// Loop thrugh each form element only if the item has any options.
	if (document.getElementById('has_selection') != null) {
		for(i = 0; i < document.item_selections.elements.length; i ++) {
			// Only check select box values
			if(document.item_selections.elements[i].type == "select-one") {
				if (document.item_selections.elements[i][document.item_selections.elements[i].selectedIndex].value != 0) {
					// Pass the value to a variable to make coding easier.
					queryString += "&"+document.item_selections.elements[i].name+"="+document.item_selections.elements[i][document.item_selections.elements[i].selectedIndex].value;
				}
				
				else {
					errorCount = true;
				}
			}
		}
	}
	
	// Update the cart if all selections were made
	if (!errorCount) {
		xmlReqs = new Array();
		xmlReqs[0] = createXMLHttpRequestObject();
		
		// Only continue if xmlHttp isn't void
		if (xmlReqs[0]) {
			// Try to connect to the server
			try {			
				// Initiate retreival of the data
				targetDiv[0] = 'basket-actions';
				xmlReqs[0].open("GET", "/"+quickBasketDir+"/index.php?action=add-to-basket&brand="+brand_url+"&item="+item_url+"&quantity="+quantity+queryString, true);
				xmlReqs[0].onreadystatechange = handleCartUpdate;
				xmlReqs[0].send(null);
			}
			
			catch(e) {}
		}
	}
	
	else {
		xmlReqs = new Array();
		xmlReqs[0] = createXMLHttpRequestObject();
		
		// Only continue if xmlHttp isn't void
		if (xmlReqs[0]) {
			// Try to connect to the server
			try {			
				// Initiate retreival of the data
				targetDiv[0] = 'cart-options-message';
				xmlReqs[0].open("GET", "/"+quickBasketDir+"/index.php?action=add-to-basket-error", true);
				xmlReqs[0].onreadystatechange = handleRequestStateChange;
				xmlReqs[0].send(null);
			}
			
			catch(e) {}
		}
	}
}



/**
*
*/
function update_price(currency_symbol, current_value) {
	// Set the initial price value of the item.
	//var current_value = parseFloat(document.item_selections.initial_item_price.value);
	var running_total = 0;
	
	// Loop thrugh each form element.
	for(i = 0; i < document.item_selections.elements.length; i ++) {
		// Only check select box values
		if(document.item_selections.elements[i].type == "select-one") {
			// Pass the value to a variable to make coding easier.
			text_value = document.item_selections.elements[i][document.item_selections.elements[i].selectedIndex].text;
			
			// Get the price ammendment of a selection.
			pos = text_value.indexOf('+ ' + currency_symbol);
			
			if (pos) prefix = "+";
			if (pos == -1) {
				pos = text_value.indexOf('- ' + currency_symbol);
				if (pos) prefix = "-";
			}
			
			// If a price ammendment is found.
			if (pos != -1) {
				ammend_value = parseFloat(text_value.substring((pos + 3), text_value.length));
				
				// Ammend the item price.
				if (prefix == "+") running_total = running_total + ammend_value;
				if (prefix == "-") running_total = running_total - ammend_value;
			}
		}
	}
	total_value = current_value + running_total;
	
	myDiv = document.getElementById('item-price');
	myDiv.innerHTML = currency_symbol+add_commas(total_value.toFixed(2));
}



/**
* Adds commas to a number
*/
function add_commas(num) {
	num += '';
	x = num.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}



/**
* Get the mouse coordinates
*/
function set_scroll_position(myElement) {
	parentElement = document.getElementById('product-display-options');
	parentElementTop = parentElement.offsetTop;
	
	var myElementTop = 0;
	
	if (myElement.offsetParent) {
		do {
			myElementTop += myElement.offsetTop;
		}
		
		while (myElement = myElement.offsetParent);
	}
	
	scrollDiv = document.getElementById('filter-indicator');
	scrollDiv.style.display = 'block';
	scrollDiv.style.top = ((myElementTop - parentElementTop) - (scrollDiv.offsetHeight / 2)) + 'px';	
}




/**
* Gets a count of products returned from a filter search
*/
function get_filter_count(myElement, section, category, brand, search_term) {
	xmlReqs = new Array();
	xmlReqs[0] = createXMLHttpRequestObject();
	
	// Only continue if xmlHttp isn't void
	if (xmlReqs[0]) {
		// Try to connect to the server
		try {
			// Set the post parameters
			field_list 	= document.attribute_list.getElementsByTagName('input');
			
			calling_url = document.getElementById('filters[calling_url]');
			parameters 	= "filters[calling_url]="+calling_url.value;
			
			for (i = 0; i < field_list.length; i ++) {
				if (field_list[i].checked || field_list[i].type == 'hidden') parameters += "&"+field_list[i].name+"="+encodeURI(field_list[i].value);
			}
			
			// Initiate retreival of the data
			targetDiv[0] = 'filter-count';
			if (document.getElementById('filter-count-categories')) targetDiv[targetDiv.length] = 'filter-count-categories';
			if (document.getElementById('filter-count-brands')) targetDiv[targetDiv.length] = 'filter-count-brands';
			if (document.getElementById('filter-count-price')) targetDiv[targetDiv.length] = 'filter-count-price';
			if (document.getElementById('filter-count-attributes')) targetDiv[targetDiv.length] = 'filter-count-attributes';
			
			xmlReqs[0].open("POST", "/"+shopDir+"/"+section+"/index.php?action=get-filter-count&category="+category+"&brand="+brand+"&search="+search_term, true);
			xmlReqs[0].onreadystatechange = handleFilterCount;
			xmlReqs[0].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlReqs[0].setRequestHeader("Content-length", parameters.length);
			xmlReqs[0].setRequestHeader("Connection", "close");
			xmlReqs[0].send(parameters);
			
			//set_scroll_position(myElement);
		}
		
		catch(e) {}
	}
}



/**
* Handles the updating of a filter search count
*/
function handleFilterCount() {
	if (xmlReqs[0]) {
		
		// Display the status of the request
		if (xmlReqs[0].readyState == 4) {
			try {
				// Read the message from the server
				response = xmlReqs[0].responseText;
				// Display the message
				for (d = 0; d < targetDiv.length; d++) {
					myDiv = document.getElementById(targetDiv[d]);
					myDiv.innerHTML = response;
				}
			}
			
			catch(e) {}
		}
	}
}



/**
* Updates the delivery methods for the checkout page
*/
function update_delivery_methods(country) {
	xmlReqs = new Array();
	xmlReqs[0] = createXMLHttpRequestObject();
	
	// Only continue if xmlHttp isn't void
	if (xmlReqs[0]) {
		// Try to connect to the server
		try {
			if (!country || typeof(country) != "number") country = get_checked_country();
			
			// Initiate retreival of the data
			targetDiv[0] = 'delivery-selection';
			xmlReqs[0].open("GET", "/"+quickBasketDir+"/index.php?action=get-delivery-options&country="+country, true);
			xmlReqs[0].onreadystatechange = update_checkout_totals;
			xmlReqs[0].send(null);
		}
		
		catch(e) {}
	}
}



/**
* Gets the id of the set delivery country
*/
function get_checked_country() {
	return document.checkout_form.delivery_country[document.checkout_form.delivery_country.selectedIndex].value; 
}




/**
* Determines the id of the checked delivery required
*/
function get_checked_delivery() {
	// If there is more than one option determine which one is checked
	if (document.checkout_form.delivery_method.length > 0) {
		for (i = 0; i < document.checkout_form.delivery_method.length; i ++ ) {
			if (document.checkout_form.delivery_method[i].checked) delivery = document.checkout_form.delivery_method[i].value;
		}
	}
	
	// Else return the only option in the list
	else {
		delivery = document.checkout_form.delivery_method.value;
	}
	
	return delivery;
}



/**
* Determines the id of the checked payment
*/
function get_checked_payment() {
	// If there is more than one option determine which one is checked
	if (document.checkout_form.payment_method.length > 0) {
		for (i = 0; i < document.checkout_form.payment_method.length; i ++ ) {
			if (document.checkout_form.payment_method[i].checked) payment = document.checkout_form.payment_method[i].value;
		}
	}
	
	// Else return the only option in the list
	else {
		payment = document.checkout_form.payment_method.value;
	}
	
	return payment;
}



/**
* Updates the totals on the checkout page
*/
function update_checkout_totals() {
	for (i = (xmlReqs.length - 1); i < xmlReqs.length; i++) {
		
		// get the document element
		myDiv = document.getElementById(targetDiv[i]);
		
		
		// Display the status of the request
		if (xmlReqs[i].readyState == 4) {
			try {				
				// Read the message from the server
				response = xmlReqs[i].responseText;
				
				// Display the message
				myDiv.innerHTML = response;
				myDiv.style.display = 'block';
				
				// Determine the set country
				country = get_checked_country();
				
				// Determine the set delivery
				delivery = get_checked_delivery();
				
				// Get the checked merchant
				merchant = get_checked_payment();
				
				// Display the new cart total
				xmlID = xmlReqs.length;
				xmlReqs[xmlID] = createXMLHttpRequestObject();
				
				targetDiv[xmlID] = 'checkout-order-totals';
				xmlReqs[xmlID].open("GET", "/"+quickBasketDir+"/index.php?action=update-checkout-totals&country="+country+"&delivery="+delivery, true);
				xmlReqs[xmlID].onreadystatechange = set_merchant; //handleRequestStateChange;
				xmlReqs[xmlID].send(null);
			}
			
			catch(e) {}
		}
	}
}



/**
* Updates the cart totals from a delivery selection
*/
function update_checkout_delivery(delivery) {	
	xmlReqs = new Array();
	xmlReqs[0] = createXMLHttpRequestObject();
	
	// Only continue if xmlHttp isn't void
	if (xmlReqs[0]) {
		// Try to connect to the server
		try {
			// Determine the set country
			country = get_checked_country();
			
			// Determine the set country
			if (!delivery) delivery = get_checked_delivery();
			
			// Initiate retreival of the data
			targetDiv[0] = 'checkout-order-totals';
			xmlReqs[0].open("GET", "/"+quickBasketDir+"/index.php?action=update-checkout-totals&country="+country+"&delivery="+delivery, true);
			xmlReqs[0].onreadystatechange = set_merchant; //handleRequestStateChange;
			xmlReqs[0].send(null);
		}
		
		catch(e) {}
	}
}



/**
* Verfies a voucher code
*/
function verify_voucher_code(voucher_code) {
	xmlReqs = new Array();
	xmlReqs[0] = createXMLHttpRequestObject();
	
	// Only continue if xmlHttp isn't void
	if (xmlReqs[0]) {
		// Try to connect to the server
		try {
			// Initiate retreival of the data
			targetDiv[0] = 'voucher-details';
			xmlReqs[0].open("GET", "/"+quickBasketDir+"/index.php?action=verify-voucher-code&voucher-code="+voucher_code, true);
			xmlReqs[0].onreadystatechange = update_checkout_totals;
			xmlReqs[0].send(null);
		}
		
		catch(e) {}
	}
}



/**
* Removes a voucher code
*/
function remove_voucher_code() {
	xmlReqs = new Array();
	xmlReqs[0] = createXMLHttpRequestObject();
	
	// Only continue if xmlHttp isn't void
	if (xmlReqs[0]) {
		// Try to connect to the server
		try {
			// Initiate retreival of the data
			targetDiv[0] = 'voucher-details';
			xmlReqs[0].open("GET", "/"+quickBasketDir+"/index.php?action=remove-voucher-code", true);
			xmlReqs[0].onreadystatechange = update_checkout_totals;
			xmlReqs[0].send(null);
		}
		
		catch(e) {}
	}
}



/**
* Sets where the checkout form gets posted to
*/
function set_checkout_action(form_action, form_label) {
	// Set the form submit label
	button = document.getElementById('checkout_action[2]');
	button.value = form_label;
	
	// Set the default form action
	document.checkout_form.action = form_action;
}



/**
* Sets the merchant fields
*/
function set_merchant_fields() {
	// Get the default merchant
	merchant = get_checked_payment();
	
	// Get the selected country
	country = get_checked_country();
	
	// Get the selected delivery
	delivery = get_checked_delivery();
	
	// Initiate retreival of the data
	xmlReqs[xmlID] = createXMLHttpRequestObject();
	targetDiv[xmlID] = 'merchant-hidden-fields';
	xmlReqs[xmlID].open("GET", "/"+quickBasketDir+"/index.php?action=display-merchant-fields&merchant="+merchant+"&country="+country+"&delivery="+delivery, true);
	xmlReqs[xmlID].onreadystatechange = handleRequestStateChange;
	xmlReqs[xmlID].send(null);	
}



/**
* Sets the merchant details
*/
function set_merchant(previous_not_required) {	
	// Get the default merchant
	merchant = get_checked_payment();
	
	// get the document element
	myDiv = document.getElementById(targetDiv[(xmlReqs.length - 1)]);
	
	// Only update if calling an update
	if (previous_not_required != "true") {
		
		// Display the status of the request
		if (xmlReqs[(xmlReqs.length - 1)].readyState == 4) {
			try {
				// Read the message from the server
				response = xmlReqs[(xmlReqs.length - 1)].responseText;
				
				// Display the message
				myDiv.innerHTML = response;
				
				myDiv.style.display = 'block';
				
				xmlID = xmlReqs.length;
				
				try {
					set_merchant_fields();
				}
				
				catch(e) {}
			}
			
			catch(e) {}
		}
	}
	
	else {
		xmlID = 0;
		set_merchant_fields();	
	}
	
	// Update the form action
	action_field 	= document.getElementById('payment_url_'+merchant);
	label_field 	= document.getElementById('payment_label_'+merchant);
	form_action 	= action_field.value;
	form_label		= label_field.value
	
	// Set the form action
	set_checkout_action(form_action, form_label);
}



/**
* Validates the checkout form, if ok submits the form
*/
function validate_checkout_form() {
	var errors = new Array();
	
	// Validate the delivery details
	if (document.checkout_form.delivery_firstname.value == "" || document.checkout_form.delivery_lastname.value == "" 
		|| document.checkout_form.delivery_address == "" || document.checkout_form.delivery_town == "" 
		|| document.checkout_form.delivery_postcode == "" || document.checkout_form.delivery_email == "" 
		|| document.checkout_form.delivery_telephone == "") {
		
		errors['delivery'] = 'true';
	}
	
	// Validate the billing details
	if (document.checkout_form.billing_firstname.value == "" || document.checkout_form.billing_lastname.value == "" 
		|| document.checkout_form.billing_address == "" || document.checkout_form.billing_town == ""
		|| document.checkout_form.billing_postcode == "") {
		
		errors['billing'] = 'true';
	}
	
	// If there are errors then display a message
	if (errors['delivery'] || errors['billing']) {
		display_checkout_errors(errors);
	}
	
	// Else submit the form
	else {
		xmlReqs = new Array();
		xmlReqs[0] = createXMLHttpRequestObject();
		
		// Only continue if xmlHttp isn't void
		if (xmlReqs[0]) {
			// Try to connect to the server
			try {
				// Set the form parameters
				parameters 	= "order_ref="+encodeURI(document.checkout_form.order_ref.value);
				parameters += "&delivery_firstname="+encodeURI(document.checkout_form.delivery_firstname.value);
				parameters += "&delivery_lastname="+encodeURI(document.checkout_form.delivery_lastname.value);
				parameters += "&delivery_company="+encodeURI(document.checkout_form.delivery_company.value);
				parameters += "&delivery_address="+encodeURI(document.checkout_form.delivery_address.value);
				parameters += "&delivery_town="+encodeURI(document.checkout_form.delivery_town.value);
				parameters += "&delivery_county="+encodeURI(document.checkout_form.delivery_county.value);
				parameters += "&delivery_country="+encodeURI(document.checkout_form.delivery_country[document.checkout_form.delivery_country.selectedIndex].value);
				parameters += "&delivery_postcode="+encodeURI(document.checkout_form.delivery_postcode.value);
				parameters += "&delivery_email="+encodeURI(document.checkout_form.delivery_email.value);
				parameters += "&delivery_telephone="+encodeURI(document.checkout_form.delivery_telephone.value);
							
				parameters += "&billing_firstname="+encodeURI(document.checkout_form.billing_firstname.value);
				parameters += "&billing_lastname="+encodeURI(document.checkout_form.billing_lastname.value);
				parameters += "&billing_company="+encodeURI(document.checkout_form.billing_company.value);
				parameters += "&billing_address="+encodeURI(document.checkout_form.billing_address.value);
				parameters += "&billing_town="+encodeURI(document.checkout_form.billing_town.value);
				parameters += "&billing_county="+encodeURI(document.checkout_form.billing_county.value);
				parameters += "&billing_country="+encodeURI(document.checkout_form.billing_country[document.checkout_form.billing_country.selectedIndex].value);
				parameters += "&billing_postcode="+encodeURI(document.checkout_form.billing_postcode.value);
				
				delivery_value = get_checked_delivery();
				parameters += "&delivery_method="+encodeURI(delivery_value);
				
				payment_value = get_checked_payment();
				parameters += "&payment_method="+encodeURI(payment_value);
				
				// Initiate retreival of the data
				//targetDiv[0] = 'checkout-order-totals';
				targetDiv[0] = 'merchant-hidden-fields';
				xmlReqs[0].open("POST", "/"+quickBasketDir+"/index.php?action=insert-order", true);
				xmlReqs[0].onreadystatechange = send_checkout_form;
				xmlReqs[0].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlReqs[0].setRequestHeader("Content-length", parameters.length);
				xmlReqs[0].setRequestHeader("Connection", "close");
				xmlReqs[0].send(parameters);
			}
			
			catch(e) {}
		}
	}
}



/**
* Submits the checkout form
*/
function send_checkout_form() {
	if (xmlReqs[0].readyState == 4) {
		try {
			// Read the message from the server
			response = xmlReqs[0].responseText;
			
			// get the document element
			myDiv = document.getElementById(targetDiv[0]);
			
			// Display the message
			myDiv.innerHTML = response;
			
			// Send the form
			send_form('checkout_form');
		}
		
		catch(e) {}
	}
}



/**
* Displays any checkout form errors
*/
function display_checkout_errors(errors) {
	xmlReqs = new Array();
	xmlReqs[0] = createXMLHttpRequestObject();
	
	// Only continue if xmlHttp isn't void
	if (xmlReqs[0]) {
		// Try to connect to the server
		try {
			// Initiate retreival of the data
			targetDiv[0] = 'form-errors';
			xmlReqs[0].open("GET", "/"+quickBasketDir+"/index.php?action=checkout-form-errors&delivery="+errors['delivery']+"&billing="+errors['billing'], true);
			xmlReqs[0].onreadystatechange = handleRequestStateChange;
			xmlReqs[0].send(null);
			
			myDiv = document.getElementById(targetDiv[0]);
			myDiv.style.display = 'block';
		}
		
		catch(e) {}
	}
}



/**
* Sends a form
*/
function send_form(form_name) {
	the_form = document.getElementById(form_name);
	the_form.submit();
}



/**
* Hides a DOM element
*/
function hide_element(id) {
	if (document.getElementById(id)) document.getElementById(id).style.display = 'none';
}



/**
* Hides all known DOM elements, e.g. cart, search bar, etc
*/
function hide_all_elements() {
	hideElementArray = new Array();
	
	hideElementArray[0] = "search-details";
	hideElementArray[1] = "form-errors";
	//elementArray[2] = "basket-actions";
	
	for (i = 0; i < hideElementArray.length; i++) {
		hide_element(hideElementArray[i]);
	}
	
	// remove the shopping basket highlight
	basket = document.getElementById('basket-actions');
	basket.innerHTML = '';
	basket.className = 'basket-actions';
}



/**
* Sets all checkboxes for a selected attribute
*/
function set_attribute_list(grp, status) { //(attribute_type, status) {
	//document.attribute_list.elements['attributes['+attribute_type+'][4]'].checked = status;
		
	var len = document.attribute_list.elements.length;
	var elm = null;
	
	for (var i=0; i<len; ++i) {
		elm = document.attribute_list.elements[i];
		if ((elm.name) && (elm.name.indexOf(grp) != -1)) {
			elm.checked = status;
		}
	}
}



/**
* Hides all product item details then reveals the selected content
*/
function display_item_details(divID) {
	// Loop through the detail tabs
	childTabList = document.getElementById('tabs').getElementsByTagName('li');
	for (c = 0; c <= document.getElementById('tabs').getElementsByTagName('li').length; c++) {
		if (childTabList[c]) {
			document.getElementById(childTabList[c].id).className = '';
		}
	}
	
	// Loop through the detail content divs
	childContentList = document.getElementById('content').getElementsByTagName('div');
	for (c = 0; c <= document.getElementById('content').getElementsByTagName('div').length; c++) {
		if (childContentList[c]) {
			document.getElementById(childContentList[c].id).style.display = 'none';
		}
	}
	
	// If a div is selected to highlight then reveal the appropriate tab and content
	if (divID) {
		document.getElementById('content-'+divID).style.display = 'block';
		document.getElementById('tab-'+divID).className = 'selected';
	}
	
	// Else default to displaying the details tab and content as standard
	else {
		document.getElementById(childTabList[0].id).className = 'selected';
		document.getElementById(childContentList[0].id).style.display = 'block';
	}
}



/**
* Replicates the delivery address to the billing address
*/
function replicate_address() {
	document.checkout_form.billing_title.selectedIndex 		= document.checkout_form.delivery_title.selectedIndex;
	document.checkout_form.billing_firstname.value 			= document.checkout_form.delivery_firstname.value;
	document.checkout_form.billing_lastname.value 			= document.checkout_form.delivery_lastname.value;
	document.checkout_form.billing_company.value 			= document.checkout_form.delivery_company.value;
	document.checkout_form.billing_address.value 			= document.checkout_form.delivery_address.value;
	document.checkout_form.billing_town.value 				= document.checkout_form.delivery_town.value;
	document.checkout_form.billing_county.value 			= document.checkout_form.delivery_county.value;
	document.checkout_form.billing_country.selectedIndex 	= document.checkout_form.delivery_country.selectedIndex;
	document.checkout_form.billing_postcode.value 			= document.checkout_form.delivery_postcode.value;
}



/**
* Performas a simple image swap
*/
function swap_image(image_id, new_image) {
	image_source = document.getElementById(image_id);
	
	image_source.src = new_image;
}



/**
* Suckerfish dropdown menu hack for ie6
*/
function startList() {
	menu_list = Array("shop-category-menu", "nav", "delivery-method", "billing-method");
	
	for (m = 0; m < menu_list.length; m ++) {
		
		if (document.all&&document.getElementById) {
			
			if (document.getElementById(menu_list[m])) {
				navRoot = document.getElementById(menu_list[m]);
				
				for (i=0; i<navRoot.childNodes.length; i++) {
					node = navRoot.childNodes[i];
					
					if (node.nodeName=="LI") {
						node.onmouseover=function() {
							this.className+=" over";
						}
						
						node.onmouseout=function() {
							this.className=this.className.replace(" over", "");
						}
					}
				}
			}
		}
	}
}
