Guest User

Untitled

a guest
Jan 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6.  
  7.  
  8. var totalcost = 0;
  9. var numanimals = 0;
  10.  
  11. function checkAnimals()
  12. {
  13.     var animalselector = document.getElementById("animals");
  14.  //   <option value = "1">Tiger $2000</option>
  15.  //   <option value = "2">Elephant $1900</option>
  16.  //   <option value = "3">Chimpanzee $1800</option>
  17.  //   <option value = "4">Warthog $1050</option>
  18.  //   <option value = "5">Lemur $180</option>
  19.     numanimals = 0;
  20.     totalcost = 0;
  21.    
  22.     if (animalselector[0].selected)
  23.         {
  24.             numanimals++;
  25.             totalcost+=2000;
  26.         }
  27.     if (animalselector[1].selected)
  28.         {
  29.             numanimals++;
  30.             totalcost+=1900;
  31.         }
  32.     if (animalselector[2].selected)
  33.         {
  34.             numanimals++;
  35.             totalcost+=1800;
  36.         }
  37.     if (animalselector[3].selected)
  38.         {
  39.             numanimals++;
  40.             totalcost+=1050;
  41.         }
  42.     if (animalselector[4].selected)
  43.         {
  44.             numanimals++;
  45.             totalcost+=190;
  46.         }
  47.     return numanimals>0;
  48. }
  49. function checkCard()
  50. {
  51.     var cardinput = document.getElementById("cardnum");
  52.     var cardnumber = cardinput.value;
  53.     var numdigits = 0;
  54.     var len = cardnumber.length;
  55.    
  56.     for(var i = 0; i<len; i++)
  57.     {
  58.         var ch = cardnumber.charAt(i);
  59.         if ((ch >='0') && (ch <= '9'))
  60.     {
  61.         numdigits++;
  62.         }
  63.     }
  64.    
  65.     return numdigits == 16;
  66. }
  67.  
  68. function checkCardName()
  69. {
  70.     var cardnumber = document.getElementById("cardnum");
  71.     var cardinput = document.getElementById("cardname");
  72.     if (cardinput[0].selected)
  73.         {
  74.             if (cardnumber.value.charAt(0) == '4')
  75.                 {
  76.                     return true;
  77.                 }
  78.                 else
  79.                     return false;
  80.         }
  81.         if (cardinput[1].selected)
  82.         {
  83.             if (cardnumber.value.charAt(0) == '5')
  84.                 {
  85.                     return true;
  86.                 }
  87.                 else
  88.                     return false;
  89.         }
  90.         else
  91.             return false;
  92. }
  93.  
  94.  
  95. function confirmSubmit()
  96. {
  97.    if (!checkAnimals())
  98.        {
  99.            alert("You didn't select an animal to sponsor");
  100.            return false;
  101.        }
  102.    else if (!checkCard())
  103.        {
  104.            alert("Your credit card details are invalid");
  105.            return false;
  106.        }
  107.    else if (!checkCardName())
  108.        {
  109.            alert("Your credit card details are invalid (try 4 or 5 as the first digit)");
  110.            return false;
  111.        }
  112. //  alert("nurrrrrrrrr");
  113.    else
  114.     {
  115.        msg = "Thankyou for offering to sponsor ";
  116.        if (numanimals == 1) msg = msg + "one of our zoo's residents.";
  117.        else msg = msg + numanimals + " of our zoo's residents.";
  118.        msg = msg + "The cost of sponsorship is $" + totalcost;
  119.        msg = msg + ". Confirm order.";
  120.        return confirm(msg);
  121.     }
  122. }
Add Comment
Please, Sign In to add comment