// indContactFunction
//This triggers the JQuery for the index contact form

$(document).ready(function() {
	
	//if submit button is clicked
	$('#submitBtn').click(function () {		
		
		//Get the data from all the fields
		var name = $('input[name=indCN]');
		var email = $('input[name=indCE]');
		var phone = $('input[name=indCP]');
		var cmessage = $('textarea[name=indCM]');
		
		var emailFilter = /^.+@.+\..{2,3}$/;


		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if ((name.val()=='')||(name.val().length < 2)) {
			name.addClass('hightlight');
			return false;
		} else name.removeClass('hightlight');


		if (email.val()=='') {
			email.addClass('hightlight');
			return false;
		} else email.removeClass('hightlight');
		
		if (!(emailFilter.test(email.val()))) {
			email.addClass('hightlight');
			return false;
		} else email.removeClass('hightlight');

		if (cmessage.val()=='') {
			cmessage.addClass('hightlight');
			return false;
		} else cmessage.removeClass('hightlight');
		
		//organize the data properly
		var data = 'indCN=' + name.val() + '&indCE=' + email.val() + '&indCP=' + phone.val() + '&indCM='  + encodeURIComponent(cmessage.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: "/contact/indCProcess.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
					$('.contactContainer').fadeOut('slow');					
					
					//show the success message
					$('.cFormSuccess').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;
	});	
});	

