// Keep user from entering more than maxLength characters
function doKeypress(control) {
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
    if (maxLength && value.length > maxLength - 1) {
        event.returnValue = false;
        maxLength = parseInt(maxLength);
    }
}
// Cancel default behavior
function doBeforePaste(control) {
    maxLength = control.attributes["maxLength"].value;
    if (maxLength) {
        event.returnValue = false;
    }
}
// Cancel default behavior and create a new paste routine
function doPaste(control) {
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
    if (maxLength) {
        event.returnValue = false;
        maxLength = parseInt(maxLength);
        var oTR = control.document.selection.createRange();
        var iInsertLength = maxLength - value.length + oTR.text.length;
        var sData = window.clipboardData.getData("Text").substr(0, iInsertLength);
        oTR.text = sData;
    }
}

// Generic re-sizing function - © 2007 The Watchmaker Project 

function colarr() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string') element = document.getElementById(element);
        if (arguments.length == 1) return element;
        elements.push(element);
    }
    return elements;
}

var BoxHeights = {
    maxh: 0,
    boxes: Array(),
    num: 0,
    op_test: false,
    equalise: function() {
        this.num = arguments.length;
        for (var i = 0; i < this.num; i++) if (!colarr(arguments[i])) return;
        this.boxes = arguments;
        this.maxheight();
        for (var i = 0; i < this.num; i++) colarr(arguments[i]).style.height = this.maxh + "px";
    },
    maxheight: function() {
        var heights = new Array();
        for (var i = 0; i < this.num; i++) {
            if (navigator.userAgent.toLowerCase().indexOf('opera') == -1) {
                heights.push(colarr(this.boxes[i]).scrollHeight);
            } else {
                heights.push(colarr(this.boxes[i]).offsetHeight);
            }
        }
        heights.sort(this.sortNumeric);
        this.maxh = heights[this.num - 1];
    },
    sortNumeric: function(f, s) {
        return f - s;
    }
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function setHomeLink() {
    var branding = document.getElementById('branding');

    branding.style.cursor = "pointer";
    branding.onclick = function() {
        document.location.href = "/";
    };
}

addLoadEvent(setHomeLink);

// START OF BENEFIT CALCULATOR CODE

function Resetfunc() {
    document.aspnetForm.reset(); document.aspnetForm.rambox.value = "0.00"; document.aspnetForm.CTbillbox.value = "0.00"
    document.aspnetForm.reset(); document.aspnetForm.empcashboxw.value = "0.00"; document.aspnetForm.wkwklist.selectedIndex = 2
    document.aspnetForm.reset()
}

function valit(obj, lowval, hival) {
    valmsg = "xxxx";  // **** set up variable valmsg

    if (obj == document.aspnetForm.frewksbox)
        (valmsg = "Rent free weeks can't really exceed maximum of 52");

    if ((obj.value < lowval) || (obj.value > hival))
        alert(valmsg)   //***  whatever msg you want !
    //   *  Call me with ----- onChange="valit(this, 18, 99)"
}

function hilite(cellid)   // *******   this function hilites cell with focus on it    ******************
{
    //document.getElementById(cellid).style.backgroundColor="yellow"       // document.all[cellid]
}
function lolite(cellid)   // *******   this function UN-hilites cell that's lost focus    ******************
{
    //document.getElementById(cellid).style.backgroundColor="white"       
}

// ****** ****** ****** *****
function DoCB() {
    sub16 = 0; sub18 = 0; cb = 0; cbrate = 13.2; cb1st = 6.8; Status = "emu"
    sub16 = document.aspnetForm.sub16box.value; sub18 = document.aspnetForm.sub18box.value
    cb = (sub16 / 1 + sub18 / 1)
    cb = (cb * cbrate)
    if (cb > 0) (cb = cb + cb1st)
    s = cb; Formatit(s); cb = s   //***** Format & display "interim" cb
    document.aspnetForm.cbbox.value = cb
}

function CAsafenet() {
    if (document.aspnetForm.statuslist.selectedIndex == 0 && document.aspnetForm.invcalist.selectedIndex == 1) document.aspnetForm.invcalist.selectedIndex = 0
    if (document.aspnetForm.statuslist.selectedIndex == 0 && document.aspnetForm.invcalist.selectedIndex == 3) document.aspnetForm.invcalist.selectedIndex = 0
    if (document.aspnetForm.statuslist.selectedIndex == 0 && document.aspnetForm.invcalist.selectedIndex == 5) document.aspnetForm.invcalist.selectedIndex = 0
}

function chkdskds() {
    allkds = (document.aspnetForm.sub16box.value / 1) + (document.aspnetForm.sub18box.value / 1)
    if (document.aspnetForm.diskidsbox.value > allkds) document.aspnetForm.diskidsbox.value = allkds
}


// ****** this func stops   ***** Back50List   LabelBack50
function chk50() {
    if (document.aspnetForm.agebox.value < 50) { (document.aspnetForm.Back50List.selectedIndex = 0); (document.getElementById("LabelBack50").style.color = "lightgrey") }
    else
        document.getElementById("LabelBack50").style.color = "black"
}

function knownPClock() {
    if (document.aspnetForm.agebox.value < 60) { (document.aspnetForm.knownPCbox.value = 0); (document.getElementById("LabelknownPC").style.color = "lightgrey") }
    else
        LabelknownPC.style.color = "black"
}

function nojob() {
    if (document.aspnetForm.empcashboxw.value < 9 && document.aspnetForm.empcashboxm.value < 9) document.aspnetForm.wkwklist.selectedIndex = 2
    else
        document.aspnetForm.wkwklist.selectedIndex = 0
}

// ****** this func stops   ***** BabyList 
function chkbaby() {
    if (document.aspnetForm.sub16box.value < 1) (document.aspnetForm.BabyList.selectedIndex = 0)
}
// ****** this func  *****   Kcarebox locker - but once locked, it can't unlock itself ! so get sub16box to run the func !!!
function kcarelock() {
    if (document.aspnetForm.sub16box.value < 1 || document.aspnetForm.wkwklist.selectedIndex == 2) document.aspnetForm.kcarebox.value = 0
}

function lock30bet() {
    allkds = (document.aspnetForm.sub16box.value / 1) + (document.aspnetForm.sub18box.value / 1)
    if (document.aspnetForm.wkwklist.selectedIndex == 0) document.aspnetForm.List30bet.selectedIndex = 0
    if (allkds < 1 && document.aspnetForm.DisWkWkhourslist.selectedIndex == 0) document.aspnetForm.List30bet.selectedIndex = 0
    if (document.aspnetForm.statuslist.selectedIndex == 0) document.aspnetForm.List30bet.selectedIndex = 0  //*** single ewitches it off 

    if (document.aspnetForm.wkwklist.selectedIndex > 0 && allkds > 0 && document.aspnetForm.statuslist.selectedIndex == 1) document.getElementById("Label30bet").style.color = "black"
    else
        if (document.aspnetForm.wkwklist.selectedIndex > 0 && document.aspnetForm.DisWkWkhourslist.selectedIndex == 1) document.getElementById("Label30bet").style.color = "black"
    else
    { document.getElementById("Label30bet").style.color = "lightgrey"; }
}
//***  If/Else  needs no  Then or End If

function lockDSQs() {
    document.getElementById("LabeladDLA").style.color = "lightgrey";
    document.getElementById("LabeladDSwrk").style.color = "lightgrey";
    document.getElementById("LabelSDP1").style.color = "lightgrey"
    document.getElementById("LabelSDP2").style.color = "lightgrey";
    document.getElementById("LabelDsAAHR").style.color = "lightgrey"

    if (document.aspnetForm.dsablelist.selectedIndex == 1) {
        document.getElementById("LabeladDLA").style.color = "black";
        document.getElementById("LabeladDSwrk").style.color = "black";
        document.getElementById("LabelSDP1").style.color = "black";
        document.getElementById("LabelSDP2").style.color = "black";
        document.getElementById("LabelDsAAHR").style.color = "black";
    }

    if (document.aspnetForm.ESAlist1.selectedIndex > 0 || document.aspnetForm.ESAlist4.selectedIndex > 0) {
        document.getElementById("LabeladDSwrk").style.color = "black";
    }
}


//  ** ** ** If single this func supresses ESA Partner row 
function singleESA() {
    if (document.aspnetForm.statuslist.selectedIndex == 0)
    { (Label_pESA.style.color = "lightgrey"); (document.aspnetForm.ESAlist4.selectedIndex = 0) }
    else
        (Label_pESA.style.color = "black")
}

function Labbabyhider() {
    if (document.aspnetForm.sub16box.value > 0) {
        document.getElementById("Labelbaby").style.color = "black";
    } else {
        document.getElementById("Labelbaby").style.color = "lightgrey";
    }
}

function Labdskidhider() {
    if (document.aspnetForm.sub16box.value > 0 || document.aspnetForm.sub18box.value > 0) {
        document.getElementById("LabelDSk").style.color = "black";
        document.getElementById("LabelDSkEN").style.color = "black";
    } else {
        document.getElementById("LabelDSk").style.color = "lightgrey";
        document.getElementById("LabelDSkEN").style.color = "lightgrey";
    }
}

function LabSLPhider() {
    allkds = (document.aspnetForm.sub16box.value / 1) + (document.aspnetForm.sub18box.value / 1)
    if (allkds > 0 && document.aspnetForm.statuslist.selectedIndex == 0) {
        document.getElementById("LabelSLP1").style.color = "black";
    } else {
        document.getElementById("LabelSLP1").style.color = "lightgrey";
    }
}

function Labkcarehider() {
    if (document.aspnetForm.sub16box.value > 0 && document.aspnetForm.wkwklist.selectedIndex < 2) {
        document.getElementById("Labelkcare").style.color = "black";
    } else {
        document.getElementById("Labelkcare").style.color = "lightgrey";
    }
}

// ****** this func stops   *****
function chkEnhDPkids() {
    if (document.aspnetForm.EnhDPkidsbox.value > document.aspnetForm.diskidsbox.value) document.aspnetForm.EnhDPkidsbox.value = document.aspnetForm.diskidsbox.value
}

function chklpp() {
    allkds = (document.aspnetForm.sub16box.value / 1) + (document.aspnetForm.sub18box.value / 1)
    if (document.aspnetForm.statuslist.selectedIndex == 1 || allkds == 0) document.aspnetForm.lpplist.selectedIndex = 0
}

function chkdis() {
    if (document.aspnetForm.dsablelist.selectedIndex == 0) {
        (document.aspnetForm.dissevlist.selectedIndex = 0); (document.aspnetForm.CAlist.selectedIndex = 0);
        (document.aspnetForm.hrccdlalist.selectedIndex = 0); (document.aspnetForm.DsAAHRList.selectedIndex = 0)
    }

    if (document.aspnetForm.dsablelist.selectedIndex == 0 && document.aspnetForm.ESAlist1.selectedIndex == 0 && document.aspnetForm.ESAlist4.selectedIndex == 0)
        (document.aspnetForm.DisWkWkhourslist.selectedIndex = 0)
}

function jumpIS() {
    if (document.aspnetForm.incsuplist.selectedIndex == 1) (document.location.href = '#1')
}

// ***** ************* ************* ************* *************
function Calcfunc() {
    FYdays = 365; ram = 0; ContRent = 0; LHAamt = 0; frewks = 0; numfrewks = 0; CT = 0; CTweek = 0; altCT = 0
    altCTwam = 0; CTwam = 0; hb = 0; maxhb = 0; exinc = 0; taper = 0
    CTexinc = 0; CTkoala = 0; CTpremaa = 0; ctb = 0; altctb = 0

    invca = "Na"; incsup = "No"; age = 45; Status = "couple"
    hipenp = "No"; Cr30hours = 16.85
    sub16 = 0; sub18 = 0; cb = 0; cbrate = 13.2; cb1st = 6.8   //'* NB this stuff 2b dup'd in func (DoCB) 4 real-time cb ok
    esaWRcomp = 25.5; esaSCcomp = 30.85

    kidzAA = 0; dskids = 0; adaa = 0; dsable = "No"; dissev = "No"; aa = 0; premaa = 0
    WkWkhours = "ugh"; empinc = 0; empcash = 0; empcashm = 0; minderm = 0; taxnim = 0; penconm = 0
    erndis = 0; minder = 0; taxni = 0; pencon = 0
    maintinc = 0; subteninc = 0; brdsum = 0; otheruninc = 0; maindis = 0
    Uncash = 0; Unearndis = 0; rnt = 0; canberra = 0; CapA = 0; CapB = 0
    Kcarekids = 0; Kcare = 0; StudiGC = 0; Pout = 0; EnhDP = 0
    HBndDeds = 0; CTndDeds = 0; ad2inc = "frog"; prem = 0; DisWkWkhours = "ugh"; BothWkhours = "yug"; Back2Wk50 = "No"
    Low = 0; High = 0
    capinc = 0; OtherGI = 0; Otherall = 0; OtherpostDis = 0; grossinc = 0; Gemp4TC = 0; sw1 = "OFF"; AdEnhdsP = "No"
    maxTC = 0; KcareFees = 0; TCthreshold = 0; familyelement = 0; k = 0; exsTCinc = 0
    taperTC = 0; annTC = 0; toptaper = 0; topslice = 0; exportTC = 0; WPC = 0; kkbal = 0
    C_ESA_Phase = "None"; C_ESA_Elem = "None"; C_ESA_Comp = "None"; P_ESA_Phase = "None"; P_ESA_Elem = "None"; P_ESA_Comp = "None"

    // ''''''********  AA's + prems uprated for Sp 09    ***************************** )
    SCsub25AA = 50.95; SCover25AA = 64.3; CPLEsub18AA = 76.9; CoupleAA = 100.95; sub16AA = 56.11; babyAA = 10.5; sub18AA = 56.11
    Sing60AA = 130; Sing65AA = 150.4; Coup60AA = 198.45; Coup65AA = 225.5
    HBndrat1 = 7.4; HBndrat2 = 17; HBndrat3 = 23.35; HBndrat4 = 38.2; HBndrat5 = 43.5
    HBndrat6 = 47.75; CTndrat1 = 2.3; CTndrat2 = 4.6; CTndrat3 = 5.8; CTndrat4 = 6.95

    Familyprem = 17.3; LPP = 4.9; dsprem1 = 27.5; dsprem2 = 39.15; dssevprem1 = 52.85; dssevprem2 = 105.7; diskidprem = 51.24
    careprem1 = 29.5; careprem2 = 59; enhdsprem1 = 13.4; enhdsprem2 = 19.3; enhdspremchld = 20.65

    CAsafenet()  // force extra run of CA safety net  
    //  -----------  VB form 2 LOGIC ----------------------------------------------- 
    ContRent = document.aspnetForm.rambox.value / 1     // Contract Rent
    LHAamt = document.aspnetForm.LHAbox.value / 1       // LHA amount

    if (document.aspnetForm.ramperlist.selectedIndex == 1) (ContRent = ContRent / 2)
    if (document.aspnetForm.ramperlist.selectedIndex == 2) { ContRent = (ContRent * 12); ContRent = (ContRent / 52) }

    ram = ContRent  // *** these 4 rules set ram which feeds EliRent
    if (LHAamt > 0) (ram = LHAamt)
    if (LHAamt > ContRent) (ram = LHAamt)
    if (LHAamt > (ContRent + 15)) { ram = (ContRent + 15) }

    frewks = document.aspnetForm.frewksbox.value

    if (frewks > 0) { numfrewks = frewks; frewks = (52 - frewks) }

    CT = document.aspnetForm.CTbillbox.value

    CTweek = (CT / FYdays * 7)

    // Module1.revdisco = List3
    altCT = CT
    if (document.aspnetForm.revdiscolist.selectedIndex == 1) (altCT = CT / 3 * 4)
    if (document.aspnetForm.revdiscolist.selectedIndex == 2) (altCT = CT * 2)

    altCTwam = (altCT / FYdays * 7)
    // 
    //  -----------  VB form 3 LOGIC -----------------------------------------------

    if (document.aspnetForm.incsuplist.selectedIndex == 1) (incsup = "Yes")
    if (document.aspnetForm.statuslist.selectedIndex == 0) (Status = "single")
    age = document.aspnetForm.agebox.value

    //   ***    Claimant's ESA Phase - bit of validation here: no ESA no Elem or Comp: no Main no Comp
    if (document.aspnetForm.ESAlist1.selectedIndex == 0) { (document.aspnetForm.ESAlist2.selectedIndex = 0); (document.aspnetForm.ESAlist3.selectedIndex = 0) }
    if (document.aspnetForm.ESAlist1.selectedIndex == 1) { (C_ESA_Phase = "Ass"); (document.aspnetForm.ESAlist3.selectedIndex = 0) }
    if (document.aspnetForm.ESAlist1.selectedIndex == 2) (C_ESA_Phase = "Main")
    // all ESA values default to "None"
    if (document.aspnetForm.ESAlist2.selectedIndex == 1) (C_ESA_Elem = "IR")
    if (document.aspnetForm.ESAlist2.selectedIndex == 2) (C_ESA_Elem = "C")
    if (document.aspnetForm.ESAlist2.selectedIndex == 3) (C_ESA_Elem = "cred")
    if (document.aspnetForm.ESAlist3.selectedIndex == 1) (C_ESA_Comp = "Work-rel")
    if (document.aspnetForm.ESAlist3.selectedIndex == 2) (C_ESA_Comp = "Supp")
    // *** Partner's ESA Phase -
    if (document.aspnetForm.ESAlist4.selectedIndex == 0) { (document.aspnetForm.ESAlist5.selectedIndex = 0); (document.aspnetForm.ESAlist6.selectedIndex = 0) }
    if (document.aspnetForm.ESAlist4.selectedIndex == 1) { (P_ESA_Phase = "Ass"); (document.aspnetForm.ESAlist6.selectedIndex = 0) }
    if (document.aspnetForm.ESAlist4.selectedIndex == 2) (P_ESA_Phase = "Main")
    if (document.aspnetForm.ESAlist5.selectedIndex == 1) (P_ESA_Elem = "IR")
    if (document.aspnetForm.ESAlist5.selectedIndex == 2) (P_ESA_Elem = "C")
    if (document.aspnetForm.ESAlist5.selectedIndex == 3) (P_ESA_Elem = "cred")
    if (document.aspnetForm.ESAlist6.selectedIndex == 1) (P_ESA_Comp = "Work-rel")
    if (document.aspnetForm.ESAlist6.selectedIndex == 2) (P_ESA_Comp = "Supp")


    if (document.aspnetForm.dsablelist.selectedIndex == 1) (dsable = "Yes")
    //  *  set dissev = "No" [no SDP], "One" [single rate SDP] or "Both" [couple/double rate SDP]
    if (Status == "single" && document.aspnetForm.dissevlist.selectedIndex == 1 && document.aspnetForm.CAlist.selectedIndex == 0) (dissev = "One")
    if (Status == "couple" && document.aspnetForm.dissevlist.selectedIndex == 1 && document.aspnetForm.CAlist.selectedIndex == 1) (dissev = "One")
    if (Status == "couple" && document.aspnetForm.dissevlist.selectedIndex == 1 && document.aspnetForm.CAlist.selectedIndex == 0) (dissev = "Both")

    if (document.aspnetForm.invcalist.selectedIndex == 0) (invca = "No CA is payable")
    if (document.aspnetForm.invcalist.selectedIndex == 1) (invca = "Both of us get a CA")
    if (document.aspnetForm.invcalist.selectedIndex == 2) (invca = "I work and get CA")
    if (document.aspnetForm.invcalist.selectedIndex == 3) (invca = "Partner works & gets CA")
    if (document.aspnetForm.invcalist.selectedIndex == 4) (invca = "I get CA but don't work")
    if (document.aspnetForm.invcalist.selectedIndex == 5) (invca = "Partner has CA, not work")

    if (age > 79) (hipenp = "Yes")
    //			'Not quite as precise as conditions in VB5  
    if (age > 59 && dsable == "Yes") (hipenp = "Yes")

    //	***Not quite as precise as conditions in VB form3A1 & form4   '***  Adult EnhDP Checker ***  and AdEnhdsP setter 
    if (Status == "single" && document.aspnetForm.hrccdlalist.selectedIndex == 1 && age < 60 && dsable == "Yes") { (EnhDP = enhdsprem1); (AdEnhdsP = "Yes") }
    if (Status == "couple" && document.aspnetForm.hrccdlalist.selectedIndex == 1 && age < 60 && dsable == "Yes") { (EnhDP = enhdsprem2); (AdEnhdsP = "Yes") }

    if (Status == "single" && C_ESA_Comp == "Supp" && age < 60) { (EnhDP = enhdsprem1); (AdEnhdsP = "Yes") }
    if (Status == "couple" && C_ESA_Comp == "Supp" && age < 60) { (EnhDP = enhdsprem2); (AdEnhdsP = "Yes") }

    if (document.aspnetForm.DisWkWkhourslist.selectedIndex == 1) (DisWkWkhours = "16plus")
    // *******   DisWkWkhours = "16plus"

    //  -----------  VB form 4 LOGIC -----------------------------------------------
    //   !!!!!  '* Logic to pre assess entlement to enhanced disability premium
    //   !!!!!  '* NB this Logic duplicated in func DoCB 4 real-time cb
    sub16 = document.aspnetForm.sub16box.value; sub18 = document.aspnetForm.sub18box.value
    cb = (sub16 / 1 + sub18 / 1)
    cb = (cb * cbrate)
    if (cb > 0) (cb = cb + cb1st)

    kidzAA = (sub16 * sub16AA + sub18 * sub18AA)
    if (document.aspnetForm.BabyList.selectedIndex == 1) (kidzAA = kidzAA + babyAA)  // new  BabyAA

    if (Status == "single" && age < 25 && cb == 0) (adaa = SCsub25AA)
    if (Status == "single" && age > 24 && cb == 0) (adaa = SCover25AA)
    if (Status == "single" && cb > 0) (adaa = SCover25AA)
    if (Status == "single" && cb > 0 && age < 18) (adaa = SCsub25AA)
    //   ***   single ESA cust under 25 gets promoted  !
    if (Status == "single" && C_ESA_Phase == "Main") (adaa = SCover25AA)

    if (Status == "couple") (adaa = CoupleAA)
    if (Status == "couple" && age < 18 && C_ESA_Phase != "Main") (adaa = CPLEsub18AA)

    if (Status == "single" && age > 59) (adaa = Sing60AA)
    if (Status == "single" && age > 64) (adaa = Sing65AA)
    if (Status == "couple" && age > 59) (adaa = Coup60AA)
    if (Status == "couple" && age > 64) (adaa = Coup65AA)

    // ** add  ESA components onto HB/CTB PA's .. in certain circos ... under 60's only !
    if (C_ESA_Comp == "Work-rel" && age < 60) (adaa = adaa + esaWRcomp) // 
    if (C_ESA_Comp == "Supp" && age < 60) (adaa = adaa + esaSCcomp) // 
    if (C_ESA_Comp == "None" && P_ESA_Comp == "Work-rel" && dsable == "No" && age < 60) (adaa = adaa + esaWRcomp)
    if (C_ESA_Comp == "None" && P_ESA_Comp == "Supp" && dsable == "No" && age < 60) (adaa = adaa + esaSCcomp)

    //  ----------- End  VB form 4 LOGIC 'cept needs bit of enhanced disability -----------
    //  -----------  VB form 5 LOGIC -----------------------------------------------

    if (document.aspnetForm.wkwklist.selectedIndex == 0) (WkWkhours = "30plus")
    if (document.aspnetForm.wkwklist.selectedIndex == 1) (WkWkhours = "16plus")
    if (document.aspnetForm.wkwklist.selectedIndex == 2) (WkWkhours = "under16")

    if (document.aspnetForm.List30bet.selectedIndex == 1) (BothWkhours = "30ish")

    empcashm = document.aspnetForm.empcashboxm.value
    empcashm = (empcashm * 12); empcashm = (empcashm / 52)
    minderm = (document.aspnetForm.minderboxm.value / 3)
    minderm = (minderm * 12); minderm = (minderm / 52)
    taxnim = document.aspnetForm.taxniboxm.value
    taxnim = (taxnim * 12); taxnim = (taxnim / 52)

    penconm = (document.aspnetForm.penconboxm.value / 2)
    penconm = (penconm * 12); penconm = (penconm / 52)

    empcash = (document.aspnetForm.empcashboxw.value / 1 + empcashm)
    minder = (document.aspnetForm.minderboxw.value / 3 + minderm)
    taxni = (document.aspnetForm.taxniboxw.value / 1 + taxnim)
    pencon = (document.aspnetForm.penconboxw.value / 2 + penconm)

    empinc = (empcash / 1 + minder / 1 - taxni - pencon)

    //'*** Monthly adjustments - This is where we got a net weekly empinc
    // // if (document.aspnetForm.payplist.selectedIndex ==1 ) { empinc = (empinc * 12) ; empinc = (empinc / 52) }
    // If Back50List.ListIndex = 1 Then      Back2Wk50 = "Yes"     Else         Back2Wk50 = "No"     End If

    if (document.aspnetForm.Back50List.selectedIndex == 1) (Back2Wk50 = "Yes")

    // ----------------				Safety net 
    if (empinc < 0) (empinc = 0)
    //<!-------------------  ---------------->
    //  -----------  VB form 6 LOGIC -----------------------------------------------        
    maintinc = (document.aspnetForm.maintincbox.value / 1)
    subteninc = (document.aspnetForm.subtenincbox.value / 1)
    brdsum = (document.aspnetForm.brdsumbox.value / 2)

    otheruninc = (document.aspnetForm.otherunincbox.value / 1)

    if (maintinc > 0) (maindis = 15)
    if (maintinc < 15) (maindis = maintinc)
    if (cb < 1) (maindis = 0)
    // got to get FP

    Uncash = (maintinc + subteninc + brdsum + otheruninc)
    Unearndis = maindis
    //'*** Unearndis is subtracted from Uncash at Form10
    rnt = (subteninc + brdsum)  //rnt needed later ?
    //  -----------  VB form 7 LOGIC -----------------------------------------------
    CapA = (document.aspnetForm.capabox.value / 1)
    CapB = (document.aspnetForm.capbbox.value / 1)

    canberra = (CapA + CapB)

    //'If canberra>= 16000 Then Exit Sub
    if (age < 60) { (canberra = canberra - 6000); (divby = 250) }
    if (age >= 60) { (canberra = canberra - 6000); (divby = 500) }

    tar1 = (canberra / divby)
    tar2 = parseInt(tar1)  //  '*** returns integer
    if (tar1 > tar2) (tar2 = tar2 + 1)
    canberra = tar2

    // ----------------				Safety net 
    if (canberra < 0) (canberra = 0)

    //  -----------  VB form 8 LOGIC ----------------------------------------------- 

    Kcarekids = sub16

    Kcare = (document.aspnetForm.kcarebox.value / 1)

    if (Kcarekids == 1 && Kcare > 175) (Kcare = 175)
    if (Kcarekids > 1 && Kcare > 300) (Kcare = 300)
    // ----------------				Safety net  --- NOT IN QBC 
    if (Kcarekids < 1) (Kcare = 0)

    StudiGC = (document.aspnetForm.studigcbox.value / 1); Pout = StudiGC
    //  -----------  VB form 9 LOGIC -----------------------------------------------
    //  ---    VB has some ds LOGIC here

    TopRate = (document.aspnetForm.TopRatebox.value / 1)
    Fifth = (document.aspnetForm.Fifthbox.value / 1)
    Fourth = (document.aspnetForm.Fourthbox.value / 1)
    ThreeA1 = (document.aspnetForm.ThreeA1box.value / 1)  // new in 07 field 4 SAR
    Third = (document.aspnetForm.Thirdbox.value / 1)
    TwoA1 = (document.aspnetForm.TwoA1box.value / 1)  // new in 07 field 4 SAR
    Second = (document.aspnetForm.Secondbox.value / 1)
    Lowest = (document.aspnetForm.Lowestbox.value / 1)
    AnyWPC = (document.aspnetForm.AnyWPCbox.value / 1)  // keep field value 4 SAR
    esa_C = (document.aspnetForm.esa_Cbox.value / 1)     // new in 08 field 4 ESA
    Residual = (document.aspnetForm.Residualbox.value / 1)
    Issub25 = (document.aspnetForm.Issub25box.value / 1)
    Isplus25 = (document.aspnetForm.Isplus25box.value / 1)

    x = (TopRate * HBndrat6)
    x = (x + Fifth * HBndrat5)
    x = (x + Fourth * HBndrat4)
    x = (x + ThreeA1 * HBndrat3)
    x = (x + Third * HBndrat3)
    x = (x + TwoA1 * HBndrat2)
    x = (x + Second * HBndrat2)
    x = (x + Lowest * HBndrat1)
    x = (x + esa_C * HBndrat1)
    x = (x + Residual * HBndrat1)
    x = (x + Isplus25 * HBndrat1)
    HBndDeds = x

    //'*** Don't forget to adjust 
    if (frewks > 0) { HBndDeds = (HBndDeds * 52); HBndDeds = (HBndDeds / frewks) }

    x = (TopRate * CTndrat4)
    x = (x + Fifth * CTndrat3)
    x = (x + Fourth * CTndrat2)
    x = (x + ThreeA1 * CTndrat2)
    x = (x + Third * CTndrat2)
    x = (x + TwoA1 * CTndrat1)
    x = (x + Second * CTndrat1)
    x = (x + Lowest * CTndrat1)
    x = (x + esa_C * CTndrat1)
    x = (x + Residual * CTndrat1)
    CTndDeds = x

    CTwam = (CTweek - CTndDeds) // NEEDS Safety net  --- NOT IN QBC

    // VB Sub SARinc()  wbc Not using variable  QotherAd
    //'****  be sure to xref the Alt CT CALC Sub in form11  ***************
    if (TopRate == 0 && Fifth == 0 && Fourth == 0 && Third == 0 && ThreeA1 == 0 && Second == 0 && TwoA1 == 0 && Lowest == 0 && AnyWPC == 0 && Residual == 0 && esa_C == 0 && Issub25 == 0 && Isplus25 == 0) (ad2inc = "none")
    if (Issub25 > 0 || Isplus25 > 0 || AnyWPC > 0) (ad2inc = "25%")
    if (Residual > 0 || Lowest > 0 || Second > 0 || esa_C > 0) (ad2inc = "15%")
    if (Third > 0 || TwoA1 > 0) (ad2inc = "7.5%")
    if (TopRate > 0 || Fifth > 0 || Fourth > 0 || ThreeA1 > 0) (ad2inc = "none")

    //  -----------  VB  formWoF TC  LOGIC ----------------------------------------------- 
    //'***  find the ANNUAL gross income first *********************************************
    capinc = ((CapA + CapB) * 0.045)  // '*** give 4.5% interest ANNUAL fig ie a realistic rate of interest on savings.
    //  don't use value of maintinc directly, better to built unearn fig up from components    
    OtherGI = ((subteninc + brdsum + otheruninc) * 52)  // *** These figs are weekly so make them ANNUAL * take Maintinc out, cb was never in
    Otherall = (OtherGI + capinc)  //'*** total "other" annual inc
    OtherpostDis = (Otherall - 300)  // '*** take off the IR Disreg on "other" weekly inc (£300 p/a)
    if (OtherpostDis < 0) (OtherpostDis = 0) //'*** don't let it go below zero

    Gemp4TC = (document.aspnetForm.empcashboxw.value / 1 + empcashm)
    Gemp4TC = (Gemp4TC + (document.aspnetForm.minderboxw.value / 1 + (minderm * 3)))
    Gemp4TC = (Gemp4TC - (document.aspnetForm.penconboxw.value / 1 + (+penconm * 2)))
    //  ***  Gemp4TC is coming here weekly minus Pension Contribs but includes fire, ch-mind, tax & NI !
    grossinc = ((Gemp4TC * 52) + OtherpostDis)     //'*** grossinc can be monthly !
    //'*** Found the ANNUAL gross income  *** it stacks up with VB ok ******************************************
    //'*** choose which calc to run here (joint WTC/CTC  or the CTC ONLY)   *****
    //'*** Default is to run CTC ONLY - which will Exit safely if NO kids
    sw1 = "CTC ONLY"
    //'*** so Let's try and satisfy the WTC tests which will run the joint WTC/CTC  *****
    if (WkWkhours == "30plus" && cb > 0) (sw1 = "WTC/CTC")
    if (WkWkhours == "16plus" && cb > 0) (sw1 = "WTC/CTC")
    if (WkWkhours == "30plus" && dsable == "Yes") (sw1 = "WTC/CTC")
    if (WkWkhours == "16plus" && dsable == "Yes" && DisWkWkhours == "16plus") (sw1 = "WTC/CTC")
    if (WkWkhours == "30plus" && dsable == "Yes") (sw1 = "WTC/CTC")
    if (WkWkhours == "30plus" && age >= 25) (sw1 = "WTC/CTC")
    if (WkWkhours == "16plus" && document.aspnetForm.Back50List.selectedIndex == 1) (sw1 = "WTC/CTC")

    // '*** Call TC_sub '***  always gets Called and tries to give CTC ! Sub TC_sub() '***  is going to aggregate  WTC/CTC calc - ! **********
    //        '*** maxTC  comes back here from WTC_OK Qual sub with the WTC elements   
    if (sw1 == "WTC/CTC") subfunWTC_OK()  // ***** call external (sub) function and come back here for CTC

    if (sw1 == "CTC ONLY")   //***  If/Else  needs no  Then or End If
        TCthreshold = 16040
    else
        TCthreshold = 6420
    // '*** insert the CTC elements  ! **************************
    //'************** if no children then no CTC can happen  **************************

    if (cb > 0) { maxTC = (maxTC + 545); familyelement = (familyelement + 545) } // ' *** Family element
    if (document.aspnetForm.BabyList.selectedIndex == 1 && cb > 0) { maxTC = (maxTC + 545); familyelement = (familyelement + 545) }
    //    ' *** Baby+Family element
    k = (sub16 / 1 + sub18 / 1)  //***   ' find no of Children
    maxTC = (maxTC + (k * 2235))
    maxTC = (maxTC + (document.aspnetForm.diskidsbox.value / 1 * 2670))
    maxTC = (maxTC + (document.aspnetForm.EnhDPkidsbox.value / 1 * 1075))
    //' *** That's the CTC elements  *****************************************************
    //'*** apply taper for every pound above threshold p/a  ***************************************

    if (grossinc > TCthreshold) (exsTCinc = (grossinc - TCthreshold))
    //'****  CALC it ***********************

    if (exsTCinc >= 0) { taperTC = (exsTCinc * 0.39); annTC = (maxTC - taperTC) }
    if (annTC < 0) (annTC = 0)
    if (cb > 0 && annTC <= familyelement) (annTC = familyelement)
    if (grossinc >= 50000) { topslice = (grossinc - 50000); toptaper = (topslice / 15) }
    toptaper = parseInt(toptaper)  //  '*** returns a true integer, no rounding up or down - better than VB

    if (grossinc >= 50000) (annTC = (annTC - toptaper))
    if (annTC < 0) (annTC = 0) // '*** don't let it go negative like
    //'***  final safety net
    if (grossinc <= TCthreshold) (annTC = maxTC)

    //  Not rounding up annTC here !  

    // '*** Finally Call PC_sub '***  always gets Called for OAP and tries to give WPC ! 
    if (age > 59) do_PC()

    // ***  zeroize all Tax Credits estimates if checkbox is unchecked
    if (document.aspnetForm.CHKTC.checked == false) { annTC = 0; WPC = 0 }

    //'***  SWING back to  pencilA_Click Sub      '***  pick up knownTC's if entered 
    if (document.aspnetForm.knownTCbox.value > 0 || document.aspnetForm.knownPCbox.value > 0) { (annTC = document.aspnetForm.knownTCbox.value); (WPC = document.aspnetForm.knownPCbox.value) }

    // '***  Need to pick up TC & PC ....add weekly sum ....    
    if (annTC > 0 && document.aspnetForm.knownTCbox.value == 0) (exportTC = annTC / 365 * 7)  //'***  picks up calc'd annTC or knownTCbox

    if (annTC > 0 && document.aspnetForm.knownTCbox.value > 0) (exportTC = annTC) //'***  picks up known weekly TC

    if (WPC > 0) (exportTC = exportTC / 1 + WPC / 1)  //'***  picks up WPC or knownPCbox

    //***##########   going to Show (weekly not ann) Credits detected by Shunting them into TCbox readonly 
    Uncash = (Uncash + exportTC / 1) // '**** Add on the weekly Creds  !!!


    //  -----------  VB form 10 LOGIC ----------------------------------------------- 

    if (incsup == "Yes") { empinc = 0; erndis = 0; canberra = 0; Uncash = 0; Unearndis = 0; Pout = 0 }

    // Call Premit   //  //Sub Premit()
    //'***** carer premium   *****
    if (document.aspnetForm.invcalist.selectedIndex == 1) { prem = (prem + careprem2) }
    if (document.aspnetForm.invcalist.selectedIndex > 1) { prem = (prem + careprem1) }


    //'*** Family premium  Use CB to test for kidz *****
    if (cb > 0) (prem = (prem + Familyprem))

    if (document.aspnetForm.lpplist.selectedIndex == 1 && age < 60 && dsable == "No") (prem = (prem + LPP))
    if (document.aspnetForm.lpplist.selectedIndex == 1 && age < 60 && dsable == "Yes") (prem = (prem + LPP))

    dskids = (document.aspnetForm.diskidsbox.value / 1)
    if (dskids > 0) { dskids = (dskids * diskidprem); prem = (prem + dskids) }

    if (age < 60 && Status == "single" && dsable == "Yes" && C_ESA_Phase == "None") (prem = (prem + dsprem1))
    if (age < 60 && Status == "couple" && dsable == "Yes" && C_ESA_Phase == "None") (prem = (prem + dsprem2))

    if (age < 60 && Status == "single" && dsable == "Yes" && dissev == "One") (prem = (prem + dssevprem1))
    if (age < 60 && Status == "couple" && dsable == "Yes" && dissev == "Both") (prem = (prem + dssevprem2))
    if (age < 60 && Status == "couple" && dsable == "Yes" && dissev == "One") (prem = (prem + dssevprem1))

    //  penprems gone  !
    if (age > 59 && Status == "single" && hipenp == "Yes" && dissev == "One") (prem = (prem + dssevprem1))
    if (age > 59 && Status == "couple" && hipenp == "Yes" && dissev == "Both") (prem = (prem + dssevprem2))
    if (age > 59 && Status == "couple" && hipenp == "Yes" && dissev == "One") (prem = (prem + dssevprem1))

    EnhDP = (EnhDP + (enhdspremchld * document.aspnetForm.EnhDPkidsbox.value))
    prem = (prem + EnhDP)

    //                  Sub Total()
    maxhb = (ram - HBndDeds)
    if (maxhb < 0) (maxhb = 0)

    //Call discal1 //Sub discal1()    fire is OUT !
    if (Status == "single") (erndis = 5)
    if (Status == "couple") (erndis = 10)
    if (invca == "I work and get CA" || invca == "Partner works & gets CA") (erndis = 20)
    if (invca == "Both of us get a CA" && empinc >= 20) (erndis = 20) // prog detects if one of 'em is working & gives £20

    if (C_ESA_Comp == "Work-rel" || C_ESA_Comp == "Supp" || P_ESA_Comp == "Work-rel" || P_ESA_Comp == "Supp") (erndis = 20)
    //    '*** award it on any of 4 ESA components
    if (dsable == "Yes" && C_ESA_Phase == "None" && P_ESA_Phase == "None") (erndis = 20)
    //    '*** award it on dsable if no ESA claim - this works ok cos ESA_Phases will surely get set
    if (dsable == "Yes" && P_ESA_Phase == "Ass") (erndis = 20)
    //  *** award it on dsable if P_ESA claim is "Ass" ! Dec '08 fix

    if (cb > 0 && Status == "single") (erndis = 25)
    //End Sub '***** End of discal1 ********)

    // ******** Sub discal3() '*** New WTC style disregard here
    //'****** we need to run  discal3()  first
    //    '***'***'***'*** ok 04 & let's be sure to award it only once   ***************************
    if (age > 24 && WkWkhours == "30plus") { erndis = (erndis + Cr30hours) }  // ok 
    else if (cb > 0 && WkWkhours == "30plus") { erndis = (erndis + Cr30hours) }  // ok 
    else if (cb > 0 && WkWkhours == "16plus") { erndis = (erndis + Cr30hours) }  // ok 
    else if (dsable == "Yes" && WkWkhours == "30plus" && C_ESA_Phase == "None" && P_ESA_Phase == "None") { erndis = (erndis + Cr30hours) }  // ok 
    else if (dsable == "Yes" && WkWkhours == "16plus" && DisWkWkhours == "16plus" && C_ESA_Phase == "None" && P_ESA_Phase == "None") { erndis = (erndis + Cr30hours) }  // ok 
    else if (Back2Wk50 == "Yes" && WkWkhours == "16plus") { erndis = (erndis + Cr30hours) }  //   ok
    else if (WkWkhours == "30plus" && C_ESA_Comp == "Work-rel") { erndis = (erndis + Cr30hours) }
    else if (WkWkhours == "30plus" && C_ESA_Comp == "Supp") { erndis = (erndis + Cr30hours) }
    else if (WkWkhours == "30plus" && P_ESA_Comp == "Work-rel") { erndis = (erndis + Cr30hours) }
    else if (WkWkhours == "30plus" && P_ESA_Comp == "Supp") { erndis = (erndis + Cr30hours) }
    else if (WkWkhours == "16plus" && DisWkWkhours == "16plus" && C_ESA_Comp == "Work-rel") { erndis = (erndis + Cr30hours) }
    else if (WkWkhours == "16plus" && DisWkWkhours == "16plus" && C_ESA_Comp == "Supp") { erndis = (erndis + Cr30hours) }
    else if (WkWkhours == "16plus" && DisWkWkhours == "16plus" && P_ESA_Comp == "Work-rel") { erndis = (erndis + Cr30hours) }
    else if (WkWkhours == "16plus" && DisWkWkhours == "16plus" && P_ESA_Comp == "Supp") { erndis = (erndis + Cr30hours) }

    if (empinc < erndis) (erndis = empinc)

    //  ********  Sub discal2()
    if (Kcare > 0) (erndis = (erndis + Kcare))

    if (exportTC > 0 && empinc < erndis) { kkbal = (erndis - empinc) }
    // If FormWof.exportTC > 0 And empinc < erndis Then kkbal = erndis - empinc

    Uncash = (Uncash - kkbal)
    // Uncash = Uncash - kkbal  '**** take off unused balance of Childcare costs !!!  !!!

    if (Uncash < 0) (Uncash = 0)
    //If Uncash < 0 Then Uncash = 0   '** don't let Uncash go below 0

    if (empinc < erndis) (erndis = empinc)
    //   ***** End of discal2  ********

    koala = (empinc - erndis + cb + Uncash - Unearndis + canberra - Pout)
    if (age > 59 && cb > 0) (koala = koala - cb)
    // '*** TEMP new line Sp '09
    if (age < 60 && document.aspnetForm.Oct09List.selectedIndex == 1 && cb > 0) (koala = koala - cb)

    koala52 = koala

    aa = (kidzAA / 1 + adaa / 1)
    premaa = (prem + aa)

    //'**** N.B. 
    //'**** so that 
    if (frewks > 0) { koala = (koala * 52); koala = (koala / frewks); premaa = (premaa * 52); premaa = (premaa / frewks) }

    exinc = (koala - premaa)

    //          '****  HBCALC ***********************
    if (exinc <= 0) (hb = maxhb)
    if (exinc >= 0) { taper = (exinc * 0.65); hb = (maxhb - taper) }
    if (hb < 0) (hb = 0)

    //  -----------  VB form 11 LOGIC -----------------------------------------------
    CTexinc = exinc //'*** set CTexinc initially
    if (frewks > 0) { CTkoala = koala52; CTpremaa = (premaa / 52); CTpremaa = (CTpremaa * frewks); CTexinc = (CTkoala - CTpremaa) }

    // '****  CT CALC ***********************
    if (CTexinc <= 0) (ctb = CTwam)
    if (CTexinc >= 0) { taper = (CTexinc * 0.2); ctb = (CTwam - taper) }
    if (ctb < 0) (ctb = 0)

    // '****  Alt CT CALC ( SAR )  ***********************
    if (ad2inc == "25%") { altctb = (altCTwam * 0.25) }
    if (ad2inc == "15%") { altctb = (altCTwam * 0.15) }
    if (ad2inc == "7.5%") { altctb = (altCTwam * 0.075) }
    if (ad2inc == "none") (altctb = 0.00)
    if (rnt > 0) (altctb = 0.00)
    if (Status == "couple") (altctb = 0.00)

    //'******* look out 
    if (canberra > 40) { (hb = 0); (ctb = 0) }

    if (age > 59 && canberra > 20) { (hb = 0); (ctb = 0) }

    s = cb; Formatit(s); cb = s     //***** Format & display cb
    document.aspnetForm.cbbox.value = cb

    // Could do a really neat 2 dec place format on numbers here but sadly older browsers give error. 
    // Needs J Scri ipt 1.5, IE 5.5 or Netscape - 6.0 - 'cos toFixed function not supported earlier.
    //    e.g. would be .....  hb = (hb.toFixed (2))  

    //    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/      write OUTPUT answers to screen   /*/*/*/*/*/*/*/*
    s = premaa; Formatit(s); premaa = s     //***** Format & display AA
    document.aspnetForm.aabox.value = premaa

    //  rounding nwi  
    s = koala; Formatit(s); koala = s     //***** Format & display nwi
    document.aspnetForm.nwibox.value = koala

    s = exportTC; Formatit(s); exportTC = s     //***** Format & display exportTC
    document.aspnetForm.TCbox.value = exportTC     //annTC  

    //  rounding hb !  
    s = hb; Formatit(s); hb = s     //***** Format & display hb
    document.aspnetForm.hbbox.value = hb

    //  rounding ctb !  
    s = ctb; Formatit(s); ctb = s     //***** Format & display ctb
    document.aspnetForm.ctbbox.value = ctb

    //  rounding altctb !  
    s = altctb; Formatit(s); altctb = s     //***** Format & display altctb
    document.aspnetForm.sarbox.value = altctb   //    //   document.aspnetForm.rambox.value     //    

}
//  *********  end of Calcfunc *****************
// *****  ************************** *************************

function subfunWTC_OK() //***  
{
    maxTC = 1890
    if (Status == "couple" || cb > 0) (maxTC = maxTC + 1860)
    if (Status == "couple" && cb < 1 && document.aspnetForm.Back50List.selectedIndex == 1 && WkWkhours == "16plus") (maxTC = maxTC - 1860)
    // '*** but take the blipping thing  off again in this one circo

    if (WkWkhours == "30plus") (maxTC = maxTC + 775)
    else if (BothWkhours == "30ish" && DisWkWkhours == "16plus") (maxTC = maxTC + 775)
    else if (BothWkhours == "30ish" && cb > 0) (maxTC = maxTC + 775)

    if (dsable == "Yes" && DisWkWkhours == "16plus") (maxTC = maxTC + 2530)
    if (AdEnhdsP == "Yes" || document.aspnetForm.DsAAHRList.selectedIndex == 1) (maxTC = maxTC + 1075)
    //   '*** NB New field DsAAHRList to pick up adult on Attendance allowance(Higher Rate) in WTC
    // '* can't rely on EnhDP for a true result !! Added AdEnhdsP setter (Y/N) at Screen 4 ok 
    if (age >= 50 && document.aspnetForm.Back50List.selectedIndex == 1 && WkWkhours == "30plus") (maxTC = maxTC + 1935)
    if (age >= 50 && document.aspnetForm.Back50List.selectedIndex == 1 && WkWkhours == "16plus") (maxTC = maxTC + 1300)
    //'*** want to give Kcare tax cred for WTC *** Kcarekids  is no. of potential Kcare kids
    KcareFees = (document.aspnetForm.kcarebox.value / 1) // ****'get actual Fees paid and peg them
    if (Kcarekids == 1 && KcareFees > 175) (KcareFees = 175)
    if (Kcarekids > 1 && KcareFees > 300) (KcareFees = 300)
    if (Kcarekids == 0) (KcareFees = 0)
    KcareFees = (KcareFees * 0.8)   // '*** then award 80% of them
    KcareFees = (KcareFees * 52)   // '*** then annualize them
    maxTC = (maxTC + KcareFees)      //         '*** add Kcare tax cred to TC !!!
}
//  *********  end of function subfunWTC_OK  **** End Sub '***  SWING back to  TC_sub to aggregate  WTC/CTC calc -

// *****  ************************** *************************
function do_PC() //***  since Autumn 03 
{
    //'*****      start off with the guarantee credit ---  weekly standard amounts
    if (Status == "couple")
    { (AppMig = 198.45) }
    else
    { (AppMig = 130) }

    if (Status == "single" && dissev == "One") (AppMig = AppMig + dssevprem1)
    if (Status == "couple" && dissev == "Both") (AppMig = AppMig + dssevprem2)
    if (Status == "couple" && dissev == "One") (AppMig = AppMig + dssevprem1)
    if (document.aspnetForm.invcalist.selectedIndex > 1) (AppMig = AppMig + careprem1) // give 1 careprem
    if (invca == "Both of us get a CA") (AppMig = AppMig + careprem2)
    //'*****      2nd leg of the guarantee credit ---  weekly income
    GCinco = 0; GCerndis = 0; Other = 0; SC = 0
    GCinco = (GCinco + canberra)
    GCinco = (GCinco + empinc)
    if (Status == "single" && empinc > 5) (GCerndis = 5)
    if (Status == "couple" && empinc > 10) (GCerndis = 10)
    if (dsable == "Yes" && empinc > 20) (GCerndis = 20)
    if (invca == "I work and get CA" || invca == "Partner works & gets CA") (GCerndis = 20)  // gives £20 CA earners dis
    if (invca == "Both of us get a CA" && empinc >= 20) (GCerndis = 20) //prog detects if work & gives £20

    GCinco = (GCinco - GCerndis)

    Other = Uncash
    if (Other < 0) (Other = 0)
    GCinco = (GCinco + Other)
    SCinco = GCinco // declared here 
    if (annTC > 0) (GCinco = GCinco + (annTC / 365 * 7))

    GC = (AppMig - GCinco) // declared here 
    SCinco = (SCinco - maintinc)
    SCthreshSing = 96; SCmaxSing = 20.4; SCthreshCoup = 153.4; SCmaxCoup = 27.03

    if (Status == "single" && SCinco >= SCthreshSing && SCinco <= AppMig) { Diff = (SCinco - SCthreshSing); SC = (Diff * 0.6) }
    if (Status == "single" && SCinco > AppMig) { Diff = (SCinco - AppMig); percent40 = (Diff * 0.4); SC = (SCmaxSing - percent40) }
    if (Status == "couple" && SCinco >= SCthreshCoup && SCinco <= AppMig) { Diff = (SCinco - SCthreshCoup); SC = (Diff * 0.6) }
    if (Status == "couple" && SCinco > AppMig) { Diff = (SCinco - AppMig); percent40 = (Diff * 0.4); SC = (SCmaxCoup - percent40) }
    if (GC < 0) (GC = 0)
    if (age < 65 || SC < 0) (SC = 0)
    if (Status == "single" && SC > SCmaxSing) (SC = SCmaxSing)
    if (Status == "couple" && SC > SCmaxCoup) (SC = SCmaxCoup)

    WPC = (GC + SC)

    //  rounding up WPC here !  
    Low = (WPC * 100); High = (WPC * 100 + 0.5)
    tar1 = parseInt(Low); tar2 = parseInt(High);
    if (tar2 > tar1) (WPC = WPC + .01)
    WPC = (WPC * 100)    //********* 2 dec place rounding
    WPC = parseInt(WPC); WPC = (WPC / 100)

    //  *********  Quickly show WPC value in Isplus25box --->> v useful   
    //  document.aspnetForm.Isplus25box.value =   WPC  

}
//  *********  end of function do_PC()   **** End Sub '***  SWING back to  aggregate  
// *****  ************************** *************************
//     valid8.js  ( JavaScript global form validation & checking routines ) 
// ***** WillMaster's  currency formatting function - source acknowledged  ********
//   Give the function an amount and it will return that amount rounded to the nearest hundredth and with two digits 
//   following a decimal point. Caution .... use for final display only !

function Formatit(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}
// end of function Formatit()


// In future Could do a really neat Final 2 dec place format on numbers but sadly older browsers will give error. 
// Needs JavaScript 1.5, JScript - 5.5, IE 5.5 or Netscape - 6.0 - 'cos toFixed function not supported earlier.
//     For example .............. hb = (hb.toFixed (2))  
//*****************************************************

////////// only inbound funcs are       NoDec  & bounds    ///////////////////////////////////////////////////
//*****   all we really want to do is stop non-numeric values and warn on Negative Values and certain decimals ! 

//*****   KB func to trap decimals where we only want whole numbers - NB Many fields will be ok to have decimals 
function NoDec(nx)  //  NODEC 
{
    var zap = false;
    var f = decapitate(nx.value);
    f = money(f);

    abcint = parseInt(nx.value);
    if (abcint < nx.value) {
        zap = true;
        alert("Whole numbers only please");
    }
    if (zap)
    { reset(nx); return false; }
    //********* zap blanks the offending field ****************************
}

function money(f) // ********** Need this one ***********************
{ if (f == "") f = 0.0; return f - 0.0; }

function reset(e)   // ********** Need this one ***********************
{ e.value = ""; e.focus(); }
// **********  Yes isNaN is a JS func to check for a numeric value
function check_number(x) { return !(isNaN(x)); }

function       // ********** Need this one ***********************
decapitate(s) {
    if (s.charAt(0) == "0")
        s = s.slice(1);
    return s;   //******     removes leading zeros 
}

// **************  BIGVAL ************************************************
// *** This function checks for NON NUMERIC  +  Negative Values   
function bounds(e) {
    var zap = false;
    var f = decapitate(e.value);
    f = money(f);
    if (!check_number(f)) //***  Calls isNaN JS func to check for a numeric value
    {
        zap = true;
        alert("Inappropriate input, please enter a number");
    }

    if (f < 0.0)  //************ LOOKING FOR NEG VAL ******************888   
    {
        zap = true;
        alert("A negative amount is not acceptable here")
    }

    if (zap) {
        reset(e);
        return false;
    }
    else
        return true;
} //****************** END of BIGVAL ********************************

// END OF BENEFIT CALCULATOR CODE