// 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;
	}
	function onChangeIncome()
		{
			switch( document.getElementById( "NetIncome" ).options[document.getElementById( "NetIncome" ).selectedIndex].value )
			{
				case "10000": document.getElementById( "In_TaxOnDrawings" ).value = "1,850"; break;
				case "15000": document.getElementById( "In_TaxOnDrawings" ).value = "3,950";  break;
				case "20000": document.getElementById( "In_TaxOnDrawings" ).value = "6,100";  break;
				case "25000": document.getElementById( "In_TaxOnDrawings" ).value = "8,200";  break;
				case "30000": document.getElementById( "In_TaxOnDrawings" ).value = "10,600"; break;
				case "35000": document.getElementById( "In_TaxOnDrawings" ).value = "14,100"; break;
				case "40000": document.getElementById( "In_TaxOnDrawings" ).value = "17,600"; 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 );
			}
			
			var iProfitMargin = toDbl( document.getElementById( "ProfitMargin" ).value );
			var iResult1 = dblTotal / ( iProfitMargin / 100 );
			var iPWResult = iResult1 / 52 * 1.175;
			
			if ( document.all )
			{
				document.getElementById( "Result_Total1" ).innerText = addCommas( dblTotal );
				document.getElementById( "Result_Total2" ).innerText = addCommas( dblTotal );
				document.getElementById( "Result_Total3" ).innerText = addCommas( iResult1.toFixed(0) );
				document.getElementById( "Result_Total4" ).innerText = addCommas( iPWResult.toFixed(0) );
			}
			else
			{
				document.getElementById( "Result_Total1" ).textContent = addCommas( dblTotal );
				document.getElementById( "Result_Total2" ).textContent = addCommas( dblTotal );
				document.getElementById( "Result_Total3" ).textContent = addCommas( iResult1.toFixed(0) );
				document.getElementById( "Result_Total4" ).textContent = addCommas( iPWResult.toFixed(0) );
			}
			
		}
