
var CQ_collab_comments_loadedForms = {};
var CQ_collab_comments_defaultMessage = ""; // overlay in page


function CQ_collab_comments_toggleForm(buttonId, formId, url) {
    var form = document.getElementById(formId);
    var button = document.getElementById(buttonId);
    if (!CQ_collab_comments_loadedForms[formId]) {
        var request = document.all ? new ActiveXObject("Microsoft.XMLHTTP") :
                      new XMLHttpRequest();
        try {
            request.open("GET", url, true);
            request.onreadystatechange = function() {
                form.innerHTML = request.responseText;
            };
            request.send(null);
        } catch (e) {
            alert("Error loading form: " + url);
        }
        CQ_collab_comments_loadedForms[formId] = true;
    }
    var hidden = form.style.display != "block";
    form.style.display = hidden ? "block" : "none";
    button.innerHTML = hidden ? "Cancel" : "Reply";
}

function CQ_collab_comments_handleOnFocus(el) {
    if(el.value == CQ_collab_comments_getDefaultMessage()) {
        el.value = "";
    }
    el.style.color = "#333";
}

function CQ_collab_comments_handleOnBlur(el) {
    if(el.value == "") {
        el.value = CQ_collab_comments_getDefaultMessage();
        el.style.color = "#888";
    }
    else {
        el.style.color = "#333";
    }
}

function CQ_collab_comments_validateFields(id) {
    // Validate text
    var message = document.getElementById(id + "-text");
    if (message.value == "" || message.value == CQ_collab_comments_getDefaultMessage()) {
        CQ_collab_comments_showError('Please enter a comment', id + "-text");
        return false;
    }
    return true;
}

function CQ_collab_comments_validateSubmit(id) {
    var message = document.getElementById(id + "-text");
    var email = document.getElementById(id + "-email");
    var url = document.getElementById(id + "-url");
    var author = document.getElementById(id + "-author");
    var nameHint = document.getElementById(id + "-nameHint");
    var validEmail = /^[a-z0-9\-\_\+]+(\.[a-z0-9\-\_\+]+)*\@(([a-z0-9\-\_\+]+(\.[a-z0-9\-\_\+]+)*)+\.[a-z]{2,}|([0-9]+\.){3}[0-9]+)$/i;

    if (!CQ_collab_comments_validateFields(id)) {
        return false;
    }

 
    return true;
}

function CQ_collab_comments_showError(msg, id) {
    var errorElem = document.getElementById(id + "-error");
    if (!errorElem) {
        alert(msg);
    } else {
        errorElem.innerHTML = msg;
    }
}

function CQ_collab_comments_getDefaultMessage() {
    return CQ_collab_comments_defaultMessage;
}

