Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function() {
- $("#cost").addClass("empty");
- $("#embed").live("click", function(){
- $("#calc h1").html($("#embed-code h1").html());
- $("#calc #cost").html($("#embed-code #code").html());
- $("#calc h2").html($("#embed-code h2").html());
- $("#calc").addClass("embed");
- });
- $("#show-calc").live("click", function(){
- $("#calc h1").html($("#calc-result h1").html());
- $("#calc #cost").html($("#calc-result .cost").html());
- $("#calc h2").html($("#calc-result h2").html());
- $("#calc").removeClass("embed");
- });
- $("textarea").live("click", function() {
- $(this).select();
- });
- $("#age").click(function() {
- if( $(this).val() == "Your Age" ) $(this).val("");
- });
- $("#age").blur(function() {
- if( $(this).val() === "" ) $(this).val("Your Age");
- });
- $("input, select").change(function() {
- if( valid_fields() ) {
- $("#cost").removeClass("empty");
- $("#cost").addClass("filled");
- $("#cost").fadeOut("slow", function() {
- $("#cost").html( update_cost() );
- addthis_share.templates.twitter = "If Romney wins, birth control will cost me "+$("#cost").text()+"! Find out how much your costs would go up. {{url}} #election2012 #VoteObama";
- $("#cost").fadeIn();
- });
- } else {
- $("#cost").removeClass("filled");
- $("#cost").addClass("empty");
- $("#cost").html("<div class='results cf'><div class='dollar'>$</div><div class='q'>?</div><div class='q'>?</div><div class='q'>?</div><div class='q'>?</div><div class='q'>?</div></div>");
- addthis_share.templates.twitter = "If Romney wins, birth control will cost me more! Find out how much your costs would go up. {{url}} #election2012 #VoteObama";
- }
- $("#calc-result .cost").html($("#cost").html());
- });
- data = {
- "pill" : { "insured" : 215.00, "uninsured" : 1210.00, "interval" : 1 },
- "iuds" : { "insured" : 500.00, "uninsured" : 1000.00, "interval" : 7.5 },
- "implanon" : { "insured" : 500.00, "uninsured" : 1100.00, "interval" : 3 },
- "injections" : { "insured" : 195.00, "uninsured" : 590.00, "interval" : 1 },
- "patch" : { "insured" : 215.00, "uninsured" : 1210.00, "interval" : 1 },
- "ring" : { "insured" : 215.00, "uninsured" : 1210.00, "interval" : 1 },
- "sterilization" : { "insured" : 1500.00, "uninsured" : 6000.00, "interval" : 61 }
- };
- function update_cost() {
- // User input
- age = parseInt($("#age").val());
- method = $("#method").val();
- insurance = $("#insurance").val();
- // Calculations
- base_cost = data[method][insurance];
- total_cost = base_cost;
- for (i = age; i < 59; i+= data[method]["interval"]) {
- inflation = Math.pow(1.03, data[method]["interval"]);
- base_cost *= inflation;
- total_cost += base_cost;
- }
- return "$"+commify(Math.round(total_cost));
- }
- function valid_fields() {
- if( ( parseInt($("#age").val()) < 18 ) || ( parseInt($("#age").val()) > 60 ) ) {
- $("#error").text("Please select an age between 18 and 60.");
- return false;
- }
- if( $("#method").val() === "" ) return false;
- if( $("#insurance").val() === "" ) return false;
- if( ( $("#age").val() === "" ) ) return false;
- $("#error").text("");
- return true;
- }
- function commify(num) {
- num += '';
- x = num.split('.');
- x1 = x[0];
- x2 = x.length > 1 ? '.' + x[1] : '';
- var rgx = /(\d+)(\d{3})/;
- while (rgx.test(x1)) {
- x1 = x1.replace(rgx, '$1' + ',' + '$2');
- }
- return x1 + x2;
- }
- $("#show-calc").click();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement