var TotalAmount = 0;

// Ready
$(document).ready(function() {
	// Total amount calculation
	$('.js-track').change(function() { 
		$('#total-amount').html('<span style="text-decoration:underline;cursor:pointer;" onclick="CalculateTotal();">[Update]</span>');
	});

	// Discount coupon processes
	$('#apply-discount-button').click(function() { CalculateTotal(); });
	$('#discount-coupon-notice').click(function () { ToggleDiscountCoupon(); });
	$('#discount-coupon').blur(function () { HideDiscountCoupon(); });

	// Proceed to purchase step
	$('#js-buy-now-link').click(function() { ProceedToCheckout(); });

	// Customer login/logout
	$('.js-customer-login').focus(function() { CustomerLoginFocus($(this).attr('id'), 'focus'); });
	$('.js-customer-login').blur(function() { CustomerLoginFocus($(this).attr('id'), 'blur'); });
	$('.js-customer-login-button').click(function() { CustomerLogin(); });
	$('#logout-link').click(function () { LogoutCustomer(); });

	// Default statuses/values
	CalculateTotal();
	CustomerLoginReset();
});

// Customer login
function CustomerLogin()
	{
	if (($("input[name='CustomerUsername']").val() == '') || ($("input[name='CustomerPassword']").val() == ''))
		{
		alert('Please enter your username and password. Click OK to try again');
		return;
		}

	$('#customer-login-button').val('Logging in...');

	$.ajax({
		url: '/clientarea/login/',
		async: false,
		type: 'POST',
		dataType: 'json',
		data: 'LoginButton=Login&Username=' + escape($("input[name='CustomerUsername']").val()) + '&Password=' + escape($("input[name='CustomerPassword']").val()) + '&ReturnType=JSON',
		success: function(JSON) {
			if (JSON.Message != '')
				{
				// Login failed
				$('#customer-login-error').text(JSON.Message);
				$('#customer-login-error').show();
				$('#customer-login-button').val('Login');
				}
			else
				{
				// Login successful
				$('#customer-login-box').hide();
				$('#customer-loggedin-info').text(JSON.Name);
				$('#customer-loggedin').show();
				$('#customer-login-button').val('Login');
				CalculateTotal();
				}
		}
	});

	$('#customer-login-button').val('Login');
	}
	
function LogoutCustomer()
	{
	$.ajax({
		url: '/clientarea/logout/',
		async: false,
		type: 'POST',
		dataType: 'json',
		data: 'JustLogin=1',
		success: function(JSON) {
			$('#customer-loggedin').hide();
			$('#customer-login-box').show();
			CalculateTotal();
		}
	});
	}

function CustomerLoginReset()
	{
	if (($('#customer-username').val() == 'username') || ($('#customer-username').val() == 'username'))
		{
		$('#customer-username').css('color', '#c1c1c1');
		$('#customer-username').val('username');
		}
	if (($('#customer-password').val() == 'password') || ($('#customer-password').val() == 'password'))
		{
		$('#customer-password').css('color', '#c1c1c1');
		$('#customer-password').val('password');
		}
	}

function CustomerLoginFocus(ObjectID, Event)
	{
	if (Event == 'blur')
		{
		if (($('#' + ObjectID).val() == '') || ($('#' + ObjectID).val() == 'username') || ($('#' + ObjectID).val() == 'password'))
			{
			$('#' + ObjectID).css('color', '#c1c1c1');
			if (ObjectID == 'customer-username') $('#' + ObjectID).val('username');
			if (ObjectID == 'customer-password') $('#' + ObjectID).val('password');
			}
		}
	else if ((Event == 'focus' && ($('#' + ObjectID).val() == 'username') || $('#' + ObjectID).val() == 'password'))
		{
		$('#' + ObjectID).val('');
		$('#' + ObjectID).css('color', '#000000');
		}
	}

// 	Discount coupon
function HideDiscountCoupon()
	{
	if ($('#discount-coupon').val() == '')
		{
		$('#discount-coupon-notice-container').show();
		$('#discount-coupon-container').hide();
		CalculateTotal();
		}
	}

function ToggleDiscountCoupon()
	{
	$('#discount-coupon-notice-container').hide();
	$('#discount-coupon-container').show();
	$('#discount-coupon').focus();
	}

// Purchase
function ProceedToCheckout()
	{
	$('#pricing-form').submit();	
	}

// Calculation
function CalculateTotal()
	{
	// Reset the total amount
	TotalAmount = 0;

	$('#total-amount').html('Calculating...');

	// Prepare the list of selected add-ons - Start {
	var SelectedAddOns = new Array;
	$("input[name='AddOns[]']:checked").each(function() {
		SelectedAddOns.push($(this).val());
	});
	SelectedAddOns = SelectedAddOns.join('|');
	// Prepare the list of selected add-ons - End }

	// Discount coupon - Start {
	var DiscountCoupon = $('#discount-coupon').val();
	// Discount coupon - End }

	$('.js-track').attr('disabled', 'disable');
	$('.js-customer-login-button').attr('disabled', 'disable');

	$.ajax({
		url: '/oempro/pricing/',
		async: false,
		type: 'POST',
		dataType: 'json',
		data: 'Command=CalculatePricing&Edition=&AddOns=' + SelectedAddOns + '&Services=&DiscountCoupon=' + DiscountCoupon,
		success: function(JSON) {
			$('.js-track').removeAttr('disabled');
			$('.js-customer-login-button').removeAttr('disabled');

			$('#total-amount').text('$' + JSON.TotalAmount.toFixed(2) + ' USD');
			if (JSON.TotalDiscount > 0)
				{
				$('#discount-notice-amount').text('$' + JSON.TotalDiscount.toFixed(2) + ' USD');
				$('#discount-notice').show();
				}
			else
				{
				$('#discount-notice-amount').text('$0.00 USD');
				$('#discount-notice').hide();
				}
			if (JSON.DiscountMessage != '')
				{
				$('#discount-coupon-error').text(JSON.DiscountMessage);
				$('#discount-coupon-error').show();
				}
			else
				{
				$('#discount-coupon-error').text('');
				$('#discount-coupon-error').hide();
				}
		}
	});
	}
