/*
Author: Anton Zhu
Company: Macdonald Realty
Date: June 1st, 2010
Purpose: HST calcualator.
Version: 1.0



*/





/*
function roundNumber(number,decimals) {
	var newString;// The new rounded number
	decimals = Number(decimals);
	if (decimals < 1) {
		newString = (Math.round(number)).toString();
	} else {
		var numString = number.toString();
		if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
			numString += ".";// give it one at the end
		}
		var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
		var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
		var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
		if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
			if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
				while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
					if (d1 != ".") {
						cutoff -= 1;
						d1 = Number(numString.substring(cutoff,cutoff+1));
					} else {
						cutoff -= 1;
					}
				}
			}
			d1 += 1;
		} 
		if (d1 == 10) {
			numString = numString.substring(0, numString.lastIndexOf("."));
			var roundedNum = Number(numString) + 1;
			newString = roundedNum.toString() + '.';
		} else {
			newString = numString.substring(0,cutoff) + d1.toString();
		}
	}
	if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
		newString += ".";
	}
	var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
	for(var i=0;i<decimals-decs;i++) newString += "0";
	//var newNumber = Number(newString);// make it a number if you like
	if (newString=="0.00")
		return "0"
	else
		return newString;
}


*/

function Comma(number) {
number = '' + number;
if (number.length > 3) {
var mod = number.length % 3;
var output = (mod > 0 ? (number.substring(0,mod)) : '');
for (i=0 ; i < Math.floor(number.length / 3); i++) {
if ((mod == 0) && (i == 0))
output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
else
output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
}
return (output);
}
else return number;
}

function roundNumber(num) {
	
	var int=Math.round(num);
	
	return Comma(int);
	
}





function GST1_purchasePrice(purchasePrice) 
{
	var GST1_purchasePrice=0;
	GST1_purchasePrice=purchasePrice;
	
	//document.maxcalc.GST1_purchasePrice.value = "$"+roundNumber(purchasePrice);
	return GST1_purchasePrice*1;
}

function HST1_purchasePrice(purchasePrice) 
{
	var HST1_purchasePrice = 0;
	HST1_purchasePrice=purchasePrice;
	document.maxcalc.HST1_purchasePrice.value ="$"+roundNumber(purchasePrice);
	document.maxcalc.Investment_purchasePrice.value ="$"+roundNumber(purchasePrice);
	return HST1_purchasePrice*1;
}

function Vacation_purchasePrice(purchasePrice) 
{
	var Vacation_purchasePrice = 0;
	Vacation_purchasePrice=purchasePrice;
	document.maxcalc.Vacation_purchasePrice.value ="$"+roundNumber(purchasePrice);
	
	return Vacation_purchasePrice*1;
}



/*
function GST1_pttax(purchasePrice) 
{
	
	var pptax;
	
	if (purchasePrice > 200000)
		pptax = 2000+ (purchasePrice - 200000)*0.02;
	else
	    pptax = purchasePrice * 0.01;
	
	
	//document.maxcalc.GST1_pttax.value = "$"+roundNumber(pptax);
	return pptax;
}
*/

/*
function HST1_pttax(purchasePrice) 
{
	
	var pttax;
	
	if (purchasePrice > 200000)
		pttax = 2000+ (purchasePrice - 200000)*0.02;
	else
	    pttax = purchasePrice * 0.01;
	
	
	document.maxcalc.HST1_pttax.value = "$"+roundNumber(pttax);
	
	return pttax;
}
*/


function GST1_purchasePrice_tax(purchasePrice) 
{
	var GST1_purchasePrice_tax =purchasePrice*0.05;
	//document.maxcalc.GST1_purchasePrice_tax.value ="$"+roundNumber(GST1_purchasePrice_tax);
	return GST1_purchasePrice_tax;
}

 function HST1_purchasePrice_tax(purchasePrice)
 {
	 var HST1_purchasePrice_tax = purchasePrice*0.12;
	 document.maxcalc.HST1_purchasePrice_tax.value = "$"+roundNumber(HST1_purchasePrice_tax);
	 document.maxcalc.Investment_purchasePrice_tax.value = "$"+roundNumber(HST1_purchasePrice_tax);
	 
	 return HST1_purchasePrice_tax;
 }
 
   function Vacation_purchasePrice_tax(purchasePrice)
  {
	   var Vacation_purchasePrice_tax = purchasePrice*0.12;
	 document.maxcalc.Vacation_purchasePrice_tax.value = "$"+roundNumber(Vacation_purchasePrice_tax);
	 return Vacation_purchasePrice_tax;
  }
 
 
  function GST1_pRebate(purchasePrice)
 {
	 //document.maxcalc.GST1_pRebate.value ="$"+"0";
	 return 0;
 }
 

 
 
   function HST1_pRebate(purchasePrice)
 {
	 var HST1_pRebate;
	 if (purchasePrice*0.05 > 26250)
	 	HST1_pRebate=26250;
	else
	    HST1_pRebate= purchasePrice*0.05;
	 
	 document.maxcalc.HST1_pRebate.value = "$"+roundNumber(HST1_pRebate);
	  document.maxcalc.Investment_pRebate.value = "$"+roundNumber(HST1_pRebate);
	 
	 return HST1_pRebate;
 }
 
 function Vacation_pRebate(purchasePrice) {
	  document.maxcalc.Vacation_pRebate.value="$"+"0";
	  return 0;
	 
 }
 
 


 function GST1_hRebate(purchasePrice)
 {
	 var GST1_hRebate;
	 
	 if (purchasePrice <= 350000 )
	   GST1_hRebate= GST1_purchasePrice_tax(purchasePrice)*0.36;
	 
	 if ( purchasePrice>350000 &&  purchasePrice<450000 )
	 {
	  //GST1_hRebate= (450000-purchasePrice)/100000*0.36*GST1_purchasePrice_tax(purchasePrice);
	  var lesser1=0;
	  var lesser2=0;
	  lesser1= (450000-purchasePrice)*6300/100000;
	  lesser2= ((450000-purchasePrice))*0.36/100000*GST1_purchasePrice_tax(purchasePrice);
	  if (lesser1<lesser2)
	  	GST1_hRebate=lesser1;
	  else 
	     GST1_hRebate=lesser12;
	 }
	 
	 if (purchasePrice >= 450000 )
	 	GST1_hRebate=0;
	  
	   //document.maxcalc.GST1_hRebate.value = "$"+roundNumber(GST1_hRebate);
	   
	   return GST1_hRebate;
	  
 }
 
  function HST1_hRebate(purchasePrice)
 {
	 var HST1_hRebate;
	 
	 if (purchasePrice <= 350000 )
	   HST1_hRebate= GST1_purchasePrice_tax(purchasePrice)*0.36;
	 
	 if ( purchasePrice>350000 &&  purchasePrice<450000 )
	 {
	  //HST1_hRebate= (450000-purchasePrice)/100000*0.36*GST1_purchasePrice_tax(purchasePrice);
	  var lesser1=0;
	  var lesser2=0;
	  lesser1= (450000-purchasePrice)*6300/100000;
	  lesser2= ((450000-purchasePrice))*0.36/100000*HST1_purchasePrice_tax(purchasePrice);
	  if (lesser1<lesser2)
	  	HST1_hRebate=lesser1;
	  else 
	     HST1_hRebate=lesser12;
	 }
	 
	 if (purchasePrice >= 450000 )
	 	HST1_hRebate=0;
	  
	   document.maxcalc.HST1_hRebate.value = "$"+roundNumber(HST1_hRebate);
	   document.maxcalc.Investment_hRebate.value = "$"+roundNumber(HST1_hRebate);
	   
	   return HST1_hRebate;
	  
 }
 
 
  function Vacation_hRebate(purchasePrice) {
	  document.maxcalc.Vacation_hRebate.value="$"+"0";
	  return 0;
	 
 }
 
 
 
 
 /*
 function GST1_totalPrice(purchasePrice) 
{
	var GST1_totalPrice = 0;
	var price =purchasePrice;
	//GST1_totalPrice=GST1_purchasePrice(price) + GST1_pttax(price) + GST1_purchasePrice_tax(price) +  GST1_pRebate(price)+  GST1_hRebate(price);
	//GST1_totalPrice=GST1_purchasePrice(price)+GST1_pttax(price)+GST1_purchasePrice_tax(price) +  GST1_pRebate(price)-GST1_hRebate(price);
	//document.maxcalc.GST1_totalPrice.value ="$"+roundNumber(GST1_totalPrice);
	return GST1_totalPrice;

}
*/
 function HST1_totalPrice(purchasePrice) 
{
	var HST1_totalPrice = 0;
	var price =purchasePrice;
	
	HST1_totalPrice= HST1_purchasePrice(price)+HST1_purchasePrice_tax(price) -  HST1_pRebate(price)-HST1_hRebate(price);
	document.maxcalc.HST1_totalPrice.value ="$"+roundNumber(HST1_totalPrice);
	document.maxcalc.Investment_totalPrice.value ="$"+roundNumber(HST1_totalPrice);
	return HST1_totalPrice;

}

function difference(purchasePrice)
{
	var difference=0;
	var price =purchasePrice;
	difference= HST1_totalPrice(price) -GST1_totalPrice(price);
	document.maxcalc.difference.value ="$"+roundNumber(difference);
	
	
}


function Vacation_totalPrice(purchasePrice) {
	var Vacation_totalPrice = 0;
	var price =purchasePrice;
	Vacation_totalPrice=Vacation_purchasePrice(price)+Vacation_purchasePrice_tax(price) + Vacation_pRebate(price) + Vacation_hRebate(price);
	document.maxcalc.Vacation_totalPrice.value ="$"+roundNumber(Vacation_totalPrice);
}



function calculateHST()
{
	var purchasePrice=0
	
	purchasePrice=document.maxcalc.purchasePrice.value;
	// GST1_purchasePrice(purchasePrice);
	 HST1_purchasePrice(purchasePrice);
	 Vacation_purchasePrice(purchasePrice);
	// GST1_pttax(purchasePrice);
	// HST1_pttax(purchasePrice);
	// GST1_purchasePrice_tax(purchasePrice);
	 HST1_purchasePrice_tax(purchasePrice);
	 Vacation_purchasePrice_tax(purchasePrice);
	 //GST1_pRebate(purchasePrice);
	 HST1_pRebate(purchasePrice);
	 Vacation_pRebate(purchasePrice);
	 //GST1_hRebate(purchasePrice);
	 HST1_hRebate(purchasePrice);
	 Vacation_hRebate(purchasePrice);
	 //GST1_totalPrice(purchasePrice);
	 HST1_totalPrice(purchasePrice);
	 Vacation_totalPrice(purchasePrice) ;
	 //difference(purchasePrice)
	 

}

