var thisForm;
var SubForm;
var  Error = 0;
var IamBeingSolved;
function showME(){
    everything.style.display=""
    //loading.style.display="none"
}
function ReAll(){
ResetSolv();
ckMe();
}
function ResetSolv(){
     SOV.style.display = thisForm.Pay[0].checked?"":"none"
     thisForm.SolveAmt.value = ""
    }
function solMe(){

    if (Trim(thisForm.SolveAmt.value).length == 0){
        thisForm.SolveAmt.select();
        return
     }
SubForm.selSolveAmt.value =thisForm.SolveAmt.value
    SolEr.innerText = "{Invalid Amount} or\nnot all required fields met." // set error mess
    SolEr.style.display = ""


        TableP1 = thisForm.Plan[0].checked?thisForm.Plan[0].value:thisForm.Plan[1].value
        TableP2 = "S" // Single pay
        AmountToSolve = parseFloat(thisForm.SolveAmt.value) - 30
//        PremFee.innerText = dollarize(AmountToSolve)// for testing
        Mulx = thisForm.sex[0].checked?thisForm.sex[0].value:thisForm.sex[1].value
        SMidx = thisForm.smo[0].checked?thisForm.smo[0].value:thisForm.smo[1].value
        SMidx = thisForm.Plan[1].checked?"":SMidx // set to space if Graded smoker not apply

        Inx = (thisForm.Age.value * 2) + parseInt(Mulx)  //Mulx = gender


        InsAmt = (AmountToSolve) / eval((TableP1+TableP2+SMidx))[Inx]

        thisForm.AmtIns.value = format(InsAmt * 1000, 2)
       // alert()
    SubForm.selAmtIns.value=thisForm.AmtIns.value
//alert()
        Inx = isNaN(thisForm.Age.value * 3)?0: parseInt(thisForm.Age.value * 3)

        daCalc = (InsAmt * eval("CV"+TableP1+TableP2+Mulx+SMidx)[Inx])  //Mulx = gender
    cv10.innerText = dollarize(daCalc)
        daCalc = (InsAmt * eval("CV"+TableP1+TableP2+Mulx+SMidx)[Inx + 1]) //Mulx = gender
        cv20.innerText = dollarize(daCalc)
        daCalc = (InsAmt * eval("CV"+TableP1+TableP2+Mulx+SMidx)[Inx + 2])  //Mulx = gender
        cv65.innerText = dollarize(daCalc)

        Inx = (thisForm.Age.value * 2) + parseInt(Mulx)

        daCalc = (InsAmt) * eval((TableP1+TableP2+SMidx))[Inx]
//alert((TableP1+TableP2))
        daFee = (daCalc + 30) * (daCalc > 1)

        if (thisForm.Mode[0].checked) {Mper = thisForm.Mode[0].value}
        if (thisForm.Mode[1].checked) {Mper = thisForm.Mode[1].value}
        if (thisForm.Mode[2].checked) {Mper = thisForm.Mode[2].value}
        if (thisForm.Mode[3].checked) {Mper = thisForm.Mode[3].value}
        if (thisForm.Mode[4].checked) {Mper = thisForm.Mode[4].value}
        if (TableP2 == "S"){Mper = thisForm.Mode[0].value}
        daMode = daFee * Mper

        PremMode.innerText = dollarize(daMode)
        db1.innerText =  thisForm.Plan[0].checked ? dollarize(DthBen(0)):"Return of Premium Paid" // dollarize(DthBen(0))
        db2.innerText = dollarize(DthBen(1))
        //db3.innerText = dollarize(DthBen(2))
        db3.innerText = dollarize(parseInt(InsAmt))
        //db4.innerText
        db5.innerText = dollarize(parseInt(InsAmt))
        SubForm.InsAmt.value = dollarize(parseInt(InsAmt))
        SubForm.Prem.value = dollarize(parseFloat(daMode))




        Error = thisForm.RePQ[0].checked?0:thisForm.RePQ[1].checked?0:1
   // Error = 0
        ShowHidden()



        Compx(thisForm.Age,AgeEr,AgeEr1,thisForm.Plan[0].checked?thisForm.Pay[1].checked?80:85:80,thisForm.Plan[0].checked?0:5)
        IamBeingSolved = true
        Compx(thisForm.AmtIns,AmtEr,AmtEr1,thisForm.Plan[0].checked?50000:25000,thisForm.Plan[0].checked?2000:1000)



        SolEr.style.display = Trim(AmtEr.innerText)!=""?"":"none"

        Prtx.style.display = Error?"none":""
        IamBeingSolved = false
}
//********************************************************************
//********************************************************************
//********************************************************************
function ckMe(){

var i
var  Selcolor="#000000"
var  Defcolor="#666666"
var  Bakcolor="#FFFFFF"
var  BakDefol="#FFFFFF"
SubForm.RePQ1.value = thisForm.RePQ[0].checked?1:thisForm.RePQ[1].checked?0:2
//thisForm.q2.value = thisForm.Plan[0].checked?"1":"" // use in PDF for required field questions
//thisForm.q3.value = thisForm.Plan[0].checked?"1":"" // use in PDF for required field questions
//thisForm.q4.value = thisForm.Plan[0].checked?"1":"" // use in PDF for required field questions
SubForm.Gender.value = thisForm.sex[0].checked?"Female":"Male"

for (i=0;i<3;i++){
    eval("Pa"+i+".style").color = thisForm.Pay[i].checked?Selcolor:Defcolor
    eval("Pa"+i+".style").background = thisForm.Pay[i].checked?Bakcolor:BakDefol
    eval("Pa"+i+".style").fontWeight = thisForm.Pay[i].checked?"bold":"normal"
    SubForm.selPay.value=thisForm.Pay[i].checked?thisForm.Pay[i].value:SubForm.selPay.value
}
SubForm.selPay.value=SubForm.selPay.value=="T"?"20 Pay":SubForm.selPay.value=="F"?"Full Pay Whole Life":"Single Pay"

for (i=0;i<5;i++){
    eval("Mo"+i+".style").color = thisForm.Mode[i].checked?Selcolor:Defcolor
    eval("Mo"+i+".style").background = thisForm.Mode[i].checked?Bakcolor:BakDefol
    eval("Mo"+i+".style").fontWeight = thisForm.Mode[i].checked?"bold":"normal"
}
SubForm.selMode.value=thisForm.Mode[0].checked?"Annual":SubForm.selMode.value
SubForm.selMode.value=thisForm.Mode[1].checked?"Semiannual":SubForm.selMode.value
SubForm.selMode.value=thisForm.Mode[2].checked?"Quarterly":SubForm.selMode.value
SubForm.selMode.value=thisForm.Mode[3].checked?"Direct Monthly":SubForm.selMode.value
SubForm.selMode.value=thisForm.Mode[4].checked?"EFT Monthly":SubForm.selMode.value
// uncomment to show the lb302   LB302.style.display=thisForm.Mode[4].checked?"":"none"

for (i=0;i<6;i++){
    eval("Sta"+i+".style").color = thisForm.aState1[i].checked?Selcolor:Defcolor
    eval("Sta"+i+".style").background = thisForm.aState1[i].checked?Bakcolor:BakDefol
    eval("Sta"+i+".style").fontWeight = thisForm.aState1[i].checked?"bold":"normal"
      SubForm.selState.value=thisForm.aState1[i].checked?thisForm.aState1[i].value:SubForm.selState.value
}


MOgrdeath.style.display = ""
if (SubForm.selState.value=="Missouri"){
    thisForm.Plan[0].checked=true
    MOgrdeath.style.display = "none"
    }
/*********************************************************************/

//for (i=0;i<5;i++){
//    eval("Sta"+i+".style").color = thisForm.aState1[i].checked?Selcolor:Defcolor
//}



for (i=0;i<2;i++){
    eval("Pl"+i+".style").color = thisForm.Plan[i].checked?Selcolor:Defcolor
    eval("Pl"+i+".style").background = thisForm.Plan[i].checked?Bakcolor:BakDefol
    eval("Pl"+i+".style").fontWeight = thisForm.Plan[i].checked?"bold":"normal"
  SubForm.selPlan.value=thisForm.Plan[i].checked?thisForm.Plan[i].value:SubForm.selPlan.value

    eval("Se"+i+".style").color = thisForm.sex[i].checked?Selcolor:Defcolor
    eval("Se"+i+".style").background = thisForm.sex[i].checked?Bakcolor:BakDefol
    eval("Se"+i+".style").fontWeight = thisForm.sex[i].checked?"bold":"normal"
    eval("Sm"+i+".style").color = thisForm.smo[i].checked?Selcolor:Defcolor
    eval("Sm"+i+".style").background = thisForm.smo[i].checked?Bakcolor:BakDefol
    eval("Sm"+i+".style").fontWeight = thisForm.smo[i].checked?"bold":"normal"

    eval("Re"+i+".style").color = thisForm.RePQ[i].checked?Selcolor:Defcolor
    eval("Re"+i+".style").background = thisForm.RePQ[i].checked?Bakcolor:BakDefol
    eval("Re"+i+".style").fontWeight = thisForm.RePQ[i].checked?"bold":"normal"
}


Smoker.style.display = thisForm.Plan[1].checked?"none":"" // No smoker on Graded Death
NoGDpay.style.display = thisForm.Plan[1].checked?"none":"" // No Single Pay on Graded Death

if(thisForm.Plan[1].checked){
    thisForm.smo[0].checked=false // No smoker on Graded Death
    thisForm.smo[1].checked=false // No smoker on Graded Death
    thisForm.Pay[1].checked=thisForm.Pay[0].checked?true:thisForm.Pay[1].checked // No Single Pay on Graded Death check 20 pay
    SubForm.selTobacco.value =""

}
    else{
      if(thisForm.smo[0].checked==false & thisForm.smo[1].checked==false){
            thisForm.smo[0].checked=true
            }
      SubForm.selTobacco.value = thisForm.smo[0].checked?"Smoker":"Non-Smoker"
    }


SubForm.selPlan.value=SubForm.selPlan.value=="SI"?"Simplified Issue":"Graded Death Benefit"

Error = thisForm.RePQ[0].checked?0:thisForm.RePQ[1].checked?0:1
ShowHidden()

Comp(thisForm.Age,AgeEr,AgeEr1,thisForm.Plan[0].checked?thisForm.Pay[1].checked?80:85:80,thisForm.Plan[0].checked?0:5)
Comp(thisForm.AmtIns,AmtEr,AmtEr1,thisForm.Plan[0].checked?50000:25000,thisForm.Plan[0].checked?2000:1000)

  SubForm.selAge.value=thisForm.Age.value
  SubForm.selAmtIns.value=thisForm.AmtIns.value

Prtx.style.display = Error?"none":""
SolEr.style.display = AmtEr.innerText!=""?"":"none"
}
/*****************************************/

function CKSmoker(){
	//alert(thisForm.Age.value)
	if(parseInt(thisForm.Age.value) < 18)  thisForm.smo[0].checked=false // Smoker not appliciable
	if(parseInt(thisForm.Age.value) < 18)  thisForm.smo[1].checked=true // Non-Smoker IS
	}
/********************************************/
function ShowHidden(){

   ReQm.style.display = thisForm.RePQ[0].checked?"none":thisForm.RePQ[1].checked?"none":""

    }



function DthBen(Yr){
    InsAmt = isNaN(parseInt(thisForm.AmtIns.value))?0:parseInt(thisForm.AmtIns.value)
    if (thisForm.Plan[0].checked) {
      SDB.style.display="" // Show Simplified Death Benefit
      GDB.style.display="none" // Hide Graded Death Benefit
      return InsAmt
      }
      SDB.style.display="none" // Hide Simplified Death Benefit
      GDB.style.display="" // Show Graded Death Benefit
      Mulx = thisForm.sex[0].checked?thisForm.sex[0].value:thisForm.sex[1].value
      Inx = isNaN(thisForm.Age.value * 6)?0: parseInt(thisForm.Age.value * 6)
      Inx += (Mulx * 3) + Yr
      if(thisForm.Pay[0].checked) {return (parseInt(InsAmt) * (Dben[Inx] / 100))}
      return (parseInt(InsAmt) * (.25 * (Yr + 1)))
    }
/******************************************************
***************************************************/
function Calc(){
    if (!Error){
        TableP1 = thisForm.Plan[0].checked?thisForm.Plan[0].value:thisForm.Plan[1].value
        TableP2 = thisForm.Pay[0].checked?thisForm.Pay[0].value:thisForm.Pay[1].checked?thisForm.Pay[1].value:thisForm.Pay[2].value
        Mulx = thisForm.sex[0].checked?thisForm.sex[0].value:thisForm.sex[1].value
        SMidx = thisForm.smo[0].checked?thisForm.smo[0].value:thisForm.smo[1].value
        SMidx = thisForm.Plan[1].checked?"":SMidx // set to space if Graded smoker not apply
        InsAmt = isNaN((parseInt(thisForm.AmtIns.value) / 1000))?0:(parseInt(thisForm.AmtIns.value) / 1000)
        Inx = isNaN(thisForm.Age.value * 3)?0: parseInt(thisForm.Age.value * 3)


        daCalc = (InsAmt * eval("CV"+TableP1+TableP2+Mulx+SMidx)[Inx])
        cv10.innerText = dollarize(daCalc)
        SubForm.Yr10.value=cv10.innerText /* mcgonigal */
        daCalc = (InsAmt * eval("CV"+TableP1+TableP2+Mulx+SMidx)[Inx + 1])
        cv20.innerText = dollarize(daCalc)
        SubForm.Yr20.value=cv20.innerText /* mcgonigal */
        daCalc = (InsAmt * eval("CV"+TableP1+TableP2+Mulx+SMidx)[Inx + 2])
        cv65.innerText = dollarize(daCalc)
        SubForm.Ag65.value=cv65.innerText /* mcgonigal */

        Inx = (thisForm.Age.value * 2) + parseInt(Mulx) //Mulx = gender

        daCalc = (InsAmt) * eval((TableP1+TableP2+SMidx))[Inx]


        daFee = (daCalc + 30) * (daCalc > 1)

        if (thisForm.Mode[0].checked) {Mper = thisForm.Mode[0].value}
        if (thisForm.Mode[1].checked) {Mper = thisForm.Mode[1].value}
        if (thisForm.Mode[2].checked) {Mper = thisForm.Mode[2].value}
        if (thisForm.Mode[3].checked) {Mper = thisForm.Mode[3].value}
        if (thisForm.Mode[4].checked) {Mper = thisForm.Mode[4].value}
        if (TableP2 == "S"){Mper = thisForm.Mode[0].value}
        daMode = daFee * Mper

        PremMode.innerText = dollarize(daMode)
        db1.innerText =  thisForm.Plan[0].checked ? dollarize(DthBen(0)):"Return of Premium Paid" // dollarize(DthBen(0))
        db2.innerText = dollarize(DthBen(1))
        db3.innerText = dollarize(parseInt(InsAmt))
        db5.innerText = dollarize(parseInt(InsAmt))

            SubForm.DeathBen1.value= thisForm.Plan[0].checked ? "":db1.innerText
            SubForm.DeathBen2.value= thisForm.Plan[0].checked ? "":db2.innerText
            SubForm.DeathBen3.value= thisForm.Plan[0].checked ? "":db3.innerText

            SubForm.Y1t.value= thisForm.Plan[0].checked ? "":"Year 1"
            SubForm.Y2t.value= thisForm.Plan[0].checked ? "":"Year 2"
            SubForm.Y3t.value= thisForm.Plan[0].checked ? "":"Year 3+"

            SubForm.DeathBen5.value= db5.innerText

        SubForm.InsAmt.value = dollarize(parseInt(InsAmt))
        SubForm.Prem.value = dollarize(parseFloat(daMode))
    }
    else{//tPrem.innerText= dollarize(0)// for testing
         db1.innerText = dollarize(0)      // set to zero
         db2.innerText = dollarize(0)
         db3.innerText = dollarize(0)

         db5.innerText = dollarize(0)
         PremMode.innerText = dollarize(0)
         SubForm.InsAmt.value = dollarize(0)
         SubForm.Prem.value = dollarize(0)

         cv10.innerText = "------"
         cv20.innerText = "------"
         cv65.innerText = "------"
         }
   allMode.style.display=""
   if (thisForm.Pay[0].checked){allMode.style.display="none"}
}
/************************************************************
************************************************************/
function Compx(ME,MEr,MEr1,Max,Min){

var Mess, IMess
ME.style.background = "#FFFFFF"
ME.style.color = "#000000"

Mess = "*Required"
IMess = "{Invalid Entry}"

Mess1 = " Range " + Min + " to " + Max


MEr.innerText = " "
MEr1.style.fontSize = "10"
MEr1.style.color = "#336600"
MEr1.innerText = Mess1

ValidDecimal = 0
if (Trim(ME.value).indexOf(".") == 4 || Trim(ME.value).indexOf(".") == 5) ValidDecimal = 1
    if ((Trim(ME.value).length == 0) ||
       (parseInt(Trim(ME.value)) < Min) ||
       ((Trim(ME.value).indexOf(".") > -1) && ValidDecimal == 0) ||
       (parseInt(Trim(ME.value)) > Max) ||
       (isNaN(ME.value))){
          MEr.innerText = (Trim(ME.value).length == 0)?Mess:IMess
          Error++
           if(IamBeingSolved)ME.value = ""
    }
}
function Comp(ME,MEr,MEr1,Max,Min){
    Compx(ME,MEr,MEr1,Max,Min)
    Calc()
}


/***********************************************************
trim is a simple function to remove leading/trailing spaces
************************************************************/
function Trim(aStr) {
    return aStr.replace(/^\s{1,}/, "").replace(/\s{1,}$/, "")
}
function LoadME(){
SubForm=document.forms[0];
thisForm=document.forms[1];

    }











