$(document).ready(function() {
	
	//if submit button is clicked
	$('#submit2').click(function () {		
		
		//Get the data from all the fields
		var name2 = $('input[name=name2]');
		var last2 = $('input[name=last2]');
		var company2 = $('input[name=company2]');
		var phone2 = $('input[name=phone2]');
		var email2 = $('input[name=email2]');
		var ae2 = $('textarea[name=ae2]');
		
		

		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (name2.val()=='') {
			name2.addClass('hightlight');
			return false;
		} else name2.removeClass('hightlight');
		
		if (last2.val()=='') {
			last2.addClass('hightlight');
			return false;
		} else last2.removeClass('hightlight');
		
		if (phone2.val()=='') {
			phone2.addClass('hightlight');
			return false;
		} else phone2.removeClass('hightlight');
		
		
		if (email2.val()=='') {
			email2.addClass('hightlight');
			return false;
		} else email2.removeClass('hightlight');
		
		
		
		
		//organize the data properly
		var data = 
		'name2=' + name2.val() 
		+ '&last2=' + last2.val() 
		+ '&company2=' + company2.val() 
		+ '&phone2=' + phone2.val() 
		+ '&email2=' + email2.val()
		+ '&ae2=' + encodeURIComponent(ae2.val())
		

		
		//disabled all the text fields
		$('.disable-input2').attr('disabled','true');

				setTimeout (function(){
					$('.loading-form2').fadeIn(); },300);
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "freeEnergyQuote-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) {	
				
						setTimeout (function(){
					$('.loading-form2').hide(); },2800);
				 

					setTimeout (function(){
					//show the success message
					$('.done2').fadeIn(1000); },2800);
					
					
					
						//show the loading sign
					$('#start-now-submit-btn2').hide();
					
				 
				//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;
	});	
});	

