$(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 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()=='') {
			 alert( "Before submitting this form you must enter Your Name" );
			return false;
		} 
		
	if (a2.val()=='') {
			 alert( "Before submitting this form you must enter Your Last Name" );
			return false;
		} 
	
	if (company.val()=='') {
			 alert( "Before submitting this form you must enter Your Company Name" );
			return false;
		} 
	if (a4.val()=='') {
			 alert( "Before submitting this form you must enter Your Email" );
			return false;
		} 
	if (ab.val()=='') {
			 alert( "Before submitting this form you must enter Your Phone Number" );
			return false;
		} 
		
		
		
	
		
		//show the loading sign
		$('.loading-form').show();
		
		
		//organize the data properly
		var data = 
		'a1=' + a1.val() 
		+ '&a2=' + a2.val() 
		+ '&company=' + company.val()
		+ '&a3=' + a3.val()
		+ '&a4=' + a4.val() 
		+ '&ab=' + ab.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/home-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
					$('.submit').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;
	});	
});	


