﻿
    //  ------------------------------------------------------------------------------------------------------------------
    //  POPULATE STATIC PAGE DATA
    //  This function takes xml returned from an AJAX call, and dynamically maps the contents to static fields on the 
    //  HTML page.. it works out what type of data (boolean, currency, date etc), and knows what HTML elements to set 
    //  the value of.
    //  ------------------------------------------------------------------------------------------------------------------
function populateStaticPageData(xml) {

    //  First we check if any errors were returned..
    $(xml).find('error').children().each(function() {
        alert("Error: " + $(this).text());
        return;
    });

    //  Now we cycle through the returned data, and map the values...
    $(xml).find('row').children().each(function () {
        //	Get the node name which is actually the field name and maps to the ID of the HTML element.
        //	We have to convert to lowercase as the nodeName raw data seems to be upper case..
        //	We also get the actual node value, and the element type, data type attributes
        var nodeRef = this.nodeName.toLowerCase();
        var nodeVal = $(this).text();
        var htmlType = $(this).attr("htmltype");

        //	Now we need to know how to present the data.. depending on the html type..
        switch (htmlType) {
            case "text":
                $('#' + nodeRef).attr('value', nodeVal);
                break;
            case "number":
                $('#' + nodeRef).attr('value', nodeVal);
                break;
            case "textarea":
                $('#' + nodeRef).html(nodeVal);
                break;
            case "boolean":
                if (nodeVal == '1' || nodeVal == 'True') {
                    $('#' + nodeRef).attr('checked', 'checked');
                }
                else {
                    $('#' + nodeRef).attr('checked', null);
                }
                break;
            case "date":
                //  We are using a DHX datepicker for dates...
                test = new dhtmlxCalendarObject(nodeRef);
                test.setDateFormat('%m/%d/%Y');
                test.setDate(nodeVal);
                break;
        }
    });         // end of XML children parse

} // end of populatePageData
