$(document).ready(function() {
	
	//if submit button is clicked
	$('#submit').click(function () {		
		
		//Get the data from all the fields
		var a1 = $('input[name=a1]');
		var a2 = $('input[name=a2]');
		var company = $('input[name=company]');
		var a3 = $('input[name=a3]');
		var a4 = $('input[name=a4]');
		var ab = $('input[name=ab]');
		var ac = $('select[name=ac]');
		var ad = $('textarea[name=ad]');
		var human = $('input[name=human]');
		

		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		
	
		
		if (a1.val()=='') {
			a1.addClass('hightlight');
			return false;
		} else a1.removeClass('hightlight');
		
		if (a2.val()=='') {
			a2.addClass('hightlight');
			return false;
		} else a2.removeClass('hightlight');
		
		if (a3.val()=='') {
			a3.addClass('hightlight');
			return false;
		} else a3.removeClass('hightlight');
		
			if (company.val()=='') {
			company.addClass('hightlight');
			return false;
		} else company.removeClass('hightlight');
		
		if (a4.val()=='') {
			a4.addClass('hightlight');
			return false;
		} else a4.removeClass('hightlight');

			if (ac.val()=='') {
			ac.addClass('hightlight');
			return false;
		} else ac.removeClass('hightlight');
		
	
		
		//show the loading sign
		$('.loading-form').show();
		
		
		//organize the data properly
		var data = 
		'a1=' + a1.val() 
		+ '&a2=' + a2.val() 
		+ '&a3=' + a3.val()
		+ '&company=' + company.val()
		+ '&ab=' + ab.val()
		+ '&a4=' + a4.val() 
		+ '&ac=' + ac.val()
		+ '&ad=' + encodeURIComponent(ad.val())
		+ '&human=' + human.val();
		

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

		
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "http://www.j9leadingsolutions.com/contact-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
					//show the success message
					$('.done').fadeIn('slow');
					
					
					$('.myform').fadeOut('fast');					
					
							//show the loading sign
					$('.loading-form').fadeOut('slow');
					
						//show the loading sign
					$('#start-now-submit-btn').hide('fast');
					
					//show the loading sign
					$('.contact-submit-replace').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;
	});	
});	



