var numbers="0123456789";
var txtl1; 
var txtl2; 
var txtl3;
var txtl4;
var Psale;
var admintype;
var Dsale,txtshowL1,txttotalA;
function calcIt()
{
Psale = document.calc.txtps;
Dsale = document.calc.txtdownps;
var checkPs = check(Psale);
var checkDs = check(Dsale);
for(ln=0; ln < document.calc.txtAdminType.length; ln++)
	{
     if(document.calc.txtAdminType[ln].checked)
		{
        admintype = document.calc.txtAdminType[ln].value;
		}
	}

 if(checkPs == true && checkDs == true)
 {
  document.calc.txtl1.value = Psale.value;
  document.calc.txtl2.value = Psale.value * Dsale.value;
  document.calc.txtl3.value = Psale.value * Dsale.value * Dsale.value;
  document.calc.txtl4.value = Psale.value * Dsale.value * Dsale.value* Dsale.value;
  txtl1 = parseInt(document.calc.txtl1.value)
  txtl2 = parseInt(document.calc.txtl2.value)
  txtl3 = parseInt(document.calc.txtl3.value)
  txtl4 = parseInt(document.calc.txtl4.value)
  document.calc.txtTotal4l.value = txtl1 + txtl2 + txtl3 + txtl4; 
  document.calc.txttotalA.value = document.calc.txtTotal4l.value;
  txttotalA = parseInt(document.calc.txttotalA.value);
  calcShow();
 }
}

function calcShow()
{
 //Calculate Level 1
 if (txtl1 > 2)
 {
  document.calc.txtshowL1.value = 2;
 }
 else
 {
  document.calc.txtshowL1.value = txtl1;
 }
 txtshowL1 = parseInt(document.calc.txtshowL1.value);
if(Psale.value > 2 && Dsale.value < 1)
{
 CalcSpillOver();
txtshowL2 = parseInt(document.calc.txtshowL2.value);
txtshowL3 = parseInt(document.calc.txtshowL3.value);
txtshowL4 = parseInt(document.calc.txtshowL4.value);
}
else
{
txtshowL2 = calcLevel(document.calc.txtshowL2,txtl2,4);
txtshowL3 = calcLevel(document.calc.txtshowL3,txtl3,8);
txtshowL4 = calcLevel(document.calc.txtshowL4,txtl4,16);
}

document.calc.txtTotalshow4l.value = txtshowL1 + txtshowL2 + txtshowL3 + txtshowL4;
txtTotalshow4l = parseInt(document.calc.txtTotalshow4l.value);
document.calc.txtshowspill.value = txttotalA - txtTotalshow4l;
txtshowspill = parseInt(document.calc.txtshowspill.value);
document.calc.txttotalshow.value = txtshowspill + txtTotalshow4l;
calcIncome();
}

function CalcSpillOver()
{
 var xStop = false
 var xMax = 2;
 var left= Psale.value ;
 for (y = 2; y < 5; y++)
 {
   if(left > xMax)
   {
    left = left - xMax;
   }
   else
   {
    left = 0;
   }
   xMax = xMax * 2;
   //alert(xMax);
   //alert(left);
   xObjshowL = eval("document.calc.txtshowL" + y);
   if(left > xMax)
   {
    xObjshowL.value = xMax;
   }
   else
   {
    xStop = true;
    xObjshowL.value = left;
   }
   //xShow = eval("document.calc.txtshowL" + y);
 }
 if(left > 0)
 {

 }

}

function calcIncome()
{
document.calc.txtsales.value = document.calc.txttotalshow.value;
document.calc.txtpersales.value = parseInt(document.calc.txtsales.value)/2;
document.calc.txtrounddown.value = Math.abs(parseInt(document.calc.txtpersales.value));
document.calc.txt4division.value = parseInt(document.calc.txtrounddown.value)/4;
document.calc.txtrounddown2.value = Math.abs(parseInt(document.calc.txt4division.value));
document.calc.txt4used.value = parseInt(document.calc.txtrounddown2.value)*4;
document.calc.txtLover.value = parseInt(document.calc.txtrounddown.value) - parseInt(document.calc.txt4used.value)
document.calc.txtCycles.value = parseInt(document.calc.txt4used.value)/6;
document.calc.txtCcycles.value = Math.abs(parseInt(document.calc.txtCycles.value));
document.calc.txtstepComc.value = parseInt(document.calc.txtCcycles.value)*6;
document.calc.txtsteprem.value = document.calc.txtLover.value;

 var xTotal =0;
for(x=1; x<=5 ;x++)
{
 xObj = eval("document.calc.txtstep" + x);
 var Commision;
 if(parseInt(document.calc.txtsteprem.value) >= x)
 {
 xObj.value = 1;
 }
 else
 {
 xObj.value = 0;
 }
 //if(x == 1)
 //{
 // Commision = 165
 //}
 //else
 //{
 if(admintype == "special")
	{
    Commision = 165
	}
    else
	{
    Commision = 82
	}

 //}
 xTotal = xTotal + (parseInt(xObj.value) * Commision);
}

if(txttotalA < 8)
{
document.calc.txtMIncome.value = formatCurrency(0);
document.calc.txtYIncome2.value = formatCurrency(0);
}
else
{
 if(txttotalA == 8)
 {
  document.calc.txtMIncome.value = formatCurrency(250);
  document.calc.txtYIncome2.value = formatCurrency(3000);
 }
 else
 {
  if(admintype == "special")
	{
    Anum = 990
	}
    else
	{
    Anum = 492
	}
document.calc.txtCyclecom.value = parseInt(document.calc.txtCcycles.value) * Anum;
document.calc.txtstepCom.value = xTotal;
document.calc.txttotalCom.value = parseInt(document.calc.txtCyclecom.value) + xTotal;
document.calc.txtMIncome.value = formatCurrency(document.calc.txttotalCom.value);
txtYIncome = parseInt(document.calc.txttotalCom.value)*12;
txtInterestPercent = 0.06;
document.calc.txtYIncome2.value = formatCurrency(txtYIncome);
document.calc.txtInterest.value = document.calc.txtYIncome2.value;
document.calc.txtPrinciple.value = formatCurrency(txtYIncome / txtInterestPercent);
document.calc.txtInvest.value = document.calc.txtPrinciple.value;
 }
}

}

function formatCurrency(num)
{
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}


function calcLevel(txtshowL,txtl,max)
{
 if (txtshowL1 >= 2)
 {
  if (txtl4 < max)
  {
  txtshowL.value = txtl;
  }
  else
  {
   txtshowL.value = max;
  }
 }
 else
 {

  if(txtl >= 2)
  {
    txtshowL.value = max/2;
  }
  else
  {
   txtshowL.value = txtl;
  }
   
 }

 return(parseInt(txtshowL.value));
}


function check(val)
{
if(IsEmpty(val)) 
   { 
      alert('You have not entered an account number') 
      val.value = 0; 
      val.focus();
      return false; 
   } 
 
 
   if (!IsNumeric(val.value)) 
   { 
      alert('Please enter only numbers or decimal points in the account field') 
      val.value = 0; 
      val.focus();
      return false; 
      } 
 
return true;
}


function IsNumeric(x) {

    if(x.length>1) {
      x=Math.abs(x)+"";
      for(j=0;j<x.length;j++) {
        number=IsNumeric(x.substring(j,j+1));
        if(!number) return number;
      }
      return number;
    }
    else {
      if(numbers.indexOf(x)>=0) return true;
      return false;
    }
  }


function IsEmpty(y) {

if(y == null || y == "")
 {return true}
if(y.value.length < 1)
 {return true}
else
 {return false}
}

function disableAll()
{
document.calc.txtl1.readOnly = true
document.calc.txtl2.readOnly = true
document.calc.txtl3.readOnly = true
document.calc.txtl4.readOnly = true
document.calc.txtTotal4l.readOnly = true
document.calc.txttotalA.readOnly = true
document.calc.txtshowL1.readOnly = true
document.calc.txtshowL2.readOnly = true
document.calc.txtshowL3.readOnly = true
document.calc.txtshowL4.readOnly = true
document.calc.txtTotalshow4l.readOnly = true
document.calc.txtshowspill.readOnly = true
document.calc.txttotalshow.readOnly = true
document.calc.txtMIncome.readOnly = true
document.calc.txtYIncome2.readOnly = true
document.calc.txtInvest.readOnly = true
document.calc.txtpersales.readOnly = true
document.calc.txtrounddown.readOnly = true
document.calc.txt4division.readOnly = true
document.calc.txtrounddown2.readOnly = true
document.calc.txt4used.readOnly = true
document.calc.txtLover.readOnly = true
document.calc.txtCycles.readOnly = true
document.calc.txtCcycles.readOnly = true
document.calc.txtstepComc.readOnly = true
document.calc.txtsteprem.readOnly = true
document.calc.txtsales.readOnly = true
document.calc.txtstep1.readOnly = true
document.calc.txtstep2.readOnly = true
document.calc.txtstep3.readOnly = true
document.calc.txtstep4.readOnly = true
document.calc.txtstep5.readOnly = true
document.calc.txtCyclecom.readOnly = true
document.calc.txtstepCom.readOnly = true
document.calc.txttotalCom.readOnly = true
document.calc.txtInterest.readOnly = true
document.calc.txtPrinciple.readOnly = true

}
