// Open the popup window and show the content.
function HelpPop(url, w, h) {
 var oWnd = FindPopupWindow();
 oWnd.setUrl(url);
 oWnd.setSize(w, h);
 oWnd.center();
 oWnd.show();
}

// Close the popup window and set the content to blank.
function HelpClose() {
 var oWnd = FindPopupWindow();
 oWnd.close();
 oWnd.setUrl('/cms/html/blank.html');
}

function formatCurrency(strValue)
{
 strValue = strValue.toString().replace(/\$|\,/g,'');
 dblValue = parseFloat(strValue);

 blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
 dblValue = Math.floor(dblValue*100+0.50000000001);
 intCents = dblValue%100;
 strCents = intCents.toString();
 dblValue = Math.floor(dblValue/100).toString();
 if(intCents<10)
  strCents = "0" + strCents;
 for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
  dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
  dblValue.substring(dblValue.length-(4*i+3));
 return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}

function checknumber(x){
    var anum=/(^\d+$)|(^\d+\.\d+$)/;
    if (anum.test(x))
        return true;
    else
        return false;
}
function innum(obj)
{
    if(checknumber(obj.value))
    {
        return true;
    }
    else
    {
        obj.value = "0";
        return false;
    }
}
function compute(debug)
{
    var income = document.getElementById('income');
    var kids1 = document.getElementById('kids1');
    var kids2 = document.getElementById('kids2');
    var kids3 = document.getElementById('kids3');
    var newborn = document.getElementById('newborn');
    var estimate = document.getElementById('estimate');
//validate fields
    innum(kids1);
    innum(kids2);
    innum(kids3);
    var first = 0.00;
    var second = 0.00;
    var third = 0.00;
    var inwork = 3120.00;
    var childtax = 0.00;
    var ic = parseInt(income.value);
    var k1 = parseInt(kids1.value);
    var k2 = parseInt(kids2.value);
    var k3 = parseInt(kids3.value);
    var nb = 0.00;
    if(newborn.checked) nb = 1200.00;
    var nochildren = parseInt(k1) + parseInt(k2) + parseInt(k3);
if(debug) {alert('starting values' + " : " + 'kid1;' + k1 + ' kid2;' + k2 + ' kid3;' + k3 + ' nb;' + nb + ' inwork;' + inwork);}
    if(nochildren > 3) childtax = 780.00;
    if((parseInt(k1) > 0) | (parseInt(k2) > 0) | (parseInt(k3) > 0))
    {
    // first kid value
        if(parseInt(k1) > 0)
        {
            first = 4953.53;
            k1--;
        }
        else if (parseInt(k2) > 0)
        {
            first = 4275.66;
            k2--;
        }
        else if (parseInt(k3) > 0)
        {
            first = 4275.66;
            k3--;
        }
    // second kid values
        if(parseInt(k1) > 0)
        {
            second = second + 4432.11 * k1;
        }
        if (parseInt(k2) > 0)
        {
            second = second + (3389.26 * k2);
        }
        if (parseInt(k3) > 0)
        {
            second = second + (2972.12 * k3);
        }
        
        var entitlement = first + second + nb + childtax + inwork;
        var incomeabatement = 0.00;
        if(parseInt(ic) > 35000)
            incomeabatement = (parseInt(ic) - 35000) * 0.2;
        var total = 0.00;
        if(entitlement > incomeabatement)
            total = entitlement - incomeabatement;
if(debug) {alert('totals' + " : " + 'total;' + total + ' entitlement;' + entitlement + ' abatement;' + incomeabatement);}

        estimate.innerHTML = formatCurrency(total);
    }
}

function DisableEnterKey() {
    if (event.keyCode == 13) {
        return false;
    }
    else {
        return true;
    }
}
