//var cart;
//var cart_error;
var cart_options = new Object();
var page_function = 'updateCart';
var order_id = '';

function toggleCart() {
	if($("div#divVc").is(":hidden")) {
		$("div#divVc").slideDown();
		$("input.vcUpdate").show();
		$("input.vcToggle").val('[-]');
		cart_options["show_cart"] = true;
	} else {
		$("div#divVc").slideUp();
		$("input.vcUpdate").hide();
		$("input.vcToggle").val('[+]');
		cart_options["show_cart"] = false;
	}
}

function changeShipping(callback) {
	if( $("select.cart_ship_choose").val() != -1) {
		var opts = new Object();
		opts['cart_ship_cost'] = false;
		if(typeof(callback) == 'function') {
			opts.cart_callback = callback;	
		}
		updateCart(opts);
	}
}

function updateCartState(result) {
	var result_array = result.split('||||');
	$("div#cart_wrapper").html(result_array[0]);
	$("div.cart_loader").hide();
	$("div#checkout_error_box").hide();
	//alert(result_array[1]);
	//alert(result_array[2]);
	eval("cart=" + result_array[1]); 
	eval("cart_error=" + result_array[2]); 
	//eval("cart_warning=" + result_array[3]);
		
	showCartErrors();
	
}

function showCartErrors() {
	hide_error();
	
	var error_string = '';
	for(var id in cart_error) {
		if(!objPropExists(cart,id)) {
			error_string += cart_error[id]['Model'] + ": " + cart_error[id]['Error'] + "<br/>";
		}
	}
//alert(error_string);
	show_error(error_string);
}

function removeCartItem(id) {
	$("div.cart_loader").show();
	
	var cart = new Object();	
	cart[id] = new Object();
	cart[id].ProductID = $("tr.product_" + id + " input.line_item_id").val();
	cart[id].Qty = 0;//$("tr.product_" + id + " input.line_item_qty").val();
	//cart[id].Price = 0;//$("tr.product_" + id + " input.line_item_unit_price").val();
	
	var cartJSON = $.toJSON(cart);
	var cartOptsJSON = $.toJSON(cart_options);
	//alert('here');
	//alert(constructQueryString(page_function,order_id));
	//alert(cartJSON);
	$.ajax({
		type: "POST",
		url: "/includes/ajax_functions.php",
		data: constructQueryString(page_function,order_id,cartJSON,cartOptsJSON),
		dataType: "html",
		success: function(result) {
			updateCartState(result);
		}
	});	
}



function updateCart(opts) {
	if(typeof(opts) == 'undefined') { opts = {}; }
	$("div.cart_loader").show();
	
	var cart = new Object();
	var cart_vals = new Object();
	
	$("tr.productrow").each(function() {
		var id = $(this).find("input.line_item_id").val();
		cart[id] = new Object();
		cart[id].ProductID = $(this).find("input.line_item_id").val();
		cart[id].Qty = $(this).find("input.line_item_qty").val();
		cart[id].Price = $(this).find("input.line_item_unit_price").val();
		cart[id].Notes = $(this).find(".line_item_notes").val();
		cart[id].Name = $(this).find("input.line_item_name").val();
		cart[id].Cost = $(this).find("input.line_item_cost_price").val();
	});	
	
	//if we are on checkout2 and there is a shipping select
	if($("select.cart_ship_choose").length > 0) {
		cart_vals['ship_id'] = $("select.cart_ship_choose").val();
		//if we are changing the shipping option, the value comes from ajax not from input field
		if(objPropExists(opts,"cart_ship_cost")) {
			cart_vals['ship_cost'] = opts["cart_ship_cost"];
		//otherwise, the value comes from the input field (recalculate link)
		} else {
			cart_vals['ship_cost'] = $("input.cart_ship_cost").val();
		}
	//we are on a page besides checkout2, do nothing with shipping
	} else if ( opts.cart_vieworder ) {
		cart_vals['ship_id'] = $("input.cart_ship_id").val();
		cart_vals['ship_cost'] = $("input.cart_ship_cost").val();
	} else {
		cart_vals['ship_id'] = -1;
		cart_vals['ship_cost'] = false;
	}
	
	//if we are on checkout2 and there is a tax input
	if( $("input.cart_tax_cost").length > 0) {
		cart_vals['tax_cost'] = $("input.cart_tax_cost").val();
	} else {
		cart_vals['tax_cost'] = false;	
	}
	
	//if we are on checkout2 and there is a discount input
	if( $("input.cart_discount_amt").length > 0) {
		cart_vals['discount_amt'] = $("input.cart_discount_amt").val();
		cart_vals['discount_comments'] = $("input.cart_discount_comments").val();
	} else {
		cart_vals['discount_amt'] = false;	
		cart_vals['discount_comments'] = false;
	}
	
	//if we are on checkout2 and there is a surcharge input
	if( $("input.cart_surcharge_amt").length > 0) {
		cart_vals['surcharge_amt'] = $("input.cart_surcharge_amt").val();
		cart_vals['surcharge_comments'] = $("input.cart_surcharge_comments").val();
	} else {
		cart_vals['surcharge_amt'] = false;	
		cart_vals['surcharge_comments'] = false;
	}
	
	
	var cartJSON = $.toJSON(cart);
	var cartOptsJSON = $.toJSON(cart_options);
	var cartValsJSON = $.toJSON(cart_vals);
	
	$.ajax({
		type: "POST",
		url: "/includes/ajax_functions.php",
		data: constructQueryString(page_function,order_id,cartJSON,cartOptsJSON,cartValsJSON),
		dataType: "html",
		success: function(result) {
			updateCartState(result);
			if(objPropExists(opts,"cart_callback") && typeof(opts.cart_callback) == "function") {
				opts.cart_callback();
			}
		}
	});	
}
function addCustomItem(data) {
	$("div.cart_loader").show();
	var id = data.ProductID;
	
	var cart = new Object();	
	cart[id] = new Object();
	cart[id].ProductID = data.ProductID;
	cart[id].Qty = data.Qty;
	cart[id].Price = data.Price;
	cart[id].Notes = '';
	
	var cartJSON = $.toJSON(cart);
	var cartOptsJSON = $.toJSON(cart_options);
	//alert(cartJSON);
	$.ajax({
		type: "POST",
		url: "/includes/ajax_functions.php",
		data: constructQueryString('addCart',cartJSON,cartOptsJSON),
		dataType: "html",
		success: function(result) {
			updateCartState(result);
		}
	});		
}

function addCartItem(id) {
	$("div.cart_loader").show();
	
	var cart = new Object();	
	cart[id] = new Object();
	cart[id].ProductID = $("tr#product_" + id + " input.line_item_id").val();
	cart[id].Qty = $("tr#product_" + id + " input.line_item_qty").val();
	cart[id].Price = $("tr#product_" + id + " input.line_item_unit_price").val();
	cart[id].Notes = '';
	
	var cartJSON = $.toJSON(cart);
	var cartOptsJSON = $.toJSON(cart_options);
	//alert(cartJSON);
	$.ajax({
		type: "POST",
		url: "/includes/ajax_functions.php",
		data: constructQueryString('addCart',cartJSON,cartOptsJSON),
		dataType: "html",
		success: function(result) {
			updateCartState(result);
		}
	});	
}

function addCartItems() {
	$("div.cart_loader").show();
	
	var cart = new Object();
	$("tr.add_productrow input.select_product:checked").each(function() {
		var row = $(this).parents("tr.add_productrow");
		var id = row.find("input.line_item_id").val();
		cart[id] = new Object();
		cart[id].ProductID = row.find("input.line_item_id").val();
		cart[id].Qty = row.find("input.line_item_qty").val();
		cart[id].Price = row.find("input.line_item_unit_price").val();
		cart[id].Notes = '';
	});	
	
	var cartJSON = $.toJSON(cart);
	var cartOptsJSON = $.toJSON(cart_options);
	//alert(cartJSON);
	
	$.ajax({
		type: "POST",
		url: "/includes/ajax_functions.php",
		data: constructQueryString('addCart',cartJSON,cartOptsJSON),
		dataType: "html",
		success: function(result) {
			updateCartState(result);
			$("tr.add_productrow input.select_product:checked").attr("checked",false).parent().parent().removeClass("rowSelected");
		}
	});	
}

function applyCoupon() {
	var coupon_code = $('input.cart_coupon_code').val();
	var cartOptsJSON = $.toJSON(cart_options);
	$("div.cart_loader").show();
	
	$.ajax({
		type: "POST",
		url: "/includes/ajax_functions.php",
		data: constructQueryString('applyCouponCode',coupon_code,cartOptsJSON),
		dataType: "html",
		success: function(result) {
			$("div#cart_wrapper").html(result);
			$("div.cart_loader").hide();
		}
	});		
}

function removeCoupon() {
	var cartOptsJSON = $.toJSON(cart_options);
	$("div.cart_loader").show();
	
	$.ajax({
		type: "POST",
		url: "/includes/ajax_functions.php",
		data: constructQueryString('removeCouponCode',cartOptsJSON),
		dataType: "html",
		success: function(result) {
			$("div#cart_wrapper").html(result);
			$("div.cart_loader").hide();
		}
	});		
}


