// JavaScript Document


Shoutbox.path = "shoutbox/";



function Shoutbox(divid,instancename,width,height){

		var id = divid;

		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 =10;

		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 uname;

		var umessage;

		

		this.isEnter = isEnter;

		this.sendMessage = sendMessage;

		this.resetmsgCount = resetmsgCount;

		this.retrieveByCycle = retrieveByCycle;

		this.retrieveRequest = retrieveRequest;

		this.ProcessInput = ProcessInput;

		

		Start();

		function Start(){

			outerbox = document.getElementById(id);

			if(outerbox!=null){

				outerbox.innerHTML = Render();

				box = document.getElementById(instancename+'shoutboxdiv');

				lowerbox = document.getElementById(instancename+'lowerbox');

				uname = document.getElementById(instancename+'name');

				umessage = document.getElementById(instancename+'message');				

				if(width != "undefined"){

					outerbox.style.width = width;					

				}

				if(height != "undefined"){

					outerbox.style.height = height;					

				}else{

				}				

				

				firstRun();				

			}

				

		}

		

		function Render(){

			var st ='<div id="' +instancename+'shoutboxdiv" class="shoutbox" align="left"></div>';

			return st;

		}

		

		function attachtextboxevents(){				

				addEvent(umessage,"onkeypress", instancename +'.ProcessInput');

		}

		function ProcessInput(e){		

				if (isEnter(e)){

					SendMessage();					

				}

				

		}

		

		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: SiteURL+Shoutbox.path + "retrieveshoutbox.php",
			
			dataType: "xml",

			data: posdata,

			success: function(msg){

				showData(msg.documentElement,box,dir);	

				if(dir==0){

					resetmsgCount();

					retrieveByCycle();

					//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="messagecontent">' + msg + '</div><div class="messagename">From: ' + name + '</div></div>\n' );

		}

		

		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(){

			var xml = retrieveXML("timestamp="+timestamp.getFullYear()+'-'+(timestamp.getMonth()+1)+'-'+timestamp.getDate()+' '+timestamp.getHours()+':'+timestamp.getMinutes()+':'+timestamp.getSeconds(),1);		

		}

		

		function sendMessage(name,msg,iduser,btn){ 

			if (1){ 

				if (msgcount < allowedmsgcountincycle){

					if (probationcount < maxprobation){

						$.ajax({type: 'POST',

								url: SiteURL+Shoutbox.path + 'processshoutbox.php',

								data: 'option=1&name='+name.value+'&message='+msg.value+'&id_user='+iduser.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 ++;														

														}														

														}

						});

					}

					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'),"Your msg is too long...");

					btn.disabled = false;

			}

		}

		

		function resetmsgCount(){

					

					overallmsgcount += msgcount;

					msgcount = 0; 

					//if (banned!= true)

					//	btn.disabled = false;

					setTimeout(instancename+'.resetmsgCount()',cycleduration * 1000);				

					

		}

		

		function retrieveByCycle(){			

					retrieveRequest();

					setTimeout(instancename+'.retrieveByCycle()',cycleduration * 1000);	

					//alert("asd");

					

		}

		

		function banUser(){

			$.ajax({type: 'POST',

							url: SiteURL+Shoutbox.path + 'processshoutbox.php',

							data: 'option=2'

							});

		}	

		

}





	

