/*****************************************************************************************************

	Copyright 2007 Forever Lovely.

*****************************************************************************************************/

/*
	Used to dynamically change images on the site.
*/
function forever_user_changeimage(image, url)
{
	image.src = url;
}


/*
	Used to dynamically change images on the site.
*/
function forever_user_changeimageid(image_id, url)
{
	document.getElementById(image_id).src = url;
}


/*
	Used to dynamically change images on the site.
	
		key			To help construct the ID for the element on the page.
		highlight		Either true or false (depending on the action to take)
		buybutton	Either true or false, indicating whether or not there is a button we also need to change.
*/
function forever_user_area_highlight(key, highlight, buybutton)
{
	if (highlight)
	{
		document.getElementById('productarea_left_'+key).style.display = 'inline';
		document.getElementById('productarea_right_'+key).style.display = 'inline';
		document.getElementById('productarea_'+key).style.backgroundColor = '#FFF5FA';
		
		if (buybutton)
			document.getElementById('productarea_buybutton_'+key).src = 'media/user/images/shared/buttonbg_buy_off.jpg';
	}
	else
	{
		document.getElementById('productarea_'+key).style.backgroundColor = '#FFFFFF';
		document.getElementById('productarea_left_'+key).style.display = 'none';
		document.getElementById('productarea_right_'+key).style.display = 'none';
	
		if (buybutton)
			document.getElementById('productarea_buybutton_'+key).src = 'media/user/images/shared/button_buy_off.jpg';
	}
}


/*
	Dynamically displays the basket.
*/
function forever_user_basket_display()
{
	// Set up request processor, XML processor and URL.
	var request;
	var xmlDoc;
	var url = 'user_basket.ajax';
	
	// Execute the query.
	if (window.XMLHttpRequest) 
	{
		request = new XMLHttpRequest();
		request.open("GET", url, false);
		request.send(null);
	}
	else if (window.ActiveXObject) 
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (request) 
		{
			request.open("GET", url, false);
			request.send();
		}
	}

	// Put the response in place.
	if (request.status == 200)
	{
		forever_user_basket_display_output(request.responseText);
		
	}
}


/*
	Child function of the function above.
*/
function forever_user_basket_display_output(xml) 
{	
	// Setup
	if (window.ActiveXObject) 
	{
		var xmlobject = new ActiveXObject("Microsoft.XMLDOM");
		xmlobject.loadXML(xml);
	}
	else
	{
		var xmlobject = (new DOMParser()).parseFromString(xml, "text/xml");
	}
	var items = xmlobject.getElementsByTagName('item');

	// Remove the previous basket (if there is one).
	while(document.getElementById('right_basket').hasChildNodes()) 
	{
		document.getElementById('right_basket').removeChild(document.getElementById('right_basket').firstChild);
	}
	
	// If there are items in the basket, display it.
	if (items.length > 0)
	{
		// Title image.
		var img = document.createElement('img');
		img.src = 'media/user/images/leftright/my_basket.jpg';
		img.setAttribute('alt', 'My Basket');
		img.setAttribute('title', 'My Basket');
		document.getElementById('right_basket').appendChild(img);
		document.getElementById('right_basket').appendChild(document.createElement('br'));

		// Add white line break.
		var whiteline = document.createElement('div');
		if (window.ActiveXObject) 
			whiteline.setAttribute('className', 'middle_right_basket_line');
		else
			whiteline.setAttribute('class', 'middle_right_basket_line');
		document.getElementById('right_basket').appendChild(whiteline);
		
		// Go through each item one by one.
		for (var i = 0; i < items.length; i++)
		{
			// Adjust behaviour based on item type.
			switch(items[i].getAttribute('type'))
			{
				case 'product':
			
					// Parse the data from the XML.
					var v_data = items[i].getElementsByTagName('data');
					var v_basket_id = v_data[0].getAttribute('value');
					var v_product_id = v_data[1].getAttribute('value');
					var v_quantity = v_data[2].getAttribute('value');
					var v_name = v_data[3].getAttribute('value');
					
					// Create a DIV for this item and append it.
					var div = document.createElement('div');
					if (window.ActiveXObject) 
						div.setAttribute('className', 'middle_right_basket_item');
					else
						div.setAttribute('class', 'middle_right_basket_item');
					document.getElementById('right_basket').appendChild(div);
				
					// Minus sign.
					var img_minus = document.createElement('img');
					img_minus.setAttribute('alt', 'Remove one from your basket'); 
					img_minus.setAttribute('title', 'Remove one from your basket');
					img_minus.setAttribute('border', '0');
					img_minus.setAttribute('align', 'absmiddle'); 
					img_minus.setAttribute('style', 'cursor: pointer;'); 
					img_minus.setAttribute('onclick', 'javascript:forever_user_basket_update(\'product\', \''+v_basket_id+'\', \''+v_product_id+'\', \''+Math.max((parseInt(v_quantity)-parseInt(1)), 0)+'\');'); 
					img_minus.src = 'media/user/images/leftright/minus.jpg';
					
					// Plus sign.
					var img_plus = document.createElement('img');
					img_plus.setAttribute('alt', 'Add one to your basket'); 
					img_plus.setAttribute('title', 'Add one to your basket');
					img_plus.setAttribute('border', '0');
					img_plus.setAttribute('align', 'absmiddle'); 
					img_plus.setAttribute('style', 'cursor: pointer;'); 
					img_plus.setAttribute('onclick', 'javascript:forever_user_basket_update(\'product\', \''+v_basket_id+'\', \''+v_product_id+'\', \''+(parseInt(v_quantity)+parseInt(1))+'\');'); 
					img_plus.src = 'media/user/images/leftright/plus.jpg';
					
					// Put everything together for this item.
					div.appendChild(img_minus);
					div.innerHTML += '&nbsp;';
					div.appendChild(img_plus);
					div.innerHTML += '&nbsp;';
					div.innerHTML += '&nbsp;';
					div.appendChild(document.createTextNode(v_quantity + ' x ' + v_name));
					
				break
				    
				case 'auto_discount':
				
					// Create a DIV for this item and append it.
					var div = document.createElement('div');
					if (window.ActiveXObject) 
						div.setAttribute('className', 'middle_right_basket_item');
					else
						div.setAttribute('class', 'middle_right_basket_item');
					document.getElementById('right_basket').appendChild(div);
				
					// Blank placeholder
					var img_blank = document.createElement('img');
					img_blank.setAttribute('alt', ' '); 
					img_blank.setAttribute('title', ' ');
					img_blank.setAttribute('border', '0');
					img_blank.setAttribute('align', 'absmiddle'); 
					img_blank.src = 'media/user/images/leftright/blank.jpg';
					
					// Put everything together for this item.
					div.appendChild(img_blank);
					div.innerHTML += '&nbsp;';
					div.appendChild(img_blank);
					div.innerHTML += '&nbsp;';
					div.innerHTML += '&nbsp;';
					div.appendChild(document.createTextNode('1 x 5% discount'));
										
				break
				
				case 'offer':
				
					// Parse the data from the XML.
					var v_data = items[i].getElementsByTagName('data');
					var v_basket_id = v_data[0].getAttribute('value');
					var v_offer_id = v_data[1].getAttribute('value');
					var v_quantity_editable = v_data[2].getAttribute('value');
					var v_quantity = v_data[3].getAttribute('value');
					var v_name = v_data[4].getAttribute('value');
					
					// Create a DIV for this item and append it.
					var div = document.createElement('div');
					if (window.ActiveXObject) 
						div.setAttribute('className', 'middle_right_basket_item');
					else
						div.setAttribute('class', 'middle_right_basket_item');
					document.getElementById('right_basket').appendChild(div);
				
					if (v_quantity_editable == 'yes')
					{
						// Minus sign.
						var img_minus = document.createElement('img');
						img_minus.setAttribute('alt', 'Remove one from your basket'); 
						img_minus.setAttribute('title', 'Remove one from your basket');
						img_minus.setAttribute('border', '0');
						img_minus.setAttribute('align', 'absmiddle'); 
						img_minus.setAttribute('style', 'cursor: pointer;'); 
						img_minus.setAttribute('onclick', 'javascript:forever_user_basket_update(\'offer\', \''+v_basket_id+'\', \''+v_offer_id+'\', \''+Math.max((parseInt(v_quantity)-parseInt(1)), 0)+'\');'); 
						img_minus.src = 'media/user/images/leftright/minus.jpg';
						
						// Plus sign.
						var img_plus = document.createElement('img');
						img_plus.setAttribute('alt', 'Add one to your basket'); 
						img_plus.setAttribute('title', 'Add one to your basket');
						img_plus.setAttribute('border', '0');
						img_plus.setAttribute('align', 'absmiddle'); 
						img_plus.setAttribute('style', 'cursor: pointer;'); 
						img_plus.setAttribute('onclick', 'javascript:forever_user_basket_update(\'offer\', \''+v_basket_id+'\', \''+v_offer_id+'\', \''+(parseInt(v_quantity)+parseInt(1))+'\');'); 
						img_plus.src = 'media/user/images/leftright/plus.jpg';
						
						// Put everything together for this item.
						div.appendChild(img_minus);
						div.innerHTML += '&nbsp;';
						div.appendChild(img_plus);
						div.innerHTML += '&nbsp;';
						div.innerHTML += '&nbsp;';
						div.appendChild(document.createTextNode(v_quantity + ' x ' + v_name));
					}
					else
					{
						// Blank placeholder
						var img_blank = document.createElement('img');
						img_blank.setAttribute('alt', ' '); 
						img_blank.setAttribute('title', ' ');
						img_blank.setAttribute('border', '0');
						img_blank.setAttribute('align', 'absmiddle'); 
						img_blank.src = 'media/user/images/leftright/blank.jpg';
						
						// Put everything together for this item.
						div.appendChild(img_blank);
						div.innerHTML += '&nbsp;';
						div.appendChild(img_blank);
						div.innerHTML += '&nbsp;';
						div.innerHTML += '&nbsp;';
						div.appendChild(document.createTextNode('1 x ' + v_name));
					}
						
				break
			
				default:
				
					alert("Error");
			}
		}
	
		// Output a new line
		document.getElementById('right_basket').appendChild(document.createElement('br'));
		
		// Get the summary information.
		var summary = xmlobject.getElementsByTagName('summary');
		var summary_total = summary[0].getAttribute('total');
		var summary_urlroot = summary[0].getAttribute('url_root');
		var summary_urlrootssl = summary[0].getAttribute('url_root_ssl');
		
		// Output the summary.
		var summarydiv = document.createElement('div');
		if (window.ActiveXObject) 
			summarydiv.setAttribute('className', 'middle_right_basket_summary');
		else
			summarydiv.setAttribute('class', 'middle_right_basket_summary');
		document.getElementById('right_basket').appendChild(summarydiv);
		
		// Create the subtotal.
		var summarydiv_subtotal = document.createElement('div');
		if (window.ActiveXObject) 
			summarydiv_subtotal.setAttribute('className', 'middle_right_basket_subtotal');
		else
			summarydiv_subtotal.setAttribute('class', 'middle_right_basket_subtotal');
		summarydiv_subtotal.innerHTML = 'Total : &pound;'+summary_total;
		summarydiv.appendChild(summarydiv_subtotal);

		// Add a new line.				
		document.getElementById('right_basket').appendChild(document.createElement('br'));
		
		// Create the view basket area.
		var viewbasketdiv = document.createElement('div');
		if (window.ActiveXObject) 
			viewbasketdiv.setAttribute('className', 'middle_right_basket_viewbasket');
		else
			viewbasketdiv.setAttribute('class', 'middle_right_basket_viewbasket');
		document.getElementById('right_basket').appendChild(viewbasketdiv);
		
		// Create the view basket link.
		var viewbasketdiv_link = document.createElement('a');
		viewbasketdiv_link.setAttribute('href', summary_urlroot+'basket.html');
		viewbasketdiv.appendChild(viewbasketdiv_link);
		
		// Create the view basket button.
		var viewbasketdiv_img = document.createElement('img');
		viewbasketdiv_img.setAttribute('alt', 'View Basket'); 
		viewbasketdiv_img.setAttribute('title', 'View Basket');
		viewbasketdiv_img.setAttribute('border', '0');
		viewbasketdiv_img.setAttribute('onmouseover', 'forever_user_changeimage(this, \'media/user/images/leftright/button_basket_over.jpg\')');
		viewbasketdiv_img.setAttribute('onmouseout', 'forever_user_changeimage(this, \'media/user/images/leftright/button_basket_off.jpg\')');
		viewbasketdiv_img.src = 'media/user/images/leftright/button_basket_off.jpg';
		viewbasketdiv_link.appendChild(viewbasketdiv_img);

		// Create the checkout area.
		var checkoutdiv = document.createElement('div');
		if (window.ActiveXObject) 
			checkoutdiv.setAttribute('className', 'middle_right_basket_checkout');
		else
			checkoutdiv.setAttribute('class', 'middle_right_basket_checkout');
		document.getElementById('right_basket').appendChild(checkoutdiv);
		
		// Create the checkout link.
		var checkoutdiv_link = document.createElement('a');
		checkoutdiv_link.setAttribute('href', summary_urlrootssl+'checkout.html');
		checkoutdiv.appendChild(checkoutdiv_link);
		
		// Create the checkout button.
		var checkoutdiv_img = document.createElement('img');
		checkoutdiv_img.setAttribute('alt', 'Checkout'); 
		checkoutdiv_img.setAttribute('title', 'Checkout');
		checkoutdiv_img.setAttribute('border', '0');
		checkoutdiv_img.setAttribute('onmouseover', 'forever_user_changeimage(this, \'media/user/images/leftright/button_checkout_over.jpg\')');
		checkoutdiv_img.setAttribute('onmouseout', 'forever_user_changeimage(this, \'media/user/images/leftright/button_checkout_off.jpg\')');
		checkoutdiv_img.src = 'media/user/images/leftright/button_checkout_off.jpg';
		checkoutdiv_link.appendChild(checkoutdiv_img);
		
		// Output a new line.
		document.getElementById('right_basket').appendChild(document.createElement('br'));
		
		// Add white line break.
		var whiteline = document.createElement('div');
		if (window.ActiveXObject) 
			whiteline.setAttribute('className', 'middle_right_basket_line');
		else
			whiteline.setAttribute('class', 'middle_right_basket_line');
		document.getElementById('right_basket').appendChild(whiteline);
		
		// Output a new line
		document.getElementById('right_basket').appendChild(document.createElement('br'));
	}
}


/*
	Returns true if the user's basket needs options to be set.  Otherwise, false.
*/
function forever_user_basket_missing()
{
	// Set up request processor, XML processor and URL.
	var request;
	var xmlDoc;
	
	var url = 'user_missing.ajax';
	
	// Execute the query.
	if (window.XMLHttpRequest) 
	{
		request = new XMLHttpRequest();
		request.open("GET", url, false);
		request.send(null);
	}
	else if (window.ActiveXObject) 
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (request) 
		{
			request.open("GET", url, false);
			request.send();
		}
	}

	// Put the new option HTML in place.
	if (request.status == 200)
	{
		if (request.responseText == 'true')
			return true;
		else
			return false;
	}
}


/*
	Dynamically updates a product option in the basket.
*/
function forever_user_basket_option(basket_id, option_id, value_id)
{
	// Set up request processor, XML processor and URL.
	var request;
	var xmlDoc;
	
	var url = 'user_option.ajax?basket_id='+String(basket_id)+'&option_id='+String(option_id)+'&value_id='+String(value_id);
	
	// Execute the query.
	if (window.XMLHttpRequest) 
	{
		request = new XMLHttpRequest();
		request.open("GET", url, false);
		request.send(null);
	}
	else if (window.ActiveXObject) 
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (request) 
		{
			request.open("GET", url, false);
			request.send();
		}
	}

	// Put the new option HTML in place.
	if (request.status == 200)
	{
		document.getElementById('basket_product_options_'+String(basket_id)).innerHTML = request.responseText;
		
		if (!forever_user_basket_missing())
			document.getElementById('basket_missing_warning').style.display = 'none';
	}
}


/*
	Dynamically updates the basket.
*/
function forever_user_basket_update(item_type, basket_id, product_id, quantity)
{
	// Set up request processor, XML processor and URL.
	var request;
	var xmlDoc;
	
	var url = 'user_update.ajax?item_type='+String(item_type)+'&basket_id='+String(basket_id)+'&product_id='+String(product_id)+'&quantity='+String(quantity);
	
	// Execute the query.
	if (window.XMLHttpRequest) 
	{
		request = new XMLHttpRequest();
		request.open("GET", url, false);
		request.send(null);
	}
	else if (window.ActiveXObject) 
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (request) 
		{
			request.open("GET", url, false);
			request.send();
		}
	}

	// Put the response in place.
	if (request.status == 200)
	{
		forever_user_basket_display();
		
		if (request.responseText != '' && request.responseText != 'null' && request.responseText != null)
			alert(request.responseText);
	}
}


/*
	Checks the account details update form before submission.
*/
function forever_user_form_account_details()
{
	var message = '';

	if (document.form_account_details.title.value == '')
		message = 'Please specify your title';
	else if (document.form_account_details.firstname.value == '')
		message = 'Please enter your first name';
	else if (document.form_account_details.surname.value == '')
		message = 'Please enter your surname';
	else if (document.form_account_details.email.value == '')
		message = 'Please enter your e-mail address';
	
	else if (document.form_account_details.pass_1.value != document.form_account_details.pass_2.value)
		message = 'Sorry, your new passwords do not match';
	
	if (message == '')			
		document.form_account_details.submit();
	else
		alert(message);
}


/*
	Calls the user_addresses.ajax with the appropriate parameters.
	Then recalculates the checkout form values.
*/
function forever_user_form_checkout_addresses(adjust, details)
{
	// Set up request processor, XML processor and URL.
	var request;
	var xmlDoc;
	
	var url = 'user_addresses.ajax?adjust='+String(adjust)+'&details='+String(details);
	
	// Execute the query.
	if (window.XMLHttpRequest) 
	{
		request = new XMLHttpRequest();
		request.open("GET", url, false);
		request.send(null);
	}
	else if (window.ActiveXObject) 
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (request) 
		{
			request.open("GET", url, false);
			request.send();
		}
	}

	// Put the response in place.
	if (request.status == 200)
	{
		if (request.responseText != '')
			alert(request.responseText);
		
		forever_user_form_checkout_recalculate();
		//alert('Your basket totals have been updated, please check them before proceeding');
	}
}


/*
	Dynamically alters the checkout form.
*/
function forever_user_form_checkout_billing_showdelivery()
{
	document.getElementById('form_billing_new').style.display = 'none';
	document.getElementById('form_billing_existing').style.display = 'none';
}


/*
	Dynamically alters the checkout form.
*/
function forever_user_form_checkout_billing_showexisting()
{
	document.getElementById('form_billing_new').style.display = 'none';
	document.getElementById('form_billing_existing').style.display = 'inline';
}


/*
	Dynamically alters the checkout form.
*/
function forever_user_form_checkout_billing_shownew()
{
	document.getElementById('form_billing_existing').style.display = 'none';
	document.getElementById('form_billing_new').style.display = 'inline';
}


/*
	Dynamically alters the checkout form.
	Also updates the delivery choice and recalculates the basket values.
*/
function forever_user_form_checkout_delivery_showexisting()
{
	document.getElementById('form_delivery_new').style.display = 'none';
	document.getElementById('form_delivery_existing').style.display = 'inline';
	forever_user_form_checkout_addresses('delivery_choice', 'address');
}


/*
	Dynamically alters the checkout form.
	Also updates the delivery choice and recalculates the basket values.
*/
function forever_user_form_checkout_delivery_shownew()
{
	document.getElementById('form_delivery_existing').style.display = 'none';
	document.getElementById('form_delivery_new').style.display = 'inline';
	forever_user_form_checkout_addresses('delivery_choice', 'country');
}


/*
	Dynamically updates the subtotal, delivery cost, VAT and total cost on the checkout form.
*/
function forever_user_form_checkout_recalculate()
{
	// Set up request processor, XML processor and URL.
	var request;
	var xmlDoc;
	var url = 'user_totals.ajax';
	
	// Execute the query.
	if (window.XMLHttpRequest) 
	{
		request = new XMLHttpRequest();
		request.open("GET", url, false);
		request.send(null);
	}
	else if (window.ActiveXObject) 
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (request) 
		{
			request.open("GET", url, false);
			request.send();
		}
	}

	// Put the response in place.
	if (request.status == 200)
	{
		var response = request.responseXML.documentElement;
		
		document.getElementById('dynamic_price_subtotal').innerHTML = '&pound;'+response.getElementsByTagName('subtotal')[0].firstChild.data;
		document.getElementById('dynamic_price_delivery').innerHTML = '&pound;'+response.getElementsByTagName('delivery')[0].firstChild.data;
		document.getElementById('dynamic_price_vat').innerHTML = '&pound;'+response.getElementsByTagName('vat')[0].firstChild.data;
		document.getElementById('dynamic_price_total').innerHTML = '&pound;'+response.getElementsByTagName('total')[0].firstChild.data;
	}
}


/*
	Checks a checkout form before submission.
*/
function forever_user_form_checkout_submit()
{
	var message = '';

	// Determine the value of the delivery_type variable.
	var delivery_type = '';
	for (var i = 0; i < document.form_checkout.delivery_type.length; i ++)
		if (document.form_checkout.delivery_type[i].checked)
			delivery_type = document.form_checkout.delivery_type[i].value;
	
	// Determine the value of the billing_type variable.
	var billing_type = '';
	try
	{
		if (document.form_checkout.billing_type != 'delivery')
			for (var i = 0; i < document.form_checkout.billing_type.length; i ++)
				if (document.form_checkout.billing_type[i].checked)
					billing_type = document.form_checkout.billing_type[i].value;
	}
	catch (e)
	{
		// This is for Safari, which doesn't seem to be able to read the values of radio buttons.
	}
	
	// See if product options need to be set.
	if (forever_user_basket_missing())
		message += 'There are products in your basket with unselected options.\n\nPlease make your choices before checking out.';
				
	// If a new delivery address has been provided, check it.	
	else if (delivery_type=='new' && document.form_checkout.delivery_address_1.value=='')
		message += 'Please enter your delivery address.';
	else if (delivery_type=='new' && document.form_checkout.delivery_town.value=='')
		message += 'Please enter your delivery town.';
	else if (delivery_type=='new' && document.form_checkout.delivery_county.value=='')
		message += 'Please enter your delivery county.';
	else if (delivery_type=='new' && document.form_checkout.delivery_postcode.value=='')
		message += 'Please enter your delivery postcode.';
	
	// If a new billing address has been provided, check it.
	else if (billing_type=='new' && document.form_checkout.billing_address_1.value=='')
		message += 'Please enter your billing address.';
	else if (billing_type=='new' && document.form_checkout.billing_town.value=='')
		message += 'Please enter your billing town.';
	else if (billing_type=='new' && document.form_checkout.billing_county.value=='')
		message += 'Please enter your billing county.';
	else if (billing_type=='new' && document.form_checkout.billing_postcode.value=='')
		message += 'Please enter your billing postcode.';
	
	// Check that all necessary card information has been provided.
	else if (document.form_checkout.card_type.value=='ignore')
		message += 'Please select your card type.';
	else if (document.form_checkout.card_number.value=='')
		message += 'Please enter your card number.';
	else if (document.form_checkout.card_cvv.value=='')
		message += 'Please enter your card CVV digits (last three digits on the rear of the card).';
	else if (document.form_checkout.card_name.value=='')
		message += 'Please enter your the name that appears on the front of your credit card.';

	// Check that the terms and conditions have been accepted.
	//else if (!document.form_checkout.accept_terms.checked)
	//	message = 'You must read and agree to the terms and conditions before checking out';
	
	// Either submit the form or issue a warning message.			
	if (message == '')
		document.form_checkout.submit();
	else
		alert(message);
}


/*
	Checks a sign up form before submission.
*/
function forever_user_form_signup()
{
	var message = '';

	//if (!document.form_signup.accept_terms.checked)
	//	message = 'You must read and agree to the terms and conditions before signing up';
	
	//else if (document.form_signup.title.value == '')
	if (document.form_signup.title.value == '')
		message = 'Please specify your title';
	else if (document.form_signup.firstname.value == '')
		message = 'Please enter your first name';
	else if (document.form_signup.surname.value == '')
		message = 'Please enter your surname';
	else if (document.form_signup.email.value == '')
		message = 'Please enter your e-mail address';
	
	else if (document.form_signup.address_1.value == '')
		message = 'Please enter the first line of your address';
	else if (document.form_signup.town.value == '')
		message = 'Please enter your town';
	else if (document.form_signup.county.value == '')
		message = 'Please enter your county';
	else if (document.form_signup.postcode.value == '')
		message = 'Please enter your postcode';
	
	else if (document.form_signup.pass_1.value != document.form_signup.pass_2.value)
		message = 'Sorry, your passwords do not match';
	else if (document.form_signup.pass_1.value == '')
		message = 'Your passwords must not be blank';
	
	if (message == '')			
		document.form_signup.submit();
	else
		alert(message);
}


/*
	Dynamically updates the basket.
*/
function forever_user_poll_vote(answer_id)
{
	// Set up request processor, XML processor and URL.
	var request;
	var xmlDoc;
	
	var url = 'user_poll.ajax?answer_id='+String(answer_id);
	
	// Execute the query.
	if (window.XMLHttpRequest) 
	{
		request = new XMLHttpRequest();
		request.open("GET", url, false);
		request.send(null);
	}
	else if (window.ActiveXObject) 
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (request) 
		{
			request.open("GET", url, false);
			request.send();
		}
	}

	// Put the response in place.
	if (request.status == 200)
	{
		if (request.responseText != '' && request.responseText != 'null' && request.responseText != null)
			alert(request.responseText);
	}
}


/*
	On the product details page, updates the price of the product depending on what bundled options are selected (if any).
*/
function forever_user_product_details_bundle()
{
	var extra_product_info = document.getElementById('extra_product_info').options[document.getElementById('extra_product_info').selectedIndex].value;
	var extra_product_info_array = extra_product_info.split(',');
	
	document.getElementById('product_details_purchase_price').innerHTML = '&pound;'+extra_product_info_array[1]+' inc. VAT';
}


/*
	On the product details page, adds the main product to the basket.
*/
function forever_user_product_details_add(product_id)
{
	forever_user_basket_update('product', '', product_id, document.getElementById('quantity_box').value);
	
	if (document.getElementById('extra_product_info'))
	{
		var extra_product_info = document.getElementById('extra_product_info').options[document.getElementById('extra_product_info').selectedIndex].value;
		var extra_product_info_array = extra_product_info.split(',');
		
		if (extra_product_info_array[0] != 'ignore')
		{
			forever_user_basket_update('product', '', extra_product_info_array[0], document.getElementById('quantity_box').value);
		}
	}
}


/*
	Adjusts which part of the product details we are viewing.
*/
function forever_user_product_switch(show)
{
	if (show == 'description')
	{
		// Buttons
		document.getElementById('button_product_description_off').style.display = 'none';
		document.getElementById('button_product_description_on').style.display = 'inline';
		document.getElementById('button_product_reviews_off').style.display = 'inline';
		document.getElementById('button_product_reviews_on').style.display = 'none';
		document.getElementById('button_product_tellafriend_off').style.display = 'inline';
		document.getElementById('button_product_tellafriend_on').style.display = 'none';
		
		// Content
		document.getElementById('area_product_description').style.display = 'inline';
		document.getElementById('area_product_reviews').style.display = 'none';
		document.getElementById('area_product_tellafriend').style.display = 'none';
	}
	else if (show == 'reviews')
	{
		// Buttons
		document.getElementById('button_product_description_off').style.display = 'inline';
		document.getElementById('button_product_description_on').style.display = 'none';
		document.getElementById('button_product_reviews_off').style.display = 'none';
		document.getElementById('button_product_reviews_on').style.display = 'inline';
		document.getElementById('button_product_tellafriend_off').style.display = 'inline';
		document.getElementById('button_product_tellafriend_on').style.display = 'none';
		
		// Content
		document.getElementById('area_product_description').style.display = 'none';
		document.getElementById('area_product_reviews').style.display = 'inline';
		document.getElementById('area_product_tellafriend').style.display = 'none';
	}
	else if (show == 'tellafriend')
	{
		// Buttons
		document.getElementById('button_product_description_off').style.display = 'inline';
		document.getElementById('button_product_description_on').style.display = 'none';
		document.getElementById('button_product_reviews_off').style.display = 'inline';
		document.getElementById('button_product_reviews_on').style.display = 'none';
		document.getElementById('button_product_tellafriend_off').style.display = 'none';
		document.getElementById('button_product_tellafriend_on').style.display = 'inline';
		
		// Content
		document.getElementById('area_product_description').style.display = 'none';
		document.getElementById('area_product_reviews').style.display = 'none';
		document.getElementById('area_product_tellafriend').style.display = 'inline';
	}
}


/*
	Used to pop up the terms and conditions for the site.
*/
function forever_user_showterms(id)
{
	window.open('terms.html','','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=300,height=400');
}
