/********************************************************
 *                                                      *
 * Script serving feedback form                         *
 * Copyright (c), Intelrate, 2008                       *
 *                                                      *
 ********************************************************/

/**
 * Feedback Form consinsing of elements with IDs mapped on corresponding Feedback class.
 */
var Feedback =
{
    componentId : '',
    formId : '',
    action : '',
    messageId : '',
    mailId : '',
    secureCodeId : '',
    imageId : ''
};

/// Asynchronous request to the server
var httpObject = getXmlHttpObject();

/**
 * Fills Feedback form and proceeds output.
 *
 * @param componentId identifier of Feedback component on Web page
 * @param formId element where feedback form should be shown
 * @param action the page to go to
 * @param messageId id of message element in the form
 * @param mailId id of sender's email element in the form
 * @param secureCodeId id of field for secure code in the form
 * @param imageId id of image element displaying secure code image
 */
function feedback(componentId, formId, action, messageId, mailId, secureCodeId, imageId)
{
    Feedback.componentId = componentId;
    Feedback.formId = formId;
    Feedback.action = action;
    Feedback.messageId = messageId;
    Feedback.mailId = mailId;
    Feedback.secureCodeId = secureCodeId;
    Feedback.imageId = imageId;

    showFeedbackForm();
}

/**
 * Constructs and shows or hides Feedback form.
 */
function showFeedbackForm()
{
    if (!window.XMLHttpRequest)
    {
        document.getElementById(Feedback.componentId).innerHTML =
            '<span style="color:#ffa72e;">AJAX is not supported by this browser.</span>';
        return;
    }

    switchVisibility(Feedback.formId);
}

/**
 * Sends message and secure code to the server.
 */
function checkSecureCode()
{
    // shield input from '&'
    var messageText = document.getElementById(Feedback.messageId).value.replace("&", "<ampersand>");
    var mailAddress = document.getElementById(Feedback.mailId).value.replace("&", "<ampersand>");
    httpObject.open("GET", Feedback.action + '?'
            + Feedback.secureCodeId + '=' + document.getElementById(Feedback.secureCodeId).value
            + '&' + Feedback.messageId + '=' + messageText
            + '&' + Feedback.mailId + '=' + mailAddress);
    httpObject.onreadystatechange = handleHTTPResponse;
    httpObject.send(null);
}

/**
 * Prevents users from sending their latest novel to us.
 *
 * @param textFieldId HTMLElement id text field element to limit
 * @param limit maximal allowed number of characters
 */
function checkLimit(textFieldId, limit)
{
    var textField = document.getElementById(textFieldId);

	if (textField.value.length > limit)
	{
		textField.value = textField.value.substring(0, limit);
	}
}

