jsv_sk_last = 'jsv';

jQuery(document).ready(function () {

if (document.getElementById('yform')!=null) {
    yformaction=document.getElementById('yform').action;
} else yformaction="";

// aktiviere Tabs
jQuery(".tabline").removeClass("hide");
jQuery(".tabcontents").each(function() {
    i = 0;
    jQuery(this).find(".tabcontent").each(function() {
        // Erst ab dem 2. Tabinhalt ausblenden
        if(i > 0) {
            if (!jQuery(this).hasClass("nohide")) jQuery(this).addClass("hide");
        }
        i++;
    });
});

// Zuletzt gew�hlten Tab nutzen
if ((GET("a")!="undefined")) {
    if ((GET("site")==GET("d")) || (GET("site")=="") || (GET("site")=="undefined")) {
        view_tab(document.getElementById(GET("a")),GET("b"),GET("c"));
    }
}

/*
if (GET("twe")==1) {
alert('x');
document.onkeydown = function(x) {
    if (x.keyCode==13) {
        document.forms.yform.onsubmit()=function() {return false;}
        var y=document.forms.yform.elements;
        for(var i=0;i<y.length;i++) {
            y[i].onblur=function() {alert(this.value);this.style.border='1px solid red;'}
        }
        y[1].focus();
        return false;
    }

};
}
*/

    jQuery("select,input,option").keydown(function(x) {
        if (x.keyCode==13) {
            this.id='ichbins';

            var y=document.forms.yform.elements;
            var act=0;
            for(var i=0;i<y.length;i++) {
                if (y[i].id=='ichbins') act=1;
                if ((act==1) && (y[i].type=='submit')) {y[i].click();return false}
            }
        }
    });


// helper
helper_classes = new Array('type-text', 'type-check', 'type-select');

jQuery(".check-box").each(function() {
    jQuery(this).find("select,input,option").focus(function() {
        if (this.type=="submit") return;
        if (this.type=="image") return;
        if (this.id=="undefined") this.id="id"+Math.round(0,1000000);
        if(typeof(helper_texts) == 'undefined') {return;}
        var a=getPageCoords(this.id);
        $("#helfer_right").css("top",(a.y-190)+"px");
        bla_name = this.name;
        text = helper_texts[bla_name];
//        alert(text);
        if(false) { // wozu soll dies gut sein??? (bla_name.search(/art/)>-1) && (bla_name.search(/warmwasser/)>-1)) {
                var temp=new Array();
                temp = helper_texts[bla_name].split("|");
                var ind=this.id.substring(this.id.length-1);
                text=temp[ind];
            }
        if (!text) {
            if(bla_name.search(/eek/)>-1) {
                bla_name=this.id;
                text = helper_texts[bla_name];
                $("#helfer_right").removeClass("hide");
            }
            if (!text) $("#helfer_right").addClass("hide");
        } else $("#helfer_right").removeClass("hide");
        jQuery("#helfer_right").find("p").each(function() {
            if(!text) {
                text = "Hier stehen Feldinformationen.";
            }
            this.innerHTML = text;
        });
    });
    jQuery(this).find("select,input,option").blur(function() {
        $("#helfer_right").addClass("hide");
    });
});

// Bild fuer Anzahl der Personen wechseln
jQuery('#household').keyup(function () {
    persons = parseInt(this.value);
    if(persons <= 0) {
        classname = 'p0';
    } else if(persons > 0 && persons < 6) {
        classname = 'p' + persons;
    } else {
        classname = 'p5';
    }
    jQuery(this).parent().find('span').attr('class', classname);
});

// Bild fuer Gefrierfach beim Kuehlschrank
jQuery('#formpart_fridge_type_id').change(function () {
    type = this.options[this.options.selectedIndex].value;
    if(type == 'ohne') {
        name = '';
    } else if(type.length > 0 && type.length < 4) {
        name = '-' + type.length + 'stern';
    } else {
        name = '';
    }
    jQuery(this).parent().parent().find('.item-pic > img').attr('src',
        'fileadmin/img/icon_fridge' + name + '.png');
});

sicherheitszaehler=0;
last_userinfos=new Array();
function getUserinfofields() {
    price = parseFloat(jQuery('#stpr').val().replace(/,/g, '.'));
//    if(!isNaN(price)) jQuery('#stpr').attr('value',price);
    jsv = parseFloat(jQuery('#jsv').val().replace(/,/g, '.'));
//    if(!isNaN(jsv))    jQuery('#jsv').attr('value', jsv);
    cost = parseFloat(jQuery('#stromkosten').val().replace(/,/g, '.'));
//    if(!isNaN(cost))    jQuery('#stromkosten').attr('value', cost);
    zr=jQuery('#sk-zeitraum').val();
}

function getUserinfoprice() {
    getUserinfofields();
    if (jsv>0) var stpr=cost*zr/jsv; else var stpr=0;

    var stprnum=stpr;
    //if (isNaN(stpr)) {jQuery('#stpr').attr('value', '');return;}
    var add="";
    if ((stpr*10)==Math.round(stpr*10)) add="0";
    stpr=String(Math.round(stpr*100)/100).replace(/\./g, ',');
    stpr+=add;

    if ((last_userinfos[(last_userinfos.length-1)]!='price')  && !isNaN(stprnum) && (stpr!=0) && (stpr!='')) jQuery('#stpr').attr('value', stpr);
    else getUserinfobeinull()
}
function getUserinfocost() {
    getUserinfofields();
    var costs=price*jsv/zr;
    costs=Math.round(costs);
//  if () jQuery('#stromkosten').attr('value', '');


    if ((last_userinfos[(last_userinfos.length-1)]!='cost') && !isNaN(costs) && (costs!=0) && (costs!='')) jQuery('#stromkosten').attr('value', costs);
    else getUserinfobeinull()
}
function getUserinfojsv() {
    getUserinfofields();
    if (price>0) var jsvs=zr*cost/price; else var jsvs=0;
    jsvs=Math.round(jsvs);
//  if (isNaN(jsvs)) jQuery('#jsv').attr('value', '');

    if ((last_userinfos[(last_userinfos.length-1)]!='jsv') && !isNaN(jsvs) && (jsvs!=0) && (jsvs!='')) jQuery('#jsv').attr('value', jsvs);
    else getUserinfobeinull()
}
function setUserinfolast(val,force) {
    if (!force) {
        if (last_userinfos[(last_userinfos.length-1)]==val) return false;
//      if (last_userinfos[(last_userinfos.length-2)]==val) return false;
    }
    last_userinfos[last_userinfos.length]=val;
    return true;
}
function getUserinfo() {
    var a=0;var xxx=new Array();
    while (a<2) {
        xxx[last_userinfos[(last_userinfos.length-a-1)]]=1;
        a++;
    }
//  alert('jsv: '+xxx['jsv']+' price '+xxx['price']+' cost '+xxx['cost']);
    if (xxx['jsv'] && xxx['price']) getUserinfocost();
    else if (xxx['jsv'] && xxx['cost']) getUserinfoprice();
    else if (xxx['price'] && xxx['cost']) getUserinfojsv();
    else {
        return;
    }
}
function getUserinfobeinull() {
    getUserinfofields();

//  alert('price '+price+' cost '+cost+' jsv '+jsv+' last_userinfos: '+last_userinfos[last_userinfos.length-1]+';'+last_userinfos[last_userinfos.length-2]+';'+last_userinfos[last_userinfos.length-3]);

    sicherheitszaehler++;
    if (sicherheitszaehler==4) {sicherheitszaehler=0;return;}

    if (price=="00") price=0;
    if (isNaN(price)) price=0;
    if (isNaN(cost)) cost=0;
    if (isNaN(jsv)) jsv=0;

    if (price==0) {
//      alert('price');
        if (last_userinfos[last_userinfos.length-1]=='price') return;
        setUserinfolast('cost');
        setUserinfolast('jsv');
        getUserinfoprice();
    } else if (cost==0) {
//      alert('cost');
        if (last_userinfos[last_userinfos.length-1]=='cost') return;
        setUserinfolast('price');
        setUserinfolast('jsv');
        getUserinfocost();
    } else if (jsv==0) {
//      alert('jsv');
        if (last_userinfos[last_userinfos.length-1]=='jsv') return;
        setUserinfolast('cost');
        setUserinfolast('price');
        getUserinfojsv();
    }
}
setUserinfolast('price');

// Stromkosten berechnen, wenn Jahresstromverbrauch eingegeben wird
/*
jQuery('#jsv').blur(function () {
    setUserinfolast('jsv');
    getUserinfo();
});
*/
jQuery('#jsv').change(function () {
    setUserinfolast('jsv');
//  else if ((parseFloat(document.getElementById('stpr').value.replace(/,/g, '.'))>0) && setUserinfolast('price')) alert('a');
//  else if ((parseFloat(document.getElementById('stromkosten').value.replace(/,/g, '.'))>0) && setUserinfolast('cost')) alert('b');
    getUserinfo();
});
// Jahresstromverbrauch berechnen, wenn Stromkosten eingegeben wurden
/*
jQuery('#stromkosten').blur(function () {
    setUserinfolast('cost');
    getUserinfo();
});
*/
jQuery('#stromkosten').change(function () {
    setUserinfolast('cost');
//  else if (parseFloat(document.getElementById('jsv').value.replace(/,/g, '.'))>0) setUserinfolast('jsv');
//  else if (parseFloat(document.getElementById('stpr').value.replace(/,/g, '.'))>0) setUserinfolast('price');
    getUserinfo();
});

// Jahresstromverbrauch berechnen, wenn Stromkostentyp geaendert wurde
jQuery('#sk-zeitraum').change(function () {
    // damit die Kosten nicht umgestellt werden:
    setUserinfolast('jsv');
    setUserinfolast('price');
    getUserinfo();
});

jQuery('#stpr').change(function () {
    setUserinfolast('price');
//  else if (parseFloat(document.getElementById('stromkosten').value.replace(/,/g, '.'))>0) setUserinfolast('cost');
//  else if (parseFloat(document.getElementById('jsv').value.replace(/,/g, '.'))>0) setUserinfolast('jsv');
    getUserinfo();
});


// infotip-box
jQuery('#closetipps_id').click(function () {
    //jQuery(this).parent().parent().parent().addClass('hide');
    jQuery('#tipps_id').addClass('hide');
    jQuery('#helpicon').removeClass('hide');
    // IE HACK
    document.getElementById('tipps_id').style.display="none";
});
jQuery('#opentipps_id').click(function () {
    //jQuery(this).parent().parent().parent().addClass('hide');
    jQuery('#tipps_id').removeClass('hide');
    jQuery('#helpicon').addClass('hide');
    // IE HACK
    document.getElementById('tipps_id').style.display="block";
});

}); // end ready function

function view_tab(node, content_id, tabstuff_id,desc) {
    try {
        new_tab = jQuery(node);
    } catch(e) { return; }

    // finde aktiven Tab und deaktiviere ihn
    old_tab = jQuery("#" + tabstuff_id + " > .tabline > .tab-active");
    old_tab.removeClass("tab-active");
    old_tab.addClass("tab");

    // Funktion siehe unten, IE Hack
    try {
        lasttab=getElementsByClassName("act")[0];
    } catch(e) {
        return;
    }


    try {
    old_tab = jQuery('#'+lasttab.id);
    old_tab.removeClass("act");


    // neuen Tab aktivieren
    new_tab = jQuery(node);
    new_tab.removeClass("tab");
    new_tab.addClass("tab-active");

    new_tab = jQuery('#'+node.id+'li');
    new_tab.addClass("act");

    // alten Inhalt ausblenden
    jQuery("#" + tabstuff_id + " > .tabcontents > " +
           ".tabcontent:visible").toggleClass("hide");
    // neuen Inhalt anzeigen
    jQuery("#" + content_id).toggleClass("hide");

    //sblindner Anpassung an realurl ?a statt &a 
    // if (yformaction!="") document.getElementById('yform').action=yformaction+'&a='+node.id+'&b='+content_id+'&c='+tabstuff_id+'&d='+document.getElementById('jssite').value;
    if (yformaction!="") document.getElementById('yform').action=yformaction+'?a='+node.id+'&b='+content_id+'&c='+tabstuff_id+'&d='+document.getElementById('jssite').value;

    } catch(e) {}

        try {
            new_tab = jQuery('#'+node.id+'desc');
            new_tab.removeClass("hide");
         //   old_tab = jQuery('#'+lasttab.id.replace(/li/,'desc'));
          //  old_tab.addClass("hide");

            var number=parseInt(node.id.substr(3,1));
            var rest=node.id.substr(0,3);
            for(var i=0;i<9;i++) if(i!=number) jQuery('#'+rest+i+'desc').addClass("hide");
        } catch(e) {}

    try {
        new_tab = jQuery('#'+desc+'desc');
        new_tab.removeClass("hide");

        var number=parseInt(desc.substr(3,1));
        var rest=desc.substr(0,3);
        for(var i=0;i<9;i++) if(i!=number) jQuery('#'+rest+i+'desc').addClass("hide");
    } catch(e) {}


}

HTTP_GET_VARS=new Array();
strGET=document.location.search.substr(1,document.location.search.length);
if(strGET!='')
    {
    gArr=strGET.split('&');
    for(i=0;i<gArr.length;++i)
        {
        v='';vArr=gArr[i].split('=');
        if(vArr.length>1){v=vArr[1];}
        HTTP_GET_VARS[unescape(vArr[0])]=unescape(v);
        }
    }

function GET(v)
{
if(!HTTP_GET_VARS[v]){return 'undefined';}
return HTTP_GET_VARS[v];
}

function getElementsByClassName(className, tag, elm){
    var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
    var tag = tag || "*";
    var elm = elm || document;
    var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
    var returnElements = [];
    var current;
    var length = elements.length;
    for(var i=0; i<length; i++){
        current = elements[i];
        if(testClass.test(current.className)){
            returnElements.push(current);
        }
    }
    return returnElements;
}


function getPageCoords (elementId) {
     var element;
     if (document.all)
       element = document.all[elementId];
     else if (document.getElementById)
       element = document.getElementById(elementId);
     if (element) {
       var coords = new Array();
       coords["x"]=0;
       coords["y"]=0;
       do {
           /*
        if(element.currentStyle)
        {
            if(element.currentStyle.position!='relative')
            {
                coords["x"] += element.offsetLeft;
                coords["y"] += element.offsetTop;
            }
        }
        else
        {*/
            coords["x"] += element.offsetLeft;
            coords["y"] += element.offsetTop;
        //}

         element = element.offsetParent;
         //alert(coords["y"]);
       }
       while (element);

       return coords;
     }
     else
       return null;
}


function unset_gray(what,eek,bja,jsv) {
    if ((what=="jsv") || (what=="bja")) {
        for(var i=0;i<eek.length;i++) document.getElementById(eek[i]).checked=false;
    }
    if ((what=="jsv") || (what=="eek")) {
        bja.selectedIndex=0;
    }
    if ((what=="bja") || (what=="eek")) {
        try{jsv.value="";}catch(e){};
    }
}


function hide_gray(who,what,eekid,baujahrid,eekgraph) {
    var divs=new Array('jsv','eek','bja');

    if (!eekgraph) eekgraph='eek-gray.gif';

    for(var i=0;i < divs.length;i++) {
        if (what==divs[i]) {
            if (i==1) {
                document.getElementById(eekid).src='fileadmin/img/eek-a.jpg';
                jQuery('#'+who+'eeklabel').removeClass('makegray');
            }
            try{jQuery('#'+who+divs[i]).removeClass('makegray');}catch(e){};
        } else {
            if (i==1) {
                document.getElementById(eekid).src='fileadmin/img/'+eekgraph;
                jQuery('#'+who+'eeklabel').addClass('makegray');
            }
            if (i==2) document.getElementById(baujahrid).selectedIndex=0;
            try{jQuery('#'+who+divs[i]).addClass('makegray');}catch(e){};
        }
    }
}

