// JavaScript Document
var chatInterval = null;
function ChatManager(){
	this.setProperty('target','chatTable');
	this.listLength = 50; //Default trimming length of chat view
	this.lines = new ObjectList();
	this.init = function(){
		if(this.getProperty('isInited')!=true){
			this.setProperty('isInited',true);
			chatInterval = setInterval('Chat.Load()',3000);
		}
		statusmessages.Add('Chat inited');
		}
	this.Stop = function(){
		if(chatInterval)
			if(chatInterval !='undefined') 
			clearInterval(chatInterval);
		this.setProperty('isInited',false);
		}
	
	this.Load = function(){
		agent.call('../_ajax.php','chatgetlines','Chat.AddLines');
		}
	this.Send	= function(){
	
		chatSendLine(arguments[0],arguments[1]);
		}
	this.Erase = function(){
			this.Trim(0);
			};
		this.Add = function(){
			var newItem = this.lines.Insert();
			newItem.setProperty('message',arguments[1]);
			newItem.setProperty('name',arguments[0]);
			if(this.autoprint) this.Print();
			}
		this.AddLines = function(resultval){
			
			if(resultval && resultval != 'false'){
			//	alert(resultval);
				var i = 0;
				var Line = null;
				for(i=0; resultval[i];i++){
					Line = this.lines.Insert();
					Line.ajaxSetProperties(resultval[i]);
					this.Print();
					//alert(Line.getProperty('message'));
					this.Trim(this.listLength);
					//alert(listLength);
					}
				}//if
			}//end function
		this.Print = function(){
			if(this.getProperty('target') && this.lines.Count()){
				var newTR = addTR(this.getProperty('target'),'BEGINNING');
				var newtd = addTD(this.getProperty('target'),this.lines.Current().getProperty('arrival'),'BEGINNING',newTR);
				newtd = addTD(this.getProperty('target'),'&lt;'+this.lines.Current().getProperty('name')+'&gt;','BEGINNING',newTR);
				newtd = addTD(this.getProperty('target'),this.lines.Current().getProperty('message').replace(/\n/g,'<br />'),'BEGINNING',newTR);
			//	statusmessages.Add('adding line:'+this.lines.Current().getProperty('message')+' by'+ this.lines.Current().getProperty('name'));
			}
			if(this.listLength != null)
			this.Trim(this.listLength);
			}
		this.PrintAll = function(){
			this.First();
			if(!this.isEmpty())
				do{
					this.Print();
					}while(this.Next());
			}
		this.Trim = function(){
			trimTable(this.getProperty('target'),arguments[0]);
			}
		
		this.CheckFeedback = function(){	
			var errors = '';
			if(getElement('chatusername'))
				if(getElement('chatusername').value=='undefined' || getElement('chatusername').value=='')
				errors ='Please input your name.\n';
				//else if(!checkEmail(getElement('chatusername').value)) errors = 'S&auml;hköpostiosoitte ei ole kelvollinen. \nTarkista antamasi osoite.\n';
			if(getElement('chatmessage'))
				if(getElement('chatmessage').value=='undefined' || getElement('chatmessage').value=='')
				errors += 'The message content is missing.\n';
			if(errors == ''){
					//Send
				this.Send(getElement('chatusername').value,getElement('chatmessage').value);
				getElement('chatmessage').value = '';
				}
			else {
				alert(errors + 'Viesti&auml; ei l&auml;hetetty..');
				}
			return(false);
				
			
		}
		
		
}
ChatManager.prototype = new Item();
var Chat = new ChatManager();
Chat.setProperty('objectID','chatcontainer');

/*
function PrompterManager(){
	this.init = function(){
		setInterval('Prompter.Load()',3000);
	
		}
	
	
	}
PrompterManager.prototype = new ChatManager();
*/
