/*
 * Migration aus alten Themenportalen zum oeffnen von Rechnern
 * z.B. Geschaeftswagenrechner mit javascript:wR('952450/HI952450.htm');
 * siehe z.B. HaufePortal/skins/Auftritte/ZielgruppenPortale/PersonalOffice/rightMacrosGlobalPersonal.xpt
*/
function wR(name){
    Fenster=window.open("/Calculators/" + name ,"OnlineRechner","toolbar=0,location=0,directories=0,menubar=0,status=0,resizable=1,width=780,height=600,scrollbars=auto");
    Fenster.focus();
}

/*
 * Handler fuer das Mengen-Inputfeld im Warenkorb. Dazu wird das Skript url
 * aufgerufen, dass die Neuberechnung anstoesst und zur Warenkorb Ansicht
 * redirected.
*/
function changeQuantity(url, elementID){
    quantity = document.getElementsByName(elementID)[0].value;
    url = url + quantity;
    window.location.href = url;
}
/*
 * onchange handler fuer das Umsetzen der Themen (category, subcategory) im Haufeshop
 * @param elementID ID des Selectelements dessen Auswahl benoetigt wird
*/
function setSubjectID(elementID){
    newCatID = document.getElementsByName(elementID)[0].value;
    catID    = document.getElementById('CategoryID');
    catID.value = newCatID;
}

if(typeof dojo == "object") {
    dojo.require("dojo.json");
}

function getLayoverDialog(dlgId, jsParams, theUrl){
    if(dlgId == undefined){
        dlgId = "shop2";
    }
    dojo.debug("create new layover dialog id=" + dlgId);
    var dialog = dojo.widget.byId("shop2");
    var jsonTmpData = dojo.json.evalJson(theUrl);
    if(dialog) {
        /* workaround for not being able to resize dialog dynamically */
        dialog.destroy();
        dialog = undefined;
    }
    if(!dialog) {
        if(typeof(jsonTmpData) == "object") {
            theUrl = "";
        }
        dialog = dojo.widget.createWidget("haufedojo:shopdialog", {
                 id: dlgId,
                 bgColor: "#4f4f4f",
                 bgOpacity:"0.60",
                 href: theUrl,
                 dlg_height: jsParams.dlg_height,
                 dlg_width: jsParams.dlg_width,
                 dlg_x: jsParams.dlg_x,
                 dlg_position_x: jsParams.dlg_position_x,
                 jsParams: jsParams,
                 refreshOnShow: true,
                 preload: false,
                 cacheContent: true,
                 executeScripts: true,
                 scrolloff: jsParams.scrolloff
        });
        if(typeof(jsonTmpData) == "object") {
            dialog.jsonProcess(jsonTmpData);
        }
    }else{
        if(typeof(jsonTmpData) == "object") {
            theUrl = "";
            dialog.jsonProcess(jsonTmpData);
        }else{
            dialog.href = theUrl;
            dialog.isLoaded = false;
        }
    }
    return dialog;
}

function getFormParameterAsQueryString(formName){
    //theForm = window.document.forms[formName];
    //If we have more than one formular with the same name 
    //(i.e. one in the page another in a layover) the code above dont work.
    //In this case we take the last one (which should be the one in the layover).
    forms = document.getElementsByTagName("form");
    for (var i=forms.length-1; i>=0; i--)
    {
        if (forms[i].name == formName)
        {
            theForm = forms[i];
            break;
        }
    }
    
    elements=theForm.elements;
    result='';
    for(var i =0;i< elements.length;i++){
        el = elements[i];
        if(!el.disabled && ((el.type != "radio" && el.type != 'checkbox') || el.checked)){
            result += ("&" + el.name + "=" + el.value);
        }
    }
    return result;
}

//Overlay auf
function explodeLayer(startO, obj, theUrl, backUrl, jsParams){
    if(backUrl== undefined){
        backUrl = "";
    }
    dojo.debug("explodeLayer (non-form) called, url="+theUrl);
    var dialog = getLayoverDialog("shop2", jsParams, theUrl);
    dialog.show();
}

function explodeLayerWithFormParameters(startO, obj, theUrl, backUrl, jsParams, formName){
    if(theUrl.indexOf("?") == -1){
        theUrl = theUrl + "?";
    }
    if(formName != null){
        theUrl += getFormParameterAsQueryString(formName);
    }
    return explodeLayer(startO, obj, theUrl, backUrl, jsParams);
}

//Overlay zu
function implodeLayer(startO,obj){
    dojo.debug("fn implodeLayer called");
    var dialog = dojo.widget.byId("shop2");//getLayoverDialog("shop2");//, 80
    if(dialog){
        dialog.setContent("");
        dialog.hide();
    }
    return;
}

function showDHTMLInfos(id,buttonIMG){
    var button = buttonIMG;
    //die speziellen buttons
    var buttonClose = button+"HideInfo.png";
    var buttonOpen = button+"ShowInfo.png";
    //öffnen/schliessen button
    var systemButton = id;
    var action = "action" + id;
    var layer = layer + id;
    var obj_action = document.getElementById(action);
    //bringt dem umgebenden div auf die richtige höhe
    var obj_systemButton = document.getElementById(systemButton);
    if(obj_action.value == "hide"){
        alert();
        obj_systemButton.src = buttonOpen;
    }else{
        obj_systemButton.src = buttonClose;
    }
}

//Enable saving state of content structure using session cookies? (on/off)
var enablepersist="on"
//persistence in # of days
var memoryduration="7"
//Path to image to represent contract state.
var contractsymbol='buttonHideInfo.png'
//Path to image to represent expand state.
var expandsymbol='buttonShowInfo.png'
/////No need to edit beyond here //////////////////////////
function getElementbyClass(rootobj, classname){
    var temparray=new Array();
    var inc=0;
    var rootlength=rootobj.length;
    for(i=0; i<rootlength; i++){
        if(rootobj[i].className==classname){
            temparray[inc++]=rootobj[i];
        }
    }
    return temparray;
}

function sweeptoggle(ec){
    var inc=0;
    while (ccollect[inc]){
        ccollect[inc].style.display=(ec=="contract")? "none" : "";
        inc++;
    }
    revivestatus();
}

function expandContent(image,div){
    divStatus = div.style.display;
    divID = div.id;
    imgID = image.id
    if(divStatus == 'none'){
        document.getElementById(divID).style.display = 'inline';
        document.getElementById(imgID).src = 'buttonHideInfo.png';
        document.getElementById(imgID).title = 'weitere Produkte ausblenden';
    }else if(divStatus != 'none'){
        document.getElementById(divID).style.display = 'none';
        document.getElementById(imgID).src = 'buttonShowInfo.png';
        document.getElementById(imgID).title = 'weitere Produkte einblenden';
    }
}

function revivecontent(){
    selectedItem=getselectedItem();
    selectedComponents=selectedItem.split("|");
    for(i=0; i<selectedComponents.length-1; i++);
        document.getElementById(selectedComponents[i]).style.display="none";
}

function revivestatus(){
    var inc=0;
    while(staecollect[inc]){
        if(ccollect[inc].style.display=="none"){
            statecollect[inc].src=expandsymbol;
        }else{
            statecollect[inc].src=contractsymbol;
            inc++;
        }
    }
}

function get_cookie(Name){
    var search = Name + "=";
    var returnvalue = "";
    if(document.cookie.length > 0){
        offset = document.cookie.indexOf(search)
        if(offset != -1){
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if(end == -1){
                end = document.cookie.length;
            }
            returnvalue=unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}

function getselectedItem(){
    if(get_cookie(window.location.pathname) != ""){
        selectedItem=get_cookie(window.location.pathname)
        return selectedItem
    }else{
        return "";
    }
}

function saveswitchstate(){
    if(typeof dojo == "object"){
        dojo.debug("popupHelper saveswitchstate called");
    }
    var inc=0, selectedItem="", ccollect=[];
    while(ccollect[inc]){
        if(ccollect[inc].style.display=="none"){
            selectedItem+=ccollect[inc].id+"|";
        }
        inc++;
    }
    if(get_cookie(window.location.pathname)!=selectedItem){//only update cookie if current states differ from cookie's
        var expireDate = new Date();
        expireDate.setDate(expireDate.getDate()+parseInt(memoryduration));
        document.cookie = window.location.pathname+"="+selectedItem+";path=/;expires=" + expireDate.toGMTString();
    }
}

function do_onload(){
    if(typeof dojo == "object"){
        dojo.debug("popupHelper do_onload called");
    }
    uniqueidn=window.location.pathname+"firsttimeload";
    var alltags=document.all? document.all : document.getElementsByTagName("*");
    ccollect=getElementbyClass(alltags, "switchcontent");
    statecollect=getElementbyClass(alltags, "showstate");
    if(enablepersist=="on" && get_cookie(window.location.pathname)!="" && ccollect.length>0){
        revivecontent();
    }
    if(ccollect.length>0 && statecollect.length>0){
        revivestatus()
    }
}

if(typeof dojo == "object"){
    // see Dojo docu, http://manual.dojotoolkit.org/WikiHome/DojoDotBook/Book34 "Page Load / Unload"
    dojo.debug("popupHelper: addOnload for do_onload");
    dojo.addOnLoad(do_onload); 
}else if(window.addEventListener){
    window.addEventListener("load", do_onload, false)
}else if(window.attachEvent){
    window.attachEvent("onload", do_onload)
}else if (document.getElementById){
    window.onload=do_onload
}

if(enablepersist=="on" && document.getElementById){
    if(typeof dojo == "object"){
        dojo.addOnUnload(saveswitchstate);
    }else{
        window.onunload=saveswitchstate;
    }
}