$(document).ready(function () { 
    /* Hide extra recipient fields in send form */
    $("div.recipient-input").slice(1).hide();

    /* Fetch reply data */
    var reply_cookie = $.cookie("ecard_reply_id");
    var $cancel_button = $("#form-button-cancel_reply");
    /* make reply if we have working cookie */
    if (reply_cookie!==null) {
        reply_cookie = reply_cookie.replace(/"/g, "");
        $.getJSON(context_url+"/ecard-data?ecard="+reply_cookie,
              function(data) {
                  $("#sender_name")[0].value = data.recipient_name;
                  $("#sender_email")[0].value = data.recipient_email;
                  $(".recipient_names:first")[0].value = data.sender_name;
                  $(".recipient_emails:first")[0].value = data.sender_email;
                  $cancel_button.show();
              });
    }
    /* Hide cancel button if not in reply. Do actual cancel */
    if (reply_cookie===null) { $cancel_button.hide(); }

    /* Bind cancel */
    $cancel_button.click(function() {
        $("#sender_name")[0].value = "";
        $("#sender_email")[0].value = "";
        $(".recipient_names:first")[0].value = "";
        $(".recipient_emails:first")[0].value = "";
        $.cookie("ecard_reply_id", null, {path: "/"});
        $cancel_button.hide();
    });
});

function addRecipient() {
    var visible = $("div.recipient-input:visible").length;
    $("div.recipient-input:eq(" + visible +")").show();
    if (visible==4) {
        $("div#add-new-recipients").fadeOut();
    }  
}


