﻿
// ############################################################
// DisplayCustomProperties: Fetch and display a set of custom properties...
// ############################################################
function DisplayCustomProperties(xmlSelection, selCount) {
    // Retrieve the feature key and layer id from the selected feature's XML...
    var props = xmlSelection.getElementsByTagName("Property");
    var featureKey = "";
    var featureName = "";
    var layerId = "";
    
    if (props != null) {
        if (props.length > 0) {
            for (var i = 0; i < props.length; i++) {
                var name = props[i].getAttribute("name");
                if (name == "datalink" || name == "PRIMARYINDEX") {
                    featureKey = props[i].getAttribute("value");
                    var layers = xmlSelection.getElementsByTagName("Layer");
                    layerId = layers[0].getAttribute("id");
                    break;
                }
            }
        }
    }

    // Get the feature class from the selected feature's XML to 
    // determine if this is a transformer feature...
    var featureClass = xmlSelection.getElementsByTagName("Class");
    if (featureClass != null) {
        classId = featureClass[0].getAttribute("id");
        if (classId.indexOf(":transformer_locations") != -1) {
            DisplayCustomTransformerDetailProperties(featureKey, "", "");
            return;
        }
    }
    
//    //
//    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//    //START TEST...
//    var els = xmlSelection.getElementsByTagName("Class");
//    var classid = els[0].attributes[0].nodeValue;
//    if (classid == "BELD:transformer") {
//        var params = "&classid=" + classid;
//        var response = SynchronousRequestAsText("GET_PROPERTY_PANEL_CONTENT", params);
//        if (response != null) {
//            var content = GetMapFrame().GetPropertyCtrl().document.getElementById("Content");
//            GetMapFrame().GetPropertyCtrl().propCount = 1;
//            content.innerHTML = response; //SAFARI
//            GetMapFrame().GetPropertyCtrl().OnResizeGrid();
//        }
//        
//        var code = '<table id="Grid" cellspacing=0 cellpadding=0 border=0>';
//        code += '<tr class="Info" style="background-color:#e8e8e8">';
//        code += '  <td colspan="3"><span style="font-family:Arial;font-size:8pt;color:gray">&nbsp;Phase:</span>&nbsp;<select id="phaseDDL" name="phaseDDL" style="font-family:Arial;font-size:8pt"><option value="1">Phase 1</option><option value="2">Phase 2</option><option value="3">Phase 3</option></select></td>';
//        code += '</tr>';
//        code += '<tr class="Header">';
//        code += '  <td id="Name" class="Name" width="65"><span>&nbsp;Name</span></td>';
//        code += '  <td class="CellSplitter" width="1" onmousedown="StartResizing()"></td>';
//        code += '  <td class="Value"><span>&nbsp;Value</span></td>';
//        code += '</tr>';
//        code += '<tr class="GridCell">';
//        code += '  <td><span >&nbsp;Test</span></td>';
//        code += '  <td></td>';
//        code += '  <td><span>Test value</span></td>';
//        code += '</tr>';
//        code += '</table>';
//        return;
//    }
    //END TEST...
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    // Create a new XMLHttpRequest object...
    var xmlHttp = GetXMLHttpRequest();

    // Define the response handler...
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            var xmlDoc = xmlHttp.responseXML.documentElement;
            var properties = new Array();
            try {
                var props = xmlDoc.getElementsByTagName("Property");
                if (props != null) {
                    for (var i = 0; i < props.length; i++) {
                        var name = props[i].getAttribute("name");
                        var value = props[i].getAttribute("value");

                        properties.push(new Property(name, value));
                    }
                }
            }
            catch (e) {
            }

            GetMapFrame().GetPropertyCtrl().SetProperties(1, properties);
        }
    }

    var sessionId = _SESSION_ID;
    var mapDefinition = _MAP_DEFINITION;

    // Make the request...
    xmlHttp.open("GET", "../MapGuide/AjaxApi.aspx?sessionId=" + sessionId + "&mapDefinition=" + mapDefinition + "&layerId=" + layerId + "&featureKey=" + featureKey + "&command=FETCH_PROPERTIES", true);
    xmlHttp.send(null);
    
}

function DisplayCustomTransformerDetailProperties(transformer_location_id, transformer_id) {
    // Create a new XMLHttpRequest object...
    var xmlHttp = GetXMLHttpRequest();

    // Define the response handler...
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            var xmlDoc = xmlHttp.responseXML.documentElement;
            var properties = new Array();
            try {
                var props = xmlDoc.getElementsByTagName("Property");
                if (props != null) {
                    for (var i = 0; i < props.length; i++) {
                        var name = props[i].getAttribute("name");
                        var value = props[i].getAttribute("value");

                        properties.push(new Property(name, value));
                    }
                }
            }
            catch (e) {
            }

            GetMapFrame().GetPropertyCtrl().SetProperties(1, properties);
        }
    }

    var sessionId = _SESSION_ID;
    var mapDefinition = _MAP_DEFINITION;

    // Make the request...
    xmlHttp.open("GET", "../MapGuide/AjaxApi.aspx?sessionId=" + sessionId + "&mapDefinition=" + mapDefinition + "&transformerLocationId=" + transformer_location_id + "&selectedTransformerId=" + transformer_id + "&command=FETCH_TRANSFORMER_PROPERTIES", true);
    xmlHttp.send(null);
}
    

