var pendingMask;

function showPendingItems(pendingItemsID)
{
    // Update the form field
    document.getElementById('pendingItemsSelected').value = pendingItemsID;
    
    // Clear everything
    document.getElementById('pendingitems').innerHTML='';
    
    PluginPendingItems();
}

function PluginPendingItems()
{
    // Clean the DIV used for showing the data
    document.getElementById('pendingitems').innerHTML = '';
    
    showMask();
    
    // Clean the info of the previously loaded js file to avoid conflicts
    cleanjsfiles();
    
    var pendingItemsID = document.getElementById('pendingItemsSelected').value;
    
    switch(pendingItemsID){
        case 'orders':
            // Load a single JavaScript file and execute a callback when it finishes loading.
            LazyLoad.js(
                '../plugins/dashboard/pendingitems/ViewPendingOrders.js',
                function(){
                    hideMask();
                    ViewPendingOrders();
                }
            );
            break;
        case 'cc':
            // Load a single JavaScript file and execute a callback when it finishes loading.
            LazyLoad.js(
                '../plugins/dashboard/pendingitems/ViewPendingCC.js',
                function(){
                    hideMask();
                    ViewPendingCC();
                }
            );
            break;
        default:
            hideMask();
            document.getElementById('pendingitems').innerHTML = '&nbsp;&nbsp;<font color=red>These items are not available yet.</font>';
            break;
    }
}

function showMask()
{
    if(!pendingMask){
        pendingMask = new Ext.LoadMask('pendingmask', {msg:"Please wait..."});
    }
    
    document.getElementById('pendingmask').style.display = '';
    pendingMask.show();
}

function hideMask()
{
    pendingMask.hide();
    document.getElementById('pendingmask').style.display = 'none';
}

function cleanjsfiles()
{
    // Remove the objects loaded from the js file
    if(typeof removeObjects == 'function'){
        removeObjects();
        
        // Remove the removeObjects function, because each js file charge its own function
        removeObjects = null;
    }
    
    // Remove the js file. You need to list all the js files here.
    removejscssfile("ViewPendingOrders.js", "js");
    removejscssfile("ViewPendingCC.js", "js");
}

function removejscssfile(filename, filetype)
{
    //determine element type to create nodelist from
    var targetelement = (filetype == "js")? "script" : (filetype == "css")? "link" : "none";
    
    //determine corresponding attribute to test for
    var targetattr = (filetype == "js")? "src" : (filetype == "css")? "href" : "none";
    
    var allsuspects = document.getElementsByTagName(targetelement);
    
    //search backwards within nodelist for matching elements to remove
    for(var i = allsuspects.length; i >= 0; i--){
        if(allsuspects[i]
          && allsuspects[i].getAttribute(targetattr) != null
          && allsuspects[i].getAttribute(targetattr).indexOf(filename) != -1){
            //remove element by calling parentNode.removeChild()
            allsuspects[i].parentNode.removeChild(allsuspects[i]);
        }
    }
}