// JavaScript Document

$(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 phone = $('input[name=phone]');
		var company = $('input[name=company]');
		var type = $('select[name=type]');
		var enquiry = $('textarea[name=enquiry]');
		var code = $('input[name=code]');
		
		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
			if (name.val()=='Your Name') {
			name.addClass('hightlight');
			return false;
		} else name.removeClass('hightlight');
 
	
	if (company.val()=='Company Name') {
			company.addClass('hightlight');
			return false;
		} else company.removeClass('hightlight');
		
		
		if (phone.val()=='Telephone Number') {
			phone.addClass('hightlight');
			return false;
		} else phone.removeClass('hightlight');
		
		if (email.val()=='Email Address') {
			email.addClass('hightlight');
			return false;
		} else email.removeClass('hightlight');
		
		
		if (enquiry.val()=='Your Enquiry') {
			enquiry.addClass('hightlight');
			return false;
		} else enquiry.removeClass('hightlight');
		
		if (type.val()=='Service Type') {
			type.addClass('hightlight');
			return false;
		} else type.removeClass('hightlight');
		
		
		if (code.val()!=='bsm123') {
			code.addClass('hightlight');
			return false;
		} else code.removeClass('hightlight');
		
		//organize the data properly
		var data = 'name=' + name.val() + '&email=' + email.val() + '&phone=' + phone.val() + '&type=' + type.val() + '&code=' + code.val() + '&enquiry='  + encodeURIComponent(enquiry.val());
		
		//disabled all the text fields
		$('.default').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: "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) {					
					//hide the form
					$('.form').animate({
    width: ['toggle', 'swing'],
    height: ['toggle', 'swing'],
    opacity: 'toggle'
  }, 1000, 'linear');			
					
					//show the success message
					$('.done').delay(800).animate({
    width: ['toggle', 'swing'],
    height: ['toggle', 'swing'],
    opacity: 'toggle'
  }, 1000, 'linear');
				//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;
	});	
});	

