<!--
var valid = "";

function loadAjax(){

google.setOnLoadCallback(onLoad);

google.load("prototype", "1.6");  
//google.load("scriptaculous", "1.8.2");
}

function onLoad(){
	
	//alert("load")
$("kiwisaverWidget").update(kiwisaverHTML);
//alert("ra")
var script = document.createElement("script");  
script.src = "http://www.huljich.co.nz/widget/validation.js";  
script.type = "text/javascript";  
document.getElementsByTagName("head")[0].appendChild(script);

}


function toggleForm(e){
	
	var ip = $$('#overlayMe input');

for (var i = 0; i < ip.length;i++){
	ip.invoke(e);
	}
	
var ip = $$('#overlayMe select');

for (var i = 0; i < ip.length;i++){
	ip.invoke(e);
	}
	
	}


function init(){
//disableForm();

//alert("init")
$("aspnetForm").reset();

valid = new Validation('aspnetForm');
//alert(valid);
//$('salarydiv').hide();
//$("overlayMe").style.display = "block";
//$("overlayMe").hide();


toggleForm("disable")

$$('#overlayMe select');

//$("sal").hide();
//$("contrib").hide();

}

var employed;
//

function calc(){
if (!valid.validate()){
//alert(valid.validate());
return false;
}

 var frm = $("aspnetForm");
 var incMulti = 1;
 var govmatchtaxcredit = 1042.86;
 var inflation = 0.00;
 
 if (employ == 0){
 //they have choosen contribution
 incMulti = 1;
 salary = frm.weekly.value;
 salary = (salary*52)/2*100
 var avgIncomeGrowth = 0; //no imcome growth if contributing 
 var emyerContrib = 0; //they have no employer contribution
  govmatchtaxcredit = 0;//no tax credit if not employed
  var emyeeContrib = 1;
 } 
 else if (employ == 1)
 { //they have choosen salary 
 var avgIncomeGrowth = 0.03;
 var emyerContrib = frm.emyercontrib.value;
 var emyeeContrib = frm.emyeecontrib.value;
 salary = frm.salary.value;
 
 } else if (employ == 2)
 {
  //they have choosen child
  alert("child")
 incMulti = 1;
 salary = frm.weekly.value;
 salary = (salary*52)/2*100
 var avgIncomeGrowth = 0.03;
 var emyerContrib = frm.emyercontrib.value;
 var emyeeContrib = frm.emyeecontrib.value;
 }

var emyeeContrib = frm.emyeecontrib.value;
var incomePerYear =  salary*incMulti;
var yearsContrib = 64-frm.age.value;

var age = frm.age.value-1;
 if (yearsContrib < 5){ // they must be in the scheme for at least 5 years
yearsContrib = 4;
}

//alert(yearsContrib);

frm.govmatchtaxcredit.value=Mathround(1042.86*yearsContrib);

var mortgageDiversion = 0.02; //if they choose mortgage diversion subtract 2% off fund return
var savAtRetirement = 0; 
var kickstart = 1000;//government kickoff
var lumpsum = 0;
 
 //alert(frm.joinFirst.options[frm.joinFirst.selectedIndex].value);
 if (frm.joinFirst.options[frm.joinFirst.selectedIndex].value == "yes"){
kickstart = 0;//if not joining for first ime them set 
joinFirst = true;
}else{
joinFirst = false;
}

 if (frm.lumpSum.value != ""){
 lumpsum = parseFloat(frm.lumpSum.value);
kickstart = lumpsum; //if they are transfering then they have more doe but do not have a kickstart
 } 
 else
 {
 kickstart = 1000
 lumpsum = 0
 }

var emplyTotalContrib = 0;
var emplrTotalContrib = 0;
var mortgageTotalContrib = 0;
var y = "";
//salert("iNCOME gROWTH"+avgIncomeGrowth);
var fundValue = frm.fundprofile.value;
fundValue = fundValue-inflation;
//alert(frm.diversion[0].checked)

/*if (frm.diversion.selectedIndex == 0 && employ != 0){
//alert("diversion")
emyeeContrib = emyeeContrib/2 ;//-mortgageDiversion;
mortgageDiversion = emyeeContrib;
$("mortgagecontribution").show();
} else {
frm.diversion.selectedIndex =1;

$("mortgagecontribution").hide();
}*/


var newIncomePerYear = incomePerYear;
var yearsContribValue = 0;
var CumulativeSavings = 0;
var totalSavingPrevious = 0;

 //if they are under 18 then they do not get a tax credit until they are 18
var under18Years = (18-age);
//alert(under18Years)

if (employ == 2){
////************************start calc loop CHILD****************************

for (i=0; i<yearsContrib+1; i++) { //we need to get the total income

if (i > 0){ ///don't add income growth first run
var incomeGrowth = Mathround(newIncomePerYear)*Mathround(avgIncomeGrowth)
 newIncomePerYear= Mathround(newIncomePerYear)+Mathround(incomeGrowth);//income plus growth in income
 
		if (employ == 0){
		 govmatchtaxcredit = 1042.86;
		 }else{
		 govmatchtaxcredit = 1042.86;
		 }
 
 } else {

 var incomeGrowth = (0.03) // zero income growth in first year
  govmatchtaxcredit = 1042.86;
  
 }
 //end first run code
 
 emplyTotalContrib += Mathround(newIncomePerYear)*Mathround(emyeeContrib);
 mortgageTotalContrib += Mathround(newIncomePerYear)*Mathround(mortgageDiversion);
 

 
 if (under18Years > 0){
	 	if (i < (under18Years-1))
		{
			govmatchtaxcredit = 0;
		} 
		else 
		{
		//alert("20")
					if ((Mathround(newIncomePerYear)*Mathround(emyeeContrib)) >= 1042.86)
				{
				 govmatchtaxcredit = 1042.86;
				}
				else
				{
				govmatchtaxcredit = (Mathround(newIncomePerYear)*Mathround(emyeeContrib));	
				}
		}
}

if (i < (under18Years+1)) //under 20
		{
	
			if (i == 0){
			alert('FIRSTRUN CHILD')
			//newIncomePerYear = 0;
			 //newIncomePerYear = Mathround(salary)*Mathround(emyeeContrib);
			 emplyTotalContrib += Mathround(salary)*Mathround(emyeeContrib);
			} 
			else 
			{
			emplyTotalContrib += Mathround(salary)*Mathround(emyeeContrib);
			//alert(newIncomePerYear);
			newIncomePerYear = Mathround(salary)//*Mathround(0);
			}
			
			var incomeGrowth = 0//Mathround(newIncomePerYear)*Mathround(avgIncomeGrowth);
			emplrTotalContrib = 0
			emyerContrib = 0;
}
		
	if (i == (under18Years+1))
		{
		alert("AGE 20:"+i)
		//when 20 assumed they start on 26000
		salary = 26000;
		newIncomePerYear = 26000;
		emplyTotalContrib += Mathround(newIncomePerYear)*Mathround(emyeeContrib);
		var incomeGrowth = Mathround(newIncomePerYear)*Mathround(avgIncomeGrowth);
 //newIncomePerYear= Mathround(newIncomePerYear)+Mathround(incomeGrowth);//income plus growth in income
 }

yearsContribValue = Mathround((Mathround(newIncomePerYear)*Mathround(emyeeContrib))+govmatchtaxcredit);
 
CumulativeSavings += yearsContribValue;

 //**********first run**************
 if (i > 0){
  kickstart = 0;//0 kick start
 lumpsum = 0;//reset lump sum only add at the first step
 var yearsEarning = Mathround(yearsContribValue);// +Mathround(yearsContribValue * fundValue))
 firstyearsearnings = 0
 
  } else { //**********first run**************
var firstyearsearnings 
firstyearsearnings = 1040*fundValue
var firstyear = Mathround(1040)

if (!joinFirst){
firstyear = 0;
firstyearsearnings = 0;
}

  var yearsEarning = Mathround((Mathround(newIncomePerYear)*Mathround(emyeeContrib))+govmatchtaxcredit+lumpsum+firstyear+firstyearsearnings);
  kickstart = 0;
  alert(Mathround(((newIncomePerYear)*Mathround(emyeeContrib))+":"+":"+govmatchtaxcredit+":"+lumpsum+":"+firstyear+":"+firstyearsearnings));
  }
  
   //**********end first run**************
savAtRetirement = savAtRetirement+Mathround(yearsEarning)+Mathround(totalSavingPrevious);

totalSavingPrevious = Mathround(savAtRetirement) * fundValue;

age = Number(age)+1;

 y = y+"<tr><td>"+age+"</td><td>"+newIncomePerYear+"</td><td>"+incomeGrowth+"</td><td>"+Mathround(newIncomePerYear)*Mathround(emyeeContrib)+"</td><td>"+Mathround(newIncomePerYear)*Mathround(mortgageDiversion)+"</td><td>"+Mathround(newIncomePerYear)*Mathround(emyerContrib)+"</td><td>"+govmatchtaxcredit+"</td><td>"+lumpsum+"</td><td>"+CumulativeSavings+"</td><td>&nbsp;</td><td>"+savAtRetirement+"</td><td>"+totalSavingPrevious+"</td></tr>";
 
}

////************************END calc loop CHILD****************************
} else {
////************************start calc loop EMPLOYED****************************

for (i=0; i<yearsContrib+1; i++) { //we need to get the total income

if (i > 0){ ///don't add income growth first run
var incomeGrowth = Mathround(newIncomePerYear)*Mathround(avgIncomeGrowth)
 newIncomePerYear= Mathround(newIncomePerYear)+Mathround(incomeGrowth);//income plus growth in income
 
		if (employ == 0){
		 govmatchtaxcredit = 1042.86;
		 }else{
		 govmatchtaxcredit = 1042.86;
		 }
 
 } else {
 //alert(i)
 
 var incomeGrowth = (0) // zero income growth in first year
  govmatchtaxcredit = 1042.86;
 }
 //end first run code
 
 emplyTotalContrib += Mathround(newIncomePerYear)*Mathround(emyeeContrib);
 emplrTotalContrib += Mathround(newIncomePerYear)*Mathround(emyerContrib);
 mortgageTotalContrib += Mathround(newIncomePerYear)*Mathround(mortgageDiversion);
 
 if ((Mathround(newIncomePerYear)*Mathround(emyeeContrib)) >= 1042.86){
 govmatchtaxcredit = 1042.86;
		 }else{
govmatchtaxcredit = (Mathround(newIncomePerYear)*Mathround(emyeeContrib));	
 }
 
 if (under18Years > 0){
	 	if (i < (under18Years-1))
		{
			govmatchtaxcredit = 0;
		} 
		else 
		{
		govmatchtaxcredit = (Mathround(newIncomePerYear)*Mathround(emyeeContrib));
		}
}

if (govmatchtaxcredit > 1042.86){
govmatchtaxcredit = 1042.86;
}
	
// yearsContribValue = Mathround(Mathround(newIncomePerYear*emyeeContrib)+Mathround(newIncomePerYear*emyerContrib)+govmatchtaxcredit+kickstart)
 yearsContribValue = Mathround((Mathround(newIncomePerYear)*Mathround(emyeeContrib))+(Mathround(newIncomePerYear)*Mathround(emyerContrib))+govmatchtaxcredit);
 
CumulativeSavings += yearsContribValue;

 //**********first run**************
 if (i > 0){
  kickstart = 0;//0 kick start
 lumpsum = 0;//reset lump sum only add at the first step
 var yearsEarning = Mathround(yearsContribValue);// +Mathround(yearsContribValue * fundValue))
 firstyearsearnings = 0
  } else { //**********first run**************
var firstyearsearnings 
firstyearsearnings = 1000*fundValue
var firstyear = Mathround(1000)

		if (!joinFirst){
		firstyear = 0;
		firstyearsearnings = 0;
		}

  var yearsEarning = Mathround((Mathround(newIncomePerYear)*Mathround(emyeeContrib))+(Mathround(newIncomePerYear)*Mathround(emyerContrib))+govmatchtaxcredit+lumpsum+firstyear+firstyearsearnings);
  kickstart = 0;
 // alert(Mathround(((newIncomePerYear)*Mathround(emyeeContrib))+":"+(Mathround(newIncomePerYear)*Mathround(emyerContrib))+":"+govmatchtaxcredit+":"+lumpsum+":"+firstyear+":"+firstyearsearnings));
  }
  
   //**********end first run**************
savAtRetirement = savAtRetirement+Mathround(yearsEarning)+Mathround(totalSavingPrevious);

totalSavingPrevious = Mathround(savAtRetirement) * fundValue;
 //y = y+"\n"+newIncomePerYear+":"+(newIncomePerYear*emyeeContrib)+":"+(newIncomePerYear*emyerContrib);
 
//Age	Income	Growth in Income p.a. 	Employee Contributions 	Mortgage Diversion Contributions	Employer Contributions	Tax Credit 	Cumulative Savings		Return p.a after fees and taxes	Return p.a after fees and taxes	Return p.a after fees and taxes	Return p.a after fees and taxes
age = Number(age)+1;
 y = y+"<tr><td>"+age+"</td><td>"+newIncomePerYear+"</td><td>"+incomeGrowth+"</td><td>"+Mathround(newIncomePerYear)*Mathround(emyeeContrib)+"</td><td>"+Mathround(newIncomePerYear)*Mathround(mortgageDiversion)+"</td><td>"+Mathround(newIncomePerYear)*Mathround(emyerContrib)+"</td><td>"+govmatchtaxcredit+"</td><td>"+lumpsum+"</td><td>"+CumulativeSavings+"</td><td>&nbsp;</td><td>"+savAtRetirement+"</td><td>"+totalSavingPrevious+"</td></tr>";
}
 
}
//alert(fundValue);
////************************end calc loop****************************

salert("<table border=1><tr>     <td height='102' width='53'>Age</td>     <td width='48'>Income</td>     <td width='64'>Growth    in Income p.a.Â </td>     <td width='82'>Employee    ContributionsÂ </td>     <td width='75'>Mortgage Diversion Contributions</td>     <td width='53'>Employer Contributions</td>     <td width='53'>Tax    CreditÂ </td>  <td width='53'>Lump SumÂ </td>   <td width='53'>Cumulative Savings</td>     <td width='53'>&nbsp;</td>     <td width='53'>Return p.a after fees and taxes</td>     <td width='53'>Return p.a after fees and taxes</td></tr>"+y+"</table>");
//alert(savAtRetirement);
// var savAtRetirement = (yearsContrib * (emyeeYearlyContrib + emyerYearlyContrib + govmatchtaxcredit)) + 1000;

 frm.estsaving.value = "$"+CurrencyFormatted(savAtRetirement);
 frm.totalcontrib.value = "$"+CurrencyFormatted((emplyTotalContrib));
 //alert("$"+CurrencyFormatted((mortgageTotalContrib)));
frm.mortgagetotalcontrib.value = "$"+CurrencyFormatted((mortgageTotalContrib));

}

function CurrencyFormatted(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;
	s = CommaFormatted(s)
	return s;
}


function CommaFormatted(amount)
{
amount=amount+"";

	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}



function disableForm(){
Field.disable('fundprofile')
Field.disable('age')
Field.disable('diversion')
Field.disable('emyeecontrib')
}

/*function Mathround(v){

return v;

}*/

function Mathround(num) {
	//var result = Math.round(num*Math.pow(10,2))/Math.pow(10,2);
	return num;
}



function salert(str){
//($('y')+"\n"+
//$('y').hide();

//Effect.BlindUp("y", { queue: 'front' })
//Element.update($('y'), str+"<br>");
//Effect.BlindDown("y", { queue: 'end' });*/
//Element.insert($('y'), str+"<br>");

}

var overlayMe = false;
function changeEmploy(t){
	
//t = $("aspnetForm");

employ = t.options[t.selectedIndex].value;
//Effect.Appear("salarydiv");
if (!overlayMe){
//$("overlayMe").style.display = "block";
//Effect.BlindDown("overlayMe");
overlayMe = true;
}

toggleForm("enable")

	if (employ == 1){
	$('emyeecontribid').disabled = false;
	$("sal").show();
	$("contrib").hide();
	$("emyeecontribnew").show();
	//$("mortgage_diversion").hide();
	$("salary").addClassName("required"); 
	$("weekly").removeClassName("required"); 
	}
	else if (employ == 0)
	{
	$('emyeecontribid').disabled = true;
	$("sal").hide();
	$("contrib").show();
	$("emyeecontribnew").hide();
	//$("mortgage_diversion").hide();
	$("weekly").addClassName("required"); 
	$("salary").removeClassName("required"); 
	} 
	else //child
	{
		toggleForm("disable")
//alert("child");
$('emyeecontribid').disabled = true;
	$("sal").hide();
	$("contrib").show();
	//$('mortgage_diversion').hide();
	$("emyeecontribnew").hide();
	//$("weekly").removeClassName("required"); 
	$("salary").removeClassName("required"); 
	}
}

function updateMe(){
	// calc();
}

var kiwisaverHTML = ' <div id="formleft"><fieldset style="margin-top:5px; border:none;"><ol style="list-style:none;"><li class="clearfix"><label class="kslabel">Employment:<span></span></label><select name="employment" id="employment" onchange="changeEmploy(this)" class="validate-selection"><option value="-1" selected="selected">Employment Type</option>           <option value="1">Employed</option><option value="0">Self-employed</option>           <option value="0">Not Employed</option>          <!-- <option value="2">Child</option> -->         </select>          <input type="hidden" name="govstartupcontrib" size="10" value="1000.00" readonly class="cap" /> <input type="hidden" name="govmatchtaxcredit" size="10" readonly class="cap" /> <input name="inflation" type="hidden" class="cap" id="inflation" value="0" size="3" maxlength="2" readonly="readonly" /> <input name="incomegrowth" type="hidden" class="sel"  value="3" size="3" maxlength="2" id="incomegrowth" /> </li>     </ol>     <div id="overlayMe" style="display:non e;">       <ol style="padding-top:0px; margin-top:0px;list-style:none;">         <li class="clearfix" id="sal">           <label class="kslabel">Salary:<span></span></label>           <input type="text" name="salary" id="salary" class="validate-number"/>         </li>         <li class="clearfix" id="contrib">           <label class="kslabel">Weekly Contribution:<span></span></label>           <input type="text" name="weekly" id="weekly" class="validate-number"/>         </li>         <li class="clearfix">           <label class="kslabel">Age:<span></span></label>           <input type="text" name="age" size="3" maxlength="2" onmouseup="updateMe()" onkeyup="updateMe()" class="required validate-less70">         </li>   <li class="clearfix">           <label class="kslabel">Are You Joining KiwiSaver For The First Time?<span></span></label>           <select name="joinFirst" onchange="updateMe()" class="sel" id="joinFirst">             <option value="yes" selected="selected">Yes</option>             <option value="no">No</option>           </select>         </li>         <li class="clearfix">           <label class="kslabel">Your Transfer Balance or Lump Sum Amount:</label>           <input type="text" name="lumpSum" onchange="updateMe()" id="lumpSum" />         </li>         <li class="clearfix" id="emyeecontribnew">           <label class="kslabel">Total KiwiSaver Contribution:<span></span></label>           <select name="emyeecontrib" onchange="updateMe()" class="sel" id="emyeecontribid">           <option value="0.02">2%</option>  <option value="0.04">4%</option>             <option value="0.08">8%</option>           </select>           <input name="emyercontrib" type="hidden" onchange="updateMe()" class="sel" value="0.02" />         </li>         <li class="clearfix">           <label class="kslabel">Choose Your Fund:<span></span></label>           <select name="fundprofile" onchange="updateMe();" class="sel">             <option value="0.06" selected="selected">Growth</option> <option value="0.05">Balanced</option> <option value="0.04">Conservative</option>           </select>         </li>         <li class="clearfix">           <label class="kslabel">&nbsp;<span></span></label>           <input type="button" name="Submit" value="Calculate" onclick="calc();" class="button">         </li>           <li class="clearfix">             <div id="formright" style="text-align:center">               <h5>Your Estimated Total Contribution</h5>               <input type="text" name="totalcontrib" size="13" readonly="readonly" class="cap totcontrib" />               <div id="mortgagecontribution" style="display:none;">                 <h5>Your Total Mortgage Contribution</h5>                 <input type="text" name="mortgagetotalcontrib" size="13" readonly="readonly" class="cap totcontrib" />                 <hr />               </div>               <h5>Total You Will Have Saved At 65</h5>               <input type="text" name="estsaving" size="13" readonly="readonly" class="cap estsaving" />             </div>         </li>         <li class="clearfix"><strong>Assumptions: </strong><br />Government Kick-start Contribution =   $1,000 (when joining KiwiSaver for the first time).<br />Members receive a   matching government contribution of up to the equivalent of $20 per   week.<br />KiwiSaver contributions are invested in either a Growth Fund, Balanced   Fund or a Conservative Fund, with average investment real rates of return of 6%,   5% and 4% (after fees and inflation) respectively.<br />Members take no   contributions holidays.<br />Salaries for employees rise steadily at 3% per annum.<br />For employees, it is assumed that their employer contributes 2%, and it is assumed employer contributions remain tax-free.<br /><br />The above figures   are estimates and do not represent any indication of future performance.    Neither the Trustee of the Scheme, Huljich Wealth Management (New Zealand)   Limited nor NZF guarantee the performance of the Scheme.  </li>        </ol>      </div>     </fieldset>   </div>  <div id="y"></div><div id="logos"><A href="http://www.huljich.co.nz/?from=nzf" style="float:left;"><img src="http://www.huljich.co.nz/widget/nzf_huljich_logo.jpg"  border="0"></A><a href="http://www.huljich.co.nz/?from=nzf" style="float:right;"><img src="http://www.huljich.co.nz/widget/nzf_kiwisaver_logo.jpg" border="0" /></a></div><style>label{ width:250px;}</style><link href="http://www.huljich.co.nz/widget/nzfformwidget.css" rel="stylesheet" type="text/css" />'
