// JavaScript Document
function cart_update(action, key){

	var id = '';
	var option = '';
	var quantity = '';
	if(key==undefined) var key = 0;
	key = key*1;	
	
	if(action == 'add'){ // add
		
		id = $('#product-id').text();
		if($('#product-option').val() != undefined) option = $('#product-option').val();
		quantity = $('#product-quantity').val();
	
		$('#alert-wrap').html('<div id="alert-fade-out" class="alert-fade-out red t16 tcenter">'+$('#product-title').text()+' added to Cart.</div>');		
		$('.cart-link').show();
		$('#add-to-view-cart').html('View Cart &raquo;').attr({'onclick':'','href':rw_root_url+'cart'});
		
		get_cart_update(id,quantity,option,action);
	}
	
	if(action == 'update'){ // update

		if($('#cart-item-' + key + '-id').length > 0){
			var price = $('#cart-item-' + key + '-price').text();
			quantity = $('#cart-item-' + key + '-quantity').val()*1;
			id = $('#cart-item-' + key + '-id').text()*1;
			option = $('#cart-item-' + key + '-option').text();
			if(price*1 > 0)
				$('#cart-item-' + key + '-total-price p').
				text('$'+ (price*quantity).toFixed(2));
		}
		
		get_cart_update(id,quantity,option,action);
	}
	
	if(action == 'remove'){ // remove
		quantity = 0;
		id = $('#cart-item-' + key + '-id').text()*1;
		option = $('#cart-item-' + key + '-option').text();
		$('#cart-row-'+key).fadeOut(300,function(){
			$(this).remove();
			get_cart_update(id,quantity,option,action);
		});
	}
	
	
}
function get_cart_update(id,quantity,option,action){
	$.get(
		rw_root_url+'process/process=get_cart_update/argument='+id+':'+quantity+':'+option+':'+action+
		'/application=e-commerce',
		function(cart_count){
			$('.cart-count').text(cart_count);
			$('.js-items-in-cart').hide();
			$('#checkout-forms').show();
			if(cart_count*1==0){
				$('#cart-table').hide();
				$('#no-items-in-cart').show();
				$('#checkout-forms').hide();
			}else{
				$('.js-items-in-cart').show();
			}
		}
	);
}
function show_password_form(){
	$('#sign-in-form').hide();
	$('#lost-password-form').show();
}
function email_cart(){
	make_popup('#email-cart-form', $('#cart-table').height()+150, 650);
	return false;
}
function email_cart_send(){

	if(valid_email($('#email-cart-input').val())){
		var email = $('#email-cart-input').val();
		$('#email-cart-results').html('<p class="tcenter"><img src="'+rw_root_url+rw_theme_folder+'images/ajax-loader.gif" alt=""/> Sending cart.</p>');
		$('#email-cart-results').load(rw_root_url+'process/process=cart_email/argument='+obfuscate(email)+'/application=e-commerce');
	}else{
		$('#email-cart-results .error').html('<p class="tcenter red">Please enter a valid email address!</p>');
	}
	return false;
}
function representative_cart(){
}
function print_cart(){
}
$(document).ready(function(){						  
	if($('.alert-fade-out').length > 0)
	setTimeout("$('.alert-fade-out').fadeOut('slow');",15000); 

	if(rw_url_bits == null)
	return false;
	
	if(rw_url_bits['form'] == 'forgot-password')
		show_password_form();
});
