Advertisement
Rapptz

Untitled

Nov 4th, 2012
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function() {
  2.   $("#cost").addClass("empty");
  3.  
  4.   $("#embed").live("click", function(){
  5.     $("#calc h1").html($("#embed-code h1").html());
  6.     $("#calc #cost").html($("#embed-code #code").html());
  7.     $("#calc h2").html($("#embed-code h2").html());
  8.     $("#calc").addClass("embed");
  9.   });
  10.  
  11.   $("#show-calc").live("click", function(){
  12.     $("#calc h1").html($("#calc-result h1").html());
  13.     $("#calc #cost").html($("#calc-result .cost").html());
  14.     $("#calc h2").html($("#calc-result h2").html());
  15.     $("#calc").removeClass("embed");
  16.   });
  17.  
  18.   $("textarea").live("click", function() {
  19.     $(this).select();
  20.   });
  21.  
  22.   $("#age").click(function() {
  23.     if( $(this).val() == "Your Age" ) $(this).val("");
  24.     });
  25.  
  26.   $("#age").blur(function() {
  27.     if( $(this).val() === "" ) $(this).val("Your Age");
  28.     });
  29.  
  30.     $("input, select").change(function() {
  31.         if( valid_fields() ) {
  32.       $("#cost").removeClass("empty");
  33.       $("#cost").addClass("filled");
  34.      
  35.       $("#cost").fadeOut("slow", function() {
  36.         $("#cost").html( update_cost() );
  37.         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";
  38.         $("#cost").fadeIn();
  39.       });
  40.         } else {
  41.       $("#cost").removeClass("filled");
  42.       $("#cost").addClass("empty");
  43.       $("#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>");
  44.         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";
  45.         }
  46.    
  47.     $("#calc-result .cost").html($("#cost").html());
  48.    
  49.     });
  50.    
  51.   data = {
  52.     "pill"          : { "insured" : 215.00,  "uninsured" : 1210.00, "interval"  : 1   },
  53.     "iuds"          : { "insured" : 500.00,  "uninsured" : 1000.00, "interval"  : 7.5 },
  54.     "implanon"      : { "insured" : 500.00,  "uninsured" : 1100.00, "interval"  : 3   },
  55.         "injections"    : { "insured" : 195.00,  "uninsured" : 590.00,  "interval"  : 1   },
  56.         "patch"         : { "insured" : 215.00,  "uninsured" : 1210.00, "interval"  : 1   },
  57.         "ring"          : { "insured" : 215.00,  "uninsured" : 1210.00, "interval"  : 1   },
  58.         "sterilization" : { "insured" : 1500.00, "uninsured" : 6000.00, "interval"  : 61  }
  59.   };
  60.    
  61.     function update_cost() {
  62.  
  63.       // User input
  64.       age       = parseInt($("#age").val());
  65.       method    = $("#method").val();
  66.       insurance = $("#insurance").val();
  67.            
  68.             // Calculations        
  69.       base_cost  = data[method][insurance];
  70.             total_cost = base_cost;
  71.            
  72.       for (i = age; i < 59; i+= data[method]["interval"]) {            
  73.                 inflation = Math.pow(1.03, data[method]["interval"]);
  74.                 base_cost *= inflation;
  75.                 total_cost += base_cost;
  76.       }
  77.  
  78.             return "$"+commify(Math.round(total_cost));
  79.     }
  80.  
  81.   function valid_fields() {
  82.     if( ( parseInt($("#age").val()) < 18 ) || ( parseInt($("#age").val()) > 60 ) ) {
  83.       $("#error").text("Please select an age between 18 and 60.");
  84.       return false;
  85.     }
  86.     if( $("#method").val() === "" ) return false;
  87.     if( $("#insurance").val() === "" ) return false;
  88.     if( ( $("#age").val() === "" ) ) return false;
  89.     $("#error").text("");
  90.     return true;
  91.   }
  92.    
  93.   function commify(num) {
  94.     num += '';
  95.     x = num.split('.');
  96.     x1 = x[0];
  97.     x2 = x.length > 1 ? '.' + x[1] : '';
  98.     var rgx = /(\d+)(\d{3})/;
  99.    
  100.     while (rgx.test(x1)) {
  101.       x1 = x1.replace(rgx, '$1' + ',' + '$2');
  102.     }
  103.    
  104.     return x1 + x2;
  105.   }
  106.  
  107.   $("#show-calc").click();
  108. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement