
/*
 * Copyright 2005-2007 WSO2, Inc. (http://wso2.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var cachedMsgFlowsXML;

function axisConfig(thisObj,index) {

    if (thisObj && index) {
        renderSelectedMenuItem(thisObj,index);
        gatherHistoryObject(thisObj.id, index);
    }

    var body_xml = '<req:getAxisConfigDataRequest xmlns:req="http://org.apache.axis2/xsd">\n' +
                   ' </req:getAxisConfigDataRequest>\n';

    var callURL = serverURL + "/" + "AxisConfigAdmin" ;

    new wso2.wsf.WSRequest(callURL,"getAxisConfigData",body_xml,function(){
        cachedMsgFlowsXML = this.req.responseXML;
        wso2.wsf.Util.callbackhelper(cachedMsgFlowsXML,"msg_flows_graphic.xsl",document.getElementById("divAxisFlowChain"));
    });
}

function listEngagedOperationHandlers(operationName){
	if (operationName != null){
		operationNameForEngagedOperationHandlers = operationName;
	} else {
		operationName = operationNameForEngagedOperationHandlers;
	}
    var body_xml = '<req:getOperationAxisConfigData xmlns:req="http://org.apache.axis2/xsd">\n' +
                   ' <req:serviceId>' + getServiceSpecificName() + '</req:serviceId>\n' +
                   ' <req:operationId>' + operationName + '</req:operationId>\n' +
                   ' </req:getOperationAxisConfigData>\n';
    var callURL = serverURL + "/" + "AxisConfigAdmin" ;

    new wso2.wsf.WSRequest(callURL,"getOperationAxisConfigData",body_xml,listEngagedOperationHandlersCallback);
}

function listEngagedOperationHandlersCallback(){
    cachedMsgFlowsXML = this.req.responseXML;
    wso2.wsf.Util.callbackhelper(cachedMsgFlowsXML,"msg_flows_graphic_per_service.xsl", document.getElementById("divAxisFlowChainPerService"));

    var _text = "<h4><a href=\"#\" onClick=\"javascript:showServiceInitializer(); return false;\">Services</a>&nbsp;&gt;&nbsp;" +
                                           "<a href=\"#\" onClick=\"javascript:listServiceData('"+ getServiceSpecificName() +"'); return false;\">"+ getServiceSpecificName() + "</a>&nbsp;&gt;&nbsp;" +
                                           '<a onclick="javascript:listAllOperations(\'' + getServiceSpecificName() + '\'); return false;" href="#">Operations[' + operationNameForEngagedOperationHandlers + ']</a>&gt;&nbsp;Flows</h4>' +
    		'<h2>Axis2 Handlers for Operation</h2>';
	document.getElementById('divAxisFlowChainGraphicPerServiceHeader').innerHTML  = _text;
    		
}	

function axisConfigText() {
    var body_xml = '<req:getAxisConfigDataRequest xmlns:req="http://org.apache.axis2/xsd">\n' +
                   ' </req:getAxisConfigDataRequest>\n';

    var callURL = serverURL + "/" + "AxisConfigAdmin" ;

    new wso2.wsf.WSRequest(callURL,"getAxisConfigData",body_xml,function(){
        wso2.wsf.Util.callbackhelper(this.req.responseXML,"msg_flows_text.xsl",document.getElementById("divAxisFlowChainText"));
    });
}

function axisConfigTextPerSerivce() {
    var body_xml = '<req:getOperationAxisConfigData xmlns:req="http://org.apache.axis2/xsd">\n' +
                   ' <req:serviceId>' + getServiceSpecificName() + '</req:serviceId>\n' +
                   ' <req:operationId>' + operationNameForEngagedOperationHandlers + '</req:operationId>\n' +
                   ' </req:getOperationAxisConfigData>\n';
    var callURL = serverURL + "/" + "AxisConfigAdmin" ;

    new wso2.wsf.WSRequest(callURL,"getOperationAxisConfigData",body_xml,axisConfigTextPerSerivceCallback);
}


function axisConfigTextPerSerivceCallback(){

    wso2.wsf.Util.callbackhelper(this.req.responseXML,"msg_flows_text_per_service.xsl", document.getElementById("divAxisFlowChainTextPerService"));

    var _text = "<h4><a href=\"#\" onClick=\"javascript:showServiceInitializer(); return false;\">Services</a>&nbsp;&gt;&nbsp;" +
                                           "<a href=\"#\" onClick=\"javascript:listServiceData('"+ getServiceSpecificName() +"'); return false;\">"+ getServiceSpecificName() + "</a>&nbsp;&gt;&nbsp;" +
                                           '<a onclick="javascript:listAllOperations(\'' + getServiceSpecificName() + '\'); return false;" href="#">Operations[' + operationNameForEngagedOperationHandlers + ']</a></h4>' +
    		'<h2>Axis2 Handlers for Operation</h2>';
	document.getElementById('divAxisFlowChainTextHeaderPerService').innerHTML  = _text;

}

function axisConfigHandlers(flowName, flowNodeName, phaseName) {
	var flowImgName;
	//Since it can only be in or out ;-)
	if (flowName.indexOf("Out") == -1){
		flowImgName = "inflow";
	} else {
		flowImgName = "outflow";	
	}
    var objPhase = getPhaseObject(flowNodeName, phaseName);
	objPhase.setAttribute("imgFlowName", flowImgName);
    wso2.wsf.Util.processXML(objPhase, "msg_flow_handlers.xsl", document.getElementById("divAxisFlowChainHandlers"));

    document.getElementById("divAxisFlowChainHandlersHeader").innerHTML =
    '<h4><a onclick="javascript:axisConfig(); return false;" href="#">Message Flows</a>&nbsp;&gt;&nbsp;' +
    ' Flow [' + flowName + '] - Phase [' + phaseName + ']</h4>' +
    '<h2>Axis2 Handlers for phase : ' + phaseName + ' in ' + flowName + '</h2>';

    wso2.wsf.Util.showOnlyOneMain(document.getElementById("divAxisFlowChainHandlers"));
}

function axisConfigHandlersPerService(flowName, flowNodeName, phaseName) {
	var flowImgName;
	//Since it can only be in or out ;-)
	if (flowName.indexOf("Out") == -1){
		flowImgName = "inflow";
	} else {
		flowImgName = "outflow";	
	}
    var objPhase = getPhaseObject(flowNodeName, phaseName);
	objPhase.setAttribute("imgFlowName", flowImgName);
    wso2.wsf.Util.processXML(objPhase, "msg_flow_handlers_per_service.xsl", document.getElementById("divAxisFlowChainHandlers"));
   
    var _text = "<h4><a href=\"#\" onClick=\"javascript:showServiceInitializer(); return false;\">Services</a>&nbsp;&gt;&nbsp;" +
                                           "<a href=\"#\" onClick=\"javascript:listServiceData('"+ getServiceSpecificName() +"'); return false;\">"+ getServiceSpecificName() + "</a>&nbsp;&gt;&nbsp;"+
            '<a onclick="javascript:listAllOperations(\'' + getServiceSpecificName() +'\'); return false;" href="#">Operations[' + operationNameForEngagedOperationHandlers + ']</a>&nbsp;&gt;&nbsp;' +
			'<a onclick="javascript:listEngagedOperationHandlers(); return false;" href="#">Message Flows</a>&nbsp;&gt;&nbsp;' +
   			' Flow [' + flowName + '] - Phase [' + phaseName + ']</h4>' +
    		'<h2>Axis2 Handlers for Phase : ' + phaseName + ' in ' + flowName + '</h2>';

    document.getElementById("divAxisFlowChainHandlersHeaderPerService").innerHTML = _text;

    wso2.wsf.Util.showOnlyOneMain(document.getElementById("divAxisFlowChainHandlers"));
}

function breakClassName(className) {
    var newClassName = "";
    var count;
    for (count = 0; count < className.length(); count++) {
        if (char(className.charAt(count)) > 64) {
            newClassName = newClassName + " " + className.charAt(count);
        } else {
            newClassName += className.charAt(count);
        }
    }
    return newClassName;
}


function getPhaseObject(flowName, expPhaseName) {
    var flowObject = getElementsByTagNameCrossBrowser(cachedMsgFlowsXML, flowName)[0];
    var xml = getElementsByTagNameCrossBrowser(flowObject, "phases");
    var curPhaseName;
    for (var x = 0; x < xml.length; x++) {
        var phaseChildNodes = xml[x].childNodes;
        for (var y = 0; y < phaseChildNodes.length; y++) {
            if (phaseChildNodes[y].nodeName == "name") {
                curPhaseName = phaseChildNodes[y].firstChild.nodeValue;
                break;
            }

        }
        if (expPhaseName == curPhaseName) {
            return xml[x];
        }
    }
}

function getElementsByTagNameCrossBrowser(xml, tagName) {
    try {
        return xml.getElementsByTagName(tagName);
    } catch(e) {
    }
    try {
        return xml.getElementsByTagName(xml.firstChild.nextSibling.prefix + ":" + tagName);
    } catch(e) {
    }
    alert("getElementsByTagName not supported");
}
