// JavaScript Document

<!--
	var intNumberPerPack = 20;

	var intUnitCost;
	var intFrequency;
	var intDuration;

	var intDailyCost;
	var intWeeklyCost;
	var intMonthlyCost;
	var intAnnualCost;
	var intTotalCost;

	function keyPressEvent(e) {
		// IF KEY WAS 'ENTER' MAKE CALCULATION

		if ( ( window	.event.keyCode == 13 ) || ( window.event.keyCode == 3 ) ) {
			document.forms['calculator'].suppliedCost.blur();
			document.forms['calculator'].suppliedFrequency.blur();
			document.forms['calculator'].suppliedPeriod.blur();
			document.forms['calculator'].calculatedWeeklyCost.blur();
			document.forms['calculator'].calculatedMonthlyCost.blur();
			document.forms['calculator'].calculatedAnnualCost.blur();
			document.forms['calculator'].calculatedTotalCost.blur();
			document.forms['calculator'].yearsSmoked.blur();

			MakeCalculation();
		}
	}

	function MakeCalculation() {

		// CHECK FOR A DOLLAR SIGN
		var strTemp = document.forms['calculator'].suppliedCost.value;
		arrTemp = strTemp.match("$");

		var strTemp = document.forms['calculator'].suppliedCost.value;
		var intPosition = strTemp.indexOf("$");
		var intLength = strTemp.length;
		if ( intPosition != -1) { 
			if ( intPosition == 0 ) { 
				strTemp = strTemp.substr(1,intLength);
				document.forms['calculator'].suppliedCost.value = strTemp;
			} else if ( intPosition > 0) {
				strTemp = strTemp.substr((intPosition+1),intLength);
				document.forms['calculator'].suppliedCost.value = strTemp;
			}
		}

		// RECORD THE SUPPLIED VALUES
		intUnitCost = document.forms['calculator'].suppliedCost.value; 
		intFrequency = document.forms['calculator'].suppliedFrequency.value;
		intDuration = document.forms['calculator'].suppliedPeriod.value;

		// DEFINE A REGULAR EXPRESSION TO USE TO
		// CHECK OUR USER HAS ENTERED POSITIVE NUMBERS
		var strRegularExpression = new RegExp("^[0-9.,]*$");
		var strRegularExpressionA = new RegExp("^[0-9]*.[0-9]$");
		//var strRegularExpressionB = new RegExp("^[0-9]*.[0-9][0-9]$");

		// CARRY OUT THE CHECK AND RECORD THE RESULT 
		// FOR EACH SUPPLIED ITEM
		arrUnitCost = intUnitCost.match(strRegularExpression);
		arrFrequency = intFrequency.match(strRegularExpression);
		arrDuration = intDuration.match(strRegularExpression);

		// CHECK TO SEE IF ANY OF THE ITEMS ARE INCORRECT
		// IF THEY ARE, THE RECORD WILL BE NULL
		if ( ( arrUnitCost == null ) || ( arrUnitCost == '' ) ||  ( arrFrequency == null ) || ( arrFrequency == '' ) ||  ( arrDuration == null ) || ( arrDuration == '' ) ) {


			strErrorMessage = 'The calculation cannot be completed. \nPlease check the following fields and \nensure they are entered as numbers.';

			// CHECK FOR SPECIFIC MISTAKES, AND ADD TO THE ERROR MESSAGE IF NECESSARY
			if ( arrUnitCost == null || arrUnitCost == '' ) { strErrorMessage = strErrorMessage + '\n\nThe cost of 20 cigarettes. \nPlease do not include a dollar sign.'; }
			if ( arrFrequency == null || arrFrequency == '' ) { strErrorMessage = strErrorMessage + '\n\nThe number you smoke per day.'; }
			if ( arrDuration == null || arrDuration == '' ) { strErrorMessage = strErrorMessage + '\n\nThe number of years you have been smoking.'; }

			// DISPLAY ERROR MESSAGE
			alert(strErrorMessage);

		} else {

			// THE SUPPLIED FIGURES PASS THE TEST SO WE CAN NOW USE THEM
			// TO CALCULATE THE VARIOUS COSTS
			intUnitCost = document.forms['calculator'].suppliedCost.value / intNumberPerPack; 
			intFrequency = parseInt(document.forms['calculator'].suppliedFrequency.value);
			intDuration = parseInt(document.forms['calculator'].suppliedPeriod.value);

			intDailyCost = ( intUnitCost*intFrequency );
			intDailyCost = Math.round(intDailyCost*100)/100;

			intWeeklyCost = intDailyCost*7;
			intWeeklyCost = Math.round(intWeeklyCost*100)/100;

			intMonthlyCost = (intDailyCost*365)/12;
			intMonthlyCost = Math.round(intMonthlyCost*100)/100;

			intAnnualCost = (intDailyCost*365);
			intAnnualCost = Math.round(intAnnualCost*100)/100;

			intTotalCost = intAnnualCost*intDuration;
			intTotalCost = Math.round(intTotalCost*100)/100;

			//POPULATE THE CALCULATION FORM FIELDS
			document.forms['calculator'].calculatedWeeklyCost.value = '$'+intWeeklyCost;
			document.forms['calculator'].calculatedMonthlyCost.value = '$'+intMonthlyCost;
			document.forms['calculator'].calculatedAnnualCost.value = '$'+intAnnualCost;
			document.forms['calculator'].calculatedTotalCost.value = '$'+intTotalCost;
			document.forms['calculator'].yearsSmoked.value = intDuration;

			// ENSURE THAT NUMBERS APPEAR AS WHOLE FIGURES INCLUDING CENTS
			var strTemp = document.forms['calculator'].calculatedWeeklyCost.value;
			var inPosition = strTemp.indexOf(".");
			var intLength = strTemp.length;
			var intIndicator = intLength - inPosition;
			if ( intIndicator < 3) { strTemp = strTemp + '0'; document.forms['calculator'].calculatedWeeklyCost.value = strTemp; }

			var strTemp = document.forms['calculator'].calculatedMonthlyCost.value;
			var inPosition = strTemp.indexOf(".");
			var intLength = strTemp.length;
			var intIndicator = intLength - inPosition;
			if ( intIndicator < 3) { strTemp = strTemp + '0'; document.forms['calculator'].calculatedMonthlyCost.value = strTemp; }

			var strTemp = document.forms['calculator'].calculatedAnnualCost.value;
			var inPosition = strTemp.indexOf(".");
			var intLength = strTemp.length;
			var intIndicator = intLength - inPosition;
			if ( intIndicator < 3) { strTemp = strTemp + '0'; document.forms['calculator'].calculatedAnnualCost.value = strTemp; }

			var strTemp = document.forms['calculator'].calculatedTotalCost.value;
			var inPosition = strTemp.indexOf(".");
			var intLength = strTemp.length;
			var intIndicator = intLength - inPosition;
			if ( intIndicator < 3) { strTemp = strTemp + '0'; document.forms['calculator'].calculatedTotalCost.value = strTemp; }

			// NO WORK OUT HTE COMPARISON COSTS
			/* EUROPEAN HOLIDAY
			var intEuropeanHolidayValue = 2500;
			var intMP3Value = 150;
			var intCDsDVDsEtcValue = 25;
			var intCarValue = 12000;

			document.forms['calculator'].europeanHoliday.value = intTotalCost/intEuropeanHolidayValue;
			document.forms['calculator'].mp3.value = intTotalCost/intMP3Value;
			document.forms['calculator'].cds.value = intTotalCost/intCDsDVDsEtcValue;
			document.forms['calculator'].car.value = intTotalCost/intCarValue;

			if ( document.forms['calculator'].europeanHoliday.value < 1 ) { document.forms['calculator'].europeanHoliday.value = '-'; } else { document.forms['calculator'].europeanHoliday.value = Math.floor(document.forms['calculator'].europeanHoliday.value); }
			if ( document.forms['calculator'].mp3.value < 1 ) { document.forms['calculator'].mp3.value = '-'; } else { document.forms['calculator'].mp3.value = Math.floor(document.forms['calculator'].mp3.value); }
			if ( document.forms['calculator'].cds.value < 1 ) { document.forms['calculator'].cds.value = '-'; } else { document.forms['calculator'].cds.value = Math.floor(document.forms['calculator'].cds.value); }
			if ( document.forms['calculator'].car.value < 1 ) { document.forms['calculator'].car.value = '-'; } else { document.forms['calculator'].car.value = Math.floor(document.forms['calculator'].car.value); }
			*/
			// DISPLAY ANSWERS LAYER
			var lyr = new getObj('answers');
			lyr.style.display = "block";
			
			// SCROLL PAGE SO USER CAN SEE ANSWERS
			window.scroll(0, 200);
		}

	}

	document.onkeypress = keyPressEvent;

//-->
