function CountCharacters(lang, member) {
	var MaxLen = 1400;	// maximum character length.
	var AdText;
	var tmpText;
	var delta;

	if (lang == null)
		lang = "EN";

	if (document.all)
		AdText = document.all["AdText"].value;
	else if (document.getElementByID)
		AdText = document.getElementByID("AdText").value;
	else
		AdText = document.FORM1.AdText.value;

	tmpText = AdText.replace(/[\r\n]/g, "");
	delta = tmpText.length - MaxLen;
	if (delta > 0) {
		if (lang != "FR")
			alert('Your ad exceeds maximum limit and has been truncated at ' + MaxLen.toString() + ' characters. See note below text box on procedure for ads that exceed ' + MaxLen.toString() + ' characters.');
		else
			alert('Votre avertissement a trop de caractères et était coupé à ' + MaxLen.toString() + ' caractères. Voyez la note suivant pour la procédure pour les avertissements plus grands que ' + MaxLen.toString() + ' caractères.');

		do {
			AdText = AdText.substring(0, AdText.length - delta);
			tmpText = AdText.replace(/[\r\n]/g, "");
			delta = tmpText.length - MaxLen;
		} while (delta > 0);				

		if (document.all)
			document.all["AdText"].value = AdText;
		else if (document.getElementByID)
			document.getElementByID("AdText").value = AdText;
		else
			document.FORM1.AdText.value = AdText;
	}

	if (document.all)
		document.all["counter"].value = tmpText.length;
	else if (document.getElementByID)
		document.getElementByID("counter").value = tmpText.length;
	else
		document.FORM1.counter.value = tmpText.length;
		
	UpdatePrice(tmpText.length, lang, member);
}

function GetDuration() {
	var Duration = 0;
	var rbl;

	if (document.all) {
		Duration = (document.all["rblDuration_0"].checked?1:Duration);
		Duration = (document.all["rblDuration_1"].checked?2:Duration);
		Duration = (document.all["rblDuration_2"].checked?3:Duration);
		Duration = (document.all["rblDuration_3"].checked?4:Duration);
		Duration = (document.all["rblDuration_4"].checked?5:Duration);
		Duration = (document.all["rblDuration_5"].checked?6:Duration);
		Duration = (document.all["rblDuration_6"].checked?1:Duration);
	} else if (document.getElementByID) {
		Duration = (document.getElementByID("rblDuration_0").checked?1:Duration);
		Duration = (document.getElementByID("rblDuration_1").checked?2:Duration);
		Duration = (document.getElementByID("rblDuration_2").checked?3:Duration);
		Duration = (document.getElementByID("rblDuration_3").checked?4:Duration);
		Duration = (document.getElementByID("rblDuration_4").checked?5:Duration);
		Duration = (document.getElementByID("rblDuration_5").checked?6:Duration);
		Duration = (document.getElementByID("rblDuration_6").checked?1:Duration);
	} else {
		Duration = (document.FORM1.rblDuration_0.checked?1:Duration);
		Duration = (document.FORM1.rblDuration_1.checked?2:Duration);
		Duration = (document.FORM1.rblDuration_2.checked?3:Duration);
		Duration = (document.FORM1.rblDuration_3.checked?4:Duration);
		Duration = (document.FORM1.rblDuration_4.checked?5:Duration);
		Duration = (document.FORM1.rblDuration_5.checked?6:Duration);
		Duration = (document.FORM1.rblDuration_6.checked?1:Duration);
	}

	return Duration;
}

function UpdatePrice(NoChars, lang, member) {
	var bBoxRequired;
	var bIsMember;
	var PriceTextBox;
	var TaxTextBox;
	var TaxCodeTextBox;
	var TotalPriceTextBox;
	var sProvince;
	var sCountry;
	var nMonths;
	var lblAmountDetail;
	var AmountDetail;
	var IsHighlighted;

	nMonths = GetDuration();

	if (document.all) {
		bBoxRequired = document.all["BoxRequiredYes"].checked;
		//bIsMember = document.all["IsMemberYes"].checked;
		PriceTextBox = document.all["AdPrice"];
		TaxTextBox = document.all["txtTaxAmount"];
		TaxCodeTextBox = document.all["txtTaxCode"];
		TotalPriceTextBox = document.all["txtTotalPrice"];
		lblAmountDetail = document.all["lblAmountDetail"];
		sProvince = document.all["cbProvince"].value;
		sCountry = document.all["cbCountry"].value;
		IsHighlighted = !document.all["rblHighlight_0"].checked
	} else if (document.getElementById) {
		bBoxRequired = document.getElementById("BoxRequiredYes").checked;
		//bIsMember = document.getElementByID("IsMemberYes").checked;
		PriceTextBox = document.getElementById("AdPrice");
		TaxTextBox = document.getElementById("txtTaxAmount");
		TaxCodeTextBox = document.getElementById("txtTaxCode");
		TotalPriceTextBox = document.getElementById("txtTotalPrice");
		lblAmountDetail = document.getElementById("lblAmountDetail");
		sProvince = document.getElementById("cbProvince").value;
		sCountry = document.getElementById("cbCountry").value;
		IsHighlighted = !document.getElementById("rblHighlight_0").checked
	} else {
		bBoxRequired = document.FORM1.BoxRequiredYes.checked;
		//bIsMember = document.FORM1.Panel_MemberInformation.visible;
		PriceTextBox = document.FORM1.AdPrice;
		TaxTextBox = document.FORM1.txtTaxAmount;
		TaxCodeTextBox = document.FORM1.txtTaxCode;
		TotalPriceTextBox = document.FORM1.txtTotalPrice;
		lblAmountDetail = document.FORM1.lblAmountDetail;
		sProvince = document.FORM1.cbProvince.value;
		sCountry = document.FORM1.cbCountry.value;
		IsHighlighted = !document.FORM1.rblHighlight_0.checked
	}

	AmountDetail = CalcPrice(NoChars,member, bBoxRequired, sProvince, sCountry, nMonths, IsHighlighted);
	PriceTextBox.value = AmountDetail.PricePerMonth.toFixed(2);
	//lblAmountDetail.innerText = "(Total for " + nMonths.toString() + " months: $" + AmountDetail.TotalPrice.ToFixed(2) + (AmountDetail.TaxCode == "NA",""," " + AmountDetail.TaxCode + ": " + AmountDetail.TaxAmount.ToFixed(2) + ")";

	if (lang != "FR")
		lblAmountDetail.innerText = "(Total for " + nMonths.toString() + " months: $"+ AmountDetail.TotalPrice.toFixed(2) + (AmountDetail.TaxCode == "NA"?"":" plus " + AmountDetail.TaxCode + ": " + AmountDetail.TaxAmount.toFixed(2)) + ")";// //
	else
		lblAmountDetail.innerText = "(Montant pour " + nMonths.toString() + " mois: "+ AmountDetail.TotalPrice.toFixed(2) + "$" + (AmountDetail.TaxCode == "NA"?"":" plus " + AmountDetail.TaxCode + ": " + AmountDetail.TaxAmount.toFixed(2)) + ")";// //

	TaxTextBox.value = AmountDetail.TaxAmount.toFixed(2);
	TaxCodeTextBox.value = AmountDetail.TaxCode;
	TotalPriceTextBox.value = AmountDetail.TotalPrice.toFixed(2);
}

function CalcPrice(NoChars, IsMember, BoxRequired, Province, Country, Months, Highlighted) {
	var ResultingPrice =0.0;
	var Result = new Object();

	Result.TotalPrice = 0.0;
	Result.TaxRate = 0.0;
	Result.TaxAmount = 0.0;
	Result.TaxCode = "NA"
	Result.PricePerMonth = 0.0;

	if (NoChars >= 1 && NoChars <=175)
		Result.PricePerMonth = (IsMember?33.0:66.0);
	else if (NoChars >= 176 && NoChars <=350)
		Result.PricePerMonth = (IsMember?48.0:96.0);
	else if (NoChars >= 351 && NoChars <=550)
		Result.PricePerMonth = (IsMember?63.0:126.0);
	else if (NoChars >= 551 && NoChars <=725)
		Result.PricePerMonth = (IsMember?78.0:156.0);
	else if (NoChars >= 726 && NoChars <=900)
		Result.PricePerMonth = (IsMember?96.0:192.0);
	else if (NoChars >= 901 && NoChars <=1075)
		Result.PricePerMonth = (IsMember?110.0:220.0);
	else if (NoChars >= 1076 && NoChars <=1400)
		Result.PricePerMonth = (IsMember?131.0:262.0);
	else
		Result.PricePerMonth = 0.0;

	if (Highlighted)
		Result.PricePerMonth += 25;
		
	if (Result.PricePerMonth != 0.0)
		if (BoxRequired)
			Result.PricePerMonth += (IsMember?6.0:12.0);

	Result.TotalPrice = Result.PricePerMonth * Months;

	if (Date() < new Date(2010, 7, 1)) {
	    switch (Province.toLowerCase()) {
	        case "newfoundland":
	        case "nova scotia":
	        case "new brunswick":
	            Result.TaxRate = 0.13;
	            Result.TaxCode = "HST";
	            break;
	        case "alberta":
	        case "british columbia":
	        case "manitoba":
	        case "northwest territories":
	        case "nunavut":
	        case "ontario":
	        case "prince edward island":
	        case "quebec":
	        case "saskatchewan":
	        case "yukon":
	            Result.TaxRate = 0.05;
	            Result.TaxCode = "GST";
	            break;
	        default:
	            Result.TaxRate = 0.0;
	            Result.TaxCode = "NA";
	    }
	    switch (Province.toLowerCase()) {
	        case "nova scotia":
	            Result.TaxRate = 0.15;
	            Result.TaxCode = "HST";
	            break;
	        case "newfoundland":
	        case "new brunswick":
	        case "ontario":
	            Result.TaxRate = 0.13;
	            Result.TaxCode = "HST";
	            break;
	        case "british columbia":
	            Result.TaxRate = 0.12;
	            Result.TaxCode = "HST";
	            break;
	        case "alberta":
	        case "manitoba":
	        case "northwest territories":
	        case "nunavut":
	        case "prince edward island":
	        case "quebec":
	        case "saskatchewan":
	        case "yukon":
	            Result.TaxRate = 0.05;
	            Result.TaxCode = "GST";
	            break;
	        default:
	            Result.TaxRate = 0.0;
	            Result.TaxCode = "NA";
	    }
	}

	Result.TaxAmount = (Result.TotalPrice * Result.TaxRate);

	if (parseFloat)
		Result.TaxAmount = parseFloat(Result.TaxAmount.toFixed(2));

	return Result;
}
