
var CMS = {};

CMS.loader = new YAHOO.util.YUILoader();
CMS.loader.loadOptional = false;
// CMS.loader.base = 'extern/yui/build/';
// CMS.loader.filter = 'RAW';

/*****************************************
Main
*****************************************/
CMS.main = {};
CMS.main.onInitSuccess = new YAHOO.util.CustomEvent("mainInitSuccess");

YAHOO.util.Event.onDOMReady(function() 
{
    CMS.main.init();
});

CMS.main.init = function() 
{
    CMS.loader.onSuccess = CMS.main.success;
    CMS.loader.insert();
}

CMS.main.success = function() 
{
    CMS.main.onInitSuccess.fire();
}

/*****************************************
Utilities
*****************************************/
CMS.util = {};

//refresh a form ... for example when a selectbox item changed
CMS.util.refresh = function(o)
{
    o.form.action += "&refresh";
    o.form.submit();
}

CMS.util.confirm_delete = function()
{
    if (confirm("Are you sure you wish to delete the selected record"))
    {
        return true;
    }
    
    return false;
}

/*****************************************
Manage http requests
*****************************************/
CMS.HttpRequest = function()
{
    this.parent = null;
    this.sUrl = null;
    this.postData = null;
    this.callbackSuccess = null;
    this.callbackError = null;
    this.argument = null; //additional custom data
    
    //the connection object
    this.request = null; 
    
    //to hide status panel on success?
///    this.hideStatus = true;
}

CMS.HttpRequest.prototype.asyncRequest = function() 
{
    var handleSuccess = function(o) 
    {
        var root, admin;
        var result = 0;
        var error = '';
        var e;

    	if (o.responseXML != undefined && o.responseXML != null) 
        {
            root = o.responseXML.documentElement; 
            if (root == null) 
            {
                error = "ERROR (request-document):\n" + o.responseText; //error
                //return;
            } 
            else 
            {
                e = root.getElementsByTagName('admin');
                if (e.length > 0) 
                {
                    if (e[0].childNodes.length > 0) 
                    {
                        admin = e[0].firstChild.nodeValue;    
                        if (admin == 0) 
                        {
///                            main.panelStatus.hide();

                            //when session has expired ... if you make a request, will be redirected to index page
                            alert("Not logged (request)");
///                            window.location.href = "index.php";
                            return;
                        } 
                    } 
                } 

                e = root.getElementsByTagName('result');
                if (e.length > 0) 
                {
                    if (e[0].childNodes.length > 0) 
                    {
                        result = e[0].firstChild.nodeValue;
                    }    
                    else
                    {
                        //error = "ERROR (request-result-empty):\n" + o.responseText; //error: the "result" tag is empty
                        //return;
                    }
                }    
                else
                {
                    error = "ERROR (request-result):\n" + o.responseText; //error: doesn't exist the "result" tag in the xml document
                    //return;
                }
            } 
        } 
        else
        {
            error = "ERROR (request-xml):\n" + o.responseText; //error
            //return;
        }

        if (error == '') 
        {
            if (result == 1) 
            {
///                if (this.hideStatus) main.panelStatus.hide();

                //invoke the parent success routine
                if (this.callbackSuccess != null) 
                {
                    this.callbackSuccess.apply(this.parent, [o]);
                }
            } 
            else
            {
                e = root.getElementsByTagName('error');
                if (e.length > 0) 
                {
                    for (var i=0; i<e.length; i++)
                    {
                        if (e[i].childNodes.length > 0) 
                        {
                            error += e[i].firstChild.nodeValue + "\n";
                        }
                    }
                }
                if (error == '')
                {
                    error = "ERROR (request):\n" + o.responseText;
                }
                
                //invoke the parent error routine
                if (this.callbackError != null) 
                {
                    this.callbackError.apply(this.parent, [o]);
                }                
            }
        }
        
        if (error != '') 
        {
///            main.panelStatus.hide();

            alert(error);
        }
    }

    var handleFailure = function(o)
    {
        var error = "ERROR (request-connection)";

///        main.panelStatus.hide();

    	if (o.responseText !== undefined) 
        {
    		error += "\nTransaction id: " + o.tId;
    		error += "\nHTTP status: " + o.status;
    		error += "\nStatus code message: " + o.statusText;
    	}
        
        alert(error);
    }                        
    
    var callback = {
        success: handleSuccess, 
        failure: handleFailure,
        scope: this
    }
    
    //re-position status panel, set message and show it
    // main.panelStatus.setPosition();
    // main.panelStatus.setMessage(main.panelStatusMsg['loading'], "#FFFF99");
    // main.panelStatus.show();
    
    this.request = YAHOO.util.Connect.asyncRequest('POST', this.sUrl, callback, this.postData);
}


/***********************************************************
Functions for XML data
***********************************************************/
CMS.XmlData = function(xmlDoc)
{
    this.xmlDoc = xmlDoc;
}

CMS.XmlData.prototype.valueByTag = function(tagName) 
{
    var nodeValue;
    
    var e = this.xmlDoc.getElementsByTagName(tagName);
    if (e.length > 0) 
    {
        if (e[0].childNodes.length > 0) 
        {
            nodeValue = e[0].firstChild.nodeValue;    //text
        }    
        else
        {
            nodeValue = '';
        }
    }    
    else
    {
        nodeValue = '';
    }

    return nodeValue;
}

CMS.XmlData.prototype.valueById = function(id) 
{
    var node;
    var nodeValue = '';
    
    var e = this.xmlDoc.getElementsByTagName('data'); //only search on <data> nodes
    for (var i=0; i<e.length; i++) 
    {
        node = e.item(i);
        nodeId = node.getAttribute('id');
        
        if (nodeId == id)
        {
            if (node.childNodes.length > 0) 
            {
                nodeValue = node.firstChild.nodeValue;    //text
                break;
            }    
            else
            {
                nodeValue = '';
            }
        }
    }
    
    return nodeValue;
}

CMS.XmlData.prototype.valuesByTag = function(tagName) 
{
    var nodeValue;
    var arrayValues = new Array();
    
    var e = this.xmlDoc.getElementsByTagName(tagName);
    for (var i=0; i<e.length; i++) 
    {
        node = e.item(i);
        nodeId = node.getAttribute('id');
        
        if (node.childNodes.length > 0) 
        {
            nodeValue = node.firstChild.nodeValue;    //text
        }    
        else
        {
            nodeValue = '';
        }
        
        arrayValues[nodeId] = nodeValue;
    }

    return arrayValues;
}

