/*
	Plays the homepage movie.
*/
function sspace_homepage()
{
	var movie = new SWFObject('home.swf?'+sspace_url_root, 'movie_home', '960', '270', '9');
	movie.addParam('scale', 'noscale');
	movie.write('area_middle_home_main_movie')
	
	return false;
}


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


/*
	Creates an "are you sure?" dialogue.
*/
function sspace_areyousure_link(message, link)
{
	if (confirm(message))
	{
		document.location = link;
	}
	
	return false;
}


/*
	Updates the page location.
*/
function sspace_location(new_location)
{
	document.location = new_location;
	
	return false;
}


/*
	Opens a URL in a new window.
*/
function sspace_show_external_resource(url, width, height)
{
	window.open(url,'','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width='+width+',height='+height);
	
	return false;
}


/*
	Converts a pence value to a pricestring.
*/
function sspace_format_pounds_pence(pence)
{
	// Convert to pounds.
	pounds = parseInt(pence) / 100;
	
	// Finished.
	return String(pounds.toFixed(2));
}


/*
	Called when a form is submitted for client side checking.
*/
function sspace_form_submit(form_name, form_ref)
{
	var warning_message = '';
	
	if (form_name == 'login')
	{
		if (form_ref.email.value == '')
			warning_message = 'Please enter your e-mail address.';
		else if (form_ref.password.value == '')
			warning_message = 'Please enter your password.';
	}
	if (form_name == 'reminder')
	{
		if (form_ref.email.value == '')
			warning_message = 'Please enter your e-mail address.';
	}
	if (form_name == 'subscribe')
	{
		if (form_ref.email.value == '')
			warning_message = 'Please enter your e-mail address.';
	}
	if (form_name == 'unsubscribe')
	{
		if (form_ref.email.value == '')
			warning_message = 'Please enter your e-mail address.';
	}
	
	if (warning_message == '')
	{
		return true;
	}
	else
	{
		alert(warning_message);
		return false;
	}
}


/*
	Shows the password for a device.
*/
function sspace_account_device_password(device_index)
{
	document.getElementById('area_page_account_devices_link_'+device_index).style.display = 'none';
	document.getElementById('area_page_account_devices_pass_'+device_index).style.display = 'inline';
	
	return false;
}


/*
	Called on the card payment form upon submission.
*/
function sspace_account_payment_submit()
{
	var warning_message = '';

	if (document.getElementById('page_account_payment_amount').value == '')
		warning_message = 'Please enter the amount that you wish to pay.';
	if (document.getElementById('page_account_payment_address_id').value == 'ignore')
		warning_message = 'Please select a billing address or enter new billing address details.';
	if (document.getElementById('page_account_payment_card_number').value == '')
		warning_message = 'Please enter the your full card number.';
	if (document.getElementById('page_account_payment_card_name').value == '')
		warning_message = 'Please enter the name as it appears on the front of your card.';
	if (document.getElementById('page_account_payment_card_cvv').value == '')
		warning_message = 'Please enter your card security digits (either the last three numbers on the back of your card or four numbers printed on the front.)';
	if (document.getElementById('page_account_payment_address_id').value == 'new')
	{
		if (document.getElementById('page_account_payment_address_1').value == '')
			warning_message = 'Please enter the first line of your billing address.';
		if (document.getElementById('page_account_payment_town').value == '')
			warning_message = 'Please enter your billing address town.';
		if (document.getElementById('page_account_payment_county').value == '')
			warning_message = 'Please enter your billing address county.';
		if (document.getElementById('page_account_payment_postcode').value == '')
			warning_message = 'Please enter your billing address postcode.';
	}
	
	if (warning_message != '')
	{
		alert(warning_message);
		return false;
	}
	else
	{
		return true;
	}
}


/*
	Called on the card payment form upon address selection.
*/
function sspace_account_payment_address()
{
	if (document.getElementById('page_account_payment_address_id').value == 'new')
	{
		document.getElementById('page_account_payment_address_details').style.display = 'block';
	}
	else
	{
		document.getElementById('page_account_payment_address_details').style.display = 'none';
	}		
	
	return false;
}


/*
	Recalculates the price of the selected options.
*/
function sspace_order_calculate()
{
	// Get the initial product prices.
	var price_setup = ssorder_price_setup;
	var price_monthly = ssorder_price_monthly;
	
	// Go through all radio options on the page.
	var form_elements = document.orderform.elements;
	for (i = 0; i < form_elements.length; i++)
	{
		if (form_elements[i].type == 'radio')
		{
			var element_array = String(form_elements[i].name).split("_");
			if (element_array[0] == 'attribute')
			{
				if (form_elements[i].checked)
				{
					price_setup += ssorder_values[form_elements[i].value]['price_setup'];
					price_monthly += ssorder_values[form_elements[i].value]['price_monthly'];
					document.getElementById('page_order_step2_attribute_'+element_array[1]).innerHTML = ssorder_values[form_elements[i].value]['content'];
				}
			}
		}
	}

	/*
	// Include VAT.
	price_setup = price_setup + Math.ceil(price_setup * sspace_vat_rate);
	price_monthly = price_monthly + Math.ceil(price_monthly * sspace_vat_rate);
	
	// Update the summary prices.
	document.getElementById('page_order_step2_summary_setup').innerHTML = '<span>&#163;'+sspace_format_pounds_pence(price_setup)+'</span> setup inc. VAT';
	document.getElementById('page_order_step2_summary_monthly').innerHTML = '<span>&#163;'+sspace_format_pounds_pence(price_monthly)+'</span> monthly inc. VAT';
	*/
	
	// Update the summary prices.
	document.getElementById('page_order_step2_summary_setup').innerHTML = '<span>&#163;'+sspace_format_pounds_pence(price_setup)+'</span> setup ex. VAT';
	document.getElementById('page_order_step2_summary_monthly').innerHTML = '<span>&#163;'+sspace_format_pounds_pence(price_monthly)+'</span> monthly ex. VAT';

	// Finished.
	return true;
}


/*
	Initialises the configuration page.
*/
function sspace_order_init(valueids)
{
	for (var i = 0; i < valueids.length; i++)
	{
		document.getElementById('page_order_step2_value_'+valueids[i]).checked = true;
	}
	
	sspace_order_calculate();
}


/*
	Called when the page is scrolled on the configuration page.
*/
function sspace_order_scroll()
{
	if (!document.all) // Breaks IE.
	{
		var offset = 250;
		if (window.pageYOffset < offset)
		{
			document.getElementById('page_order_step2_summary').style.top = '0px';
		}
		else
		{
			document.getElementById('page_order_step2_summary').style.top = parseInt(window.pageYOffset - offset)+'px';
		}
	}
}


/*
	Called on the order checkout form upon submission.
*/
function sspace_order_checkout()
{
	var warning_message = '';

	if (document.getElementById('page_order_checkout_email').value == '')
		warning_message = 'Please enter your e-mail address.';
	if (document.getElementById('page_order_checkout_firstname').value == '')
		warning_message = 'Please enter your firstname.';
	if (document.getElementById('page_order_checkout_surname').value == '')
		warning_message = 'Please enter your surname.';
	if (document.getElementById('page_order_checkout_address_1').value == '')
		warning_message = 'Please enter the first line of your billing address.';
	if (document.getElementById('page_order_checkout_town').value == '')
		warning_message = 'Please enter your billing address town.';
	if (document.getElementById('page_order_checkout_county').value == '')
		warning_message = 'Please enter your billing address county.';
	if (document.getElementById('page_order_checkout_postcode').value == '')
		warning_message = 'Please enter your billing address postcode.';
	if (document.getElementById('page_order_checkout_payment_card').checked)
	{
		if (document.getElementById('page_order_checkout_card_number').value == '')
			warning_message = 'Please enter the your full card number.';
		if (document.getElementById('page_order_checkout_card_name').value == '')
			warning_message = 'Please enter the name as it appears on the front of your card.';
		if (document.getElementById('page_order_checkout_card_cvv').value == '')
			warning_message = 'Please enter your card security digits (either the last three numbers on the back of your card or four numbers printed on the front.)';
	}
	
	if (warning_message != '')
	{
		alert(warning_message);
		return false;
	}
	else
	{
		return true;
	}
}

