// Total Variables
var dFeeDIY = 90;
var dFeeFS = 900;

var dSundriesDIY = 0;
var dSundriesFS = 100;

var dGSTDIY = 0;
var dGSTFS = 0;

var dConveyancingFeeDIY = 0;
var dConveyancingFeeFS = 0;

var dStampDutyDIY = 0;
var dStampDutyFS = 0;

var dSearchesDIY = 75;
var dSearchesFS = 140;

var dTotalPurchaseCostsDIY = 0;
var dTotalPurchaseCostsFS = 0;

var dConveyancingFee = 245;
var dStampDuty = 0;
var dSearches = 0;
var dRegFeesOnTransfer = 60;
var dPurchaseTotal = 0;

// Question Variables
var dPurchasePrice;
var iPropertyType;
var iFirstProperty;
var iLivingInPropertyVacantLand;
var iLivingInPropertyHouseUnit;

var bCalculateWasPressed = false;

function DataChange()
{
	if(bCalculateWasPressed)
		if(SetQuestions())
			SetResults(false);
		else
			ClearResults();		
}

function OnLoad()
{
	ClearResults();
	FirstPropertyOnClick();
	document.calculator.PurchasePrice.focus();
}

function ClearResults()
{
	hideObject('ResultsPurchaseCost');
}

function FirstPropertyOnClick()
{
	DataChange();
	if(document.calculator.FirstProperty[0].checked && (document.calculator.PropertyType[0].checked || document.calculator.PropertyType[1].checked || document.calculator.PropertyType[2].checked))
	{
		if(document.calculator.PropertyType[0].checked)
		{
			showObject('LivingInPropertyVacantLandQuestion');
			hideObject('LivingInPropertyHouseUnitQuestion');
		}
		else if(document.calculator.PropertyType[1].checked || document.calculator.PropertyType[2].checked)
		{
			hideObject('LivingInPropertyVacantLandQuestion');
			showObject('LivingInPropertyHouseUnitQuestion');			
		}
		return;
	}
	hideObject('LivingInPropertyVacantLandQuestion');
	hideObject('LivingInPropertyHouseUnitQuestion');
}

function CalculateOnClick()
{
	if(SetQuestions())
		SetResults(true);
	else
		ClearResults();		
}

function SetQuestions()
{
	if(HasCommas(document.calculator.PurchasePrice.value)) dPurchasePrice = NaN;
	else dPurchasePrice = parseFloat(document.calculator.PurchasePrice.value);
	
	iPropertyType = RadioButtonValue(document.calculator.PropertyType);
	iFirstProperty = RadioButtonValue(document.calculator.FirstProperty);
	iLivingInPropertyVacantLand = RadioButtonValue(document.calculator.LivingInPropertyVacantLand);
	iLivingInPropertyHouseUnit = RadioButtonValue(document.calculator.LivingInPropertyHouseUnit);
	return CheckQuestions();
}

function setErrorMsg(index)
{
	hideObject('ErrorMsg0');
	hideObject('ErrorMsg1');
	hideObject('ErrorMsg2');
	hideObject('ErrorMsg3');
	hideObject('ErrorMsg4');
	if(index>=0)
		showObject('ErrorMsg' + index.toString());
}

function CheckQuestions()
{
	if(isNaN(dPurchasePrice))
	{
		setErrorMsg(0);
		document.calculator.PurchasePrice.focus();
		return false;
	}
	else if(isNaN(iPropertyType))
		setErrorMsg(1);
	else if(isNaN(iFirstProperty))
		setErrorMsg(2);
	else if(iFirstProperty==0 && iPropertyType==0 && isNaN(iLivingInPropertyVacantLand))
		setErrorMsg(3);
	else if(iFirstProperty==0 && (iPropertyType==1 || iPropertyType==2) && isNaN(iLivingInPropertyHouseUnit))
		setErrorMsg(4);
	else
	{
		setErrorMsg(-1);
		return true;
	}
	return false;
}

function SetResults(bGoToEnd)
{
	CalculateCosts();
	
	setInnerHTML('ConveyancingFeeDIY', ToMoney(dFeeDIY));
	setInnerHTML('ConveyancingFeeFS', ToMoney(dFeeFS));
	
	setInnerHTML('SundriesDIY', ToMoney(dSundriesDIY));
	setInnerHTML('SundriesFS', ToMoney(dSundriesFS));
	
	setInnerHTML('GSTDIY', ToMoney(dGSTDIY));
	setInnerHTML('GSTFS', ToMoney(dGSTFS));

	setInnerHTML('TotalConveyancingFeeDIY', ToMoney(dConveyancingFeeDIY));
	setInnerHTML('TotalConveyancingFeeFS', ToMoney(dConveyancingFeeFS));

	setInnerHTML('StampDutyDIY', ToMoney(dStampDutyDIY));
	setInnerHTML('StampDutyFS', ToMoney(dStampDutyFS));

	setInnerHTML('SearchesDIY', ToMoney(dSearchesDIY));
	setInnerHTML('SearchesFS', ToMoney(dSearchesFS));

	setInnerHTML('TotalPurchaseCostsDIY', ToMoney(dTotalPurchaseCostsDIY));
	setInnerHTML('TotalPurchaseCostsFS', ToMoney(dTotalPurchaseCostsFS));
	
	showObject('ResultsPurchaseCost');
	if(bGoToEnd) window.location = '#end';
	bCalculateWasPressed = true;
}

function CalculateCosts()
{
	dStampDutyDIY = GetStampDuty(dPurchasePrice, isInvestment(), isFirstHouseLand(), isFirstVacantLand());
	dStampDutyFS = dStampDutyDIY;

	CalcSearches();	
	dGSTDIY = 0.1 * (dFeeDIY + dSundriesDIY);
	dGSTFS = 0.1 * (dFeeFS + dSundriesFS);
	dConveyancingFeeDIY = dFeeDIY + dSundriesDIY + dGSTDIY;
	dConveyancingFeeFS = dFeeFS + dSundriesFS + dGSTFS;
	
	dTotalPurchaseCostsDIY = dConveyancingFeeDIY + dSearchesDIY; //dStampDutyDIY + 
	dTotalPurchaseCostsFS = dConveyancingFeeFS + dSearchesFS; //dStampDutyFS + 
}

function FirstPrinciplePlaceOfResidence()
{
	return ((iPropertyType == 1 || iPropertyType == 2) && iLivingInPropertyHouseUnit == 0 && iFirstProperty == 0)
}

function PrinciplePlaceOfResidence()
{
	return ((iPropertyType == 1 || iPropertyType == 2) && iLivingInPropertyHouseUnit == 0 && iFirstProperty == 1)
}

function VacantLandInvestmentProperty()
{
	return (iPropertyType == 0 || ((iPropertyType == 1 || iPropertyType == 2) && iLivingInPropertyHouseUnit == 1))
}

function CalcSearches()
{
	switch(iPropertyType)
	{
		case 0:
			dSearchesDIY = 270;
			break;
		case 1:
			dSearchesDIY = 270;
			break;
		case 2:
			dSearchesDIY = 440;
			break;
	}
	dSearchesFS = dSearchesDIY;
}

function CalcRegFeesOnTransfer()
{
	var dResult = 100;
		
	if(dPurchasePrice > 180000)
	{
		var dTemp = dPurchasePrice - 180000;
		dTemp = Math.ceil(dTemp / 10000);
		dResult += (dTemp * 21.50);
	}
	dRegFeesOnTransfer = dResult;
}

function isFirstProperty()
{
	return (iFirstProperty==0);
}

function willBeLivingInProperty()
{
	if(iPropertyType==0)
		return (iLivingInPropertyVacantLand==0);
	else
		return (iLivingInPropertyHouseUnit==0);
}

function hasFirstHomeConcession()
{
	return (isFirstProperty() && willBeLivingInProperty());
}

//************ Stamp Duty *****************************************

function isInvestment()
{
	return !hasFirstHomeConcession();
}

function isFirstHouseLand()
{
	return hasFirstHomeConcession()&&(iPropertyType!=0);
}

function isFirstVacantLand()
{
	return hasFirstHomeConcession()&&(iPropertyType==0);
}
//************* END STAMP DUTY ************************

