//############################    CHECKOUT (STEP 1)   ####################################//
	// refreshes checkout items and totals
		function show_checkout(clientid, identifierid, shippingid, speedid, promoid){
			var currentWidth = $('#cesCartDiv').width();
			var currentHeight = (($('#cesCartDiv').height())+20);
			$('#loadingDiv').css({'display':'block','position':'absolute', 'z-index':'999', 'width':currentWidth+'px', 'height':currentHeight+'px', 'background':'no-repeat url('+server_url+'images/loading.gif) 50% 35%'});
			$("#loadingDivBG").css({'display':'block','position':'absolute', 'z-index':'888', 'width':currentWidth+'px', 'height':currentHeight+'px', 'background':'#ffffff', 'opacity':'0.7'});
			$.ajax({
				url: contented_server_url + "ces/show_checkout.php",
				data: {client:clientid, identifier:identifierid, shipping:shippingid, speed:speedid, promo:promoid},
				dataType: 'jsonp',
				jsonp: 'callback',
				jsonpCallback: 'show_checkout_return',
				success: function(){
					//alert("success");
				}
			});
		}
		function show_checkout_return(data){
			$('#loadingDiv').css({'display':'none'});
			$('#loadingDivBG').css({'display':'none'});
			$('#cesCartDiv').html(data.output);
			add_to_cart();
			get_shipping_data(data.client);
		}
		
	// get data for the shipping description popup
		function get_shipping_data(clientid){
			
			$.ajax({
				url: contented_server_url + "ces/shipping_description.php",
				data: {client:clientid},
				dataType: 'jsonp',
				jsonp: 'callback',
				jsonpCallback: 'get_shipping_data_return',
				success: function(){
					//alert("success");
				}
			});
		}
		function get_shipping_data_return(data){
			$("#shippingDesc").colorbox({width:"525px", height:"275px", html:data.info});
		}

	// removes item from cart
		function checkout_remove_item(itemid, clientid, identifierid, shippingid, speedid, promoid){
			$.ajax({
				url: contented_server_url + "ces/step1_functions.php",
				data: {action:'remove', id:itemid, client:clientid, identifier:identifierid, shipping:shippingid, speed:speedid, promo:promoid},
				dataType: 'jsonp',
				jsonp: 'callback',
				jsonpCallback: 'checkout_remove_item_return',
				success: function(){
					//alert("success");
				}
			});
		}
		function checkout_remove_item_return(data){
			show_checkout(data.client, data.identifier, data.shipping, data.speed, data.promo);
		}

	// +1 to item in cart
		function checkout_add_item(itemid, clientid, identifierid, shippingid, speedid, promoid){
			$.ajax({
				url: contented_server_url + "ces/step1_functions.php",
				data: {action:'add', id:itemid, client:clientid, identifier:identifierid, shipping:shippingid, speed:speedid, promo:promoid},
				dataType: 'jsonp',
				jsonp: 'callback',
				jsonpCallback: 'checkout_add_item_return',
				success: function(){
					//alert("success");
				}
			});	
		}
		function checkout_add_item_return(data){
			show_checkout(data.client, data.identifier, data.shipping, data.speed, data.promo);
		}

	// -1 to item in cart
		function checkout_subtract_item(itemid, clientid, identifierid, shippingid, speedid, promoid){
			$.ajax({
				url: contented_server_url + "ces/step1_functions.php",
				data: {action:'subtract', id:itemid, client:clientid, identifier:identifierid, shipping:shippingid, speed:speedid, promo:promoid},
				dataType: 'jsonp',
				jsonp: 'callback',
				jsonpCallback: 'checkout_subtract_item_return',
				success: function(){
					//alert("success");
				}
			});	
		}
		function checkout_subtract_item_return(data){
			show_checkout(data.client, data.identifier, data.shipping, data.speed, data.promo);
		}

	// promo code
		function checkout_promo_code(itemid, clientid, promocode, identifierid, shippingid, speedid){
			$.ajax({
				url: contented_server_url + "ces/step1_functions.php",
				data: {action:'promo', id:itemid, client:clientid, code:promocode, identifier:identifierid, shipping:shippingid, speed:speedid},
				dataType: 'jsonp',
				jsonp: 'callback',
				jsonpCallback: 'checkout_promo_code_return',
				success: function(){
					//alert("success");
				}
			});	
		}
		function checkout_promo_code_return(data){
			if(data.output!='success'){
				$('#promo_error').html(data.output);
			}else{
				show_checkout(data.client, data.identifier, data.shipping, data.speed, data.promo);
			}
		}

	// shipping
		function checkout_shipping(itemid, clientid, shippingid, speedid, identifierid, promoid){
			$.ajax({
				url: contented_server_url + "ces/step1_functions.php",
				data: {action:'shipping', id:itemid, client:clientid, shipping:shippingid, speed:speedid, identifier:identifierid, promo:promoid},
				dataType: 'jsonp',
				jsonp: 'callback',
				jsonpCallback: 'checkout_shipping_return',
				success: function(){
					//alert("success");
				}
			});	
		}
		function checkout_shipping_return(data){
			show_checkout(data.client, data.identifier, data.shipping, data.speed, data.promo);
		}
	
	// update
		function checkout_update(clientid, identifierid, shippingid, speedid, promoid){
			var queryString = $('#checkoutForm').serialize();
			$.ajax({
				url: contented_server_url + "ces/step1_functions.php",
				data: {action:'update', submitvalues:queryString, client:clientid, identifier:identifierid, shipping:shippingid,  speed:speedid, promo:promoid},
				dataType: 'jsonp',
				jsonp: 'callback',
				jsonpCallback: 'checkout_update_return',
				success: function(){
					//alert("success");
				}
			});
		}
		function checkout_update_return(data){
			show_checkout(data.client, data.identifier, data.shipping, data.speed, data.promo);
		}
		
//############################    SIGN UP / LOGIN (STEP 2)   ####################################//
	$(document).ready(function () {
		//colorbox
			$(".cesForgottenPassword").colorbox({iframe:true, innerWidth:400, innerHeight:250});
		//accordian
			function showRecaptcha(element, themeName) {
				Recaptcha.destroy();
				Recaptcha.create("6LdPB80SAAAAABE4iYm_1KSfhRcV9bD3VkxxZ-kh", element, {
					theme: themeName,
					tabindex: 0,
					callback: Recaptcha.focus_response_field
				});
			}
			$('#ces_signup_accordian_content').hide();
			$('#ces_no_signup_accordian_content').hide();
			$('#ces_signup_accordian_toggler').click(function(){
				$('#ces_no_signup_accordian_content').slideUp();
				$('#ces_signup_accordian_content').slideDown();
				var target_offset = $("#cesSignupBottomOfBoxes").offset();
				var target_top = target_offset.top;
				$('html, body').animate({scrollTop:target_top}, 500);
				showRecaptcha('recaptcha_div_1', 'white');
			});
			$('#ces_no_signup_accordian_toggler').click(function(){
				$('#ces_signup_accordian_content').slideUp();
				$('#ces_no_signup_accordian_content').slideDown();
				var target_offset = $("#cesSignupBottomOfBoxes").offset();
				var target_top = target_offset.top;
				$('html, body').animate({scrollTop:target_top}, 500);
				showRecaptcha('recaptcha_div_2', 'white');
			});
			var hashValue = self.document.location.hash.substring(1);
			if(hashValue == 'signup'){
				jQuery.fn.exists = function(){return this.length>0;}
				if ($('#cesSignupBottomOfBoxes').exists()) {
					$('#ces_no_signup_accordian_content').slideUp();
					$('#ces_signup_accordian_content').slideDown();
					var target_offset = $("#cesSignupBottomOfBoxes").offset();
					var target_top = target_offset.top;
					$('html, body').animate({scrollTop:target_top}, 500);
					showRecaptcha('recaptcha_div_1', 'white');
				}
			}else if(hashValue == 'nosignup'){
				jQuery.fn.exists = function(){return this.length>0;}
				if ($('#cesSignupBottomOfBoxes').exists()) {
					$('#ces_signup_accordian_content').slideUp();
					$('#ces_no_signup_accordian_content').slideDown();
					var target_offset = $("#cesSignupBottomOfBoxes").offset();
					var target_top = target_offset.top;
					$('html, body').animate({scrollTop:target_top}, 500);
					showRecaptcha('recaptcha_div_2', 'white');
				}
			}
		//set up pwd meter
			$('#ces_signup_password').pwdMeter({
				minLength: 6,
				displayGeneratePassword: false
			});
	});
	//login form
		function changeLoginPasswordfield(errorstring){
			if(errorstring=='errortrue'){
				document.getElementById("loginPasswordBox").innerHTML = '<input id="loginPasswordField" type="password" name="password" class="ces_password_field_error" value="" />';
				document.getElementById("loginPasswordField").focus();
			}else{
				document.getElementById("loginPasswordBox").innerHTML = '<input id="loginPasswordField" type="password" name="password" class="ces_password_field" value="" />';
				document.getElementById("loginPasswordField").focus();
			}
		}	

//############################    DELIVERY (STEP 3)   ####################################//		
	function purchase_as_gift(answer){
		if(answer=='yes'){
			$('#recipientDiv').css({'display':'block'});
			$('#ces_delivery_firstname').val('');
			$('#ces_delivery_lastname').val('');
			$('#ces_delivery_street').val('');
			$('#ces_delivery_suburb').val('');
			$('#ces_delivery_city').val('');
			$('#ces_delivery_postcode').val('');
			$('#delivery_instructions').val('');

		}else{
			$('#ces_delivery_firstname').val('');
			$('#ces_delivery_lastname').val('');
			$('#recipientDiv').css({'display':'none'});
			$('#ces_delivery_street').val('');
			$('#ces_delivery_suburb').val('');
			$('#ces_delivery_city').val('');
			$('#ces_delivery_postcode').val('');
			$('#delivery_instructions').val('');
		}
	}
