/*
 * ____________________________________________________________
 *
 * Original File Name:  combeso.xsl
 * Creation Date:       01/04/2002
 * Description:         plasma injector for our warp kernel
 * ____________________________________________________________

 * ____________________________________________________________
 *
 * Copyright (c) 2002 bestsolution.at Systemhaus GmbH
 * All Rights Reserved.
 * 
 * bestsolution.at MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT 
 * THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR 
 * NON_INFRINGEMENT. 
 * bestsolution.at SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED 
 * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING 
 * THIS SOFTWARE OR ITS DERIVATIVES.
 * ____________________________________________________________

 * ____________________________________________________________
 *
 * $Source: /usr/bestsolution/cvsroot/web/fides/src/htdocs/javascript/BesoUtil.js,v $
 * $Revision: 1.1 $
 * $State: Exp $
 * locked by $Locker:  $
 * checked out with tagname $Name:  $
 * 
 * last modified $Date: 2003-01-09 08:12:21 $ by $Author: tom $
 * ____________________________________________________________
*/

/*
 * This module provides common functions used by many applications
 * most of them are addons to existing objects
 *
 * 1) window-object:
 *      * parseCurrency( value, symbol_name, type )
 *          similar to parseFloat parsing a currencyFormatedString and return the float-value
 *      * parseFromFormatedNumber( number, decimal_sep, thousand_sep, suffix, prefix )
 *          similar to parseFloat parsing a formatedString and return the float-value
 *      * parseFloatComma( val )
 *          parses a number string like parseFloat does but uses the , as digit-seperator
 * 2) document-object:
 *      * getElementById(id)
 *          adds getElementById for versions of IE using document.all
 * 3) Number-object:
 *      * toFixed( decimal_digits )
 *          Browsers like NN >= 6.x and IE >= 5.x provid this function built in. Others
 *          like IE 4.x and e.g. Konqueror for these browsers we implement them
 *      * toCurrencyFormatedString(decimal_digits, symbol_name, type)
 *          Does some nice formating on numbers like you're already familiar when using e.g. Excel
 *      * formatNumber( decimal_digits, decimal_sep, thousand_sep, suffix, prefix )
 *          Formats a number like want it
 * 4) Date-object:
 *      * getDateOutOfString( val ) => static
 *          returns a date out of a string
 *      * makeStringOutOfDate()
 *          returns the Date as a. At the moment e.g. 01.01.2002
*/

var DEBUG = false;

function getElementById(id)
{
    return document.all[id];
}


if( ! document.getElementById )
{
    if( document.all )
    {
        if( DEBUG )
        {
            alert("getElementById");
        }
        
        document.prototype.getElementById = getElementById;
    }
    else
    {
        alert( "Sie können dieses Formular nur mit IE oder einem Browser kompatibel zu NN 6.0 oder höher verwenden." );
    }
}


function toFixed( length )
{
    var calculated = Math.round( this * Math.pow( 10, length ) )/Math.pow( 10, length );
    var parts = (""+calculated).split( "." );
    
    // needed because of f... problems when converting floating values
    // into binary numbers
    if( parts[1] && parts[1].length > length )
    {
        calculated = parts[0] + "." + parts[1].substring( 0, length );
    }
    else if( parts[1] && parts[1].length < length )
    {
        calculated += "0";
    }
    else if( ! parts[1] )
    {
        calculated += ".00";
    }
    
    return calculated.toString();
}


if( ! Number.prototype.toFixed )
{
    if( DEBUG )
    {
        alert( "toFixed" );
    }
    
    Number.prototype.toFixed = toFixed;
}

var charCodes4CurrencySymbols = new Array();
charCodes4CurrencySymbols["EURO"] = 8364;
charCodes4CurrencySymbols["POUND"] = 163;
charCodes4CurrencySymbols["YEN"] = 165;
charCodes4CurrencySymbols["CENT"] = 162;
charCodes4CurrencySymbols["DOLLAR"] = 36;

var seperators = new Array();
seperators["european"] = new Array();
seperators["european"]["digitsep"] = ",";
seperators["european"]["thousandsep"] = ".";
seperators["angloamerican"] = new Array();
seperators["angloamerican"]["digitsep"] = ",";
seperators["angloamerican"]["thousandsep"] = ".";

function toCurrencyFormatedString( decimal_digits, symbol_name, type )
{
    return this.formatNumber( decimal_digits, seperators[type]["digitsep"], seperators[type]["thousandsep"], null, String.fromCharCode(charCodes4CurrencySymbols[symbol_name]) );
}

Number.prototype.toCurrencyFormatedString = toCurrencyFormatedString;

function parseCurrency( value, symbol_name, type )
{
    return parseFromFormatedNumber( value, seperators[type]["digitsep"], seperators[type]["thousandsep"], null, String.fromCharCode(charCodes4CurrencySymbols[symbol_name]) );
}

function formatNumber( decimal_digits, decimal_sep, thousand_sep, suffix, prefix )
{
    var number_string;
    
    if( decimal_digits != -1 )
    {
        number_string = this.toFixed( decimal_digits );
    }
    else
    {
        number_string = this.toString();
    }
    
    
    if( decimal_sep != "." )
    {
        number_string = number_string.replace( ".", decimal_sep );
    }
        
    if( thousand_sep != null )
    {
        var parts  = number_string.split( decimal_sep );
        var point_amount = Math.floor( (parts[0].length+1) / 3 );
        var final_string = "";
        var start;
        var end;
        var i = 0;
        var diff;
        
        for( i = 0; i < point_amount; i++ )
        {
            start = parts[0].length - ( 3*(i+1) );
            end   = parts[0].length - ( 3*i );
            final_string = "." + parts[0].substring( start, end ) + final_string;
        }
        
        diff = parts[0].length - ( final_string.length - point_amount );
        
        if( diff > 0 )
        {
            final_string = parts[0].substring( 0, diff ) + final_string;
        }
        else
        {
            final_string = final_string.substring( 1, final_string.length );
        }
        
        if( parts[1] )
        {
            final_string += decimal_sep+parts[1];
        }
        
        number_string = final_string;
    }
    
    if( suffix )
    {
        number_string += suffix;
    }
    
    if( prefix )
    {
        number_string = prefix + number_string;
    }
    
    return number_string;
}

Number.prototype.formatNumber = formatNumber;


function parseFromFormatedNumber( number, decimal_sep, thousand_sep, suffix, prefix )
{
    if( thousand_sep )
    {
        number = number.replace( new RegExp( "\\.", "g" ), "" );
    }
    
    
    if( suffix )
    {
        number = number.replace( suffix, "" );
    }
    
    if( prefix )
    {
        number = number.replace( prefix, "" );
    }
    
    number = number.replace( decimal_sep, "." );
    
    return parseFloat( number );
}

function parseFloatComma( val )
{
    return parseFloat( val.replace( ",", "." ) );
}


function getDateOutOfString( val )
{
    var vals = val.split( "." );
    
    return new Date( vals[2], vals[1]-1, vals[0] );
}


Date.getDateOutOfString = getDateOutOfString;


function makeStringOutOfDate()
{
    var val = "";
    val  = ((this.getDate()<10)?"0"+this.getDate():this.getDate()) +  ".";
    val += ((this.getMonth()+1<10)?"0"+(this.getMonth()+1):this.getMonth()+1) + ".";
    val += (this.getYear()<1900)?this.getYear()+1900:this.getYear();
    
    return val;
}

Date.prototype.makeStringOutOfDate = makeStringOutOfDate;
