// conContactFunction
//This triggers the JQuery for the contact form

$(document).ready(function() {
	
	//if submit button is clicked
	$('#conSubmitBtn').click(function () {		
		
		//Get the data from all the fields
		var dname = $('input[name=conCN]');
		var demail = $('input[name=conCE]');
		var dphone = $('input[name=conCP]');
		var durl = $('input[name=conCU]');
		var dmessage = $('textarea[name=conCM]');
		
		var demailFilter = /^.+@.+\..{2,3}$/;

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

		if (demail.val()=='') {
			demail.addClass('CInputHL');
			return false;
		} else demail.removeClass('CInputHL');
		
		if (!(demailFilter.test(demail.val()))) {
			demail.addClass('CInputHL');
			return false;
		} else demail.removeClass('CInputHL');

		if (dmessage.val()=='') {
			dmessage.addClass('CTextAreaHL');
			return false;
		} else dmessage.removeClass('CTextAreaHL');
		
		//organize the data properly
		var data = 'conCN=' + dname.val() + '&conCE=' + demail.val() + '&conCP=' + dphone.val() + '&conCU=' + durl.val() + '&conCM=' + encodeURIComponent(dmessage.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/conCProcess.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
					$('.formContainer').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;
	});	
});	

