$(document).ready(function() {
	
	//if submit button is clicked
	$('#submit').click(function () {		
		
		firstnameVar = "tx_cmregister_pi1[firstname]";
		lastnameVar = "tx_cmregister_pi1[lastname]";
		addressVar = "tx_cmregister_pi1[address]";
		postalVar = "tx_cmregister_pi1[postal]";
		emailVar = "tx_cmregister_pi1[email]";
		phoneVar = "tx_cmregister_pi1[phone]";
		
		//Get the data from all the fields
		var firstname = $('#form-firstname');
		var lastname = $('#form-lastname');
		var address = $('#form-address');
		var postal = $('#form-postal');
		var email = $('#form-email');
		var phone = $('#form-phone');
		
		var errors = false;
			
		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
	
		if (firstname.val()=='' || firstname.val() == firstname.attr("defaultValue")) {
			
			firstname.parent().addClass('highlight');
			errors = true;
		} else firstname.parent().removeClass('highlight');
		
		if (lastname.val()=='' || lastname.val() == lastname.attr("defaultValue")) {
			lastname.parent().addClass('highlight');
			errors = true;
		} else lastname.parent().removeClass('highlight');
		
		if (address.val()=='' || address.val() == address.attr("defaultValue")) {
			address.parent().addClass('highlight');
			errors = true;
		} else address.parent().removeClass('highlight');
		
		if (postal.val()=='' || postal.val() == postal.attr("defaultValue")) {
			postal.parent().addClass('highlight');
			errors = true;
		} else postal.parent().removeClass('highlight');
		
		if (email.val()=='' || email.val() == email.attr("defaultValue")) {
			email.parent().addClass('highlight');
			errors = true;
		} else email.parent().removeClass('highlight');
		
		if (phone.val()=='' || phone.val() == phone.attr("defaultValue")) {
			phone.parent().addClass('highlight');
			errors = true;
		} else phone.parent().removeClass('highlight');
		
		if (errors) return false;
		
		//organize the data properly
		var data = firstnameVar+'=' + encodeURIComponent(firstname.val()) + '&'+lastnameVar+'=' + encodeURIComponent(lastname.val()) + '&'+addressVar+'=' + encodeURIComponent(address.val()) + '&'+postalVar+'='  + encodeURIComponent(postal.val()) + '&'+emailVar+'='  + encodeURIComponent(email.val()) + '&'+phoneVar+'='  + encodeURIComponent(phone.val());
		
		//disabled all the text fields
		$('.text').attr('disabled','true');
		
		//show the loading sign
		$('.loading').show();
		
		//start the ajax
		postUrl = $("#registrationform").attr("action");
		if(postUrl == "") alert('Fant ikke processurl');
		
		$.ajax({
			//this is the php file that processes the data and send mail
			url: postUrl,	
			
			//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').fadeOut('slow', function(){
						$('#form-success').fadeIn('slow');					
						
					});
					
					//show the success message
					/*$('#form-success').fadeIn('slow');*/
					
				//if process.php returned 0/false (send mail failed)
				} else alert('Beklager, uventet feil. Vennligst pr&oslash;v igjen senere.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
});	
