// Progressbar

var initial = -120;
var imageWidth=240;
var eachPercent = (imageWidth/2)/100;
/************************************************************\
*
\************************************************************/
function setText (id, percent)
{
    $(id+'Text').innerHTML = percent+"%";
}
/************************************************************\
*
\************************************************************/
function display ( id, percentage, color, betraga, betragz, titel )
{	
	if (typeof color == "undefined") {
    color = "1";
  	}
    var percentageWidth = eachPercent * percentage;
    var actualWidth = initial + percentageWidth ;
    document.write('<div style="font-size:10px; font-weight:bold; color:#999; border-bottom:1px dotted #CCC;">' + titel + '</div><div class="barbg"><div class="barinner">Gesamt: ' + formatZahl(betraga,2,true) + ' EUR</div><img id="'+id+'" src="images/percentImage.png" alt="'+percentage+'% "class="percentImage'+color+'" title="' + titel + '" style="background-position: '+actualWidth+'px 0pt;"/> <span id="showpercent">'+percentage+'%</span></div>');
	
}
/************************************************************\
*
\************************************************************/
function emptyProgress(id)
{
    var newProgress = initial+'px';
    $(id).style.backgroundPosition=newProgress+' 0';
    setText(id,'0');
}
/************************************************************\
*
\************************************************************/
function getProgress(id)
{
    var nowWidth = $(id).style.backgroundPosition.split("px");
    return (Math.floor(100+(nowWidth[0]/eachPercent))+'%');
	
}
/************************************************************\
*
\************************************************************/
function setProgress(id, percentage)
{
    var percentageWidth = eachPercent * percentage;
    var newProgress = eval(initial)+eval(percentageWidth)+'px';
    $(id).style.backgroundPosition=newProgress+' 0';
    setText(id,percentage);
}
/************************************************************\
*
\************************************************************/
function plus ( id, percentage )
{
    var nowWidth = $(id).style.backgroundPosition.split("px");
    var nowPercent = Math.floor(100+(nowWidth[0]/eachPercent))+eval(percentage);
    var percentageWidth = eachPercent * percentage;
    var actualWidth = eval(nowWidth[0]) + eval(percentageWidth);
    var newProgress = actualWidth+'px';
    if(actualWidth>=0 && percentage <100)
    {
        var newProgress = 1+'px';
        $(id).style.backgroundPosition=newProgress+' 0';
        setText(id,100);
    }
    else
    {
        $(id).style.backgroundPosition=newProgress+' 0';
        setText(id,nowPercent);
    }
}
/************************************************************\
*
\************************************************************/
function minus ( id, percentage )
{
    var nowWidth = $(id).style.backgroundPosition.split("px");
    var nowPercent = Math.floor(100+(nowWidth[0]/eachPercent))-eval(percentage);
    var percentageWidth = eachPercent * percentage;
    var actualWidth = eval(nowWidth[0]) - eval(percentageWidth);
    var newProgress = actualWidth+'px';
    if(actualWidth<=-120)
    {
        var newProgress = -120+'px';
        $(id).style.backgroundPosition=newProgress+' 0';
        setText(id,0);
        alert('empty');
    }
    else
    {
        $(id).style.backgroundPosition=newProgress+' 0';
        setText(id,nowPercent);
    }
}
/************************************************************\
*
\************************************************************/
function fillProgress(id, endPercent)
{
    var nowWidth = $(id).style.backgroundPosition.split("px");
    startPercent = Math.ceil(100+(nowWidth[0]/eachPercent))+1;
    var actualWidth = initial + (eachPercent * endPercent);

    if (startPercent <= endPercent && nowWidth[0] <= actualWidth)
    {
        plus(id,'1');
        setText(id,startPercent);
        setTimeout("fillProgress('"+id+"',"+endPercent+")",10);
    }
}

/* FORMATING NUMBER */
function formatZahl(zahl, k, fix) {

    if(!k) k = 0;

    var neu = '';

 

	var dec_point = ',';

	var thousands_sep = '.';

 

    // Runden

    var f = Math.pow(10, k);

    zahl = '' + parseInt(zahl * f + (.5 * (zahl > 0 ? 1 : -1)) ) / f ;

 

    // Komma ermittlen

    var idx = zahl.indexOf('.');

 

    // fehlende Nullen einfügen

    if(fix)    {

         zahl += (idx == -1 ? '.' : '' )

         + f.toString().substring(1);

    }

	var sign = zahl < 0;

	if(sign) zahl = zahl.substring(1);

    idx = zahl.indexOf('.');

 

	// Nachkommastellen ermittlen

    if( idx == -1) idx = zahl.length;

    else neu = dec_point + zahl.substr(idx + 1, k);

 

 

    while(idx > 0)    {

        if(idx - 3 > 0)

        neu = thousands_sep + zahl.substring( idx - 3, idx) + neu;

        else

        neu = zahl.substring(0, idx) + neu;

        idx -= 3;

    }

    return (sign ? '-' : '') + neu;

}
