// JavaScript Document

// Params
		
		// @ this orders number / id
		var orderNum = '';

		// @ loading icon
		var loading_icon = " <img src='images/ajax-loader.gif' alt='loading...' title='loading...' /> Loading Content, Please Wait. <br />  \
		<span> If This Message Continues For More Than 1 Minute, Please Refresh This Page, All Details Will Remain Saved </span> ";
			  
	// function to apply a coupon to an order
	// coupons insert as an item line in order items table but only show when needed
	function applyCouponCode() {
	
		// select some varaibles 
		// Coupon code
		var couponCode = $('users_coupon').value;	
		
		
		new Ajax.Request('includes/checkout/ajax_apply_coupon.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     coupon_code: couponCode,
					     order_num: orderNum
					    },		 
			onLoading: function(){
				
			  $('coupon_result_box').innerHTML = loading_icon;
			  
			}, 
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  setTimeout(function() { 
								  
				 $('coupon_result_box').innerHTML = response;
			 
				 // finished and now refresh the totals
				  refresh_order_summary();
				  refresh_order_items();
				 
				 // and now clear the value of the coupon input
				 $('users_coupon').value = "";
				 
			  }, 200); 
			  
			},
			onFailure: function(){ 
			 $('coupon_result_box').innerHTML = "Sorry, Something went wrong... Please refresh the page";
			 $('users_coupon').value = "";
		   }
		});
	}
	
	// refresh order summary totals - AJAX
	function refresh_order_summary() {
		
		
		new Ajax.Request('includes/checkout/refresh_order_summary.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     order_num: orderNum
					    },	 
			/*onLoading: function(){
				
			  $('order_summary_totals').innerHTML = loading_icon;
			  
			},*/
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			 setTimeout(function() { 
				$('order_summary_totals').innerHTML = response; 
			 }, 200); 
			  
			},
			onFailure: function(){ 
			 $('order_summary_totals').innerHTML = "Sorry, Something went wrong... Please refresh the page";
		   }
		});
	}
	
	// refresh PRODUCTS/order items - AJAX
	function refresh_order_items() {
		
		
		new Ajax.Request('includes/checkout/refresh_order_items.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     order_num: orderNum
					    },	 
			/*onLoading: function(){
				
			  $('order_product_items').innerHTML = loading_icon;
			  
			},*/
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  setTimeout(function() { $('order_product_items').innerHTML = response; }, 200); 
			  
			},
			onFailure: function(){ 
			 $('order_product_items').innerHTML = "Sorry, Something went wrong... Please refresh the page";
		   }
		});
	}
	
	// refresh Delivery Address - AJAX
	function refresh_delivery_address() {
	
		// select some varaibles 
		// delivery address
		var deliveryAddress = $('delivery_address').value;	
		
		new Ajax.Request('includes/checkout/refresh_delivery_address.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     order_num: orderNum,
						 delivery_address: deliveryAddress
					    },	 
			/*onLoading: function(){
				
			  $('delivery_address_result').innerHTML = loading_icon;
			  
			},*/
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  setTimeout(function() { $('delivery_address_result').innerHTML = response; }, 200); 
			  
			},
			onFailure: function(){ 
			 $('delivery_address_result').innerHTML = "Sorry, Something went wrong... Please refresh the page";
		   }
		});
	}
	
	// refresh Delivery Method - AJAX
	function refresh_delivery_method() {
	
		// select some varaibles 
		// delivery address
		var delivery_address = $('delivery_address').value;	
									
		new Ajax.Request('includes/checkout/refresh_delivery_methods.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     order_num: orderNum,
						 delivery_address: delivery_address
					    },	 
			/*onLoading: function(){
				
			  $('delivery_method_result').innerHTML = loading_icon; 
			},*/
			onSuccess: function(transport){
				
			  var response = transport.responseText || "no response text";
			  
			  setTimeout(function() { $('delivery_method_result').innerHTML = response; }, 200); 
			},
			onFailure: function(){ 
			 $('delivery_method_result').innerHTML = "Sorry, Something went wrong... Please refresh the page";
		   }
		});
	}
	
	// refresh Payment Method - AJAX
	function refresh_payment_method() {
	
		// select some varaibles 
		// payment method id 
		var payment_service_selected = $('payment_service_selected').value;	
		
		new Ajax.Request('includes/checkout/refresh_payment_methods.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     order_num: orderNum,
						 payment_service_selected: payment_service_selected
					    },	 
			/*onLoading: function(){
				
			  $('payment_method_result').innerHTML = loading_icon;
			  
			},*/
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  setTimeout(function() { $('payment_method_result').innerHTML = response; }, 200); 
			  
			},
			onFailure: function(){ 
			 $('payment_method_result').innerHTML = "Sorry, Something went wrong... Please refresh the page";
		   }
		});
	}
	
	// function to change the delivery address in the database, and then the delivery methods box needs to be refreshed 
	var pre_defined_address;
	function changeDeliveryAddress(pre_defined_address) {
	
		// select some varaibles 
		// delivery address
		if(pre_defined_address != 'box') {
			var delivery_address = pre_defined_address;	
		} else {
			var delivery_address = $('delivery_address').value;	
		}	
		
		new Ajax.Request('includes/checkout/ajax_delivery_address.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     order_num: orderNum,
					     delivery_address: delivery_address
					    },	 
			onLoading: function(){
				
			  $('new_delivery_address_result').innerHTML = loading_icon;
			  
			},
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  setTimeout(function() { 
									  
				  $('new_delivery_address_result').innerHTML = response;
				 
				  // finished and now refresh the totals
				  refresh_order_summary();
				  refresh_delivery_method();
				  refresh_delivery_address();
				  refresh_order_items();
				  
			   }, 200); 
			  
			},
			onFailure: function(){ 
			 $('new_delivery_address_result').innerHTML = "Sorry, Something went wrong... Please refresh the page";
		   }
		});
		
		pre_defined_address = ''
	}
	
	
	// function to change the delivery METHOD in the database    ( delivery Method id ) 
	function changeDeliveryMethod(mId) { 
	
		
		// select some varaibles 		
		$('delivery_service_selected').value = mId;
		
		new Ajax.Request('includes/checkout/ajax_delivery_method.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     order_num: orderNum,
					     delivery_service: mId
					    },	 
			onLoading: function(){
				
			  $('new_delivery_method_result').innerHTML = loading_icon;
					  
			},
			onSuccess: function(transport) {			  
				 
				 var response = transport.responseText || "no response text";
				 setTimeout(function() { 
										 
					 $('new_delivery_method_result').innerHTML = response;
					 
					 // finished and now refresh the totals
					 refresh_order_summary();
					 refresh_delivery_method();
					 refresh_order_items();
				
					// set some varaibles 		
					$('delivery_service_selected').value = mId;
				
				 }, 200); 
				
			},
			onFailure: function(){ 
			 $('new_delivery_method_result').innerHTML = "Sorry, Something went wrong... Please refresh the page";
		   }
		});
	}
	
	
	// function to change the payment METHOD in the database    ( payment Method id ) 
	function changePaymentMethod(mId) { 
		
		new Ajax.Request('includes/checkout/ajax_payment_method.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     order_num: orderNum,
					     payment_service: mId
					    },	 
			onLoading: function(){
				
			  $('new_payment_method_result').innerHTML = loading_icon;
			  
			},
			onSuccess: function(transport){
				 
				 var response = transport.responseText || "no response text";
				 setTimeout(function() { 
									 
					$('new_payment_method_result').innerHTML = response;
				 
					 // finished and now refresh the totals
					 refresh_payment_method();
				
					// set some varaibles 		
					$('payment_service_selected').value = mId;
				
				 }, 200); 
			},
			onFailure: function(){ 
			 $('new_payment_method_result').innerHTML = "Sorry, Something went wrong... Please refresh the page";
		   }
		});
	}
	
	// Remove all discounts from an order  ( both percentage type and lump sum )
	function remove_all_discounts() {
		
		new Ajax.Request('includes/checkout/remove_all_discounts.php', {
			method: 'get',	 
			onLoading: function(){
			  $('remove_all_discounts').innerHTML = "Removing Discounts... ";			  
			},
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  setTimeout(function() { 
									  
				  $('remove_all_discounts').innerHTML = response; 
				 
				  // finished and now refresh the totals
				  refresh_order_summary();
				  refresh_order_items();
			  
			  }, 200); 
			  
			},
			onFailure: function(){ 
			 $('remove_all_discounts').innerHTML = "Sorry, Something went wrong... Please refresh the page";
		   }
		});	
	}
	
	// function that is activated on clcik of proceed to checkout,
	// this function updates the order header with the new status, as well as the new delivery details
	function update_paypal_items() {
		
		var added_delivery_note = '';
		if($('added_delivery_note')) {
			added_delivery_note = $('added_delivery_note').value;
		}
		
		var added_user_order_num = '';
		if($('added_user_order_num')) {
			added_user_order_num = $('added_user_order_num').value;
		}		
				
		var url = 'includes/checkout/ajax_update_on_paypal_click.php';
		
		new Ajax.Request(url, {
			method: 'get',
		  	parameters: {
		  	 	delnote: added_delivery_note,
				customers_order_id: added_user_order_num
			},
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			 // alert('Text: ' + response);
		
				// now submit the form which in turn takes you to PayPal
				// document.form1.submit();
				setTimeout(function() { document.final_processes_payment.submit(); }, 200); 
			},
			onFailure: function(){ 
			  alert('Sorry there has been an error with your request, please try again.');
			  window.location = "checkout.php";
		   }
		});	
		
	}

