var clickedButton = false;

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function changeYear() {
	//document.getElementById('year'+year).value=
	
	
	document.getElementById('year').value='';
	document.getElementById('yearvalue').value='';
	
	var topup = document.getElementById("topup");
	var yearAdd = document.getElementById('yearAdd');
	
	//var expressTotal = document.getElementById("yearTotal");	
	var tempTotal = 0;
	for(a=1;a<=20;a++) {
		var tempYear =  document.getElementById("year"+a);
		
		if (tempYear) {
			tempYear.value = dollarsToNumbers(tempYear.value);
			if ((parseInt(tempYear.value) < 5000) && (parseInt(tempYear.value) != 0)){
				tempYear.value = 5000;
				alert("The minimum draw down is $5000");
			}	
			
			if (tempYear.value=='') { tempYear.value = '0'; }
			tempTotal = tempTotal + parseInt(tempYear.value);
		}		 
	}
	tempTotal = Math.round(tempTotal);
	
	
	if (parseInt(tempTotal) > topup.value) {
		alert("This exceeds the Express Top Up Remaining above");	
	}
	
	document.getElementById('step').value='step2';
}


function gotoProjectionScript(){
	
	if (addYear()==true) { 
		setStepComplete('step3');
	}
}

function addYear() {
	//alert(document.getElementById('yearAddDropdown').value);
	//var maximumTopup = document.getElementById("maximumTopup");
	var topup = document.getElementById("topup");
	var yearAdd = document.getElementById('yearAdd');
	
	//var expressTotal = document.getElementById("yearTotal");	
	var tempTotal = 0;
	for(a=1;a<=20;a++) {
		var tempYear =  document.getElementById("year"+a);
		
		if (tempYear) {
			tempYear.value = dollarsToNumbers(tempYear.value);
			if (tempYear.value=='') { tempYear.value = '0'; }
			tempTotal = tempTotal + parseInt(tempYear.value);
		}		 
	}
	tempTotal = Math.round(tempTotal + parseInt(yearAdd.value));
	
	
	if (parseInt(tempTotal) > topup.value) {
		alert("This exceeds the Express Top Up Remaining above");
		return false;	
	} else {
		if (parseInt(yearAdd.value) < 5000) {
			//yearAdd.value = 5000;
			alert("The minimum draw down is $5000");
			return false;
		} else {
			document.getElementById('year').value=document.getElementById('yearAddDropdown').value;
			document.getElementById('yearvalue').value=document.getElementById('yearAdd').value;	
		}
		
	}
	
	document.getElementById('step').value='step2';	
	return true;
}

function setNextStep( stepName ) {
	document.getElementById('step').value=stepName;
}


var hasDom=true;
var hasAll=document.all;
var hasNSDom=document.layers;

function disguise() {
    var ary=disguise.arguments;
    for(a=0;a<ary.length;a++) {
        var name=ary[a];
        if (hasDom) {
            var lyrStyle=document.getElementById("lyr_"+name).style;
            lyrStyle.display="none";
        }
    }
}

function reveal() {
	var ary=reveal.arguments;
    for(a=0;a<ary.length;a++) {
		var name=ary[a];
        if (hasDom) {
			var lyrStyle=document.getElementById("lyr_"+name).style;
			lyrStyle.display="block";
        }

    }
}
function dollarsToNumbers(dollars) {
	var myregexp = /[^.0-9]/g;
	dollars = dollars.replace(myregexp, "");
	var period = dollars.indexOf(".");
	if (period!=-1) {
		//alert('found a period');
		dollars = dollars.substring(0,period);
	}
	return dollars;
}


updateExpress();

function updateExpress() {
	var lumpsum = document.getElementById("lumpsum");	
	//var topup = document.getElementById("topup");	
	var maximumLoanAmount = document.getElementById("maximumLoanAmount");	
	var maximumAmount = document.getElementById("maximumAmount");	
	var maximumTopup = document.getElementById("maximumTopup");
	var topup = document.getElementById("topup");
	
	var maximumTopupDisplay = document.getElementById("maximumTopupDisplay");
	var maximumAmountDisplay = document.getElementById("maximumAmountDisplay");
	
	lumpsum.value = dollarsToNumbers(lumpsum.value); 
	topup.value = dollarsToNumbers(topup.value);
	//alert("max :"+maximumAmount.value+"lump :"+lumpsum.value);
	if (parseInt(lumpsum.value) > parseInt(maximumAmount.value)) {
		lumpsum.value = maximumAmount.value;
		alert("Cannot enter more than the Maximum Loan Amount");
	} else {
		if (parseInt(lumpsum.value) < 10000) {
			lumpsum.value = 10000;
			alert("Minimum Loan amount is $10,000");
		}
	}
	
	if ( maximumLoanAmount != null && lumpsum != null ) {
		maximumTopup.value = Math.round(maximumLoanAmount.value-lumpsum.value);
		maximumTopupDisplay.value = addCommas(maximumTopup.value); 
		topup.value = maximumTopup.value; 
		if (parseInt(topup.value) > parseInt(maximumTopup.value)) {
			topup.value =  Math.round(maximumLoanAmount.value-lumpsum.value);
		}
	}
}


function updateRemaining(){
	var maximumTopup = document.getElementById("maximumTopup");
	var maximumTopupDisplay = document.getElementById("maximumTopupDisplay");
	var topup = document.getElementById("topup");
	
	
	topup.value = dollarsToNumbers(topup.value);	
	if ((parseInt(topup.value) < 5000) && (parseInt(topup.value) != 0)) {
		topup.value = 5000;
		alert("You must enter a minimum of $5,000 for the Express Top Up Remaining if you wish to use it.");	
	}
	if (parseInt(topup.value) > parseInt(maximumTopup.value)) {
		topup.value = maximumTopup.value;
		alert("Cannot enter more than the Maximum Express Top Up of $"+maximumTopupDisplay.value);	
	}	
}



function updateExpressTotal() {
	var expressTotal = document.getElementById("yearTotal");	
	var tempTotal = 0;
	for(a=1;a<=20;a++) {
		var tempYear =  document.getElementById("year"+a);
		
		if (tempYear) {
			tempYear.value = dollarsToNumbers(tempYear.value);
			if (tempYear.value=='') { tempYear.value = '0'; }
			tempTotal = tempTotal + parseInt(tempYear.value);
		}		 
	}
	expressTotal.value = Math.round(tempTotal);
	//var year1 = document.getElementById("year1");	
	//var year2 = document.getElementById("year2");	
	//var year3 = document.getElementById("year3");	
	//var year4 = document.getElementById("year4");	
	//var year5 = document.getElementById("year5");
	//year1.value = dollarsToNumbers(year1.value);
	//year2.value = dollarsToNumbers(year2.value);
	//year3.value = dollarsToNumbers(year3.value);
	//year4.value = dollarsToNumbers(year4.value);
	//year5.value = dollarsToNumbers(year5.value);	
	//if (year1.value=='') { year1.value = '0'; }
	//if (year2.value=='') { year2.value = '0'; }
	//if (year3.value=='') { year3.value = '0'; }
	//if (year4.value=='') { year4.value = '0'; }
	//if (year5.value=='') { year5.value = '0'; }
	
	//expressTotal.value =  Math.round(parseInt(year1.value) + parseInt(year2.value) + parseInt(year3.value) + parseInt(year4.value) + parseInt(year5.value));
	
}

function toggle(name) {
	if (toggle_set==true) {
		disguise(name);
				var equity10 = document.getElementById("equityOption10");	
				equity10.checked = false;
				var equity20 = document.getElementById("equityOption20");	
				equity20.checked = false;
				toggle_set=false;
	} else {
		reveal(name);
		toggle_set=true;
	}
}

function equity( property, percent, epercent ) {
	
	var elem = document.getElementById("maximumAmount");		
	var maximumAmountDisplay = document.getElementById("maximumAmountDisplay");
	elem.value = Math.round((property-(property*(epercent/100)))*(percent/100));
	maximumAmountDisplay.value = addCommas(elem.value);
}

function ClearDefault(id) {
	
	if(document.getElementById(id).value == "(Click Here)") 
		document.getElementById(id).value = "";
	
}
