﻿    var rate_warned = false;
    function rate() {
        var data = getData();
    
        if (!rate_warned) {
            // validate
            var m = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
            
            // empty
            if (!data.dirty && !rate_warned) {
                    $("#invalid").text("The form is empty.");
                    rate_warned = true;
            }
            
            // email
            if (data.contactMe && !rate_warned && !m.test(data.email)) {
                    $("#invalid").text("Your e-mail seems to be invalid.");
                    rate_warned = true;
            }
            
            if (rate_warned) {
                    $("#invalid").show();
                    $("#submit").text("Submit anyway");
                    delayedReset();
                    return;
            }
        }

	$("#submit").attr('disabled', 'disabled');

        $.ajax({
            type: "POST",
            url: "/RateIt/RateIt.aspx?" + new Date().getTime(),
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data, textStatus) {
                $("#box").fadeOut("slow", function() {
                    $("#result").fadeIn("slow");
                });
/*
                $("#box").slideUp("slow", function() {
                    $("#result").slideDown("slow");
                });
*/
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                error();
            }
        });
    }
    
    function error() {
	$("#submit").removeAttr('disabled');
        $("#invalid").text("Something went wrong. Please try again later.");
        $("#invalid").show();
        delayedReset();
    }
    
    function resetValidator() {
        $("#invalid").fadeOut("slow", function() {
            $("#submit").text("Submit");
            rate_warned = false;
        });
    }
    
    function delayedReset() {
        window.setTimeout(resetValidator, 2000);
    }
    
    function updateEmail() {
        var checked = $("#contactMe").length > 0 && $("#contactMe")[0].checked;
        
        if (checked) {
            $("#email").removeAttr('disabled');
        }
        else {
            $("#email").attr('disabled', 'disabled');
        }
    }
    
    function getData() {
        var vote = {};
        vote.dirty = false;
        
        $("#likePrice").each(function() {
            vote.likePrice = this.checked;
        });
        $("#likeFeatures").each(function() {
            vote.likeFeatures = this.checked;
        });
        $("#likeEasy").each(function() {
            vote.likeEasy = this.checked;
        });
        $("#likeReleases").each(function() {
            vote.likeReleases = this.checked;
        });
        
        $("#dontLikePrice").each(function() {
            vote.dontLikePrice = this.checked;
        });
        $("#dontLikeFeatures").each(function() {
            vote.dontLikeFeatures = this.checked;
        });
        $("#dontLikeComplex").each(function() {
            vote.dontLikeComplex = this.checked;
        });
        $("#dontLikeLook").each(function() {
            vote.dontLikeLook = this.checked;
        });
        
        $("#contactMe").each(function() {
            vote.contactMe = this.checked;
        });
        $("#email").each(function() {
            vote.email = this.value;
        });
        $("#comment").each(function() {
            vote.comment = this.value;
            vote.dirty = this.value ? true : false;
        });
        
        $(":checkbox:checked").each(function() {
            vote.dirty = true;
        });
        
        return vote;
    };
