// JavaScript Document

	// 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,
					     visitors_id: visitorsId,
					     order_num: orderNum,
					     user_email: userEmail
					    },		 
			onLoading: function(){
			  var loading_icon = "<img src='images/4-1.gif' alt='loading...' title='loading...' /> ";
			  $('coupon_result_box').innerHTML = loading_icon;
			  
			}, 
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  $('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 = "";
			  
			},
			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
					     visitors_id: visitorsId,
					     order_num: orderNum,
					     user_email: userEmail
					    },	 
			onLoading: function(){
			  var loading_icon = "<img src='images/4-1.gif' alt='loading...' title='loading...' /> ";
			  $('order_summary_totals').innerHTML = loading_icon;
			  
			},
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  $('order_summary_totals').innerHTML = response;
			  
			},
			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
					     visitors_id: visitorsId,
					     order_num: orderNum,
					     user_email: userEmail
					    },	 
			onLoading: function(){
			  var loading_icon = "<img src='images/4-1.gif' alt='loading...' title='loading...' /> ";
			  $('order_product_items').innerHTML = loading_icon;
			  
			},
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  $('order_product_items').innerHTML = response;
			  
			},
			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
					     visitors_id: visitorsId,
					     order_num: orderNum,
					     user_email: userEmail,
						 delivery_address: deliveryAddress
					    },	 
			onLoading: function(){
			  var loading_icon = "<img src='images/4-1.gif' alt='loading...' title='loading...' /> ";
			  $('delivery_address_result').innerHTML = loading_icon;
			  
			},
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  $('delivery_address_result').innerHTML = response;
			  
			},
			onFailure: function(){ 
			 $('delivery_address_result').innerHTML = "Sorry, Something went wrong... Please refresh the page";
		   }
		});
	}
	
	// refresh Delivery Address - 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
					     visitors_id: visitorsId,
					     order_num: orderNum,
					     user_email: userEmail,
						 delivery_address: delivery_address
					    },	 
			onLoading: function(){
			  var loading_icon = "<img src='images/4-1.gif' alt='loading...' title='loading...' /> ";
			  $('delivery_method_result').innerHTML = loading_icon;
			  
			},
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  $('delivery_method_result').innerHTML = response;
			  
			},
			onFailure: function(){ 
			 $('delivery_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 
	function changeDeliveryAddress() {
	
		// select some varaibles 
		// delivery address
		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
					     visitors_id: visitorsId,
					     order_num: orderNum,
					     user_email: userEmail,
					     delivery_address: delivery_address
					    },	 
			onLoading: function(){
			  var loading_icon = "<img src='images/4-1.gif' alt='loading...' title='loading...' /> ";
			  $('new_delivery_address_result').innerHTML = loading_icon;
			  
			},
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  $('new_delivery_address_result').innerHTML = response;
			 
			 // finished and now refresh the totals
			  refresh_order_summary();
			  refresh_delivery_method();
			  refresh_delivery_address();
			  
			},
			onFailure: function(){ 
			 $('new_delivery_address_result').innerHTML = "Sorry, Something went wrong... Please refresh the page";
		   }
		});
	}
	
	
	// function to change the delivery METHOD in the database    ( delivery Method id ) 
	function changeDeliveryMethod(mId) { 
	
		// select some varaibles 
		// delivery address
		var delivery_service = $('delivery_service_'+mId).value;	
		
		$('delivery_service_selected').value = delivery_service;
		
		new Ajax.Request('includes/checkout/ajax_delivery_method.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     visitors_id: visitorsId,
					     order_num: orderNum,
					     user_email: userEmail,
					     delivery_service: delivery_service
					    },	 
			onLoading: function(){
			  var loading_icon = "<img src='images/4-1.gif' alt='loading...' title='loading...' /> ";
			  $('new_delivery_method_result').innerHTML = loading_icon;
			  
			},
			onSuccess: function(transport){
			  var response = transport.responseText || "no response text";
			  $('new_delivery_method_result').innerHTML = response;
			 
			 // finished and now refresh the totals
			  refresh_order_summary();
			  refresh_delivery_method();
			  refresh_order_items();
			  
			},
			onFailure: function(){ 
			 $('new_delivery_method_result').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() {
	
		// select some varaibles 
		// delivery method
		var deliveryMethod = $('delivery_service_selected').value;		
		
		new Ajax.Request('includes/checkout/ajax_update_on_paypal_click.php', {
			method: 'get',
			parameters: { // these are the variables to be passed into the script
					     deliveryMethod: deliveryMethod, // <- the method in which the delivery will be sent
					     visitors_id: visitorsId,
					     order_num: orderNum,
					     user_email: userEmail
					    }
		});
		
		// now submit the form which in turn takes you to PayPal
		document.form1.submit();
	}
	