function isDigit( c )
{
	return ( ( c >= "0" ) && ( c <= "9" ) )
}

function isInteger( s )
{
	var i;
	
	for ( i = 0; i < s.length; i++ )
	{
		if ( !isDigit( s.charAt( i ) ) )
			return false;
	}
	
	return true;
}

// Return input value, or -1 if not valid.
function ValueOf( FieldName )
{
	var field = document.getElementById( FieldName )
	var s = field.value;

	// Strip leading/trailing spaces.
    s = (s.replace(/^\W+/,'')).replace(/\W+$/,'');

	if ( s == '' )
		return 0;
	
	if ( !isInteger( s ) )
	{
		alert( "ERROR: Value must be a whole number in the range 0 to 999999." );
		field.focus();
		return -1;
	}
		
	var num = parseInt( s, 10 );

	return ( (( num >= 0 ) && ( num <= 999999 )) ? num : -1 );
}

// Application's I/O requirements.
var ains, aouts, dins, douts, encs, rlysdi, rlysdo, rlysai, rlysao, cntrs, cbllen18, dinrail;

function GetRequirements()
{
	// Fetch and validate the i/o quantity fields.
	if ( ( ains    = ValueOf( 'ains'   ) ) == -1 ) return false;
	if ( ( aouts   = ValueOf( 'aouts'  ) ) == -1 ) return false;
	if ( ( dins    = ValueOf( 'dins'   ) ) == -1 ) return false;
	if ( ( douts   = ValueOf( 'douts'  ) ) == -1 ) return false;
	if ( ( encs    = ValueOf( 'encs'   ) ) == -1 ) return false;
	if ( ( rlysdi  = ValueOf( 'rlysdi' ) ) == -1 ) return false;
	if ( ( rlysdo  = ValueOf( 'rlysdo' ) ) == -1 ) return false;
	if ( ( rlysai  = ValueOf( 'rlysai' ) ) == -1 ) return false;
	if ( ( rlysao  = ValueOf( 'rlysao' ) ) == -1 ) return false;
	if ( ( cntrs = ValueOf( 'cntrs' ) ) == -1 ) return false;

	// Get radio buttons.
	cbllen18 = document.testform.cbllen18.checked;
	dinrail = document.testform.dinrailyes.checked;

	return true;
}

function SetField( FieldName, Value )
{
	document.getElementById( FieldName ).innerHTML = Value;
}

function ResetInput( FieldName )
{
	document.getElementById( FieldName ).value = '';
}

function GetPrice( ProductId, Quantity )
{
	var suffix;
	switch( Quantity )
	{
		case 0: return 0;
		case 1: suffix = "_a"; break;
		default: suffix = "_b"; break;
	}

	var price = parseFloat( document.getElementById( "price_" + ProductId + suffix ).innerHTML );

	if ( isNaN( price ) )
		price = 0;

	return price;
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function CommaFormatted(amount)
{
	var delimiter = ",";
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

function Formatted(amount)
{
	return CommaFormatted( CurrencyFormatted( amount ) );
}


// Display line item info and return extended item price.
function SetLineItem( ProductId, Quantity )
{
	var price = GetPrice( ProductId, Quantity );
	var ext = Quantity * price;

	SetField( 'qty_' + ProductId, ( Quantity > 0 ) ? Quantity : '&nbsp;' );
	SetField( 'ea_'  + ProductId, ( Quantity > 0 ) ? ( ( price == 0 ) ? '*' : Formatted( price ) ) : '&nbsp;' );
	SetField( 'ext_' + ProductId, ( Quantity > 0 ) ? ( ( price == 0 ) ? '*' : Formatted( ext ) ) : '&nbsp;' );

	return ext;
}

function ResetSystem()
{
	ResetInput( 'ains' );
	ResetInput( 'aouts' );
	ResetInput( 'dins' );
	ResetInput( 'douts' );
	ResetInput( 'encs' );
	ResetInput( 'cntrs' );
	ResetInput( 'rlysdi' );
	ResetInput( 'rlysdo' );
	ResetInput( 'rlysai' );
	ResetInput( 'rlysao' );

	document.testform.cbllen18.checked  = true;
	document.testform.dinrailyes.checked = true;

	BuildSystem();
}

function max( a, b )
{
	return ( ( a > b ) ? a : b );
}

function BuildSystem()
{
	// Fetch and validate the i/o quantity fields.
	if ( !GetRequirements() )
		ains = aouts = dins = douts = encs = rlysdi = rlysdo = rlysai = rlysao = cntrs = 0;

	// Compute the unit quantities ----------------------------

	// Relay boards.
	var ntbrly = Math.floor( ( 23 + rlysdi + rlysdo + rlysai + rlysao ) / 24 );
	var nb720rb = ( dinrail ? 0 : ntbrly );
	var nb720rbdin = ( dinrail ? ntbrly : 0 );

	// 7505 termination boards.
	var ntbdio = Math.floor( ( 23 + dins + douts ) / 24 );
	var ntbain = ( 15 + ains ) >> 4;
	var ntbaout = ( 3 + aouts ) >> 2;
	var ntbaio = max( ntbain, ntbaout );
	var nb7505tdin = ntbdio + ntbaio; 

	// 7503 termination boards.
	var ntbenc = Math.floor( ( 2 + encs + cntrs ) / 3 );
	var nb7503tdin = ntbenc;

	// 7505 cables.
	var n7501c = nb7505tdin + ntbrly;
	var nb7501c = ( cbllen18 ? n7501c  : 0 );
	var nb7501c1 = ( cbllen18 ? 0 : n7501c );

	// 7503 cables.
	var nb7503c = ( cbllen18 ? nb7503tdin : 0 );
	var nb7503c1 = ( cbllen18 ? 0 : nb7503tdin );

	// 626 boards.
	var nb626enc = Math.floor( ( 1 + ntbenc ) / 2 );
	var nb626dio = Math.floor( ( 1 + ntbdio + ntbrly ) / 2 );
	var nb626aio = ntbaio;
	var nb626 = max( nb626enc, max( nb626dio, nb626aio ) );

	// Display line items and compute total cost.
	var total = 0;
	total += SetLineItem( '626',			nb626 );
	total += SetLineItem( '7505TDIN',		nb7505tdin );
	total += SetLineItem( '7503TDIN',		nb7503tdin );
	total += SetLineItem( '7501C',			nb7501c );
	total += SetLineItem( '7501C1',			nb7501c1 );
	total += SetLineItem( '7503C',			nb7503c );
	total += SetLineItem( '7503C1',			nb7503c1 );
	total += SetLineItem( '720RB',			nb720rb );
	total += SetLineItem( '720RBDIN',		nb720rbdin );
	total += SetLineItem( 'RELAY_G4IDC5',	rlysdi );
	total += SetLineItem( 'RELAY_G4ODC5',	rlysdo );
	total += SetLineItem( 'RELAY_G4IAC5A',	rlysai );
	total += SetLineItem( 'RELAY_G4OAC5A',	rlysao );
	SetField( 'ext_total', ( total > 0 ) ? Formatted( total ) : '&nbsp;' );
}





































