var DebuggerWindow		= null; 
var g_objDebugger		= null;

Logger.info("Debugger", (debugMenu) ? "loaded." : "failed to load.");

var showPortParsedHTML  = _unavailable;
var showPreParsedHTML   = _unavailable;
var showXSD				= _unavailable;
var showData			= _unavailable;
var saveData			= _unavailable;
var showUpdate			= _unavailable;
var showXMLObject		= _unavailable;
var showXML				= _unavailable;

SetDebugger();

function rawDataDump()
{
	// This function produces the rawData page 
	// Basically it takes the data that exists currently and outputs it all to a text based editting
	// page for quick access or 
	
	if(!dataLoaded) return;
	
//	var d= MSXML.DOMDocument(false);
	var c;
//	d.async = false;
	var d = g_objLoader.loadURLToDOM(NC_BASE + "/includes/rawdata.xsl");
//	d.load(NC_BASE + "/includes/rawdata.xsl");
	try
	{ c = g_domXMLData.transformNode(d); }
	catch(e)
	{ c = x.parseError.reason + ':' + x.parseError.line + ':' + x.parseError.pos + ':' + e.description; }

	return c;
}

function SetDebugger()
{
	if(!debugMenu) return;
	g_objDebugger  = (Debugger) ? new Debugger() : null;
	debugMenu = (g_objDebugger!=null);
	if(!debugMenu) return;

	Logger.info("Debugger", (debugMenu) ? "loaded." : "failed to load.");

	showPortParsedHTML  = g_objDebugger.showPortParsedHTML;
	showPreParsedHTML   = g_objDebugger.showPreParsedHTML;
	showXSD				= g_objDebugger.showXSD;
	showData			= g_objDebugger.showData;
	saveData			= g_objDebugger.saveData;
	validateData		= g_objDebugger.validateData;
	showUpdate			= g_objDebugger.showUpdate;
	showXMLObject		= g_objDebugger.showXMLObject;
	showXML				= g_objDebugger.showXML;
}
/**
 * Class for showing Debug output. 
 * Use this class to add a Debug Menu option and show data.
 *
 * @author Paul Reed - NetCall Telecom
 */
function Debugger()
{
	var m_dw=null;
	
	this.showPostParsedHTML = _SAP;
	this.showPreParsedHTML  = _SBP;
	this.showXSD			= _XSD;
	this.showData			= _VWD;
	this.showUpdate			= _VWU;
	this.showXMLObject		= _VWO;
	this.showXML			= _VWX;
	this.saveData			= _SVD;
	this.saveXMLObject		= _SVO;
	this.validateData		= _VAL;
	this.close	= _c;
	
	function _c() { if(m_dw!=null && m_dw.close) m_dw.close(); m_dw=null;	}

	function _VAL()
	{
		var aP= new Array();
		xmlbuf = g_domXMLData.documentElement.xml;
		aP.upData = xmlbuf.replace(/\</g,'\n<');
		var r= g_objLoader.callWebService('validateData',aP);
		r = r.replace(/\n/g,'<BR/>');
		_VWO(r,"Validation");
	}

	function _SAP()	{_VWX(ncDivContent.innerHTML,"Post Parsed HTML");}

	function _SBP()	{_VWX(g_objParser.getTemplateHTML(), "PreParsed HTML");}

	function _XSD()	
	{
			_VWO(g_domXMLMap, "DATA MAP");
	} 

	function _VWD()	{_VWO(g_domXMLData, "DATA XML");}

	function _SVD(f)
	{
		if(f=="") f = "c:\\tmp\\savedata.xml";
		_SVO(g_domXMLData,f);
	}
	function _SVO(x,f)
	{
		var pD = window.createPopup();
		pD.document.write(x.xml.replace(/\</g,'\n<'));
		pD.show(500,500,100,100,window.document.body);
		pD.document.execCommand("SaveAs");
	}

	//Shows the xml update document.
	function _VWU()
	{
		var b;
		var c= g_domXMLUpdate.documentElement.cloneNode(true);
		while((b=c.selectSingleNode("//*[not(descendant-or-self::*[@edit] or ancestor-or-self::*[@edit='add'])]"))!=null)
			b.parentNode.removeChild(b);
		_VWO(c, "UPDATE, XML");
	}

	function _VWO(x, t)
	{
//		var d= MSXML.DOMDocument(false);
		var c;
//		d.async = false;
		var d = g_objLoader.loadURLToDOM(NC_BASE + "/includes/defaultss.xsl");
//		d.load(NC_BASE + "/includes/defaultss.xsl");
		switch(typeof(x))
		{
			case 'object':
				try
				{ c = x.transformNode(d); }
				catch(e)
				{ c = NC_BASE + "/includes/defaultss.xsl" + ":" + d.parseError.reason + ':' + d.parseError.line + ':' + d.parseError.pos + ':' + x.parseError.reason + ':' + x.parseError.line + ':' + x.parseError.pos + ':' + e.description; }
				break;
			default:
				c = x;
		}

		try
		{
			m_dw = window.open("", "XML", "WIDTH=400, HEIGHT=160, dependent, status, scrollbars, top=50, left=50");
			if(m_dw!=null)
			{
				m_dw.resizeTo(700, 700);
				m_dw.document.clear;
				m_dw.document.write("<h1>" + t + "</h1>");
				m_dw.document.write("<div>" + c + "</div>");
				m_dw.focus();
			}
		}
		catch(eee) { alert('Could not write to Debug window. Access Denied. Close all browser windows and retry.'); }
	}

	function _VWX(c, t)
	{
		try
		{
			m_dw = window.open("", "XML", "WIDTH=400, HEIGHT=160, dependent, status, scrollbars, top=50, left=50");
			if(m_dw!=null)
			{
				m_dw.resizeTo(700, 700);
				m_dw.document.clear;
				m_dw.document.write("<link rel=\"stylesheet\" href=\"" + ERRORSTYLES + "\" type=\"text/css\">");
				m_dw.document.write("<h1>" + t + "</h1>");
				c = c.replace(new RegExp(">", "gi"), "¬").replace(new RegExp("<", "gi"), "~");
				c = c.replace(new RegExp("¬", "gi"), "&gt;</font>");
				c = c.replace(new RegExp("~", "gi"), "<font color=\"#FF0000\">&lt;");
				c = c.replace(new RegExp("\r\n", "gi"), "<br>"); 
				c = c.replace(new RegExp("\t", "gi"), "&nbsp;&nbsp;&nbsp;");
				m_dw.document.write("<div>" + c + "</div>");
				m_dw.focus();
			}
		}
		catch(eee) { alert('Could not write to Debug window. Access Denied. Close all browser windows and retry.'); }
	}
}

