//------------------------------------------------------------------------------
//  WAX 1.5 - general-purpose web application library
//  Copyright (C) 2000-2006 SnapWorks
//  Website : http://www.snap-works.com/
//  Email   : info@snap-works.com
//------------------------------------------------------------------------------

//  Helper functions

//  TODO: help function to get form field.

function $E(id)
{
    return document.getElementById(id);
}

function $V(name)
{
    return JSTemplate.getVariable(name);
}

function $(name)
{
    var ret = $V(name);
    return (null != ret) ? ret : $E(name);  
}

function createCenteredElement(parent, width, height)
{
    document.createElement('div');
    
    var container1 = document.createElement('div');
    var container2 = document.createElement('div');
    
    container1.style.position = 'absolute';
    container1.style.left = '50%';
    container1.style.top = '50%';
    
    container2.style.position = 'absolute';
    container2.style.left = Math.round(-width/2) + 'px';
    container2.style.top = Math.round(-height/2) + 'px';
    container2.style.width = (width) + 'px';
    container2.style.height = (height) + 'px';
    
    container1.appendChild(container2);
    parent.appendChild(container1);
    return container2;
}

//  Base class for object-based variables

function JSObject(object) // {{{
{
    if (object) {
        for (var m in object) {
            this[m] = object[m];
        }
    }
} // }}}

JSObject.prototype.format = function(key) // {{{
{
    return this[key];
} // }}}

JSObject.prototype.polish = function(node) // {{{
{
} // }}}

function ExtendClass(superClass, baseClass) // {{{
{
    function inheritance() {}
    inheritance.prototype = baseClass.prototype;
    superClass.prototype = new inheritance();
    superClass.prototype.constructor = superClass;
} // }}}

function toJSObject(className, object) // {{{
{
    if (object instanceof Array) {
        for (var i=0; i<object.length; i++)
            object[i] = toJSObject(className, object[i]);
        return object;
    } else {
        if (className) {
            return new className(object);
        } else {
            return new JSObject(object);
        }
    }
} // }}}

//  Template class

function JSTemplate() // {{{
{
    this.variables = {};
    this.messages = [];
    this.redirect = '';
} // }}}

JSTemplate.prototype.getVariable = function(name) // {{{
{
    return this.variables[name];
} // }}}

JSTemplate.prototype.setVariable = function(name, value) // {{{
{
    this.variables[name] = value;
} // }}}

JSTemplate.prototype.setMessages = function(messages) // {{{
{
    this.messages = messages;
} // }}}

JSTemplate.prototype.setRedirect = function(url) // {{{
{
    this.redirect = url;
} // }}}

JSTemplate.prototype.closeMessage = function() // {{{
{
    $('JSTemplateMessageDialog').style.display = 'none';
    this.gotoRedirect();
} // }}}

JSTemplate.prototype.gotoRedirect = function() // {{{
{
    if (0 != this.redirect.length) {
        document.location = this.redirect;
    }
} // }}}

JSTemplate.prototype.run = function() // {{{
{
    if (this.messages.length > 0) {
        document.write("<div id=\"JSTemplateMessageDialog\" style=\"position: absolute; width: 90%; top: 20%; height: 1px; z-index: 99;\" align=\"center\">"
            + "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td style=\"border-left: 1px solid #D4D0C8; border-top: 1px solid #D4D0C8; border-right: 1px solid #404040; border-bottom: 1px solid #404040\"><table width=\"400px;\" cellpadding=\"10\" cellspacing=\"10\" border=\"0\""
            + " style=\"background-color: #D4D0C8; "
            + "font-family: helvetica; font-size: 10pt; border-left: 1px solid #ffffff; border-top: 1px solid #ffffff; border-right: 1px solid #808080; border-bottom: 1px solid #808080;\">\n");
        document.write("<tr><td width=\"64\" valign=\"top\"><img src=\""+this.variables['FRAMEWORK_WEBPATH']+"/common/error.png\"></td>\n");
        document.write("<td align=\"left\" style=\"vertical-align: top; font-size: 10pt\">\n");
        for (var i=0; i<this.messages.length; i++ ) {
            if (i>0) { document.write('<hr size="1">'); }
            document.write(this.messages[i][1]);
        }
        document.write("</td></tr>");
        document.write("<tr><td colspan=\"2\" align=\"right\">");
        document.write("<input type=\"button\" value=\"Ok\" style=\"height: 23px; width: 96px;\" onclick=\"JSTemplate.closeMessage();\"></td></tr>");
        document.write("</table></td></tr></table></div>\n");
    } else {
        this.gotoRedirect();
    }
} // }}}

JSTemplate.prototype.createElementFromDummy = function(dummyElement, row, formatter) // {{{
{
    var clone = dummyElement.cloneNode(true);
    clone.id = null;
    
    var ret;    
    
    function replacecb(m0, m1)
    {
        ret = false;
        if (formatter) ret = formatter(m1, row);
        if (false === ret && row instanceof JSObject) ret = row.format(m1);
        return (false !== ret) ? ret : row[m1];
    }
    
    clone.innerHTML = clone.innerHTML.replace(/%([_a-z0-9]*?)%/gi, replacecb);
    //if (row instanceof JSObject) row.polish(clone);
    return clone;
} // }}}

JSTemplate.prototype.createElementsFromDummy = function(parentElement, dummyElement, rows, formatter, polisher) // {{{
{
    var node;
    for (var i=0; i<rows.length; i++) {
        node = this.createElementFromDummy(dummyElement, rows[i], formatter);
        parentElement.appendChild(node);
        if (0 != (i % 2)) { node.className +=  ' even'; }
        if (polisher) polisher(rows[i], node);
        if (rows[i] instanceof JSObject) rows[i].polish(node); 
    }
    dummyElement.parentNode.removeChild(dummyElement);
} // }}}
