// JavaScript Document

Shoutbox.path = "shoutbox/";

function Shoutbox(divid,instancename,username,width,height){
		var id = divid;
		var username = username;
		var instancename = instancename;
		var width = width;
		var height = height;
		var timestamp = new Date("January 1, 1970 00:00:00");
		var newTime=new Date();
		var allowedmsgcountincycle =10;
		var cycleduration =5;
		var banned = false;
		var msgcount = 0;
		var overallmsgcount = 0;
		var maxprobation = 10;
		var probationcount=0;
		var allowedmsglength = 255;
		var outerbox;
		var box;
		var lowerbox;
		
		
		var prequest =  false;
		
		this.isEnter = isEnter;
		this.sendMessage = sendMessage;
		this.resetmsgCount = resetmsgCount;
		this.retrieveByCycle = retrieveByCycle;
		this.retrieveRequest = retrieveRequest;
		
		Start();
		function Start(){
			outerbox = document.getElementById(id);
			if(outerbox!=null){
				outerbox.innerHTML = Render();
				box = document.getElementById(instancename+'shoutboxdiv');
				lowerbox = document.getElementById(instancename+'lowerbox');
				if(width != "undefined"){
					outerbox.style.width = width;					
				}
							
				
				firstRun();				
			}
				
		}
		
		function Render(){
			var txt;
			if(username!=""){
				txt = '			<input name="' +instancename+'name" type="hidden" id="' +instancename+'name" class="name" value="' + username + '"/>' +

					'	<div class="lowerpane" align="left">' +
					'		<div class="messagecaption">' +
					'		Message' +
					'		</div>' +
					'		<div class="messageholder">' +
					'		<input name="' +instancename+'message" type="text" id="' +instancename+'message" class="messagebox" onkeypress="if(' +instancename+'.isEnter(event)){' +instancename+'.sendMessage(document.getElementById(\'' +instancename+'name\'),document.getElementById(\'' +instancename+'message\'),this);' +
					'							  this.disabled = false; ' +
					'							  this.value = \'\';	' +
					'							  this.focus();	' +
					'							  }" onkeydown="" />' +
					'		</div>					' +
					'	</div>' +
					'</div>';
			}
			else{
				txt = '	<div class="lowerpane" align="left">' +
					'		<div class="messagecaption2">' +
					'		Please log in to use the shoutbox' +
					'		</div>' +
					'		<div class="messageholder">' +		
					'		</div>					' +
					'	</div>' +
					'</div>';
			}
			var st ='<div id="' +instancename+'shoutboxdiv" class="shoutbox" align="left"></div>' + txt;
					
			return st;
		}
		
		function isEnter(e)
		{
			var keynum;
			var keychar=0;
			var numcheck;	
			if(e.keyCode)
			{
				keynum = e.keyCode;
				
			}
			else if(e.which)
			{
				keynum = e.which;
			}
			if(keynum == 13)
				return true;
			else
				return false;	
		}
		
		function retrieveXML(posdata,dir){
			var xml = $.ajax({
			type: "POST",
			url: Shoutbox.path + "retrieveshoutbox.php",
			dataType: "xml",
			data: posdata,
			error: function(msg){
				prequest = false;
				},
			success: function(msg){
				prequest = false;
				showData(msg.documentElement,box,dir);	
				if(dir==0){
					resetmsgCount();					
					setTimeout(instancename+'.retrieveRequest()',cycleduration * 1000);					
				}else{					
					setTimeout(instancename+'.retrieveRequest()',cycleduration * 1000);					
				}
			}
			}).responseXML;			
			return xml;
		}
		function showData(xml,datadoc,dir){		
			for (var i = 0; i < xml.childNodes.length; i++)
			{		
				try{
					name =  xml.getElementsByTagName("NAME")[i].childNodes[0].nodeValue;			
				}catch(e){
					name = "Noname";				
				}
				try{		
					msg =  xml.getElementsByTagName("MESSAGE")[i].childNodes[0].nodeValue;	
				}catch(e){	
					msg = "message";					
				}
				try{
					created = xml.getElementsByTagName("CREATED")[i].childNodes[0].nodeValue;			
					newTime = new Date(created);			
				}catch(e){
					created = "No Date";	
					newTime = new Date();
				}
					
				if(dir == 0 ){
					datadoc.innerHTML = formatmsg(name,msg,created)  + "" + datadoc.innerHTML;
				}
				else{
					datadoc.innerHTML =  datadoc.innerHTML + formatmsg(name,msg,created) + "";	
	
				}
				
				if (timestamp < newTime ){


					timestamp =newTime;				
				}
			}  
			if(i!=0)			
				datadoc.scrollTop = datadoc.scrollHeight;
			
		}
		
		function formatmsg(name,msg,created){
			return ('<div class="message"><div class="messagedate">' + created + '</div><div class="messagename">' + name + ':</div><div class="messagecontent">' + msg + '</div></div>' );
		}
		
		function addContent(doc,content){
			doc.innerHTML = doc.innerHTML + '<div class="message">' + content + '</div>';
			doc.scrollTop = doc.scrollHeight;
		}
		
		function firstRun(){
			var xml = retrieveXML("limit=25",0);		
		}
		
		function retrieveRequest(){
			//if (forcerequest == true)
			//	requestcancel = true;
			//else
			//	requestcancel = false;
			
			if (prequest == false){				
				prequest = true;
				var xml = retrieveXML("timestamp="+timestamp.getFullYear()+'-'+(timestamp.getMonth()+1)+'-'+timestamp.getDate()+' '+timestamp.getHours()+':'+timestamp.getMinutes()+':'+timestamp.getSeconds(),1);		
			}
			
		}
		
		function sendMessage(name,msg,btn){
			//alert (timestamp);
			var str;		
			//str = oEditor.GetHTML;
			btn.disabled = true;
			//alert (oEditor.GetHTML);			
			if (msg.value!=""){
				if(msg.value.length<1000){
					if (msgcount < allowedmsgcountincycle){
						if (probationcount < maxprobation){
							$.ajax({type: 'POST',
									url: Shoutbox.path + 'processshoutbox.php',
									data: 'option=1&name='+name.value+'&message='+msg.value, 						
									success: function(msg){ if (msg!=''){  
																probationcount++;
																addContent(document.getElementById(instancename+'shoutboxdiv'),msg);														
																if (probationcount > maxprobation){	
																banUser();
																addContent(document.getElementById(instancename+'shoutboxdiv'),"Due to your actions... you are banned from sending msg.");
																banned = true;
															}
															}
															else{																														
																retrieveRequest();															
																msgcount ++;														
															}
															btn.disabled = false;
															}
							});
						}
						else{
							banUser();
							addContent(document.getElementById(instancename+'shoutboxdiv'),"Sorry... You are banned from sending msg.");
							banned = true;
						}
					}
					else{
							addContent(document.getElementById(instancename+'shoutboxdiv'),"Sorry... Please refrain from sending too many msg at a time. Wait a while to send your msg.");
							probationcount ++;
							if (probationcount > maxprobation){	
								banUser();
								addContent(document.getElementById(instancename+'shoutboxdiv'),"Due to your actions... you are banned from sending msg.");
								banned = true;
							}
							btn.disabled = false;
					}
				}
				else{
						addContent(document.getElementById(instancename+'shoutboxdiv'),"Please refrain from sending long messages.");
						probationcount ++;
						if (probationcount > maxprobation){	
								banUser();
								addContent(document.getElementById(instancename+'shoutboxdiv'),"Due to your actions... you are banned from sending msg.");
								banned = true;
							}
							btn.disabled = false;
						
				}
			}
		}
		
		function resetmsgCount(){
					
					overallmsgcount += msgcount;
					msgcount = 0; 
					setTimeout(instancename+'.resetmsgCount()',cycleduration * 1000);				
					
		}
		
		function retrieveByCycle(){			
					retrieveRequest();
					setTimeout(instancename+'.retrieveByCycle()',cycleduration * 1000);								
					
		}
		
		function banUser(){
			$.ajax({type: 'POST',
							url: Shoutbox.path + 'processshoutbox.php',
							data: 'option=2'
							});
		}
}
