function Ajax(file, data, div)
{
	$.ajax({
	   type: "POST",
	   url: file,
	   data: data,
	   cache: false,
	   success: function(html){
	     $("#"+div).html(html);
	   }
	});

}

Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;
Date.ONE_DAY = Date.ONE_HOUR * 24;
function nap (m) {var then = new Date(new Date().getTime() + m); while (new Date() < then) {}}

function AjaxFade(file, data, div)
{
	$("#"+div).fadeOut("slow");
	$.ajax({
	   type: "POST",
	   url: file,
	   data: data,
	   cache: false,
	   success: function(html){
	     $("#"+div).html(html);
		 $("#"+div).fadeIn("slow");
	   }
	});

}


/*za iz4islqvane na inside div kolko % e*/
var res = 0;
function calculate_res()
{
	res = (99 - ((410 / screen.width) *100)).toFixed();
	/*alert(res);*/
	if(res > 50)
	{
		$("#inside_div").css("width",res + "%");
	}
}

function AjaxAppend(file, data, div)
{
	$.ajax({
	   type: "POST",
	   url: file,
	   data: data,
	   cache: false,
	   success: function(html){
	     $("#"+div).append(html);
	   }
	});
}


function reloadAjax(file, data, div)
{
	 AjaxAppend(file, data, div);
	 setTimeout('reloadAjax("'+file+'","'+ data+'","'+ div+'")', 2000);
}


function delDiscount(discount_id)
{
	 var product_id = $("#id").val();
	 var data = '';
	 data ="product_id="+product_id+"&discount_id="+discount_id;
	 Ajax("ajax_php/discounts_handle.php", data, "discounts");
}

function addDiscount()
{	 var data = '';
	 var product_id = $("#id").val();
	 var qty_from = $("#qty_from").val();
	 var qty_to = $("#qty_to").val();
	 var discount = $("#discount").val();
	 var shop_id = $("#shop_id").val();

	 data ="product_id=" + product_id + "&qty_from="+qty_from + "&qty_to="+qty_to + "&discount="+discount + "&shop_id="+shop_id + "&mode=add";
	 Ajax("ajax_php/discounts_handle.php", data, "discounts");
}

function delPriceProfile(price_profile_id)
{
	 var user_id = $("#id").val();
	 var shop_id = $("#shop_id").val();
	 var data = '';
	 data = "shop_id=" + shop_id + "&user_id="+user_id+"&price_profile_id="+price_profile_id;
	 Ajax("ajax_php/price_profiles_handle.php", data, "price_profiles");
}

function addPriceProfile()
{	 var data = '';
	 var user_id = $("#id").val();
	 var price_type_id = $("#price_type_id").val();
	 var supplier_id = $("#supplier_id").val();
	 var product_type_id = $("#product_type_id").val();
	 var product_id = $("#product_id").val();
	 var price = $("#price").val();
	 var shop_id = $("#shop_id").val();

	 data ="shop_id=" + shop_id + "&user_id=" + user_id + "&price_type_id="+price_type_id + "&supplier_id="+supplier_id + "&product_type_id="+product_type_id + "&product_id="+product_id + "&price="+ price + "&mode=add";
	 alert(data);
	 Ajax("ajax_php/price_profiles_handle.php", data, "price_profiles");
}

/* site funcs */


function changePriceDiscount(price,cart_price,sofia,discount,city_id)
{	
	if(cart_price >= price)
	{
		$("#someid").html("Безплатна доставка за сума над 100.00 лв.");
	}else{
		var all = 0;
		if(city_id = 6)
		{
			all = cart_price + sofia;
			all = all.toFixed(2);
			$("#someid").html("Цена " + cart_price + "<br />Цена за доставка "+ sofia + "<br />Обща цена " + all);
		}else{
			all = cart_price + bulgaria;
			all = all.toFixed(2);
			$("#someid").html("Цена " + cart_price + "<br />Цена за доставка "+ sofia + "<br />Обща цена " + all);
		}
	}
}

function calculateCart() {
	//obhojdane na vsi4ki selecti v tablicata i iz4islqvane na ob6tata cenata
	var total_price = 0;
	$("#cartProducts div input:even").each(function(i) {;
		total_price = parseFloat(total_price) + parseFloat($(this).val());
	});

	total_price = total_price.toFixed(2);
	$("#total").html(total_price);
	$("#cartPrice").fadeTo("slow", 0.00);
        $("#cartPrice").fadeTo("slow", 1.00);

}

function calculateBigCart()
{
	//obhojdane na vsi4ki selecti v tablicata i iz4islqvane na ob6tata cenata
	var big_total_price = 0;
	var big_all_total = 0;
	var big_dds = 0;
	$("#cart_ttl input:odd").each(function(i) {;
		big_total_price = parseFloat(big_total_price) + parseFloat($(this).val());
	});

	big_total_price = big_total_price.toFixed(2);
	big_dds = big_total_price*0.2;
	big_all_total = big_total_price*1.2;
	big_dds = big_dds.toFixed(2);
	big_all_total = big_all_total.toFixed(2);
	if(big_all_total == 0)
	{
		window.location = "http://handy-eshop.com/";
	}
	$("#big_total").html(big_total_price);
	$("#cart_price").val(big_total_price);
	//$("#big_dds").html(big_dds);
	//$("#big_all_total").html(big_all_total);
}

function show_fakt(){ $("#faktura_info").css("display","block"); }
function hide_fakt(){ $("#faktura_info").css("display","none"); }

function calculatePrice(quantity, p_id)
{
	if(quantity < 1)
	{
		//$("#qtt_" + p_id).val(1);
		return removeBigCartProduct('product_cart_'+p_id, p_id);
	}
	var new_total = 0;
	var price = 0;
	price = $("#price_one_" + p_id).html();
	price = parseFloat(price);
	new_total = quantity*price;
	new_total = new_total.toFixed(2);
	$("#price_li_" + p_id).html(new_total);
	$("#price_" + p_id).val(new_total);

	var big_total_price = 0;
	var big_all_total = 0;
	var big_dds = 0;
	$("#cart_ttl input:odd").each(function(i) {;
		big_total_price = parseFloat(big_total_price) + parseFloat($(this).val());
	});

	Ajax("ajax_php/handle_cart.php", "btn=addProduct&product_id="+p_id+"&quantity=" + quantity, "");

	big_total_price = big_total_price.toFixed(2);
	big_dds = big_total_price*0.2;
	big_all_total = big_total_price*1.2;
	big_dds = big_dds.toFixed(2);
	big_all_total = big_all_total.toFixed(2);
	$("#big_total").html(big_total_price);
	$("#big_dds").html(big_dds);
	$("#big_all_total").html(big_all_total);
	/*Ajax("ajax_php/calculate_discount.php","big_total_price=" + big_total_price + "&big_dds=" + big_dds + "&big_all_total=" + big_all_total,"cart_total_price");*/
}

function isCartEmpty() {
	var cnt = 0;
	$('#cartProducts div ').each(function(i) {;
	cnt++
	});

	/*ako e prazna, za4istvame si4ko s title-a*/
	if (cnt == 0) {
	$('#cart_has_products').val("0");
	$('#cartProducts').html("<br /><span style='margin-left:20px;'>Korpa je prazna</span> <br />");
	$('#cartPrice').html("");
	$('#cartBuy').css("display", "none");
	}
}

function removeCartProduct(id, product_id) {
	$('#' + id).slideToggle("normal");
	setTimeout("$('#" +id +"').remove()", 700);
	setTimeout("calculateCart()", 900);
	Ajax("ajax_php/handle_cart.php", "btn=removeProduct&product_id="+product_id, "");
	setTimeout("isCartEmpty()", 1200);
	return false;
}

function removeBigCartProduct(id, product_id) {
	$('#' + id).slideToggle("normal");
	setTimeout("$('#" +id +"').remove()", 500);
	setTimeout("calculateBigCart()", 600);
	Ajax("ajax_php/big_cart.php", "btn=removeProduct&product_id="+product_id, "");
	setTimeout("isCartEmpty()", 700);
}

function addCartProduct(p_id, p_name, p_price) {

	/*var rem_html = "removeCartProduct('product_cart_" + p_id +"','"+ p_id+"'); return false;";*/
	/*alert(rem_html);*/
 var new_product_html= "<div style=' padding-left: 5px;overflow: visible; opacity: 0.9999; display: block;' class='productCart' id='product_cart_" + p_id + "'><span style='width: 200px;'><a href='show_product.php?id=" + p_id +"' style='line-height: 7.6pt;'>" + p_name + "</a></span><br><h3 style='height: 20px; margin-top:6px;padding-bottom:5px;'><i><a href='#'  onclick=removeCartProduct('" + 'product_cart_' + p_id +"'," + p_id + "); id='product_cart_remove_" + p_id +"' >obriši</a></i></h3> <h2><span class='text'  style='margin-top:5px;'><i>broj:&nbsp;</i></span><span id='quantity_span_" + p_id + "''>1</span><br /> <span class='text'><i>Cena:&nbsp;</i></span><span id='price_span_"+p_id+"'>" + p_price + "</span> din.</h2> <input type='hidden' name='price_"+p_id+"' id='price_"+p_id+"' value='"+p_price+ "' /> <input type='hidden' name='price_single_"+p_id+"' id='price_single_"+p_id+"' value='"+p_price+ "' /> <img src='i/right_line.gif' /> </div>";
 var total_price="<h4 id='total_price_p'><span style='font-size: 10px;'>Ukupno:</span><span id='total'></span><span style='font-size: 10px;'>din.</span></h4>";
 var cart_has_products = $('#cart_has_products').val();

 if (cart_has_products == '0') {
	 $("#cartProducts").html(new_product_html);
	 $('#cart_has_products').val("1");
	 $("#cartPrice").append(total_price);
	 $('#cartBuy').css("display", "block");
 } else {
	 if ( $("#product_cart_" + p_id).html()) {
		 var quantity = 0;
		 var price_single = 0.00;
		 var price = 0;
		
		 quantity = parseInt($("#quantity_span_" + p_id).html());
		 price_single = parseFloat($("#price_single_" + p_id).val());

		 quantity = parseInt(quantity) + parseInt(1);
		 price = quantity * price_single;
		 price = price.toFixed(2);
		 $("#price_span_" + p_id).html(price);
		 $("#price_" + p_id).val(price);
		 $("#quantity_span_" + p_id).html(quantity);
		 $("#product_cart_" + p_id).fadeTo("slow", 0.00);
		 $("#product_cart_" + p_id).fadeTo("slow", 1.00);

	 } else {
		 $("#cartProducts").append(new_product_html);
		 $('#cart_has_products').val("1");
	 }
 }
/* $("#product_cart_remove_" + p_id).attr("onclick", " removeCartProduct('product_cart_" + p_id +"','"+ p_id+"');");*/
 $("#product_cart_" + p_id).slideDown();
 Ajax("ajax_php/handle_cart.php", "btn=addProduct&product_id="+p_id, "");

 calculateCart();
}


function additionalAction(form_id) {
	var original_action = $("#"+form_id).attr("action");
	var add_action = $("#additional_action").val();
	$("#"+form_id).attr("action", original_action + add_action);
}
