$(document).ready(function() {
	
	//if submit button is clicked
	$('#submit').click(function () {		
		
		//Get the data from all the fields
		var name = $('input[name=name]');
		var email = $('input[name=email]');
        var address = $('input[name=address]');
        var company_name = $('input[name=company_name]');
        var phone_number = $('input[name=phone_number]');
        var state = $('input[name=state]');
        var zipcode = $('input[name=zipcode]');
		//var website = $('input[name=website]');
		var comment = $('textarea[name=comment]');

		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		/*
if (name.val()=='') {
			name.addClass('hightlight');
			error=1;
		} else name.removeClass('hightlight');
		
		if (email.val()=='') {
			email.addClass('hightlight');
			error=1;
		} else email.removeClass('hightlight');
        
        if (address.val()=='') {
			address.addClass('hightlight');
			error=1;
		} else address.removeClass('hightlight');
        
        if (company_name.val()=='') {
			company_name.addClass('hightlight');
			error=1;
		} else company_name.removeClass('hightlight');
        
        if (phone_number.val()=='') {
			phone_number.addClass('hightlight');
			error=1;
		} else phone_number.removeClass('hightlight');
        
        if (state.val()=='') {
			state.addClass('hightlight');
			error=1;
		} else state.removeClass('hightlight');
        
        if (zipcode.val()=='') {
			zipcode.addClass('hightlight');
			error=1;
		} else zipcode.removeClass('hightlight');
        
    
		if (comment.val()=='') {
			comment.addClass('hightlight');
			error=1;
		} else comment.removeClass('hightlight');
		
		if (error) return false;
*/ 
		if (name.val()=='') {
			name.addClass('hightlight');
			return false;
		} else name.removeClass('hightlight');
		
		/*
if (email.val()=='') {
			email.addClass('hightlight');
			return false;
		} else email.removeClass('hightlight');
        
*/

		if ((email.val()=='') || (email.val().indexOf("@")==-1) || (email.val().indexOf(".")==-1)) {
			email.addClass('hightlight');
			return false;
		} else email.removeClass('hightlight');
		
        if (address.val()=='') {
			address.addClass('hightlight');
			return false;
		} else address.removeClass('hightlight');
        
        if (company_name.val()=='') {
			company_name.addClass('hightlight');
			return false;
		} else company_name.removeClass('hightlight');
        
        if (phone_number.val()=='') {
			phone_number.addClass('hightlight');
			return false;
		} else phone_number.removeClass('hightlight');
        
        if (state.val()=='') {
			state.addClass('hightlight');
			return false;
		} else state.removeClass('hightlight');
        
        if (zipcode.val()=='') {
			zipcode.addClass('hightlight');
			return false;
		} else zipcode.removeClass('hightlight');
        
    
		if (comment.val()=='') {
			comment.addClass('hightlight');
			return false;
		} else comment.removeClass('hightlight');
		

		//organize the data properly
		var data = 'name=' + name.val() + '&email=' + email.val() + '&address=' + address.val() + '&company_name='  +
        company_name.val() + '&phone_number='  +
        phone_number.val() + '&state='  +
        state.val() + '&zipcode='  +
        zipcode.val() + '&comment='  + encodeURIComponent(comment.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: "includes/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)
				//alert(html)
				if (html==1) {					
					//hide the form
					$('.form').fadeOut('slow');					
					
					//show the success message
					$('.done').fadeIn('slow');
					$('.contactus').fadeOut('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;
	});	
});

