/* The global modal dialog stub */
	var g_Modal = null;
    var g_Name = null;
	var g_CharFile = null;
	
	window.onload=function()
	{
		/* Sweep unnecessary empty text nodes. */
		DOMManager.sweep();

		/*
		 * Attach supporting css bind required
		 * classes for transparency support in Opera.
		 */
		addExtensionsForOpera();

		/* Attach opacity css. */
		attachOpacityCSS();

		/* Adjust height. */
		adjustHeight();

		/* Create the modal dialog */
		g_Modal=new ModalDialog("ModalBG","DialogWindow",
			"DialogContent","DialogActionBtn");

		/* Bind an event listener to double-click event. */
		/*EventHandler.addEventListener(document,"dblclick",document_dblclick);*/
		EventHandler.addEventListener("btnGet","click",btnGet_click);

		/* Re-adjust height on window resize */
		EventHandler.addEventListener(window,"resize",window_resize);
	};

	function window_resize(evt)
	{
		adjustHeight();
	}

	function adjustHeight()
	{
		var intWindowHeight=WindowObject.getInnerDimension().getY();
		var dynModalBG=new DynamicLayer("ModalBG");
		var intModalHeight=dynModalBG.getHeight();

		if(intModalHeight<intWindowHeight)
		{
			dynModalBG.setHeight(intWindowHeight);
		}
	}

	function addExtensionsForOpera()
	{
		/* classes for opera */
		var ModalBG=new CBObject("ModalBG").getObject();
		if(typeof(window.opera)!="undefined")
		{
			ModalBG.className="modalOpera";
		}
	}

	function attachOpacityCSS()
	{
		/*
		 * CSS for opacity support
		 * Note that this can be directly added to the body.
		 * If you do not care about blindly adhering to standards
		 * you can directly include the rules into Master.css
		 *
		 * Do I care? Yes and no.(visit http://www.sarmal.com/Exceptions.aspx
		 * to learn how I feel about it).
		 */
		var opacityCSS = document.createElement("link");
		opacityCSS.type="text/css";
		opacityCSS.rel="stylesheet";
		opacityCSS.href="Opacity.css";
		document.getElementsByTagName("head")[0].appendChild(opacityCSS);
	}

	function btnGet_click(evt)
	{
		/* create an AJAX request */
		var ajax = _.ajax();
		/*
		 * Note that _.ajax(); is a shorthand notation 
		 * for new XHRequest();
		 * Visit http://sardalya.pbwiki.com/Shortcuts details.
		 */

		/* 
		 * You can add as many fields as you like to the post data. 
		 * Normally the server will use this data to create an
		 * output that makes sense which may be an XML, a JSON String
		 * or an HTML String.
		 */
		ajax.removeAllFields();
		ajax.addField("name","John");
		ajax.addField("surname","Doe");

		ajax.oncomplete=ajax_complete;
		ajax.onerror=ajax_error;

		g_Modal.show("Fetching data... Please wait...");

		/*
		 * Disable close action if you want to force the user 
		 * to wait for the outcome of the AJAX request.
		 * Although it is generally not recommended 
		 * this may be necessary at certain times.
		 */
		g_Modal.disableClose();

		/* Post data to the server. */
		ajax.get(g_CharFile);

		/* Stop event propagation. */
		new EventObject(evt).cancelDefaultAction();
	}

	/* Triggered when a successful AJAX response comes from the server.*/
	function ajax_complete(strResponseText,objResponseXML)
	{
		g_Modal.show(strResponseText);
		
		/* Re-activate close button. */
		g_Modal.enableClose();
	}

	/* Triggered when server generates an error. */
	function ajax_error(intStatus,strStatusText)
	{
		g_Modal.show("Error code: ["+ intStatus+ "] error message: [" + 
			strStatusText + "].");

		/* Re-activate close button. */
		g_Modal.enableClose();
	}
