<?php
function wpsc_format_price( $amt, $currency = false ) {
	$currencies_without_fractions = array( 'JPY', 'HUF' );
	if ( ! $currency ) {
		$country = new WPSC_Country( get_option( 'currency_type' ) );
		$currency = $country->get( 'code' );
	}

	$dec = in_array( $currency, $currencies_without_fractions ) ? 0 : 2;
	return number_format( $amt, $dec );
}

function wpsc_format_convert_price( $amt, $from_currency = false, $to_currency = false ) {
	return wpsc_format_price( wpsc_convert_currency( $amt, $from_currency, $to_currency ), $to_currency );
}

function wpsc_get_plaintext_table( $headings, $rows ) {
	$colwidths = array();
	$output = array();
	$alignment = array_values( $headings );
	$headings = array_keys( $headings );
	foreach ( $headings as $heading ) {
		$colwidths[] = strlen( $heading );
	}

	foreach ( $rows as $row ) {
		$i = 0;
		foreach ( $row as $col ) {
			$colwidths[$i] = max( strlen( $col ), $colwidths[$i] );
			$i ++;
		}
	}

	foreach ( $rows as &$row ) {
		$i = 0;
		foreach ( $row as &$col ) {
			$align = ( $alignment[$i] == 'left' ) ? STR_PAD_RIGHT : STR_PAD_LEFT;
			$col = str_pad( $col, $colwidths[$i], ' ', $align );
			$i ++;
		}
		$output[] = implode( '  ', $row );
	}

	$line = array();
	$i = 0;

	foreach ( $colwidths as $width ) {
		$line[] = str_repeat( '-', $width );
		$headings[$i] = str_pad( $headings[$i], $width );
		$i ++;
	}

	$line = implode( '--', $line );
	array_unshift( $output, $line );
	array_unshift( $output, implode( '  ', $headings ) );
	array_unshift( $output, $line );
	$output[] = $line;

	return implode( "\r\n", $output ) . "\r\n";
}
