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

}

function is_int( mixed_var ) {
    var y = parseInt(mixed_var * 1);
    if (isNaN(y)) {
        return false;
    }
    return mixed_var == y && mixed_var.toString() == y.toString();
}
function is_numeric( mixed_var ) {
    return !isNaN( mixed_var );
}

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*/

function calculate_res()
{
	var res = 0;
	res = (screen.width - 450).toFixed();
	res = Math.floor(res / 180);
	/*alert(res);*/
	return 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";
	 Ajax("ajax_php/price_profiles_handle.php", data, "price_profiles");
}

/* site funcs */

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
	//alert('ffff');
	var big_total_price = 0;
	var big_all_total = 0;
	var big_dds = 0;
	var big_total_dds = 0;
	$(".cart_total").each(function(i) {
		big_total_price = parseFloat(big_total_price) + parseFloat($(this).html());
	});
 
	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_dds = parseFloat(big_dds);
	big_total_dds = big_total_dds.toFixed(2);
	if(big_all_total == 0)
	{
		window.location = "http://digiteq.com/";
	}
	$("#big_total").html(big_total_price);
	$("#cart_price").val(big_total_price);
	$("#big_total_dds").html(big_total_dds);
	$("#big_total_plus_dds").html(big_all_total);
	/*$("#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(is_numeric(quantity) == false) { $("#qtt_" + p_id).val(1); quantity = 1; }
		if(quantity < 1) { $("#qtt_" + p_id).val(1); quantity = 1; }
		if(is_int(quantity) == false) { $("#qtt_" + p_id).val(1); quantity = 1; }
		
		
		var new_total = 0;
		var price = 0;
		price = $("#price_" + p_id).val();
		/*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_total").each(function(i) {;
				/*alert($(this).html());*/
			big_total_price = parseFloat(big_total_price) + parseFloat($(this).html());
		});
	
		Ajax("ajax_php/handle_cart.php", "btn=addProduct&product_id="+p_id+"&quantity=" + quantity, "");
	
			/*alert(big_total_price);*/
		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);
		$("#cart_price").val(big_total_price);
		calculateBigCart();
		/*$("#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) {
		/*window.location = "http://ts.planbg.net/";*/
	$('#cart_has_products').val("0");
	$('#cartProducts').html("<span class='empty'>Кошницата е празна</span>");
	$('#cart').html("<input name='cart_has_products' id='cart_has_products' value='0' type='hidden'> <form action='cart_advanced.php' method='post' id='login_form'> <div class='div_head'><span style='float: left;'>Кошница</span><img src='i/arrow_right.gif' border='0'></div> <div id='cartProducts'> <br><span style='line-height:43px; margin-left:30px; margin-top:-15px; font-weight: bold;'> Кошницата е празна !</span>  </div> <div style='float:left; width:185px;' id='cartPrice'>  </div> </form>");
	$('#cartPrice').html("");
	$('#cartBusy').css("display", "none");
	/*$('#cartBusy2').css("display", "block");*/
	}
}

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

function removeBigCartProduct(id, product_id) {
	$('#' + id).fadeOut("slow");
	setTimeout("$('#" +id +"').remove()", 600);
	setTimeout("calculateBigCart()", 700);
	Ajax("ajax_php/handle_cart.php", "btn=removeProduct&product_id="+product_id, "");
	setTimeout("isCartEmpty()", 800);
}

String.prototype.wordWrap = function(m, b, c){
    var i, j, s, r = this.split("\n");
    if(m > 0) for(i in r){
        for(s = r[i], r[i] = ""; s.length > m;
            j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length
            || m,
            r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")
        );
        r[i] += s;
    }
    return r.join("\n");
};



function addCartProduct(p_id, p_name, p_price) {
 p_name = p_name.wordWrap(22, "\n ", true);
 var new_product_html = "<div style='overflow: visible; opacity: 0.9999; display: none;' class='cartItem' id='product_cart_" + p_id + "'><h1>" + p_name + "</h1><p style='height: 10px;'>&nbsp;</p><p><font>брой:</font> <span id='quantity_span_" + p_id + "' class='black'>1</span> <br /><font>цена: </font> <span class='black' id='price_span_"+p_id+"'>" + p_price + "</span> <span class='black'>лв.</span> <a href='#' onclick=removeCartProduct('" + 'product_cart_' + p_id +"'," + p_id + "); id='product_cart_remove_" + p_id + "'>изтрий</a><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+ "'><hr /></div>";
 var total_price = "<span id='total_price_p' class='cartTotal'><strong>Общо:&nbsp;<span id='total'></span>&nbsp;лв.</strong> <a class='add png red' href='cart.php'><p style='margin:0;padding:0'>поръчай</p></a></span>";
 var cart_has_products = $('#cart_has_products').val();
 
 
 if (cart_has_products == '0') {
	 $("#cartProducts").html(new_product_html);
	 $('#cart_has_products').val("1");
	 $("#cartPrice").html(total_price);
	 $('#cartBusy').css("display", "inline");
	 /*setTimeout(function(){ alert($("#cartBusy").attr('style')); },1000);*/
	 /*alert('aaaa');*/
	 /*$("#cart_buy_test").append("<a href='cart.php'><img src='../i/buy.gif' border='0'></a>");*/
 } 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+"'); return false;");*/
 $("#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);
}
