// JavaScript Document
function doCalc()
{
	var studentCount = 0;
	var preferredCount = 0;
	var generalCount = 0;
	var donationAmt = 0;
	var mailFee = 0;
	
	var studentBox = document.getElementById("student");
	var preferredBox = document.getElementById("preferred");
	var generalBox = document.getElementById("general");
	
	var mailBox = document.getElementById("mail");
	var donationBox = document.getElementById("donation");
	
	var discountBox = document.getElementById("discount");
	var studentDiscBox = document.getElementById("stddisc");
	var totalBox = document.getElementById("total");
	
	var discountPrice = 0;
	var studentDiscPrice = 0;
	var totalPrice = 0;
	
	SetStudentCount(studentBox);
	SetPreferredCount(preferredBox);
	SetGeneralCount(generalBox);
	SetDonationAmt(donationBox)
	SetMailFee(mailBox);
	
	doValidate();

	discountPrice = (-5)*(preferredCount + generalCount);
	studentDiscPrice = (-5)*(studentCount);
	totalPrice = (25*preferredCount) + (15*generalCount) + discountPrice + studentDiscPrice + donationAmt + mailFee;
	
	discountBox.value = discountPrice.toFixed(2).toString();
	studentDiscBox.value = studentDiscPrice.toFixed(2).toString();
	totalBox.value = totalPrice.toFixed(2).toString();
	
	loadTxtPrint(discountBox);
	loadTxtPrint(studentDiscBox);
	loadTxtPrint(totalBox);

	function GetStudentCount()
	{
		return studentCount;
	}

	function SetStudentCount(studentBox)
	{
		if(IsNumeric(studentBox.value))
		{
			studentCount = parseInt(studentBox.value);
		}
		else
		{
			if(studentBox.value != "")
			{
				alert("Warning: An invalid amount of students were entered. Please enter the number of students attending.");
				studentBox.value = "";
			}
		
			studentCount = 0;
		}
	}

	function GetPreferredCount()
	{
		return preferredCount;
	}

	function SetPreferredCount(preferredBox)
	{
		if(IsNumeric(preferredBox.value))
		{
			preferredCount = parseInt(preferredBox.value);
		}
		else
		{
			if(preferredBox.value != "")
			{
				alert("Warning: An invalid amount of preferred seat tickets were entered. Please enter the number of preferred seat tickets you wish to purchase.");
				preferredBox.value = "";
			}
		
			preferredCount = 0;
		}
	}

	function GetGeneralCount()
	{
		return generalCount;
	}

	function SetGeneralCount(generalBox)
	{
		if(IsNumeric(generalBox.value))
		{
			generalCount = parseInt(generalBox.value);
		}
		else
		{
			if(generalBox.value != "")
			{
				alert("Warning: An invalid amount of general seat tickets were entered. Please enter the number of general seat tickets you wish to purchase.");
				generalBox.value = "";
			}
		
			generalCount = 0;
		}
	}

	function SetDonationAmt(donationBox)
	{
		if(IsFloat(donationBox.value))
		{
			if(donationBox.value == ".")
			{
				donationAmt = 0;
			}
			else
			{
				donationAmt = parseFloat(donationBox.value);
			}
		}
		else
		{
			if(donationBox.value != "")
			{
				alert("Warning an invalid donation amount was entered. Please enter a dollar and cents amount into the Additional Donation text box.");
				donationBox.value = "";
			}
		
			donationAmt = 0;
		}
	}

	function SetMailFee(mailBox)
	{
		if(mailBox.checked == 1)
		{
			mailFee = 1.5;
		}
		else
		{
			mailFee = 0;
		}
	}

	function IsNumeric(sText)
	{
		var ValidChars = "0123456789";
		var IsNumber=true;
		var Char;

		if(sText != "")
		{
			for (i = 0; i < sText.length && IsNumber == true; i++) 
			{ 
				Char = sText.charAt(i); 
				if(ValidChars.indexOf(Char) == -1) 
				{
					IsNumber = false;
				}
			}
		}
		else
		{
				IsNumber = false;
		}
	
		return IsNumber
	}
	
	function IsFloat(sText)
	{
		var ValidChars = "0123456789.";
		var IsNumber=true;
		var Char;

		if(sText != "")
		{
			for (i = 0; i < sText.length && IsNumber == true; i++) 
			{ 
				Char = sText.charAt(i); 
				if(ValidChars.indexOf(Char) == -1) 
				{
					IsNumber = false;
				}
			}
		}
		else
		{
				IsNumber = false;
		}
	
		return IsNumber
	}
	
	function doValidate()
	{
		if(studentCount > (preferredCount + generalCount))
		{
			alert("Warning: The amount of student tickets you have chosen exceeds the amount of tickets you have ordered. Please make sure the amount of tickets ordered or the amount of students attending is correct.");
			clearAll();
		}
	}
	
	function clearAll()
	{
		preferredBox.value = "";
		generalBox.value = "";
		studentBox.value = "";
		mailBox.checked = "";
		donationBox.value = "";
		discountBox.value = "";
		studentDiscBox.value = "";
		totalBox.value = "";
		
		studentCount = 0;
		preferredCount = 0;
		generalCount = 0;
		donationAmt = 0;
		mailFee = 0;
		discountPrice = 0;
		studentDiscPrice = 0;
		totalPrice = 0;
		
		loadTxtPrint(document.getElementById("preferred"));
		loadTxtPrint(document.getElementById("general"));
		loadTxtPrint(document.getElementById("student"));	
		loadTxtPrint(document.getElementById("donation"));
		loadTxtPrint(document.getElementById("discount"));
		loadTxtPrint(document.getElementById("stddisc"));
		loadTxtPrint(document.getElementById("total"));
	}
}

PrintLoad = function loadAll()
{
	loadTxtPrint(document.getElementById("firstname"));
	loadTxtPrint(document.getElementById("lastname"));
	loadTxtPrint(document.getElementById("company"));
	loadTxtPrint(document.getElementById("attention"));
	loadTxtPrint(document.getElementById("careof"));
	loadTxtPrint(document.getElementById("address"));
	loadTxtPrint(document.getElementById("city"));
	loadTxtPrint(document.getElementById("province"));
	loadTxtPrint(document.getElementById("postalcode"));
	loadTxtPrint(document.getElementById("country"));
	loadTxtPrint(document.getElementById("homephone"));
	loadTxtPrint(document.getElementById("workphone"));
	loadTxtPrint(document.getElementById("extension"));
	loadTxtPrint(document.getElementById("cellphone"));
	loadTxtPrint(document.getElementById("email"));
	
	loadSelPrint(document.getElementById("event"));

	loadTxtPrint(document.getElementById("preferred"));
	loadTxtPrint(document.getElementById("general"));
	loadTxtPrint(document.getElementById("student"));
		
	loadTxtPrint(document.getElementById("donation"));
	loadTxtPrint(document.getElementById("discount"));
	loadTxtPrint(document.getElementById("stddisc"));
	loadTxtPrint(document.getElementById("total"));
}

window.onload = PrintLoad;