﻿/**
 * @name	-	AnimatedCart
 * @desc	-	animated effect based shopping cart.  Includes dropdown cart listings, animated "add-to-cart" effect and ajax fetching of cart data
 * @version	-	1.0.0
 * @author	-	John Wamer
 * @date	-	11/16/2010

 * @notes	-	cart_options = array.  Must be set in config.js or in page, contains basic settings for animated cart functionality
 				
 				cart_options:
 				
 				parameter				desc
 				======================|===========================================
 				
 				dropdown_id			  |	(string)id of element containing list of cart items
 				item_from			  |	(string)id of element the transfer effect will originate from
 				item_to				  |	(string)id of element transfer effect will end at
 				transfer_class		  |	(string)class name of transfer effect element
 				cart_button			  |	(string)id of element containing the cart preview button	
 				dropdown_obj		  |	(boolean) default = 0, indicates if the dropdown is active
 				closetimer			  |	(boolean) default = 0, indicates if the close timer has been started
 				timer				  |	(integer) number of milliseconds before hiding dropdown
 				use_overlay			  | (boolean) default = false, indicates if a overlay will be shown during transfer
 				overlay_active		  |	(boolean) default = false, indicates if there is an active modal instanve
 				overlay_id			  | (string)id of overlay element if applicable
 				overlay_zindex		  |	(integer)z-index to use for overlay is in use
 				items_loaded		  |	(boolean) flag indicates if items have been loaded,prevents duplicate ajax requests
 				add_item			  |	(boolean) flag to indicate if an item is being added to the cart (default = false)
 				item_validated		  |	(boolean) flag to indicate if selected item has been validated (qty, stockingcode, etc..) default = false
 				empty_cart			  |	(boolean) flag to indicate if cart is empty (default = true)
 				delete_item			  |	(boolean) flag to indicate if the last action was to delete an item (default = false)
 				cart_total			  |	id of element that will display cart total 
 				cart_count			  | id of element that will display cart item count
 				silent_load			  |	(boolean) flag to indicate if cart items are being loaded silently (without displaying anything)
 				
 				======================|===========================================
 
 * @requires:	jquery-1.4.2
 				jquery-ui-1.8.2
 				class.modaldialog-1.0.0
 
 */

//============================================================================================================================================

//============================================================================================================================================
function add_to_cart(){

	if(!cart_options.silent_load){
	
		if(cart_options.use_overlay){
			modal						=	new ModalDialog;
			
			modal.overlay				=	'modal-overlay';
		
			modal.overlay_zindex		=	20;
				
			modal.showOverlay();
			
			cart_options.overlay_active	=	true;
		}//end if
		
		if(!cart_options.items_loaded){
		
			cart_options.add_item		=	true;
			cart_options.delete_item	=	false;
	   		$("#"+cart_options.dropdown_id).show('blind', null, 100, get_cart);
	   		
	   		
	   	}
	   	else{
	   		$( "#"+cart_options.dropdown_id).show('blind', null, 500, start_item_transfer);
	   	}


	
	stop_cart_events();
	
	}//end if
	else{
		
		//alert('silent loading');
	
		t		=	setTimeout('add_to_cart()',1000);
	
	}
	

}//end function

function validate_add_to_cart(){
	
	var e				=	new ErrorHandler;
	e.error_return		=	'cart-preview-error';
	
	var error			=	false;
	
	e.ClearErrors();



	if($('#uid').length !=0){
			var uid		=	$('#uid').val();
	}
	else{
		
		var uid		=	0;
		
	}//end else
	
		
	if($('#fid').length !=0){
		var fid		=	$('#fid').val();
	}
	else{
		
		var fid		=	0;
		
	}//end else
		
	if($('#product-id').length !=0){
		var product_id		=	$('#product-id').val();
	}
	else{
		
		var product_id		=	0;
		
	}//end else
		
	if($('#qty').length !=0){
		var product_qty		=	$('#qty').val();
	}
	else{
		
		var product_qty		=	0;
		
	}//end else

	
	if($('#stocking-code').length !=0){
		var stocking_code		=	$('#stocking-code').val();
		
		
	}
	else{
		
		var stocking_code	=	0;
			
	}//end else
		
		
	//start field validation
		
		
		if(uid.length !=128 && uid != 0){
		
			uid = 0;
		
		}
		
		if(fid.length !=32){
		
			e.error_array[e.error_array.length]		=	"INTERNAL ERROR: Invalid FID, please refresh your browser and try again.  Please contact support if issue continues.";
			error									= 	true;
			
		}
		
		if(product_qty.length <1 || product_qty < 1){
		
			e.error_array[e.error_array.length]		=	"INVALID PRODUCT QTY: Please choose the quantity of the item you wish to purchase, must be a minimum of 1.";
			error									= 	true;
			
		}
		
		if(product_id < 1){
		
			e.error_array[e.error_array.length]		=	"INTERNAL ERROR: Invalid ProductID, please refresh your browser and try again.  Please contact support if issue continues.";
			error									= 	true;
			
		
		}//end if
		
		if($('#stocking-code').length !=0 && $('#stocking-code').val() < 1){
		
			e.error_array[e.error_array.length]		=	"PRODUCT OPTIONS NOT SELECTED: Please choose one of the options in the dropdown to select the actual product model you need.";
			error									= 	true;

		
		}
		
		
	//end validation
		
		if(error){
			e.BuildErrors();
		}
		else{
		//API CONNECTION

			cart_options.item_validated	=	true;
			cart_options.delete_item	=	false;
			add_to_cart();
			
		//API CONNECTION
		}



}//end function
	
function start_item_transfer(){

	//build transfer to wrapper
	var div			=	document.getElementById(cart_options.dropdown_id);
	var to			=	document.createElement('div');
	
	to.id			=	cart_options.item_to;
	to.setAttribute("class",cart_options.item_to_class);
	to.setAttribute("className",cart_options.item_to_class);
	
	div.appendChild(to);
	
		
	options = { to: '#'+cart_options.item_to, className: cart_options.transfer_class};
		
	$( "#"+cart_options.item_from).effect('transfer', options, 1200, add_cart_item);
	
	//build html to insert into transfer
	var product_image		=	document.getElementById('product-image-form').value;
				
	html	=	'<img style="border:1px;border-color:#CCCCCC;border-style:solid;margin-top:10px;margin-left:10px;" height="75%" src="/templates/template/product_images/small_images/'+product_image+'"/>';
			
	$('div.'+cart_options.transfer_class).html(html);
		
		
}//end function
	
function end_item_transfer(){
	
	$("#"+cart_options.dropdown_id).hide('blind', null, 500, complete_add_to_cart);
	
		
}//end function
	
function complete_add_to_cart(){
	
	if(cart_options.use_overlay && cart_options.overlay_active){
	
		modal.hideOverlay();
		
		cart_options.overlay_active	=	false;
		
	}//end if
	
	start_cart_events();	

}//end function


function show_cart_preview(){  

	//get_cart();
	
	if(cart_options.dropdown_obj){
		cancel_preview_timer();
	}
	else{	
	   //hide_cart_preview();
	   
	   cart_options.dropdown_obj = $("#"+cart_options.dropdown_id);//.css('display', 'block');
	   
	   if(!cart_options.items_loaded){
	   		$("#"+cart_options.dropdown_id).show('blind', null, 100, get_cart);
	   }
	   else{
	   		$("#"+cart_options.dropdown_id).show('blind', null, 100, null);
	   }

	}
	
}//end function

function hide_cart_preview(){  
	
	if(cart_options.dropdown_obj){
	
		$("#"+cart_options.dropdown_id).hide('blind', null, 200, null);
		cart_options.dropdown_obj	=	0;
		
		hide_cart_loading();
		
	}//end if

}//end function

function cart_preview_timer(){

	cart_options.closetimer = window.setTimeout(hide_cart_preview, cart_options.timer);
	
}//end function

function cancel_preview_timer(){  
	
	if(cart_options.closetimer){  
		
		window.clearTimeout(cart_options.closetimer);
		cart_options.closetimer = null;

	}//end if
	
}//end function

function start_cart_events(){
	
	$(document).ready(function(){  
		
		$("#"+cart_options.cart_button).bind('mouseover', show_cart_preview);
		$("#"+cart_options.cart_button).bind('mouseout', cart_preview_timer);
		$("#"+cart_options.dropdown_id).bind('mouseover', cancel_preview_timer);
		$("#"+cart_options.dropdown_id).bind('mouseout',  cart_preview_timer);
		
	});


}//end function

function stop_cart_events(){

	$("#"+cart_options.cart_button).unbind('mouseover', false);
	$("#"+cart_options.cart_button).unbind('mouseout', false);
	$("#"+cart_options.dropdown_id).unbind('mouseover', false);
	$("#"+cart_options.dropdown_id).unbind('mouseout',  false);

	
}//end function

function show_cart_loading(){

	$('#cart-loading-msg-wrapper').remove();
	
		if(!cart_options.add_item){
			$('ul.cart-item-wrapper').remove();
		}//end if

	var div			=	document.getElementById(cart_options.dropdown_id);
	
	var new_div		=	document.createElement('div');
	var img			=	document.createElement('img');
	var p			=	document.createElement('p');
	
	new_div.id		=	"cart-loading-msg-wrapper";
	
	img.src			=	'/templates/coachyouths_template/images/pngs/circle_processing_image_50x50.gif';
	img.id			=	'cart-loading-image';
	
	
	
	p.appendChild(img);
	
	

	
	new_div.appendChild(p);
	var p			=	document.createElement('p');
	
	p.innerHTML		=	"Loading Cart items";
	
	new_div.appendChild(p);
	
	div.appendChild(new_div);
	
}//end function

function hide_cart_loading(){
	
	if(cart_options.items_loaded){
	
		$('#cart-loading-msg-wrapper').remove();
			
	}

}//end function

function get_cart(){

	var uid				=	document.getElementById('uid').value;
	var fid				=	document.getElementById('fid').value;
	
	if(uid.length == 128){

		show_cart_loading();
		
		//$('ul.cart-item-wrapper').remove();
		
		//API CONNECTION
	
				var	request					=	new	AjaxRequest();
				var cmd						=	"GetCart";
					
				request.url					=	"/connectors/ajax-connector.php?Command="+cmd; 
				request.url					=	request.url+"&UID="+uid;
				request.url					=	request.url+"&FID="+fid;
						
				
				request.callback			=	get_cart_callback;
				request.error_callback		=	get_cart_error_callback;
				
				var runnit					=	request.ProcessRequest();
				
		//API CONNECTION

	}//end if
	else{
		
		//if(!cart_options.silent_load){
			//alert('empty cart');
			show_empty_cart();
			cart_options.silent_load	=	false;
		//}
	}//end else
}//end function

function get_cart_callback(data,xml,uid,fid,options_array){


var arr = xml2array(xml);
	
	try{
		if(arr['APIErrorResponse'].length !=0){
		
			if(parseInt(arr['APIErrorResponse']['APIErrorNumber']) == 241){
				
				cart_options.empty_cart		=	true;
				cart_options.items_loaded	=	true;
				
				//update cart display
				$('#'+cart_options.cart_count).html('0');
				$('#'+cart_options.cart_total).html('0.00');

				
				show_empty_cart();

				
				if(cart_options.delete_item){
							
					var t	=	setTimeout('end_item_transfer()',1000);
							//$("#"+cart_options.dropdown_id).hide('blind', null, 2200, null);
						
				}//end if
				
				
			}
			else{
				
				if(!cart_options.silent_load){
					var e				=	new ErrorHandler;
			
					e.error_return		=	'cart-preview-error';	
					
					e.error_array[e.error_array.length]		=	arr['APIErrorResponse']['APIErrorMessage']+' - ('+arr['APIErrorResponse']['APIErrorNumber']+')';
					
					e.BuildErrors();
				}//end if
			}//end else
			
		}//end if
		else{
			//alert("no error");
		}//end else
	}//end try
	catch(e){
	
	
		var items_count			=	parseInt(arr.GetCartResponse.CartCount);
		var i					=	0;
		var html				=	"";
		
		var div					=	document.getElementById(cart_options.dropdown_id);
		var ul;
		var li;
		
			if(items_count > 1){
				
				//update cart display
				$('#'+cart_options.cart_count).html(arr.GetCartResponse.CartCount);
				$('#'+cart_options.cart_total).html(arr.GetCartResponse.CartTotal);

				cart_options.empty_cart		=	false;
				cart_options.items_loaded	=	true;
				
				hide_cart_loading();
				
				while(items_count > i){
				//alert('wtf');
					
					
					
					ul				=	document.createElement('ul');
					ul.setAttribute("class","cart-item-wrapper");
					ul.setAttribute("className","cart-item-wrapper");
					ul.id			=	'cart-id-'+arr.GetCartResponse.CartDetail.CartLine[i].CartID;
					
					li				=	document.createElement('li');
					
					li.setAttribute("class","product-image");
					li.setAttribute("className","product-image");
					li.innerHTML	=	'<img width="50%" height="50%" src="/templates/template/product_images/small_images/'+arr.GetCartResponse.CartDetail.CartLine[i].ProductImage+'"/>';

					ul.appendChild(li);
					
					li				=	document.createElement('li');
					
					li.setAttribute("class","product-name");
					li.setAttribute("className","product-name");
					li.innerHTML	=	arr.GetCartResponse.CartDetail.CartLine[i].ProductName;

					ul.appendChild(li);
					
					li				=	document.createElement('li');
					
					li.setAttribute("class","product-qty");
					li.setAttribute("className","product-qty");
					li.innerHTML	=	'<strong>Qty:</strong> '+arr.GetCartResponse.CartDetail.CartLine[i].ProductQty;

					ul.appendChild(li);
					
					li				=	document.createElement('li');
					
					li.setAttribute("class","product-cost");
					li.setAttribute("className","product-cost");
					li.innerHTML	=	'<strong>Cost:</strong> $'+arr.GetCartResponse.CartDetail.CartLine[i].LineTotal;

					ul.appendChild(li);
					
					li				=	document.createElement('li');
					
					li.setAttribute("class","delete-button");
					li.setAttribute("className","delete-button");
					li.innerHTML	=	'<img src="/templates/coachyouths_template/images/pngs/blue_delete_button.png" onclick="remove_cart_item('+"'"+arr.GetCartResponse.CartDetail.CartLine[i].CartID+"'"+');"/>';

					ul.appendChild(li);

					div.appendChild(ul);
					
				i++;
				}//end while
				
					if(cart_options.delete_item){
						
						var t	=	setTimeout('end_item_transfer()',1000);
											
					}//end if

			
			
			}//end if
			else if(items_count == 1){
				
				//update cart display
				$('#'+cart_options.cart_count).html(arr.GetCartResponse.CartCount);
				$('#'+cart_options.cart_total).html(arr.GetCartResponse.CartTotal);
				
				cart_options.empty_cart		=	false;
				cart_options.items_loaded	=	true;
				
				hide_cart_loading();

			
					ul				=	document.createElement('ul');
					ul.setAttribute("class","cart-item-wrapper");
					ul.setAttribute("className","cart-item-wrapper");
					ul.id			=	'cart-id-'+arr.GetCartResponse.CartDetail.CartLine.CartID;

					li				=	document.createElement('li');
					
					li.setAttribute("class","product-image");
					li.setAttribute("className","product-image");
					li.innerHTML	=	'<img width="50%" height="50%" src="/templates/template/product_images/small_images/'+arr.GetCartResponse.CartDetail.CartLine.ProductImage+'"/>';

					ul.appendChild(li);
					
					li				=	document.createElement('li');
					
					li.setAttribute("class","product-name");
					li.setAttribute("className","product-name");
					li.innerHTML	=	arr.GetCartResponse.CartDetail.CartLine.ProductName;

					ul.appendChild(li);
					
					li				=	document.createElement('li');
					
					li.setAttribute("class","product-qty");
					li.setAttribute("className","product-qty");
					li.innerHTML	=	'<strong>Qty:</strong> '+arr.GetCartResponse.CartDetail.CartLine.ProductQty;

					ul.appendChild(li);
					
					li				=	document.createElement('li');
					
					li.setAttribute("class","product-cost");
					li.setAttribute("className","product-cost");
					li.innerHTML	=	'<strong>Cost:</strong> $'+arr.GetCartResponse.CartDetail.CartLine.LineTotal;

					ul.appendChild(li);
					
					li				=	document.createElement('li');
					
					li.setAttribute("class","delete-button");
					li.setAttribute("className","delete-button");
					li.innerHTML	=	'<img src="/templates/coachyouths_template/images/pngs/blue_delete_button.png" onclick="remove_cart_item('+"'"+arr.GetCartResponse.CartDetail.CartLine.CartID+"'"+');"/>';

					ul.appendChild(li);

					div.appendChild(ul);
					
						if(cart_options.delete_item){
							
							var t	=	setTimeout('end_item_transfer()',1000);
												
						}//end if


			
			}//end else if
			else if(items_count < 1){
				
				//update cart display
				$('#'+cart_options.cart_count).html('0');
				$('#'+cart_options.cart_total).html('0.00');
				
				cart_options.empty_cart		=	true;
				cart_options.items_loaded	=	true;
				
				hide_cart_loading();
				
					if(cart_options.delete_item){
						
						var t	=	setTimeout('end_item_transfer()',1000);
											
					}//end if


			
			}//end else if
			else{
			
			
			}//end else
			
		
		cart_options.items_loaded	=	true;
		
		if(!cart_options.silent_load){				
			//start item transfer if add to cart is true
			if(cart_options.add_item){
								
				start_item_transfer();
							
			}//end if
			else{
				show_cart_buttons();
			}//end else
		}//end if
		else if(cart_options.silent_load){
			show_cart_buttons();
		}
	}//end catch
	
	//only needs to run silent once
	cart_options.silent_load	=	false;

}//end function

function get_cart_error_callback(code,msg){


}

function show_empty_cart(){

	cart_options.items_loaded	=	true;
	cart_options.empty_cart		=	true;
	
	hide_cart_loading();
	
	var div					=	document.getElementById(cart_options.dropdown_id);
	var button_div			=	document.getElementById('cart-button-wrapper');
	
	//clear div - avoid duplicate buttons
	button_div.innerHTML	=	"";
	
	var p		=	document.createElement('p');
	
	p.id			=	'empty-cart-msg';
	p.innerHTML		=	'Your shopping cart is empty';
	
	div.appendChild(p);

					
		//start item transfer if add to cart is true
		if(cart_options.add_item){
							
			start_item_transfer();
						
		}//end if

}

function hide_empty_cart(){

	if(!cart_options.empty_cart){
		
		$('#empty-cart-msg').remove();
	
	}//end if

}//end function

function add_cart_item(){
	
			
		if(!cart_options.item_validated){
			validate_add_to_cart();
		}
		else{
			
			cart_options.add_item		=	true;
			cart_options.delete_item	=	false;
			//show loading message until add is complete
			show_cart_loading();
		
			var uid						=	$('#uid').val();
			var fid						=	$('#fid').val();
			var id						=	$('#product-id').val();
			var qty						=	$('#qty').val();
			
				if($('#stocking-code').length !=0){
					var stocking_code	=	$('#stocking-code').val();
				}
				else{
					var stocking_code	=	0;
				}
				
		//API CONNECTION

			var	request					=	new	AjaxRequest();
			var cmd						=	"AddtoCart";
				
			request.url					=	"/connectors/ajax-connector.php?Command="+cmd;
				
				if(uid !=0 && uid.length == 128){
					
					request.url					=	request.url+"&UID="+uid;
					request.url					=	request.url+"&FID="+fid;

				}//end if			
 
			
			request.url					=	request.url+"&ProductID="+id;
			request.url					=	request.url+"&Qty="+qty;
			
				if(stocking_code !=0){
					request.url					=	request.url+"&StockingCode="+stocking_code;
				}//end if

					
			//alert(request.url);
			
			request.callback			=	add_cart_item_callback;
			request.error_callback		=	get_cart_error_callback;
			
			var runnit					=	request.ProcessRequest();
			
		//API CONNECTION
		}


}//end function

function add_cart_item_callback(data,xml,uid,fid,options_array){

	var arr = xml2array(xml);
	
	try{
		if(arr['APIErrorResponse'].length !=0){
			
			var e				=	new ErrorHandler;
	
			e.error_return		=	'cart-preview-error';	
			
			e.error_array[e.error_array.length]		=	arr['APIErrorResponse']['APIErrorMessage']+' - ('+arr['APIErrorResponse']['APIErrorNumber']+')';
			
			e.BuildErrors();
			
			end_item_transfer();

			
		}//end if
		else{
			//alert("no error");
		}//end else
	}//end try
	catch(e){
		
		//alert(data);
		
		if(arr.AddtoCartResponse.UID.length == 128){
			
			//update uid and fid
			$('#uid').val(arr.AddtoCartResponse.UID);
			$('#fid').val(arr.AddtoCartResponse.FID);
			
			//update cart display
			$('#'+cart_options.cart_count).html(arr.AddtoCartResponse.CartCount);
			$('#'+cart_options.cart_total).html(arr.AddtoCartResponse.CartTotal);


		
			cart_options.empty_cart		=	false;
			cart_options.items_loaded	=	true;
		
			var product_id			=	document.getElementById('product-id').value;
			var product_name		=	document.getElementById('product-name').value;
			var product_image		=	document.getElementById('product-image-form').value;
			var product_price		=	document.getElementById('product-price').value;
			var product_qty			=	document.getElementById('qty').value;
			
			var div					=	document.getElementById(cart_options.dropdown_id);
		
			ul				=	document.createElement('ul');
			ul.setAttribute("class","cart-item-wrapper");
			ul.setAttribute("className","cart-item-wrapper");
			ul.id			=	'cart-id-'+arr.AddtoCartResponse.CartID;

						
			li				=	document.createElement('li');
						
			li.setAttribute("class","product-image");
			li.setAttribute("className","product-image");
			li.innerHTML	=	'<img width="50%" height="50%" src="/templates/template/product_images/small_images/'+product_image+'"/>';
	
			ul.appendChild(li);
						
			li				=	document.createElement('li');
						
			li.setAttribute("class","product-name");
			li.setAttribute("className","product-name");
			li.innerHTML	=	product_name;
	
			ul.appendChild(li);
						
			li				=	document.createElement('li');
						
			li.setAttribute("class","product-qty");
			li.setAttribute("className","product-qty");
			li.innerHTML	=	'<strong>Qty:</strong> '+product_qty;
	
			ul.appendChild(li);
						
			li				=	document.createElement('li');
						
			li.setAttribute("class","product-cost");
			li.setAttribute("className","product-cost");
			li.innerHTML	=	'<strong>Cost:</strong> $'+(product_qty * product_price);
	
			ul.appendChild(li);
						
			li				=	document.createElement('li');
						
			li.setAttribute("class","delete-button");
			li.setAttribute("className","delete-button");
			li.innerHTML	=	'<img src="/templates/coachyouths_template/images/pngs/blue_delete_button.png" onclick="remove_cart_item('+"'"+arr.AddtoCartResponse.CartID+"'"+');"/>';
	
			ul.appendChild(li);
	
			div.appendChild(ul);
			
			hide_empty_cart();
			
			//remove item_to div
			$('.'+cart_options.item_to_class).remove();
			
			
			hide_cart_loading();
			//end_item_transfer();
			
			show_cart_buttons();
			
			
			//automatically continue shopping after delay
			var t	=	setTimeout('continue_shopping()',5000);

		}//end if
		else{
		
			alert('an error occured');
		
		}//end else
	}//end catch


	

}//end function

function show_cart_buttons(){

	var div					=	document.getElementById('cart-button-wrapper');
	
	//clear div - avoid duplicate buttons
	div.innerHTML			=	"";
	
	var continue_img		=	document.createElement('img');
	var checkout_img		=	document.createElement('img');
	
	continue_img.id			=	'cart-continue-button';
	continue_img.src		=	'/templates/coachyouths_template/images/pngs/continue_shopping.png'
	continue_img.onclick	=	continue_shopping;
	
	checkout_img.id			=	'cart-checkout-button';
	checkout_img.src		=	'/templates/coachyouths_template/images/pngs/checkout.png';
	checkout_img.onclick	=	goto_checkout;
	
	div.appendChild(continue_img);
	div.appendChild(checkout_img);
	
}//end function

function remove_cart_item(id){

	cart_options.delete_item	=	true;
	
	var uid				=	document.getElementById('uid').value;
	var fid				=	document.getElementById('fid').value;
	
	cart_options.add_item	=	false;
	
	if(uid.length == 128){
	
		$( "#cart-id-"+id).hide('highlight', null, 500, show_cart_loading);
		
		
		
		
		//API CONNECTION
	
				var	request					=	new	AjaxRequest();
				var cmd						=	"RemoveCartItem";
					
				request.url					=	"/connectors/ajax-connector.php?Command="+cmd; 
				request.url					=	request.url+"&UID="+uid;
				request.url					=	request.url+"&FID="+fid;
				request.url					=	request.url+"&CartID="+id;
						
				
				request.callback			=	remove_cart_item_callback;
				request.error_callback		=	remove_cart_item_error_callback;
				
				var runnit					=	request.ProcessRequest();
				
		//API CONNECTION

	}//end if
	else{
	
		//alert('empty cart');
		show_empty_cart();
	
	}//end else
	

	

}//end function

function remove_cart_item_callback(data,xml,uid,fid,options_array){

	//alert(data);
	var arr = xml2array(xml);
	
	try{
		if(arr['APIErrorResponse'].length !=0){
		
			if(parseInt(arr['APIErrorResponse']['APIErrorNumber']) == 241){
				
				cart_options.empty_cart		=	true;
				cart_options.items_loaded	=	true;
				
				//update cart display
				$('#'+cart_options.cart_count).html('0');
				$('#'+cart_options.cart_total).html('0.00');

				
				show_empty_cart();
			
				
			}//end if
			else{
				var e				=	new ErrorHandler;
		
				e.error_return		=	'cart-preview-error';	
				
				e.error_array[e.error_array.length]		=	arr['APIErrorResponse']['APIErrorMessage']+' - ('+arr['APIErrorResponse']['APIErrorNumber']+')';
				
				e.BuildErrors();
			}//end else
			
			
			
			//end_item_transfer();

			
		}//end if
		else{
			//alert("no error");
		}//end else
	}//end try
	catch(e){
		
		
			if(arr.RemoveItemResponse.Status=='Success'){
				get_cart();
			}
		
	}//end catch


	

}//end function

function remove_cart_item_error_callback(){

	alert('error occured');
}



function continue_shopping(){

	$("#"+cart_options.dropdown_id).hide('blind', null, 500, null);
	
	stop_cart_events();
	
		if(!cart_options.overlay_active){
	
			modal						=	new ModalDialog;
						
			modal.overlay				=	'modal-overlay';
					
			modal.overlay_zindex		=	50;
							
			modal.showOverlay();
		
		}
		else{
			$('#'+modal.overlay).css("z-index","50");
		}
	
	new_url		=	window.location.pathname+"?uid="+$('#uid').val()+"&fid="+$('#fid').val();
	
	//alert(new_url);
	
	window.location = new_url;

}//end function

function goto_checkout(){

	$("#"+cart_options.dropdown_id).hide('blind', null, 500, null);
	
	stop_cart_events();
	
		if(!cart_options.overlay_active){
	
			modal						=	new ModalDialog;
						
			modal.overlay				=	'modal-overlay';
					
			modal.overlay_zindex		=	50;
							
			modal.showOverlay();
		
		}
		else{
			
			$('#'+modal.overlay).css("z-index","50");
			
		}

	
	new_url		=	"/checkout/view-cart/?uid="+$('#uid').val()+"&fid="+$('#fid').val();
	
	window.location = new_url;
}//end function

function silent_load_cart(){
	
	$(document).ready(function(){  
		
		stop_cart_events();
		
		cart_options.silent_load	=	true;
		get_cart();
		
		//alert('cart silent loaded');
		
		
		start_cart_events();
		
		
	});

	
	

}//end function


