<!--
/*
	Javascript Implementation of Macromedia's LoadVars Class In Flash
	Uses ajax to do background posts / gets!
	Written By: Matt Maclean
*/
// Based on code from http://www.youngpup.net/
if (!Function.prototype.apply) 
{
  Function.prototype.apply = function(object, parameters) 
	{
    var parameterStrings = new Array();
    if (!object)     object = window;
    if (!parameters) parameters = new Array();
    
    for (var i = 0; i < parameters.length; i++)
      parameterStrings[i] = 'parameters[' + i + ']';
    
    object.__apply__ = this;
    var result = eval('object.__apply__(' + 
      parameterStrings.join(', ') + ')');
    object.__apply__ = null;
    
    return result;
  }
}

//Code From: http://prototype.conio.net/dist/prototype-1.3.1.js
Function.prototype.bind = function(object) 
{
  var __method = this;
  return function() {
    __method.apply(object, arguments);
  }
}

if ( !xapi )
{
	var xapi = {

		getRoot: function(xml)
		{
			if ( xml.documentElement )
			{
				return xml.documentElement;
			}
			else
			{
				return null;
			}
		},
		
		wasSuccessful: function(xml)
		{
			if ( xml == null )
				return false;
				 
			var success = xml.getElementsByTagName("success")[0].firstChild.nodeValue;
				
			if ( success == "true" )
				return true;
			else
				return false;
		},
		
		getErrorMessage: function(xml)
		{
			if ( xml )
				return xml.getElementsByTagName("error")[0].firstChild.nodeValue;
			else
				return "No Error Returned.";
		},
		
		getTextNode: function(xml, nodeName)
		{
			return xml.getElementsByTagName(nodeName)[0].firstChild.nodeValue;
		},
		
		getAttribute: function(xml, nodeName, att)
		{
			var node = xml.getElementsByTagName(nodeName)[0];
			var val = node.getAttribute(att);
			return val;
		},
		
		getNodes: function(xml, nodeName)
		{
			return xml.getElementsByTagName(nodeName);
		}
		
	};
}

function LoadVars()
{
	//the request obejct
	this.request = null;
	
	//get the transport
	if ( window.XMLHttpRequest ) 
	{
		this.request = new XMLHttpRequest();
	}
	else if ( window.ActiveXObject ) 
	{
		this.request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		throw "No Ajax Transport Available";
	}
	
	//the loaded flag
	this.loaded = false;
	
	//the default request headers
	this.requestHeaders = new Array();
	
	//default onload function
	this.onLoad = function(){};
	
	this.xmlData = null;
	this.textData = null;
	
	//create the props array
	this.props = new Array();
	
	//enumerate the native properties
	for ( var prop in this )
	{
		this.props.push(prop);
	}
}

//stateChanged
LoadVars.prototype.stateChanged = function()
{
	var events = ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
	var state = events[this.request.readyState];
	if ( this.request.readyState != -1 )
	{
		if ( state == "Loaded" )
			this.loaded = true;
			
		if ( state == "Complete" )
		{
			if (  this.request.responseXML == null )
			{
				this.textData = this.request.responseText;
				this.xmlData = this.request.responseXML;
				this.decode( this.textData );
			}
			else
			{
				//try to properly parse the xml
				this.textData = this.request.responseText;
				this.xmlData = this.request.responseXML;
			}
			
			this.request.onreadystatechange = function(){};
			var lFunc = this.onLoad.bind(this);
			lFunc(this);
		}
	}
};

//addRequestHeaders
// Usage
//    my_lv.addRequestHeader(headerName:String, headerValue:String) : Void
LoadVars.prototype.addRequestHeaders = function()
{
	if ( arguments.length != 2 )
		throw "addRequestHeaders: Wrong Argument Count, expected 2";
	
	this.requestHeaders[arguments[0]] = arguments[1];
};

//decode
//Usage
//   my_lv.decode(variables:String) : Void
LoadVars.prototype.decode = function(variables)
{
	try
	{
		//First split up into key/value paris
		var pairs = variables.split("&");
		
		//Now split up into key / values array
		var i =0;
		for ( i=0; i<pairs.length; i++ )
		{
			var pair = pairs[i];
			var parts = pair.split("=");
			var key = parts[0];
			var value = parts[1];
			
			var isNative = false;
			for ( var prop in this.props )
			{
				if ( prop == key )
					isNative = true;
			}
			
			key = key.replace(/^[ \t\r\n]+|[ \t\r\n]+$/g, "");
			value = value.replace(/^[ \t\r\n]+|[ \t\r\n]+$/g, "");
			
			if ( !isNative )
			{
				eval("this." + key + " = \"" + value + "\";");
			}
		}
	}
	catch (err) {}
};

//toString
LoadVars.prototype.toString = function()
{
	var str = "";
	
	for ( var prop in this )
	{
		var isNative = false;
		for ( i=0; i<this.props.length; i++ )
		{
			if ( prop == this.props[i] )
				isNative = true;
		}
		
		if ( !isNative )
		{
			if ( str.length == 0 )
				str += prop + "=" + escape(this[prop]);
			else
				str += "&" + prop + "=" + escape(this[prop]);
		}
	}
	
	return str;
};

//prepare
LoadVars.prototype.prepare = function()
{
	this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
};

//load
//Usage
//   my_lv.load(url:String) : Boolean
LoadVars.prototype.load = function(url)
{
	this.loaded = false;
	this.request.open("POST", url, true);
	this.prepare();
	this.request.onreadystatechange = this.stateChanged.bind(this);
	this.request.send(null);
};

//send
//Usage
//   my_lv.send(url:String) : Void
LoadVars.prototype.send = function()
{
	if ( arguments.length == 0 )
		throw "send: No URL Specified!";
		
	var url = arguments[0];
	
	this.loaded = false;
	this.request.open("POST", url, true);
	this.prepare();
	this.request.onreadystatechange = function(){};
	this.request.send( this.toString() );
};

//sendAndLoad
//Usage
//   my_lv.send(url:String) : Void
LoadVars.prototype.sendAndLoad = function()
{
	if ( arguments.length == 0 )
		throw "sendAndLoad: No URL Specified!";
	
	var url = arguments[0].toString();
	
	this.loaded = false;
	this.request.open("POST", url, true);
	this.prepare();
	this.request.onreadystatechange = this.stateChanged.bind(this);
	this.request.send( this.toString() );
};

//setarameter
LoadVars.prototype.setParameter = function(key, value)
{
	var isNative = false;
	for ( i=0; i<this.props.length; i++ )
	{
		if ( this.props[i] == key )
			isNative = true;
	}
	
	if ( !isNative )
	{
		eval( "this." + key + " = \"" + value + "\";");
	}
};
-->