        function disableSubmitButton() {
            $('#btnSubmit').attr('disabled', 'disabled').text('Please wait...');
        }

        function enableSubmitButton() {
            $('#btnSubmit').removeAttr('disabled').text('Submit');
        }
        
        function formatFields() {
            $(".integer").numeric({ format: "0" });
        }

        function redirectToThankyouPage() {
            document.location = "Content/thankyou.htm";
        }
        
        function applyValidation() {

			$.validator.addMethod("clarification", function(value) {
				var clarification = $('#OtherClarification');				
				if (!clarification.is(':visible')) {
					return true;
				}
				
                return (clarification.val() != '');
            }, 'Please supply a description of your company type.');


            if (homepage) {
                $.validator.setDefaults({
                        submitHandler: function() {
                            disableSubmitButton();
                            var dataString = $("#registrationForm").serialize();

                            $.ajax({
                                    type: "POST",
                                    url: "/Home.aspx/Register",
                                    data: dataString,
                                    dataType: "json",
                                    success: function(data) {
                                        redirectToThankyouPage();
                                    },
                                    error: function(xhr, ajaxOptions, thrownError) {
                                        // Invalid captcha
                                        if (xhr.status == '403') {
                                            $('#invalidCaptcha').show();
                                            Recaptcha.reload();
                                            enableSubmitButton();
                                        }
                                        else if (xhr.status == '200') {
                                            redirectToThankyouPage();
                                        }
                                        else {
                                            enableSubmitButton();
                                            alert(xhr.status);
                                            alert(thrownError);
                                        }
                                    }
                                });
                        }
                    });
            }

            $("#registrationForm").validate({
                rules: {
                    Name: "required",
                    Surname: "required",
					CompanyName: "required",
                    EmailAddress: {
                        required: true,
                        email: true
                    },
					Telephone: {
                        minlength: 10
                    },
                    Fax: {
                        minlength: 10
                    },
                    OtherClarification : "clarification"
                },
                messages: {
                    Name: "Please enter your name",
					Surname: "Please enter your surname",
					CompanyName: "Please enter your company name",
					EmailAddress: "Please enter a valid email address",
                    Telephone: "Please enter a valid telephone number",
                    Fax: "Please enter a valid fax number",
                    Cell: "Please enter a valid cell number",
                    PhysicalAddress: "Please enter a valid physical address",
                    PostalAddress: "Please enter a valid postal address",
                    OtherClarification: "Please supply a description of your company type"
                }
            });
        }
        

        $(document).ready(function() {
            formatFields();
            applyValidation();
			
			$('#CompanyType').change(function() {
				var val = $(this).val();
				
				if (val == 'Other') {
					$('#OtherContainer').show();
				}
				else {
					$('#OtherContainer').hide();
				}
			});
			
			
			 $(".scroll").click(function(event){
				//prevent the default action for the click event
				event.preventDefault();
		 
				//get the full url - like mysitecom/index.htm#home
				var full_url = this.href;
		 
				//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
				var parts = full_url.split("#");
				var trgt = parts[1];
		 
				//get the top offset of the target anchor
				var target_offset = $("#"+trgt).offset();
				var target_top = target_offset.top;
				
				//goto that anchor by setting the body scroll top to anchor top
				$('html, body').animate({scrollTop:target_top}, 500);
			});
        });
