/*
 * This is a set of functions to handle displaying or hiding a dhtml layer
 * that will be displayed at the center of the browser screen.
 *
 * A css style must be declared within the html page that includes this js
 * file. Ex:
 *
 * <style>
 * #myLayer
 * {
 *   display: none;
 *   position: absolute;
 *   left: 0px;
 *   top: 0px;
 *   z-index: 5;
 * }
 * </style>
 *
 * Copyright (C) 2003 by John Glorioso, Zitego Solutions, LLC
 * <jglorioso@zitego.com>. All rights reserved.
 *
 * Redistribution and use in source forms, with or without modification, are
 * permitted provided that the following condition is met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * @author John Glorioso
 * @version $Id: ajax.js,v 1.1 2008/02/20 15:19:35 jglorioso Exp $
 */

//DHTML include
document.write("<scr"+"ipt src=\"/js/floating_layer.js\"></scr"+"ipt>");
document.write("<scr"+"ipt src=\"/_js/ajax_functions.js\"></scr"+"ipt>");

ajaxTimeout = null;
/**
 * Executes the given form by submitting it.
 *
 * @param url The form action to submit to
 * @param name The form name
 * @param msg The save message
 */
function go(url, name, msg)
{
//alert("url="+url+", name="+name);alert(", qstring="+toQueryString(document[name]));
    saving = true;
    getObject("save_msg_txt").innerHTML = msg;
    toggleLayer(true, "save_msg");
    new ajax(url, {postBody: (name ? toQueryString(document[name]) : ""), onComplete: showResponse}).request();
    ajaxTimeout = window.setTimeout("timeoutPopup(true)", 30000);
}

/**
 * Shows the response by evaluating the returned text into Javascript. The javascript
 * is expected to construct an object named "resp" with the following properties:
 *
 * success: true|false
 * msg: The html to be displayed in the msg area.
 * last_updated: The new last updated date. (optional)
 *
 * Once done processing, it will call the postProcess function if it is defined.
 */
function showResponse(response)
{
//alert("response="+response);
    eval(response);
    var msg = getObject('msg');
    if (msg)
    {
        msg.innerHTML = resp.msg;
        if (resp.success) msg.className = "success_text";
        else msg.className = "error_text";
    }
    if (window.postProcess) postProcess(resp);
    var dt = getObject('last_updated');
    if (dt && dt.innerHTML) dt.innerHTML = resp.last_updated;
    var dt = getObject('created');
    if (dt && dt.innerHTML) dt.innerHTML = resp.created;
    if (resp.changed)
    {
        var dt = getObject('changed');
        if (dt && dt.innerHTML) dt.innerHTML = resp.changed;
    }
    if (resp.published)
    {
        var dt = getObject('published');
        if (dt && dt.innerHTML) dt.innerHTML = resp.published;
    }
    timeoutPopup();
}

/**
 * Returns the elements of a form url encoded.
 *
 * @return String
 */
function toQueryString(frm)
{
    var ret = "";
    var count = 0;
    for (var i=0; i<frm.elements.length; i++)
    {
        //if (frm.elements[i].value != "")
        //{
            if (frm.elements[i].type == "checkbox" || frm.elements[i].type == "radio")
            {
                if (frm.elements[i].checked)
                {
                    ret += (count++ > 0 ? "&" : "") + encodeURIComponent(frm.elements[i].name) + "=";
                    ret += encodeURIComponent(frm.elements[i].value);
                }
            }
            else if (frm.elements[i].type.indexOf("select") == 0)
            {
                ret += (count++ > 0 ? "&" : "") + encodeURIComponent(frm.elements[i].name) + "=";
                var count2 = 0;
                for (var j=0; j<frm.elements[i].options.length; j++)
                {
                    if (frm.elements[i].options[j].selected) ret += (count2++ > 0 ? "," : "") + encodeURIComponent(frm.elements[i].options[j].value);
                }
            }
            else
            {
                ret += (count++ > 0 ? "&" : "") + encodeURIComponent(frm.elements[i].name) + "=";
                ret += encodeURIComponent(frm.elements[i].value);
            }
        //}
    }
    return ret;
}

function showProps(obj)
{
    var props = "null";
    if (obj)
    {
        props = "";
        for (var prm in obj)
        {
            props += prm+": "+obj[prm]+"\n";
        }
    }
    alert(props);
}

function timeoutPopup(isErr)
{
    toggleLayer(false, "save_msg");
    saving = false;
    window.clearTimeout(ajaxTimeout);
    if (isErr) alert("An error has occurred processing your request. Please try again");
}

