var currentmain;
var currentaob;
var currentroom;
var flashcount = 0;

function submitenter(myfield,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (keycode == 13)
	{
		//return false;
		submit_chat();
		return false;
	}
	else return true;
}

function reload_messaging()
{
	AjaxRequest.get(
	{
		'url':'/bdna/messaging.htm'
		,'onSuccess':function(req)
		{ 
			
			document.getElementById("messagingdiv").innerHTML = req.responseText;
		}
	});
}

function peek(id)
{
	document.getElementById("peek_"+id).style.display="block";
}
function hide(id)
{
	document.getElementById("peek_"+id).style.display="none";	
}

function submit_captcha(theform)
{
	AjaxRequest.submit(theform,{'onSuccess':function(req)
	{ 
		if(req.responseText=="fail")
		{
			alert("Sorry, you didn't enter the data correctly!");
		}
		else
		{
			alert("Thanks for validating! Please continue to submit your message!");
			captcha_div = document.getElementById('captchadiv');
			captcha_div.style.display="none";
		}
	}});
	return false;
}

function submit_chat()
{
	
	AjaxRequest.get(
	{
		'url':'/bdna/captchavalidated.htm'
		,'onSuccess':function(req_one)
		{ 	
			 
			if(req_one.responseText=="validated") 
			{ 
				var theform = document.getElementById("commentform");
				if(theform.name.value=="") 
				{
					alert("Please enter your name :)");
					return false;			
				}
				if(theform.message.value=="")
				{
					alert("Please enter some kind of message before submitting.");
					return false;
				}
				AjaxRequest.submit(theform,{'onSuccess':function(req)
				{ 
					
					reload_chat();
					theform.message.value="";
					theform.message.focus();
					theform.type.selectedIndex=0;
				}});
			}
			else
			{
				
				alert("You will now be asked to complete a quick SPAM protection question.\n\nYou will only be asked this once and will help to stop SPAM from being posted on the chat room!");
				var captcha_div = document.getElementById('captchadiv');
				captcha_div.style.display="block";
					
			}
		}
	});
	return false;
	
	
	
}

function reload_home()
{
	AjaxRequest.get(
	{
		'url':'/bdna/latesthome.htm'
		,'onSuccess':function(req)
		{ 	
			var pieces = req.responseText.split("[my_split]");
			document.getElementById('latestdiv').innerHTML = pieces[0]; 
			document.getElementById('communitydiv').innerHTML = pieces[1]; 
		}
	});
}

function reload_chat()
{
	//console.log("starting");
	AjaxRequest.get(
	{
		'url':'/bdna/latestchat.htm'
		,'onSuccess':function(req)
		{ 
			//console.log("done");
			var pieces = req.responseText.split("[my_split]");
			var topbit = pieces[0];
			var timeout = pieces[1];
			var boot = pieces[2];
			var lastmain = pieces[3];
			var lastaob = pieces[4];
			var shoutouts = pieces[5];
				
			document.getElementById('chatdiv').innerHTML = shoutouts; 
			document.getElementById('chattop').innerHTML = topbit;
			
			if(boot=="1")
			{
				if(document.location.href=="http://www.lambwatch.co.uk/chat.htm") document.location.href="/"; 
				else window.close();
			}
			
			if(parseInt(lastmain)!=parseInt(currentmain)&&currentroom=="aob")
			{
				// flash main
				hidebox("main");
			}
			
			if(parseInt(lastaob)!=parseInt(currentaob)&&currentroom=="main")
			{
				// flashaob	
				hidebox("aob");
			}
			
			currentmain = lastmain;
			currentaob = lastaob;
		}

	});
}

function hidebox(id)
{
	flashcount++;
	document.getElementById(id+"roombox").style.visibility="hidden";
	setTimeout("showbox('"+id+"')",500);
}
function showbox(id,hideit)
{
	document.getElementById(id+"roombox").style.visibility="visible";
	if(flashcount<3) setTimeout("hidebox('"+id+"')",500);
	else flashcount=0;
}

function submit_contact(theform)
{
	
	AjaxRequest.get(
	{
		'url':'/bdna/captchavalidated.htm'
		,'onSuccess':function(req_one)
		{ 	
			 
			if(req_one.responseText=="validated") 
			{ 
				var name = theform.name.value;
				var message = theform.message.value;
				var subject = theform.subject.value;
				var msg = "";
			
				if(subject=="View Request") msg = "We will try our best to accommodate your viewing preferences! :)";
				else if(subject=="Sighting") msg = "Thank you for submitting your sighting!\n\nThey help us to keep track of events in the area!";
				else msg = "We appreciate your message!";
				
				if(name=="") 
				{
					alert("Please enter your name :)");
					return false;			
				}
				if(message=="")
				{
					alert("Please enter some kind of message before submitting.");
					return false;
				}
				AjaxRequest.submit(theform,{'onSuccess':function(req){
					theform.message.value="";
					alert("Thank you!\n\n"+msg);
					theform.message.focus();		
				}});
				return false;
			}
			else
			{
				
				alert("You will now be asked to complete a quick SPAM protection question.\n\nYou will only be asked this once and will help to stop SPAM from being sent!");
				var captcha_div = document.getElementById('captchadiv');
				captcha_div.style.display="block";
				
				return false;
			}
		}
	});
	return false;
	
}

function change_background(id)
{	
	AjaxRequest.get(
	{
		'url':'/bdna/togglebackground.htm?action=set&id='+id
		,'onSuccess':function(req)
		{ 
			
			document.getElementById("container").style.backgroundImage = "url(/img/back/options/"+req.responseText+".jpg)";
		}
	});
}

function clear_background()
{
	AjaxRequest.get(
	{
		'url':'/bdna/togglebackground.htm?action=clear'
		,'onSuccess':function(req)
		{ 
			document.getElementById("container").style.backgroundImage = "url(/img/back/"+actual_background+".jpg)";
		}
	});	
}

function reload_weather() 
{
	AjaxRequest.get(
	{
		'url':'/bdna/weather.htm'
		,'onSuccess':function(req)
		{ 
			var splits = (req.responseText).split("[my_split]");
			document.getElementById("weather_box").innerHTML = splits[0];
			document.getElementById("css_link").href = "/css/"+splits[1]+".css";
		}
	});	
}

function submit_entity_comment(theform)
{
	var name = theform.comment_name.value;
	var message = theform.comment_message.value;

	if(name=="") 
	{
		alert("Please enter your name :)");
		return false;			
	}
	if(message=="")
	{
		alert("Please enter some kind of comment before submitting.");
		return false;
	}
	var thediv = document.getElementById("bdnabox");
	AjaxRequest.submit(theform,{'onSuccess':function(req){
		thediv.innerHTML = req.responseText;
	}});
	return false;
}

function form_is_complete(form)
{
	for (i=0; i<form.length;i++)
	{
    	if(form[i].className=="req"&&form[i].value=="")
		{
			alert("Please complete form field: "+form[i].name);
			return false;
		}
	}	
	return true;	
}

function forum_logout()
{
	AjaxRequest.get(
	{
		'url':'/forum.htm?action=logout'
		,'onSuccess':function(req)
		{ 
			document.location.href="/forum.htm";
		}
	});	
}


function check_new_forum_post(theform)
{
	if(theform.title.value=="")
	{
		alert("Please specify a topic title!");
		return false;
	} 
	if(theform.post_content.value=="")
	{
		//alert("Please enter some content for your forum post!");
		//return false;
	}
	return true;
}

function cam_popup()
{
	var imagediv = document.getElementById("popup_alert_div");
	var streamdiv = document.getElementById("live_stream_div");
	if(imagediv) imagediv.style.display = "block";
	if(streamdiv) streamdiv.style.display = "none"; 
	window.open ("/popup.htm","campopup","location=0,status=0,resizable=0,scrollbars=0,width=385,height=360"); 
}
function chat_popup()
{
	window.open ("/chatpopup.htm","chatpopup","location=0,status=0,resizable=0,scrollbars=0,width=460,height=532"); 
}
function show_chat_ignore_list()
{
	window.open ("/chatignores.htm","chatignores","location=0,status=0,resizable=0,scrollbars=0,width=340,height=390"); 
}
function open_popup()
{
	var url = "/bdna/popupcontroller.htm?session=start";
	AjaxRequest.get({'url':url,'onSuccess':function(req){ }});
}

function close_popup()
{
	var url = "/bdna/popupcontroller.htm?session=stop";
	AjaxRequest.get({'url':url,'onSuccess':function(req){ }});
}

function ignore_user(sessionid)
{
	var proceed = confirm("Are you sure you wish to ignore this person?");
	if(proceed)
	{
		var url = "/bdna/removecomment.htm?subaction=ignore&sessionid="+sessionid;
		AjaxRequest.get({'url':url,'onSuccess':function(req)
														{
															//alert(req.responseText);
															reload_chat();
														}});
	}
}

function unignore_ip(sessionid)
{
	var proceed = confirm("Are you sure you wish to unignore this person?\n\nThis means you will see their messages once again.");
	if(proceed)
	{
		var url = "/bdna/removecomment.htm?subaction=unignore&sessionid="+sessionid;
		AjaxRequest.get({'url':url,'onSuccess':function(req){
														document.getElementById(sessionid).style.display="none";
														}});
	}
}
				 

function restore_live_stream()
{
	close_popup();
	document.location.href = document.location.href; 
}
