/*/////////////////////////////////////////////////////////////////////////////
	javascript xml utility
	support browser: ie

	author: max huang
	
date: 2005/03/23
===============================================================================
	xmlDoc	xml_CreateDocument();
	xmlDoc	xml_GetXML(cResource)
	oNode	xml_GetXMLRoot(xmlDoc);
	oNode 	xml_GetChildNodeN( oNode, strName, layer );
	arrNode xml_GetChildNodeArray( oNode, strName );
	oNode 	xml_GetSiblingTagNode( oNode, type );
	arrNode xml_GetSiblingTagNodeArray( oNode, type );
	arrNode xml_GetSiblingNodeArray( oNode, type );
	void	xml_SetNodeText( oNode, cValue );
	string 	xml_GetNodeValue( oNode );
	string	xml_GetNodeValueByTagName( oNode, tagName );
	oNode 	xml_CreateElementNS( odXML, cTagName, cNS );
	oNode	xml_CreateTextNode( oXML, value );
	oNode	xml_CreateLeafNode( oXML, tagName, value );
	string 	xml_GetLocalName(odXML);
	string 	xml_SerializeXMLElement( oXML );
	void 	xml_removeChildren( oTarget );
	void	xml_removeNode( oTarget );
/////////////////////////////////////////////////////////////////////////////*/

/* ========================================================
nodeType:
NODE_ELEMENT (1) --------------- Element Node
NODE_ATTRIBUTE (2)
NODE_TEXT (3) ------------------ Text Node
NODE_CDATA_SECTION (4)
NODE_ENTITY_REFERENCE (5)
NODE_ENTITY (6)
NODE_PROCESSING_INSTRUCTION (7)
NODE_COMMENT (8)
NODE_DOCUMENT (9)
NODE_DOCUMENT_TYPE (10)
NODE_DOCUMENT_FRAGMENT (11)
NODE_NOTATION (12)
===========================================================*/
var is_ie = ((__agt.indexOf("msie") != -1) && (__agt.indexOf("opera") == -1));

/* ========================================================
create XML Object
	xmlDoc	xml_CreateDocument();
===========================================================*/
function xml_CreateDocument()
{
	var xmlDoc;
	if (is_ie)
	{
		xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
		xmlDoc.async = false;
   		xmlDoc.setProperty("SelectionLanguage", "XPath");
		xmlDoc.setProperty("SelectionNamespaces", "xmlns:j='urn::JPGSystem' xmlns:a='urn::DXAF-architecture'" );
	}
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", null, null );
	}
	
	return xmlDoc;
};

/* ========================================================
Get XML Engine
	xmlDoc	xml_GetXML(cResource)
	param : cResource: URL path
	return: xml engine
===========================================================*/
xml_Local_Run = false;

function xml_GetXML( cResourceURL, param, cNSDeclaration )
{
	if( !xml_Local_Run )
		xml_Local_Run = false;
	
	if( xml_Local_Run )
	{
		return xml_GetLocalXML(cResourceURL);
	}
	else
	{
		cResourceURL = cResourceURL+param;
		return xml_GetXMLfromServer( cResourceURL, cNSDeclaration );
	}	
};

function xml_GetLocalXML(cResource)
{
	var xmlDoc = xml_CreateDocument();
	xmlDoc.load(cResource);

	return xmlDoc;
};

function xml_GetXMLfromServer( cResourceURL, cNSDeclaration )
{
	var oXML, oXMLHttp;
	
	cResourceURL = cResourceURL + "&timeFlag" + (new Date()).getTime();	
	
	if( ! cNSDeclaration )
	{
		cNSDelaration = "xmlns:a='urn::DXAF-architecture'"
	}
	
	if( cResourceURL )
	{
		try
		{
			oXMLHttp = new XMLHttpRequest();
		}
		catch (e)
		{
		  	oXMLHttp = new ActiveXObject("Msxml2.XMLHttp");
		}
		try {
			oXMLHttp.open("GET", cResourceURL ,false);
			oXMLHttp.send(null);
		} catch (e) {
		  	return("<error>Network Down!</error>");
		}
		oXML = oXMLHttp.responseXML;

		if( is_ie )
		{
			oXML.setProperty("SelectionLanguage", "XPath");
			oXML.setProperty("SelectionNamespaces", cNSDeclaration );
		}
	}

	return( oXML );
};


/* ========================================================
Get XML Root
	XMLDOM	xml_GetXMLRoot(xmlDoc )
	param : xmlDoc: xml Doc
	return: root node
===========================================================*/

function xml_GetXMLRoot(xmlDoc)
{	return xmlDoc.documentElement;
}

/* ========================================================
Get child node by TagName
	param : oNode: xml node object
		strName : node name string
		layer : search child layer(default is 5 )
	return: finded node
===========================================================*/
function xml_GetChildNodeN( oNode, strName, layer )
{
	layer ? layer : layer = 5;
	if(oNode.nodeType == 9 )
		oNode = oNode.documentElement;
		
	if( oNode.nodeType != 1 || layer < 1 )
		return null;
	
	layer--;
	var oResultNode = null;
	
	if( oNode.nodeName == strName )
		return oNode;
	else
	{
		for (var i=0; i<oNode.childNodes.length; i++ )
		{	
			var curNode = oNode.childNodes.item(i);
			
			if( curNode.nodeType == 1 )
			{
			  	oResultNode = xml_GetChildNodeN(curNode, strName, layer);
			  	if( oResultNode != null )
			  		break;
		  	}
		}
	}
  	
  	return oResultNode;
}


/* ========================================================
Get child node array
	param : oNode: xml node object
		
	return: node array
===========================================================*/
function xml_GetChildNodeArray( oNode, strName )
{
	if (is_ie)
	{
		if( oNode.hasChildren == false )
			return null;
			
		var arrNode = new Array();
		var NodeList = oNode.getElementsByTagName(strName);
		
		for( var i=0; i<NodeList.length; i++)	
		{	
			var item = NodeList.item(i);
			arrNode = arrNode.concat( item );
		}
	
		return arrNode;
	}
	else
		return null;
};

/* ========================================================
Get same tag slibing node
	param : oNode: xml node object
		type : "previous", "next"
	return: node array
===========================================================*/
function xml_GetSiblingTagNode( oNode, type )
{
	var strName = oNode.nodeName;
	var curNode = null;
	
	while(1)
	{	
		curNode = (type == "previous")? oNode.previousSibling: oNode.nextSibling;
		
		if( curNode == null || curNode.nodeName == strName )
		{	break;
		}
	}
	
	return curNode;
};

/* ========================================================
Get same tag slibing node array
	param : oNode: xml node object
		type : "previous", "next"
	return: node array
===========================================================*/
function xml_GetSiblingTagNodeArray( oNode, type )
{
	var strName = oNode.nodeName;
	var curNode = oNode;
	
	var arrNode = new Array();

	do
	{	curNode = (type == "previous")? curNode.previousSibling: curNode.nextSibling;
		if( curNode != null && curNode.nodeName == strName )
		{	arrNode = arrNode.concat(curNode);
		}
	}
	while(curNode != null)
	
	return arrNode;
};

/* ========================================================
Get slibing node array
	param : oNode: xml node object
		type : "previous", "next"
	return: node array
===========================================================*/
function xml_GetSiblingNodeArray( oNode, type )
{
	var curNode = oNode;
	
	var arrNode = new Array();

	do
	{	curNode = (type == "previous")? curNode.previousSibling: curNode.nextSibling;
		if( curNode != null )
		{	arrNode = arrNode.concat(curNode);
		}
	}
	while(curNode != null)
	
	return arrNode;
};

/* ========================================================
Set node text
	param : oNode: xml node object
		cValue  : nodeText string
===========================================================*/
function xml_SetNodeText( oNode, cValue )
{
	if( oNode.nodeType == 3 )
	{	oNode.value = cValue;
		return( true );
	}
	else if (oNode.nodeType == 1)
	{
		var oChildList = oNode.childNodes;
		
		for( var i = 0; i < oChildList.length ; i++ )
		{	oNode.removeChild(oChildList[i]);
		}
		
		oNode.appendChild( document.createTextNode( cValue ) ) ;
	}
}

/* ========================================================
Get node text
	param : oNode: xml node object
		cValue  : nodeText string
		return nodevalue
===========================================================*/
function xml_GetNodeValue( oNode )
{
	var oChildList = oNode.childNodes;
	var retVal = "";
	
	for( var i = 0; i < oChildList.length; i++ )
	{	var oCurrentChild = oChildList[i];
		retVal += xml_GetNodeValue( oCurrentChild );		
	}
	
	if( oNode.nodeType == 3 )
	{	return( trim( oNode.nodeValue ) );
	}
	else
	{	return( retVal );
	}
}

function xml_GetNodeValueByTagName( oNode, tagName )
{
	var oTarget = xml_GetChildNodeN( oNode, tagName );
	if( oTarget != null )
		return xml_GetNodeValue( oTarget );
	else
		return "";
}

var __WithNameSpace = false;
/* ========================================================
Get node text
	param : oNode: xml node object
		cValue  : nodeText string
		return nodevalue
===========================================================*/
function xml_CreateElementNS( odXML, cTagName, cNS )
{
	var oResult;

	if (is_ie)
	{
		if(__WithNameSpace)
		{
			oResult = odXML.createNode( 1, "o:" + cTagName, cNS );
		}
		else
		{	oResult = odXML.createNode( 1, cTagName, "");	
		}
		
		return( oResult );
		
	}
	else if (document.implementation && document.implementation.createDocument) 
	{
		if(__WithNameSpace)
		{
			oResult = odXML.createElementNS( cNS, "o:" + cTagName );
		}
		else
		{
			oResult = odXML.createElementNS( cNS, cTagName );
		}
		return( oResult );
	}
}

/* ========================================================
Get node text
	param : oXML: xml node object
		value  : nodeText string
		return oNode
===========================================================*/
function xml_CreateTextNode( oXML, value )
{
	var oNode = oXML.createNode(3,"","");
	oNode.nodeValue = value;

	return oNode;
}


/* ========================================================
Get node text
	param : oXML: xml node object
		tagName : tag name
		value  : nodeText string
		return node (<>xxx</>)
===========================================================*/
function xml_CreateLeafNode( oXML, tagName, value )
{
	var oNode = xml_CreateElementNS(oXML,tagName, "" );
	
	if(value != null )
	{	var oTextNode = xml_CreateTextNode( oXML, value );
		oNode.appendChild( oTextNode );
	}
	
	return oNode;
}

function xml_GetLocalName(odXML)
{
	if(odXML.nodeType != 1)
	{
		return "";
	}
	
	if (is_ie)
	{
		if(odXML.baseName)
		{	return odXML.baseName;
		}
		else
		{	return odXML.tagName;
		}
	} 
	else
	{	return odXML.localName;
	}
}

function _xml_SerializeXMLElement( oXML )
{
	if ( !is_ie )
	{
		return( (new XMLSerializer()).serializeToString( oXML )) ;
	}
	else
	{
		return( oXML.xml );
	}
}

function xml_removeChildren( oTarget )
{
	if(oTarget != null)
	{	var oChildList = oTarget.childNodes;
		for( var i = oChildList.length; i > 0; )
		{
			oTarget.removeChild(oChildList[--i]);
		}
	}
}


function xml_removeNode( oTarget )
{
	if( oTarget && oTarget.parentNode)
		oTarget.parentNode.removeChild(oTarget);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
var oXMLHttp;
var ostep4,ostep2;
function initHttp()
{
	try
	{
		oXMLHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		oXMLHttp = new ActiveXObject("Msxml2.XMLHttp");
	}
}
var doc;
var oaction;
function SubmitXML3( action, xmlDoc )
{
		doc=xmlDoc;
		oaction=action;
		CheckSession(ApplyPage2);
}
function ApplyPage4()  
{
	if( oXMLHttp.readyState != 4 ) return; 
	window.parent.parent.DisableMessage();
	var temp=oXMLHttp.responseXML.xml;
        var arr1=temp.split("[");
        var temp1=arr1[2].valueOf();
        var arr2=temp1.split("]");
	alert(arr2[0].valueOf());
	window.parent.parent.EnableApply();
	if(oaction=="DATE_TIME")
		initial();
}

function ApplyPage3()  
{
	if( oXMLHttp.readyState == 1 ) 
	{
		var cMessage = _interop_SerializeXMLElement( doc );
		oXMLHttp.onreadystatechange = ApplyPage4;
		oXMLHttp.send( cMessage );
	}
}
function ApplyPage2()
{
	if( oXMLHttp.readyState != 4 ) return; 
	if(CheckSession3())
	{
		initHttp();
		xml_PostXML_async( oXMLHttp, ApplyPage3, "/cgi-bin/savexml.cgi?action="+oaction+"&sid="+gsid+"&CGIDEBUG=ixpui.log");
	}
}
////////////////////////////////////////////////////////////////////////////////////////
var url;
function xml_GetXML3( cResourceURL, param,step2,step4 )
{
	if( !step4 )
		ostep4 = initial_4;
	else
		ostep4=step4;
		
	url=cResourceURL;
	if( !step2 )
		CheckSession(initial_2);
	else
		CheckSession(step2);
}

function initial_3()  
{
	if( oXMLHttp.readyState == 1 ) 
	{
		oXMLHttp.onreadystatechange = ostep4;
		oXMLHttp.send();
	}
}

function initial_2()  
{
	if( oXMLHttp.readyState != 4 ) return;
	if(CheckSession3())
	{
		initHttp();
		xml_GetXML_async( oXMLHttp, initial_3,url, "&sid="+gsid+"&CGIDEBUG=ixpui.log" );
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
function CheckSession3()
{
	if(_interop_GetLocalName(oXMLHttp.responseXML.documentElement) == "error")
	{
		var response = _interop_getTextValue(oXMLHttp.responseXML.documentElement);
		alert(response);
		QuitConfirmed = true;
		window.top.location.assign("/index.html");
		return false;
	}
	return true;
}
function CheckSession1() 
{
	if( oXMLHttp.readyState == 1 )
	{
		oXMLHttp.onreadystatechange = ostep2;
		oXMLHttp.send();
	}
}

function CheckSession(step2) 
{
	ostep2=step2;
	initHttp();
	xml_GetXML_async( oXMLHttp, CheckSession1, "/cgi-bin/pmcmd.cgi?action=REFRESH", "&sid="+gsid+"&CGIDEBUG=ixpui.log");
}
/* ========================================================
Get XML Engine Asynchronously
	xmlDoc	xml_GetXML_async( oXMLHttp, fChange, cResourceURL, param)
	param : cResource: URL path
	return: xml engine
===========================================================*/

function xml_GetXML_async( oXMLHttp, fChange, cResourceURL, param )
{
	cResourceURL = cResourceURL + param + "&timeFlag" + (new Date()).getTime();
	oXMLHttp.onreadystatechange = fChange;
	oXMLHttp.open( "GET", cResourceURL ,true );
}
/* ========================================================
Post XML Engine Asynchronously
	xmlDoc	xml_PostXML_async( oXMLHttp, fChange, cResourceURL )
    cResource: URL path
	return: xml engine
===========================================================*/

function xml_PostXML_async( oXMLHttp, fChange, cResourceURL )
{
	oXMLHttp.onreadystatechange = fChange;
	oXMLHttp.open( "POST", cResourceURL ,true );
}