    $(document).ready(function () {
        // Username validation logic
        var validateUsername = $('#validateUsername');
        $('#username').keyup(function () {
            // cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
            var t = this; 
            
            // only run the check if the username has actually changed - also means we skip meta keys
            if (this.value != this.lastValue) {
                
                // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                // a particular key, it will only fire when the release the key.
                                
                if (this.timer) clearTimeout(this.timer);
                
                // show our holding text in the validation message space
                validateUsername.removeClass('error').html('<img src="../images/ajax-loader.gif" height="16" width="16" /> Checking availability...');
                
                // fire an ajax request in 1/5 of a second
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'home.php',
                        data: 'p=register&action=check_username&username=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            // put the 'msg' field from the $resp array from check_username (php code) in to the validation message
                            validateUsername.html(j.msg);
                        }
                    });
                }, 200);
                
                // copy the latest value to avoid sending requests when we don't need to
                this.lastValue = this.value;
            }
        }).change(function () {
            $(this).keyup(); // call the keyup function
        });
		
		
		// Username validation logic
        var validateEmail = $('#validateEmail');
        $('#email').keyup(function () {
            // cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
            var t = this; 
            
            // only run the check if the username has actually changed - also means we skip meta keys
            if (this.value != this.lastValue) {
                
                // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                // a particular key, it will only fire when the release the key.
                                
                if (this.timer) clearTimeout(this.timer);
                
                // show our holding text in the validation message space
                validateEmail.removeClass('error').html('<img src="../images/ajax-loader.gif" height="16" width="16" /> Checking availability...');
                
                // fire an ajax request in 1/5 of a second
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'home.php',
                        data: 'p=register&action=check_email&email=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            // put the 'msg' field from the $resp array from check_username (php code) in to the validation message
                            validateEmail.html(j.msg);
                        }
                    });
                }, 200);
                
                // copy the latest value to avoid sending requests when we don't need to
                this.lastValue = this.value;
            }
        }).change(function () {
            $(this).keyup(); // call the keyup function
        });
        
		
		//form fields validation
		$("#validateForm").validate({
			rules: {
				firstName: "required",
				lastName: "required",
				f_middlename: "required",
				sss: "required",
				username: {
					required: true,
					minlength: 4
				},
				password1: {
					required: true,
					minlength: 5
				},
				password2: {
					required: true,
					minlength: 5,
					equalTo: "#password1"
				},
				email: {
					required: true
				},
				topic: {
					required: "#newsletter:checked",
					minlength: 2
				},
				phoneNumber: "required",
				f_address: "required",
				f_city: "required",
				f_zip: "required",
				f_civilstatus: "required",
				f_spouse_firstname: "required",
				f_spouse_middlename: "required",
				f_spouse_lastname: "required",
				f_state: "required",
				f_country: "required",
				
				agree: "required"
			},
			
			messages: {
				firstName: "<br />* Please enter your firstname",
				lastName: "<br />* Please enter your lastname",
				f_middlename: "<br />* Please enter your middlename",
				sss: "<br />* Please enter your SS Number for US and Canada<br />(or Int'l Taxpayer ID Number for other countries.)",
				username: {
					 required: "<br />* Please select a username",
					 minlength: "<br />* Your username must be at least 4 characters long" 
				},
				password1: {
					required: "<br />* Please provide a password",
					minlength: "<br />* Your password must be at least 5 characters long"
				},
				password2: {
					required: "<br />* Please provide a password",
					minlength: "<br />* Your password must be at least 5 characters long",
					equalTo: "<br />* Please enter the same password as above"
				},
				email: "<br />* Please enter a valid email address",
				phoneNumber: "<br />* Please enter your phone number",
				f_address: "<br />* Please enter your address",
				f_city: "<br />* Please enter the city you are living in",
				f_zip: "<br />* Please enter your zip code",
				f_civilstatus: "<br />* Please select a status",
				f_spouse_firstname: "<br />* Please enter your spouse's firstname",
				f_spouse_middlename: "<br />* Please enter your spouse's middlename",
				f_spouse_lastname: "<br />* Please enter your spouse's lastname",
				f_state: "<br />* Please select a state",
				f_country: "<br />* Please select a country",
				
				agree: "<br />* Please accept our policy"
			}							
		});	//end form fields validation
		
    });
