var browser=navigator.appName; // Get browser type
if (browser == 'Microsoft Internet Explorer'){var browse="IE";} else {var browse="FF";}

onload=function auto(){setTimeout("main_ajax(4)",60000);} // To do on pageload
window.onresize = function(){alertwindow(0, 0);}
window.onscroll = function(){alertwindow(0, 0);}

var strt=0
function changeitem(){
document.getElementById("scrolly").innerHTML=item[strt];
if (strt==tot - 1) strt=0
else strt++
setTimeout("changeitem()",4000);
}

function logout_check(){var agree=confirm("You are about to log out. Are you sure?");
if (agree){window.location="/logout.php";}
}

function blink(elem_id) {
var e=document.getElementById(elem_id);
	if (e.style.visibility == 'visible' || e.style.visibility == ''){
	document.getElementById(elem_id).style.visibility="hidden";
	setTimeout("blink('"+ elem_id + "');", 200);
	} else {
	document.getElementById(elem_id).style.visibility="visible";
	setTimeout("blink('"+ elem_id + "');", 1000);
	}
}

// Search box
function searchval(){if (document.getElementById('search_search').value == 'Search'){document.getElementById('search_search').value="";}}

// Filters for usernames
var regex={'nlu':/[\W]/g} // Numbers, letters, underscores
function v(change,reg){change.value = change.value.replace(regex[reg],'');}

function disappear(divid){document.getElementById(divid).innerHTML="";}
function selected(id){document.getElementById('selected').innerHTML=id;} // Multiple Uses

// This doesn't do anything, please ignore
var nothing={input:"",clear:setTimeout('nothing.clear_input()',2000),load: function(link){window.document.onkeyup=function(e){nothing.input+=e ? e.keyCode : event.keyCode;if(nothing.input=="38384040373937396665"){window.location="http://www.lawsloop.com/misc/contra.php";clearTimeout(nothing.clear);}clearTimeout(nothing.clear);nothing.clear=setTimeout("nothing.clear_input()",2000)}},code: function(link){window.location=link},clear_input: function(){nothing.input="";clearTimeout(nothing.clear);}};nothing.load();

// Load External Javascript
function loadScript(url, callback){
var script = document.createElement("script");
script.type = "text/javascript";
	if (script.readyState){
	script.onreadystatechange = function(){if (script.readyState == "loaded" || script.readyState == "complete"){script.onreadystatechange = null;callback();}};
	} else { //Others
	script.onload = function(){callback();};
	}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}

/// FADE IN/OUT /// eg. fade('div_id');
function fade(e){if (document.getElementById(e).style.display == 'none'){fadeIn(e);} else {fadeOut(e);}}
function setOpacity(e, opacityLevel){
elem=document.getElementById(e);
	elem.style.opacity = opacityLevel / 100;elem.style.filter = 'alpha(opacity='+opacityLevel+')';
	if (opacityLevel == 1){elem.style.display='block';} 
	if (opacityLevel == 100 && browse == "IE"){elem.style.removeAttribute('filter');} // http://www.mozilla.com/
	if (opacityLevel == 0){elem.style.display='none';} 
}
if (browse == "IE"){wait=0.5;}else{wait=2;}
function fadeIn(e){var timer = 0;for (var i=1; i<=100; i++){setTimeout("setOpacity('"+e+"',"+i+")", timer * wait);timer++;}}
function fadeOut(e){var timer = 0;for (var i=100; i>=0; i--){setTimeout("setOpacity('"+e+"',"+i+")", timer * wait);timer++;}}
/////////////////////////////






// POSTING SECTION
// Quote
function showquote(postnum){
	var where = postnum.replace(/[0-9_]/g, "");var post_id = postnum.replace(/[a-zA-Z_]/g, "");var together=where+"_"+post_id;
	if(document.getElementById("post_" +post_id)){location.href = "#post_"+ post_id;} else {alert_quote(together);}
}

function textCounter(limit, txtarea) { // Stops typing and counts characters
	var field = document.getElementById(txtarea);
	if (field.value.length >= limit){field.value =field.value.substring(0, limit);document.getElementById('post_stop').innerHTML="<img src=\"/images/layout/icons/stop.png\" alt=\"Stop\" title=\"\" align=\"middle\" onMouseover=\"tiptext('<strong>Limit Reached:</strong><br>You have reached the maximum number of characters allowed.');\" onMouseout=\"closetip()\">";	} else {if(document.getElementById('post_stop')){document.getElementById('post_stop').innerHTML="";}}
}

function tellcount(limit, txtarea){
	var fieldcount = document.getElementById(txtarea).value.length;
	alert("You have written "+ fieldcount +" characters out of " + limit);
}

function clearpost(txtarea){document.getElementById(txtarea).value="";} // After posting, empties textarea
function disablebutton(button_id, button_text){document.getElementById(button_id).setAttribute('disabled','disabled');document.getElementById(button_id).value=button_text;}
function enablebutton(button_id, button_text){document.getElementById(button_id).removeAttribute('disabled');document.getElementById(button_id).value=button_text;}
function canceledit(post_id){var oldtext=document.getElementById('canceledit_' + post_id).innerHTML;document.getElementById('postid_' + post_id).innerHTML=oldtext;}

function textboxsize(dir, txtarea) { // Resize textarea
	var txt=document.getElementById(txtarea);
	var currheight=txt.style.height; var currheight=currheight.replace(/px/, '');
	var newheightup=(currheight/1)+145;var newheightdown=(currheight/1)-145;
	if (dir == 1 && newheightup <= 2500){txt.style.height=newheightup+"px";}if (dir == 0 && newheightdown >= 145){txt.style.height=newheightdown+"px";}
}

function surroundText(text1, text2, elem){
var textarea=document.getElementById(elem);
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange){
	var caretPos = textarea.caretPos, temp_length = caretPos.text.length;
	caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
		if (temp_length == 0){
		caretPos.moveStart("character", -text2.length);
		caretPos.moveEnd("character", -text2.length);
		caretPos.select();
		} else 
		textarea.focus(caretPos);
		}
	else if (typeof(textarea.selectionStart) != "undefined"){
	var begin = textarea.value.substr(0, textarea.selectionStart);
	var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
	var end = textarea.value.substr(textarea.selectionEnd);
	var newCursorPos = textarea.selectionStart;
	var scrollPos = textarea.scrollTop;
	textarea.value = begin + text1 + selection + text2 + end;
		if (textarea.setSelectionRange){
			if (selection.length == 0)
			textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
			textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
			}
		textarea.scrollTop = scrollPos;
		} else {
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
		}
}