var animationSpeed = 600;
var results = new Array();
var boxWidth = 401;
var notify = true;

function getRandomNumber(min, max) {
    return Math.round((max - min) * Math.random() + min);
}

function sendResults() {
    var n1 = getRandomNumber(100, 999);
    var n2 = getRandomNumber(100, 999);
    var n3 = getRandomNumber(100, 999);
    var reference = n1 + " " + n2 + " " + n3;

    var data = "";
    for(var i = 0; i < results.length; i++) {
        data = data + results[i][0] + "\n- " + results[i][1].join("\n- ") + "\n\n";
    }

    $("#reference").val(reference);
    $("#results").val(data);
    $("#valid").val("yes");

    var name = $("#name").val();
    var phone = $("#phone").val();
    var email = $("#email").val();
    var date = $("#date").val();
    var time = $("#time").val();
    var town = $("#town").val();

    var dataString = 'submit=submit&valid=yes&mobile=0&name='+ name + "&phone=" + phone + "&email=" + email + "&date=" + date + "&time=" + time + "&town=" + town;

    $("input[type='checkbox'][name='service']").each(function() {
        if(this.checked) {
            dataString += "&service[]=" + this.value;
        }
    });

    //alert(dataString); return false;

    $.ajax({
        type: "POST",
        url: "http://lcs.myweddingsupermarket.com/complete.php",
        data: dataString,
        success: function() {
        }
    });

    $("#form1").submit();
}

function selectAssociatedInput() {
    var parent = $(this).parent();
    var checkBox = parent.find("input[type=checkbox]:first");
    var radioButton = parent.find("input[type=radio]:first");

    if(checkBox.size() > 0) {
        checkBox.attr("checked", true);
    }
    else if(radioButton.size() > 0) {
        radioButton.attr("checked", true);
    }
}

$(document).ready(function()
{
    var i = 0;
    var boxes = $(".part").each(function() {
        $(this).css("left", (i * boxWidth) + "px");
        $(this).css("visibility", "visible");

        /*if(i > 0) {
            $(this).append('<a href="javascript:;" class="back">Back</a>');
        }*/

        i = i + 1;
    });

    $("input[type=text]").focus(selectAssociatedInput);
    $("textarea").focus(selectAssociatedInput);

    $("label").click(function() {
        var input = $(this).parent().find("input[type!=text]");

        if(input) {
            if(input.attr("checked")) {
                input.attr("checked", false);
            }
            else {
                input.attr("checked", true);

                if(input.attr("type") == "radio") {
                    input.click();
                }
            }
        }
    });

    $("input[type=radio]").click(function() {
        if($(this).attr("checked")) {
            var button = $(this).parents(".part").find(".button");
            if(button) {
                notify = false;
                button.click();
            }
        }
    });

    $("a.button").click(function() {
        var parent = $(this).parent();
        var question = parent.find("span:first").text();
        var details =  parent.hasClass("details");
        var answers = new Array();
        var notified = false;
        var count = 0;
        var valid = 0;

        if(details) {
            var errors = "";

            parent.find("input[type=text]").each(function() {
                var value = $(this).val();
                var label = $(this).parent().find("label");
                count = count + 1;

                if(value.length <= 0) {
                    if(!$(this).hasClass("optional")) {
                        var label = $(this).parent().find("label").text();
                        errors = errors + "Please complete " + label + ".\n";
                        valid = 0;
                    }
                    else {
                        valid = valid + 1;
                    }
                }
                else {
                    valid = valid + 1;
                    answers.push((label.size() == 1 ? $(label).text() + ": " : "") + value);
                }
            });;

            if(errors.length > 0) {
                if(notify) {
                    alert(errors);
                }
                notified = true;
            }
        }
        else {
            parent.find("input[type!=text]").each(function() {
                var input = $(this);
                count = count + 1;

                if(input.attr("checked")) {
                    var textbox = input.parent().find("input[type=text]");
                    var textarea = input.parent().find("textarea");
                    var label = input.parent().find("label");

                    if(textbox.size() > 0) {
                        if(textbox.attr("value").length <= 0) {
                            if(notify) {
                                alert("Please complete the textbox.");
                            }
                            count = count - 1;
                            notified = true;
                            textbox.focus();
                        }
                        else {
                            answers.push((label ? $(label).text() + " " : "") + textbox.attr("value"));
                        }
                    }
                    else if(textarea.size() > 0) {
                        if(textarea.attr("value").length <= 0) {
                            if(notify) {
                                alert("Please complete the textarea.");
                            }
                            count = count - 1;
                            notified = true;
                            textarea.focus();
                        }
                        else {
                            answers.push((label ? $(label).text() + " " : "") + textarea.attr("value"));
                        }
                    }
                    else {
                        answers.push(label ? label.text() : "Unknown");
                    }
                }
                else {
                    count = count - 1;
                }
            });;
        }

        if(details) {
            if(count != valid) {
                count = 0;
            }
        }

        if(count > 0 && !notified) {
            results.push(new Array(question, answers));

            if($(this).hasClass("final")) {
                sendResults();
            }
            else {
                $(".part").animate({
                    left: "-=" + boxWidth + "px"
                }, animationSpeed);
            }
        }
        else if(!notified && notify) {
            alert("Please select an option.");
        }

        notify = true;
    });

    $("a.back").click(function() {
        $(".part").animate({
            left: "+=" + boxWidth + "px"
        }, animationSpeed);
    });

    $("#suppliers-button").hover(
        function() {
            var menu = $(this).find("ul");
            $(menu).hide();
            $(menu).css("visibility", "visible");
            $(menu).fadeIn("fast");
        },
        function() {
            var menu = $(this).find("ul");
            $(menu).fadeOut("fast");
            $(menu).css("visibility", "hidden");
        }
    );

    $("#suppliers-button a.menu-button").click(
        function() {
            var menu = $(this).parent().find("ul");

            if($(menu).css("visibility") == "visible") {
                $(menu).fadeOut("fast");
                $(menu).css("visibility", "hidden");
            }
            else {
                $(menu).hide();
                $(menu).css("visibility", "visible");
                $(menu).fadeIn("fast");
            }

            return false;
        });
});