/*
 * handle surveys
 */

function loadSurveyList() {

    var surveyEditList = jQuery("select.survey_edit_list");

    if (surveyEditList) {

        var url = surveyEditList.attr("getSurveysUrl").replace(/[&]amp[;]/,"&");
        jQuery.ajax({
            url: url,
            type: "POST",
            dataType: "xml",
            success: function(data) {

                surveyEditList.children("option").remove();
                var options = data.getElementsByTagName("option");
                for (i=0; i<options.length; i++) {

                    var option = options[i];
                    surveyEditList.append("<option value='" + option.getAttribute("value") + "'>" + option.getAttribute("text") + "</option>");
                }
                jQuery("div.survey_edit_locale div.answer_inserted").remove();
                oneMoreAnswer();
            },
            failure: function(req,msg) {
                
                alert("failure:" + msg);
            }
        });
    }
}

function loadSurvey(me) {

    var surveyEditList = jQuery("select.survey_edit_list");

    if (surveyEditList) {

        var selIdx = me.selectedIndex;
        if (selIdx >= 0) {

            var value = me.options[selIdx].value;
            var url = surveyEditList.attr("getSurveyUrl").replace(/[&]amp[;]/,"&");
            jQuery.ajax({
                url: url,
                type: "POST",
                dataType: "xml",
                data: {

                    uuid: value
                },
                success: function(data) {

                    var id = data.getElementsByTagName("id")[0];
                    var surveyEditId = jQuery("#survey_id");
                    if (id.firstChild != null) id = id.firstChild.nodeValue;
                    surveyEditId.val(id);

                    var technicalName = data.getElementsByTagName("technicalName")[0];
                    var surveyEditTechnicalName = jQuery("#survey_technicalName");
                    if (technicalName.firstChild != null) technicalName = technicalName.firstChild.nodeValue;
                    else technicalName = "";
                    surveyEditTechnicalName.val(technicalName);

                    jQuery("div.survey_edit_locale div.answer_inserted").remove();
                    var langlist = jQuery("td.survey_edit_details");
                    var details = data.getElementsByTagName("detail");
                    var i;
                    for (i=0; i<details.length; i++) {

                        var detail = details[i];
                        var locale = detail.getAttribute("name");

                        var description = detail.getElementsByTagName("description")[0];
                        var surveyEditDescription = jQuery("#detail_" + locale + "_description");
                        if (description.firstChild != null) description = description.firstChild.nodeValue;
                        else description = "";
                        surveyEditDescription.val(description);

                        var question = detail.getElementsByTagName("question")[0];
                        var surveyEditQuestion = jQuery("#detail_" + locale + "_question");
                        if (question.firstChild != null) question = question.firstChild.nodeValue;
                        else question = "";
                        surveyEditQuestion.val(question);

                        var answers = detail.getElementsByTagName("answer");
                        var pattern = jQuery("#answer_pattern");
                        var locarea = jQuery("#locale_" + locale);
                        var j;
                        for (j=0; j<answers.length; j++) {

                            var answer = answers[j];
                            var ans = pattern.html();
                            ans = ans.replace("NNN","answer_" + locale + "_" + answer.getAttribute("id"));
                            ans = ans.replace("value=VVV", "value='" + answer.firstChild.nodeValue + "'");
                            ans = ans.replace("VVV", answer.firstChild.nodeValue);
                            ans = ans.replace("survey_edit_locale_answer_more", "survey_edit_locale_answer_more_hidden");
                            locarea.append(ans);
                        }
                        var ans = pattern.html();
                        ans = ans.replace("NNN","answer_" + locale);
                        ans = ans.replace("value=VVV", "value=''");
                        ans = ans.replace("VVV", "");
                        locarea.append(ans);
                    }
                    var validFrom = data.getElementsByTagName("validFrom")[0];
                    var surveyEditValidFrom = jQuery("#survey_validFrom");
                    if (validFrom.firstChild != null) validFrom = validFrom.firstChild.nodeValue;
                    else validFrom = "";
                    surveyEditValidFrom.val(validFrom);

                    var validTo = data.getElementsByTagName("validTo")[0];
                    var surveyEditValidTo = jQuery("#survey_validTo");
                    if (validTo.firstChild != null) validTo = validTo.firstChild.nodeValue;
                    else validTo = "";
                    surveyEditValidTo.val(validTo);

                    var state = data.getElementsByTagName("state")[0];
                    var surveyEditState = jQuery("#survey_state");
                    if (state.firstChild != null) state = state.firstChild.nodeValue;
                    else state = "";
                    surveyEditState.val(state);

                    var category = data.getElementsByTagName("category")[0];
                    var surveyEditCategory = jQuery("#survey_category");
                    if (category.firstChild != null) category = category.firstChild.nodeValue;
                    else category = "";
                    surveyEditCategory.val(category);

                    var comment = data.getElementsByTagName("comment")[0];
                    var surveyEditComment = jQuery("#survey_comment");
                    if (comment.firstChild != null && comment.firstChild.nodeValue == "yes") surveyEditComment.attr("checked", true);
                    else surveyEditComment.attr("checked", false);
                },
                failure: function(req,msg) {

                    alert("failure:" + msg);
                }
            });
        }
    }
}

function oneMoreAnswer() {

    jQuery("div.survey_edit_locale img.survey_edit_locale_answer_more").each( function(i) {

        this.className += "_hidden";
    });
    jQuery("div.survey_edit_locale").each( function(i) {

        var instance = jQuery(this);
        var pattern = jQuery("#answer_pattern");
        var locale = instance.attr("id").replace("locale_", "");
        var locarea = jQuery("#locale_" + locale);
        var ans = pattern.html();
        ans = ans.replace("NNN","answer_" + locale);
        ans = ans.replace("value=VVV", "value=''");
        ans = ans.replace("VVV", "");
        locarea.append(ans);
        jQuery("#answer_pattern div.answer_label").remove();
    })
}

function newSurvey() {

    jQuery("select.survey_edit_list").val(-1);
    jQuery("#survey_id").val("");
    jQuery("#survey_technicalName").val("");

    jQuery("div.survey_edit_locale").each( function(i) {

        var instance = jQuery(this);
        var locale = instance.attr("id").replace("locale_", "");
        jQuery("#detail_" + locale + "_description").val("");
        jQuery("#detail_" + locale + "_question").val("");
    })

    jQuery("div.survey_edit_locale div.answer_inserted").remove();
    oneMoreAnswer();

    jQuery("#survey_validFrom").val("");
    jQuery("#survey_validTo").val("");
    jQuery("#survey_state").val("");
    jQuery("#survey_category").val("");
    jQuery("#survey_comment").attr("checked",false);
}

function deleteSurvey() {

    var surveyEditList = jQuery("select.survey_edit_list");

    if (surveyEditList) {

        var value = surveyEditList.val();
        if (value != null && value.length > 0) {

            if (confirm('"' + jQuery('#survey_technicalName').val() + '" wirklich löschen?')) {

                var url = surveyEditList.attr("deleteSurveyUrl").replace(/[&]amp[;]/,"&");
                jQuery.ajax({
                    url: url,
                    type: "POST",
                    data: {

                        uuid: value
                    },
                    success: function(data) {

                        loadSurveyList();
                        newSurvey();
                    },
                    failure: function(req,msg) {

                        alert("failure:" + msg);
                    }
                });
            }
        } else {

            alert("Bitte erst eine Umfrage selektieren");
        }
    }
}

function saveSurvey() {

    var surveyEditList = jQuery("select.survey_edit_list");

    if (surveyEditList) {

        var url = surveyEditList.attr("saveSurveyUrl").replace(/[&]amp[;]/,"&");
        var langlist = jQuery("td.survey_edit_details");

        var data = new Object();
        data.id = jQuery('#survey_id').val();
        data.technicalName = jQuery('#survey_technicalName').val();
        data.langlist = langlist.attr("langs");

        var langs = data.langlist.split(" ");
        var qnum = 0;
        for (i=1; i<langs.length; i++) {

            data["description_" + langs[i]] = jQuery('#detail_' + langs[i] + '_description').val();
            data["text_" + langs[i]] = jQuery('#detail_' + langs[i] + '_question').val();
            var qnumsub = 0;

            jQuery("input[name^=answer_" + langs[i] + "]").each( function(k) {

                data["answer_" + langs[i] + "_" + k] = this.value;
                qnumsub ++;
            });
            if (qnumsub >= qnum) {

                qnum = qnumsub;
                data.qnum = qnum;
            }
        }
        
        data.validFrom = jQuery('#survey_validFrom').val();
        data.validTo = jQuery('#survey_validTo').val();
        data.state = jQuery('#survey_state').val();
        // data.category = jQuery('#survey_category').val();
        var sc = document.getElementById("survey_category");
        if (sc.selectedIndex >= 0) data.category = sc.options[sc.selectedIndex].value;
        else data.category = "";
        sc = document.getElementById("survey_comment");
        if (sc.checked) data.comment = "yes";
        else data.comment = "no";

        jQuery.ajax({
            url: url,
            type: "POST",
            data: data,
            complete: function(request,status) {

                switch (request.getResponseHeader("portlet.http-status-code")) {

                    case "200":
                        loadSurveyList();
                        newSurvey();
                        break;

                    case "500":
                        alert("Datums/Zeit-Format stimmt nicht");
                        break;
                }
            }
        });
    }
}

function checkForAjaxCall(theId) {

    var container = document.getElementById("voting_" + theId);
    var form = document.getElementById("voting_form_" + theId);
    
    if (container && form) {

        var data = new Object();
        var i;
        for (i=0; i<form.elements.length; i++) {

            if (form.elements[i].type == "radio") {

                if (form.elements[i].checked) data[form.elements[i].name] = form.elements[i].value;

            } else data[form.elements[i].name] = form.elements[i].value;
        }

        jQuery.ajax({

            url: form.action,
            type: "POST",
            data: data,

            success: function(data) {

                jQuery('#voting_' + theId).html(data)
            }
        });
    }

    return (container == null);
}

function checkForAjaxCall2(theId,portletnamespace) {

    var container = document.getElementById("voting_" + theId);
    var form = document.getElementById(portletnamespace + "voting_form_" + theId);
    
    if (container && form) {

        var data = new Object();
        var i;
        for (i=0; i<form.elements.length; i++) {

            if (form.elements[i].type == "radio") {

                if (form.elements[i].checked) data[form.elements[i].name] = form.elements[i].value;

            } else data[form.elements[i].name] = form.elements[i].value;
        }

        jQuery.ajax({

            url: form.action,
            type: "POST",
            data: data,

            success: function(data) {

                jQuery('#voting_' + theId).html(data)
            }
        });
    }

    return (container == null);
}