﻿
$(document).ready(function(){
	//global vars
	
	var inputBalsas = $("#balsas");
	var loading = $("#loading");
	var messageList = $("#rezultatai");
	
	//functions
	function updateShoutbox(){
		//just for the fade effect
		messageList.hide();

		
		$.ajax({
			type: "POST", url: "index.php/apklausa/rezultatai", data: "action=show",
			complete: function(data){
				loading.hide();
				messageList.html(data.responseText);
				messageList.show();
			}
		});
	}
	
	//functions
	function showShoutbox(){
		//just for the fade effect
		messageList.hide();

		
		$.ajax({
			type: "POST", url: "index.php/apklausa/rezultatai", data: "action=show",
			complete: function(data){
				loading.hide();
				messageList.html(data.responseText);
				messageList.show();
			}
		});
	}
	
	//check if all fields are filled
	function checkForm(){
		if(inputBalsas.attr("value"))
			return true;
		else
			return false;
	}
		
	loading.hide();
	
	//on submit event
	$("#form").submit(function(){
		if(checkForm()){
			
			var balsas = $("input[@name='balsas']:checked").val();
			
			//we deactivate submit button while sending
			$("#balsuoti").attr({ disabled:true, value:"Siunčiama..." });
			$("#balsuoti").blur();
			
			//send the post to shoutbox.php
			$.ajax({
				type: "POST", url: "index.php/apklausa/balsuoti", data: "action=insert&balsas=" + balsas,
				complete: function(data){
					messageList.html(data.responseText);
					updateShoutbox();
					

					//reactivate the send button
					$("#balsuoti").attr({ disabled:false, value:"Balsuoti" });
				}
			 });
			
		}
		else alert("Prašome pasirinkti atsakymą!");
		//we prevent the refresh of the page after submitting the form
		return false;
	});
	
	
	$("#rodytirez").click(function(){

		showShoutbox();
		return false;
	});
});
