﻿function hide(divid) {
    var div = document.getElementById(divid)
    div.style.display = "none";
}

function show(divid) {
    var div = document.getElementById(divid)
    div.style.display = "";
}

function toggleShowHide(divid) {
    var div = document.getElementById(divid)
    if (div.style.display == "none")
        div.style.display = "";
    else
        div.style.display = "none";
}

function showAll() {
    var allDivs = document.getElementsByTagName("div");
    for (var i = 0; i < allDivs.length; i++) {
        var div = allDivs[i];

        if (div.id != "" && div.id.indexOf("day") > -1)
            show(div.id);
    }
}

function openBrochure() {
    open("/acce2009/files/ACCE09_Brochure.pdf");
}

$(document).ready(function() {
    $("#leftAccordian .AccordianContent").pngfix({ sizingMethod: 'scale' });
    $(".title").pngfix({ sizingMethod: 'scale' });

    $("#forwardColleague").click(function(e) {
        e.returnValue = false;
        e.preventDefault();
        $("#dialog").modal({
            overlayCss: { backgroundColor: '#000000' },
            containerCss: { backgroundColor: '#3b3b3b', border: 'solid 3px #ffffff' },
            onOpen: function(dialog) {
                dialog.overlay.addClass("largeoverlay");
                dialog.overlay.fadeIn('fast', function() {
                    dialog.container.animate({ width: "500px", height: "450px" }, 'fast', 'swing', function() {
                        dialog.data.fadeIn('fast');
                    });
                });
            },
            onShow: function(dialog) {
                $("#fc_sendButton").click(function() {
                    var validationCode = validate()
                    if (validationCode.status == "success") {
                        $("#dialog .form").fadeOut('fast', function() {
                            dialog.container.animate({ height: "100px" }, 'fast', 'swing', function() {
                                $("#dialog .message").html("Sending...").show('fast');
                                $.post("/acce2009/sendEmail.aspx",
                                    {
                                        your_name: $("#fc_your_name").val(),
                                        your_email: $("#fc_your_email").val(),
                                        colleague_emails: $("#fc_colleague_emails").val(),
                                        message: $("#fc_message").val(),
                                        subject: "ICMI ACCE 2009: Forward to a Colleague"
                                    },
                                    function(data) {
                                        if (data.StatusCode == "Success") {
                                            $("#dialog .message").hide('fast', function() {
                                                $("#dialog .sentMessage").html(data.MessageHtml).css({ fontWeight: "bold" }).show('fast');
                                            });
                                        }
                                    },
                                    "json");
                            });
                        });
                    }
                    else {
                        $("#dialog .error").html(validationCode.message).show(200);
                    }
                });
            },
            onClose: function(dialog) {
                dialog.data.fadeOut('fast', function() {
                    dialog.container.animate({ width: "0px", height: "0px" }, 'fast', 'swing', function() {
                        dialog.overlay.fadeOut('fast', function() { $.modal.close(); });
                    });
                });
            }
        });
    });
});

function validate() {
    var statusCode = "success";
    var validationMessage = "";

    var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if ($("#fc_your_name").val().length == 0) {
        statusCode = "name";
        validationMessage = "'Your Name' cannot be empty!";
    }
    else if ($("#fc_your_email").val().length > 0 && !emailFilter.test($("#fc_your_email").val())) {
        statusCode = "email";
        validationMessage = "Verify 'Your Email' is in the proper format!";
    }
    else if ($("#fc_colleague_emails").val().length == 0) {
        statusCode = "colleagues";
        validationMessage = "You must specify at least one colleague!";
    }
    else if ($("#fc_colleague_emails").val().length > 0) {
        var emails = $("#fc_colleague_emails").val().split(",");

        for (var i = 0; i < emails.length; i++) {
            if (!emailFilter.test(emails[i])) {
                statusCode = "colleagues";
                validationMessage = "Verify colleague emails are in the proper format!";
                break;
            }
        }
    }

    return { status: statusCode, message: validationMessage };
}