// javascript for the contact page

$(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 website = $('input[name=website]');
		var inquiry = $('textarea[name=inquiry]');
		
		//Get the error divs
		var nameError = document.getElementById('nameError');
		var emailError = document.getElementById('emailError');
		var inquiryError = document.getElementById('inquiryError');

		//Simple validation to make sure user entered something
		//If error found, add highlight class to the text field and display error message
		if (name.val()=='') {
			name.addClass('highlight');
			nameError.setAttribute('class', 'error');
			name.focus()
			return false;
		} else {
			name.removeClass('highlight');
			nameError.style.display = "none";
		}
		
		if (email.val()=='') {
			email.addClass('highlight');
			emailError.setAttribute('class', 'error');
			email.focus()
			return false;
		} else {
			email.removeClass('highlight');
			emailError.style.display = "none";
		}
		
		if (inquiry.val()=='') {
			inquiry.addClass('highlight');
			inquiryError.setAttribute('class', 'error');
			inquiry.focus()
			return false;
		} else {
			inquiry.removeClass('highlight');
			inquiryError.style.display = "none";
		}
		
		//organize the data properly
		var data = 'name=' + name.val() + '&email=' + email.val() + '&website=' + 
		website.val() + '&inquiry='  + encodeURIComponent(inquiry.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 sends the email
			url: "includes/inquiryprocess.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 and the instructions
					$('#inquiryform').fadeOut(20);
					//$('.instructions').fadeOut('slow');					

					//show the success message
					$('.done').fadeIn(1200);
					
				//if process.php returned 0/false (send mail failed)
				} else alert('Sorry, an unexpected error occurred. Please try again.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
});
