// JScript source code
//
// Copyright AccSys Business Consultants 2007
//
// Written by Paul Challis

	function addCommas(nStr)
	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
	function isNumeric( st )
	{
		var  validChars = "0123456789.,";
		var  bNumeric = true;
		var	 chChar;
		
		for ( i = 0; i < st.length && bNumeric; i++ )
		{
			chChar = st.charAt(i);
			if ( validChars.indexOf( chChar ) == -1 )
				bNumeric = false;
		}
		
		return bNumeric;
	}
	// Removes any non-numeric values, should be used before converting to an int.
	// Note: Removes the commas from input strings as they're only for display
	function stripNumeric( st )
	{
		var  chOut = "";
		var  validChars = "0123456789.";
		var	 chChar;
		
		for ( i = 0; i < st.length; i++ )
		{
			chChar = st.charAt(i);
			if ( validChars.indexOf( chChar ) != -1 )
				chOut += chChar;
		}
		
		return chOut;
	}
	function onValidateNumeric( formField )
	{
		var   bFailed, i;
		
		bFailed = false;

		if ( formField.value != "" )
			bFailed = !isNumeric( formField.value );
		
		if ( bFailed )
		{
			var  strNewValue;
			
			strNewValue = "";
			
			alert( "Please only enter numerics here" );
			
			for (i = 0; i < formField.value.length; i++ )
			{
				if ( ( ( formField.value.substr(i,1) >= '0' ) && ( formField.value.substr(i,1) <= '9' ) ) || ( formField.value.substr(i,1) == ',' ) ||
					( formField.value.substr(i,1) == '.' ) )
					strNewValue += formField.value.substr(i,1);
			}
			
			formField.value = strNewValue;
			return false;
		}
	}
	function toDbl( stVal )
	{
		var		iResult = 0;
		
		if ( isNumeric( stVal ) )
			iResult = Number( stripNumeric( stVal ) );
			
		return iResult;
	}
var iResult_Netincome = 0;

		function onChangeIncome()
		{
			switch( document.getElementById( "NetIncome" ).options[document.getElementById( "NetIncome" ).selectedIndex].value )
			{
				case "10000": document.getElementById( "NIPayable" ).value = "1,850"; iResult_Netincome = 11850; break;
				case "15000": document.getElementById( "NIPayable" ).value = "3,950"; iResult_Netincome = 18950; break;
				case "20000": document.getElementById( "NIPayable" ).value = "6,100"; iResult_Netincome = 26100; break;
				case "25000": document.getElementById( "NIPayable" ).value = "8,200"; iResult_Netincome = 33200; break;
				case "30000": document.getElementById( "NIPayable" ).value = "10,600"; iResult_Netincome = 40600; break;
				case "35000": document.getElementById( "NIPayable" ).value = "14,100"; iResult_Netincome = 49100; break;
				case "40000": document.getElementById( "NIPayable" ).value = "17,600"; iResult_Netincome = 57600; break;
			}
			if ( document.all )
				document.getElementById( "Result_NetIncome" ).innerText = addCommas( iResult_Netincome );
			else
				document.getElementById( "Result_NetIncome" ).textContent = addCommas( iResult_Netincome );
				
			onChange();
		}
		function onInitialCalc()
		{
			onChangeIncome();
		}
		function onChange()
		{
			var		dblTotal = 0;
			var		objects = document.getElementsByTagName( "INPUT" );
			
			for (var i = 0; i < objects.length; i++ )
			{
				if ( objects[i].id.substr(0, 3) == "In_" )
					dblTotal += toDbl( document.getElementById( objects[i].id ).value );
			}
			
			if ( document.all )
			{
				document.getElementById( "Span_Total1" ).innerText = addCommas( dblTotal );
				document.getElementById( "Span_Total2" ).innerText = addCommas( ( dblTotal + iResult_Netincome ) );
				document.getElementById( "Result_RequiredFees" ).innerText = addCommas( ( dblTotal + iResult_Netincome ) );
			}
			else
			{
				document.getElementById( "Span_Total1" ).textContent = addCommas( dblTotal );
				document.getElementById( "Span_Total2" ).textContent = addCommas( ( dblTotal + iResult_Netincome ) );
				document.getElementById( "Result_RequiredFees" ).textContent = addCommas( ( dblTotal + iResult_Netincome ) );
			}

			if ( document.getElementById( "No_Hours" ).value != "" )
			{
				var  iHours = toDbl( document.getElementById( "No_Hours" ).value );
				var  dblHours = ( dblTotal + iResult_Netincome ) / iHours;
				
				if ( document.all )
					document.getElementById( "Result_Perhour" ).innerText = addCommas( dblHours.toFixed(2) );
				else
					document.getElementById( "Result_Perhour" ).textContent = addCommas( dblHours.toFixed(2) );
			}
		}
