Advertisement
Guest User

Main Javascript

a guest
Jan 9th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $("document").ready(function () {
  2.     "use strict";
  3.     var currentStep;
  4.     //Hide additional steps and the differences between the two steps if they have Javascript
  5.     function hideSteps() {
  6.         //hide the divs
  7.         $("#step2").hide();
  8.         $("#step3").hide();
  9.         $("#step4").hide();
  10.         $("#step5").hide();
  11.         $("#step6").hide();
  12.         $("#showResults").hide();
  13.         $("#makingAOneTimeGift").hide();
  14.         $("#makingARecurringGift").hide();
  15.     }
  16.     function showNextStep(currentStep) {
  17.         var chosenDonationType, checkedAllocations, selectedAllocationValue;
  18.         $("#step" + currentStep).slideToggle("slow", function () {
  19.             if (currentStep === 1) {
  20.                 //figure out what kind of donation they are making
  21.                 chosenDonationType = $("[name=donationType]").val();
  22.                 //show the apppropriate slide
  23.                 switch (chosenDonationType) {
  24.                 case "oneTimeGift":
  25.                     currentStep += 1;
  26.                     $("#makingAOneTimeGift").show();
  27.                     $("#step" + currentStep).slideToggle("slow");
  28.                     break;
  29.                 case "recurringDonation":
  30.                     currentStep += 1;
  31.                     $("#makingARecurringGift").show();
  32.                     $("#step" + currentStep).slideToggle("slow");
  33.                     break;
  34.                     //if somehow they changed it to something else, ignore them and return false.
  35.                 default:
  36.                     return false;
  37.                     //break; not needed due to return
  38.                 }//end switch
  39.             } else if (currentStep === 3) {
  40.                 checkedAllocations = $("#step3 :checkbox:checked");
  41.                 if (checkedAllocations.length === 1) {
  42.                     selectedAllocationValue = checkedAllocations.val();//do whatever you want with that
  43.                     $("[name=" + selectedAllocationValue + "-Allocation]").val(100);
  44.                     currentStep += 2;
  45.                 } else {
  46.                     currentStep += 1;
  47.                 }
  48.                 $("#step" + currentStep).slideToggle("slow");
  49.             } else {
  50.                 currentStep += 1;
  51.                 $("#step" + currentStep).slideToggle("slow");
  52.             }
  53.         });
  54.         return currentStep;
  55.     }
  56.     function showPreviousStep(currentStep) {
  57.         if (currentStep === 2) {
  58.             var chosenDonationType = $("[name=donationType]").val();
  59.             switch (chosenDonationType) {
  60.             case "oneTimeGift":
  61.                 $("#makingAOneTimeGift").slideToggle();
  62.                 break;
  63.             case "recurringDonation":
  64.                 $("#makingARecurringGift").slideToggle();
  65.                 break;
  66.             default:
  67.                 $("#makingARecurringGift").hide();
  68.                 $("#makingAOneTimeGift").hide();
  69.                 break;
  70.             }
  71.         }
  72.  
  73.         $("#step" + currentStep).slideToggle("slow", function () {
  74.             currentStep -= 1;
  75.             $("#step" + currentStep).slideToggle("slow");
  76.         });
  77.         return currentStep;
  78.     }
  79.  
  80.     function showResults(currentStep) {
  81.         $("#showResults").slideToggle("slow");
  82.     }
  83.     function validateBeforeSubmitContent() {
  84.         //if true submit form / if false do NOT submit the form
  85.         return true;
  86.     }
  87.  
  88.     function replaceDonationAmount() {
  89.         //check which they used
  90.         var donationAmount, OneTimeDonation, RecurringDonation;
  91.         donationAmount = 0;
  92.         OneTimeDonation = $("[name=oneTimeDonationValue]").val();
  93.         RecurringDonation = parseInt($("#totalRecurringDonationValue").text(), 10);
  94.  
  95.         if (OneTimeDonation !== 0) {
  96.             donationAmount = OneTimeDonation;
  97.         } else {
  98.             donationAmount = RecurringDonation;
  99.         }
  100.         $("#showTotalDonationAmount").text(donationAmount);
  101.     }
  102.  
  103.     //Replace the Recurring donation amount so they know how much their total gift amount.
  104.     function replaceRecurringDonationValue() {
  105.         //Let's get the values and store them in our variables
  106.         var perDonationValue, numberOfPayments, paymentFrequency, TotalRecurringDonationValue, howLong, howManyYears, howManyMonths, numberOfMonths;
  107.         perDonationValue = $("#recurringDonationValue").val();
  108.         numberOfPayments = $("#numberOfPayments").val();
  109.         paymentFrequency = $("#paymentFrequency").val();
  110.  
  111.         //ensure that the maximum number of months is enforced.
  112.         if (numberOfPayments > 60) {
  113.             alert("Donations can last for a maximum of 60 times.");
  114.             $("#numberOfPayments").val(60);
  115.         } else if (numberOfPayments < 0) {
  116.             alert("You cannot make a negative number of domnations.");
  117.             $("#numberOfPayments").val(0);
  118.         }
  119.  
  120.         TotalRecurringDonationValue = perDonationValue * numberOfPayments;
  121.         howLong = " over ";
  122.  
  123.         //now say over what time period. Switch will be faster than an if else.
  124.         switch (paymentFrequency) {
  125.         case "Monthly":
  126.             if (numberOfPayments === 0) {
  127.                 howLong = "";
  128.             } else if (numberOfPayments === 1) {
  129.                 howLong += numberOfPayments + " month.";
  130.             } else if (numberOfPayments <= 12) {
  131.                 howLong += numberOfPayments + " months.";
  132.             } else {
  133.                 howManyYears = Math.floor(numberOfPayments / 12);
  134.                 howManyMonths = numberOfPayments % 12;
  135.  
  136.                 howLong += howManyYears + " years, and " + howManyMonths + " months.";
  137.             }
  138.             $("#lengthOfTime").text(howLong);
  139.             break;
  140.  
  141.         case "Quarterly":
  142.             howLong = " will be made quarterly over ";
  143.             if (numberOfPayments === 0) {
  144.                 howLong = "";
  145.             } else if (numberOfPayments <= 4) {
  146.                 numberOfMonths = numberOfPayments * 3;
  147.                 howLong += numberOfMonths + " months.";
  148.             } else {
  149.                 howManyYears = Math.floor((numberOfPayments * 3) / 12);
  150.                 howManyMonths = (numberOfPayments * 3) % 12;
  151.                 howLong += howManyYears + " years, and " + howManyMonths + " months.";
  152.             }
  153.             $("#lengthOfTime").text(howLong);
  154.             break;
  155.  
  156.         case "Annually":
  157.             howLong += numberOfPayments;
  158.             if (numberOfPayments === 0) {
  159.                 howLong = "";
  160.             } else if (numberOfPayments === 1) {
  161.                 howLong += " year.";
  162.             } else {
  163.                 howLong += " years.";
  164.             }
  165.             $("#lengthOfTime").text(howLong);
  166.             break;
  167.  
  168.         default:
  169.             $("#lengthOfTime").text("");
  170.         }
  171.         $("#totalRecurringDonationValue").val(TotalRecurringDonationValue);
  172.         replaceDonationAmount();
  173.  
  174.     }
  175.     function showResepectiveAllocation() {
  176.         //scholarship functions
  177.         $("div.ifScholarshipsSelected input[type=\"checkbox\"]").click(function () {
  178.             $("#" + $(this).val().replace(/_/g, "") + "Allocation").slideToggle("fast");
  179.         });
  180.         $("div.ifAcademicsSelected input[type=\"checkbox\"]").click(function () {
  181.             $("#" + $(this).val().replace(/_/g, "") + "Allocation").slideToggle("fast");
  182.         });
  183.         $("div.ifAthleticsAreSelected input[type=\"checkbox\"]").click(function () {
  184.             $("#" + $(this).val().replace(/_/g, "") + "Allocation").slideToggle("fast");
  185.         });
  186.     }
  187.     //Let's add the ability to show and hide fields via the checkboxes
  188.     function addInteractivity() {
  189.         //to unrestricted fund
  190.         $(".showUnrestricted").css("display", "none");
  191.         $("#unrestrictedFund").click(function () {
  192.             if ($("#unrestrictedFund:checked").val() === "unrestricted") {
  193.                 $(".showUnrestricted").slideToggle("fast"); //Slide Down Effect
  194.             } else {
  195.                 $(".showUnrestricted").slideToggle("fast");  //Slide Up Effect
  196.             }
  197.         });
  198.         //to a specific scholarship
  199.         $(".ifScholarshipsSelected").css("display", "none");
  200.         $(".ScholashipSelection").click(function () {
  201.             if ($("input[name=ScholashipSelection]:checked").val() === "to_Scholarships") {
  202.                 $(".ifScholarshipsSelected").slideToggle("fast"); //Slide Down Effect
  203.             } else {
  204.                 $(".ifScholarshipsSelected").slideToggle("fast");  //Slide Up Effect
  205.             }
  206.         });
  207.     //to a academics
  208.         $(".ifAcademicsSelected").css("display", "none");
  209.         $(".AcademicSelection").click(function () {
  210.             if ($("input[name=toAcademics]:checked").val() === "to_Academics") {
  211.                 $(".ifAcademicsSelected").slideToggle("fast"); //Slide Down Effect
  212.             } else {
  213.                 $(".ifAcademicsSelected").slideToggle("fast");  //Slide Up Effect
  214.             }
  215.         });
  216.      //Would you like to donate to athletic funds?
  217.         $(".ifAthleticsAreSelected").css("display", "none");
  218.         $(".enableAthletics").click(function () {
  219.             if ($('input[name=toAthletics]:checked').val() === "to_athletics") {
  220.                 $(".ifAthleticsAreSelected").slideDown("fast"); //Slide Down Effect
  221.             } else {
  222.                 $(".ifAthleticsAreSelected").slideUp("fast");  //Slide Up Effect
  223.             }
  224.         });
  225.     }
  226.  
  227.     function validateCurrentStep(currentStep) {
  228.         var chosenDonationType, validateTotalAllocationAmount = 0, validateOneTimeDonationAmount, fields, checkAcademicsFields, checkAthleticsFields, checkScholarshipFields, validateRecurringDonationAmount, validateNumberOfPayments, validatePaymentFrequency, validateTotalRecurringDonationAmount;
  229.         chosenDonationType = $("[name=donationType]").val();
  230.         switch (currentStep) {
  231.             //This checks to make sure that they selected a donation type.
  232.         case 1:
  233.             return true;
  234.         //break; Can't break due to return
  235.  
  236.         //This verifies donation type and checks to make sure they entered a donation amount
  237.         case 2:
  238.             switch (chosenDonationType) {
  239.             case "oneTimeGift":
  240.                 validateOneTimeDonationAmount = $("[name=oneTimeDonationValue]").val();
  241.                 if (validateOneTimeDonationAmount < 5) {
  242.                     $("#makingAOneTimeGift").addClass("error");
  243.                     $("#oneTimeDonationValidateError").text("Sorry, we have a $5 donation minimum.");
  244.                     return false;
  245.                 }
  246.  
  247.                 if ($("#makingAOneTimeGift").hasClass("error")) {
  248.                     $("#oneTimeDonationValidateError").text("");
  249.                     $("#makingAOneTimeGift").removeClass("error");
  250.                 }
  251.                 return true;
  252.  
  253.             case "recurringDonation":
  254.                 validateRecurringDonationAmount = $("[name=recurringDonationValue]").val();
  255.                 validateNumberOfPayments = $("[name=numberOfPayments]").val();
  256.                 validatePaymentFrequency = $("[name=paymentFrequency]").val();
  257.                 validateTotalRecurringDonationAmount = validateRecurringDonationAmount * validateNumberOfPayments;
  258.                 //first check the total amount, it should be at least $5.00
  259.                 if (validateTotalRecurringDonationAmount < 5) {
  260.                     $("#makingARecurringGift").addClass("error");
  261.                     $("#recurringDonationAmountValidateError").text("Sorry, your total donation was less than $5. We have a $5 donation minimum.");
  262.                     return false;
  263.                 }
  264.                 if (validateNumberOfPayments === 0) {
  265.                     $("#makingARecurringGift").addClass("error");
  266.                     $("#recurringDonationAmountValidateError").text("Sorry, you must choose how many months you would like to make this donation for.");
  267.                     return false;
  268.                 }
  269.                 if ($("#makingARecurringGift").hasClass("error")) {
  270.                     $("#oneTimeDonationValidateError").text("");
  271.                     $("#makingARecurringGift").removeClass("error");
  272.                 }
  273.                 return true;
  274.             }//end chosenDonationType switch
  275.             break;
  276.  
  277.             //This verifies that they chose a fund to donate to.
  278.         case 3:
  279.  
  280.             fields = $("#step3 input:checkbox:checked");
  281.             //check that they chose any fund at all
  282.             if (fields.length === 0) {
  283.                 $("#step3").addClass("error");
  284.                 $("#checkboxError").text("Please choose a fund to donate to.");
  285.                 return false;
  286.             }
  287.  
  288.             //Check Scholarships
  289.             if ($("[name=ScholashipSelection]").is(":checked")) {
  290.                 checkScholarshipFields = $(".ifScholarshipsSelected input:checkbox:checked");
  291.                 if (checkScholarshipFields.length === 0) {
  292.                     $("#step3").addClass("error");
  293.                     $("#checkboxError").text("Please choose which scholarship you would like to donate to.");
  294.                     return false;
  295.                 }
  296.                 if ($("#step3").hasClass("error")) {
  297.                     $("#step3").removeClass("error");
  298.                     $("#checkboxError").text("");
  299.                 }
  300.             }
  301.  
  302.             //Check Acadmics
  303.             if ($("[name=toAcademics]").is(":checked")) {
  304.                 checkAcademicsFields = $(".ifAcademicsSelected input:checkbox:checked");
  305.                 if (checkAcademicsFields.length === 0) {
  306.                     $("#step3").addClass("error");
  307.                     $("#checkboxError").text("Please choose which academic fund you would like to donate to.");
  308.                     return false;
  309.                 }
  310.                 if ($("#step3").hasClass("error")) {
  311.                     $("#step3").removeClass("error");
  312.                     $("#checkboxError").text("");
  313.                 }
  314.             }
  315.  
  316.             //check Athletics
  317.             if ($("[name=toAthletics]").is(":checked")) {
  318.                 checkAthleticsFields = $(".ifAthleticsAreSelected input:checkbox:checked");
  319.                 if (checkAthleticsFields.length === 0) {
  320.                     $("#step3").addClass("error");
  321.                     $("#checkboxError").text("Please choose which athletic fund you would like to donate to.");
  322.                     return false;
  323.                 }
  324.                 if ($("#step3").hasClass("error")) {
  325.                     $("#step3").removeClass("error");
  326.                     $("#checkboxError").text("");
  327.                 }
  328.             }
  329.  
  330.             //if we didn't have to stop anywhere above this, let the step continue
  331.             if ($("#step2ValidateError").hasClass("error")) {
  332.                 $("#step2ValidateError").removeClass("error");
  333.                 $("#oneTimeDonationCheckboxError").text("");
  334.             }
  335.             return true;
  336.  
  337.  
  338.         case 4:
  339.             $("#step4 :input[type=\"number\"]").each(function () {
  340.                 validateTotalAllocationAmount += parseInt($(this).val(), 10);
  341.             });
  342.  
  343.             if (validateTotalAllocationAmount < 100) {
  344.                 alert("Sorry, your allocation amount adds up to under 100%. Please try again.");
  345.                 return false;
  346.             }
  347.  
  348.             if (validateTotalAllocationAmount > 100) {
  349.                 alert("Sorry, your allocation amount adds up to over 100%. Please try again.");
  350.                 return false;
  351.             }
  352.  
  353.             return true;
  354.  
  355.         case 5:
  356.             //Check user information i.e. Name / State / Etc.
  357.             return true;
  358.  
  359.         default:
  360.             return false;
  361.         } //end currentStep switch
  362.     }
  363.  
  364.     currentStep = 1;
  365.     hideSteps();
  366.     addInteractivity();
  367.     showResepectiveAllocation();
  368.     $("#recurringDonationValue, #numberOfPayments, #paymentFrequency").change(function () {
  369.         replaceRecurringDonationValue();
  370.     });
  371.  
  372.     $("#oneTimeGift").click(function () {
  373.         $("[name=donationType]").val("oneTimeGift");
  374.     });
  375.     $("#recurringGift").click(function () {
  376.         $("[name=donationType]").val("recurringDonation");
  377.     });
  378.     $("#replaceDonationAmountNow").click(function () {
  379.         replaceDonationAmount();
  380.         var chosenDonationType = $("[name=donationType]").val();
  381.         switch (chosenDonationType) {
  382.         case "oneTimeGift":
  383.             $("#typeOfGift").text("one-time");
  384.             break;
  385.         case "recurringDonation":
  386.             $("#typeOfGift").text("recurring");
  387.             break;
  388.         default:
  389.             $("#typeOfGift").text("");
  390.             break;
  391.         }
  392.     });
  393.  
  394.     //change steps
  395.     $(".nextStep").click(function () {
  396.         if (validateCurrentStep(currentStep)) {
  397.             currentStep = showNextStep(currentStep);
  398.         } else {
  399.             return false;
  400.         }
  401.     });
  402.     $(".previousStep").click(function () {
  403.         currentStep = showPreviousStep(currentStep);
  404.     });
  405.  
  406.     $("[name=submit_form]").click(function (e) {
  407.         var options = {
  408.             target: "#step6",
  409.             clearForm: true
  410.         };
  411.         if (validateBeforeSubmitContent(e)) {
  412.             $("#DonationForm").ajaxForm(options);
  413.         } else {
  414.             e.preventDefault();
  415.             return false;
  416.         }
  417.     });
  418. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement