var selectedSize = -1;
//
// Select size. Mark value on screen and store it in internal Value.
//
function selectSize(sizeId){
	selectedSize = sizeId;
	$("#sizes-avail a").each(function(){
		$(this).removeClass("selected");
	});	
	
	$("#size_"+sizeId).addClass("selected");
	
	$("span.size-info").html("Wybrano rozmiar: "+$("#size_"+sizeId).text());
	
	$.post("/x/getSize?id="+sizeId,function(json){
		var dane = eval("(" + json + ")");
		var buffer="<option value=\"1\">1</option>";
		for(var i=2;i<=dane.amount;i++){
			buffer+="<option value=\""+i+"\">"+i+"</option>";
		}
		$("#quant").html(buffer);
	});
}
//
// Show contact box
//
function showContactInfo(sizeId){	
	$("#productandsize").val($("#prname").text() + " "+$("#z_"+sizeId).text());	
	$("#productsize").val(sizeId);
	$("#dialog").dialog({ width: 460 , buttons: 
		{ "Zamknij": function() {
						$("#dialog").dialog('close');
					},
		  "Wyślij zapytanie": function(){
		     if(validateRequestForm()){
		  		sendRequestForm();		  		
		  		$('#cq_error').text("");
		  	 }else{
		  	 	$('#cq_error').text("Proszę wypełnić wszystkie pola!");
		  	 }		  	 
		  }
	}})
	$("#dialog").dialog('open');
}
//
// Validaes entry from request form
//
function validateRequestForm(){
	var result = true;
	if(isEmpty($('#clientname'))){
		$('#cq_name').addClass('error');
		result = false;
	}else{
		$('#cq_name').removeClass('error');
	}
	if(isEmpty($('#productandsize'))){		
		$('#cq_product').addClass('error');
		result = false;
	}else{
		$('#cq_product').removeClass('error');
	}		
	if(isEmpty($('#clientrequest'))){
	 	$('#cq_text').addClass('error');
		result = false;
	}else{
		$('#cq_text').removeClass('error');
	}
	if(isEmpty($('#clientcontact'))){
	 	$('#cq_contact').addClass('error');
		result = false;
	}else{
		$('#cq_contact').removeClass('error');
	}
	
	return result;
}

function isEmpty(obj){
	var x = obj.val().replace(/^\s+|\s+$/g, '');
	if(x.length<=0)
		return true;
	else
		return false;
}
//
// Sends client requset QUery to server
//
function sendRequestForm(){
	$.post( "/x/sendQuery", $("#requestform").serialize(), 
  				function(data){ 
  					$("#dialog").dialog('close');
  					var d = eval('(' +data + ')' );
  					if(d.code=='OK'){ 
  						showConfirmDialog();
  					}else{
  						showErrorDialog();
  					}
  				} 
  			);
}
//
// Add to cart
//
function addToCart(itemId){
	if(selectedSize<0){
		showAlert();
	}else{
		$.post(	"/x/addToCart",
				{ size: selectedSize,
				  item: itemId,
				  amount:$("#quant").val()},
				function(json){
					var dane = eval("(" + json + ")");
					if(dane.code==0){
						updateCartInfo(dane.count);
						showCartConfirm();
					}
				});
	}
}

function updateCartInfo(c){
    var buf;
	if(c>=2 && (c%10==2 || c%10==3 || c%10==4) ){
  		buf=c+ " PRODUKTY";
	}else if(c>=5){
  			buf=c+ " PRODUKTÓW";  
	}else if(c==1){
  		buf=c+ " PRODUKT";
  	}else{
  		buf="PUSTY";
	}
	$("#cartcount").text(buf);
}
//
// Show confirm dialog, after adding to cart
//
function showCartConfirm(){
	$("#dialog-cart").dialog({ width: 460, buttons: { 
			"Kontynuuj zakupy": function() {
				$("#dialog-cart").dialog('close');
			},
			"Realizacja zamówienia":function(){
				location.href="/koszyk";
			}
	}});
	$("#dialog-cart").dialog('open');
}

//
// Add to temporary store for client.
//
function addToMyStore(itemId){

}
//
// Show confirm dialog box for sending user query request.
//
function showConfirmDialog(){
	$("#dialog-confirm").dialog({ buttons: { "Zamknij": function() {
		$("#dialog-confirm").dialog('close');
	}}});
	$("#dialog-confirm").dialog('open');
}

function showErrorDialog(){
	$("#dialog-error").dialog({ buttons: { "Zamknij": function() {
		$("#dialog-error").dialog('close');
	}}});
	$("#dialog-error").dialog('open');
}
//
// Show Alert If Size is Not Choosen.
//
function showAlert(){
	$("span.size-info").html("Proszę wybrać rozmiar!");	
}