// We like ugly fixes.
is_ie = false;

function handle(e)
{
	switch (e.name)
	{
		//Is it our error? :)
		case "E_USER_ERROR":
		case "E_CORE_ERROR":
			message(e.message, true);			return;
		break;

		default:
		{

			tmp = 'There has been a JavaScript error. \\nError: ';
			tmp += e.message;
			if (e.lineNumber)
			{
				tmp += '\nLine: ';
				tmp += e.lineNumber;
			}
			if (e.fileName)
			{
				tmp += '\nFile: ';
				tmp += e.fileName;
			}
			message(tmp, true);			return;
		}
	}
}
function load_shout()
{
	try
	{
        var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1));
		if (display_shoutbox == false)
		{
			return;
		}
		
		if (document.getElementById('shoutbox') == null)
		{
		
			var ev = err_msg('The shoutbox div cann’t be found.');
	
			ev.name = 'E_CORE_ERROR';
			throw ev; 
			return;
		}
		else
		{
			div = document.getElementById('shoutbox');

			// Display message ;)
			message('Loading');
			// HTTP vars, required to relead/post things.
			
			hin = http();
			
			if (!hin)
			{
				return;
			}
			hin2 = http();
			huit = http();	
			hsmilies = http();
			hnr = http();
						// Div exists in the html, write it.
			write_main();
		}
	}
	catch (e)
	{
			handle(e);
			return;
	}
}
function write_main()
{
	try
	{
		// Write the base.

		var base = ce('ul');
		base.className = 'topiclist forums';
		base.id = 'base_ul'
		// base.style.height = '210px';

		var li = ce('li');
		li.style.display = 'none';
		var dl = ce('dl');
		dl.style.width = '98%';
		var posting_form = ce('dt');
		posting_form.id = 'post_message';
		posting_form.className = 'row';
		posting_form.width = '98%';
		posting_form.style.display = 'none';
		posting_form.height = '20px';
		posting_form.style.width = '98%';

		var posting_box = ce('form');
		posting_box.id = 'chat_form';

				
		li.style.display = 'block';
		posting_box.appendChild(tn('Post message: '));

		el = null;
		var el = ce('input');

		el.className = 'inputbox';
		el.name = el.id = 'chat_message';

		el.style.width = '325px';

		el.onkeypress = function(evt)
		{
			try
			{
				evt = (evt) ? evt : event;
				var c = (evt.wich) ? evt.wich : evt.keyCode;
				if (c == 13)
				{
					document.getElementById('user').click();
					evt.returnValue = false;
					this.returnValue = false;
					return false;
				}
				return true;
			}
			catch (e)
			{
				handle(e);
				return;
			}
		}	
		posting_box.appendChild(el);
		posting_box.appendChild(tn(' '));
		// posting_box.appendChild(ce('br'));

		var el = ce('input');
		el.name = el.id = 'user';
		el.value = el.defaultValue = 'Post message';

		el.type = 'button';
		el.className = 'button1 btnmain';

		el.onclick = function()
		{
			try
			{
							
				// Here we send later the message ;)
				this.disabled = true;
	
				document.getElementById('post_message').style.display = 'none';
				this.disabled = false;
				document.getElementById('msg_txt').innerHTML = '';
				document.getElementById('msg_txt').appendChild(tn('Sending message.'));

				if (document.getElementById('chat_message').value == '')
				{
					document.getElementById('msg_txt').innerHTML = '';
					throw err_msg('Message is empty.');
				}

				if (huit.readyState == 4 || huit.readyState == 0)
				{

					// Lets got some nice things :D
					huit.open('POST','./ajax.php?m=post&sid=6b7db4075d733cab99de154b032c4c59&rand='+Math.floor(Math.random() * 1000000),true);

					huit.onreadystatechange = function()
					{
						try
						{
					        if (huit.readyState != 4)
					        {
					            return;
							}
							if (huit.readyState == 4)
							{
								xml = huit.responseXML;
								if (xml.getElementsByTagName('error') && xml.getElementsByTagName('error').length != 0)
								{
									err = xml.getElementsByTagName('error')[0].childNodes[0].nodeValue;
									document.getElementById('msg_txt').innerHTML = '';
									document.getElementById('post_message').style.display = 'block';
									last = 0;
									message(err, true);
								}
								else
								{
									document.getElementById('msg_txt').innerHTML = '';
									document.getElementById('msg_txt').appendChild(tn('Message posted.'));
									setTimeout("document.getElementById('msg_txt').innerHTML = ''",3000);
									document.getElementById('post_message').style.display = 'block';
									count = 0;// Set count to 0, because otherwise user willn't see his message
									clearTimeout(timer_in);
									timer_in = setTimeout('reload_post();reload_page();', 200);
									setTimeout('last = 0;', 500);
								}
								document.getElementById('chat_message').focus();
							}
						}
						catch (e)
						{
							handle(e);
							return;
						}
					}
					post = 'chat_message=';

					post += encodeURIComponent(document.getElementById('chat_message').value);

					document.getElementById('chat_message').value = '';
					
					
					huit.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

					huit.send(post);
				}
				else
				{
					throw err_msg('This should not happen, double request found!');
				}
			}
			catch (e)
			{
				document.getElementById('post_message').style.display = 'inline';
				setTimeout("document.getElementById('msg_txt').innerHTML = ''",5000);
				handle(e);
				return;
			}
		}
		posting_box.appendChild(el);

		posting_box.appendChild(tn(' '));
		
				
		posting_form.appendChild(posting_box);
		dl.appendChild(posting_form);
		li.appendChild(dl);
		base.appendChild(li);
		
		var msg_txt = ce('div');
		
		msg_txt.id = 'msg_txt';
		msg_txt.height = '180px';
		msg_txt.appendChild(tn(' '));
		
		base.appendChild(msg_txt);

		var post = ce('div');//In this div, the chats will be placed ;)
		post.style.display = 'block';
		post.id = 'msg';
		
		post.style.width = '98%';
		post.style.height = '160px';
			post.style.overflow = 'auto';		post.appendChild(tn('Loading'));
		base.appendChild(post);

		// Nr div
		var nr_d = ce('div');
		nr_d.id = 'nr';

		base.appendChild(nr_d);
		 
		div.innerHTML = '';
		div.appendChild(base);
		// Everyting loaded, lets select posts :)
		reload_post();
		reload_page();

	}
	catch (e)
	{
		handle(e);
		return;
	}
}

function reload_page()
{
	if (hnr.readyState == 4 || hnr.readyState == 0)
	{

		// Lets got some nice things :D
		hnr.open('GET','./ajax.php?m=number&sid=6b7db4075d733cab99de154b032c4c59&rand='+Math.floor(Math.random() * 1000000),true);

		hnr.onreadystatechange = function()
		{
			try
			{
		        if (hnr.readyState != 4)
		        {
		            return;
				}
				if (hnr.readyState == 4)
				{
					xml = hnr.responseXML;
					if (xml.getElementsByTagName('error') && xml.getElementsByTagName('error').length != 0)
					{
						err = xml.getElementsByTagName('error')[0].childNodes[0].nodeValue;
						message(err, true);
						clearTimeout(timer_in);

						setTimeout('reload_post();',500);
					}
					else
					{
						var nr = xml.getElementsByTagName('nr')[0].childNodes[0].nodeValue;
						var f = document.getElementById('nr');
						f.innerHTML = '';
						var d = ce('div');
						
						if (nr < 20)
						{
							return;
						}
						
						var per_page = 20;
						
						var total_pages = Math.ceil(nr / per_page);
						
						if (total_pages == 1 || !nr)
						{
							return;
						}				

						on_page = Math.floor(count / per_page) + 1;
						
						var p = ce('span');
						var a = ce('a');
						var b = ce('strong');
						
						if (on_page == 1)
						{
							b.appendChild(tn('1'));
							p.appendChild(b);
							b = ce('strong');
						}
						else
						{
							a.c = ((on_page - 2) * per_page);
							a.href = 'javascript:;';
							a.onclick = function()
							{
								count = this.c;
								last = 0; // Reset last, otherwise it will not be loaded.
								clearTimeout(timer_in);
								reload_post();
								reload_page();
							}
							
							a.appendChild(tn('Previous'));
							
							p.appendChild(a);
							
							p.appendChild(tn(' '));
							
							a = ce('a');						
						
							a.c = 0;
							a.href = 'javascript:;';
							a.onclick = function()
							{
								count = this.c;
								last = 0; // Reset last, otherwise it will not be loaded.
								clearTimeout(timer_in);
								reload_post();
								reload_page();
							}
							
							a.appendChild(tn('1'));
							
							p.appendChild(a);
							a = ce('a');
						} 
						
						if (total_pages > 5)
						{
							var start_cnt = Math.min(Math.max(1, on_page - 4), total_pages - 5);
							var end_cnt = Math.max(Math.min(total_pages, on_page + 4), 6);
							
							p.appendChild((start_cnt > 1) ? tn(' ... ') : cp());
							
							for (var i = start_cnt + 1; i < end_cnt; i++)
							{
								if (i == on_page)
								{
									b.appendChild(tn(i));
									p.appendChild(b);
									b = ce('strong');	
								}
								else
								{
									a.c = (i - 1) * per_page;
									a.href = 'javascript:;';
									a.onclick = function()
									{
										count = this.c;
										last = 0; // Reset last, otherwise it will not be loaded.
										clearTimeout(timer_in);
										reload_post();
										reload_page();
									}
									
									a.appendChild(tn(i));
									
									p.appendChild(a);
									a = ce('a');			
								}
								if (i < end_cnt - 1)
								{
									p.appendChild(cp());
								}
							}
							
							p.appendChild((end_cnt < total_pages) ? tn(' ... ') : cp());							
						}
						else
						{
							p.appendChild(cp());
							for (var i = 2; i < total_pages; i++)
							{
								if (i == on_page)
								{
									b.appendChild(tn(i));
									p.appendChild(b);
									b = ce('strong');	
								}
								else
								{
									a.c = (i - 1) * per_page;
									a.href = 'javascript:;';
									a.onclick = function()
									{
										count = this.c;
										last = 0; // Reset last, otherwise it will not be loaded.
										clearTimeout(timer_in);
										reload_post();
										reload_page();
									}
									
									a.appendChild(tn(i));
									
									p.appendChild(a);
									a = ce('a');			
								}
								if (i < total_pages)
								{
									p.appendChild(cp());
								}
							}						
						}
						
						if (on_page == total_pages)
						{
							b.appendChild(tn(total_pages));
							p.appendChild(b);
							b = ce('strong');
						}
						else
						{
							
							a = ce('a');						
						
							a.c = ((total_pages - 1) * per_page);
							a.href = 'javascript:;';
							a.onclick = function()
							{
								count = this.c;
								last = 0; // Reset last, otherwise it will not be loaded.
								clearTimeout(timer_in);
								reload_post();
								reload_page();
							}
							
							a.appendChild(tn(total_pages));
							
							p.appendChild(a);
							a = ce('a');

							a.c = ((on_page) * per_page);
							a.href = 'javascript:;';
							a.onclick = function()
							{
								count = this.c;
								last = 0; // Reset last, otherwise it will not be loaded.
								clearTimeout(timer_in);
								reload_post();
								reload_page();
							}
							
							a.appendChild(tn('Next'));
							
							p.appendChild(tn(' '));
							
							p.appendChild(a);
							a = ce('a');
						}						
						
						f.appendChild(p);
					}
				}
			}
			catch (e)
			{
				handle(e);
				return;
			}
		}
		hnr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

		hnr.send(null);
	}
}
function reload_post()
{
	// If there is a open edit field, we dont reload.
	// If we reload, this edit field will be closed, and data lost.
	if (one_open)
	{
	    return;
	}
	// First check if there new posts.
	if (hin2.readyState == 4 || hin2.readyState == 0)
	{
		hin2.open('GET','./ajax.php?m=check&sid=6b7db4075d733cab99de154b032c4c59&last=' + last + '&rand='+Math.floor(Math.random() * 1000000),true);
		hin2.onreadystatechange = function()
		{
			try
			{
		        if (hin2.readyState != 4)
		        {
		            return;
				}
				if (hin2.readyState == 4)
				{
					if (!hin2.responseXML)
					{
						throw err_msg('XML error.');
					}
					var xml = hin2.responseXML;		
					if (xml.getElementsByTagName('error') && xml.getElementsByTagName('error').length != 0)
					{
						err = xml.getElementsByTagName('error')[0].childNodes[0].nodeValue;
						throw err_msg(err);
						return;
					}
					
					var t = xml.getElementsByTagName('time')[0].childNodes[0].nodeValue;
					if (t == '0')
					{
						// If start is true, we let notice that there are no messages
						if (start == true)
						{
														if (first)
							{
								document.getElementById('post_message').style.display = 'inline';
								first = false;
							}
													
							var posts = document.getElementById('msg');
							posts.innerHTML = '';
							posts.appendChild(tn('There are no messages.'));
						}
					}
					else
					{
						if (hin.readyState == 4 || hin.readyState == 0)
						{
							last = xml.getElementsByTagName('last')[0].childNodes[0].nodeValue;
							// Lets got some nice things :D
							hin.open('GET','./ajax.php?m=view&sid=6b7db4075d733cab99de154b032c4c59&start=' + count + '&rand='+Math.floor(Math.random() * 1000000),true);
							hin.onreadystatechange = function()
							{
								try
								{
							        if (hin.readyState != 4)
							        {
							            return;
									}
									if (hin.readyState == 4)
									{
										if (!hin.responseXML)
										{
											throw err_msg('XML error.');
											return;
										}
										var xml = hin.responseXML;
										if (xml.getElementsByTagName('error') && xml.getElementsByTagName('error').length != 0)
										{
											var msg = xml.getElementsByTagName('error')[0].childNodes[0].nodeValue;
											throw err_msg(msg);
											return;
										}
										else
										{
											start = false;
											var tmp = xml.getElementsByTagName('posts');
											if (tmp.length == 0)
											{
																								if (first)
												{
													document.getElementById('post_message').style.display = 'inline';
													first = false;
												}
																							
												var posts = document.getElementById('msg');
												posts.innerHTML = '';
												posts.appendChild(tn('There are no messages.'));
												setTimeout('reload_post();',5000);
												return;
											} 
											var posts = document.getElementById('msg');
											posts.innerHTML = '';
													
											var row = false;
											for (var i = 0; i < tmp.length ; i++)
											{
												var li = ce('li');
												li.className = (!row) ? 'row row1' : 'row row2';
												row = !row;
												
												var dl = ce('dl');
												var dd = ce('dd');
												var dt = ce('dt');
												var inh = tmp[i];
												dt.style.width = '15%';
												dt.style.styleFloat = dt.style.cssFloat = 'left';
												dd.style.styleFloat = dd.style.cssFloat = 'left';

												dd.style.paddingLeft = '3px';

												var s = ce('span');

												var msg = parse_xml_to_html(inh.getElementsByTagName('shout_text')[0]);
												

												dt.appendChild(parse_xml_to_html(inh.getElementsByTagName('shout_time')[0]));
												dt.appendChild(tn(' | '));

												dt.appendChild(parse_xml_to_html(inh.getElementsByTagName('username')[0]));

												dt.appendChild(tn(': '));

												dl.appendChild(dt);
												
																								var msg2 = ce('span');
												msg2.id = 'shout' + i;
												msg2.i = i;
												msg2.appendChild(msg);
												var form = ce('span');
																								
												dd.appendChild(msg2);
												dd.appendChild(form);
												dd.id = 'msgbody';

												dl.appendChild(dd);
												li.appendChild(dl);
												posts.appendChild(li);

											}

																						if (first)
											{
												document.getElementById('post_message').style.display = 'inline';
												first = false;
											}
																					}
									}
								}
								catch (e)
								{
									timer_in = setTimeout('reload_post();',5000);
									handle(e);
									return;
								}
							}
							hin.send(null);
						}
					}
					timer_in = setTimeout('reload_post();',5000);
				}
			}
			catch (e)
			{
				handle(e);
				return;			
			}			
		}
		hin2.send(null);
	}
}

lang['COMMA_SEPARATOR'] = ', ';
lang['NO_AJAX'] = 'No ajax';
