/*////////////////////////////////////////////////////////////////////////////////////////////
// Function Index :
//-------------------------------------------------------------------------------------------

	( array ) 	__GetMenuID();
	( array )	__GetNetworkTab();
	( array ) 	__MapString();	
  --------------------------------------------------------------------------------------------
	
	( void )  	ShowPage ( oMenuItem );	
	( string )	GetShowTitle( stringArray, idMenu, idItem );
	( void ) 	LoadHelp ( name );
  --------------------------------------------------------------------------------------------
	(void)		_GetHelp();
	(oFrame) 	_GetTabContent( oContent );
	(oFrame)	_GetContent();
	(oFrameSet)	_GetContentSet();
  --------------------------------------------------------------------------------------------

	(void)  	LoadAXEFXML ( sFileName );
	(oResultTable)	ApplyXMLtoDOM( xmlDoc, oFragment, oWorkSheet );	
  --------------------------------------------------------------------------------------------
	
	(void)  	GenerateDataForm( bShow );		// xml form
	(xmlDoc)	ExtraXML( oWorkSheet );			// extrace xml from DOM.
	(xmlContent)	PasteXML( xmlDoc );			// past xml to form.
	(void)		SendXml();				// send xml to backend from form.
	(void) 		__ApplyPage( oButton );
*	(void)			__defApplyPage( oButton );	//private default ApplyPage()
#       (void)		ApplyPage( oButton );			//overwrite ApplyPage() by each content page;
  --------------------------------------------------------------------------------------------
	
	(void)		__ReloadPage ( oButton );
*	(void)  	   	__defReloadPage( oButton );	//private default ReloadPage()
#	(void)		ReloadPage( oButton );			//overwrite ReloadPage() by each content page;
//--------------------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////*/


//////////////////////////////////////////////////////////////////////////////////////////////
//	Generating Menu


/* --------------------------------------------------------------------------------------
 	generate id to Menu id (html path/file)
  ---------------------------------------------------------------------------------------  
# Prototype:	( arrString ) __GetMenuID ()
# Parameters:	
# Return:	string array
# Remarks:
----------------------------------------------------------------------------------------- */
function __GetMenuID()
{
	var MainMenuIDArray = new Array(
		new Array( "system", "system/summary", "system/bm", "system/diagnostics", "system/dhcp" ,"system/datetime", "system/administration" ),
		new Array( "network", "network/wansetting", "network/lansetting", "network/dhcpsetting", "network/hostname", "network/service" , "network/ipgrouping", "network/servicegrouping"),
		new Array( "service", "service/firewall", "service/autorouting","service/tunnelrouting", "service/virtualserver", "service/bandwidth", "service/multihoming", "service/internaldns", "service/snmp", "service/upnp" , "service/maxconnection" , "service/maxratecontrol"),
		new Array( "log", "log/view","log/control","log/notification" )
	);
	return ( MainMenuIDArray );
};

/* --------------------------------------------------------------------------------------
 	generate network tab id ( html path/file )
  ---------------------------------------------------------------------------------------  
# Prototype:	( arrString ) __GetNetworkTab ()
# Parameters:	
# Return:	string array
# Remarks:
----------------------------------------------------------------------------------------- */
/*function __GetNetworkTab()
{
	var NetworkTabArray = new Array("wansetting", "lansetting", "dhcpsetting", "hostname", "service" );
	return ( NetworkTabArray );
};
*/

/* --------------------------------------------------------------------------------------
 	generate id to string mapping array
  ---------------------------------------------------------------------------------------  
# Prototype:	( arrString ) __MapString ()
# Parameters:	
# Return:	string array
# Remarks:
----------------------------------------------------------------------------------------- */
function __MapString()
{	
	var stringArray = {
		"system" 		: "System",
		"system/summary" 	: "Summary",
		"system/bm" 	: "Traffic Statistics",	
		"system/diagnostics" 	: "Diagnostic Tools",
		"system/dhcp"	: "DHCP Lease Info",
		"system/datetime" 	: "Date & Time",
		"system/administration" 	: "Administration",
		"network" 			: "Network",
		"network/wansetting" 		: "WAN Setting",
		"network/lansetting" 		: "LAN Setting",
		"network/dhcpsetting" 		: "DHCP Setting",
		"network/hostname" 		: "Host Names",
		"network/service" 		: "Service Names",
		"network/ipgrouping" 	: "IP Grouping",
		"network/servicegrouping": "Service Grouping",
		"service"			: "Service",
		"service/firewall"		: "Firewall",
		"service/autorouting"		: "Auto Routing",
		"service/tunnelrouting"		: "Tunnel Routing",
		"service/virtualserver"		: "Virtual Server",
		"service/bandwidth"		: "QoS",
		"service/multihoming"		: "Multihoming",
		"service/internaldns"		: "Internal DNS",
		"service/snmp"			: "SNMP",
		"service/upnp"			: "UPnP",
		"service/maxconnection" : "Per IP Max Connection",
		"service/maxratecontrol"    :"Per IP Max Rate Control",
		"log"				: "Log",
		"log/view"			: "View",
		"log/control"			: "Control",
		"log/notification"		: "Notification"};
		
	return (stringArray);
};

var IsDirty=null;

/* --------------------------------------------------------------------------------------
 	Show Content Page
  ---------------------------------------------------------------------------------------  
# Prototype:	( void ) ShowPage ( oMenuItem )
# Parameters:	oMenuItem : The Object Triggers This Function	
# Return:	void
# Remarks:	Show the Content Page of clicked menu index
----------------------------------------------------------------------------------------- */
function ShowPage( oMenuItem )
{
	var oContentFrame = _GetContent();
	var user=GetCookie("User");
	if(user=="Administrator")
	{
		if( oContentFrame.contentWindow.IsDirty != null )
		{	if( oContentFrame.contentWindow.IsDirty() == true )
				return;
		}
	}
	
	sRequestPage = oMenuItem.title.toLowerCase() + ".html";

	/* "ContentFrame" is the id of the <iframe> tag in the index.html */
	
	oContentFrame.src = sRequestPage;
	
	//var oPageName = document.getElementById( "PageName" );	
	//oPageName.innerHTML="&nbsp;&nbsp;" + oMenuItem.name;

	// get help
	LoadHelp( oMenuItem.title.toLowerCase() );
};


///////////////////////////////////////////////////////////////////////////////////////
// ori xml; It is used for Dirty checking.
var xmlOriDoc;

function __IsDirty( strXml, strModelTable )
{	
	var xmlDoc = ExtraXML( strModelTable );	
	var xmlOriDoc = xmlDoc.xml;
	if( strXml != xmlDoc.xml )
	{	
		var bRet = confirm( gChangeWarning );
		if( bRet == true )
			return false;
		else
			return true;
	}
	else
		return false;
}
/* --------------------------------------------------------------------------------------
 	transfer menu -> display label
  ---------------------------------------------------------------------------------------  
# Prototype:	( string ) GetShowTitle ( name )
# Parameters:	stringArray : id map array
		idMenu: menu id
		idItem: menuitem id
# Return:	label string
# Remarks:	transfer to label string
----------------------------------------------------------------------------------------- */
function GetShowTitle( stringArray, idMenu, idItem )
{
	var str;
	str =  stringArray[ idMenu ]+ "/" + stringArray[ idItem ];
	return str;
};

/* --------------------------------------------------------------------------------------
 	Load Help html
  ---------------------------------------------------------------------------------------  
# Prototype:	( void ) LoadHelp ( name )
# Parameters:	name : help file name	
# Return:	void
# Remarks:	load help html to HelpFrame
----------------------------------------------------------------------------------------- */
function LoadHelp( name )
{	
	//if( name != "system/network" )
	//{
		aHelpPage="../Small Link On-line Help/en/help/"+ name +".html";
		var oHelpFrame = _GetHelp();
		oHelpFrame.src = aHelpPage;
	//}
}


//////////////////////////////////////////////////////////////////////////////////////////////
//	Load Xml
  
/* --------------------------------------------------------------------------------------
 	default Load AXEF XML File	 
  ---------------------------------------------------------------------------------------
# Prototype:	( void ) LoadAXEFXML ( sFileName )
# Parameters:	sFileName : Represents The Path to XML Conf File to Be Loaded
# Return:	void
# Remarks:	Load XML to Template Table
----------------------------------------------------------------------------------------- */
	
function LoadAXEFXML( sFileName )
{
	var oXML = new ActiveXObject("Msxml2.DOMDocument");
	oXML.async = false;
	oXML.resolveExternals = false;
	oXML.load( sFileName );
				
	ApplyXMLtoDOM(  oXML, oTemplateDiv, oWorkSheetDiv );
};

/* --------------------------------------------------------------------------------------
 	Apply XMLDOM to HTMLDOM
  ---------------------------------------------------------------------------------------
# Prototype:	( oDOM ) ApplyXMLtoDOM ( xmlDoc, oFragment, oWorkSheet )
# Parameters:	xmlDoc     : XMLDOMDocument
		oFragment  : object Fragment
		oWorkSheet : object WorkSheet
# Return:	oDOM
# Remarks:	Apply XML to Template Table
----------------------------------------------------------------------------------------- */

function ApplyXMLtoDOM( xmlDoc, oFragment, oWorkSheet )
{
	var oResultTable = _core_ExpandTemplate( xmlDoc.documentElement , oFragment );
	oResultTable.style.display = "block";
	
	_interop_removeChildren( oWorkSheet );
	oWorkSheet.appendChild( oResultTable );
	restoreCheckbox();
	
	return oResultTable;
};


//////////////////////////////////////////////////////////////////////////////////////////////
//	Load Frame
  
/* --------------------------------------------------------------------------------------
 	Get Help frame
  ---------------------------------------------------------------------------------------  
# Prototype:	( oDOM ) _GetHelp()
# Parameters:	
# Return:	HelpFrame;
# Remarks:
----------------------------------------------------------------------------------------- */	

function _GetHelp()
{
	var oContentFrame = document.getElementById( "ContentFrame" );		
	var oContentSet = oContentFrame.contentWindow.document.getElementById( "oContentSet" );	
	var oHelp = oContentSet.all("Help");
	
	return oHelp;
}

/* --------------------------------------------------------------------------------------
 	Get Network Content frame
  ---------------------------------------------------------------------------------------  
# Prototype:	( oDOM ) _GetTabContent( oContent )
# Parameters:	oContent : parent frame
# Return:	Network_ContentFrame
# Remarks:	Network_ContentFrame
----------------------------------------------------------------------------------------- */
/*
function _GetTabContent( oContent )
{
	var oWindow = oContent.contentWindow;

	if( oWindow != null )
	{	var oFrame = oWindow.document.getElementById( "network_content" );
		if( oFrame )
		{
			return oFrame;
		}
	}
	return oContent;
}
*/

/* --------------------------------------------------------------------------------------
 	Get Content frame
  ---------------------------------------------------------------------------------------  
# Prototype:	( oDOM ) _GetContent( oButton )
# Parameters:	oButton : An Object that Triggers this Function
# Return:	ContentFrame object.
# Remarks:	ContentFrame
----------------------------------------------------------------------------------------- */
function _GetContent()
{
	var oContentFrame = document.getElementById( "ContentFrame" );		
	var oContentSet = oContentFrame.contentWindow.document.getElementById( "oContentSet" );	
	var oContent = oContentSet.all("Content");

	return oContent;
};

/* --------------------------------------------------------------------------------------
 	Get Content frame set
  ---------------------------------------------------------------------------------------  
# Prototype:	( oDOM ) _GetContentSet()
# Parameters:
# Return:	ContentFrameSet object;
# Remarks: 	ContentFrameSet which contain ContentFrame & HelpFrame
----------------------------------------------------------------------------------------- */	
function _GetContentSet()
{
	var oContentFrame = document.getElementById( "ContentFrame" );		
	var oContentSet = oContentFrame.contentWindow.document.getElementById( "oContentSet" );		

	return oContentSet;
}


//////////////////////////////////////////////////////////////////////////////////////////////
//	Send Xml

/* --------------------------------------------------------------------------------------
 	Generating XML Data Form
  ---------------------------------------------------------------------------------------  
# Prototype:	( null ) GenerateDataForm ( bShow )
# Parameters:	bShow : true: debug | false;
# Return:	null
# Remarks:
----------------------------------------------------------------------------------------- */
function GenerateDataForm( bShow )
{
	var display = (bShow == true )?"block":"none";
	
	document.write( 
		"<form id='xmlForm' action='http://[PATH_TO_SCRIPT]/savexml.php' method='post' style='display: "+display+"'>\n" +
		"<textarea id='xmlData' name='xmlData' rows='15' cols='60'></textarea>\n" +
		"</form><br />"
	);
};

/* --------------------------------------------------------------------------------------
 	Extra XML data from DOM
  ---------------------------------------------------------------------------------------  
# Prototype:	( xmlContent ) ExtraXML ( WorkSheetID )
# Parameters:	WorkSheetID : WorkSheet ID
# Return:	xmlContent 	
# Remarks:	Retrieve xml data string from Table
----------------------------------------------------------------------------------------- */
function ExtraXML( WorkSheetID )
{	
	var obj = _d(  document, WorkSheetID );
	
	if( obj )
	{	var oTableData = _core_ExtractXML(obj);
		return oTableData.ownerDocument;
	}
	else
		return null;
}

/* --------------------------------------------------------------------------------------
 	Paste XML Content to Hidden Form
  ---------------------------------------------------------------------------------------  
# Prototype:	( xmlContent ) RetrieveXML ( oXML )
# Parameters:	oXML : XmlDocument Root
# Return:	xmlContent 	
# Remarks:	Retrieve data from Table & Append the XML string to Textarea of Hidden Form 	
----------------------------------------------------------------------------------------- */
function PasteXML( xmlDoc )
{	
	var xmlContent = xmlDoc.documentElement.xml;
	
	var oHiddenFormTextArea = document.getElementById("xmlData");
	var oNewNode = document.createTextNode( xmlContent );

	if( oHiddenFormTextArea.hasChildNodes() == false )
		oHiddenFormTextArea.appendChild( oNewNode );
	else
		oHiddenFormTextArea.replaceChild( oNewNode, oHiddenFormTextArea.firstChild );
		
	return xmlContent;
};

/* --------------------------------------------------------------------------------------
 	Send Xml data from Hidden Form to backend 
  ---------------------------------------------------------------------------------------  
# Prototype:	( xmlContent ) RetrieveXML ( oXML )
# Parameters:	oXML : XmlDocument Root
# Return:	xmlContent 	
# Remarks:	Retrieve data from Table & Append the XML string to Textarea of Hidden Form 	
----------------------------------------------------------------------------------------- */
function SendXml()
{
	var oForm = document.getElementById("xmlForm");
	oForm.submit();
}

/* --------------------------------------------------------------------------------------
 	default ApplyPage ( extra Xml & send to backend )
  ---------------------------------------------------------------------------------------  
# Prototype:	( null ) __defApplyPage( oButton )
# Parameters:	oButton : An Object that Triggers this Function
# Return:	NULL
# Remarks:	Send XML Data to Backend
----------------------------------------------------------------------------------------- */
function __defApplyPage( oButton )
{
	// Retrieve XML Data From Table to Hidden Form
	var oContentFrame = window.parent.document.getElementById("Content");
	var oContentWindow = oContentFrame.contentWindow;
		
	var xmlDoc = oContentWindow.ExtraXML("oModelTable");
	var xmlContent = oContentWindow.PasteXML( xmlDoc );	
	
	oButton.blur();
}

function _cntApplyPage( oButton )
{
	var xmlDoc = ExtraXML("oModelTable");	
	PasteXML(xmlDoc);
	
	oButton.blur();
	
	return xmlDoc;
}

/* --------------------------------------------------------------------------------------
 	ApplyPage
  ---------------------------------------------------------------------------------------  
# Prototype:	( null ) __ApplyPage( oButton )
# Parameters:	oButton : An Object that Triggers this Function
# Return:	NULL
# Remarks:	Send XML Data to Backend
		each conten page can overwrite this function by ApplyPage();
----------------------------------------------------------------------------------------- */

var ApplyPage = null;

function __ApplyPage( oButton )
{	
	var oContent = window.parent.document.getElementById("Content");	
	var oFrameWindow = oContent.contentWindow;
	
	if(oFrameWindow.ApplyPage != null )
	{		
		oFrameWindow.ApplyPage( oButton );
	}
	else
	{	
		__defApplyPage ( oButton );
	}
};


//////////////////////////////////////////////////////////////////////////////////////////////
//	Reload page

/* --------------------------------------------------------------------------------------
 	 default Reload Content Page Function
  ---------------------------------------------------------------------------------------
# Prototype :	( null ) __defReloadPage (  oButton  )
# Parameters:	oButton : Which Triggers This Function
# Return:	NULL
# Remarks:	Refresh the Content Frame
----------------------------------------------------------------------------------------- */
function __defReloadPage( oButton )
{
	var oCurrentPage = window.parent.document.getElementById("Content");	
	var oldSrc = oCurrentPage.src;
	oCurrentPage.src = '';
	oCurrentPage.src = oldSrc;
	oButton.blur();
};

/* --------------------------------------------------------------------------------------
 	ReloadPage : call by ReloadPage button 
   ---------------------------------------------------------------------------------------  
# Prototype:	( null ) __ReloadPage( oButton )
# Parameters:	oButton : An Object that Triggers this Function
# Return:	NULL
# Remarks:	Reload content page
		each conten page can overwrite this function by ReloadPage();
----------------------------------------------------------------------------------------- */
var ReloadPage = null;

function __ReloadPage( oButton )
{
	var oContent = window.parent.document.getElementById("Content");	
	var oFrameWindow = oContent.contentWindow;
	
	if( oFrameWindow.ReloadPage != null )
	{
		oFrameWindow.ReloadPage( oButton );
	}
	else
	{
	__defReloadPage( oButton );
	}
};


/* --------------------------------------------------------------------------------------
 	ReloadPage : call by ReloadPage button 
   ---------------------------------------------------------------------------------------  
# Prototype:	( null ) __ReloadPage( oButton )
# Parameters:	oButton : An Object that Triggers this Function
# Return:	NULL
# Remarks:	Reload content page
		each conten page can overwrite this function by ReloadPage();
----------------------------------------------------------------------------------------- */

var gCurrentSelectValue="";

function gGetSelectValue(obj)
{	
	gCurrentSelectValue = obj.options[ obj.selectedIndex ].value;	
}


/* --------------------------------------------------------------------------------------
 	SubmitXML2 : 
   ---------------------------------------------------------------------------------------  
# Prototype:	( null ) SubmitXML2 action, oXML, Local_Test )
# Parameters:	oButton : An Object that Triggers this Function
# Return:	NULL
# Remarks:	Reload content page
		each conten page can overwrite this function by ReloadPage();
----------------------------------------------------------------------------------------- */
function SubmitXML2( action, xmlDoc, sid , Local_Run )
{
	if( !Local_Run )
		Local_Run = false;
	
	if( Local_Run )
	{
		PasteXML( xmlDoc );
	}
	else
	{
		var result=SubmitXML( "/cgi-bin/savexml.cgi?action="+action+"&sid="+sid+"&CGIDEBUG=ixpui.log", xmlDoc );
		return result;
	}
};

function GetCookie (name)
{
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen)
        {
                var j = i + alen;
                if (document.cookie.substring(i, j) == arg)
                        return getCookieVal (j);
                i = document.cookie.indexOf(" ", i) + 1;
                if (i == 0)
                        break;
        }
        return 0;
}
function getCookieVal (offset)
{
        var endstr = document.cookie.indexOf (";", offset);
        if (endstr == -1)
                endstr = document.cookie.length;

        return unescape(document.cookie.substring(offset, endstr));
}

