function nobug()
{
    return true;
}

function HideDIV(d)
{
    document.getElementById(d).style.display = "none";
}

function DisplayDIV(d)
{
    document.getElementById(d).style.display = "block";
}

function addKey(key, toFields, count)
{
    if(key != '')
    {   // ist key ueberhaupt belegt?
        with(document.forms[0])
        {
            for(i = 0; i < count; i++)
            {   // alle Felder durchgehen
                if(elements[toFields + '[' + i + ']'].value == '')
                {   // bis ein noch leeres kommt
                    elements[toFields + '[' + i + ']'].value = key; // ins Feld key schreiben
                    i++;                                            // zum naechsten Feld

                    if(i < count)                                   // wenn dieses ex.
                        elements[toFields + '[' + i + ']'].focus(); // es fokusieren

                    break;                                          // und abbrechen
                }
            }
        }
    }
}

var buffer = new Array;

function showText(id, field, text)
{
    buffer[id] = field.value;
    field.value = text;
}

function hideText(id, field)
{
    //  if (buffer[id])
    field.value = buffer[id];
}

function addText(field, text)
{
    field.value = field.value + text;
}

function writer(field, text, pos)
{
    field.value = text.substring(0, pos);
    pos++;

    if(pos <= text.length)
        setTimeout("writer(document.forms[0].elements['" + field.name + "'], '" + text + "', " + pos +")", 40);
}

function closeAndGoTo(href)
{
    parent.opener.focus();
    parent.opener.location.href = href;
    parent.opener.child = self;
    top.close();
}

function closeAndLoadNew() {
    if (opener && opener.closed!=1) {
    opener.top.location.reload();
    self.close();
    }
}

function showPopup(popupURL, nameWindow, breite, hoehe)
{
    // open the popup window
    var features = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width="+breite+",height="+hoehe
    var popup = window.open(popupURL,nameWindow,features);

    // set the opener if it's not already set.  It's set automatically
    // in netscape 3.0+ and ie 3.0+.
    if(navigator.appName.substring(0,8) == "Netscape" )
    {
        popup.location = popupURL;
    }
}

function anim(bild)
{
    if(document.images)
    {
        alert (bild);
        document.images[bild].src= 'bilder/' + bild + '_punkt.gif';
    }
}

function removeFromLanguageList()
{
    //Wenn ein Feld markiert wurde
    if(document.forms[0].LANGUAGE.selectedIndex > -1)
    {
        // Markierung loeschen
        document.forms[0].LANGUAGE.options[document.forms[0].LANGUAGE.selectedIndex] = null;
    }
}

function addToLanguageList()
{
    //Wenn ein Feld markiert wurde
    if(document.forms[0].LANGUAGESSELECT.selectedIndex > -1)
    {
        NeuerEintrag = new Option(document.forms[0].LANGUAGESSELECT.options[document.forms[0].LANGUAGESSELECT.selectedIndex].value);
        //Wenn im select-Feld Language im ersten Option "Sprache auswaehlen" steht dann dort rein

        if(document.forms[0].LANGUAGE.options[0])
        {
            if(document.forms[0].LANGUAGE.options[0].text == "Sprache waehlen")
            {
                document.forms[0].LANGUAGE.options[0] = NeuerEintrag;
            }
            else
            {
                //Sonst in jedes weitere Feld
                document.forms[0].LANGUAGE.options[document.forms[0].LANGUAGE.length] = NeuerEintrag;
            }
        }
        else
        {
            //Sont in jedes weitere Feld
            document.forms[0].LANGUAGE.options[document.forms[0].LANGUAGE.length] = NeuerEintrag;
        }
    }
}

//Ein- und Ausblenden von Bereichen
//Variablen die übergeben werden müssen: 
// elementname - name von radiobuttons oder checkboxen
// areaname - name vom bereich der ausgeblendet oder eingeblendet werden soll, mit <span = 'sectionareaname'></span>, das Wort section muss da bleiben
// areaname1 - name vom bereich der ausgeblendet oder eingeblendet werden soll, mit <span = 'sectionareaname'></span>, das Wort section muss da bleiben, blendet zusätzliche bereiche ein
// areaname2 - name vom zweiten bereich der ausgeblendet werden soll, wenn ein anderer eingeblendet ist und umgekehrt (mehrere möglich durch kommas getrennt)
// checkednr - array mit nummern der felder, die nach dem ancklicken den bereich einblenden sollen
// checkednr1 - array mit nummern der felder, die nach dem ancklicken den bereich einblenden sollen, falls es areaname1 gibt, die was anderes einblenden soll
// formularnr - nummer des formulares, standart '0', aber bei mehereren formularen auf der seite, auch andere nummern möglich

function checkchange(elementname, areaname, areaname1, areaname2, checkednr, checkednr1, formularnr)
{
    if(areaname1 == '')
        areaname1 = areaname;

    if(checkednr1 == '')
        checkednr1 = checkednr;

    found = false;
    found1 = false;

    checkedcele = new Array();
    checkedcele1 = new Array();

    if(cele = document.forms[formularnr].elements[elementname])
    {
        for(var i = 0;i < checkednr.length;i++)
        {
            nr = checkednr[i];
            checkedcele[i] = cele[nr].checked;

            if(checkedcele[i])
                found = true;
        }

        for(var j = 0;j < checkednr1.length;j++)
        {
            nr1 = checkednr1[j];
            checkedcele1[j] = cele[nr1].checked;

            if(checkedcele1[j])
                found1 = true;
        }

        if(found)
        {
            //erster Bereich
            showoptions(areaname, 1);

            //Unterbereich
            if(areaname != areaname1)
            {
                if(found1)
                {
                    showoptions(areaname1, 1);
                }
                else
                {
                    showoptions(areaname1, 0);
                }
            }

            //zweiter Bereich
            areaFadeOut = areaname2.split(",");

            for(var f = 0;f < areaFadeOut.length;f++)
            {
                //alert(areaFadeOut[f]+" "+areaname);

                if(areaname != areaFadeOut[f])
                {
                    //alert(areaFadeOut[f]);
                    showoptions(areaFadeOut[f], 0);
                }
            }
        }
        else
        {
            showoptions(areaname, 0);
        }
    }
}

function showoptions(id, show)
{
    if(document.getElementById)
        if(cobj = document.getElementById("section" + id))
        {
            if (show == -1)
                show = (cobj.style.display=="inline")? 0 : 1;

            cobj.style.display = (show)? "inline" : "none";
        }
}

function load2peak(a, b, c, d, e, f, g)
{
    checkchange(a, b, c, d, e, f, g);
}

function hideShow(elementname,areaname,formularnr)
{
    if(document.forms[formularnr].elements[elementname].checked == true)
        $status = 0;
    else
        $status = 1;

    areaFadeOut = areaname.split(",");

    for(var f = 0;f < areaFadeOut.length;f++)
    {
        //alert(areaFadeOut[f]);
        showoptions(areaFadeOut[f], $status);
    }
}

function showHide(elementname,areaname,formularnr)
{
    if(document.forms[formularnr].elements[elementname].checked == true)
        $status = 1;
    else
        $status = 0;

    areaFadeOut = areaname.split(",");

    for(var f = 0;f < areaFadeOut.length; f++)
    {
        //alert(areaFadeOut[f]);
        showoptions(areaFadeOut[f], $status);
    }
}

function ShowBatterie(batteriestand)
{
    if(batteriestand <= 0){
        batlevel = false;
    }else if(batteriestand <= 30){
        batlevel = "red";
    }else if(batteriestand <= 60){
        batlevel = "orange";
    }else{
        batlevel = "green";
    }
    document.getElementById("batlevel").style.backgroundImage = "url(../images/bat/"+batlevel+".gif)";
    document.getElementById("batlevel").style.width = (batteriestand*0.75)+"px";
    
}


function SetButtonSettings(farbe,flag)
{
    try
    {
        document.forms[0].action.disabled = flag;
        document.forms[0].action.style.background = farbe;
    }
    catch(e1)
    {

    }

    try
    {
        document.forms[0].submit_edit.disabled = flag;
        document.forms[0].submit_edit.style.background = farbe;
    }
    catch(e2)
    {

    }
}



function CheckHeartRateValues(modus)
{
    var number = 0;

    switch(modus)
    {
        case "RUN_PULSE":   number += CheckTrainingIntensityValue("pulse");

                            break;

        case "RUN_POWER":   number += CheckTrainingIntensityValue("power");

                            break;

        case "RUN_MANUAL":  number += CheckTrainingIntensityArray("");
                            //number += CheckTrainingIntensityArray("_speed");

                            break;

        case "BIKE_PULSE":  number += CheckTrainingIntensityArray("pulse");

                            break;

        case "BIKE_POWER":  if(document.getElementsByName('crit_power').value == 'mas')
                                number += CheckTrainingIntensityArray("power_mp");
                            else
                                number += CheckTrainingIntensityArray("power");

                            break;

        case "BIKE_MANUAL": number += CheckTrainingIntensityArray("");
                            //number += CheckTrainingIntensityArray("_power");

                            break;
    }

    if(number > 0)
    {
        SetCompetitionMessageStatus("message_forbidden","block");
        return false;
    }
    else
    {
        SetCompetitionMessageStatus("message_forbidden","none");
        return true;
    }

    return false;
}

function CheckTrainingIntensityArray(suffix)
{
    var number = 0;

    number +=  CheckTrainingIntensityValue('inputrange[EB' + suffix + ']');
    number +=  CheckTrainingIntensityValue('inputrange[GA2' + suffix + ']');
    number +=  CheckTrainingIntensityValue('inputrange[GA1' + suffix + ']');
    number +=  CheckTrainingIntensityValue('inputrange[KB' + suffix + ']');

    return number;
}

function CheckTrainingIntensityValue(fieldname)
{
    try
    {
        return parseFloat(document.getElementsByName(fieldname)[0].value) > 0.0 ? 0 : 1;
    }
    catch(e)
    {
        return 0;
    }
}

function CheckDuration(fieldname,minDuration,maxDuration,showWarnings)
{
    //showWarnings: false   --> Meldungen werden ausgegeben und Eingabe nicht zugelassen
    //              true    --> Meldungen werden ausgegeben und Eingabe zugelassen

    var errorReturnValue = showWarnings ? true : false;

    try
    {
        var inputVal        =   document.getElementsByName(fieldname)[0].value
        var timeFormat      =   /(\d{1,2})\:([0-5]\d)/;

        var isTimeFormat    =   timeFormat.test(inputVal);

        if(isTimeFormat == false)
        {
            alert('Die Eingabe muss vom Format mm:ss sein.');
            return errorReturnValue;
        }

        var timeInfo        =   timeFormat.exec(inputVal);

        var duration        =   0;

        for(var i = 1;i < timeInfo.length;i++)
        {
            duration += timeInfo[i] * Math.pow(60.0,timeInfo.length - i - 1);
        }

        if(duration < minDuration || duration > maxDuration)
        {
            alert('Die Eingabe muss zwischen ' + Time2String(minDuration) + ' und ' + Time2String(maxDuration) + ' liegen');
            return errorReturnValue;
        }

        return true;
    }
    catch(e)
    {
        return errorReturnValue;
    }
}

function Time2String(seconds)
{
    var min = Math.floor(seconds / 60);
    var sec = (seconds % 60);

    return min + ':' + (sec < 10 ? '0' : '') + sec;
}

function HourMin2Min(HourMin)
{
    var time = HourMin.split(":");
    
    if(time[0].substr(0,1) == 0) 
        var hours = time[0].substr(1,1);
    else 
        var hours = time[0];
    
    if(time[1].substr(0,1) == 0) 
        var min = time[1].substr(1,1);
    else 
        var min = time[1];
    
    min = (parseInt(hours)*60)+parseInt(min);
    return min;
}

function CheckNoTimeLink(weekdayNr,trainingUnitNr)
{
    valStart = GetFormElementValue("start",weekdayNr,trainingUnitNr);
    valDur = GetFormElementValue("dur",weekdayNr,trainingUnitNr);
    valSport = GetFormElementValue("sport",weekdayNr,trainingUnitNr);
    try
    {
        return !(valStart == "00:00:00" || parseInt(valDur) == 0 || valSport == "");
    }
    catch(e)
    {
        alert(e);
        return false;
    }
}

function CheckNoTime(elementTypeName,weekdayNr,trainingUnitNr)
{
    val = GetFormElementValue(elementTypeName,weekdayNr,trainingUnitNr);
    if(elementTypeName != "dur" && parseInt(GetFormElementValue("dur",weekdayNr,trainingUnitNr)) == 0) doCount = true;
    else if(elementTypeName == "dur" && GetFormElementValue("start",weekdayNr,trainingUnitNr) == "00:00:00") doCount = true;
    else if(elementTypeName == "dur" && GetFormElementValue("sport",weekdayNr,trainingUnitNr) == "") doCount = true;
    else if(elementTypeName == "sport" && val == "") doCount = true;
    else if(elementTypeName == "start" && val == "00:00:00") doCount = true;
    else if(elementTypeName == "dur" && parseInt(val) == 0) doCount = true;
    else doCount = false;
    
    switch(elementTypeName)
    {
        case "start":   if(val == "00:00:00")
                            SetNoTime(weekdayNr,trainingUnitNr);
                        else{
                            SetDefault(weekdayNr,trainingUnitNr);
                        }
                        break;

        case "dur":     if(parseInt(val) == 0){
                            SetNoTime(weekdayNr,trainingUnitNr);
                        }else{
                            SetDefault(weekdayNr,trainingUnitNr);
                        }
                        break;

        case "sport":   if(val == ""){
                            SetNoTime(weekdayNr,trainingUnitNr);
                        }else
                            SetDefault(weekdayNr,trainingUnitNr);
                        break;
    }
    countValues(weekdayNr,trainingUnitNr,elementTypeName,doCount);
    
}


function SetNoTime(weekdayNr,trainingUnitNr)
{
    SelectFormElementValue("start",weekdayNr,trainingUnitNr,0);
    SelectFormElementValue("dur",weekdayNr,trainingUnitNr,0);
    CheckFormElementValue("sport",weekdayNr,trainingUnitNr,0,true);
}


function countValues(weekdayNr,trainingUnitNr,elementTypeName,doCount)
{
    try
    {  
        val = parseInt(GetFormElementValue("dur",weekdayNr,trainingUnitNr));
        if(elementTypeName == "dur" || doCount){
            
            curVal = document.getElementById('minSum').value;
            if (typeof(curValArray[GetFormElementName("dur",weekdayNr,trainingUnitNr)])=="undefined"){
                sumMin = parseInt(curVal) + val;
            }else{
                sumMin = parseInt(curVal) + val - curValArray[GetFormElementName("dur",weekdayNr,trainingUnitNr)];
            }
            curValArray[GetFormElementName("dur",weekdayNr,trainingUnitNr)] = val;
            document.getElementById('minSum').value = sumMin;
            document.getElementById('minSumF').innerHTML = formatMin(sumMin);
        }
        
        
        if(doCount){
            if(val > 0) sumUnit = parseInt(document.getElementById('unitSum').value) + 1;
            else sumUnit = parseInt(document.getElementById('unitSum').value) - 1;
            document.getElementById('unitSum').value = sumUnit;
            document.getElementById('unitSumF').innerHTML = sumUnit;
        }
    }
    catch(e)
    {
        alert(e);
    }
}


function SetDefault(weekdayNr,trainingUnitNr)
{   
    if(GetFormElementValue("start",weekdayNr,trainingUnitNr) == "00:00:00")
        SelectFormElementValue("start",weekdayNr,trainingUnitNr,13);
    if(GetFormElementValue("dur",weekdayNr,trainingUnitNr) == "0")
        SelectFormElementValue("dur",weekdayNr,trainingUnitNr,3);
    if(GetFormElementValue("sport",weekdayNr,trainingUnitNr) == "")
        CheckFormElementValue("sport",weekdayNr,trainingUnitNr,1,true);
        
}

function CheckFormElementValue(elementTypeName,weekdayNr,trainingUnitNr,elementNr,val)
{
    formElementName = GetFormElementName(elementTypeName,weekdayNr,trainingUnitNr);

    try
    {
        document.getElementsByName(formElementName)[elementNr].checked = val;
    }
    catch(e)
    {
        alert(e);
    }
}

function SelectFormElementValue(elementTypeName,weekdayNr,trainingUnitNr,val)
{
    formElementName = GetFormElementName(elementTypeName,weekdayNr,trainingUnitNr);

    try
    {
        document.getElementsByName(formElementName)[0].selectedIndex = val;
    }
    catch(e)
    {
        alert(e);
    }
}

function GetFormElementValue(elementTypeName,weekdayNr,trainingUnitNr)
{
    formElementName = GetFormElementName(elementTypeName,weekdayNr,trainingUnitNr);

    try
    {
        switch(elementTypeName)
        {
            case "start":
            case "dur":     comboBox = document.getElementsByName(formElementName)[0];

                            return comboBox.options[comboBox.selectedIndex].value;

                            break;

            case "sport":   radioButtonArr = document.getElementsByName(formElementName);

                            for(var i = 0;i < radioButtonArr.length;i++)
                            {
                                if(radioButtonArr[i].checked)
                                    return radioButtonArr[i].value;
                            }

                            return null;

                            break;

            default:        return null;
        }
    }
    catch(e)
    {
        alert(e);
        return null;
    }
}

function GetFormElementName(formElementTypeName,weekdayNr,trainingUnitNr)
{
    return "vars[" + formElementTypeName + "][" + weekdayNr + "][" + trainingUnitNr + "]";
}

function CountJoker(weekdayNr,maxTrainingUnitNr,maxJoker,errorText,captionText)
{
    var numberOfJoker,showErrorMessage;

    numberOfJoker = 0;

    showErrorMessage = false;

    for(var i = 1;i <= maxTrainingUnitNr;i++)
    {
        if(document.forms[0].elements['vars[sport]['+weekdayNr+']['+i+']'][document.forms[0].elements['vars[sport]['+weekdayNr+']['+i+']'].length - 1].checked == true)
        {
            numberOfJoker++;

            if(numberOfJoker > maxJoker)
            {
                showErrorMessage = true;
            }
        }
    }

    if(showErrorMessage)
    {
        var closeimg = '<img src="<?= $relPath ?>js/overlibmws/exit.gif" alt="<?= l("Click to close","","layers") ?>" '+'width="12" height="11" border="0">';
        overlib(errorText + ' ' + maxJoker,CAPTION,numberOfJoker + ' ' + captionText,STICKY,DRAGGABLE,CLOSETEXT,closeimg,CLOSECLICK);
    }

    return !showErrorMessage;
}

function formatMin(minute) {
	std = Math.floor(minute / 60)+":"+((minute % 60 >= 10)? (minute % 60) : "0"+(minute % 60)); 
	return std;
}
//Ende vom Ein und Ausblenden von Bereichen

