// Prize Calculator
// Author: Brian O'Keefe
// Email: okeefeb@canisius.edu
// This code may be used or modified freely as long as the appropriate
// credit is given.
// This is the second release, version 1.0 was unknowingly incompatible
// with anything but Netscape 4

// Thank you Netscape for the next two lines
var userAgent = navigator.appName + " " + navigator.appVersion;
var agentInfo = userAgent.substring(0, 12);

function formClear(form)
{
form.a1.value=""; form.a2.value=""; form.a3.value=""; form.a4.value="";
form.a5.value=""; form.a6.value=""; form.a7.value=""; form.a8.value="";
form.a9.value="";
}

function computeForm(form)
{
 form.a9.value= form.a1.value * 1 + form.a2.value * 1 + form.a3.value *
1 + form.a4.value * 1 + form.a5.value * 1 + form.a6.value * 1 +
form.a7.value * 1 + form.a8.value * 1;

}

function validateData(theNum)
{
    var str=theNum;
    if (agentInfo == "Netscape 4.0")
 {
     var correctedNum = 0;
     var decimalPlace = 0;
  var addOn = "0";
  var isDecimal = false;
     for (var i=0; i < str.length; i++)
     {
         var ch=str.substring(i, i+1);
         if (((ch >= "0") && (ch <= "9")) && (!isDecimal))
         {
          correctedNum *= 10;
       correctedNum += ch * 1;
         }
         else if (((ch >= "0") && (ch <= "9")) && (isDecimal))
         {
    addOn += ch;
    ++decimalPlace;
         }
         else if (ch == ".")
          isDecimal = true;
         else if ((ch != ",") && (ch != "$"))
          alert("The invalid character " + ch + " was detected "+
        "and ignored.");
     }
  if (decimalPlace > 0)
   correctedNum += addOn / pow(decimalPlace);
     return correctedNum;
 }
 else
 {
     for (var z=0; z < str.length; z++)
     {
  var ch=str.substring(z, z+1);
  if (((ch < "0") || (ch > "9")) && (ch != "."))
   return false;
     }
     return true;
        }

}

function calc(input)
{
 if (agentInfo == "Netscape 4.0")
 {
     input.value = validateData(input.value);
     computeForm(input.form);
 }
 else
 {
     if (validateData(input.value))
      computeForm(input.form);
     else
     {
       alert("Please do not use any characters besides numbers on"+
       " this line. Your entry will be erased.");
            input.value = 0;
     }
 }

}

function pow(exp)
{
 var returnTotal = 10;
 for (var j = 1; j < exp; j++)
     returnTotal *= 10;
 return returnTotal;
}
