var position_top = 0;
var position_left = 0;
var strEmptyGroup = "";
var groups = [];
var AttachmentList = false;
var cat1 = false;

function show_cal_repeat(val) {

    var objRepeatForm = document.getElementById("repeat_form_inner");
    var objRepeatInterval = document.getElementById("repeat_form_interval");
    var objRepeatOne = document.getElementById("repeat_form_one");
    var objRepeatWeekday = document.getElementById("repeat_form_weekday");
    var objRepeatMonth = document.getElementById("repeat_form_month");
    var objRepeatYear = document.getElementById("repeat_form_year");
    var objRepeatEveryXDays = document.getElementById("repeat_form_everyxdays");
    var el = objRepeatInterval.getElementsByTagName("span");

    //objRepeatForm.style.display     = (val == "none" || val == "") ? "none" : "inline";
    objRepeatInterval.style.display = (val == "daily" || val == "weekly" ||
            val == "monthly" || val == 'yearly') ? "" : "none";

    objRepeatOne.style.display = (val == "one") ? "" : "none";
    objRepeatWeekday.style.display = (val == "weekly") ? "" : "none";
    objRepeatMonth.style.display = (val == "monthly" || val == "yearly") ? "" : "none";
    objRepeatYear.style.display = (val == "yearly") ? "" : "none";
    objRepeatEveryXDays.style.display = (val == "everyxdays") ? "" : "none";

    if (el)
        for (i = 0; i < el.length; i++) {
            el[i].style.display = (el[i].id != "repeat_form_interval_" + val)
                    ? "none" : "inline";
        }
}

function ToggleTicketTitleRequired() {
    var repeatMode = document.getElementById("RepeatMode");
    var ticketShortDescription = document.getElementById("TicketShortDescription").innerText;

    if (repeatMode.value != 'none') {
        if (!ticketShortDescription.includes("*"))
            document.getElementById("TicketShortDescription").innerHTML += " *";
    }
    else {
        if (ticketShortDescription.includes("*"))
        {
            var newString = ticketShortDescription.slice(0, ticketShortDescription.indexOf('*'));
            document.getElementById("TicketShortDescription").innerHTML = newString;
        }
    }
}

function showComboBoxList(fieldid, combobox)
{
    var userbox = document.getElementById(combobox);
    if (combobox == "comboboxlist") {
        document.getElementById('comboboxlist2').style.visibility = "hidden";
        document.getElementById('comboboxlist2').style.display = "none";
    } else {
        document.getElementById('comboboxlist').style.visibility = "hidden";
        document.getElementById('comboboxlist').style.display = "none";
    }

    var txtfield = document.getElementById(fieldid);
    var spantop = absTop(txtfield);
    var spanleft = absLeft(txtfield);
    var spanwidth = txtfield.offsetWidth;
    var spanheight = txtfield.offsetHeight;
    spantop = spantop + spanheight;
    spanwidth = spanwidth - 2;

    if (IE) {
        spantop += 1;
        spanleft += 1;
    }
    userbox.style.width = spanwidth + "px";
    userbox.style.left = spanleft + "px";
    userbox.style.top = spantop + "px";

    if (userbox.style.visibility != "visible" || position_left != spanleft
            || position_top != spantop) {
        userbox.style.display = "block";
        position_top = spantop;
        position_left = spanleft;
        userbox.style.visibility = "visible";
    } else {
        userbox.style.visibility = "hidden";
        userbox.style.display = "none";
    }
}

function showSupporter()
{
    var oResult = document.getElementById("comboboxres");
    var GroupID = parseInt(document.getElementById('group_id').value);
    makeHttpRequest("index.php?module=user&event=AJAXSupporterList&gid=" + GroupID, "buildSupporterList", true);
}

function buildSupporterList(xmldoc) {
    var UserID = document.getElementById("user_id").value;
    var users = xmldoc.getElementsByTagName("user");
    HTMLoutput = "";
    if (users.length > 0) {
        for (i = 0; i < users.length; i++) {
            HTMLoutput += "<div";
            if (users[i].getAttribute("id") == UserID) {
                HTMLoutput += " class=\"selected\"";
            }
            HTMLoutput += "><a href=\"JavaScript:void(0);\" style=\"color:#000;text-decoration:none;\"onclick=\"setSupporter(";
            HTMLoutput += users[i].getAttribute('id') + ",'";
            HTMLoutput += users[i].getAttribute('name') + "');\">&nbsp;";
            HTMLoutput += users[i].getAttribute("name") + "</a></div>";
        }
    } else {
        HTMLoutput += "&nbsp;" + NoSupporter + "&nbsp;";
    }
    document.getElementById("comboboxres").innerHTML = HTMLoutput;
    showComboBoxList('user_descr', "comboboxlist");
}
function showGroups()
{
    document.getElementById("comboboxres").innerHTML = "";
    showComboBoxList('group_descr', "comboboxlist");
    HTMLOutput = "<div>&nbsp;<a href=\"JavaScript:void(0);\" onclick=\"setGroup(0, '');\" style=\"color:#000;text-decoration:none;\">" + strEmptyGroup + "</a>"
    for (i = 0; i < groups.length; i++) {
        if (typeof (groups[i]) == "object" && groups[i].length == 2) {
            HTMLOutput += "<div";
            if (groups[i][0] == document.getElementById("group_id").value) {
                HTMLOutput += " class=\"selected\"";
            }
            HTMLOutput += ">&nbsp;<a href=\"JavaScript:void(0)\" onclick=\"setGroup(" + groups[i][0] + ",'" + groups[i][1] + "');\" style=\"color:#000;text-decoration:none;\">" + groups[i][1] + "</a></div>";
        }
    }
    document.getElementById("comboboxres").innerHTML = HTMLOutput;
}

function setGroup(id, name)
{
    showComboBoxList('group_descr', "comboboxlist");
    if (document.getElementById('group_id').value != id) {
        document.getElementById('group_id').value = id;
        document.getElementById('group_descr').value = name;
        document.getElementById('user_id').value = 0;
        document.getElementById('user_descr').value = "";
    }
}

function showPriority()
{
    oResult = document.getElementById("comboboxres2");
    showComboBoxList('prio_descr', "comboboxlist2");
    HTMLOutput = "";
    for (i = 0; i < prios.length; i++) {
        if (typeof (prios[i]) == "object" && prios[i].length == 2) {
            HTMLOutput += "<div";
            if (prios[i][0] == document.getElementById("prio_id").value) {
                HTMLOutput += " class=\"selected\"";
            }
            HTMLOutput += ">&nbsp;<a href=\"JavaScript:void(0)\" onclick=\"setPrio(" + prios[i][0] + ",'" + prios[i][1] + "');\" style=\"color:#000;text-decoration:none;\">" + prios[i][1] + "</a></div>";
        }
    }
    oResult.innerHTML = HTMLOutput;
}

function showUserPriority()
{
    oResult = document.getElementById("comboboxres2");
    showComboBoxList('user_prio_descr', "comboboxlist2");
    HTMLOutput = "";
    for (i = 0; i < prios.length; i++) {
        if (typeof (prios[i]) == "object" && prios[i].length == 2) {
            HTMLOutput += "<div";
            if (prios[i][0] == document.getElementById("user_prio_id").value) {
                HTMLOutput += " class=\"selected\"";
            }
            HTMLOutput += ">&nbsp;<a href=\"JavaScript:void(0)\" onclick=\"setUserPrio(" + prios[i][0] + ",'" + prios[i][1] + "');\" style=\"color:#000;text-decoration:none;\">" + prios[i][1] + "</a></div>";
        }
    }
    oResult.innerHTML = HTMLOutput;
}

function setPrio(id, name)
{
    document.getElementById("prio_id").value = id;
    document.getElementById("prio_descr").value = name;
    showComboBoxList("prio_descr", "comboboxlist2");
}

function setUserPrio(id, name)
{
    document.getElementById("user_prio_id").value = id;
    document.getElementById("user_prio_descr").value = name;
    showComboBoxList("user_prio_descr", "comboboxlist2");
}

function setSupporter(id, name) {
    document.getElementById("user_id").value = id;
    document.getElementById("user_descr").value = name
    showComboBoxList("user_descr", "comboboxlist");
}

function ValidateForm(snapshot) {

    var shortdescr = trim(document.getElementById("ShortDescr").value);
    // var expires    = trim(document.getElementById("expires").value);

//Startzeit-Uhrzeit validieren:
    var objRepeatInterval = document.getElementById("repeat_form_interval");
    if (document.getElementById("StartTimePoint") && 'none' != objRepeatInterval.style.display) {
        var elStartTimePoint = document.getElementById("StartTimePoint");
        var StartTimePoint = trim(elStartTimePoint.value);
    }

    ValidPage = true;
    HTMLOutput = "<ul>";

    var tref_obj = document.getElementById("TicketRefId");
    var repeatMode = document.getElementById("RepeatMode");

    // Wurde die Kurzbeschreibung zumindest ausgefuellt ?
    if (repeatMode.value != 'none' && (!tref_obj || tref_obj.value <= 0) && (shortdescr == "" || shortdescr.length < 4)) {
        document.getElementById("ShortDescrField").className = "form-group row mb-3 has-error";
        document.getElementById("ShortDescr").focus();
        HTMLOutput += "<li>" + ShortDescrErr + "</li>";
        ValidPage = false;
    } else {
        document.getElementById("ShortDescr").className = "form-control";
    }

    var Title = trim(document.getElementById("Title").value);

    if ((!tref_obj || tref_obj.value <= 0) && (Title == "")) {
        document.getElementById("TitleField").className = "form-group row mb-3 has-error";
        document.getElementById("Title").focus();
        HTMLOutput += "<li>" + TitleErr + "</li>";
        ValidPage = false;
    } else {
        document.getElementById("Title").className = "form-control";
    }

    if (typeof (Cat1Err) == "string") {
        var obj = document.getElementById("CategoryID1");
        var category_id = parseInt(obj.value);
        if (isNaN(category_id) || category_id == 0) {
            HTMLOutput += "<li>" + Cat1Err + "</li>";
            ValidPage = false;
        }
    }

    if (undefined != elStartTimePoint) {
        //if(StartTimePoint){
        if (StartTimePoint != '') {
            var newTime = is_time_HM(StartTimePoint);
            if (false == newTime) {
                elStartTimePoint.className = "textboxerror";
                HTMLOutput += "<li>" + StartTimePointErr + "</li>";
                ValidPage = false;
            } else {
                elStartTimePoint.value = newTime;
                elStartTimePoint.className = "form-control";
            }
        } else {
            elStartTimePoint.className = "textboxerror";
            HTMLOutput += "<li>" + StartTimePointErr + "</li>";
            ValidPage = false;
        }
        //}
    }


    // Ticket kann erstellt werden
    if (ValidPage) {

        if (snapshot == 1) {
            document.getElementById("snapshot").value = 1;
        }
        $("#hm-TicketTemplateForm").submit();

    } else {

        document.getElementById("errorres").innerHTML = HTMLOutput + "</ul>";
        document.getElementById("errorbox").style.visibility = "visible";
        document.getElementById("errorbox").style.display = "block";
    }

}

function showFrame(oldFrame, newFrame) {

    document.getElementById('comboboxlist').style.display = "none";
    document.getElementById('comboboxlist2').style.display = "none";

    document.getElementById(oldFrame).style.display = "none";
    document.getElementById(oldFrame + "Content").style.display = "none";
    document.getElementById(newFrame).style.display = "block";
    document.getElementById(newFrame + "Content").style.display = "block";
}


function switchPage(page) {
    makeHttpRequest("index.php?module=upload&event=AJAXUploadList&pos=" + page, "buildAttachmentList", true);
}

function is_time_HM(time) {
    var tmp = time.split(':');

    if (2 != tmp.length) {
        //Zeit liegt offenbar nicht im format H:M vor.
        //Evtl: ja 0800 für 08:00 Uhr? Oder 800 für 8:00 Uhr:
        if (4 == time.length) {
            time = time.substr(0, 2) + ':' + time.substr(-2, 2);
        } else {
            if (3 == time.length) {
                time = time.substr(0, 1) + ':' + time.substr(-2, 2);
            }
        }
    }
    //alert('time: '+ time);

    var Ausdruck = /^(?:2[0-3]||(([0-9]||0[0-9])||1[0-9])):[0-5][0-9]$/;
    var Ergebnis = Ausdruck.test(time);
    //if(time.match('/^(?:2[0-3]||(([0-9]||0[0-9])||1[0-9])):[0-5][0-9]$/') ){
    if (Ergebnis) {
        return time;
    } else {
        return false;
    }
}


