// JavaScript Document
$(document).ready(function() {
	
	//if submit button is clicked
	$('#submit').click(function () {		
		
		//Get the data from all the fields
		var name = $('input[name=name]');   
        var address = $('input[name=address]');   
        var state = $('input[name=state]');   
        var country = $('input[name=country]'); 
		 var zip = $('input[name=zip]');   
        var phone = $('input[name=phone]');   
        var email = $('input[name=email]');   
        var adult = $('input[name=adult]'); 
		 var children = $('input[name=children]');   
        var edate = $('input[name=edate]');   
        var package = $('input[name=package]');   
        var requirement = $('textarea[name=requirement]');   

		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (name.val()=='') {   
           name.addClass('hightlight');   
            return false;   
        } else name.removeClass('hightlight'); 
		
		
		if (country.val()=='') {   
           country.addClass('hightlight');   
            return false;   
        } else country.removeClass('hightlight'); 
		
		
		if (phone.val()=='') {   
           phone.addClass('hightlight');   
            return false;   
        } else phone.removeClass('hightlight'); 
		
		if (email.val()=='') {   
           email.addClass('hightlight');   
            return false;   
        } else email.removeClass('hightlight');   
		
		 if (adult.val()=='') {   
           adult.addClass('hightlight');   
            return false;   
        } else adult.removeClass('hightlight');   
           
             if (children.val()=='') {   
           children.addClass('hightlight');   
            return false;   
        } else children.removeClass('hightlight');   
           
        
		 if (edate.val()=='') {   
           edate.addClass('hightlight');   
            return false;   
        } else edate.removeClass('hightlight');   
           
        
        //organize the data properly   
		        var data = 'name=' + name.val() + '&address=' + address.val() + '&state='  
        + state.val() + '&country=' + country.val() + '&zip=' + zip.val() +  '&phone=' + phone.val() + '&email=' + email.val() +  '&adult=' + adult.val() + '&children=' + children.val() +  '&edate='+ edate.val() + '&package=' + package.val() + '&requirement=' + encodeURIComponent(requirement.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: "<?=WB_URL ?>/templates/eco/process.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('slow');					
					
					//show the success message
					$('.done').fadeIn('slow');
					
				//if process.php returned 0/false (send mail failed)
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
});	
