function FieldCheck( pWhich, pField )
{  
	switch ( pWhich ) {
		case 1: testString = $( '#monthlyAmount').attr( 'value' );   break;
		case 2: testString = $( '#monthlyPayment').attr( 'value' );  break;
		case 3: testString = $( '#monthlyInterest').attr( 'value' ); break;
		case 4: testString = $( '#monthlyResult').attr( 'value' );   break;
		case 5: testString = $( '#monthlyIntpaid').attr( 'value' );  break;

		case 11: testString = $( '#affordAmount').attr( 'value' );   break;
		case 12: testString = $( '#affordPayment').attr( 'value' );  break;
		case 13: testString = $( '#affordInterest').attr( 'value' ); break;
		case 14: testString = $( '#affordResult').attr( 'value' );   break;
		case 15: testString = $( '#affordIntpaid').attr( 'value' );  break;
	}
	
	if ( testString != '' ) {

		var textLen   = testString.length;
		var newPrefix = '';
		var newSuffix = '';
		var newResult = '';
		var testChar  = '';
		var whichPart = 0;

		for ( currPos = 0; currPos < textLen; currPos++ ){
			testChar = testString.substr( currPos, 1 );
			
			if ( ( testChar >= '0' && testChar <= '9' ) || testChar == '.'  ) {
				if ( testChar == '.' ) {
					whichPart = 1;
					newSuffix = newSuffix + testChar;
				} else {
					if ( whichPart == 0 ) {
						newPrefix = newPrefix + testChar;
					} else {
						newSuffix = newSuffix + testChar;
						if ( newSuffix.length == 3 ) break;
					}
				}
			}
		}
		
		switch ( newPrefix.length ) {
			case 4: newPrefix = newPrefix.substr( 0, 1 ) + ',' + newPrefix.substr( 1, 3 ); break;
			case 5: newPrefix = newPrefix.substr( 0, 2 ) + ',' + newPrefix.substr( 2, 3 ); break;
			case 6: newPrefix = newPrefix.substr( 0, 3 ) + ',' + newPrefix.substr( 3, 3 ); break;
			case 7: newPrefix = newPrefix.substr( 0, 1 ) + ',' + newPrefix.substr( 1, 3 ) + ',' + newPrefix.substr( 4, 3 ); break;
			case 8: newPrefix = newPrefix.substr( 0, 2 ) + ',' + newPrefix.substr( 2, 3 ) + ',' + newPrefix.substr( 5, 3 ); break;
			case 9: newPrefix = newPrefix.substr( 0, 3 ) + ',' + newPrefix.substr( 3, 3 ) + ',' + newPrefix.substr( 6, 3 ); break;
		}
				
		if ( newPrefix.length > 0 || newSuffix.length > 0 ) {
			if ( false && pField == 1 ) newResult = '$';
			if ( newPrefix.length > 0 ) newResult = newResult + newPrefix;

			if ( newSuffix.length > 0 ) {
				if      ( pField == 2 )                                             newResult = newResult + newSuffix;
				else if ( newSuffix.length == 1 )                                   newResult = newResult + newSuffix;
				else if ( newSuffix.length == 2 && ( pWhich == 3 || pWhich == 6 ) ) newResult = newResult + newSuffix + '0';
				else                                                                newResult = newResult + newSuffix;
			}

		}

		switch ( pWhich ) {
			case 1: testString = $( '#monthlyAmount').attr( 'value', newResult );   break;
			case 2: testString = $( '#monthlyPayment').attr( 'value', newResult );  break;
			case 3: testString = $( '#monthlyInterest').attr( 'value', newResult ); break;
			case 4: testString = $( '#monthlyResult').attr( 'value', newResult );   break;
			case 5: testString = $( '#monthlyIntpaid').attr( 'value', newResult );  break;

			case 11: testString = $( '#affordAmount').attr( 'value', newResult );   break;
			case 12: testString = $( '#affordPayment').attr( 'value', newResult );  break;
			case 13: testString = $( '#affordInterest').attr( 'value', newResult ); break;
			case 14: testString = $( '#affordResult').attr( 'value', newResult );   break;
			case 15: testString = $( '#affordIntpaid').attr( 'value', newResult );  break;
		}
	}
}


function MonthlyPayment()
{
	var Amount   = parseFloat( $( '#monthlyAmount').attr( 'value' ).replace( /[$,]/g, '' ) );

	var Payment  = parseFloat( $( '#monthlyPayment').attr( 'value' ).replace( /[$,]/g, '' ) );
	var Interest = parseFloat( $( '#monthlyInterest').attr( 'value' ) );
	var Months   = parseInt( $( '#monthlyMonths option:selected').val() );

	if ( isNaN( Amount ) )   Amount   = 0;
	if ( isNaN( Payment ) )  Payment  = 0;
	if ( isNaN( Interest ) ) Interest = 0;

	Amount = Amount - Payment;

	var temp1 = parseFloat( ( Interest / 100 ) / 12 );
	var temp2 = parseFloat( Math.pow( ( temp1 + 1 ), Months ) );

	if ( temp2 <= 1 ) var NewResult = Amount / Months;
	else              var NewResult = parseFloat( ( Amount * ( ( temp1 * temp2 ) / ( temp2 - 1 ) ) * 100 ) / 100 );
	
	IntPaid = ( NewResult * Months ) - Amount;

	$( '#monthlyResult').attr( 'value', NewResult );
	$( '#monthlyIntpaid').attr( 'value', IntPaid );
	
	FieldCheck( 4, 1 );
	FieldCheck( 5, 1 );
}

function AffordablePayment()
{
	var Amount   = parseFloat( $( '#affordAmount').attr( 'value' ).replace( /[$,]/g, '' ) );
	var Payment  = parseFloat( $( '#affordPayment').attr( 'value' ).replace( /[$,]/g, '' ) );
	var Interest = parseFloat( $( '#affordInterest').attr( 'value' ) );
	var Months   = parseInt( $( '#affordMonths option:selected').val() );

	if ( isNaN( Amount ) )   Amount   = 0;
	if ( isNaN( Payment ) )  Payment  = 0;
	if ( isNaN( Interest ) ) Interest = 0;

	var temp1 = parseFloat( ( Interest / 100 ) / 12 );
	var temp2 = parseFloat( Math.pow( ( 1 + temp1 ), Months ) );

	if ( ( Interest / 100 ) != 0 ) var NewResult = ( Amount * ( ( temp2 - 1 ) / ( temp1 * temp2 ) ) ) + Payment;
	else                           var NewResult = ( Amount * Months ) + Payment;

	IntPaid = ( ( Amount * Months ) - NewResult ) + Payment;

	$( '#affordResult').attr( 'value', NewResult );
	$( '#affordIntpaid').attr( 'value', IntPaid );

	FieldCheck( 14, 1 );
	FieldCheck( 15, 1 );
}

