// Tool:   GDK_FORMATTING.js
// Author: Anthony James Blackshaw
// Date:   11-2002

// Getme Ltd (c)2002. All rights reserved.

/*
The GDK_FORMATTING object supports for the formatting of data via the client-side
browser.
*/

/*
	<script type="text/javascript" src="GDK_formatting.js"></script>

	<script type="text/javascript">
	<!--
			
		formatting = new GDK_FORMATTING();
			
	-->
	</script>
*/

function GDK_FORMATTING()
	{
	// Public
		
	// Properties
	this.version = 0.1;
	
	// Methods
	this.lTrim = lTrim_GDK_FORMATTING;
	this.rTrim = rTrim_GDK_FORMATTING;
	this.trim = trim_GDK_FORMATTING;
	this.removeWhitespaces = removeWhitespaces_GDK_FORMATTING;
	this.hex2Dec = hex2Dec_GDK_FORMATTING;
	this.dec2Hex = dec2Hex_GDK_FORMATTING;
	this.readHsdSingle = readHsdSingle_GDK_FORMATTING;
	this.writeHsdSingle = writeHsdSingle_GDK_FORMATTING;
	
	// Private
	
	// Properties
	this._hex = new Array
		( 
		'0',
		'1',
		'2', 
		'3', 
	 	'4', 
		'5',
		'6', 
		'7', 
		'8',
		'9', 
		'A', 
		'B', 
		'C', 
		'D',
		'E',
		'F' 
		);

	// Methods
		
	}
	
function lTrim_GDK_FORMATTING( str )
	{
	// Trim all white spaces left of string
	return str.replace( /^\s*/, "" );
	}

function rTrim_GDK_FORMATTING( str )
	{
	// Trim all white spaces right of string
	return str.replace( /\s*$/, "" );
	}

function trim_GDK_FORMATTING( str )
	{
	// Trim all white space both left and right of string
	return this.rTrim( this.lTrim( str ) );
	}
	
function removeWhitespaces_GDK_FORMATTING( str )
	{
	// Trim all white from the string
	return str.replace( /\s*/g, "" );
	}
	
function hex2Dec_GDK_FORMATTING( str )
	{
	// Convert hexidecimal value to decimal value
	return parseInt( str, 16 );
	}
	
function dec2Hex_GDK_FORMATTING( str )
	{
	hexStr = '';
	
	// Convert decimal value to hexidecimal value
	do
		{
		// Divide str by 16, then round down
		roundedDown = Math.floor( str / 16 );
			
		// Calculate the difference between str and roundedDown
		difference = str - ( roundedDown * 16 );
			
		// Lookup difference aa hex value, append the value to hexStr
		hexStr = this._hex[ difference ] + hexStr;
			
		// Move str to next digit
		str = roundedDown;
		}
		while( roundedDown > 15 ); // Repeat until last digit
	
	// Append the final left digit to hexStr
	hexStr = this._hex[ str ] + hexStr;
	
	return hexStr;
	}

function readHsdSingle_GDK_FORMATTING( hsd )
{
    // This script takes a single row of hsd data and converts it
    // into an array of values. 
    
    var characterList = hsd.split( '' );
    
    var skipCharacter = 0;
    var inRecord = 0;
    var recordArray = new Array();   
    var field = '';                
    
    for ( characterIndex in characterList )
    {
        character = characterList[ characterIndex ];
    
        if ( skipCharacter )
        {
            if ( inRecord )
            {
                field += character;
            }            

            skipCharacter = 0;
            
            if ( 1 ) { continue };
        }                
    
        if ( character == '\\' )
        {
            skipCharacter = 1;
        }                    
        else if ( character == '#' )
        {                    
            recordArray.push( field );

            field = '';
        }
        else if ( character == '{' )
        {
            inRecord = 1;
        }
        else if ( character == '}' )
        {
            inRecord = 0;
        }
        else
        {
            if ( inRecord )
            {
                field += character;
            }                        
        }                
    }
    
    return recordArray;
}

function writeHsdSingle_GDK_FORMATTING( record )
{     
    // This script takes an array of values and converts it into a
    // single row or hsd data.
                    
    var hsdString = '{';           
                
    for ( field in record )
    {
        var value = String( record[ field ] );
    
        value = value.replace( /\\/g , '\\\\' );    
        value = value.replace( /#/g, '\\#' );
        value = value.replace( /{/g, '\\{' );
        value = value.replace( /}/g, '\\}' );
        value = value.replace( /"/g, '\\"' );
        
        hsdString += value + '#';
    }       
    
     hsdString += '}';
     
     return hsdString;
}
