jQuery(document).ready(startPurchasePage);

function startPurchasePage()
{
	jQuery('img.preview').live('click', showImage);
	jQuery('span.colors').live('click', openColorSchemes);
	jQuery('div.scheme_head').live('click', openColorSchemes);
	jQuery('span.more').live('click', openAddonPDF);
	jQuery('div.slider').easySlider({
		prevText: '',
		nextText: '',
		auto: true,
		pause: 10000,
		continuous: true 
	});
	
	jQuery('div.hosting').live('click', function() {changeHosting(this)});
}

function validateUserInfo()
{
	var first = jQuery('.fname').val();
	var last = jQuery('.lname').val();
	var email = jQuery('.email').val();
	var conf = jQuery('.confirm').val();
	
	var errors = "";
	
	// Validate names
	if (first.length <= 1)
	{
		if (errors != "")
			errors += "<br />";
		errors += "Invalid First Name";
	}
	
	if (last.length <= 1)
	{
		if (errors != "")
			errors += "<br />";
		errors += "Invalid Last Name";
	}
	
	// Validate Email
	if (email.indexOf(' ') >= 0 || email.indexOf('@') == -1 || email.indexOf('.') == -1)
	{
		if (errors != "")
			errors += "<br />";
		errors += "Email Address is invalid";
	}
	
	if (conf == '' || email != conf)
	{
		if (errors != "")
			errors += "<br />";
		errors += "Email Addresses do not match";
	}
	
	return errors;
}

function addToCart(button)
{
	var itemNum = jQuery(button).val('Adding').attr('disabled', 'disabled').siblings('.item_ref').val();
	
	var info = 'item=' + encodeURIComponent(itemNum);
	
	jQuery.ajax({
	   type: 'POST',
	   url: '/wp-content/themes/visual_lounge/sales/process/',
	   data: info,
	   success: onCartSuccess,
	   error: onCartFailure
	 });
}

function onCartSuccess(data, status)
{
	var message = eval('(' + data + ')');
	
	var content = message['content'];
	
	var cost = Number(message['cost']);
	
	var id = Number(message['id']);
	
	var oldCost = Number(jQuery('span.rawTotal').html());
	
	var newCost = oldCost + cost;
	
	var proceed = true;
	jQuery('.cart_item').each(function()
	{
		var itemId = jQuery(this).children('.id').val();
		
		if (itemId == id)
			proceed = false;
	});
	
	if (proceed)
	{
		jQuery('div.cart_items').append(content);
		jQuery('span.rawTotal').html(newCost);
	}
	else
	{
		jQuery('.cart .error').html("You have already added that item to your cart").css('display', 'block').fadeOut(5000);
	}
	
	jQuery('.addCart').val('Add to Cart').attr('disabled', '');
}

function onCartFailure(request, data, status)
{
	
}

function removeFromCart(span)
{
	var oldCost = Number(jQuery('span.rawTotal').html());
	var itemCost = Number(jQuery(span).siblings('.cost').val());
	var newCost = oldCost-itemCost;
	
	jQuery(span).parent().remove();
	jQuery('span.rawTotal').html(newCost)
}

function checkout()
{
	var confirmed = confirm('This will open a new window and submit your order to paypal. Are you sure you are finished with your order?');
	
	if (!confirmed)
		return '';
	
	var i = 1;
	
	var errors = validateUserInfo();
	
	if (errors != "")
	{
		jQuery('.information .error').html(errors).css('display', 'block');
		return '';
	}
	else
	{
		jQuery('.information .error').html('').css('display', 'none');	
	}
	
	
	var hiddenCart = jQuery('form.cartForm');
	var cartChildren = hiddenCart.children();
	var childLength = cartChildren.length;
	
	var cartItems = new Array();
	
	if (childLength > 3)
		cartChildren.not('input[name="cmd"], input[name="upload"], input[name="business"]').remove();
	
	var hosting = 0;
	jQuery('.cart_item').each(function()
	{
		var itemName = jQuery(this).children('.name').val();
		var itemCost = jQuery(this).children('.cost').val();
		
		cartItems.push(jQuery(this).children('.id').val());
		
		hiddenCart.append('<input type="hidden" name="item_name_'+i+'" value="'+itemName+'" /> <input type="hidden" name="amount_'+i+'" value="'+itemCost+'" />');
		
		i++;
		
		if (jQuery(this).hasClass('cart_host'))
			hosting = jQuery(this).children('.id').val();
	});
	
	var fname = jQuery('.fname').val();
	var lname = jQuery('.lname').val();
	var email = jQuery('.email').val();
	var phone = jQuery('.phone').val();
	var business = jQuery('.business').val();
	var color = jQuery('.color_scheme').val();
	
	var info = 'fname=' + encodeURIComponent(fname)
				+ '&lname=' + encodeURIComponent(lname)
				+ '&email=' + encodeURIComponent(email)
				+ '&phone=' + encodeURIComponent(phone)
				+ '&business=' + encodeURIComponent(business)
				+ '&items=' + encodeURIComponent(cartItems)
				+ '&color=' + encodeURIComponent(color)
				+ '&hosting=' + encodeURIComponent(hosting);
	
	jQuery.ajax({
	   type: 'POST',
	   url: '/wp-content/themes/visual_lounge/sales/process/',
	   data: info,
	   success: onCheckoutSuccess,
	   error: onCheckoutFailure
	 });
	
	hiddenCart.submit();
}

function onCheckoutSuccess(data, status)
{
	
}

function onCheckoutFailure(request, data, status)
{
		
}

function showImage(e)
{
	var image = jQuery(this).attr('src');
	var len = image.length-4;
	var newImage = image.substr(0, len);
	newImage += '_full.jpg';
	window.open(newImage);
}

function openColorSchemes()
{
	var info = 'colors=1';
	
	jQuery.ajax({
	   type: 'POST',
	   url: '/wp-content/themes/visual_lounge/sales/process/',
	   data: info,
	   success: onOpenColorsSuccess,
	   error: onOpenColorsFailure
	 });
}

function onOpenColorsSuccess(data, status)
{
	var newWindow = window.open('', 'colors');
	if (newWindow != null)
		newWindow.document.write(data);
}

function onOpenColorsFailure(request, data, status)
{
	
}

function openAddonPDF()
{
	window.open("http://www.visualloungemedia.com/wp-content/themes/visual_lounge/sales/addons.pdf");	
}

function changeHosting(obj)
{
	if (jQuery(obj).children('.host_code').val() == jQuery('.hosting.selected').children('.host_code').val())
		return '';
	else
	{
		var selected = jQuery('.hosting.selected');
		var notSelected = jQuery('.hosting:not(.selected)');
		
		selected.removeClass('selected');
		notSelected.addClass('selected');
	}
	
	var hosting = Number(jQuery('.hosting.selected').children('.host_code').val());
	
	if (jQuery.browser.msie && jQuery.browser.version == '6.0')
	{
		if (hosting == 0)
		{
			jQuery('.vl_host').empty().append('<div class="hosting selected"><input type="hidden" class="host_code" value="0" /></div>');
			jQuery('.their_host').empty().append('<div class="hosting"><input type="hidden" class="host_code" value="1" /></div>');
		}
		else
		{
			jQuery('.vl_host').empty().append('<div class="hosting"><input type="hidden" class="host_code" value="0" /></div>');
			jQuery('.their_host').empty().append('<div class="hosting selected"><input type="hidden" class="host_code" value="1" /></div>');
		}
		
		supersleight.limitTo('host_root');
		supersleight.run();
	}
	
	var cartHost = jQuery('.cart_item.cart_host');
	var costDiff = 0;
	switch (hosting)
	{
		case 0: cartHost.children('h3').html('Hosting with Visual Lounge');
				cartHost.children('.desc').html('Host your website with Visual Lounge');
				cartHost.children('.cost').val(120);
				cartHost.children('.name').val('Hosting with Visual Lounge');
				cartHost.children('.id').val('-1');
				costDiff = 21;
				break;
		
		case 1: cartHost.children('h3').html('Your Hosting Setup');
				cartHost.children('.desc').html('We setup your website on your server');
				cartHost.children('.cost').val(99);
				cartHost.children('.name').val('Your Hosting Setup');
				cartHost.children('.id').val('-2');
				costDiff = -21;
				break;
	}
	
	var cost = Number(jQuery('span.rawTotal').html());
	jQuery('span.rawTotal').html(Number(cost+costDiff));
}