$(document).ready(function() {
	
	//if submit button is clicked
	$('#submit_booking').click(function () {		
		
		//Get the data from all the fields
		
		var apartman = $('select[name=apartman]');
		var dolazak = $('input[name=dolazak]');
		var odlazak = $('input[name=odlazak]');
		var odrasli = $('input[name=odrasli]');
		var djeca = $('input[name=djeca]');
		var ime = $('input[name=ime]');
		var telefon = $('input[name=telefon]');
		var adresa = $('input[name=adresa]');
		var email = $('input[name=email]');
		var napomena = $('textarea[name=napomena]');

		//Simple validation to make sure user entered something
		//If error found, add highlight class to the text field
	
		if (dolazak.val()=='') {
			dolazak.addClass('highlight');
			return false;
		} else dolazak.removeClass('highlight');
		
		if (odlazak.val()=='') {
			odlazak.addClass('highlight');
			return false;
		} else odlazak.removeClass('highlight');

		if (odrasli.val()=='') {
			odrasli.addClass('highlight');
			return false;
		} else odrasli.removeClass('highlight');

		if (djeca.val()=='') {
			djeca.addClass('highlight');
			return false;
		} else djeca.removeClass('highlight');
				
		if (ime.val()=='') {
			ime.addClass('highlight');
			return false;
		} else ime.removeClass('highlight');
		
		if (email.val()=='') {
			email.addClass('highlight');
			return false;
		} else email.removeClass('highlight');
		


		//organize the data properly
		var data = 
		'apartman='  + encodeURIComponent(apartman.val()) +
		'&dolazak='  + encodeURIComponent(dolazak.val()) +
		'&odlazak=' + encodeURIComponent(odlazak.val()) + 
		'&odrasli=' + encodeURIComponent(odrasli.val()) +
		'&djeca=' + encodeURIComponent(djeca.val()) +
		'&ime=' + encodeURIComponent(ime.val()) + 
		'&telefon=' + encodeURIComponent(telefon.val()) +
		'&adresa=' + encodeURIComponent(adresa.val()) +
		'&email=' + email.val()+
		'&napomena=' + encodeURIComponent(napomena.val());


//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: "process_smjestaj.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {				
				//if process.php returned 1/true (send mail success)
				if (html==1) {					
					//hide the form
					$('.form').fadeOut('fast');		
					//show the success message
					$('.done').fadeIn('fast');
					
				//if process.php returned 0/false (send mail failed)
				} else email.addClass('highlight');
					$('.error_message').fadeIn('fast');
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
});	
