function addToCart(id)
{	
	var cartqHtml = '<input type="text" name="q" value="1" size="3" /> <a href="javascript:void(0)" onclick="addToCartQ('+id+')">dodaj</a>';

	$('#p'+id).html(cartqHtml).show();
}

function addToCartQ(id)
{
	var q = parseInt($.trim($('#p'+id+' input').val()));
	$('#p'+id).html('ładowanie...');
	$.getJSON(path+"ajax_dokoszyka,"+id+","+q+".html", function(json){if(json.ret){updateTopCart();}});
	$('#p'+id).hide();
}

function addToStore(id)
{
	$('#p'+id).html('ładowanie...').show();
	$.getJSON(path+"ajax_doprzechowalni,"+id+",1.html", function(json){if(json.ret){updateTopStore();}});
	$('#p'+id).hide();
}

function updateTopCart()
{
	$("#topcartcart").load(path+"ajax_wkoszyku.html");
}

function updateTopStore()
{
	$("#topcartstore").load(path+"ajax_wprzechowalni.html").show();
}

function setSumPriceAll(prodprice,shipprice,giftvalue,packageType,id) {
	var sumpriceall = parseFloat(prodprice)+parseFloat(shipprice)-parseFloat(giftvalue);
	if(parseFloat(prodprice)+parseFloat(shipprice) < parseFloat(giftvalue)) {sumpriceall = 0;$(".giftcodeover").css('display','block');} 
	else{$(".giftcodeover").css('display','none');}
	$("#sumpriceall").html(number_format(sumpriceall,2,',',' '));
	if(packageType==8000) {
		$('.userdataship').css('display','none');
		$('#payment8000').css('display','block');
		$('.payment').show();
		$('#payment1000 input').attr('checked',"checked");
	} else if(packageType==8) {	
		$('[name=paymentMethod]').removeAttr("checked");
		$('#payment7000 input').attr('checked',"checked");
		$('.payment').hide();
	} else {
		$('.userdataship').css('display','block');
		$('#payment8000').css('display','none');
		$('.payment').show();
		$('#payment1000 input').attr('checked',"checked");
	}
	$("#packageinput").val(id);
}

function setProdPhoto(id)
{
	$(".photo a").hide();
	$("#"+id).show();
}

function reverseSelection(formId)
{
	$("#"+formId+" input:checkbox").each(function(i,item){
		//if($(item).removeClass("error");
		if($(item).attr('checked'))
		{
			$(item).attr('checked',false);
		}
		else
		{
			$(item).attr('checked',true);
		}
	});		
}

$(document).ready(function(){hideCom();});

function hideCom()
{
	if($("#com").hasClass("comna")) {return;}
	
	$("#com").animate({opacity: 1.0}, 3000).fadeOut(1000);
}

function showFeaturesUnder(idp,idf)
{
	$("#featuresunder").html('ładowanie...').load(path+"ajax_featuresunder,"+idp+","+idf+".html");
}

function link(url)
{
   document.location = url;   
   return false;      
}

function showCom(el,type,msg)
{
	if(type==1){$(el).css('background-color','#336600');}
	else{$(el).css('background-color','#FF0000');}
	$(el).html(msg);
	$(el).show();
	$(el).animate({opacity: 1.0}, 3000).fadeOut(1000);
}

function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival
    // %        note 1: For 1000.55 result with precision 1 in FF/Opera is 1,000.5, but in IE is 1,000.6
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'

    var n = number, prec = decimals;
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep == "undefined") ? ',' : thousands_sep;
    var dec = (typeof dec_point == "undefined") ? '.' : dec_point;

    var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = Math.abs(n).toFixed(prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');

        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    return s;
}

function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    }
}

function sendInfo()
{
	$.post("ajax_sendinfo.html", { idorders: $("input[name='idorders']").val(), info: $("textarea[name='info']").val() },
   function(data){
    if(data.ret==1) {$("#orderinfo").html('dziękujemy za wysłanie uwagi!');} else {$("#orderinfo").html('wystąpił błąd podczas wysyłania uwagi!');}
  }, "json");

}

function showChilds(id,lvl,link)
{
	if($("#child"+id).css('display') == 'none')
	{
		$(".childs"+lvl).hide();
		$("#child"+id).show();
		return false;
	}
	if(link!=undefined)
	{
		window.location = link;
	}
}

function newProdSGProd(id)
{
	$("#newprodsg2 .listnew .left .product").attr("class","producthide");
	$("#"+id).attr("class","product");
}

function newProdSGDost(id)
{
	$("#newprodsg2 .listdel .left .product").attr("class","producthide");
	$("#"+id).attr("class","product");
}

function newProdSGSwitch(p1,p2)
{
	$("#newprodsg2 ."+p1).hide();
	$("#newprodsg2 ."+p2).show();
}

function markReaded(id)
{
	$.get("ajax_markreaded,"+id+".html");
	$("#notify"+id).attr('class','notify');
	unread--;
	if(!unread) {$('#notifications').attr('class','notifyoff');}
}

function showhideArticles()
{
	if($(".articles .more span").html() == 'rozwiń') {
		$(".articles .hidden").show();
		$(".articles .more span").html('zwiń');
	} else { 
		$(".articles .hidden").hide();
		$(".articles .more span").html('rozwiń');
	}		
}

function dropDownShow(id)
{
	if($('#'+id).css('display')=='none') {
		$('#'+id).show();
		$('#'+id).mouseleave(function() {$('#'+id).hide();});
	} else {
		$('#'+id).hide();
	}
	return false;
}

/**
 * YOU ARE FREE TO USE THIS CODE IF YOU HOLD THE REFERENCE TO THE AUTHOR
 * Plugin for jQuery that delimites the maximum of characteres in inputs and textareas
 * @author: Iván Guardado Castro
 * @email: dev.ivangc@gmail.com
 * @website: http://yensdesign.com/
 */
jQuery.fn.maxLength = function(max){
	this.each(function(){
		//Get the type of the matched element
		var type = this.tagName.toLowerCase();
		//If the type property exists, save it in lower case
		var inputType = this.type? this.type.toLowerCase() : null;
		//Check if is a input type=text OR type=password
		if(type == "input" && inputType == "text" || inputType == "password"){
			//Apply the standard maxLength
			this.maxLength = max;
		}
		//Check if the element is a textarea
		else if(type == "textarea"){
			//Add the key press event
			this.onkeypress = function(e){
				//Get the event object (for IE)
				var ob = e || event;
				//Get the code of key pressed
				var keyCode = ob.keyCode;
				//Check if it has a selected text
				var hasSelection = document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;
				//return false if can't write more
				return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);
			};
			//Add the key up event
			this.onkeyup = function(){
				//If the keypress fail and allow write more text that required, this event will remove it 
				if(this.value.length > max){
					this.value = this.value.substring(0,max);
				}
			};
		}
	});
};
