function ChatControl(wi2Table) {
	this.wisTable=wi2Table;
	
	this.GetUsername = function()
	{
		return this.wisTable.GetUsername();
	}
};

function wi2Table(baseURL,tblGUID,_ownerName,_ownerNickName,_userName,_certificate,_escapedSharedMediaInfo,_accountType,_avatarURL,encodedTableSettingsAsJsonFromDatabase,_tableInitialStateSetting) {

	this.ChatControl=new ChatControl(this);
	this.baseURL=baseURL;
	this.tableGUID = tblGUID;
	this.sessionID = tblGUID; // added for compatability
	this.accountType=_accountType;
	this.avatarURL=_avatarURL;
	this.tableInitialStateSetting=_tableInitialStateSetting;
	
	//alert("ownerName="+_ownerName+" , ownerNickName="+_ownerNickName+" , userName="+_userName+" , avatarURL="+_avatarURL+" , encodedTableSettingsAsJsonFromDatabase="+encodedTableSettingsAsJsonFromDatabase); //555
	
	this.userName="";
	if(_userName!=null&&_userName.length>0)
		this.userName=_userName;
		
	this.nickName=this.userName; // temp
		
	this.ownerName="";
	if(_ownerName!=null&&_ownerName.length>0)
		this.ownerName=_ownerName;
	
	this.ownerNickName="";
	if(_ownerNickName!=null&&_ownerNickName.length>0)
		this.ownerNickName=_ownerNickName.replace(","," ");
	else
		this.ownerNickName=this.ownerName.replace(","," ");
	//alert(this.ownerNickName);
		
	this.certificate="";
	if(_certificate!=null&&_certificate.length>0)
		this.certificate=_certificate;
	
	this.escapedSharedMediaInfo="";
	if(_escapedSharedMediaInfo!=null&&_escapedSharedMediaInfo.length>0)
		this.escapedSharedMediaInfo=_escapedSharedMediaInfo;
		
	this.flashVideoConferenceMovie=null;
	this.infoArray=GenerateTableInfoArray(this);
	this._privateTableSettingsCSV = "";//private,*enter title here*,*enter description here*,true"; // intialial settings
	this._privateAllCanControl = true;  // this is the default setting for whether all can control is set initially (if above is changed to "public" probably need to change this to false)
	
	//alert("ownerName="+_ownerName+" , ownerNickName="+_ownerNickName+" , userName="+_userName+" , avatarURL="+_avatarURL); //555
	
	this.GetUsername = function () 
	{ 
		//alert("wi2Table.GetUsername() returning "+this.userName);
		return this.userName; 
	};
	
	this.SetUsername = function (un) 
	{ 
		this.userName=un; 
		
	};
	
	this.GetNickname = function () 
	{ 
		//alert("wi2Table GetNickname returning "+this.nickName);
		return this.nickName; 
	};
	
	this.SetNickname = function (nn) 
	{ 
		//alert("wi2Table SetNickname to "+nn);
		this.nickName=nn; 
	};
	
	this.GetOwnername = function () 
	{ 
		return this.ownerName; 
	};
	
	this.SetOwnername = function (on) 
	{ 
		this.ownerName=on; 
	};
	
	this.GetOwnerNickName = function () 
	{ 
		//alert("wi2Table GetOwnerNickName returning "+this.ownerNickName);
		return this.ownerNickName; 
	};
	
	this.SetOwnerNickName = function (onn) 
	{ 
		this.ownerNickName=onn; 
	};
	
	this.GetTableGUID = function () 
	{ 
		return this.tableGUID; 
	};
	
	this.SetUserAvatarURL = function (aurl) 
	{ 
		this.avatarURL=aurl; 
	}
	
	this.GetUserAvatarURL = function () 
	{ 
		return this.avatarURL; 
	}
	
	this.SetInitialTableStateSetting = function (initialState)
	{
		this.tableInitialStateSetting = initialState;
	}
	
	this.GetInitialTableStateSetting = function ()
	{
		return this.tableInitialStateSetting;
	}
	
	
	this.GetTableInfo  = function() 
	{
		return this.infoArray;
	}
	
	this.SetAccountType = function (at) 
	{ 
		this.accountType=at; 
	}
	
	this.GetAccountType = function () 
	{ 
		return this.accountType; 
	}
	
	this.SetCertificate = function (cert) 
	{ 
		this.certificate=cert; 
	};
	
	this.GetCertificate = function () 
	{ 
		return this.certificate; 
	};
	
	this.IsOwner = function ()
	{
		if(this.userName==null)
			return false;
		// should probably create a bool variable for this, and cache it, instead of doing this comparison each time
		var bIsOwner=(this.userName.length>0&&this.ownerName==this.userName);
		//alert(bIsOwner);
		return bIsOwner;
	}
	
	this.GetTableSettingsCSV = function () 
	{ 
		//alert("GetTableSettingsCSV returning: "+this._privateTableSettingsCSV);
		return this._privateTableSettingsCSV; 
	};
	
	this.PutTableSettingsCSV = function (tableSettingsCSV,bWriteToDatabase) 
	{ 
		//alert("PutTableSettingsCSV :"+tableSettingsCSV); //888
		this._privateTableSettingsCSV=tableSettingsCSV; 
		// interpret the string, to get control setting
		var comp=tableSettingsCSV.split(",");
		
		if(comp.length>4)
		{
			if(comp[3]=="true"||comp[3]=="1")
			{
				//alert("everyone has control");
				this._privateAllCanControl=true;
			}
			else
			{
				//alert("owner has control");
				this._privateAllCanControl=false;
			}
		}
		
		if(bWriteToDatabase)
			this.WriteTableSettingsToDatabaseServer();
	};
	
	this.WriteTableSettingsToDatabaseServer = function ()
	{
		//alert("WriteTableSettingsToDatabaseServer");
		//alert(this._privateTableSettingsCSV);
		// send the info to the db
		var comp=this._privateTableSettingsCSV.split(",");
		// only if the owner
		if(this.IsOwner()==true&&comp.length>4)
		{
		
			var url=this.baseURL+"moc/dbaseRoutines/updateTable.php";
			var params="ownerID=";
			params+=this.userName;
			params+="&tableID=";
			params+=this.tableGUID;
			params+="&tableType=";
			params+=comp[0];
			params+="&tableTitle=";
			params+=comp[1];
			params+="&tableDescription=";
			params+=comp[2];
			params+="&tableImageURL=";
			if(comp[4]=="")
				params+=this.avatarURL;
			else
				params+=escape(comp[4]);
			params+="&allControlFlag=";
			if(this._privateAllCanControl)
				params+="1";
			else
				params+="0";
			//alert(url+" , "+params); //777
			var returnString=this.syncronousPost(url,params);
			//alert(returnString);//777
		}
	}
	
	this.AllowedToControl = function()
	{
		//alert(this.ownerName);
		
		if(this.IsOwner()==true)
			return true;
		return this._privateAllCanControl; 
		
		return false;
	}
	
	/*
	this.AllCanControl = function () 
	{ 
		alert(this.userName);
		alert(this.ownerName);
		return this._privateAllCanControl; 
	};
	*/
	
	this.AddDropArea = function(upperLeftX,upperLeftY, width, height, type, typeFilter)//300,0,440,385,"","")
	{
		return;
	}
	
	this.SetFMSChatObject = function(flashVideoConferenceMovie)
	{
		this.flashVideoConferenceMovie=flashVideoConferenceMovie;
	}
	
	this.OnFMSChatStateChanged = function(newState)
	{
	
	}
	
	this.SendTypingEvent = function(csvToList,bStart)
	{
		// need to impement
	}
	
	this.syncronousPost=function(url,params)
	{
		var returnString="";
		var xmlobj=null;
	    // check for existing requests
	    try{
	        // instantiate object for Mozilla, Nestcape, etc.
	        xmlobj=new XMLHttpRequest();
	    }
	    catch(e){
	        try{
	            // instantiate object for Internet Explorer
	            xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
	        }
	        catch(e){
	            // Ajax is not supported by the browser
	            xmlobj=null;
	            return false;
	        }
	    }
		
		try{
	    // open socket connection
	    xmlobj.open("POST", url, false);
		
		//Send the proper header information along with the request
		xmlobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlobj.setRequestHeader("Content-length", params.length);
		xmlobj.setRequestHeader("Connection", "close");

		// force non-cached reply
		xmlobj.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
	    // send POST request
	    xmlobj.send(params);
		if (xmlobj.status >= 200&&xmlobj.status<300)
		{
			returnString=xmlobj.responseText;
		}
		}
		catch(e){}
	return returnString;
	}	
	
	// **************** THIS IS ESSETIALLY THE CONSTRUCTOR ENTRY POINT *******************
	// if this is the first time its called, set it to default values
	
	
	
	// get the nick name
		var data = "userID="+this.userName; 
		var requestFile = baseURL + 'moc/dbaseRoutines/getUserInfo.php';
		//alert(data);
		//alert(requestFile);
		result = runSyncAjaxPost(requestFile, data);
		
		var returnValues = $.evalJSON(result);
		//var returnValues=result.parseJSON();
		if(returnValues&&returnValues[1])
			this.nickName = returnValues[1];
	
		
	if(this._privateTableSettingsCSV.length==0)
	{
		var nameToUse=this.userName;
		if(this.nickName.length>0)
			nameToUse=this.nickName;
		if(nameToUse.length==0)
			nameToUse="Somebody";
			
		// if nameToUse has any commas need to remove them
		nameToUse=nameToUse.replace(","," ");
		
		//alert(decodeURIComponent(encodedTableSettingsAsJsonFromDatabase));
		//if(encodedTableSettingsAsJsonFromDatabase.length==0)
		//{
		//	this._privateTableSettingsCSV = "private,"+nameToUse+"'s Show,Come and join "+nameToUse+"'s show!,true,,AVATAR"; // intialial settings
		//	this.PutTableSettingsCSV(this._privateTableSettingsCSV,true);
		//}
		//else
		//{
			// extract the info
			//alert(encodedTableSettingsAsJsonFromDatabase);
			var decodedJsonString=decodeURIComponent(encodedTableSettingsAsJsonFromDatabase);
			//alert(decodedJsonString);
			
			if(decodedJsonString!="-1"&&decodedJsonString!=null&&decodedJsonString.length>10) // 10 just picked as some small value
			{
				//returnValue=$.toJSON(decodedJsonString);
				returnValue=$.evalJSON(decodedJsonString);
				//returnValue=decodedJsonString.parseJSON();
				
				var owner_id = returnValue[0];
				var type = returnValue[1];
				var status = returnValue[2];
				var title = returnValue[7];
				var description = returnValue[8];
				var allControl_flag = returnValue[9];
				var flash_media_URL = returnValue[3];
				var table_image_URL = returnValue[4];
				var num_views = returnValue[6];
				var window_info  = returnValue[5];
				var tableThumbnailSource="CONTENT";
				// simple test to see if the thumbnail is from AVATAR, CUSTOM, or CONTENT
				if(table_image_URL.indexOf("memberAvatars")!=-1)
					tableThumbnailSource="AVATAR";
				else if(table_image_URL.indexOf("userUploads")!=-1)
					tableThumbnailSource="CUSTOM";
					
				var allCanControlAsText="false";
				if(allControl_flag=="1"||allControl_flag=="true"||allControl_flag==1||allControl_flag==true)
					allCanControlAsText="true";
				this._privateTableSettingsCSV = status+","+title+","+description+","+allCanControlAsText+","+table_image_URL+","+tableThumbnailSource; // intialial settings
				//alert(this._privateTableSettingsCSV); //888
				this.PutTableSettingsCSV(this._privateTableSettingsCSV,false);
			}
			else
			{
				this._privateTableSettingsCSV = "private,"+nameToUse+"'s Show,Come and join "+nameToUse+"'s show!,true,,AVATAR"; // intialial settings
				//alert(this._privateTableSettingsCSV); //888
				this.PutTableSettingsCSV(this._privateTableSettingsCSV,true);
			}
			
		//} 
	}
}

function GenerateTableInfoArray(t)
{
	//alert("GenerateTableInfoArray");
	//var elements = tableInfoString.split(",");
	var jsArray=new Array();
	
	// first([0])is the sessionID
    // [1] is bool indicating if the table was created locally
	// [2] is a description of the table
	// [3] is xml with static table info
	// [4] is the url of the shared media
	// [5] is the width of the shared media window  
	// [6] is the height of the shared media window 
	// [7] is the tableGUID
	
	jsArray[0]=t.tableGUID;
	if(t.userName==t.ownerName)
		jsArray[1]=true;
	else
		jsArray[1]=false;
	
	jsArray[2]=""; // might need to get this at some point
	
	jsArray[3]=""; // this shouldnt be needed anymore
	
	jsArray[4]=t.escapedSharedMediaInfo;// this will only be needed if this table belongs to the owner. any other participants  query this url from the owner when they join the table
	
	jsArray[5]=""; // this shouldnt be needed anymore
	
	jsArray[6]=""; // this shouldnt be needed anymore
	
	jsArray[7]=t.tableGUID;
		
	return jsArray;
}