// Global Vars
var IsDonation=false;
var IsCredit=false;
var IsNetcost=false;

// Check for decimal 
function IsDec(no) 
{
    return FoundChar(no, ".");
}
// Check for text
function IsNumber(textValue) 
{
 var IsDec = false;
 var k = 0;
     var Find = "";
     for (k = 0; k < textValue.length; k ++) 
 {
        Find = textValue.substring(k, k + 1 );
       if ((Find < "0") || (Find > "9")) 
  {
          if ((Find == ".") && (!IsDec)) 
   {
             IsDec = true;
             continue;
          } 
   else 
   {
             if ((Find == "-") && (k == 0)) 
    {
     return false;   
             }
          }
          return false;
       }
     } 
     return true;
}
 
function FoundChar(text, chr) 
{
     var j;
    for (j = 0; j < text.length; j++)
 { 
       if (text.substring(j, j + 1) == chr)
  {
          return true;
       }
     }
 return false;
}
// Calculate temp money vars for final calc 
function calcMoney(temp)
{
 var Final;
 var Dollars; 
 var tmpC;
 var chrDol;
 var Cents;
 Dollars=Math.floor(temp);
 chrDol=""+ Math.floor(temp*100);
 tmpC=""+Math.round(temp*100);
 Cents=tmpC.substring(tmpC.length-2, tmpC.length);
 if ((chrDol.substring(chrDol.length-2, chrDol.length)=="99") && (Cents=="00"))
 {
  Dollars = Math.floor(Dollars+1);
 }
 temp=Dollars+"."+Cents;
 Final=temp;
 return Final;
}
// Check to see if string inputted to donation field 
function donationChanged(form, donation) 
{
 var temp;
 var crTmp;
 if (donation.value.length==0)
 {
  alert("Negative numbers are invalid. You must enter a positive value.");
       donation.value = form.donation.defaultValue;
 }  
     if (!IsNumber(donation.value)) 
 {
  alert("Negative numbers are invalid. You must enter a positive value.");
       donation.value = form.donation.defaultValue;
      } 
 else 
 {
  donation.value=calcMoney(donation.value);
  form.donation.defaultValue=donation.value;
  if (donation.value > 200)
  {
//   crTmp=0.29*(donation.value-200);
//   temp=crTmp*1.41 + 0.17*200*1.41;
   temp=44.1+(donation.value-200)*0.437;
   form.credit.value=calcMoney(temp);
   form.credit.defaultValue = form.credit.value;
   temp=donation.value - form.credit.value;
   form.netcost.value = calcMoney(temp);
   form.netcost.defaultValue = form.netcost.value;
  }
  else
  {
   temp=donation.value * 0.2205;
   form.credit.value = calcMoney(temp);
   form.credit.defaultValue = form.credit.value; 
   temp=donation.value - form.credit.value
   form.netcost.value = calcMoney(temp);
   form.netcost.defaultValue = form.netcost.value;
  }
      }
     IsDonation=false;
}
// Check to see if string inputted to credit field  
function creditChanged(form, credit) 
{
 var temp;
 if (credit.value.length==0)
 {
  alert("Negative numbers are invalid. You must enter a positive value.");
       credit.value = form.credit.defaultValue;
 }  
     if (!IsNumber(credit.value)) 
 {
  alert("Negative numbers are invalid. You must enter a positive value.");
       credit.value = form.credit.defaultValue;
      } 
 else 
 {
  credit.value=calcMoney(credit.value);
  form.credit.defaultValue=credit.value;
  if (credit.value > 53.04)
  {
   temp=credit.value/0.4524 + 37.44/0.4524
   form.donation.value = calcMoney(temp);
   form.donation.defaultValue = form.donation.value;
   temp=form.donation.value - credit.value;
   form.netcost.value = calcMoney(temp);
   form.netcost.defaultValue = form.netcost.value;
  }
  else
  {
   temp=credit.value/0.2652;
   form.donation.value = calcMoney(temp);
   form.donation.defaultValue = form.donation.value;
   temp=form.donation.value - credit.value; 
   form.netcost.value=calcMoney(temp);
   form.netcost.defaultValue = form.netcost.value;
  }
      }
     IsCredit=false;
}
// Check to see if string inputted to netcost field  
function netcostChanged(form, netcost) 
{
 var temp;
 if (form.netcost.value.length==0)
 {
  alert("Negative numbers are invalid. You must enter a positive value.");
       netcost.value = form.netcost.defaultValue;
 }  
     if (!IsNumber(netcost.value)) 
 {
  alert("Negative numbers are invalid. You must enter a positive value.");
       netcost.value = form.netcost.defaultValue;
      } 
 else 
 {
  netcost.value=calcMoney(netcost.value);
  form.netcost.defaultValue=netcost.value;
  if (netcost.value > 253.04)
  {
   temp = netcost.value/0.5476 - 37.44/0.5476;
   form.donation.value = calcMoney(temp);
   form.donation.defaultValue = form.donation.value;
   temp=form.donation.value - netcost.value;
   form.credit.value = calcMoney(temp);
   form.credit.defaultValue = form.credit.value;
  }
  else
  {
   temp=netcost.value/0.7348;
   form.donation.value = calcMoney(temp);
   form.donation.defaultValue = form.donation.value;
   temp=form.donation.value - netcost.value;
   form.credit.value = calcMoney(temp);
   form.credit.defaultValue = form.credit.value;
  }
      }
 IsNetcost=false;
}
// Check to see which field string was inputted into  
function CheckValue(form)
{
 if (IsDonation==true)
 {
  donationChanged(form, form.donation);
 }
 else if (IsCredit==true)
 {
  creditChanged(form, form.credit);
 }
 else if (IsNetcost==true)
 {
  netcostChanged(form, form.netcost);
 }
}
// Set which fields calc should change   
function SetChange(chValue)
{
 if (chValue==1)
 {
  IsDonation=true;
 }
 else if (chValue==2)
 {
  IsCredit=true;
 }
 else if (chValue==3)
 {
  IsNetcost=true;
 }
}

