var path     = "";
var currency = "usd"; 
//var admin = "";
//var orderID;
function updateCart(act, prodID, prodCount, cartType, orderID){
		act       = act || "update";
		prodID    = prodID || 0;
		orderID   = orderID || 0;
		prodCount = prodCount || 0;
		cartType  = cartType || "small";
		currency  = currency || "usd";
		$.post(
			path+"ajax/cart.php",
			{
    			action:      act,
    			productID:   prodID,
    			count:       prodCount,
    			type:        cartType,
    			orderID:     orderID,
    			currency:    currency
  			},
  			function(data){
  				//alert(data);
  				if (cartType == "small"){
  					$("#cartDiv").html(data);
  					if (act == "add"){
  						$("#cartOK").modal();
  						$("#modalContainer").animate({
  							height: "150px"
  							}, 200);
  					}
  				}else{
  					$("#divBigCart").html(data);
  					//update small cart
  					updateCart();
  				}	
  			}
    	);
}

function sendOrder(ordID){
	orderID     = ordID || 0;
	//alert($('#accepted :selected').val());
	$.post(
			path+"ajax/cart.php",
			{
    			firstname:   $("#firstname").attr("value"),
    			lastname:    $("#lastname").attr("value"),
    			email:       $("#email").attr("value"),
    			phone:       $("#phone").attr("value"),
    			address:     $("#address").attr("value"),
    			description: $("#description").attr("value"),
    			accepted:    $('#accepted :selected').val() || 0,
    			action:      "order",
    			type:        "big",
    			orderID:     orderID
  			},
  			orderOnAjaxSuccess
 	);
}

function orderOnAjaxSuccess(data){
	
	//alert(data);
	//alert ($("ok", data));
	
	if ($("ok", data).html()){
  		$("#divForm").empty();
  		$("#divBigCart").html("<center><h3>Заказ принят!</h3></center>");
  		if ($("#tr"+orderID).html()){
  			$("#tr"+orderID).empty();
  		}
  		//update small cart
  		updateCart();
  	}

	if ($("errors", data).html()){
		var error = "";
  		$("err", data).each(
  			function()
  			{
    			error += "-" + (this.innerHTML) + "\n";
  			}
		);
		if (error){
			alert("Найдены следущие ошибки: \n" + error);
		}
	}	
}

function addToCart(prodID){
	var count = $("#productCount"+prodID).val();
	updateCart("add", prodID, count);
}

function addNewToCart(){
	count = $("#newProductCount").attr("value");
	prodID = $("#newProductID").attr("value");
	updateCart("add", prodID, count, "big");
}

function updateProduct(prodID){
	count = $("#productCount"+prodID).attr("value");
	updateCart("update", prodID, count);
}

function updateProductBigCart(prodID){
	count = $("#productBigCartCount"+prodID).attr("value");
	updateCart("update", prodID, count, 'big');
}

function showHideOrder(){
	if ($("#cartOrder").css("display") == "none"){
		//$('html,body').animate({scrollTop: 70}, 1000);
		$("#cartProducts").hide(); 
		$("#cartOrder").slideDown(200);
		$("#buttonShowHideOrder").attr("value", "Показать товары");
	} else{
		//$('html,body').animate({scrollTop: $(window).height()}, 1000);
		$("#cartOrder").hide();
		$("#cartProducts").slideDown(200);
		$("#buttonShowHideOrder").attr("value", "Перейти к оформлению заказа");
	}
}
