﻿//
// Padd characters to a value when the value's number of characters is less
// than the required number of characters. 
//
// Parameters
//      value                       : The value that's padded.
//      requiredNumberOfCharacters  : The required number of characters.
//      paddingCharacter            : The character that's padded to the value.
//      paddingSide                 : The side where the characters are padded to.
//                                    Possible values
//                                    - "left"  : Characters are padded to the left side of the value (default).
//                                    - "right" : Characters are padded to the right side of the value.                           
//
function CharacterPadding( value, requiredNumberOfCharacters, paddingCharacter, paddingSide )
{
    convertedValue = String(value);
    numberOfCharacters = convertedValue.length;
		
    if ( numberOfCharacters < requiredNumberOfCharacters )
    {
        numberOfCharactersToAdd = requiredNumberOfCharacters - numberOfCharacters;
        paddedValue = "";
        for ( character = 0 ; character < numberOfCharactersToAdd ; character++ )
        {
	        paddedValue = paddedValue + paddingCharacter;
        }
        
        if ( paddingSide == "right" ) paddedValue = convertedValue + paddedValue;
        else                          paddedValue = paddedValue + convertedValue;
    }
    else
    {
        paddedValue = convertedValue;
    }

    return (paddedValue);
}

//
// Padd the zero character "0" to the value when the value's number of 
// characters is less than the required number of digits.
//
// Parameters
//      value                   : The value that's padded.
//      requiredNumberOfDigits  : The required number of digits.
//
function ZeroPadding( value, requiredNumberOfDigits )
{
    return CharacterPadding( value, requiredNumberOfDigits, "0", "left" );
}

//
// Formats a number.
//
// Parameters
//      value                    : The value to be formatted.
//      requiredNumberOfDecimals : The required number of decimals.    
//
function FormatNumber( value, requiredNumberOfDecimals ) 
{   
    var decimalSeparator = ".";
    
    if ( value == "" ) return ("");
    
    if ( value.indexOf(",") != -1 ) 
    {
        decimalSeparator = ",";
        value = value.replace( ",", "." );
    }

    //round method only works with a decimal separator of "."! 
    value = String(Math.round( value*Math.pow(10,requiredNumberOfDecimals) ) / Math.pow(10,requiredNumberOfDecimals)); 
    indexPeriod = value.indexOf("."); 

    //no period found (no decimals)
    if ( indexPeriod == - 1 ) 
    { 
        value = value + decimalSeparator; 
        for ( indexDecimal = 0 ; indexDecimal < requiredNumberOfDecimals ; indexDecimal++ ) 
        { 
            value = value + "0"; 
        } 
    } 
    else 
    { 
        numberOfDecimals = value.length - (indexPeriod + 1); 

        if ( numberOfDecimals < requiredNumberOfDecimals ) 
        { 
            numberOfDecimalsToAdd = requiredNumberOfDecimals - numberOfDecimals; 

            for ( indexDecimal = 0 ; indexDecimal < numberOfDecimalsToAdd ; indexDecimal++ ) 
            { 
                value = value + "0"; 
            } 
        } 
    } 

    return (value); 
}

// 
// Verwijdert alle spaties uit de waarde. 
// 
function Trim( value ) 
{ 
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,''); 
  
  return value; 
} 

// 
// Positions a control relative to another control. 
// 
function PositionControlRelative( controlName, controlToPositionName, offsetHorizontal, offsetVertical ) 
{ 
    var control = document.getElementById( controlName ); 
    var controlToPosition = document.getElementById( controlToPositionName );      
        
    if ( control != null && controlToPosition != null ) 
    { 
        var controlLeft = control.offsetLeft; 
        var controlTop = control.offsetTop; 
        
        var parentControl = control.offsetParent;         
        while (parentControl) 
        { 
            controlLeft += parentControl.offsetLeft; 
            controlTop += parentControl.offsetTop; 
            parentControl = parentControl.offsetParent; 
        } 
        
        controlToPosition.style.left = controlLeft + offsetHorizontal + "px"; 
        controlToPosition.style.top = controlTop + offsetVertical + "px"; 
    } 
}

//
// Description:  
//      Opens a browser window which display the passed address (URL).
// 
// Parameters:
//      strAddress: The address (URL) of the page to be shown.
//      strTarget: The target of the load page.
//
function OpenDefaultWindow( strAddress, strTarget )
{
    objLink = window.open( strAddress, strTarget );
}

//
// Description:  
//      Opens a browser window which display the passed address (URL).
// 
// Parameters:
//      strAddress: The address (URL) of the page to be shown.
//      strTarget: The target of the load page.
//      intwidth: width of the browser window
//      intheight: height of the browser window
//
function OpenWindow( strAddress, strTarget, intwidth, intheight )
{
    objLink = window.open( strAddress, strTarget, "width=" + intwidth + ", height=" + intheight );
}
