/* ---------------------------
' (C) Copyright 2008
' Niro Solutions Pty Limited
' ABN 89 106 967 140
' ---------------------------
' Revision: 1.0.0
' Author: ZhongBo Sun
' Modtime: 31/10/08
' --------------------------- */
var INVESTMENT_RATE = 0.06
var AWOTE_RATE = 0.04
var CEASE_AGE_MAX = 65
var CEASE_AGE_MIN = 15
var RET_AGE = 65
var SUP_SCI_PCTG = 10;
var BEN_SCI_PCTG = 75;
var MAX_SCI_PCTG = 85;
var SCI_UNIT_AMT = 100
var MIN_SCI_INCOME = 0;
var MAX_SCI_INCOME1 = 60000;
var MAX_SCI_INCOME2 = 72000;
var MAX_SCI_INCOME3 = 96000;
var MAX_SCI_TOTAL = 240000;
var MAX_COVER = 2000000;
var limit=false
var limitsci=false
var salary=false
var step1=true
var noOfTabs = 3

function myRound(number,dp)
{
var ret
ret = Math.round(number*Math.pow(10,dp))/Math.pow(10,dp);
ret = Math.floor(ret);
var ls = ret.toString()
var li = ls.lastIndexOf(".")
if(li<0)
	return (""+ret+".0");
else
    return ret
}


function YouOrPartner()
{
	var f=document.forms.frmName
	if(f.rdCalc[0].checked)
	{
	    f.txtAge2.className=''
	    f.txtAge2.disabled = false
	    f.txtIncome2.className=''
	    f.txtIncome2.disabled = false
	    document.getElementById("tdIncome").className='';
	    f.txtIPCover2.className=''
	    f.txtIPCover2.disabled = false
	    document.getElementById("tdIP").className='';
	    
	    document.getElementById("tdPart").className='TitleBigBlue';
	    document.getElementById("chkyou").style.display='';
	    document.getElementById("chkpar").style.display='';
	}
	else
	{
	    f.txtAge2.className='InputBorderGray'
	    f.txtAge2.value=''
	    f.txtAge2.disabled = true
	    f.txtIncome2.className='InputBorderGray'
	    f.txtIncome2.value=''
	    f.txtIncome2.disabled = true
	    document.getElementById("tdIncome").className='CellDimmed';
	        
	    f.txtIPCover2.className='InputBorderGray'
	    f.txtIPCover2.value=''
	    f.txtIPCover2.disabled = true
	    document.getElementById("tdIP").className='CellDimmed';
	    
	    document.getElementById("tdPart").className='TitleBigBlueDim';
	    document.getElementById("chkpar").style.display='none';
	    if(!f.rdBus[0].checked){
	    document.getElementById("chkyou").style.display='';
	    }
	   	    
	}
	//showDeps()
	//showDeps2()
}

function Business()
{
	var f=document.forms.frmName
	if(f.rdBus[0].checked)
	{
	    document.getElementById("trBLoan").style.display='';
	    document.getElementById("trBRent").style.display='';
	    document.getElementById("trBElec").style.display='';
	    document.getElementById("trBStaff").style.display='';
	    document.getElementById("trBCost").style.display='';
	    document.getElementById("trBHE").style.display='';
	    document.getElementById("trBCover").style.display='';
	    document.getElementById("trBCoverLS").style.display='';
	    document.getElementById("FreqResults").innerHTML='Display Income protection and Business expenses insurance cover per'
	    document.getElementById("tab3H").innerHTML='<b>INCOME PROTECTION AND BUSINESS EXPENSES INSURANCE COVER</b>'
	    document.getElementById("chkbus").style.display='';
	     document.getElementById("chkyou").style.display='';
	    
	}
	else
	{
	    document.getElementById("trBLoan").style.display='none';
	    document.getElementById("trBRent").style.display='none';
	    document.getElementById("trBElec").style.display='none';
	    document.getElementById("trBStaff").style.display='none';
	    document.getElementById("trBCost").style.display='none';
	    document.getElementById("trBHE").style.display='none';
	    document.getElementById("trBCover").style.display='none';
	    document.getElementById("trBCoverLS").style.display='none';
	    document.getElementById("FreqResults").innerHTML='Display Income Protection insurance cover per'
	    document.getElementById("tab3H").innerHTML='<b>INCOME PROTECTION INSURANCE COVER</b>'
	    document.getElementById("chkbus").style.display='none';
	    
	    	   	    
	}
	//showDeps()
	//showDeps2()
}

function isValidValueDefault2(field, sMsg)
{
	var ret = false;
	
	if (field.value == ""){
	    field.value = "0"
	}
	if(isInt(field.value))
	{
	    if (field.value >= 0 && field.value <= 2000000){
		    ret=true
		}
		else
		{
		//switchToDIV(field)
		
		alert(sMsg)
		select(field)
		}
	}
	else
	{
	    //switchToDIV(field)
		
		alert(sMsg)
		select(field)
		
	}
	return ret;
}
function isInt(v)
{
var ret=false;
var int1 = /^\d{1,8}$/;
var reg=new RegExp(int1);
var ret = reg.test(v);
if(ret)
	ret=true;
return ret
}

function isFloat(v)
{
var ret=false;
var float1 = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
var reg=new RegExp(float1);
var ret = reg.test(v);
if(ret)
	ret=true;
return ret
}

function getCmbIndex(cmb1,firstLetter)
{
	for(i=0;i<cmb1.options.length;i++)
	{
		if(((cmb1.options[i].text).charAt(0)).toUpperCase()==firstLetter.toUpperCase())
			return i;
	}
	return --i;//just in case
}
function getSafeFieldValue(field)
{
	var x=0
	if(jsLRTrim(field.value)=="" || isNaN(jsLRTrim(field.value)))
		x=0
	else
		x = parseInt(field.value,10)
	return x;
}

function getSafeFieldValueF(field)
{
	var x=0
	if(jsLRTrim(field.value)=="" || isNaN(jsLRTrim(field.value)))
		x=0
	else
		x = parseFloat(field.value,10)
	return x;
}

function jsLRTrim(s)
{
	return s.replace(/(^\s+)|(\s+$)/g, "");
}


function setCursorHand(e)
{
	e.style.cursor = "pointer";
}
function setActiveTab(e,s)
{
	if(e.className == 'active')
		e.className='active hov'
	else
		e.className='hov'
	setCursorHand(e);
	showStatus(s)
}
function getSafeDecimalFieldValue(field)
{
	var x=0
	if(jsLRTrim(field.value)=="" || isNaN(jsLRTrim(field.value)))
		x=0
	else
		x = parseFloat(field.value)
	return x;
}
function setLimits()
{
	limit = false
	limitsci=false
    salary=false
    fcs=true
}
function validAge(f){ 

    var sAge = getSafeFieldValue(f.txtAge)
          
    if (sAge < CEASE_AGE_MIN || sAge >= CEASE_AGE_MAX) {
            alert("You have entered an age of less than 16 or 65 and over. Income Protection is generally not available to those under age 16 or 65 and over.")
            select(f.txtAge)
            return false;
    }
    
    if(f.rdCalc[0].checked)
	{
	    var sAge = getSafeFieldValue(f.txtAge2)
          
    if (sAge < CEASE_AGE_MIN || sAge >= CEASE_AGE_MAX) {
            alert("You have entered an age of less than 16 or 65 and over. Income Protection is generally not available to those under age 16 or 65 and over.")
            select(f.txtAge2)
            return false;
    }
	}
                
    return true;
}
function validSalary(f){ 
    var ANN_INCOME = (getSafeDecimalFieldValue(f.txtIncome)* f.cmbIncomeFreq.value)
    //window.alert(ANN_INCOME)
    
    if (validSCI()){
    if (ANN_INCOME < MIN_SCI_INCOME){
        alert("To be eligible to apply for TSC cover with CSRF Super the amount of total income must be greater than $16,000 per annum on an ongoing basis. ")
        select(f.txtIncome)
        return false;
    }
    
    }
        
    return true;
}
function get_max(f, cmb, _max)
{
	return Math.floor(_max/cmb.value)
}
function limitNoDecimalPlaces(ns)
{
// find if '.' entered
var li = ns.indexOf(".");
var n=2;
//window.alert(li)
if(li<0)
{
	if(ns.length > (n+2))
		return ns.substring(0,(n+2))
}
else
{
	var s1 = ns.substring(0, li)
	var s2 = ns.substring(li+1,(ns.length))
	if(s2.length > n)
		return s1+"."+s2.substring(0,n)
}
}
function parseNeg(n,p){
    var mylbl
    
    switch (p){
        case 0:
            mylbl="lblDeathS";
            mylbl1="lbl1"
            break;
        case 1:
            mylbl="lblDeathS2";
            mylbl1="lbl1"
            break;
        case 2:
            mylbl="lblDeathSB"
            mylbl1="lbl2"
            break;
    }
    
    if (n < 0){
        document.getElementById(mylbl1).innerHTML="<b>Excess</b>"
        document.getElementById(mylbl).style.color = "black";
        n=n*-1;
        return "-$"+removeDecimalPart(addCommas(n.toLocaleString()))+"&nbsp;";
    }
    else{
        if(n==0){
            document.getElementById(mylbl1).innerHTML="<b>Shortfall (additional cover required)</b>"
            document.getElementById(mylbl).style.color = "black";
        }
        else{
            document.getElementById(mylbl1).innerHTML="<b>Shortfall (additional cover required)<b/>"
            document.getElementById(mylbl).style.color = "red";
        }

    return "$"+removeDecimalPart(addCommas(n.toLocaleString()))+"&nbsp;";
    }
}

function calcResults()
{
var f=document.forms.frmName

var reqDeath = getSafeFieldValue(f.txtIncome) * parseInt(f.cmbSalFreq.value) * 0.75;
var reqDeath2 = getSafeFieldValue(f.txtIncome2) * parseInt(f.cmbSalFreq.value) * 0.75;
var reqDeathB = getSafeFieldValue(f.txtBLoan) + (getSafeFieldValue(f.txtBRent)* parseInt(f.cmbRentFreq.value)) + (getSafeFieldValue(f.txtBElec)* parseInt(f.cmbElecreq.value)) + (getSafeFieldValue(f.txtBStaff)* parseInt(f.cmbStaffFreq.value)) + getSafeFieldValue(f.txtBCost)
 
if ( reqDeath > MAX_SCI_TOTAL){
        reqDeath = MAX_SCI_TOTAL
        alert("The maximum Income protection cover available is generally $20,000  a month. Your cover has been restricted accordingly.")
        //limitsci = true
        }
        
if ( reqDeath2 > MAX_SCI_TOTAL){
        reqDeath2 = MAX_SCI_TOTAL
        alert("The maximum Income protection cover available is generally $20,000  a month. Your cover has been restricted accordingly.")
        //limitsci = true
        }
   
reqDeath = removeDecimalPart(myRound(reqDeath / parseInt(f.cmbResultsFreq.value),0))
reqDeath2 = removeDecimalPart(myRound(reqDeath2 / parseInt(f.cmbResultsFreq.value),0))
reqDeathB = removeDecimalPart(myRound(reqDeathB / parseInt(f.cmbResultsFreq.value),0))





var othIP = getSafeFieldValue(f.txtIPCover) * parseInt(f.cmbIPFreq0.value) ;
var othIP2 = getSafeFieldValue(f.txtIPCover2) * parseInt(f.cmbIPFreq0.value);
var othIPB = (getSafeFieldValue(f.txtBCover) * parseInt(f.cmbExistFreq.value)) + getSafeFieldValue(f.txtBCoverLS);

othIP = removeDecimalPart(myRound(othIP / parseInt(f.cmbResultsFreq.value), 0))
othIP2 = removeDecimalPart(myRound(othIP2 / parseInt(f.cmbResultsFreq.value), 0))
othIPB = removeDecimalPart(myRound(othIPB / parseInt(f.cmbResultsFreq.value), 0))

var shortDeath = (reqDeath - othIP)
var shortDeath2 = (reqDeath2 - othIP2)
var shortDeathB = (reqDeathB - othIPB)
var sPeriod = getPeriodWords(parseInt(f.cmbResultsFreq.value))
document.getElementById("lblDeathRFreq").innerHTML="Total required cover per " + sPeriod + ""
document.getElementById("lblDeathOFreq").innerHTML="Current cover per " + sPeriod + ""
document.getElementById("lblDeathRFreq2").innerHTML="Total required cover per " + sPeriod + ""
document.getElementById("lblDeathOFreq2").innerHTML="Current cover per " + sPeriod + ""

document.getElementById("lblDeathR").innerHTML=parseNeg(reqDeath,0);
document.getElementById("lblDeathO").innerHTML=parseNeg(othIP,0);
document.getElementById("lblDeathS").innerHTML="<b>" + parseNeg(shortDeath,0) + "</b>";

document.getElementById("lblDeathRH").style.display = "none";
document.getElementById("lblDeathR2").style.display = "none";
document.getElementById("lblDeathO2").style.display = "none";
document.getElementById("lblDeathS2").style.display = "none";

document.getElementById("lblDeathRB").style.display = "none";
document.getElementById("lblDeathOB").style.display = "none";
document.getElementById("lblDeathSB").style.display = "none";
document.getElementById("tblbus").style.display = "none";

//alert(f.rdCalc[1].checked)
if (f.rdCalc[0].checked){

document.getElementById("lblDeathRH").style.display = "";
document.getElementById("lblDeathR2").style.display = "";
document.getElementById("lblDeathO2").style.display = "";
document.getElementById("lblDeathS2").style.display = "";

document.getElementById("lblDeathR2").innerHTML=parseNeg(reqDeath2,1);
document.getElementById("lblDeathO2").innerHTML=parseNeg(othIP2,1);
document.getElementById("lblDeathS2").innerHTML="<b>" + parseNeg(shortDeath2,1) + "</b>";
var txtY
var txtP
if (shortDeath<0){
    txtY = "Excess";
}
else{
    txtY="Shortfall"
}
if (shortDeath2<0){
    txtP = "/Excess";
}
else{
    txtP="/Shortfall"
}



if (shortDeath*shortDeath2<0||(shortDeath*shortDeath2==0&&shortDeath!=shortDeath2)){
    document.getElementById("lbl1").innerHTML="<b>"+txtY+txtP+"</b>";
   
}
}

if (f.rdBus[0].checked){

document.getElementById("lblDeathRB").style.display = "";
document.getElementById("lblDeathOB").style.display = "";
document.getElementById("lblDeathSB").style.display = "";
document.getElementById("tblbus").style.display = "";

document.getElementById("lblDeathRB").innerHTML=parseNeg(reqDeathB,2);
document.getElementById("lblDeathOB").innerHTML=parseNeg(othIPB,2);
document.getElementById("lblDeathSB").innerHTML="<b>" + parseNeg(shortDeathB,2) + "</b>";
}

c1Req = reqDeath
c1Have = othIP

c12Req = reqDeath2
c12Have = othIP2

c1BReq = reqDeathB
c1BHave = othIPB

//alert(c1BHave)

updateGraph()

var sWho
var sBus

if(f.rdCalc[0].checked){
    sWho = "0"
 }else{
    sWho = "1"
}

if(f.rdBus[0].checked){
    sBus = "0"
 }else{
    sBus = "1"
}


if (window.parent.dataY1[0]!=sWho&&window.parent.dataY1[0]!=undefined&& (window.parent.dataY1[100]==1||window.parent.dataY1[200]==1||window.parent.dataY1[300]==1) ){
    var answer = confirm ("When updating your details on this calculator you have changed a field that has been prepopulated in another calculator. If you proceed with this change the other calculators will need to be opened and recalculated to display the revised amounts. Would you like to proceed?")
                if (!answer){
                
                return false}
     window.parent.reStartSlide(1,2);
      window.parent.reStartSlide(3,2);
       window.parent.reStartSlide(2,2);
       window.parent.dataY1[100]=0
       window.parent.dataY1[200]=0
       window.parent.dataY1[300]=0
    
}

dataY[0] = sWho
dataY[1] = getSafeFieldValue(f.txtAge)

dataY[18] = f.cmbSalFreq.value
dataY[19] = getSafeFieldValue(f.txtIncome)
dataY[20] = getSafeFieldValue(f.txtIPCover)
dataY[21] = f.cmbResultsFreq.value
dataY[22] = sBus
dataY[23] = getSafeFieldValue(f.txtBLoan)
dataY[24] = getSafeFieldValue(f.txtBRent)
dataY[25] = getSafeFieldValue(f.txtBElec)
dataY[26] = getSafeFieldValue(f.txtBStaff)
dataY[27] = getSafeFieldValue(f.txtBCost)
dataY[36]= f.cmbExistFreq.value
dataY[37]=getSafeFieldValue(f.txtBCover)
dataY[38]=getSafeFieldValue(f.txtBCoverLS)
dataY[39]=f.cmbIPFreq0.value
dataY[40]=f.cmbRentFreq.value
dataY[41]=f.cmbElecreq.value
dataY[42]=f.cmbStaffFreq.value


dataY[400] = 1
dataY[401] = reqDeath
dataY[402] = othIP
dataY[403] = shortDeath
dataY[404] = reqDeathB
dataY[405] = othIPB
dataY[406] = shortDeathB
dataY[504]=sWho 
dataP[0] = sWho
dataP[1] = getSafeFieldValue(f.txtAge2)

dataP[18] = f.cmbSalFreq.value
dataP[19] = getSafeFieldValue(f.txtIncome2)
dataP[20] = getSafeFieldValue(f.txtIPCover2)
dataP[21] = f.cmbResultsFreq.value



dataP[400] = 1
dataP[401] = reqDeath2
dataP[402] = othIP2
dataP[403] = shortDeath2
var txtYP
txtYP=document.getElementById("lbl1").innerHTML
dataY[407] = txtYP
txtYP=document.getElementById("lbl2").innerHTML
dataY[408] = txtYP

window.parent.saveData(dataY, dataP)
return true
}
function cursorHU(e)
{
	e.style.cursor = "pointer";
	e.className='hrHoover1';
}
function cursorHU0(e)
{
	e.className='hrHoover0';
}


function getPeriodWords(Val)
{
switch (Val)
		{
		    case 26 :
				return "fortnight"
				break;
			case 12 :
				return "month"
				break;
			case 1 :
				return "year"
				break;
			case 52 :
				return "week"
				break;
			
		}

}

function limitNoDecimalPlaces2(field, n)
{
// this new version will allow non-decimal number of any length
// find if '.' entered
var li = (field.value).indexOf(".")
if(li<0)
	;
else
{
	var s1 = (field.value).substring(0, li)
	var s2 = ((field.value).substring(li+1,(field.value).length))
	if(s2.length > n)
		field.value=s1+"."+s2.substring(0,n)
}
}
function myMathCeil(number,dp)
{
return myRound((Math.ceil(number*Math.pow(10,dp)))/Math.pow(10,dp),dp)
}
function myMathFloor(number,dp)
{
return myRound((Math.floor(number*Math.pow(10,dp)))/Math.pow(10,dp),dp)
}
function adjustDecimalPoint(field)
{
	if((field.value).indexOf(".")==0)
		field.value="0"+(field.value)
}

function isDecimal_MinMax(field,min1,max1,prompt0,prompt1)
{
var v=field.value
//var int1 = /^\d{1,2}(\.\d{1,})?$/;
var int1 = /^\d{1,}(\.\d{1,})?$/;
var reg=new RegExp(int1);
var ret = reg.test(v);
if(ret && parseFloat(v) >= min1 && parseFloat(v) <= max1)
	return true;
else
{
	switchToDIV(field)
	//select(field)
	if(prompt1)
		alert(prompt0)
	return false;
}
}

function showDIV_CSRF(myDIV, noOfTabs)
{
	var setDIV = myDIV.getAttribute("ID");
	for(i=1;i<=noOfTabs;i++)
	{
		// disable all DIVs
		eval("divTab"+i+".style.display = 'none';")
		// change element's class to another class:
		eval("tabStep"+i+".className='tabOff';")
	}
	myDIV.style.display = "";
	for(i=1;i<=noOfTabs;i++)
	{
		eval("if(setDIV == 'divTab"+i+"')	{	tabStep"+i+".className='tabOn';}")
	}
}

function cursorH(e)
{
	e.style.cursor = "pointer";
}

function cursorA(e)
{
	e.style.cursor = "default";
}	

function doChanges(e)
{
	// get this 'e' element's table and set backgroundColor
	// of rows to cCol
	var rr = getMyTable(e);
	for(i=0;i<rr.rows.length-3;i++)
		rr.rows[i].style.backgroundColor=cCol
	e.style.backgroundColor = "#efefef";
}

function restore(e)
{
	var rr = getMyTable(e);
	for(i=0;i<rr.rows.length-3;i++)
		rr.rows[i].style.backgroundColor=cCol
	e.style.backgroundColor = cCol;
}

function select(field)
{
if((navigator.appName).toUpperCase()!="NETSCAPE")
{
	try
	{
		field.focus()
		field.select()
	}
	catch(e)
	{
		;//alert(e)
	}
}
}

function getMyDIV(x)
{
	// start with an element and go up through the tree trying to find
	// a <DIV> HTML element;
	// return a reference to an object of type <DIV>
	// or, if not found, return null
	if(x!=null && x.tagName!="DIV")
		return (getMyDIV(x.parentNode));
	else
		return x;
}

function getMyTable(x)
{
	if(x!=null && x.tagName!="TABLE")
		return (getMyTable(x.parentNode));
	else
		return x;
}

function switchToDIV(field)
{
	//var rr = getMyDIV(field);
	//if(rr.style.display=="none")
	//	showDIV_CSRF(rr,numberOfTabs)
}












function doChanges1(e)
{
	// get this 'e' element's table and set backgroundColor
	// of rows to cCol
	
	e.style.backgroundColor = "#efefef";
}
function restore1(e)
{
	
	e.style.backgroundColor = "white";
}

