$(document).ready(function() {	
	$('#nrFemije').change(function () {
		//Get the data from all the fieldsnrAdult
		var nrFemije = $('select[name=nrFemije]');
		var nrAdult = $('select[name=nrAdult]');
		var gjuha = $('input[name=gjuha]');
		var gj=gjuha.val();
		var numriF=nrFemije.val();
		var numriA=nrAdult.val();
		
			//start the ajax
			$.ajax({
				//this is the php file that processes the data and send mail
				url: "/admin/currentYear.php",	
				
				//GET method is used
				type: "GET",

				//pass the data			
				data: 'gj='+gj+'&nrFemije='+numriF+'&nrAdult='+numriA,		
				
				//Do not cache the page
				cache: false,
				
				//success
				success: function (data) {				
					//if process.php returned 1/true (send mail success)
					if (data!='') {	
						$('#result').html(data);
						$('#totali').html('');
						$('#plotesoForem').html('');
					//if process.php returned 0/false (send mail failed)
					} else {if(gj=="gj1"){alert('Ndodhi nje gabim. Ju lutemi te provoni perseri samir');}else{alert('Sorry, unexpected error. Please try again later.');}}				
				}		
			});
		return false;
	});
	$('#kalkulo').click(function () {		
		
		//Get the data from all the fields
		var nrAdult=document.getElementById('nrAdult').value;
		var nrFemije = document.getElementById('nrFemije').value;
		var tourID = $('input[name=tourID]');
		var gjuha = $('input[name=gjuha]');
		var gj=gjuha.val();
		var data='tID='+tourID.val()+'&gj='+gj+'&nrAdult='+nrAdult+'&nrFemije='+nrFemije;
		
		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		
		if(nrFemije>0)
		{
			//organize the data properly
			for(j=1;j<=nrFemije;j++)
			{
				var ditaPost=$('select[name=dita'+j+']');
				var muajiPost=$('select[name=muaji'+j+']');
				var vitiPost=$('select[name=viti'+j+']');
				var dita=ditaPost.val();
				var muaji=muajiPost.val();
				var viti=vitiPost.val();
				if (dita==0) {
					ditaPost.addClass('hightlight');
					return false;
				} else ditaPost.removeClass('hightlight');
				if (muaji==0) {
					muajiPost.addClass('hightlight');
					return false;
				} else muajiPost.removeClass('hightlight');
				if (viti==1995) {
					vitiPost.addClass('hightlight');
					return false;
				} else vitiPost.removeClass('hightlight');
				
				data+='&data'+j+'='+viti+'-'+muaji+'-'+dita;
			}
		}
		
		//disabled all the text fields
		//$('.text').attr('disabled','true');
		
		//show the loading sign
		$('.loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "/admin/process.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (data) {				
				//if process.php returned 1/true (send mail success)
				if (data!='') {	
					$('#totali').html(data).fadeIn('slow');				
					$('#resultati').html('').fadeOut('slow');
					$('#result').html('').fadeOut('slow');
					//hide the form
					
					
				//if process.php returned 0/false (send mail failed)
				} else {if(gj=="gj1"){alert('Ndodhi nje gabim. Ju lutemi te provoni perseri');}else{alert('Sorry, unexpected error. Please try again later.');}}				
			}		
		});
		//cancel the submit button default behaviours
		return false;
	});
	return false;
});