var keepAliveSeconds = 600;

function addSpaces(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	return x1 + x2;
}

function keepAlive() {
    $.post("ajax/keep_alive.php", {},
        function (data) {
	        if (data.status){
	        // Uncomment for debugging:
	        // popupWindow("OK");
	        }
        },
        "json");
    setTimeout("keepAlive()", keepAliveSeconds * 1000);
}

$(document).ready(function() {
    $('a[rel ^= "to_basket_"]').click(function (e){
        e.preventDefault();
        e.stopPropagation();
        var amount=$('input[name = "'+$(this).attr('rel')+'"]').val();
        var id=$(this).attr('rel').slice(10);
        
        $.post( "ajax/add_to_cart.php", {
                                    'product_id': id,
                                    'count': amount
                                 },
                function (data) {
                    $('#cart_total').text(data.cart_total);
                    $('#cart_number').text(addSpaces(data.cart_number) + ' Ft');
	                if(data.status){
	                    popupWindow(dict["intoCart"]);
	                } else{
	                    popupWindow(dict["intoCartError"]);
	                }
                },
                "json");
        }
    );

    $('a[rel ^= "save_cart"]').click(function (e){
        e.preventDefault();
        e.stopPropagation();
        
        $.post( "ajax/save_cart.php", {},
                function (data) {
					if(data.status){
	                    popupWindow(dict["cartSaved"]);
	                } else{
	                    popupWindow(dict["cartSaveError"]);
	                }
                },
                "json");
        }
    );

    setTimeout("keepAlive()", keepAliveSeconds * 1000);
});

var timer = 0;

var scrolledX, scrolledY;
var centerX, centerY;
function popupWindow(msg)
{
	var Xwidth = 300;
	var Yheight = 100;
	if( self.pageYOffset ) {
		scrolledX = self.pageXOffset;
		scrolledY = self.pageYOffset;
	} else if( document.documentElement && document.documentElement.scrollTop ) {
		scrolledX = document.documentElement.scrollLeft;
		scrolledY = document.documentElement.scrollTop;
	} else if( document.body ) {
		scrolledX = document.body.scrollLeft;
		scrolledY = document.body.scrollTop;
	}
	
	if( self.innerHeight ) {
		centerX = self.innerWidth;
		centerY = self.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) {
		centerX = document.documentElement.clientWidth;
		centerY = document.documentElement.clientHeight;
	} else if( document.body ) {
		centerX = document.body.clientWidth;
		centerY = document.body.clientHeight;
	}
	
	var leftOffset = scrolledX + (centerX - Xwidth) / 2;
	var topOffset = scrolledY + (centerY - Yheight) / 2;
	// The initial width and height of the div can be set in the
	// style sheet with display:none; divid is passed as an argument to // the function
	var divToOpen = $("<div/>").addClass("cartaction").css("width", Xwidth).css("height", Yheight).css("position", "absolute").css("top", topOffset).css("left", leftOffset).css("backgroundColor", "#000").css("lineHeight", "100px").css("textAlign", "center").css("opacity", 0.8).css("color", "#fff").css("fontWeight", "bold").html(msg);
	$("body").append(divToOpen);
	/*
	var o=document.getElementById(divid);
	var r=o.style;
	r.position='absolute';
	r.top = topOffset + 'px';
	r.left = leftOffset + 'px';
	r.display = "block";*/
	setTimeout("removeBox()", 2000);
	 
}

function removeBox()
{
	$(".cartaction").remove();
}


function add_to_cart()
{

/*
     $.post("ajax/add_to_cart.php", { 'product_id' : $("#product_id").val(), 'count' : $("#to_cart_count").val() }, function(data){

                    if(data.status == 1)
                    {
                         popupWindow(data.message);
                         $(".cart_text").html(data.count+"<br />"+data.count_distinct);
                         $(".cart_price").text(data.sum);
                    }
                    else
                    {
                         alert("nemok");
                    }
              }, "json");*/
}

$('a[rel ^= "to_basket_"]').click(function (e){
            e.preventDefault();
            e.stopPropagation();
            var amount=$('input[name = "'+$(this).attr('rel')+'"]').val();
            var id=$(this).attr('rel').slice(10);
            $.post( "/kosar/add", {
                                        'id': id,
                                        'amount': amount
                                     },
                    function (data) {
                        $('#cart_total').text(data.cart_total);
                        $('#cart_number').text(data.cart_number);
                        if(data.result){
                            popupWindow(dict["intoCart"]);
                        } else{
                            popupWindow(dict["intoCartError"]);
                        }
                    },
                    "json");
    }
);

function shippingAdressCopy() 
{
		$('#szl_nev').val($('#szm_nev').val());
		$('#szl_iranyitoszam').val($('#szm_iranyitoszam').val());
		$('#szl_telepules').val($('#szm_telepules').val());
		$('#szl_cim').val($('#szm_cim').val());
}

function limitTextarea(limitField, limitNum) {
    $('#ct').html(limitField.value.length+"/250");
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
        
        
        //alert('500 karakternél többet nem írhat a szövegmezőbe!');
        //popupWindow('500 karakternél többet nem írhat a szövegmezőbe!');
    } 
    
}

