/*
 * Copyright 1997-2009 Day Management AG
 * Barfuesserplatz 6, 4001 Basel, Switzerland
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Day Management AG, ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Day.
 */

/**
 * Utility functions for comments components.
 */

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 your 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;
    }

    /*
    if(author.value.length < 1) {
        showError('Please enter a name to comment', id + "-author");
        return false;
    }

    // Validate email
    if(!validEmail.test(email.value)) {
        showError('Please enter a valid email to comment.', id + "-email");
        return false;
    }

    // Validate website url
    if(url.value != "" && url.value.indexOf('.') == -1) {
        showError('Please check your website URL (this field is optional)', id + "-url");
        return false;
    }
    */
    if (message) {
        // set name hint (as long as we are not sure if jcr:text will automatically be a name hint)
        nameHint.value = message.value.length > 20 ? message.value.substring(0, 19) : message.value;
    }
    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;
}
