$(document).ready(function() {
	
	//if submit button is clicked
	$('#submit2').click(function () {

//Get the data from all the fields
		var name = $('input[name=cname]');
		var phone = $('input[name=phone]');
		var email = $('input[name=email]');
		var remarks = $('textarea[name=remarks]');
		
		//alert(childs.val());
		//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');
			alert("Your name please.");
			name.focus();
			return false;
		} else name.removeClass('hightlight');
		
		if (phone.val()=='') {
			phone.addClass('hightlight');
			alert("Your phone number please.");
			phone.focus();
			return false;
		} else phone.removeClass('hightlight');

		//organize the data properly
		var data = 'name=' + name.val() + '&phone=' + phone.val() + '&email=' + email.val() + '&remarks=' + remarks.val();
		
		//alert(data);
		//disabled all the text fields
		$('.f').attr('disabled','true');
		
		//show the loading sign
		$('#sending').show();
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "ajax/process-contact-us.php",
			//url: "http://localhost/ajaxform/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) {
					$('#sending').hide();
					alert('We received your message and will get back to you soon. Thank you.');
					$('#reset').click();
					$('.f').removeAttr('disabled');
				
				//if process.php returned 0/false (send mail failed)
				}
				else {
					alert('Sorry, unexpected error. Please try again later.');
					$('#sending').hide();
					$('.f').removeAttr('disabled');
				}
				//alert(html);
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});
});
