// Begin Cart Effects

var cart_turned_on = false;
									 
function effects_start() { 
	 
	update_cart_onopen();  // Run Ajax
	
	// If IE 6, and hide all drop down menus on-open (they showed over the top of the modal), re-show them on close
	if(isRunningIE6OrBelow) {
		$$('select').each(function(el) {
			  el.style.display = "none";
		});
	}

	// set the background to be able to show
	if($('modal_window_background')) {
		
		$('modal_window_background').show({ queue: 'front' });
	
		// and fade in the background
		new Effect.Opacity('modal_window_background', { from: 0, to: 0.7, duration: 0.5, queue: 'end' });
			
		// check if IE or not...
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
		// fade in the main box
			new Effect.Opacity('modal_window_box', { from: 0, to: 1.1, duration: 0.6, queue: 'end' });
		} else {
		// fade in the main box ( but in 0 time so its not there on start and has to fade in before the slide in next)
			new Effect.Opacity('modal_window_box', { from: 0, to: 1.1, duration: 0, queue: 'end' });
		}
		// slide in box
		new Effect.SlideDown('modal_window_box');
		
		cart_turned_on = true;
		
		Effect.ScrollTo('modal_window_background');
		
	}
	
}	

function effects_end() { // removing the modal window
		
	Effect.Queues.get('modal_window_background').interval = 200;
		
	// slide up the	main box
	new Effect.SlideUp('modal_window_box', { queue: 'front' });
	Effect.Fade('modal_window_box');
	
	// finaly hide the window
	Effect.Fade('modal_window_background');
	
	cart_turned_on = false;
	
	setTimeout(function() { 
		// If IE 6, re-open the closed select 
		if(isRunningIE6OrBelow) {
			$$('select').each(function(el) {
				  el.style.display = "inline";
			});
		}
	}, 600);
}	

<!--// Update The Cart As It Opens Ajax Script //--> 
function update_cart_onopen(){ 

	var session_id = '72237705';
	
 	var url = 'includes/cart/update_cart_onopen.inc.php' + '?session_id=' + session_id

	new Ajax.Request(url, { 
			method:'get',  
			onLoading: function(){ 

				$('cart_update_box').innerHTML = 'Loading <img src="images/4-1.gif"/>';
			},
			onSuccess: function(transport){ 

				setTimeout(function() {
					var response = transport.responseText || " No response text found, please try again ";
					$('cart_update_box').innerHTML = response; 

					// Update Cart Summary
					refresh_cart_content();
				}, 100);
			},
			onFailure: function(){ 

				setTimeout(function() {
					$('cart_update_box').innerHTML = '<p> Sorry, but there was a problem with this request, please try again </p>'; 
				}, 100);
			}
	});	
	
} 

// this function watches for the user to press the ESC key 
 function keyPressHandler(e) {
      var kC  = (window.event) ?    // MSIE or Firefox?
                 event.keyCode : e.keyCode;
      var Esc = (window.event) ?   
                27 : e.DOM_VK_ESCAPE // MSIE : Firefox
      if(kC==Esc) {
      
	    if(cart_turned_on == true) { // if the modal cart is open currently, then close it on click of ESC 
			 effects_end();
		}
      
	    if(lightboxImageGalOn == true) { // if the modal image window is open currently, then close it on click of ESC 
			 closeLightboxImageGal();
		}
	  }
   }

// Add to wish list 
var itemId;
function add_to_wish_list(itemId) {
	
 	var url = 'includes/shop/wish_lists/add_to_wish_list.inc.php?itemId=' + itemId;

	new Ajax.Request(url, { 
			method:'get',
			onLoading: function(){ 

				if($('wish_update_box')) {
					$('wish_update_box').innerHTML = 'Loading... <img src="images/4-1.gif"/>';
				}
			},
			onSuccess: function(transport){ 

					var response = transport.responseText || " No response text found, please try again ";
					if($('wish_update_box')) {
						$('wish_update_box').innerHTML = response;
					} 
					new Effect.Highlight('wish_update_box');
					
			},
			onFailure: function(){ 

					if($('wish_update_box')) {
						$('wish_update_box').innerHTML = '<p> Sorry, but there was a problem with adding this item to your wish list, please try again </p>'; 
					}
			}
	});	
}

// Add to compare list 
var itemId;
function add_to_compare_list(itemId) {
	
 	var url = 'includes/shop/compare/add_to_compare.inc.php?itemId=' + itemId;

	new Ajax.Request(url, { 
			method:'get',
			onLoading: function(){ 

				$('compare_update_box').innerHTML = 'Loading... <img src="images/4-1.gif"/>';
			},
			onSuccess: function(transport){ 

					var response = transport.responseText || " No response text found, please try again ";
					$('compare_update_box').innerHTML = response; 
					new Effect.Highlight('compare_update_box');
					
			},
			onFailure: function(){ 

					$('compare_update_box').innerHTML = '<p> Sorry, but there was a problem with adding this item to your compare list, please try again </p>'; 
			}
	});	
}


// loop a bundle of products by there ids into the cart 
var bundle;
var bundle_array = new Array();
function loop_bundle_array_cart(bundle) {

	bundle_array = bundle.split(",");
			
	for(var i=0; i<bundle_array.length; i++) {
		
		//alert(bundle_array[i]);
		
		var includePath = "includes/cart/add_to_cart.inc.php?js=true";
		
		new Ajax.Request(includePath,
		  {
			method:'get',  
			parameters: {
				product_id: bundle_array[i], 
				quantity: '1'
			},
			onLoading: function(){ },
			onSuccess: function(transport){	},
			onFailure: function(){ }
		});
				
	}

	setTimeout(function(){
		// Open Modal Window
		effects_start();
		// Update Cart Summary
		refresh_cart_content();
	}, 200);
}
   
var cart_content; 
var product_id;
var quantity;
var typeOfInput;
var op_string;
function update_cart_content(product_id,quantity,typeOfInput){ 
	
	// Product Id
	if(product_id == undefined) {
		product_id = $('product_id').value;
	}	
		
	// Quantity 
	if(quantity == undefined) {
		quantity = ($('quantity')) ? $('quantity').value : '1';
	}
	
	if(typeOfInput != 'direct') { 
			
		var op_string = "";
		
			for (var i=0; i<4; i++) {
				if ($("os"+i)) {
			
					var option = $("os"+i).value;
					
					if(option != '') {
				
						var option_name = $("on"+i).value;
						var op_string = op_string + '&' + option_name + '=' + option;
					}
			
				}
			} 	
	
	
		}
	else { op_string = '' }
	
	var includePath = "includes/cart/add_to_cart.inc.php?js=true" + op_string;

	new Ajax.Request(includePath,
		  {
			method:'get',  
			parameters: {
				product_id: product_id, 
				quantity: quantity
			},
			onLoading: function(){
			
				if($('result_box')) { 
			 		$('result_box').innerHTML = "<img src='images/4-1.gif' alt='Loading...' title='Loading...' align='middle' /> Loading Please Wait... ";
				}
			},
			onSuccess: function(transport) { 
			
				setTimeout(function(){ 
						
					if($('result_box')) {
						var response = transport.responseText || " No response text found, please try again ";
						$('result_box').innerHTML = response; 
					}
					// Open Modal Window 
					effects_start(); 
									
				},300); // give 300 miliseconds to make sure all the functions definetly finished before result happens 
	
			},
			onFailure: function(){ 
			
				if($('result_box')) {
					$('result_box').innerHTML = '<p> Sorry, but there was a problem adding this item to your cart, please try again </p>'; 
				}
			}
	});

	// Update Cart Summary
	refresh_cart_content()

} 


var radioObj;
function getCheckedValue(radioObj) {

	var radioGrp = document['forms']['addcartform'][radioObj];
	
	for(i=0; i < radioGrp.length; i++){
		if (radioGrp[i].checked == true) {
			var radioValue = radioGrp[i].value;
		}
	 }
	 
	 return radioValue;
}


/** add a product to the cart and reload the page */
var prods_ids;
function addAndReloadPage(prods_ids) {
	
	// use the loop to cart function 
	loop_bundle_array_cart(prods_ids);
	
	// and reload the page 
	setTimeout(function() { 
		location.reload(); 
	}, 400);
}





//function for forcing form submission
function checkEnter(e, urlLink, iEl) {
	
	var characterCode
	 if(e && e.which) {
		 e = e;
		 characterCode = e.which;
	 }
	 else { 
		 e = event;
		 characterCode = e.keyCode;
	 }	 
	 if(characterCode == 13){
 	 	update_content(urlLink, iEl);
 	 	return false;
	 }
	return true;
	
}
	
	// Loading Image
	var imageInsert = " <img src='images/4-1.gif' alt='Loading...' title='Loading...' align='middle' /> Loading Please Wait...  ";
	
	// Delete Ajax Script 
	var the_content; 
	var box_id; 

function del_content(the_content, box_id){ 

	new Ajax.Request(the_content, { 
			method:'get',  
			onLoading: function(){ 
				$('outside_product_modal'+box_id).innerHTML = imageInsert;
			},
			onSuccess: function(transport){ 
				var response = transport.responseText;
				$('outside_product_modal'+box_id).innerHTML = response; 	
			},
			onFailure: function(){ 
				$('outside_product_modal'+box_id).innerHTML = '<p> Sorry, but there was a problem with this request, please try again </p>'; 
			}
	});

	var includePath = 'includes/cart/update_order_item.inc.php?totals=main';
	
	new Ajax.Request(includePath, { 
			method:'get',  
			onLoading: function(){ 
				$('overall_total_costs').innerHTML = imageInsert;
			},
			onSuccess: function(transport){ 
				var response = transport.responseText;
				$('overall_total_costs').innerHTML = response; 
			},
			onFailure: function(){ 
				$('overall_total_costs').innerHTML = '<p> Sorry, but there was a problem with this request, please try again </p>'; 
			}
	});	
	
		// Update Cart Summary
		refresh_cart_content();
}

// Update qty Ajax Script
function update_content(the_content, box_id){ 

	var qty = $('qty'+box_id).value;
	
	var qty_val = the_content + '&qty=' + qty + '&i=' + box_id;

	new Ajax.Request(qty_val, { 
			method:'get',  
			onLoading: function(){ 
				$('update'+box_id).innerHTML = imageInsert;
			},
			onSuccess: function(transport){ 
				var response = transport.responseText || " No response text found, please try again ";
				$('update'+box_id).innerHTML = response; 
			},
			onFailure: function(){ 
				$('update'+box_id).innerHTML = '<p> Sorry, but there was a problem with this request, please try again </p>'; 
			}
	});

	//$('total'+box_id).fade({ duration: 0.2 });
	//$('update_each_'+box_id).fade({ duration: 0.2 });
	//$('overall_total_costs').fade({ duration: 0.2 });
	new Effect.Opacity('total'+box_id, { from: 1.0, to: 0.2, duration: 0.2 });
	new Effect.Opacity('update_each_'+box_id, { from: 1.0, to: 0.2, duration: 0.2 });
	new Effect.Opacity('overall_total_costs', { from: 1.0, to: 0.2, duration: 0.2 });

	// set a time delay to make sure the previous request was complete first 	
	setTimeout(function() {
					
	// send off again but this time to update the total for that product	
	var url_next = qty_val + '&totals=yes';	
	new Ajax.Request(url_next, { 
			method:'get',  
			onLoading: function(){ 
				//$('total'+box_id).innerHTML = imageInsert;
			},
			onSuccess: function(transport){ 
				var response = transport.responseText || " No response text found, please try again ";
				$('total'+box_id).innerHTML = response; 
				//new Effect.Highlight('total'+box_id);
				setTimeout(function() { $('total'+box_id).appear({ duration: 0.4 }); }, 400);
			},
			onFailure: function(){ 
				$('total'+box_id).innerHTML = '<p> Sorry, but there was a problem with this request, please try again </p>'; 
				$('total'+box_id).appear();
			}
	});	
	
	// Update the each price of a cart item 
	var url_each = qty_val + '&totals=each';
	new Ajax.Request(url_each, { 
			method:'get',  
			onLoading: function(){ 
				//$('update_each_'+box_id).innerHTML = imageInsert;
			},
			onSuccess: function(transport){ 
				var response = transport.responseText || " No response text found, please try again ";
				$('update_each_'+box_id).innerHTML = response; 
				//new Effect.Highlight('update_each_'+box_id);
				setTimeout(function() { $('update_each_'+box_id).appear({ duration: 0.4 }); }, 400);
			},
			onFailure: function(){ 
				$('update_each_'+box_id).innerHTML = '<p> Sorry, but there was a problem with this request, please try again </p>'; 
				$('update_each_'+box_id).appear();
			}
	});	

	// and a third time sent to change the total overall	
	var url_last = qty_val + '&totals=main';
	new Ajax.Request(url_last, { 
			method:'get',  
			onLoading: function(){ 
				//$('overall_total_costs').innerHTML = imageInsert;
			},
			onSuccess: function(transport){ 
				var response = transport.responseText || " No response text found, please try again ";
				$('overall_total_costs').innerHTML = response; 
				setTimeout(function() { $('overall_total_costs').appear({ duration: 0.4 }); }, 400);
			},
			onFailure: function(){ 
				$('overall_total_costs').innerHTML = '<p> Sorry, but there was a problem with this request, please try again </p>'; 
				$('overall_total_costs').appear();
			}
	});		
	
	// Update Cart Summary
	refresh_cart_content();
	
	}, 200); // finish time delay
}

// Set the cart content so it can be viewed in JS mode 
document.observe("dom:loaded", function() {		
	
	if($('cart_content')) $('cart_content').style.display = 'block';
}); 


/** 
 * Multi Price Changer AJAX Function 
 *  If multiple prices is switched on, 
 *  then it will take the new qty and match it with a relevant price via an ajax request 
 *  otherwise it will do nothing 
 */
var optionalAddtionalSum; // an optional addtional sum to add to the total 
function qty_price_changer_ajax() {
	
		if($('pp_price') && $('quantity') && ($('quantity').value!='')) {
		
		var newQty = ($('quantity').value!='0') ? $('quantity').value : '1';		
		var product_id = $('product_id').value;
		var pp_el_id = 'pp_price';
		
		var is_special = 'false';
		if($('pp_special_price')) {
			is_special = 'true';
			pp_el_id = 'pp_special_price';
		}
		
		var url = 'includes/cart/price_qty_ajax_change.php?qty=' + newQty + '&product_id=' + product_id + '&is_special=' + is_special;
		
		// Count addtional amounts 
		if($$('select')) {
			optionalAddtionalSum = 0;
			$$('select').each(function(eS){
				if(eS.options[eS.selectedIndex].title!='') optionalAddtionalSum = optionalAddtionalSum + parseFloat(eS.options[eS.selectedIndex].title); 
			});
		}
		
		if(optionalAddtionalSum && (optionalAddtionalSum!='')) url += '&added_amt=' + optionalAddtionalSum;
				
		new Effect.Opacity(pp_el_id, { from: 1.0, to: 0.2, duration: 0.2 });
		
		new Ajax.Request(url, { 
				method:'get',  
				onLoading: function(){ 
				},
				onSuccess: function(transport){ 
					var response = transport.responseText || " No response text found, please try again ";
					$(pp_el_id).innerHTML = response; 
					setTimeout(function() { $(pp_el_id).appear({ duration: 0.4 }); }, 400);
				},
				onFailure: function(){ 
					$(pp_el_id).innerHTML = '<p> Sorry, but there was a problem with this request, please try again </p>'; 
					$(pp_el_id).appear();
				}
		});		
		
	}
	}

