//***************************************************************************************
//
//	comms.js
//	
//	Objects and functionality that supports communications
//
//	Copyright: 	David Horne, Tuross Technologies Australia P/L 2006
//				dkhorne@bigpond.net.au
//
//****************************************************************************************

//****************************************************************************************
//	Parse the URL into Paramter Name/Value pairs 
//****************************************************************************************
		function LocationObj() {
			this.URL = document.location.toString();
			this.File = this.URL.substr(0, (this.URL.indexOf("?") > 0 ? this.URL.indexOf("?") : this.URL.length));
			
			var TheParameters = (this.URL.indexOf("?") > 0 ? this.URL.substr(this.File.length + 1, this.URL.length) : "").split("&");
			var TheParameter;
			for (var i = 0; i < TheParameters.length; i++) {
				TheParameter = TheParameters[i].split("=");
				if (this[TheParameter[0]]) {
					if (this[TheParameter[0]].constructor != Array) this[TheParameter[0]] = new Array(this[TheParameter[0]]);
					this[TheParameter[0]][this[TheParameter[0]].length] = TheParameter[1];
				} else {
					this[TheParameter[0]] = TheParameter[1];
				}
			}
			
			this.Parameter = GetParameter;
		}
		
		function GetParameter(ParameterName, Default) {
			if (this[ParameterName] != null)
				return this[ParameterName];
			else
				return (Default != null ? Default : null);
		}

		var Location = new LocationObj();

//****************************************************************************************
// Cookie Support
//****************************************************************************************
	function SetCookie(Document, Name, Value, Expires) {
		var ExpiryDate = "";
		if (Expires != null) ExpiryDate = "expires=" + Date.parse(Expires).toGMTString() + ";";
		var strVal = Name + "=" + escape(Value) + ";" + ExpiryDate;
		Document.cookie = strVal;
	}
	
	function GetCookie(Document, Name, Default) {
		var re = new RegExp(Name + "=([^;]+)");
		var Value = re.exec(Document.cookie);
		return (Value != null) ? unescape(Value[1]) : Default;
	}
	
	function DeleteCookie(Document, Name) {
		SetCookie(Document, Name, null);
	}

//****************************************************************************************
// Return Functions used to define what processing pages do when they are ready to return
//****************************************************************************************

	function SetReturn(Name, Action) {
		if (window != null) SetCookie(window.top.document, Name, Action, null);
	}
	
	function Return(Name) {
		var strCommands = new String(GetCookie(window.top.document, Name)).split("|");

		for (var i = 0; i < strCommands.length; i++) {
			eval(strCommands[i]);
		}
	}
	
//****************************************************************************************
// Return Functions used to define what processing pages do when they are ready to return
//****************************************************************************************
	

