﻿﻿//-----------------------------------------------------------------------------------
//
//	BC1.js
//
//	JS-Funktionen zum Konfigurator-Web
//
//  09.06.2008  kam  TabGrBerechnen: Korrektur beim Berechnen der Höhe
//  04.04.2008  iff  Wechsel des onclick nicht mehr durch Änderung im Source, da der IE dies nicht mag...
//  01.04.2008  iff  OnClick für Popupfenster bei Fahrradwechsel ändern
//  26.03.2008  iff  Checkboxen: Als Wert nur false (führt zum Löschen) übergeben
//  05.03.2007  kam  BeschreibungSpeichern[JS]() auskommentiert
//  29.01.2008  kam  Beschreibungen: Speichern per JS (kurz) bzw. Postback (lang)
//  28.01.2008  kam  Optik bei Beschreibungen: HTML-Formatierung realisiert
//  21.01.2008  kam  Optik: Optionen: unteren Rand bei Optionen/Ergebnisse "synchronisiert"
//  16.01.2008  kam  Fehler beim Speichern/Buchen - Falschangaben abgefangen (Meldung)
//  24.07.2007  rae  auch bei Hauspreis ',' durch '.' ersetzen, damit gültige Zahl entsteht
//  23.05.2007  iff  Stringfeld (Textbox) mit encodeURIComponent umwandeln, da ansonsten
//                   Umlaute und Sonderzeichen in sendRequest abgeschnitten werden
//  14.02.2007  iff  Funktionen zum Aktivieren und Deaktivieren des Hyperlinks zum Speichern
//  08.02.2007  rae  Hyperlink zu RH-Berechnung 'wandert' mit
//  05.02.2007  rae  bei Hauspreis 0 keinen Rabatt berechnen
//  01.02.2007  rae  + Aufschlag in Berechnen-Funktionen
//  30.01.2007  rae  Rabatt- und HauspreisBerechnen
//  29.01.2007  iff  Teilebilder laden
//  29.01.2007  iff  location.reload() statt window.location.href = window.location.href
//  27.01.2007  iff  Optionenrückgabewert: Gewicht + ":" + Bildname
//  26.01.2007  iff  Fahrradbild bei erfolgreichem Speichern aktualisieren
//  24.01.2007  iff  Fehlermeldung bei Regelverstoss ausgeben
//  22.01.2007  iff  OptionenSpeichern: Rückgabe != -1 wird als Gewicht interpretiert
//  22.01.2007  iff  Falschen Aufruf von Speicherfunktion mit Optionen korrigiert
//  18.01.2007  iff  Speicherfunktionen für Checkboxen kopiert (cb)
//  09.01.2007  iff  OS -> OptionSpeichern
//  30.08.2006  rae  TabGr...
//  22.08.2006  iff  neu
//
//-----------------------------------------------------------------------------------
//	Funktionen, um Tabellengröße automatisch ans Fenster anzupassen:
//-----------------------------------------------------------------------------------
var bTagGr = 0;        //  muss Tabelle neu berechnet werden?

//-----------------------------------------------------------------------------------
function HauspreisBerechnen(lblPreis,tbRabatt,tbHauspreis,sEK,lblAufschlag,lblFehler)
{
    try {
        dPreis = lblPreis.innerHTML;
        //  Angabe prüfen - ob nummerisch
        if ((tbRabatt.value != "") && isNaN(tbRabatt.value)) {
            lblFehler.innerHTML = "Die Angabe ist nicht nummerisch!";
            setTimeout('document.' + tbRabatt.form.name + '.' + tbRabatt.name + '.focus()', 10);
            return false;
        }
        dRabatt = tbRabatt.value.replace(/,/, ".");
        //  Angabe prüfen - ob im zulässigen Bereich
        if ((dRabatt < 0) || (dRabatt > 100)) {
            lblFehler.innerHTML = "Die Angabe muß im Bereich 0..100 liegen!";
            setTimeout('document.' + tbRabatt.form.name + '.' + tbRabatt.name + '.focus()', 10);
            return false;
        }
        lblFehler.innerHTML = "";
        dRabatt = dPreis * dRabatt / 100;
        d = dPreis - dRabatt;
        d = Math.round(d);
        tbHauspreis.value = d;
        
        if (lblAufschlag) {
            dAufschlag = 0;
            if (sEK != 0)
                dAufschlag = (d - sEK) / sEK * 100;
                
            lblAufschlag.innerHTML = Math.round(dAufschlag);
        }            
    }
    catch(err) {
    }
}

//-----------------------------------------------------------------------------------
function RabattBerechnen(lblPreis,tbRabatt,tbHauspreis,sEK,lblAufschlag,lblFehler)
{
    try {
        dPreis = lblPreis.innerHTML;
        //  Angabe prüfen - ob nummerisch
        if ((tbHauspreis.value != "") && isNaN(tbHauspreis.value)) {
            lblFehler.innerHTML = "Die Angabe ist nicht nummerisch!";
            setTimeout('document.' + tbHauspreis.form.name + '.' + tbHauspreis.name + '.focus()', 10);
            return false;
        }
        dHauspreis = tbHauspreis.value.replace(/,/, ".");
        //  Angabe prüfen - ob im zulässigen Bereich
        if (dHauspreis < 0) {
            lblFehler.innerHTML = "Die Angabe muß größer 0 sein!";
            setTimeout('document.' + tbHauspreis.form.name + '.' + tbHauspreis.name + '.focus()', 10);
            return false;
        }
        lblFehler.innerHTML = "";
        if (dHauspreis > 0) {
            dRabatt = (dPreis - dHauspreis) / dPreis * 100;
            tbRabatt.value = Math.round(dRabatt * 10) / 10; 
            
            if (lblAufschlag) {
                dAufschlag = 0;
                if (sEK != 0)
                    dAufschlag = (dHauspreis - sEK) / sEK * 100;
                
                lblAufschlag.innerHTML = Math.round(dAufschlag);
            }
        }            
    }
    catch(err) {
    }
}

//-----------------------------------------------------------------------------------
function LieferKWPruefen(tbLieferKW,lblFehler)
{
    try {
        //  Angabe prüfen - ob nummerisch
        if ((tbLieferKW.value != "") && isNaN(tbLieferKW.value)) {
            lblFehler.innerHTML = "Die Angabe ist nicht nummerisch!";
            setTimeout('document.' + tbLieferKW.form.name + '.' + tbLieferKW.name + '.focus()', 10);
            return false;
        }
        iLieferKW = tbLieferKW.value;
        //  Angabe prüfen - ob im zulässigen Bereich
        if ((tbLieferKW.value != "") && ((iLieferKW < 1) || (iLieferKW > 54))) {
            lblFehler.innerHTML = "Die Angabe muß im Bereich 1..54 liegen!";
            setTimeout('document.' + tbLieferKW.form.name + '.' + tbLieferKW.name + '.focus()', 10);
            return false;
        }
        lblFehler.innerHTML = "";
    }
    catch(err) {
    }
}

//-----------------------------------------------------------------------------------
//  bei body-onload:
function TabGrOnload() {
    bTagGr = 1;
    parent.window.setTimeout("TabGrBerechnen()", 400);

	//	außerdem Ergebnis-Iframe füllen:    
    sType = typeof(ifErg);
    if (sType == "object")
        ifErg.location.href = "Ergebnis.aspx";
}

//-----------------------------------------------------------------------------------
//  bei body-onresize:
function TabGrOnResize() {
    bTagGr = 1;
}

//-----------------------------------------------------------------------------------
//	per Timer aufgerufen:
function TabGrBerechnen() {
    //  eigentliche Berechnung:
    if (bTagGr == 1) {
        doc = parent.window.document;
        var divTabelle = doc.getElementById("divTabelle");
        if (divTabelle) {
            var imgRad = doc.getElementById("ctl00_cphBild_imgRad");    //  veränderbare Größe - groß/klein
            hRad = parseInt(imgRad.style.height);
            if (isNaN(hRad))
            {
                hRad = parseInt(imgRad.height);
                if (isNaN(hRad))
                    hRad = 0;
            }
            var dlLinks = doc.getElementById("ctl00_cphMain_dlLinks");  //  veränderbare Zeilenanzahl - je nach Fensterbreite
            hLinks = parseInt(dlLinks.style.height);
            if (isNaN(hLinks))
                hLinks = dlLinks.offsetHeight;

            var h = Fensterhoehe();
            hNeu = h - hRad - hLinks - 153;
            if (hNeu < 100)
                hNeu = 100;
                
            divTabelle.style.height = hNeu + "px";
        }        
        bTagGr = 0;
    }
    parent.window.setTimeout("TabGrBerechnen()", 400);
}

//-----------------------------------------------------------------------------------
// Der Dateiname des Bildes wird bei erfolgreichem Speichern imm Rückgabewert
// übergeben. Diese Datei in imgRad zeigen
function BildZeigen(s) {
	if (s.indexOf("1:") == 0) {
	    doc = parent.window.document;
	    imgRad = doc.getElementById("ctl00_cphBild_imgRad");
	    if (imgRad) {
	        imgRad.src = s.substr(2);
	    }
    }
}  

//-----------------------------------------------------------------------------------
//  Funktionen, um Ausprägungen zu speichern.
//-----------------------------------------------------------------------------------
//	Ausprägung mit Option speichern:
function AS_mitOptrb(rbMMA, sUrl, sP5) {
    doc = parent.window.document;
    //  Alle Optionen-ddls zum Merkmal müssen auf unsichtbar
    //  und die aktuelle auf sichtbar gesetzt werden.
    //  Achtung: ich gehe von max. 9 Ausprägungen aus!
    lblOptId0 = rbMMA.id.substr(0, 35);
    iAktuell = rbMMA.id.substr(35, 1);
    for(i=0; i<9; i++) {
        if (i == iAktuell)
            sDisplay = "inline";
        else
            sDisplay = "none";
            
        lblOptId = lblOptId0 + i + "_lblOptionen";
        lblOpt = doc.getElementById(lblOptId);
        if (lblOpt)
            lblOpt.style.display = sDisplay;

        hlRhId = lblOptId0 + i + "_hlRahmenhoehe";
        hlRh = doc.getElementById(hlRhId);
        if (hlRh)
            hlRh.style.display = sDisplay;

        ddlOptId = lblOptId0 + i + "_ddlOptionen";
        ddlOpt = doc.getElementById(ddlOptId);
        if (ddlOpt) {
            ddlOpt.style.display = sDisplay;
            if (sDisplay == "inline")
                ddlAktiv = ddlOpt;
        }
    }
    sRet = ASrb(rbMMA, sUrl, sP5);
	if (sRet.indexOf("-1:") == 0) {
        location.reload();
        return sRet;
    }
        
    //  Radio-Button wurde umgeschaltet.
    //	Also ist auch Value der anderen (jetzt aktiven) ddl zu speichern:
    if (ddlAktiv)
        OS(ddlAktiv, sUrl)
    return sRet;               
}

//-----------------------------------------------------------------------------------
//  Ausprägung speichern:
function ASrb(rbMMA, sUrl, sP5) {
	sRet = sendRequest("BC1Send.aspx?f=as" + sUrl + sP5);
	if (sRet.indexOf("-1:") == 0) {
	    alert(sRet.substr(3));
        location.reload();
        return sRet;
    }
    BildZeigen(sRet);
    sType = typeof(ifErg);
    if (sType == "object")
        ifErg.location.href = "Ergebnis.aspx";
    return sRet;                 
}

//-----------------------------------------------------------------------------------
//  Ausprägung speichern:
function AScb(cbMMA, sUrl, sP5) {
    if (!cbMMA.checked)
    	sRet = sendRequest("BC1Send.aspx?f=as" + sUrl + sP5 + "&p4=" + cbMMA.checked, "");
    else
	    sRet = sendRequest("BC1Send.aspx?f=as" + sUrl + sP5, "");
//    	sRet = sendRequest("BC1Send.aspx?f=as" + sUrl + sP5 + "&p4=" + cbMMA.checked, "");
	if (sRet.indexOf("-1:") == 0) {
	    alert(sRet.substr(3));
        location.reload();
        return sRet;
    }
    BildZeigen(sRet);
    sType = typeof(ifErg);
    if (sType == "object")
        ifErg.location.href = "Ergebnis.aspx";
    return sRet;                 
}

//-----------------------------------------------------------------------------------
//  Ausprägung speichern:
function AStb(tbMMA, sUrl, sP5) {
	sRet = sendRequest("BC1Send.aspx?f=as" + sUrl + sP5 + "&p4=" + encodeURIComponent(tbMMA.value), "");
	if (sRet.indexOf("-1:") == 0) {
	    alert(sRet.substr(3));
        location.reload();
        return sRet;
    }
    BildZeigen(sRet);
    sType = typeof(ifErg);
    if (sType == "object")
        ifErg.location.href = "Ergebnis.aspx";
    return sRet;                 
}

//-----------------------------------------------------------------------------------
//  Option speichern:
function OS(ddl, sUrl) {
	sRet = sendRequest("BC1Send.aspx?f=os" + sUrl + ddl.value);
	if (sRet.indexOf("-1:") == 0) {
	    alert(sRet.substr(3));
        location.reload();
        return sRet;
    }
    sType = typeof(ifErg);
    if (sType == "object")
        ifErg.location.href = "Ergebnis.aspx";
}

//-----------------------------------------------------------------------------------
//  Option speichern:
function OptionSpeichern(ddl, sMerkmalNr, sAuspraegungNr) {
	sRet = sendRequest("BC1Send.aspx?f=os&p3=" + sMerkmalNr + "&p4=" + sAuspraegungNr + "&p5=" + ddl.value);
	if (sRet.indexOf("-1") != 0) {
	    // Gewicht (Rückgabewert) in Label darstellen
	    sId = ddl.id.replace("_ddlOptionen", "_lblGewicht");
	    var lblGewicht = document.getElementById(sId);
	    if (lblGewicht) {
	    	if (sRet.indexOf(":") > 0)
	        	lblGewicht.innerHTML = sRet.substr(0, sRet.indexOf(":")) + " g";
	    	else 
	        	lblGewicht.innerHTML = sRet + " g";
	    }
	}
	else
	    if (sRet.indexOf("-1:") == 0) {
    	    alert(sRet.substr(3));
            location.reload();
            return sRet;
        }
    sType = typeof(ifErg);
    if (sType == "object")
        ifErg.location.href = "Ergebnis.aspx";
}

//-----------------------------------------------------------------------------------
function LadeArtikelbild(sDatei, sHeight) {
    doc = parent.window.document;
    imgArtikelbild = doc.getElementById("ctl00_cphBild_imgArtikelbild");
    if (imgArtikelbild) {
        imgArtikelbild.visible = true;
        imgArtikelbild.src = sDatei;
        imgArtikelbild.height = sHeight;
    }	       
}
   
//-----------------------------------------------------------------------------------
//  IFrame für Artikeldetails
//-----------------------------------------------------------------------------------
function KonfiguratorDetails() {
    sType = typeof(ifDetails);
    if (sType == "object")
        ifDetails.location.href = "KonfigurationDetails.aspx";
}

//-----------------------------------------------------------------------------------
function FrameGroesse() {
    ifDetails = document.getElementById("ifDetails");
/*    
	winFrame = parent.frames["ifDetails"].window;
	iFrameHoehe = winFrame.frameElement.offsetHeight;
	iFrameBreite = winFrame.frameElement.offsetWidth;
*/
	win = parent.window;
	iFensterBreite = win.document.body.offsetWidth;
	
//	winFrame.frameElement.offsetWidth = 500;
}

//-----------------------------------------------------------------------------------
function FensterInfo() {
    var w = Fensterweite();
    var h = Fensterhoehe();
    lbl = document.getElementById("lblFenster");
    lbl.innerText = w + "x" + h;
}

//-----------------------------------------------------------------------------------
function Fensterweite () {
  if (window.innerWidth) {
    return window.innerWidth;
  } else if (document.body && document.body.offsetWidth) {
    return document.body.offsetWidth;
  } else {
    return 0;
  }
}

function Fensterhoehe () {
  if (window.innerHeight) {
    return window.innerHeight;
  } else if (document.body && document.body.offsetHeight) {
    return document.body.offsetHeight;
  } else {
    return 0;
  }
}

//-----------------------------------------------------------------------------------
//	(wird aus Konfiguration.aspx aufgerufen.)
function KonfigurationSpeichern(sMarke, iProduktgruppenNr, iModellNr, sMerkmalText, iAuspraegungsNr) {
    	sRet = sendRequest("KonfiguratorSend.aspx?p1=KonfigurationSpeichern&p2=" + sMarke + "&p3=" + iProduktgruppenNr + "&p4=" + iModellNr + "&p5=" + sMerkmalText + "&p6=" + iAuspraegungsNr);

    sType = typeof(ifDetails);
    if (sType == "object")
        ifDetails.location.href = "KonfigurationDetails.aspx";
}

//function OptionSpeichern(sMarke, iProduktgruppenNr, iModellNr, sMerkmalText, iAuspraegungsNr, ddl) {
//	sRet = sendRequest("KonfiguratorSend.aspx?p1=OptionSpeichern&p2=" + sMarke + "&p3=" 
//		+ iProduktgruppenNr + "&p4=" + iModellNr + "&p5=" + sMerkmalText + "&p6=" + iAuspraegungsNr + "&p7=" + ddl.value);
//
//    sType = typeof(ifDetails);
//    if (sType == "object")
//        ifDetails.location.href = "KonfigurationDetails.aspx";
//}

function Beschr(iNr) {
    divBeschr = document.getElementById("divBeschr" + iNr);
    if (divBeschr)
        divBeschr.style.display = "inline";
}

//-----------------------------------------------------------------------------------
function getXMLHTTPRequest() {
	var xmlhttp = null;

	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = null;
		}
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}
    
//-----------------------------------------------------------------------------------
function sendRequest(url, params) {
	syncFlag = false;	//	benutze asynchron, nur dann gibt es Return-Werte.
	params = "";		//	benutze ich nicht			
	
    req = getXMLHTTPRequest();
    if (req) {
//		req.onreadystatechange = readyStateChangeCallback;	//	evtl. zum Debuggen
        req.open("Post", url, syncFlag);
        req.setRequestHeader("Content-Type", "applicattion/x-www-form-urlencoded");
        req.send(params);
    }
	if (!syncFlag)
		return(req.responseText);
}    

//-----------------------------------------------------------------------------------
//  Hyperlinks zum Konfiguration speichern deaktivieren
function DisableKonfigurationSpeichern(sText) {
    hl1 = parent.window.document.getElementById("ctl00_cphInfos_hlInfos1");
    hl2 = parent.window.document.getElementById("ctl00_cphInfos_hlInfos2");
    if (hl1) {
        hl1.disabled = true;
        hl1.href = "#";
    }
    if (hl2) {
        hl2.disabled = true;
        hl2.href = "#";
        hl2.title = sText;
    }
}

//-----------------------------------------------------------------------------------
//  Hyperlinks zum Konfiguration speichern aktivieren
function EnableKonfigurationSpeichern(sText) {
    hl1 = parent.window.document.getElementById("ctl00_cphInfos_hlInfos1");
    hl2 = parent.window.document.getElementById("ctl00_cphInfos_hlInfos2");
    if (hl1) {
        hl1.disabled = false;
        hl1.href = "KonfigSpeichern.aspx";
    }
    if (hl2) {
        hl2.disabled = false;
        hl2.href = "KonfigSpeichern.aspx";
        hl2.title = sText;
    }
}

//-----------------------------------------------------------------------------------
function BeschreibungEditieren(sProdGrNr, sMM_A, sLabel, sHfName) {
    doc = parent.window.document;
    ctrl = doc.getElementById("ctl00_cphBild_hfProdGrNr");
    if (ctrl) {
        ctrl.innerText = sProdGrNr;
    }
    ctrl = doc.getElementById("ctl00_cphBild_hfProduktgruppe");
    if (ctrl) {
        ctrl.innerText = sMM_A;
    }
    ctrl = doc.getElementById("ctl00_cphBild_lblProduktgruppe");
    if (ctrl) {
        ctrl.innerHTML = sLabel;
    }
    sText = "";
    ctrl = doc.getElementById(sHfName);
    if (ctrl) {
        sText = ctrl.value;
    }
    ctrl = doc.getElementById("ctl00_cphBild_tbBeschreibungen");
    if (ctrl) {
        ctrl.innerText = sText.replace(/<br \/>/g, "\r\n");
    }
    //  zusätzlich den Namen des HiddenFiled's merken - fürs spätere Speichern
    ctrl = doc.getElementById("ctl00_cphBild_hfHiddenFieldCtrlName");
    if (ctrl) {
        ctrl.value = sHfName;
    }
}

//-----------------------------------------------------------------------------------
function BeschreibungAnzeigen(sProdGrNr, sMM_A, sLabel, sHfName) {
    doc = parent.window.document;
    ctrl = doc.getElementById("ctl00_cphBild_hfProdGrNr");
    if (ctrl) {
        ctrl.innerText = sProdGrNr;
    }
    ctrl = doc.getElementById("ctl00_cphBild_hfProduktgruppe");
    if (ctrl) {
        ctrl.innerText = sMM_A;
    }
    ctrl = doc.getElementById("ctl00_cphBild_lblProduktgruppe");
    if (ctrl) {
        ctrl.innerHTML = sLabel;
    }
    sText = "";
    ctrl = doc.getElementById(sHfName);
    if (ctrl) {
        sText = ctrl.value;
    }
    ctrl = doc.getElementById("ctl00_cphBild_lblBeschreibungen");
    if (ctrl) {
        ctrl.innerHTML = sText;
    }
}

//-----------------------------------------------------------------------------------
//  kam, 05.03.2007, sicherheitshalber nur auskommentiert, später kann weg!!!
//function BeschreibungSpeichern() {
//    doc = parent.window.document;
//    sProdGrNr = "";
//    sProduktgruppe = "";
//    sBeschreibung = "";
//    sBeschreibungImHiddenField = "";
//    ctrl = doc.getElementById("ctl00_cphBild_hfProdGrNr");
//    if (ctrl) {
//        sProdGrNr = ctrl.value;
//    }
//    ctrl = doc.getElementById("ctl00_cphBild_hfProduktgruppe");
//    if (ctrl) {
//        sProduktgruppe = ctrl.value;
//    }
//    ctrl = doc.getElementById("ctl00_cphBild_tbBeschreibungen");
//    if (ctrl) {
//        sBeschreibung = ctrl.value.replace(/\r\n/g, "<br \/>");
//        sBeschreibungImHiddenField = sBeschreibung;
//        //  Wegen Umlaute und Sonderzeichen muß encodiert (als ASCII-Hex-Zahlenwerte) werden:
//        //  Auf der anderen Seite (aspx) wir automatisch decodiert.
//        sBeschreibung = encodeURIComponent(sBeschreibung);
//    }
//    sReq = "BC1Send.aspx?f=bs" + "&p3=" + sProdGrNr + "&p4=" + sProduktgruppe + "&p7=" + sBeschreibung;
//    //  bei zu langen Beschreibungen wird über PostBack gespeichert...
//    if (sReq.length > 2048)
//        return false;

//	sRet = sendRequest(sReq, "");
//	if (sRet.indexOf("-1:") == 0) {
//	    alert(sRet.substr(3));
//        location.reload();
//        return sRet;
//    }
//    //  wenn alles ok, die neue Beschreibung auch bei Option merken...
//    ctrl = doc.getElementById("ctl00_cphBild_hfHiddenFieldCtrlName");
//    if (ctrl) {
//        ctrl = doc.getElementById(ctrl.value);
//        if (ctrl) {
//            ctrl.value = sBeschreibungImHiddenField;
//        }
//    }
//    return true;
//}

//-----------------------------------------------------------------------------------
//  kam, 05.03.2007, sicherheitshalber nur auskommentiert, später kann weg!!!
//function BeschreibungSpeichernJS() {
//    if (BeschreibungSpeichern())    //  wenn nicht erfolgreich, dann per PostBack
//        return true;
//    else
//        return false;
//}

//-----------------------------------------------------------------------------------
