
if( document.all && !document.getElementById ) {
    document.getElementById = function( id ) {
         return document.all[ id ];
    }
}


function sync_buy_credits_to_dollars(){

	var credits = document.getElementById( 'CreditAmount' );
	var dollars = document.getElementById( 'DollarAmount' );


	credits.value = removeCommas( credits.value );

	if( credits.value == '' ) return true;

	if( ! isNaturalNumber( credits.value ) ) return false;

	dollars.value = buy_credits_to_dollars( credits.value );
	//credits.value = buy_dollars_to_credits( dollars.value  );

	sync_buy_dollars_to_total();
}

function sync_buy_dollars(){

	var dollars = document.getElementById( 'DollarAmount' );

	dollars.value = removeCommas( dollars.value );
	if( dollars.value == '' ) return true;
	if( ! isPositiveNumber( dollars.value ) ) return false;
	dollars.value = Math.round( dollars.value * 100 ) / 100;
	dollars.value = format_currency( dollars.value );

	sync_buy_dollars_to_credits();
	sync_buy_dollars_to_total();
}

function sync_buy_dollars_to_credits(){

	var dollars = document.getElementById( 'DollarAmount' );
	var credits = document.getElementById( 'CreditAmount' );


	credits.value = buy_dollars_to_credits( dollars.value );
}

function sync_buy_dollars_to_total(){
	var dollars = document.getElementById( 'DollarAmount' );
	var fee = document.getElementById( 'FeeAmount' );
	var total = document.getElementById( 'TotalAmount' );

	total.value = format_currency( parseFloat(dollars.value) + parseFloat(fee.value) );
}

function sync_buy_total_to_dollars(){

	var dollars = document.getElementById( 'DollarAmount' );
	var fee = document.getElementById( 'FeeAmount' );
	var total = document.getElementById( 'TotalAmount' );

	total.value = removeCommas( total.value );
	if( total.value == '' ) return true;
	if( ! isPositiveNumber( total.value ) ) return false;
	total.value = Math.round( total.value * 100 ) / 100;
	total.value = format_currency( total.value );


	dollars.value = format_currency( parseFloat(total.value) - parseFloat(fee.value) );
	sync_buy_dollars_to_credits();
}

function buy_credits_to_dollars( credit_amount ){
//	cents = Math.ceil( 100 * ( credit_amount / buy_rate + buy_fee ) );
	return format_currency( Math.ceil( 100 * ( credit_amount / buy_rate ) ) / 100 );

}

function format_currency( amount ){
	var cents = amount * 100;
	var dollars = Math.floor( cents / 100 );
	cents = '' + Math.round((cents % 100));

	if( cents != 0 ){

		cents = '' + cents;

		if( cents.length == 1 ) cents = '0' + cents;

		dollars = '' + dollars + '.' + cents;
	}

	return dollars;
}

function buy_dollars_to_credits( dollar_amount ){
//	return Math.floor( ( dollar_amount - buy_fee ) * buy_rate );

	return Math.floor( ( dollar_amount ) * buy_rate );
}

function removeCommas( s ){
//alert( 'removing Commas' );
	return s = s.replace(/,/g, '');

}

function isNaturalNumber(x) {
	if( isNaN( parseInt( x ) ) ) return false;
	if( ! x > 0 ) return false;
	return true;
}

function isPositiveNumber(x) {
	if( isNaN( parseFloat( x ) ) ) return false;
	if( ! x > 0 ) return false;
	return true;
}

function isNumeric( field ){
	var value = document.getElementById( field ).value;



	var stripped = value.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters


	if (isNaN(parseInt(stripped))) {
	   var error = "The phone number contains illegal characters.";
	}

	if (!(stripped.length == 10)) {
		var error = "The phone number is the wrong length. Make sure you included an area code.";
	}

	if( error ){
		alert( error );
		return false;
	}

	return true;
}

