$(function() { if ($(".giving_back_form").size() == 0) return false; $("#GivingBackGivingBackFormId").change(function() { $(".gbf").hide(); var form = form_code(); if (form) { $(".gbf." + form).show(); } var saved_data = $.cookie(cookie_name()); if (saved_data) { if (confirm("You have got a saved application for this grant, would you like to retrieve your saved application?")) { window.setTimeout(function() { $(".load_application").click(); }, 100); } } return true; }); if (document.location.search) { var form = String(document.location.search).substring(1); $(".gbf." + form).show(); $(".gbf." + form + " h3 + p > a").attr("href", "/forms/" + form + ".pdf"); $.each(jsdata(), function(id, r) { if (form == r.code) { $("#GivingBackGivingBackFormId").val(id).change(); return false; } }); } $(".gbf input[type=checkbox]").add(".gbf input[type=radio]").click(function() { return $(".preview_application:visible").size() > 0 ? true : false; }); $(".gbf select").change(function() { $(this).parent().next()[$(this).val() == "other" ? "show" : "hide"](); }).change(); $(".preview_application").click(function() { $(".change_application").add(".submit_application").show(); $(".preview_application").add(".save_application").add(".load_application").hide(); var scope = $(".gbf:visible")[0]; $("input[type=text]", scope).each(function() { var value = $(this).hide().val(); $(this).after($("").text(value)); }); $("select", scope).each(function() { var value = $("option", this).eq($(this).hide()[0].selectedIndex).text(); $(this).after($("").text(value)); }); $("textarea", scope).each(function() { var value = $(this).hide().val(); $(this).after($("").text(value)); }); $(document).scrollTop(0); return false; }); $(".save_application").click(function() { var scope = $(".gbf:visible")[0]; var data = {}; $("input[type=text]", scope).each(function() { data[$(this).attr("name")] = $(this).val(); }); $("textarea", scope).each(function() { data[$(this).attr("name")] = $(this).val(); }); $("select", scope).each(function() { data[$(this).attr("name")] = $(this).val(); }); $("input[type=radio]", scope).each(function() { if ($(this).attr("checked")) data[$(this).attr("name")] = $(this).val(); }); $("input[type=checkbox]", scope).each(function() { if ($(this).attr("checked")) data[$(this).attr("name")] = "yes"; }); $.cookie(cookie_name(), JSON.stringify(data), { expires: 30, path: '/' }); alert("Saved"); return false; }); $(".load_application").click(function() { var saved_data = $.cookie(cookie_name()); if (saved_data) { var data = JSON.parse(saved_data); var scope = $(".gbf:visible")[0]; $("input[type=text]", scope).each(function() { $(this).val(data[$(this).attr("name")] || ""); }); $("textarea", scope).each(function() { $(this).val(data[$(this).attr("name")] || ""); }); $("select", scope).each(function() { $(this).val(data[$(this).attr("name")] || "").change(); }); $("input[type=radio]", scope).each(function() { if ((data[$(this).attr("name")] || "") == $(this).val()) { $(this).attr("checked", true); } else { $(this).attr("checked", false); } }); $("input[type=checkbox]", scope).each(function() { if (data[$(this).attr("name")] || false) { $(this).attr("checked", true); } else { $(this).attr("checked", false); } }); } else { alert("Sorry, you don't have any saved application for this grant.") } }); $(".change_application").click(function() { $(".change_application").add(".submit_application").hide(); $(".preview_application").add(".save_application").add(".load_application").show(); var scope = $(".gbf:visible")[0]; $("input[type=text]", scope).each(function() { $(this).next("span").remove(); $(this).show(); }); $("textarea", scope).each(function() { $(this).next("span").remove(); $(this).show(); }); $("select", scope).each(function() { $(this).next("span").remove(); $(this).show(); }); $(document).scrollTop(0); return false; }); $(".giving_back_form").submit(function() { var url = $(this).attr("action"); var id = form_id(); var success_page = jsdata()[id].success_page; var scope = $(".gbf:visible")[0]; var data = {}; $("input[type=text]", scope).each(function() { data[$(this).attr("name")] = $(this).val(); }); $("textarea", scope).each(function() { data[$(this).attr("name")] = $(this).val(); }); $("select", scope).each(function() { data[$(this).attr("name")] = $(this).val(); }); $("input[type=radio]", scope).each(function() { if ($(this).attr("checked")) data[$(this).attr("name")] = $(this).val(); }); $("input[type=checkbox]", scope).each(function() { if ($(this).attr("checked")) data[$(this).attr("name")] = "yes"; }); $.post(url, {"data[GivingBack][giving_back_form_id]": id, "data[GivingBack][response]": JSON.stringify(data)}, function (resp) { window.open(success_page, "_self"); }); return false; }); function jsdata() { return JSON.parse($("#GivingBackJsdata").val()); } function form_code() { return jsdata()[form_id()].code || ""; } function form_id() { return $("#GivingBackGivingBackFormId").val(); } function cookie_name() { return "saved_gbf_" + form_id(); } });