// Javascript Shared Utility Library
var SharedUL = new SUL();
function SUL()
{
	// Keep MSIE from crashing
	if( !window.console )
	{
		window.console = {};
		window.console.log = function( msg )
		{
		    if(!window.console.element)
		    {
		        var div = document.createElement('div');
		        document.body.appendChild(div);
		        div.className = "IEConsoleLog";
		        window.console.element = div;
		    }
		    
		    var msgDiv = document.createElement('div');
		    window.console.element.appendChild(msgDiv);
		    msgDiv.innerHTML = msg;
		};
		
	}
	
	this.serializeNode = function( node )
	{
		// MSIE doesn't like the node parsing, and has the string ready in the xml property
		if( document.all )
		{
			return node.xml;		
		}
		else
		{
			if(!node.tagName) return node.textContent;
			var myNode = "<" + node.tagName; 
			if(node.attributes) 
			{ 
				for(var i=0; i<node.attributes.length; i++) 
				{ 
					var attr = node.attributes[i]; 
					myNode += " " + attr.name + "=\""+attr.value+"\""; 
				} 
			} 
			myNode += ">";
			for(var j=0; j<node.childNodes.length; j++) 
			{ 
				myNode += this.serializeNode(node.childNodes[j]); 
			} 
			myNode += "</" + node.tagName + ">";

			return myNode; 
		}
	}
	
	// Parses a string of xml into an XML DOM object
	// Got this from http://www.w3schools.com/Xml/tryit.asp?filename=tryxml_parsertest2
	this.parseXML = function( strXML )
	{
		try //Internet Explorer
		{
			var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = "false";
			xmlDoc.loadXML( strXML );
			return xmlDoc;
		}
		catch(e)
		{
			try //Firefox, Mozilla, Opera, etc.
			{
				parser = new DOMParser();
				xmlDoc = parser.parseFromString( strXML, "text/xml" );
				return xmlDoc;
			}
			catch(e)
			{
				alert( e.message );
				return null;
			}
		}
	}
	
	this.sendRequest = function(sender,url,onResult,vars,isasync)
	{
		var oRequest = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");

		setTimeout(
			function(id) {
				var objThis = this.reflect;
				function readyStateHandler()
				{
					switch(oRequest.readyState)
					{
						case 0: 
							//console.log('uninitialized'); 
							break;
						case 1: 
							//console.log('open'); 
							break;
						case 2: 
							//console.log('sent'); 
							break;
						case 3: 
							//console.log('receiving'); 
							break;
						case 4: 
							//console.log(['loaded',oRequest.responseXML]);
							if(onResult && oRequest)
							{ 
								if(oRequest.responseXML || oRequest.responseText)
								{
									var xml = (oRequest.responseXML) ? ( (document.all) ? oRequest.responseXML.xml : oRequest.responseXML.documentElement ): {length:''};
									if(oRequest.responseText.length > 0 || xml.length > 0)
									{
										if(typeof(onResult) == 'function') onResult.call(sender,oRequest);
									}
								}
							}
							break;
					}
				}
				//console.log('url: ' + url);
				if ( vars == null ) vars = '';
				oRequest.onreadystatechange = readyStateHandler;
				var async = (isasync) ? isasync : true;
				oRequest.open("POST", url, async);
				oRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
				oRequest.setRequestHeader("Content-Length", vars.length); 
				oRequest.send(vars);
			},0);
		 return oRequest; 
	}

}

/* Flash shared functionality */
function cFlashCreator( ContainerID, Width, Height, SourceFileName, FlashVars, Loop )
{
	// Parameters
	this.ContainerID = ContainerID;
	this.Width = Width;
	this.Height = Height;
	this.SourceFileName = SourceFileName;
	this.FlashVars = FlashVars;
	this.Loop = Loop;

	// Functions
	//
	this.onLoad = function()
	{
		var Container = document.getElementById( this.ContainerID );

		Container.innerHTML = 
			'<object classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' + 
			' codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"' +
			' id=' + Container.ID + '_flash' +
			' style="display: block;"' +
			' viewastext=""' + 
			' width=' + this.Width + 
			' height=' + this.Height + 
			'>' +
			this.createParam( "movie", this.SourceFileName ) +
			this.createParam( "flashvars", this.FlashVars ) +
			this.createParam( "play", "TRUE" ) +
			this.createParam( "quality", "HIGH" ) +
			this.createParam( "wmode", "TRANSPARENT" ) +
			this.createParam( "loop", this.Loop ) +
			((!document.all) ? this.createEmbed() : '') +
			'</object>';
	};


	this.createEmbed = function()
	{
		return '<embed' +
			' width=' + this.Width +
			' height=' + this.Height +
			' wmode=' + 'transparent' +
			' pluginspage=' + 'http://www.macromedia.com/go/getflashplayer' +
			' type=' + 'application/x-shockwave-flash' +
			' allowscriptaccess=\"sameDomain\"' +
			' quality=' + 'high' +
			' loop=' + this.Loop +
			' flashvars=\"' + this.FlashVars + '\"' +
			' play=true' +
			' src=\"' + this.SourceFileName + '\"' +
			'/>';
	}

	this.createParam = function( strName, strValue )
	{
		return '<param name="' + strName + '" value=' + strValue + '>';
	}
}

