﻿var feedbackJustOpened = false;
document.getElementById('feedback').style.cursor = 'pointer';
document.body.setAttribute("onclick", "javascript:if (!feedbackJustOpened) feedback(null);");

function feedback(ele) {
    var form = document.getElementById('feedbackForm');
    if (ele != null && form == null) {
        var div = document.createElement('div');
        div.setAttribute("id", "feedbackForm");
        div.setAttribute("style", "position: absolute; z-index: 1; left: " + ele.offsetLeft + "px; top: " + ele.offsetTop + "px; margin-top: -210px; margin-left: -175px; width: 350px; height: 200px; border-style: solid; border-width: 1px; border-color: lightgray gray gray lightgray; padding: 2px; background-color: silver;");
        div.setAttribute("onclick", "javascript:feedbackNoCloseClick();");
        var textarea = document.createElement('textarea');
        textarea.setAttribute("id", "feedbackText");
        textarea.setAttribute("style", "width: 344px; height: 168px; color: Gray; font-style: italic; margin-bottom: 2px;");
        textarea.setAttribute("onclick", "javascript:if (this.value=='váš názor') this.value='';this.style.color='black';this.style.fontStyle='normal';");
        textarea.appendChild(document.createTextNode("váš názor"));
        var label = document.createElement("label");
        label.setAttribute("id", "feedbackLabel");
        label.setAttribute("for", "email");
        label.setAttribute("style", "font-family: Arial; font-size: 13px;");
        label.appendChild(document.createTextNode("Email: "));
        var input = document.createElement("input");
        input.setAttribute("id", "feedbackEmail");
        input.setAttribute("name", "email");
        input.setAttribute("type", "text");
        input.setAttribute("value", "(nepovinné)");
        input.setAttribute("style", "color: Gray; font-style: italic; width: 180px;");
        input.setAttribute("onclick", "javascript:if (this.value=='(nepovinné)') this.value='';this.style.color='black';this.style.fontStyle='normal';");
        var button = document.createElement("button");
        button.setAttribute("style", "width: 121px; margin-left: 2px; cursor: pointer;");
        button.setAttribute("onclick", "javascript:feedbackSend(this);");
        button.appendChild(document.createTextNode("Odeslat"));
        div.appendChild(textarea);
        div.appendChild(label);
        div.appendChild(input);
        div.appendChild(button);
        ele.parentNode.appendChild(div);
        feedbackNoCloseClick();
    } else if (form != null) {
        form.parentNode.removeChild(form);
    }
}

function feedbackNoCloseClick() {
    feedbackJustOpened = true;
    setTimeout("feedbackJustOpened = false", 500);
}

function feedbackSend(ele) {
    ele.disabled = true;
    document.getElementById('feedbackLabel').disabled = true;
    var form = document.getElementById('feedbackForm');
    var textarea = document.getElementById('feedbackText');
    var email = document.getElementById('feedbackEmail');
    textarea.disabled = true;
    email.disabled = true;
    var msg = document.createElement('div');
    msg.setAttribute("style", "position: absolute; z-index: 2; left: " + (textarea.offsetLeft + 95) + "px; top: " + (textarea.offsetTop + 60) + "px; background-color: silver; font-family: Arial; font-size: 12px; padding: 20px;");
    msg.appendChild(document.createTextNode("Děkujeme za váš názor."));
    form.appendChild(msg);

    if (typeof XMLHttpRequest != "undefined") {
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function() { feedbackSent(xmlHttp); };
        xmlHttp.open('POST', 'http://i.dajbych.net/feedback.ashx', true);
        xmlHttp.send("<feedback page=\"" + escape(document.location) + "\" email=\"" + escape(email.value) + "\">" + escape(textarea.value) + "</feedback>");
    } else {
        feedback(null);
    }
}

function feedbackSent(xmlHttp) {
    if (xmlHttp.readyState == 4) feedback(null);
}