/**
 * Fahrzeugdetails.js
 * @author Elko Panzyk, Panzyk Network
 * $Revision: 273 $
 * $Id: Fahrzeugdetails.js 273 2008-09-22 00:58:04Z panzyk $
 */


/**
 * float2euro
 * Wandelt eine Floatzahl in ein lesefreundliches Format
 * @param float Zahl;
 * @param integer AnzahlNullen ( 0-4 )
 * @return string Zahl;
 * @author Elko Panzyk, Panzyk Network
 * @version 1.1
 */
function float2euro ( f_Zahl, i_Nullen ) {
    var s_Vorzeichen = "";
    if( f_Zahl < 0 ) { f_Zahl = f_Zahl * -1; s_Vorzeichen = "-"; }
    var i_Faktor = ( i_Nullen > 0 )? Math.pow(10, i_Nullen) : 1;
    f_Zahl = (Math.round( f_Zahl*i_Faktor )/i_Faktor);
	var a_Zahl = f_Zahl.toString().split (".");
	if ( typeof(a_Zahl[1] ) === "undefined" ){ a_Zahl [1] = "00000"; }
	if ( a_Zahl [1].length === 1 ) { a_Zahl [1] += "0000"; }
	
	var i_Position = a_Zahl[0].length;
	while ( i_Position > 0 ){ 
		i_Position -= 3; if (i_Position <= 0) { break; }
		a_Zahl[0] = a_Zahl[0].substring( 0, i_Position ) + '.' + a_Zahl[0].substring( i_Position, a_Zahl[0].length );
		}
	
	a_Zahl[1]=( i_Nullen !== null ? a_Zahl[1].substring (0, i_Nullen) : a_Zahl[1].substring (0, 2) );
	return i_Nullen === 0 ? s_Vorzeichen+a_Zahl[0] : s_Vorzeichen+a_Zahl[0]+","+a_Zahl[1];
	}

/**
* ZubehoerRechner
* @author Elko Panzyk, Panzyk.Net
* @uses Prototype 1.5.1 + 
*/  
function ClassZubRechner()
	{
	this.b_Dbf = true;
	this.f_Preis = 0.00;
	this.f_Uvp = 0.00;
	this.f_ZubehoerUvp = 0.00;
	this.f_Ueberfuehrung = 0.00;
	this.f_Mwst = 0.00;
	this.f_Grundpreis = 0.00;
	this.a_Kategorie = [];
	this.s_Ausschlusstext = "#{Zubehoer}, #{Kategorie}";
	
	this.o_tplErsparnis_Prozent = new Template( "#{ErsparnisProzent}% #{UvpGesamt}" );
	this.o_tplErsparnis = new Template( "#{Ersparnis} &euro;" );
	}

/**
 * fn_Float()
 * Erzeugt eine Floatzahl
 * @param string Zahl;
 * @return float Zahl;
 */
ClassZubRechner.prototype.fn_Float = function ( s_Daten ){ 
    var f_Daten = parseFloat( s_Daten );
    return isNaN ( f_Daten ) ? 0.00 : f_Daten;
    };
    
/**
 * daten ()
 * Zerlegt die Informationen
 * @param string s_Wert;
 * @return object {id,aus,preis,kat,uvp}
 */
ClassZubRechner.prototype.daten = function ( s_Wert ){
    var a_Daten = s_Wert.split(":");
    return { id:a_Daten[0], aus:a_Daten[1], preis:parseFloat( a_Daten[2] ), kat:a_Daten[3], uvp:parseFloat( a_Daten[4] ) };
    };

/**
 * tplErsparnis_Prozent
 */
ClassZubRechner.prototype.tpl_ersparnis_prozent = function ( s_Tpl ){
    this.o_tplErsparnis_Prozent = new Template( s_Tpl );
    };

/**
 * tplErsparnis
 */
ClassZubRechner.prototype.tpl_ersparnis = function ( s_Tpl ){
    this.o_tplErsparnis = new Template( s_Tpl );
    };
       
/**
 * Floatgeparste Mwst
 */
ClassZubRechner.prototype.mwst = function ( s_Mwst ){
    this.f_Mwst = this.fn_Float( s_Mwst );
    };

/**
 * Floatgeparster Grundpreis
 */
ClassZubRechner.prototype.grundpreis = function ( s_Grundpreis ){
    this.f_Grundpreis = this.fn_Float( s_Grundpreis );
    };

/**
 * FloatGeparster Zubehoeraufpreis
 */ 
ClassZubRechner.prototype.aufpreis = function ( s_Aufpreis ){
    this.f_Preis = this.fn_Float( s_Aufpreis );
    };

/**
 * FloatGeparste Uvp
 */
ClassZubRechner.prototype.uvp = function ( s_Uvp ){
    this.f_Uvp = this.fn_Float( s_Uvp );
    }

/**
 * DBF-Modus
 */
ClassZubRechner.prototype.dbf = function ( s_Dbf ){
    this.b_Dbf = ( s_Dbf == '1' ? true : false );
    }
    
/**
 * FloatGeparste ZubehoerUvp
 */
ClassZubRechner.prototype.zubehoeruvp = function ( s_ZubehoerUvp ){
    this.f_ZubehoerUvp = this.fn_Float( s_ZubehoerUvp );
    }
       
/**
 * FloatGeparste Ueberfuehrungskosten
 */
ClassZubRechner.prototype.ueberfuehrungskosten = function ( s_Ueberfuehrung ){
    this.f_Ueberfuehrung = this.fn_Float( s_Ueberfuehrung );
    }   
  
/**
 * show()
 * Zeigt Datenwerte an
 */
ClassZubRechner.prototype.show = function (){
    var s_Preis = float2euro ( this.f_Preis, 2 );
    var f_Gesamtpreis = this.f_Preis + this.f_Grundpreis;
    var s_Gesamtpreis = float2euro ( f_Gesamtpreis, 2 );
    var s_MwstAnteil = float2euro ( ( f_Gesamtpreis / ( 100 + this.f_Mwst ) ) * this.f_Mwst, 2 );
    
    $$("span.Aufpreis").each ( function ( o_Item ){ o_Item.update ( s_Preis ); } );
    $$("span.Gesamtpreis").each ( function ( o_Item ){ o_Item.update ( s_Gesamtpreis ); } );
    $$("span.MwstAnteil").each ( function ( o_Item ){ o_Item.update ( s_MwstAnteil ); } );
    
    /**
     * DBF - Voodoo
     */
    if ( this.b_Dbf === true ){
        var f_Gesamtpreis_nu = f_Gesamtpreis - this.f_Ueberfuehrung;
        var f_UvpGesamt = this.f_Uvp + this.f_ZubehoerUvp;
        var f_Ersparnis = f_UvpGesamt - f_Gesamtpreis_nu;
        var f_ErsparnisProzent = 100 - ( ( f_Gesamtpreis_nu * 100 ) / f_UvpGesamt );
        
        var o_Tpl1 = this.o_tplErsparnis_Prozent; 
        var o_Tpl2 = this.o_tplErsparnis;
        
        if ( f_Ersparnis > 0 ){
            $$("td.uvp").each ( function ( o_Item ){ o_Item.update ( o_Tpl1.evaluate( { ErsparnisProzent: float2euro ( f_ErsparnisProzent, 2 ), UvpGesamt: float2euro ( f_UvpGesamt, 2 ) } ) ); } ); 
            $$("td.uvp_preis").each ( function ( o_Item ){ o_Item.update ( o_Tpl2.evaluate( { Ersparnis: float2euro ( f_Ersparnis, 2 ) } ) ); } ); 
            } else {
                $$("td.uvp").each ( function ( o_Item ){ o_Item.update ( "" ); } );
                $$("td.uvp_preis").each ( function ( o_Item ){ o_Item.update ( "" ); } );
                }
        }
    };

/**
 * Setzt den Zubehoer-Aufpreis auf 0
 */
ClassZubRechner.prototype.reset = function (){
    this.f_Preis = 0.00;
    this.show();
    };

/**
 * Ließt die Problemfälle (Ausschluss) aus der Tabelle
 * @param string Zubehoer-Id
 * @return array Report
 */
ClassZubRechner.prototype.Probe = function ( a_Problem ){
    var daten = this.daten;
    var myTemplate = new Template( this.s_Ausschlusstext );
    var a_Kategorie = this.a_Kategorie;

    a_Problem.each(function(s_Key) { 
        var o_Daten = $( "zub"+s_Key );
        var o_Quelle = ( o_Daten === null ) ? {id:'0', aus:'', preis:'0.00', kat:'0', uvp:'0.00'} : daten ( o_Daten.value );
        
        var o_Zubtext = $( "ztxt" + s_Key );
        var o_Show = {Zubehoer: o_Zubtext.innerHTML.unescapeHTML(), Kategorie: a_Kategorie[ o_Quelle.kat ] };
        alert( myTemplate.evaluate(o_Show) ); 
        });
    
    return true;
    };

/**
 * Prüft, ob es zu Abhängigkeiten kommt
 * @param string Ausschlusswerte
 * @return boolean ja/nein
 */  
ClassZubRechner.prototype.checkAusschluss = function( s_Ausschluss ){
    var b_Exit = false;
    var a_Problem = [];
    $w( s_Ausschluss ).each(function( s_Key ){
        var o_Checkbox = $( "zub" + s_Key );
        if ( o_Checkbox !== null ){ 
            if ( o_Checkbox.checked === true ) { a_Problem[a_Problem.length]=s_Key; }
            }
        });
    if ( a_Problem.length > 0 ){ b_Exit = this.Probe( a_Problem ); }
    return b_Exit;
    };
         
/**
 * click ()
 * @param object checkbox
 */    
ClassZubRechner.prototype.click = function ( o_Checkbox ){
    o_Checkbox.disabled = true;
    var o_Zubehoer = this.daten( o_Checkbox.value );
    if ( o_Checkbox.checked === true ){
        if ( this.checkAusschluss( o_Zubehoer.aus ) === true ){ 
            o_Checkbox.checked = false; 
            o_Checkbox.disabled = false;
            return false;
            }
        this.f_Preis += o_Zubehoer.preis;
        this.f_ZubehoerUvp += o_Zubehoer.uvp;
        } else {
        this.f_Preis -= o_Zubehoer.preis;
        this.f_ZubehoerUvp -= o_Zubehoer.uvp;
        }
    this.show();
    o_Checkbox.disabled=false;
    };

/**
 * Rechner_Class initialisieren
 */
var o_Zub = new ClassZubRechner();

/**
 * Zubehoer-Checkbox geklickt
 */
function zubClick (event) {
    var o_Checkbox = event.element();
    o_Zub.click( o_Checkbox );
    }


/**
 * Liest Anzahl und Modelle aus
 */      
function hole_Resellerdaten ( o_Event )
	{
    new Ajax.Request('../RSDienste/',{ 
    parameters: {todo: 'jsReseller'},
    method:'post',
    encoding:'UTF-8',
    onSuccess: function(transport){
        //alert(transport.responseText);
        var json = transport.responseText.evalJSON(true);
        //$$("input[id^='bf']").each( function( o_Item ) { o_Item.value = json[o_Item.name]; } ); 
        $$("textarea[id^='bf']").each( function( o_Item ) { o_Item.value = json[o_Item.name]; } ); 
        }
    });
    return false;
    }

/**
 * Setzt Event-Händler nach dem Laden
 */
Event.observe(window, 'load', function() {
    /**
     * Wenn ein Reset-Schalter vorhanden ist,
     * Belege ihn mit einer Alle Zubehoer-Checkboxen abwählen
     * Funktion
     */ 
    if ( $('but_reset') !== null ) { 
        $('but_reset').observe('click', 
        function (){$$("input[id^='zub']").each( function( o_Item ) { o_Item.checked=false; } ); o_Zub.reset(); });
        }
    
    o_Zub.mwst ( $("Mwst_Satz").value );
    o_Zub.grundpreis ( $("Grundpreis").value );
    o_Zub.aufpreis ( $("Zubehoerpreis").value );
    
    /**
     * Daten für die UVP-Berechung
     */
    o_Zub.uvp ( $("Uvp").value );
    o_Zub.zubehoeruvp ( $("ZubehoerUvp").value );
    o_Zub.ueberfuehrungskosten ( $("Ueberfuehrungskosten").value );
    o_Zub.tpl_ersparnis_prozent ( $("Ersparnistext").value );
    o_Zub.dbf ( $("DbfModus").value );
    o_Zub.s_Ausschlusstext = $("Ausschlusstext").value;
    o_Zub.a_Kategorie = $("Kategorienlabel").value.split("|");
    
    /**
     * Erzeugt ein Click-Event auf allen Zubehoer-Checkboxen
     */
    $$("input[id^='zub']").each( function( o_Item ) { o_Item.observe('click', zubClick ); } );
    
    /**
     * Dropdown für die Bildauswahl
     */
    $$("#Bildwahl li").each( function( o_Knoten ){
		var o_ul = $A( o_Knoten.getElementsByTagName("ul") ).first();
		if( o_ul != null ){ o_Knoten.onmouseover = o_Knoten.onmouseout = function(){ Element.toggle( o_ul ); } }
		});
	
	/**
	 * Verstecke die Bildauswahl
	 */
	$$("ul#Bildwahl li ul").each(function( o_Knoten ){ Element.hide( o_Knoten ); });
	
	if ( $('TabKategorie') !== null ){
	    var o_ZubehoerTab = new ProtoTabs('tab_zubehoer',{defaultPanel:$('TabKategorie').value});
        }
    });

/* --- Ende Load-Event --- */
