//copyright © 2010 Taiga Software LLC, Samuel Groves - all rights reserved

var taigapostregex = /^\/(post|private)\//;
var taigahostregex = /^http:\/\/([^\/]+).*/i;
var taigaintregex = /.*[^\d](\d+)\.html$/;
var taigaposthumanregex = new RegExp('<(?:div|DIV) id="([^"]+\/([^"\/]+)\\.[^"]*)" class="post">');
var taigaposthumanformregex = new RegExp('[^"]+\/([^"\/]+)\\.[^"]*');
var taigaprofilefields = new Array();
var splitstring = '<!--taiga-->';
var taigaajax = '0';
var taigapollinterval = 60000;
//taigapollinterval = 5000;
var taigainjectionfields = [ 'newmessagecount', 'newpostcount', 'totalposts', 'totaltopics', 'totalregisteredusers', 'userssection' ];
var taigaattachments = new Array();
var taigacurrentlyuploadingattachments = false;
var taigatemppostext = '';
var taigacurrentlydeletingpost = '0';
var taigafirstajax = '1';
var taigacurrentlyeditingpost = 0;
var taigainunreadview = false;
var taigamessagequeue = new Array();
var taigaforceposthuman = '';
var taigaajaxget = '1';
var taigaredirecting = '0';
var taigashorthost = '';
var taigaunreadhash = '';
var taigadeletingpost = '0';
var taigathisisthelastpage = false;
var taigaautosaveinterval = 10000;
var taigainputext = '  taigainitialvalue';
var taigajumptopost = '';
var taigaselectedavatar = '';
var taigaajaxtimeouti = 0;
var taigaajaxtimeout = (5 * 60);
var taigagetnewpostsajax;
var taigatitle = document.title;
var taigablurred = false;
var taiganewposts = 0;
var taigaonbeforeunloadalreadysaved = false;
var taiganewpostlinks = new Array();
var taigaloggedinuid = '0';
var taigalastextarea = 'posttextarea';
var taigamainbarorder = '';
var taigajustchangedorderbool = false;
var taigafirsttimeinitialsetting = 2;
var taigafirsttime = taigafirsttimeinitialsetting; //number of times to check for new posts before reloading unread view if there are new posts

function taigaNewAjax() {
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	
	return xmlhttp;
}

//from http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                                     
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}


//from http://snipplr.com/view/1696/get-elements-by-class-name/
function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}



function taigastyleswitchsupplicant(taigaclass, taigasourceclass, taigatargetclass) {
	var taigatoswitch = getElementsByClassName(taigaclass+taigasourceclass);
	var taigathings = new Array();
	for (var i = 0; i < taigatoswitch.length; i++) {
		taigathings.push(taigatoswitch[i]);
	}
	for (var i = 0; i < taigathings.length; i++) {
		taigathings[i].className=taigaclass+taigatargetclass;
	}
}

function taigastyleswitch() {
	var taigaatbottom = taigaloc();
	for (var arg = 0; arg < arguments.length; arg++) {
		var taigaclass = arguments[arg];
		taigastyleswitchsupplicant(taigaclass, '_hide', 'taigaintermediateclass');
		taigastyleswitchsupplicant(taigaclass, '_show', '_hide');
		taigastyleswitchsupplicant(taigaclass, 'taigaintermediateclass', '_show');
	}
	taigascroll(taigaatbottom);
}

function taigastyleswitchnoscroll() {
	for (var arg = 0; arg < arguments.length; arg++) {
		var taigaclass = arguments[arg];
		taigastyleswitchsupplicant(taigaclass, '_hide', 'taigaintermediateclass');
		taigastyleswitchsupplicant(taigaclass, '_show', '_hide');
		taigastyleswitchsupplicant(taigaclass, 'taigaintermediateclass', '_show');
	}
}

function taigastyleswitchid(taigaid) {
	//first hide everything else - have to hardcode pane names
	var taigapaneclassnames = ['newcategory', 'newboard', 'addboardmod', 'adduser', 'addgroup', 'usergroup', 'groupperms'];
	for (var arg = 1; arg < arguments.length; arg++) {
		var taigaclass = arguments[arg];
		for (var c = 0; c < taigapaneclassnames.length; c++) {
			var taigaswitchclass = taigapaneclassnames[c];
			var taigathings = getElementsByClassName(taigaswitchclass+'_show');
			//alert(taigaswitchclass+'_show');
			for (var i = 0; i < taigathings.length; i++) {
				if (taigathings[i].id != taigaclass+'_'+taigaid) { //don't hide the one we clicked on or else it will be shown again in a moment
					taigathings[i].className = taigaswitchclass+'_hide';
				}
			}
		}
		taigastyleswitchidsupplicant(taigaid, taigaclass);
	}
}

function taigastyleswitchidsupplicant(taigaid, taigaclass) {
	var taigathing = document.getElementById(taigaclass+'_'+taigaid);
	if (!taigathing) {
		//alert('error: could not find id '+taigaclass+'_'+taigaid);
	} else {
		taigathing.className=(taigathing.className==taigaclass+'_hide' ? taigaclass+'_show' : taigaclass+'_hide');
	}
}

function taigastyleswitchidshow(taigaid, taigaclass) {
	var taigathing = document.getElementById(taigaclass+'_'+taigaid);
	if (!taigathing) {
		//alert('error: could not find id '+taigaclass+'_'+taigaid);
	} else {
		taigathing.className=taigaclass+'_show';
	}
}

function taigastyleswitchidhide(taigaid, taigaclass) {
	var taigathing = document.getElementById(taigaclass+'_'+taigaid);
	if (!taigathing) {
		//alert('error: could not find id '+taigaclass+'_'+taigaid);
	} else {
		taigathing.className=taigaclass+'_hide';
	}
}

function taigastyleswitchid2(taigaid) { //do not use this unless you know what you are doing - currently only used for editing posts and checking ips?
	for (var arg = 1; arg < arguments.length; arg++) {
		var taigaclass = arguments[arg];
		var taigathingtohide = document.getElementById(taigaclass+'_show_'+taigaid);
		var taigathingtoshow = document.getElementById(taigaclass+'_hide_'+taigaid);
		
		if (taigathingtohide) {
			taigathingtohide.className=taigaclass+'_hide';
			taigathingtohide.id=taigaclass+'_hide_'+taigaid;
		}
		
		if (taigathingtoshow) {
			taigathingtoshow.className=taigaclass+'_show';
			taigathingtoshow.id=taigaclass+'_show_'+taigaid;
		}
	}
}

function taigastyleswitchid2show(taigaid) { //do not use this unless you know what you are doing - currently only used for editing posts and checking ips?
	for (var arg = 1; arg < arguments.length; arg++) {
		var taigaclass = arguments[arg];
		var taigathingtoshow = document.getElementById(taigaclass+'_hide_'+taigaid);
		
		if (taigathingtoshow) {
			taigathingtoshow.className=taigaclass+'_show';
			taigathingtoshow.id=taigaclass+'_show_'+taigaid;
		}
	}
}

function taigastyleswitchid2hide(taigaid) { //do not use this unless you know what you are doing - currently only used for editing posts and checking ips?
	for (var arg = 1; arg < arguments.length; arg++) {
		var taigaclass = arguments[arg];
		var taigathingtoshow = document.getElementById(taigaclass+'_show_'+taigaid);
		
		if (taigathingtoshow) {
			taigathingtoshow.className=taigaclass+'_hide';
			taigathingtoshow.id=taigaclass+'_hide_'+taigaid;
		}
	}
}

function taiganewpmto(taiganewpmto) {
	document.getElementById('ajaxposttarget').innerHTML='<form name="newpmto" action="/private/messages.html" method="POST"><input type="hidden" name="newpmto" value="'+taiganewpmto+'" /></form>';
	document.newpmto.submit();
}

function taigadeletecatfor(taigaid) {
	var taigacode = taigaid.substring(0,1);
	var catboard = 'board';
	if ('c' == taigacode) {
		catboard = 'category';
	}
	if (confirm(lang__delete_category_or_forum_confirmation_warning)) {
		var taigaParams = 'taiga=admin&adminparams='+urlencode('deletecatfor='+taigaid);
		var xmlhttp = taigaNewAjax();
		xmlhttp.open("POST", document.location.pathname, true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//xmlhttp.setRequestHeader("Content-length", taigaParams.length);
		//xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				taigastyleswitch('ajaxloading');
				if (xmlhttp.status == 200) {
					if (xmlhttp.responseText == 'OK') {
						document.location=document.location.pathname;
					} else {
						alert(xmlhttp.responseText);
					}
				} else {
					alert(lang__sorry_an_error_occurred_please_try_your_request_again);
				}
			}
		}
		taigastyleswitch('ajaxloading');
		xmlhttp.send(taigaParams);
	}
}

function taigaeditcatfor(taigaid, taigafunc) {
	var taigacode = taigaid.substring(0,1);
	var catboard = 'board';
	if (  ('c' == taigacode) && ( (taigafunc != 'new') || (taigaid == 'cnew') )  ) { //new forum pane has the cid in it in which we are making the new forum
		catboard = 'category';
	}
	var taiganame = document.getElementById('new'+catboard+'nameinput_'+taigaid).value;
	if (taiganame == '') {
		alert(lang__please_enter_a_name);
		return false;
	}
	var taigaParams = 'taiga=admin&adminparams='+urlencode('editcatfor='+taigaid+'&name='+taiganame);
	if (!  ( ('c' == taigacode) && ( (taigafunc != 'new') || (taigaid == 'cnew') ) )  ) { //new forum pane has the cid in it in which we are making the new forum
		taigaParams += urlencode(
					'&desc='+document.getElementById('new'+catboard+'descriptioninput_'+taigaid).value
					+'&subboard='+(document.getElementById('new'+catboard+'subboardinput_'+taigaid).checked == true ? 1 : 0)
					);
	}
	//alert(taigaParams);
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					//if ('new' != taigafunc) {
					//	taigastyleswitchid(taigaid, 'new'+catboard);
					//	document.getElementById('current'+catboard+'name_'+taigaid).innerHTML=taiganame;
					//} else {
						document.location=document.location.pathname;
					//}
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigastyleswitchshow() {
	var taigaatbottom = taigaloc();
	for (var arg = 0; arg < arguments.length; arg++) {
		var taigaclass = arguments[arg];
		taigastyleswitchsupplicant(taigaclass, '_hide', '_show');
	}
	taigascroll(taigaatbottom);
}

function taigastyleswitchshownoscroll() {
	for (var arg = 0; arg < arguments.length; arg++) {
		var taigaclass = arguments[arg];
		taigastyleswitchsupplicant(taigaclass, '_hide', '_show');
	}
}

function taigastyleswitchhide() {
	var taigaatbottom = taigaloc();
	for (var arg = 0; arg < arguments.length; arg++) {
		var taigaclass = arguments[arg];
		taigastyleswitchsupplicant(taigaclass, '_show', '_hide');
	}
	taigascroll(taigaatbottom);
}

function taigastyleswitchhidenoscroll() {
	for (var arg = 0; arg < arguments.length; arg++) {
		var taigaclass = arguments[arg];
		taigastyleswitchsupplicant(taigaclass, '_show', '_hide');
	}
}

function taigahideotherssupplicant(taigaclass, taigaclasses) {
	var taigaatbottom = taigaloc();
	
	//first hide everything in taigaclasses
	for (var i = 0; i < taigaclasses.length; i++) {
		var taigatochange = getElementsByClassName(taigaclasses[i]+"_show");
		for (var j = 0; j < taigatochange.length; j++) {
			taigatochange[j].className=taigaclasses[i]+"_hide";
		}
	}
	//then show the requested taigaclass
	taigastyleswitchnoscroll(taigaclass);
	
	//taigascroll(taigaatbottom);
}

function taigahideothersusers(taigaclass, taigasort) {
	var taigaParams = 'taiga=view&usersclass='+taigaclass+'&usersort='+taigasort;
	//alert(taigaParams);
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				//var taigamessages = xmlhttp.responseText.split("\n");
				//taigamessages.reverse();
				//if (taigamessages[0] == 'OK') {
					document.getElementById('users_content').innerHTML=xmlhttp.responseText;
					var taigaclasses = new Array('byusername', 'bydateregged', 'byposition', 'bypostcount');
					taigahideotherssupplicant(taigaclass, taigaclasses);
					
					taigastyleswitchhide('asc', 'desc');
					if ('topfirst' == taigasort) {
						taigastyleswitchshow('desc');
					} else {
						taigastyleswitchshow('asc');
					}
				//} else {
				//	alert(xmlhttp.responseText);
				//}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigahideothersgroupscat(taigaclass) {
	var taigaclasses = new Array('byusername', 'bydateregged', 'byposition', 'bypostcount');
	taigahideotherssupplicant(taigaclass, taigaclasses);
}

function taigaloc() {
	return (f_scrollTop() + document.documentElement.clientHeight >= document.getElementById('wholepagecontainer').scrollHeight);
}

function taigascrolltobottom() {
	window.scroll(0,document.getElementById('wholepagecontainer').scrollHeight);
}

function taigascroll(taigaatbottom) {
// 	if (taigaatbottom) {
// 		setTimeout('taigascrolltobottom()', 600);
// 	}
	if (taigaatbottom) {
		taigascrolltobottom();
	}
}

// function taigacomparenumbers(a, b) {
// 	var x = a.replace(taigaintregex, "$1");
// 	var y = b.replace(taigaintregex, "$1");
// 	//fixme first post is not always first? will only fix on actual report of error by first post not being first
// 	var retval = (x - y);
// 	//alert('returning '+retval);
// 	return retval;
// }

function taigasubmitpost() {
	document.getElementById('posttextarea').focus();
	
	if (taigacurrentlyuploadingattachments == true) {
		alert(lang__you_must_wait_until_your_attachments_finish_uploading_to_post);
		return false;
	}
	
	var rm = "post";
	
	var taigatopictitle = '';
	if (document.getElementById('posttitlearea')) {
		if (document.getElementById('posttitlearea').value == '') {
			alert(lang__please_enter_a_topic_title);
			return false;
		}
		
		var taigastickyannouncement = '3'; //default - normal topic
		if ( document.getElementById('normalnewtopic') && (document.getElementById('normalnewtopic').checked == true) ) {
			taigastickyannouncement = '3';
		} else if (document.getElementById('stickynewtopic') && (document.getElementById('stickynewtopic').checked == true) ) {
			taigastickyannouncement = '2';
		} else if (document.getElementById('announcenewtopic') && (document.getElementById('announcenewtopic').checked == true) ) {
			taigastickyannouncement = '1';
		}
		
		taigatopictitle =
			'&title='+urlencode(document.getElementById('posttitlearea').value)
			+'&stickyannouncement='+taigastickyannouncement
		;
		rm = "topic";
	}
	
	var taigaan = '';
	if (document.getElementById('postrecipientarea')) {
		if (document.getElementById('postrecipientarea').value == '') {
			alert(lang__please_enter_a_recipient);
			return false;
		}
		taigaan =
			'&an='+urlencode(document.getElementById('postrecipientarea').value)
		;
	}
	
	var taigaPollQuestion = '';
	if (document.getElementById('pollquestion')) {
		if (document.getElementById('pollquestion').value != '') {
			taigaPollQuestion = document.getElementById('pollquestion').value;
		}
	}	
	
	var taigaPollMaxvotes = '';
	if (document.getElementById('maxvotes')) {
		if (document.getElementById('maxvotes').value != '') {
			taigaPollMaxvotes = document.getElementById('maxvotes').value;
		}
	}	
	
	var taigaPollParams = '';
	var taigaNumberOfPollParams = 0;
	for (var i = 1 ; i <= 100; i++) {
		if (document.getElementById('polloption'+i)) {
			if (document.getElementById('polloption'+i).value != '') {
				taigaNumberOfPollParams++;
				taigaPollParams += '&polloption'+i+'='+document.getElementById('polloption'+i).value;
			}
		} else {
			break;
		}
	}

	if ( (taigaNumberOfPollParams > 0) && (parseInt(taigaPollMaxvotes) > taigaNumberOfPollParams) ) {
		alert(lang__you_have_allowed_users_to_select_more_options_than_exist_in_this_poll);
		return false;
	}
	
	var taigaPollParamsFinal = '';
	if (  (taigaPollQuestion == '') && ( 
		(
			document.getElementById('editpollarea') && (document.getElementById('editpollarea').className == 'editpollarea_show')
		) || (
			(taigaPollParams != '')
			&&
			(!document.getElementById('polloption1')) //taigapollparams will be set because the poll option inputs will be filled in topics that already have polls so we need to check if we're displaying that poll to avoid a false "please enter a poll question"
		)
	)  ) {
		alert(lang__please_enter_a_poll_question);
		return false;
	} else if ( (taigaPollQuestion != '') && (taigaPollParams == '') ) {
		alert(lang__please_enter_at_least_one_poll_option);
		return false;
	} else if ( (taigaPollQuestion != '') && ((parseInt(taigaPollMaxvotes) > 100) || (parseInt(taigaPollMaxvotes) < 1)) ) {
		alert(lang__please_enter_a_maximum_number_of_items_to_vote_for);
		return false;
	} else if ( (taigaPollQuestion != '') && (taigaPollParams != '') ) {
		taigaPollParamsFinal = '&pollparams='+urlencode('pollquestion='+taigaPollQuestion+'&maxvotes='+taigaPollMaxvotes+taigaPollParams);
	}
	
	if ( (taigaPollParamsFinal == '') && (document.getElementById('posttextarea').value == '') ) {
		alert(lang__please_enter_text_in_the_post_area);
		return false;
	}
	
	
	
	var taigaguestusernamepart = '';
	if (document.getElementById('guestusername')) { //not logged in
		if (document.getElementById('guestusername').value == '') {
			alert(lang__please_enter_a_username);
			return false;
		}
		taigaguestusernamepart = '&guestusername='+urlencode(document.getElementById('guestusername').value);
	}
	
	
	
	var taigaParams;
	var taigaAddingPoll = false;
	
	if ( (taigaPollParamsFinal != '') && (rm == 'post') ) { //they're adding a poll to an existing topic
		taigaAddingPoll = true;
		taigaParams =
			'taiga='+urlencode(rm)+
			taigaPollParamsFinal
			;
	} else {
		taigaParams =
			'taiga='+urlencode(rm)+
			'&newpost='+urlencode(document.getElementById('posttextarea').value)+
			'&attachments='+urlencode(taigaattachments.join(','))+
			taigatopictitle+
			taigaan+
			taigaguestusernamepart+
			taigaPollParamsFinal
			;
	}
	
	
// 	alert(taigaParams);
// 	return false;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//alert(document.location);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			document.getElementById('submitpost').disabled = false;
			if (xmlhttp.status == 200) {
				//alert(xmlhttp.responseText);
				var taigamessages = xmlhttp.responseText.split("\n");
				taigamessages.reverse();
				if (taigamessages[0] == 'OK') {
					taigamessages.reverse();
					if ( (rm == 'topic') || (taigaAddingPoll == true) ) { //new topic or adding poll
						document.location=document.location.pathname;
					} else { //new post
						if (taigaajax == '0') { //we're not on the last page so we need to go there to show their post
							//alert('redirecting to "'+taigamessages[0]+'" ('+xmlhttp.responseText+')');
							if (taigamessages[0] != '') {
								document.location=taigamessages[0];
							} else { //can happen if e.g. we're just adding a poll
								document.location=document.location.pathname;
							}
							return;
						}
						taigaattachments = [];
						//reset attachment area
						for (var taigaattachmentnumber=1; taigaattachmentnumber<=10; taigaattachmentnumber++) {
							if (taigaattachmentnumber > 1) {
								taigastyleswitchidhide(taigaattachmentnumber, 'attachuploadcontainer');
							}
							taigastyleswitchid2show(taigaattachmentnumber, 'attachouter');
							document.getElementById('attachmentcomplete'+taigaattachmentnumber).innerHTML = '';
							taigastyleswitchidshow(taigaattachmentnumber, 'addattachment'); //the + signs
							taigastyleswitchhide('newattach'); //hide the attach file panel so it doesn't get in the way
						}
						//document.getElementById('').innerHTML = document.getElementById('').innerHTML;
// 						if (document.getElementById('posttitlearea')) {
// 							document.getElementById('posttitlearea').value='';
// 						}
						if (document.getElementById('postpreviewarea')) {
							document.getElementById('postpreviewarea').innerHTML='';
						}
						taigascrolltobottom();
					}
					taigatemppostext='';
				} else {
					if (!(document.getElementById('posttitlearea'))) {
						taigarestorepostext(taigatemppostext);
					}
					alert(xmlhttp.responseText);
				}
			} else {
				if (!(document.getElementById('posttitlearea'))) {
					taigarestorepostext(taigatemppostext);
				}
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	document.getElementById('submitpost').disabled = true;
	xmlhttp.send(taigaParams);
	
	taigatemppostext = document.getElementById('posttextarea').value;
	if (!(document.getElementById('posttitlearea'))) {
		document.getElementById('posttextarea').value='';
	}
}

function taigarestorepostext(taigatemppostext) {
	if (document.getElementById('posttextarea').value != '') {
		document.getElementById('posttextarea').value=taigatemppostext+"\n\n"+document.getElementById('posttextarea').value;
	} else {
		document.getElementById('posttextarea').value=taigatemppostext;
	}
}

function taigaopenunreadintabs() {
	var taigalink = taiganewpostlinks.shift();
	while (taigalink) {
		window.open(taigalink, '_blank');
		taigalink = taiganewpostlinks.shift();
	}
	
	setTimeout("taigapoll(true)",10000); //taigadontdoitagain
}

function taigalogin() {
	var xmlhttp = taigaNewAjax();
	var taigaParams =
	'taiga=log_in'+
	'&sern='+urlencode(document.getElementById('loginusernameinput').value)+
	'&sabrac='+urlencode(document.getElementById('loginpasswordinput').value)+
	'&rememberme='+(document.getElementById('rememberme').checked == true ? 1 : 0)
	;
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					document.location=document.location.pathname;
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	//alert(taigaParams);
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigalogout() {
	if (confirm(lang__do_you_really_want_to_log_out)) {
		var xmlhttp = taigaNewAjax();
		var taigaParams = 'taiga=log_out';
		xmlhttp.open("POST", document.location.pathname, true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				taigastyleswitch('ajaxloading');
				if (xmlhttp.status == 200) {
					if (xmlhttp.responseText == 'OK') {
						document.location='/';
					} else {
						alert(xmlhttp.responseText);
					}
				} else {
					alert(lang__sorry_an_error_occurred_please_try_your_request_again);
				}
			}
		}
		//alert(taigaParams);
		taigastyleswitch('ajaxloading');
		xmlhttp.send(taigaParams);
	}
}

function taigaregister() {
	if (document.getElementById('registerusernameinput').value == '') {
		alert(lang__please_choose_a_username);
		return false;
	}
	if (document.getElementById('registeremailinput').value == '') {
		alert(lang__please_enter_an_email_address);
		return false;
	}
	if (document.getElementById('registerpasswordinput').value.length < 4) {
		alert(lang__sorry_the_password_is_not_long_enough);
		return false;
	}
	if (document.getElementById('registerpasswordinput').value != document.getElementById('registerpasswordinputconfirm').value) {
		alert(lang__password_fields_dont_match);
		return false;
	}

	var taigaParams =
	'taiga=register'+
	'&sern='+urlencode(document.getElementById('registerusernameinput').value)+
	'&sabrac='+urlencode(document.getElementById('registerpasswordinput').value)+
	'&email='+urlencode(document.getElementById('registeremailinput').value);
	
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				var taigamessages = xmlhttp.responseText.split("\n");
				taigamessages.reverse();
				if (taigamessages[0] == 'OK') {
					alert(lang__your_profile_has_been_created__you_can_now_customize_your_profile_or_just_start_posting);
					taigamessages.reverse();
					document.location=taigamessages[0];
				} else {
					alert(xmlhttp.responseText);
				}
// 				if (xmlhttp.responseText == 'OK') {
// // 					var taigaboxes = ['registeremailinput', 'registerpasswordinputconfirm', 'registerpasswordinput', 'registerusernameinput'];
// // 					for (i in taigaboxes) {
// // 						document.getElementById(taigaboxes[i]).disabled=true;
// // 					}
// 					alert(lang__your_profile_has_been_created__you_can_now_customize_your_profile_or_just_start_posting);
// 					document.location='/profile/edit.html';
// 					//taigastyleswitchshow('captchagame');
// 				} else {
// 					alert(xmlhttp.responseText);
// 				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	//alert(taigaParams);
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigachangepassword(taigaform) {
	if (taigaform.password) { //this doesn't exist if it's not your profile
		if (taigaform.password.value == '') {
			alert(lang__please_enter_your_current_password);
			return false;
		}
	}
	if (taigaform.newpassword1.value != taigaform.newpassword2.value) {
		alert(lang__password_fields_dont_match);
		return false;
	}
	if (taigaform.newpassword1.value.length < 4) {
		alert(lang__new_password_is_not_long_enough);
		return false;
	}

	var taigaParams =
	'taiga=changepassword'+
	'&newpassword1='+urlencode(taigaform.newpassword1.value)+
	'&newpassword2='+urlencode(taigaform.newpassword2.value);
	if (taigaform.password) {
		taigaParams += '&password='+urlencode(taigaform.password.value);
	}
	
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					document.location=document.location.pathname;
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigapageselectchange(taigaselect) {
	document.location=taigaselect.value;
}

function taigalock(taigafunc, taigatarget) { //taigafunc = lock | unlock
	if (!taigatarget) {
		taigatarget = document.location;
	}
	var taigaParams = 'taiga='+taigafunc;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", taigatarget, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				var taigamessages = xmlhttp.responseText.split("\n");
				taigamessages.reverse();
				if (taigamessages[0] == 'OK') {
					//var taigatochange = 'lockbutton';
					taigamessages.reverse();
					if (taigamessages[0] != '') { //there is something for us to redirect to
						document.location=taigamessages[0];
					}
					taigastyleswitch('lockbutton');
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	//alert(taigaParams);
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigasticky(taigafunc, taigatarget) { //taigafunc = sticky | unsticky
	if (!taigatarget) {
		taigatarget = document.location;
	}
	var taigaParams = 'taiga='+taigafunc;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", taigatarget, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				var taigamessages = xmlhttp.responseText.split("\n");
				taigamessages.reverse();
				if (taigamessages[0] == 'OK') {
					//var taigatochange = 'lockbutton';
					taigamessages.reverse();
					if (taigamessages[0] != '') { //there is something for us to redirect to
						document.location=taigamessages[0];
					}
					taigastyleswitch('stickybutton');
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	//alert(taigaParams);
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigamodmultiple(taigafunc, taigadest) {
	var taigapostboxes = getElementsByClassName('modbox');
	var taigaposts = new Array();
	for (var i = 0; i < taigapostboxes.length; i++) {
		if (taigapostboxes[i].checked == true) {
			taigaposts.push(taigapostboxes[i].name);
		}
	}
	taigamod(taigafunc, taigaposts, taigadest);
}

function taigamod(taigafunc, taigaposts, taigadest) {
	var taigaParams = 'taiga='+taigafunc;
	if ('merge' == taigafunc) {
		var taigamergetopicappend = '';
		
		var mergefunc = taigafunc;
		
		//if ( (!(document.getElementById('mergewholetopic'))) || (taigaposts === undefined) ) { //either they hit whole topic on the right side or they're using the left side function which is always whole topic
		if (taigaposts === undefined) { //they're using the left side function which is always whole topic
			taigaposts = [];
			mergefunc = 'mergetopic';
			
			taigamergetopicappend = 'single';
			taigaParams += '&type=mergewholetopic'; //it doesn't matter that this gets sent twice because params are a hash on the server side
		}
		taigaParams += '&taigafunc='+mergefunc;

		if ( (document.getElementById('mergedtopictitleinput'+taigamergetopicappend) && (document.getElementById('mergedtopictitleinput'+taigamergetopicappend).value == '') ) && ( (document.getElementById('mergenewtitle'+taigamergetopicappend)) && (document.getElementById('mergenewtitle'+taigamergetopicappend).checked == true) ) ) {
			alert(lang__please_enter_a_new_title);
			return;
		}
		if ( (taigaposts.length > 1) && document.getElementById('mergefromchecked') && (document.getElementById('mergefromchecked').checked == true) ) {
			alert(lang__please_check_only_one_post_from_which_to_start_merging_away_posts);
			return;
		}
		if ( 
			//( (!(document.getElementById('mergewholetopic'))) && (taigaposts.length == 0) )
			//||
			//( (document.getElementById('mergewholetopic')) && (document.getElementById('mergewholetopic').checked == false) && 
			(taigaposts.length == 0) && (mergefunc != 'mergetopic')
			//) 
		) {
			alert(lang__to_use_this_function_please_select_some_posts_by_clicking_their_checkboxes);
			return;
		}
		if (
			( (document.getElementById('destinationtopictitleinput'+taigamergetopicappend)) && (document.getElementById('destinationtopictitleinput'+taigamergetopicappend).value == '') )
		) {
			alert(lang__please_enter_the_url_of_the_destination_topic);
			return;
		}
		
		if (taigaposts.length == 0) { //some functions work on every post on the page or topic so get some unchecked posts to help them work
			var taigapostboxes = getElementsByClassName('modbox');
			for (var i = 0; i < taigapostboxes.length; i++) {
				taigaposts.push(taigapostboxes[i].name);
			}
		}
		
		if (document.getElementById('destinationtopictitleinput'+taigamergetopicappend)) {
			taigaParams += '&dest='+urlencode(document.getElementById('destinationtopictitleinput'+taigamergetopicappend).value);
		} else { // forum view/mergetopic
			taigaParams += '&dest='+urlencode(taigaposts.shift());
			if (taigaposts.length == 0) {
				alert(lang__please_select_more_than_one_topic_to_merge);
				return;
			}
		}
		
		//individual posts only (right side function)
		var taigamergeradiostype = ['mergefromchecked', 'mergeallchecked', 'mergewholetopic'];
		for (var i = 0; i < taigamergeradiostype.length; i++) {
			if (document.getElementById(taigamergeradiostype[i])) {
				if (document.getElementById(taigamergeradiostype[i]).checked == true) {
					taigaParams += '&type='+urlencode(taigamergeradiostype[i]);
				}
			}
		}
		var taigamergeradiostitle = ['mergedestinationtitle', 'mergethistitle', 'mergenewtitle'];
		for (var i = 0; i < taigamergeradiostitle.length; i++) {
			if (document.getElementById(taigamergeradiostitle[i]+taigamergetopicappend)) {
				if (document.getElementById(taigamergeradiostitle[i]+taigamergetopicappend).checked == true) {
					//alert(taigamergeradiostitle[i]);
					taigaParams += '&title='+urlencode(taigamergeradiostitle[i]);
				}
			}
		}
		if (document.getElementById('mergedtopictitleinput'+taigamergetopicappend)) {
			taigaParams += '&newtitle='+urlencode(document.getElementById('mergedtopictitleinput'+taigamergetopicappend).value);
		}
		taigaParams += '&posts='+urlencode(taigaposts.join(','));
// 		alert(taigaParams);
// 		return;
		taigamodsupplicant(taigafunc, taigaParams, document.location, true);
	} else if ('split' == taigafunc) {
		taigaParams = 'taiga=merge'; //same dispatch as merge
		taigaParams += '&taigafunc='+taigafunc;
		if (document.getElementById('splittopictitleinput').value == '') {
			alert(lang__please_enter_a_new_title);
			return;
		}
		if ( (taigaposts.length > 1) && (document.getElementById('splitfromchecked').checked == true) ) {
			alert(lang__please_check_only_one_post_from_which_to_start_merging_away_posts);
			return;
		}
		if (taigaposts.length == 0) {
			alert(lang__to_use_this_function_please_select_some_posts_by_clicking_their_checkboxes);
			return;
		}
		
		taigaParams += '&dest='+urlencode(document.getElementById('splitdestselect').value);
		var taigamergeradiostype = ['splitfromchecked', 'splitallchecked'];
		for (var i = 0; i < taigamergeradiostype.length; i++) {
			if (document.getElementById(taigamergeradiostype[i])) {
				if (document.getElementById(taigamergeradiostype[i]).checked == true) {
					taigaParams += '&type='+urlencode(taigamergeradiostype[i]);
				}
			}
		}
		if (document.getElementById('splittopictitleinput')) {
			taigaParams += '&newtitle='+urlencode(document.getElementById('splittopictitleinput').value);
		}
		taigaParams += '&posts='+urlencode(taigaposts.join(','));
// 		alert(taigaParams);
// 		return;
		taigamodsupplicant(taigafunc, taigaParams, document.location, true);
	} else if ('move' == taigafunc) {
		taigaParams += '&dest='+taigadest;
		if (document.getElementById('movetopicsarea2')) { //forum view, use checked stuff
			if (taigaposts.length == 0) {
				alert(lang__to_use_this_function_please_select_some_posts_by_clicking_their_checkboxes);
				return;
			}
		} else { //post view
			taigaposts.push(document.location.toString());
		}
		for (var i = 0; i < taigaposts.length; i++) {
			taigamodsupplicant(taigafunc, taigaParams, taigaposts[i], (i == (taigaposts.length - 1)) );
		}
	} else { //delete
		if (taigaposts.length == 0) {
			alert(lang__to_use_this_function_please_select_some_posts_by_clicking_their_checkboxes);
			return;
		}
		
		if (!confirm(lang__really_delete_post)) {
			return false;
		}
		
		if (typeof taigaposts == "string") {
			taigamodsupplicant(taigafunc, taigaParams, taigaposts, true);
		} else {
			for (var i = 0; i < taigaposts.length; i++) {
				//alert('taigaposts.length: '+taigaposts.length+', i: '+i);
				taigamodsupplicant(taigafunc, taigaParams, taigaposts[i], (i == (taigaposts.length - 1)) );
			}
		}
	}
}

function taigamodsupplicant(taigafunc, taigaParams, taigapost, taigalast) {
	//alert(taigalast);
// 	if (taigafunc == 'deletepost') {
// 		if (taigacurrentlydeletingpost == '1') {
// 			alert('Sorry, you can\'t delete more than one post at a time without using the checkboxes.');
// 			return false;
// 		}
// 	}
	
	
	var xmlhttp = taigaNewAjax();
	//alert(taigapost);
	//alert(taigaParams);
	xmlhttp.open("POST", taigapost, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			if (taigafunc != 'deletepost') {
				taigastyleswitch('ajaxloading');
			} else {
				//alert('delete post response: '+xmlhttp.responseText);
				taigacurrentlydeletingpost = '0';
				taigastyleswitchid2(taigapost, 'deletepostloading');
			}
			if (xmlhttp.status == 200) {
				//alert(xmlhttp.responseText);
				var taigamessages = xmlhttp.responseText.split("\n");
				taigamessages.reverse();
				if (taigamessages[0] == 'OK') {
					taigamessages.reverse();
					//if (taigamessages[1] == '') {
						if ( taigalast == true ) { //this is the last operation, time to redirect
							
							if (taigafunc != 'deletepost') {
								alert(lang__success);
							} else {
								taigastyleswitchid2(taigapost, 'deletepostloading'); //start it goin again so it looks like something is going on until the delete event comes in and takes it away
							}
						
							if ( (taigaajax == '0') || (taigafunc != 'deletepost') ) {
								//alert('taigaajax is 0 ...');
								if (taigamessages[0] != '') { //there is something for us to redirect to
									//alert('there is something for us to redirect to ...');
									document.location=taigamessages[0];
								} else {
									//alert('taigaajax is 0 but we didn ...');
									document.location='/';
								}
							}
							
							taigastyleswitchhide('deleteposts');
							$('wholepagecontainer').fade(1); //last so if it fails they won't suspect anything
						}// else { alert(taigalast) }
					//} else {
					//	alert(taigamessages[1]);
					//}
				} else {
					alert(lang__sorry_an_error_occurred_please_try_your_request_again);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	//alert(taigaParams+' to '+taigapost);
	if (taigafunc != 'deletepost') {
		taigastyleswitch('ajaxloading');
	} else {
		taigastyleswitchid2(taigapost, 'deletepostloading');
		taigacurrentlydeletingpost = '1';
	}
	xmlhttp.send(taigaParams);
}

function taigacheckall(taigabox) {
	var taigaboxes = getElementsByClassName(taigabox.name);
	for (var i=0; i < taigaboxes.length; i++) {
		taigaboxes[i].checked = taigabox.checked;
	}
}

function taigaviewsettings(taigachanged) {
	var taigaParams = 'taiga=setprefs';
	var taigachangedvalue = taigachanged.value;
	if (taigachanged.type == 'checkbox') {
		taigachangedvalue = (taigachanged.checked == true) ? 1 : 0;
		if (taigachanged.name == 'ipbasedsessions') {
			taigachangedvalue = (taigachangedvalue == 0 ? 1 : 0);
		}
	}
	taigaParams += '&'+taigachanged.name+'='+taigachangedvalue;
	//alert(taigaParams);
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					//if (/^(?:view|avatarmaxsize)/.test(taigachanged.name)) {
					if (/^(?:ipbasedsessions)/.test(taigachanged.name)) {
						//taigagetnewposts('firstpost');
					} else {
						document.location=document.location.pathname;
					}
				} else {
					alert(xmlhttp.responseText);
					return false;
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
				return false;
			}
		}
	}
	xmlhttp.send(taigaParams);
}

function taigapoll(taigadontdoitagain) {
	//var taigadoitagain = true;
	var taigaParams = 'taiga=poll&ajax='+taigaajax;
	var xmlhttp = taigaNewAjax();
	//alert('posting to '+document.location);
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText);
			if (xmlhttp.status == 200) {
				var taigamessages = xmlhttp.responseText.split("\n");
				if (taigamessages[0] == 'OK') {
					for (var i = 1; i < taigamessages.length; i++) {
						 if (document.getElementById(taigainjectionfields[i-1])) {
						 	//alert(taigamessages[i]);
						 	document.getElementById(taigainjectionfields[i-1]).innerHTML = taigamessages[i];
						 }
					}
					//reload the page if the number of unread posts changed to show them to the user
					if (taigainunreadview == true) {
						if (taigafirsttime <= 0) {
							if (taigaunreadhash != taigamessages[7]) {
								document.location='/unread/topics.html';
							}
							taigaunreadhash = taigamessages[7];
						} else if (taigafirsttime == taigafirsttimeinitialsetting) {
							taigaunreadhash = taigamessages[7]; //initial setting
							taigafirsttime--;
						} else {
							taigafirsttime--;
						}
					}
					
					taiganewpostlinks = [];
					//alert(taigamessages[8]);
					//var taiganewposts = new Array();
					var taiganewposts = taigamessages[8].split('\\');
					//alert(taiganewposts);
					for (var i = 0; i < taiganewposts.length; i++) {
						if (taiganewposts[i]) {
							//alert('pushing "'+taiganewposts[i]+'"');
							taiganewpostlinks.push(taiganewposts[i]);
						}
					}
					
					if (taigamessages[9] == 'guest') {
						taigadontdoitagain = true;
					}
				} else if (taigamessages[0] == 'error: human data not found!') { //the entire topic has gone byebye
					alert(lang__this_topic_has_been_deleted__redirecting_you_to_the_index);
					document.location='/';
				} else {
					//alert(xmlhttp.responseText);
				}
			} else {
				//alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
			if (!taigadontdoitagain) {
				setTimeout("taigapoll()",taigapollinterval);
				if (taigafirsttime == (taigafirsttimeinitialsetting-1)) { //we want to check onload, onload+10s, and every taigapollinterval ms
					setTimeout("taigapoll(true)",10000); //taigadontdoitagain
				}
			}
		}
	}
	xmlhttp.send(taigaParams);
}

function taigaajaxtimeoutinc() {
	taigaajaxtimeouti++;
	if (taigaajaxtimeouti > taigaajaxtimeout) {
		taigaajaxtimeouti = 0;
		if (taigagetnewpostsajax) {
			taigagetnewpostsajax.abort();
			//taigagetnewposts(); //it restarts automatically on its own after (currently) 5 seconds
		}
	}
	
	setTimeout("taigaajaxtimeoutinc()", 1000);
}

function taigagetnewposts(taigafirst) {
	if (taigafirst != 'firstpost') {
		taigaajaxtimeouti = 0;
	}
	
	taigaajax = '1'; //for taigapoll() to tell the server whether we need users viewing also

// 	var taigaposts = getElementsByClassName('filled');
// 	if (!(taigaposts.length)) {
// 		document.location=document.location.pathname;
// 		return;
// 	}
// 	for (var i = 0; i < taigaposts.length; i++) {
// 		var taigamatches = taigaposthumanregex.exec(taigaposts[i].innerHTML);
// 		if (taigamatches != null) {
// 			//alert(taigaposts[i].innerHTML+' ... '+taigamatches[2]);
// 			var posthuman = taigamatches[2];
// 			if (posthuman == taigahumantodel) { //mitsuketa
// 				taigaposts[i].className = 'deleted';
// 				taigaposts[i].innerHTML = '';
// 				i=taigaposts.length; //we're done here
// 			}
// 		}
// 	}

 	var posthumans = new Array();
	var posthumans_html = getElementsByClassName('post');
	if (!(posthumans_html.length)) {
		//alert('no posthumans found');
		//setTimeout("taigagetnewposts()",1000);
		document.location=document.location.pathname;
		return;
	}
	for (var i = 0; i < posthumans_html.length; i++) {
		posthumans.push(posthumans_html[i].id);
	}
	if (taigaforceposthuman == '') { //oneesama no first time
		if (taigafirst != 'firstpost') {
			//lol
			posthumans.reverse();
			//taigaforceposthuman = posthumans.shift();
			///post/smf_linking_to_to_topics_from_other_sites_comma_problem_181.html != smf_linking_to_to_topics_from_other_sites_comma_problem_180
			var taigaforceposthumanshort = taigadeletingpost; //so we can use this cool while loop
			while ((taigaforceposthumanshort == taigadeletingpost) && (posthumans.length > 0) ) {
				taigaforceposthuman = posthumans.shift();
				taigaforceposthumanshort = taigaforceposthuman.replace(taigaposthumanformregex, '$1');
			}
			taigadeletingpost='0';
			if (taigaforceposthuman == '') {
				taigaredirecting = '1';
				alert(lang__all_posts_on_this_page_have_disappeared__redirecting_you_to_the_index);
				document.location='/';
			}
			taigaforceposthuman = taigaforceposthuman.replace(taigapostregex, "/newposts/");
		} else {
			taigaforceposthuman = posthumans[0];
			taigaforceposthuman = taigaforceposthuman.replace(taigapostregex, "/viewchanged/");
		}
	}
	var posthuman = taigaforceposthuman;
	if (taigafirst != 'firstpost') {
		posthuman = posthuman.replace(taigapostregex, "/newposts/");
	} else {
		posthuman = posthuman.replace(taigapostregex, "/viewchanged/");
	}
	//alert(posthumans.join());
	//alert(posthuman);
	
	
	var taigahost = document.location.toString();
	taigahost = taigahost.replace(taigahostregex, '$1');
	
	taigagetnewpostsajax = taigaNewAjax();
	
	if (  
	false && 
	/\.taigaforum\.com$/.test(taigahost) && 
	(  (taigagetnewpostsajax.withCredentials !== undefined) || ('withCredentials' in taigagetnewpostsajax)  ) &&
	(taigashorthost != '')
	) {
		var taigaport = '';
		if (/(:\d+)$/.test(taigahost)) {
			taigaport = taigahost.replace(/^.*(:\d+)$/, '$1'); //get port
			taigahost = taigahost.replace(/(:\d+)$/, ''); //kill port
		}
		taigahost = 'taigaajax'+Math.floor(Math.random()*101)+'.'+taigahost+'.taigaajax.'+taigashorthost+'.taigaforum.com'+taigaport;
	}
	
	var taigaParams = '';
	if (taigafirstajax == '1') {
		taigaParams = 'firstajax=1';
		taigafirstajax = '0';
		setTimeout('taigaprocessmessages()',1000);
	}
	
	var taigapostto = 'http://'+taigahost+posthuman;
	
	
	
	if (
	false &&
	taigaloggedinuid == '19') {
		document.getElementById('mainbar').innerHTML = '<div class="mainbarlinkname">'+taigapostto+'</div>';
	}
	
	
	
	
	taigagetnewpostsajax.open("POST", taigapostto, true);
	if ( (taigagetnewpostsajax.withCredentials !== undefined) || ("withCredentials" in taigagetnewpostsajax) ) {
		taigagetnewpostsajax.withCredentials = 'true';
	}
	taigagetnewpostsajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	taigagetnewpostsajax.onreadystatechange=function() {
		if (taigagetnewpostsajax.readyState==4) {
			taigagetnewpostscallback(taigafirst);
		}
	}
	taigagetnewpostsajax.send(taigaParams);
}

function taigagetnewpostscallback(taigafirst) {
	if (taigagetnewpostsajax.status == 200) {
		taigamessagequeue.push(taigagetnewpostsajax.responseText, taigafirst);
		
		//find last posthuman, if given
		var taigamessages = taigagetnewpostsajax.responseText.split('<!--message-->');
		taigamessages.reverse(); //need to see deletes before anything else for setting taigadeletingpost
		for (var taigamessage = 0; taigamessage < taigamessages.length; taigamessage++) {
			var taigaresponsearr = taigamessages[taigamessage].split("\n");
			if (taigaresponsearr[1] == '0') { //new messages
				//alert(taigamessages[taigamessage]);
				//alert('setting taigaforceposthuman to "'+taigaresponsearr[5]+'"');
				if (taigaresponsearr[4] != '') {
					if (taigadeletingpost == '0') { //if we are deleting a post it may be this one so we don't want to try to POST to /newposts/it
						taigaforceposthuman = taigaresponsearr[4];
					}
				}
			} else if (taigaresponsearr[1] == '4') { //deleting a post; save this value for ignoring when we POST next
				taigadeletingpost=taigaresponsearr.slice(6).join('');
				taigaforceposthuman = '';
			}
		}
	} else {
		//alert('Sorry, an error occurred.');
	}
	if (taigafirst != 'firstpost') {
		var taigacoderef = function() { taigagetnewposts(taigafirst); }
		var taigaajaxdelay = 0;
		if (taigagetnewpostsajax.status != 200) {
			taigaajaxdelay = 5000;
		}
		setTimeout(taigacoderef,taigaajaxdelay);
	}
}

function taigaprocessmessages() {
	if (!taigacurrentlyeditingpost) { //shhh - don't interrupt them while they're editing posts - try again later to process incoming message events
		if (taigamessagequeue.length) {
			while (taigamessagequeue.length) {
				var xmlhttpresponseText = taigamessagequeue.shift();
				//alert(xmlhttpresponseText);
				var taigafirst = taigamessagequeue.shift();
				//var posthumans = taigamessagequeue.shift();
				var taigamessages = xmlhttpresponseText.split('<!--message-->');
				taigamessages.sort();
				taigamessages.reverse(); //highest event ids first to prioritize edits over new posts which bork them
				//var taigacurrentlyeditingpostyes = taigacurrentlyeditingpost; //in case this changes during the below loop - we won't get an inconsistent state
				for (var taigamessage = 0; taigamessage < taigamessages.length; taigamessage++) {
					//alert(taigamessages[taigamessage]);
					var taigaresponsearr = taigamessages[taigamessage].split("\n");
					var firstfield = parseInt(taigaresponsearr[1]);
					var secondfield = parseInt(taigaresponsearr[2]);
					var thirdfield = parseInt(taigaresponsearr[3]);
					
					//alert(firstfield);
					if ( (firstfield == 0) && (secondfield > 0) ) { //new messages
					
						var taigaatbottom = taigaloc();
						taigastyleswitchhidenoscroll('postcheckboxes', 'postcheckboxfuncs'); //new messages coming in without modboxes displayed - need to hide mod functions if shown
						
						//find the first occupied postdiv
						var taigakillinc = 0;
						while ( (document.getElementById('postdivnum'+taigakillinc).className == 'blank') && (taigakillinc++ < 1000) ) {}
						
						if (taigafirst != 'firstpost') { //not view settings changed
							if (getElementsByClassName('filled').length >= thirdfield) { //need to chop one or more messages off the top of the current display
								//alert(getElementsByClassName('filled').length+' >= '+thirdfield);
								var taigapostskilled = 0;
								while (taigapostskilled < secondfield) {
								//while (taigakillinc <= secondfield) {
									//if (document.getElementById('postdivnum'+taigakillinc).className != 'blank') { //there will be blank ones throughout if people have been deleting posts
										document.getElementById('postdivnum'+taigakillinc).className='blank';
										document.getElementById('postdivnum'+taigakillinc).innerHTML='';
										taigakillinc++;
										taigapostskilled++;
									//}
								}
								
								//remove red line from first post displayed
								while ( (document.getElementById('postdivnum'+taigakillinc).className == 'blank') && (taigakillinc < 1000) ) {
									taigakillinc++;
								}
								//alert(document.getElementById('postdivnum'+taigakillinc).innerHTML);
								//alert(/<(?:div|DIV) class="newpagemarker"><\/(?:div|DIV)>/.test(document.getElementById('postdivnum'+taigakillinc).innerHTML));
								document.getElementById('postdivnum'+taigakillinc).innerHTML = document.getElementById('postdivnum'+taigakillinc).innerHTML.replace(/<(?:div|DIV) class="newpagemarker"><\/(?:div|DIV)>/g, ''); //capitalized div for opera lol ...
								
// 								var taigaposts = document.getElementById('posts').innerHTML.split(splitstring);
// 								var taigakilllength = taigaposts[0].length;
// 								var taigakillinc = 1;
// 								while (taigakillinc <= secondfield) {
// 									taigakilllength += (taigaposts[taigakillinc].length + splitstring.length);
// 									taigakillinc++;
// 								}
// 								document.getElementById('posts').innerHTML = document.getElementById('posts').innerHTML.substring(taigakilllength);
// 								
// 								//remove red line if it's on the first post displayed
// 								var taigaposts = taigaextractposts();
// 								//alert(taigaposts[1]);
// 								var taigamatches = taigaposthumanregex.exec(taigaposts[1]); //is it really a good idea to hardcode this?
// 								if (taigamatches != null) {
// 									var posthumanform = taigamatches[1];
// 									var taigafirstpost = document.getElementById(posthumanform);
// 									if (taigafirstpost) {
// 										taigafirstpost.innerHTML = taigafirstpost.innerHTML.replace(/<(?:div|DIV) class="newpagemarker"><\/(?:div|DIV)>/, ''); //capitalized div for opera lol ...
// 									} else { alert('taigafirstpost not found!'); }
// 								} else { alert('taigamatches is null!'); }
							}
						} else {
							//document.getElementById('posts').innerHTML = '';
						}
						
						//find the first postdiv without a post in it
						while (document.getElementById('postdivnum'+taigakillinc).className != 'blank') {
							if (taigakillinc >= (1000-secondfield)) { //we're out of postdivs - abort!
								document.location=document.location.pathname;
							}
							taigakillinc++;
						}
// 						for (var p = taigakillinc; p < 1000; p++) {
// 							if (document.getElementById('postdivnum'+p).className == 'blank') { //found first free postdiv after posts
// 								taigakillinc = p;
// 								break;
// 							}
// 						}
						var taigapostmessages = taigamessages[taigamessage].split('<!--taiga-->');
						taigapostmessages.shift(); //we don't want the first one
						var taigapostsreplaced = 0;
						while (taigapostsreplaced < taigapostmessages.length) {
							if (taigakillinc >= (1000-secondfield)) { //we're out of postdivs - abort!
								document.location=document.location.pathname;
							}
							//if (document.getElementById('postdivnum'+taigakillinc).className == 'blank') { //non-blank ones may come unexpectedly if people have been deleting posts on this page
								//alert('setting in "'+taigapostmessages[taigapostsreplaced]+'"');
								document.getElementById('postdivnum'+taigakillinc).className='filled';
								document.getElementById('postdivnum'+taigakillinc).innerHTML = taigapostmessages[taigapostsreplaced];
								taigakillinc++;
								taigapostsreplaced++;
							//}
						}
						
						if (taigaresponsearr[5] != taigaloggedinuid) {
							taiganewposts+=secondfield;
							taigasetitle();
						}
						
						taigascroll(taigaatbottom);
						
					} else if (firstfield == 1) { //paging
						document.getElementById('pages').innerHTML = taigamessages[taigamessage];
					} else if (firstfield == 2) { //userlist
						document.getElementById('userssection').innerHTML = taigamessages[taigamessage];
					} else if (firstfield == 3) { //view settings
						document.getElementById('viewsettingscontainer').innerHTML = taigamessages[taigamessage];
					} else if (firstfield == 4) { //delete post
						var taigahumantodel = taigaresponsearr.slice(6).join(''); //that is, the message body
						//alert('taigahumantodel: "'+taigahumantodel+'"');
						
						if (document.getElementById(taigahumantodel+'editbox')) { //it's on this page
							var taigaposts = getElementsByClassName('filled');
							for (var i = 0; i < taigaposts.length; i++) {
								var taigamatches = taigaposthumanregex.exec(taigaposts[i].innerHTML);
								if (taigamatches != null) {
									//alert(taigaposts[i].innerHTML+' ... '+taigamatches[2]);
									var posthuman = taigamatches[2];
									if (posthuman == taigahumantodel) { //mitsuketa
										taigaposts[i].className = 'deleted';
										taigaposts[i].innerHTML = '';
										i=taigaposts.length; //we're done here
										
										//we don't know whether this is the one that caused taiganewposts to increment or not ... the worst that will happen is they will be called in for a phantom post
// 										if (taiganewposts > 0) {
// 											taiganewposts--;
// 											taigasetitle();
// 										}
									}// else { alert(posthuman+' != '+taigahumantodel) }
								}// else { alert('taigamatches is null') }
							}
							
// 							var taigaposts = taigaextractposts();
// 							for (var i = 0; i < taigaposts.length; i++) {
// 								var taigamatches = taigaposthumanregex.exec(taigaposts[i]);
// 								if (taigamatches != null) {
// 									var posthuman = taigamatches[2];
// 									if (posthuman == taigahumantodel) { //mitsuketa
// 										if ( i != (taigaposts.length-1) ) { //we are not deleting the last post
// 											document.getElementById('posts').innerHTML = 
// 												document.getElementById('posts').innerHTML.substring(0, taigaposts.slice(0,i).join('').length)
// 												//deleted post was here
// 												+document.getElementById('posts').innerHTML.substring(taigaposts.slice(0,i+1).join('').length)
// 											;
// 										} else { //we are deleting the last post
// 											document.getElementById('posts').innerHTML = 
// 												document.getElementById('posts').innerHTML.substring(0, taigaposts.slice(0,i).join('').length)
// 											;
// 										}
// 										i=taigaposts.length; //we're done here
// 									}
// 								}
// 							}
						}
					} else if (firstfield == 5) { //edit post
						var taigaatbottom = taigaloc();
						//alert('received edit event');
						//alert(xmlhttp.responseText);
						var taigahumantoedit = taigaresponsearr[2];
						var taigabbencodedpostext = taigaresponsearr[3];
						var taigarawpostext = taigaresponsearr.slice(6).join("\n"); //that is, the message body
						
						if (document.getElementById(taigahumantoedit+'editbox')) { //it's on this page
							var taigaeditbox = document.getElementById(taigahumantoedit+'editbox');
							taigaeditbox.innerHTML=taigarawpostext;
							
							var taigabbcodetarget = document.getElementById('edit_hide_'+taigahumantoedit);
							if (taigaeditbox.parentNode == taigabbcodetarget) { //they're currently editing this post so the ids are swapped
								taigabbcodetarget = document.getElementById('edit_show_'+taigahumantoedit);
							}
							taigabbcodetarget.innerHTML=taigabbencodedpostext;
						}// else { alert('could not find id '+taigahumantoedit+'editbox') }
						taigascroll(taigaatbottom);
					} else if (firstfield == 6) { //edit history
						var taigaatbottom = taigaloc();
						//alert('received edithistory event');
						var taigahumantoedit = taigaresponsearr[2];
						var taigaedithistory = taigaresponsearr.slice(6).join(''); //that is, the message body
						
						if (document.getElementById('edithistoryparent_'+taigahumantoedit)) { //it's on this page
							//alert(taigaedithistory);
							var taigaeditbox = document.getElementById('edithistoryparent_'+taigahumantoedit).innerHTML=taigaedithistory;
						}// else { alert('could not find id edithistoryparent_'+taigahumantoedit) }
						taigascroll(taigaatbottom);
					}
				}
			}
		//if (document.getElementById('posts')) {
			//alert ('made it this far');
// 			var taigaposts = taigaextractposts();
// 			if (taigaposts.length == 1) { //all posts on this page are gone - redirect
// 				taigaredirecting = '1';
// 				alert('All posts on this page have disappeared! Redirecting you to the index ...');
// 				document.location='/';
// 			}
			var taigafoundpost = '0';
			for (var p = 0; p < 1000; p++) {
				if (document.getElementById('postdivnum'+p).className == 'filled') {
					//alert('found a post! '+'postdivnum'+p);
					taigafoundpost='1';
					break;
				}
			}
			if (taigafoundpost != '1') {
				taigaredirecting = '1';
				alert(lang__all_posts_on_this_page_have_disappeared__redirecting_you_to_the_index);
				document.location='/';
			}
		//}// else { alert("no 'posts'!") }
		}
	}
	if (taigaredirecting == '0') {
		setTimeout('taigaprocessmessages()',100);
	}
}


function taigatest(taigathing) {
	for (var i=0; i<taigathing.parentNode.parentNode.childNodes.length; i++) {
		alert(taigathing.parentNode.parentNode.childNodes[i].tagName);
	}
}

function taigaextractposts(taigamaterial) {
	var taigaposts;
	if (taigamaterial) {
		taigaposts = taigamaterial;
	} else {
		taigaposts = document.getElementById('posts').innerHTML.split(splitstring);
	}
	for (var i = 1; i < taigaposts.length; i++) { //add the separator back in ...
		taigaposts[i] = splitstring+taigaposts[i];
	}
	return taigaposts;
}
						
function taigadelay(taigadelay, taigafunc, taigaarg) {
	setTimeout(taigafunc+'("'+taigaarg+'")', taigadelay*1000);
}

function taigastyleswitchsetpref(taigatoswitch, taigapref) {
	taigastyleswitch(taigatoswitch);
	var taigaParams = 'taiga=setprefs';
	var taigachangedvalue = (getElementsByClassName(taigatoswitch+'_show').length > getElementsByClassName(taigatoswitch+'_hide').length) ? 1 : 0;
	taigaParams += '&'+taigapref+'='+taigachangedvalue;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(taigaParams);
}

function taigaautosave(taigadoitagain) {
	if (
		!document.getElementById('posttextarea')
		||
		document.getElementById('posttitlearea')
	) {
		return false;
	}
	
	if (taigainputext == document.getElementById('posttextarea').value) { //hasn't changed since last time
		//alert('returning false since taigainputext == document.getElementById(\'posttextarea\').value');
		if (taigadoitagain) {
			setTimeout("taigaautosave(true)",taigaautosaveinterval);
		}
		return false;
	}
	taigainputext = document.getElementById('posttextarea').value;
	
	var taigaParams = 'taiga=autosave&mode=newpost&data='+urlencode(taigainputext);
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(taigaParams);

	if (taigadoitagain) {
		setTimeout("taigaautosave(true)",taigaautosaveinterval);
	}
}

function taigaadminpane(taigapanename) {
	if (taigapanename == '') {
		return false;
	}
	
	var taigaclasses = new Array('globalcat', 'tapcat', 'boardscat', 'groupscat', 'stylescat');
	taigahideotherssupplicant(taigapanename, taigaclasses);
	
	var taigaParams = 'taiga=setprefs&adminpane='+taigapanename;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(taigaParams);
}

function taigagotoadminpane(taigapanename) {
	document.getElementById('ajaxposttarget').innerHTML='<form name="newpmto" action="/admin/admin.html" method="POST"><input type="hidden" name="newpmto" value="'+taigapanename+'" /></form>';
	document.newpmto.submit();
}

function taigamarkallread(taigatime, taigafid) {
	var taigaParams = 'taiga=markallread&time='+urlencode(taigatime)+'&fid='+urlencode(taigafid);
	
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				var taigamessages = xmlhttp.responseText.split("\n");
				if (taigamessages[0] == 'OK') {
					document.location=document.location.pathname;
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigaeditthispost(taigahuman, taigaclass) {
// 	if (taigacurrentlyeditingpost == taigahuman) {
// 		taigacurrentlyeditingpost = '0';
// 	} else if (taigacurrentlyeditingpost == '0') {
// 		taigacurrentlyeditingpost = taigahuman;
// 	} else {
// 		alert('Sorry, you can only edit one post at a time.');
// 		return false;
// 	}

//	if (document.getElementById('edit_hide_'+taigahuman).className == 'editarea') {
	if (taigacheckparentsforid(document.getElementById(taigahuman+'editbox'), 'edit_show_'+taigahuman)) {
		//start editing the post
		taigacurrentlyeditingpost++;
		taigalastextarea = taigahuman+'editbox';
	} else {
		//abort editing the post
		taigacurrentlyeditingpost--;
		taigalastextarea = 'posttextarea';
	}
	
	var taigaatbottom = taigaloc();
	taigastyleswitchid2(taigahuman, taigaclass);
	taigascroll(taigaatbottom);
	
	//document.getElementById('gotobar').innerHTML = taigacurrentlyeditingpost;
}

function taigacheckparentsforid(taigaobject, taigaid) {
	while (taigaobject.parentNode) {
		if (taigaobject == document.getElementById(taigaid)) {
			return true;
		}
		taigaobject = taigaobject.parentNode
	}
	
	return false;
}

function taigaeditpost(taigaid) {
	var taigaeditingfirstpost = false;
	
	var taigatext = document.getElementById(taigaid+'editbox').value;
	if (taigatext == '') {
		alert(lang__please_enter_text_in_the_post_area);
		return false;
	}
	
	var taigaParams = 'taiga=editpost&human='+urlencode(taigaid)+'&postext='+urlencode(taigatext);
	if (document.getElementById(taigaid+'posttitlearea')) {
		if (document.getElementById(taigaid+'posttitlearea').value == '') {
			alert(lang__please_enter_a_topic_title);
			return false;
		}
		taigaParams += '&title='+urlencode(document.getElementById(taigaid+'posttitlearea').value);
		taigaeditingfirstpost = true;
	}
	
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				//alert(xmlhttp.responseText);
				var taigamessages = xmlhttp.responseText.split("\n");
				if (taigamessages[0] == 'OK') {
					if (taigaeditingfirstpost == true) {
						document.location=document.location.pathname;
					} else {
						document.getElementById('edit_show_'+taigaid).innerHTML=taigamessages.slice(1).join(''); //should actually be only one since the bbcode module replaces newlines with <br />
						taigastyleswitchid2(taigaid, 'edit');
					}
					taigacurrentlyeditingpost--;
					taigalastextarea = 'posttextarea';
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigatimezonedateformatchange(taigatochange) {
	var taigaParams = 'taiga=setprefs&'+taigatochange+'='+urlencode(document.getElementById(taigatochange+'entry').value);
	//alert(taigaParams); return false;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				var taigamessages = xmlhttp.responseText.split("\n");
				taigamessages.reverse();
				if (taigamessages[0] == 'OK') {
					taigamessages.shift();
					document.getElementById('currentformat').innerHTML=taigamessages[0];
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigagroupcolorchange(taigagid, taigacolor) {
	var taigaParams = 'taiga=admin&adminparams='+urlencode('groupcolor='+taigacolor+'&gid='+taigagid);
// 	alert(taigaParams); return false;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(taigaParams);
}

function taiganotification(taigahuman, taigaonoff) {
	//for post/message view
	if (document.getElementById('notifypostview_'+taigahuman)) {
		taigaonoff = (document.getElementById('notifypostview_'+taigahuman).checked) ? '1' : '0';
	}
	var taigaParams = 'taiga=notify&human='+urlencode(taigahuman)+'&onoff='+taigaonoff;
// 	alert(taigaParams); return false;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					if (taigaonoff == '0') { //for edit profile
						//for edit profile
						if (document.getElementById('notify'+taigahuman)) {
							document.getElementById('notify'+taigahuman).innerHTML='';
						}
					}
				} else {
					alert(xmlhttp.responseText);
					document.getElementById('notifypostview_'+taigahuman).checked = (!(taigaonoff=='1'));
					return false;
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
				document.getElementById('notifypostview_'+taigahuman).checked = (!(taigaonoff=='1'));
				return false;
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigaignore(taigahuman, taigaonoff, taigatype) { //like notification only with taigatype for topics, forums, users ...
	//for post/message view
	if (document.getElementById('ignore_'+taigahuman)) {
		taigaonoff = (document.getElementById('ignore_'+taigahuman).checked) ? '1' : '0';
	}
	var taigaParams = 'taiga=ignore&ignoretype='+taigatype+'&human='+urlencode(taigahuman)+'&onoff='+taigaonoff;
// 	alert(taigaParams); return false;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					if (taigaonoff == '0') { //for edit profile
						if (document.getElementById('profileignore_'+taigatype+'_'+taigahuman)) {
							document.getElementById('profileignore_'+taigatype+'_'+taigahuman).innerHTML='';
						}
					}
				} else {
					alert(xmlhttp.responseText);
					document.getElementById('ignore_'+taigahuman).checked = (!(taigaonoff=='1'));
					return false;
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
				document.getElementById('ignore_'+taigahuman).checked = (!(taigaonoff=='1'));
				return false;
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigaeditprofile() {
	//var taigaParams = 'taiga=editprofile'; //happens below
	var editprofileparams = '';
	
	for (var i = 0; i < taigaprofilefields.length; i++) {
	var taigachanged = document.getElementById(taigaprofilefields[i]+'entry');
		if (taigachanged) {
			var taigavalue;
			if (taigachanged.type == 'checkbox') {
				taigavalue = (taigachanged.checked == true) ? 1 : 0;
			} else {
				taigavalue = taigachanged.value.replace(/\&/g, '&amp;'); //don't need to do = because i split on first = per param sent
			}
			editprofileparams += '&'+taigaprofilefields[i]+'entry'+'='+taigavalue;
		}
	}
	
	editprofileparams += '&birthdateentry=';
	if (
		(document.getElementById('birthdateyearentry') && document.getElementById('birthdateyearentry').value)
		&&
		(document.getElementById('birthdatemonthentry') && document.getElementById('birthdatemonthentry').value)
		&&
		(document.getElementById('birthdatedayentry') && document.getElementById('birthdatedayentry').value)
	) {
		var birthdateyear = document.getElementById('birthdateyearentry').value;
		var birthdatemonth = document.getElementById('birthdatemonthentry').value;
		var birthdateday = document.getElementById('birthdatedayentry').value;
		if (
			(
				(parseInt(birthdateyear, 10) < 100)
				||
				(parseInt(birthdateyear, 10) > 3000)
			)
		||
			(
				(parseInt(birthdatemonth, 10) < 1)
				||
				(parseInt(birthdatemonth, 10) > 12)
			)
		||
			(
				(parseInt(birthdateday, 10) < 1)
				||
				(parseInt(birthdateday, 10) > 31)
			)
		) {
			//alert(parseInt(birthdateyear, 10)+' < 100 or > 3000, '+parseInt(birthdatemonth, 10)+' < 1 or > 12, '+parseInt(birthdateday, 10)+' < 1 or > 31');
			alert(lang__if_you_are_going_to_enter_a_birthdate_please_enter_a_correct_one);
			return false;
		}
		editprofileparams += birthdateyear+'-'+birthdatemonth+'-'+birthdateday;
	}
	
	//get gender
	if (document.getElementById('genderentry')) {
		editprofileparams += '&genderempty='+document.getElementById('genderentry').value; //empty to get it past the whitelist
	}
	
	var taigaParams = 'taiga=editprofile&editprofileparams='+urlencode(editprofileparams);
// 	alert(taigaParams);
// 	return false;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				var taigamessages = xmlhttp.responseText.split("\n");
				taigamessages.reverse();
				if (taigamessages[0] == 'OK') {
					taigamessages.reverse();
					document.location=taigamessages[0];
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigapostpreview() {
	var taigaParams = 'taiga=previewpost';
	
	if (document.getElementById('posttextarea')) {
		document.getElementById('posttextarea').focus();
		if (document.getElementById('posttextarea').value == '') {
			return false;
		}
		taigaParams += '&newpost='+urlencode(document.getElementById('posttextarea').value); //empty to get it past the whitelist
	} else {
		alert(lang__error_posttextarea_not_found);
		return false;
	}
	
	if (document.getElementById('guestusername')) { //not logged in
		if (document.getElementById('guestusername').value == '') {
			alert(lang__please_enter_a_username);
			return false;
		}
		taigaParams += '&guestusername='+urlencode(document.getElementById('guestusername').value);
	}
	
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				var taigamessages = xmlhttp.responseText.split("\n");
				taigamessages.reverse();
				if (taigamessages[0] == 'OK') {
					taigamessages.reverse();
					document.getElementById('postpreviewarea').innerHTML=taigamessages[0];
					taigascrolltobottom();
					taigaautosave(false);
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}












function taigachangeavatar(taigaavatar) {
	var taigaParams = 'taiga=editprofile&editprofileparams='+urlencode('avatar='+taigaavatar);
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(taigaParams);
}

function taigareloadavatargallery() {
// 	document.location=document.location.pathname;
// 	return;
	
	var taigaParams = 'taiga=avatargallery';
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				document.getElementById('avatargallery').innerHTML=xmlhttp.responseText;
				if (!getElementsByClassName('f3_show').length && !getElementsByClassName('f3_hide').length) {
					taigastyleswitchhide('avataroptionsarea');
				} else {
					taigastyleswitchshow('avataroptionsarea');
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}



function taigasetselectedav() {
	if (taigaselectedavatar) {
		taigachangeavatar(taigaselectedavatar);
	} else {
		var taigaselectedguys = getElementsByClassName('f3_show');
		if (taigaselectedguys.length) {
			taigaselectedavatar = taigaselectedguys[0].id;
			taigaselectedavatar.replace(/galleryavatar_/, '');
		}
		
		if (taigaselectedavatar) {
			taigachangeavatar(taigaselectedavatar);
		} else {
			alert(lang__to_use_this_feature_please_click_on_an_avatar_from_your_avatar_gallery_or_upload_a_new_avatar);
			
			//can't do this because we don't know which one was checked previously ...
// 			var taigapreviousradio = 'noavbutton';
// 			if (document.getElementById('randomavbutton').checked) {
// 				taigapreviousradio = 'randomavbutton';
// 			}
// 			document.getElementById(taigapreviousradio).checked = true;
		}
	}
}

function taigasetrandomav() {
	taigahideallf3();
	taigachangeavatar('random');
}

function taigasetnoav() {
	document.getElementById('selectedavatarinner').innerHTML = '(none)';
	taigahideallf3();
	taigachangeavatar('kill');
	taigaselectedavatar = '';
}



function taigaavatardelete(taigaavatar) {
	var taigaParams = 'taiga=editprofile&editprofileparams='+urlencode('deleteavatar='+taigaavatar);
	//alert(taigaParams);
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (taigaavatar == taigaselectedavatar) {
					document.location=document.location.pathname; //have to reload to show if they deleted their current av
				} else {
					taigareloadavatargallery();
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigaavatarupload(taigaform) {
	if (document.getElementById('uploadavatarbutton')) {
		document.getElementById('uploadavatarbutton').checked=true;
	}
	document.getElementById('selectedavatarinner').innerHTML = lang__please_wait_dotdotdot;
	taigaform.submit();
}

function taigaavatardownload(taigaform) {
	if (document.getElementById('avatarurlentryinput').value != '') {
		if (document.getElementById('linkavatarbutton')) {
			document.getElementById('linkavatarbutton').checked=true;
		}
		document.getElementById('selectedavatarinner').innerHTML = lang__please_wait_dotdotdot;
		taigaform.submit();
	} else {
		alert(lang__please_enter_a_valid_url);
	}
}

function taigaavatarcomplete(taigaavatar) { //after uploading or downloading an av
	//document.getElementById('currentavatar').innerHTML = '<iframe id="new_avatar_iframe" name="new_avatar_iframe" src="'+taigaurl+'" style="width:60;height:60;border:0px solid #fff;"></iframe>';
	if (document.getElementById('noavbutton').checked && !getElementsByClassName('f3_show').length && !getElementsByClassName('f3_hide').length) { //they uploaded their first av to the gallery, so it's automatically selected for them
		document.getElementById('selectedavbutton').checked = true;
	}
	
	//if (!document.getElementById('randomavbutton').checked) {
		document.getElementById('selectedavatarinner').innerHTML = '<img class="avatarimage" src="/file/'+urlencode(taigaavatar)+'" />';
		taigaselectedavatar = taigaavatar;
	//}
	taigareloadavatargallery();
	document.location.hash='avatargallerybottom';
	if (!taigaloc()) { //not at the bottom
		window.scrollBy( f_scrollLeft(), (-document.getElementById('mainbarcontainer').scrollHeight) );
	}
}

function taigahideallf3() {
	var taigatoswitch = getElementsByClassName('f3_show');
	var taigathings = new Array();
	for (var i = 0; i < taigatoswitch.length; i++) {
		taigathings.push(taigatoswitch[i]);
	}
	for (var i = 0; i < taigathings.length; i++) {
		taigathings[i].className='f3_hide';
	}
}

function taigachangetoavataringallery(taigaavatar) {
	if (taigajustchangedorderbool) {
		return false;
	}
	taigahideallf3();
	document.getElementById('galleryavatar_'+taigaavatar).className='f3_show';
	
	//if (!document.getElementById('randomavbutton').checked) {
		document.getElementById('selectedavbutton').checked = true;
	//}
	
	document.getElementById('selectedavatarinner').innerHTML = '<img class="avatarimage" src="/file/'+urlencode(taigaavatar)+'" />';
	taigaselectedavatar = taigaavatar;
	
	taigachangeavatar(taigaavatar);
}














function taigaattachmentupload(taigaform, taigaattachmentnumber) {
	taigacurrentlyuploadingattachments = true;
	taigastyleswitchid2(taigaattachmentnumber, 'attachuploadstuff');
	taigaform.submit();
}

function taigalogoupload(taigaform) {
	taigacurrentlyuploadingattachments = true;
	document.getElementById('logofilenameentry').value = document.getElementById('attachmentpathentry').value;
	taigaform.submit();
	//taigastyleswitch('ajaxloading');
}

function taigaattachmentcomplete(taigafileid, taigaattachmentfilename, taigaattachmentnumber) {
	taigastyleswitchid2(taigaattachmentnumber, 'attachuploadstuff'); //go ahead and reset this in case they want to make another post with attachments ...
	taigastyleswitchid2hide(taigaattachmentnumber, 'attachouter'); //hide both the upload box and "please wait"
	document.getElementById('attachmentcomplete'+taigaattachmentnumber).innerHTML = '<i>'+taigaattachmentfilename+'</i>';
	taigaattachments.push(taigafileid);
	taigacurrentlyuploadingattachments = false;
}

function taigalogocomplete(taigafileid, taigaattachmentfilename, taigaattachmentnumber) { //last arg not needed
	document.getElementById('logoentry').value=taigafileid;
	document.getElementById('logofilenameentry').value=taigaattachmentfilename;
	taigacurrentlyuploadingattachments = false;
	//taigastyleswitch('ajaxloading');
	taigaadmin();
}


function taigacheckotherinfolength(taigaotherinfoentry) {
	document.getElementById('charsremaininginotherinfo').innerHTML = (500 - document.getElementById('otherinfoentry').value.length);
}

document.onkeydown=function(e) {
	if (!e) var e = window.event;
	var code;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code == 34) {
		if (document.getElementById('mainbarcontainer') && document.getElementById('bottomarea')) {
			window.scrollBy(f_scrollLeft(), (
				document.documentElement.clientHeight
				- document.getElementById('mainbarcontainer').scrollHeight
				- document.getElementById('bottomarea').scrollHeight
			) );
			return false;
		}
	} else if (code == 33) {
		if (document.getElementById('mainbarcontainer') && document.getElementById('bottomarea')) {
			window.scrollBy(f_scrollLeft(), (
				0
				- document.documentElement.clientHeight
				+ document.getElementById('mainbarcontainer').scrollHeight
				+ document.getElementById('bottomarea').scrollHeight
			) );
			return false;
		}
	}
}

//close popup dialogs when user clicks outside them
var taigaclickprefixes = ['view', 'undo'];
document.onclick = checkpopups;
function checkpopups(e) {
	var target = (e && e.target) || (event && event.srcElement);
	
	for (var i = 0; i < taigaclickprefixes.length; i++) {
		var prefix = taigaclickprefixes[i];
		if (checkpopupParentView(target, prefix))
			taigastyleswitch(prefix+'settings');
	}
}

function checkpopupParentView(target, prefix) {
	if (getElementsByClassName(prefix+'settings_hide').length > getElementsByClassName(prefix+'settings_show').length) { //is the popup even showing atm?
		return false;
	}
	
	while(target.parentNode) {
		if( (target == document.getElementById(prefix+'settingscontainer')) || (target == document.getElementById(prefix+'container')) ) {
			return false; //they clicked inside the thing - don't hide it
		}
		target = target.parentNode
	}
	
	return true;
}

//disable/enable links in a div
var linkarray = new Array();

function disablelinks() {
	var divlinks = document.getElementById('wholepagecontainer').getElementsByTagName('a');
	for(var i = 0; i < divlinks.length; i++) {
		linkarray[i] = divlinks[i].href.toString();

		if (divlinks[i].id == 'ssomething')
		{ /*can skip some with this*/
		}

		else {
			divlinks[i].disabled = true;
			divlinks[i].onclick = new Function("return false;");
		}
	}
}

function enablelinks() {
	var divlinks = document.getElementById('wholepagecontainer').getElementsByTagName('a');
	for(var i = 0; i < divlinks.length; i++) {
		divlinks[i].disabled = false;
		divlinks[i].href = linkarray[i];
		divlinks[i].onclick = linkarray[i];
	}
}

function taigamainbar(taigamode, taigaid, taigasortable) {
	var taigaitems = new Array;
	
	if ( (taigamode == 'delete') && (taigaid == undefined) ) { //revert
		taigaitems.push('revert=1');
	} else if ( (taigamode == 'delete') || (taigamode == 'save') ) {
		for (var i = 1; i <= 10; i++) {
			if (!document.getElementById('mainbarurl'+i)) {
				continue;
			}
			var taigaurl = document.getElementById('mainbarurl'+i);
			var taigatext = document.getElementById('mainbartext'+i);
			if ( (taigaurl.value == '') || (taigatext.value == '') ) {
				continue;
			}
			var taigarealmainbarid = taigaurl.name;
			if ( (taigamode == 'delete') && (taigaid == taigarealmainbarid) ) {
				continue;
			}
			taigaitems.push('save'+taigarealmainbarid+'='+urlencode('url='+urlencode(taigaurl.value)+'&text='+urlencode(taigatext.value)));
		}
	} else if (taigamode == 'cancel') {
		document.location=document.location.pathname;
		return;
	}
	
	var taigaParams = 'taiga=mainbar&mainbarparams='+urlencode(taigaitems.join('&'));
	//alert(taigaParams);
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				taigaorder(taigamainbarorder, 'editmainbarlinkcontainers', 'mainbar', 'mainbarparams', 'order');
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigaorder(taigasortable, taigasep, taigarm, taigaparamskey, taigaparamname) {
	//categoriescontainer[]=1&categoriescontainer[]=3&categoriescontainer[]=2&categoriescontainer[]=4&categoriescontainer[]=5&categoriescontainer[]=6
	taigasortable = taigasortable.substring((taigasep+'[]=').length)
	var taigaforums = taigasortable.split('&'+taigasep+'[]=');
	var taigaParams = 'taiga='+taigarm+'&'+taigaparamskey+'='+urlencode(taigaparamname+'='+taigaforums.join(','));
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				//if (xmlhttp.responseText != 'OK') {
				if (!(/OK$/.test(xmlhttp.responseText))) { //for profile edit view, ends in \nOK whereas other views are just 'OK'
					document.location=document.location.pathname;
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigaorderforums(taigasortable) {
	taigaorder(taigasortable, 'categoriescontainer', 'admin', 'adminparams', 'forumorder');
}

function taigaordermainbar(taigasortable) {
	taigamainbarorder = taigasortable;
}

function taigaorderavatars(taigasortable) {
	taigaorder(taigasortable, 'avatargallery', 'editprofile', 'editprofileparams', 'avorder');
	taigajustchangedorder();
}

function taigajustchangedorder(taigacallback) {
	if (!taigacallback) {
		taigajustchangedorderbool = true;
		setTimeout("taigajustchangedorder(true)", 1000);
	} else {
		taigajustchangedorderbool = false;
	}
}

function taigaadmin() {
	if (taigacurrentlyuploadingattachments == true) {
		alert(lang__you_must_wait_until_your_logo_finishes_uploading_to_save_your_settings);
		return false;
	}
	
	var adminparams = '';
	
	for (var i = 0; i < taigasettings.length; i++) {
		if (document.getElementById(taigasettings[i]+'entry')) {
			var taigainput = document.getElementById(taigasettings[i]+'entry');
			var taigavalue = taigainput.value.replace(/\&/g, '&amp;'); //don't need to do = because i split on first = per param sent
			if (taigainput.type == 'checkbox') {
				taigavalue=(taigainput.checked ? '1' : '0');
			}
			adminparams += '&'+taigasettings[i]+'entry'+'='+taigavalue;
		}
	}

	var taigaParams = 'taiga=admin&adminparams='+urlencode(adminparams);
// 	alert(taigaParams); return false;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					document.location=document.location.pathname;
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigaperm(taigacheckboxid, taigagid, taigapermname, taigalabelclick) {
	if (document.getElementById(taigacheckboxid).disabled == true) {
		return false;
	}
	var taigachecked = document.getElementById(taigacheckboxid).checked;
	if (taigalabelclick == true) {
		taigachecked = !taigachecked;
	}
	var adminparams;
	(taigachecked == true) ? (adminparams = 'givegroupglobalpermission') : (adminparams = 'removegroupglobalpermission');
	var taigaParams = 'taiga=admin&adminparams='+urlencode(adminparams+'='+taigagid+'&name='+taigapermname);
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					//document.location=document.location.pathname;
					if (taigalabelclick == true) {
						document.getElementById(taigacheckboxid).checked = !document.getElementById(taigacheckboxid).checked;
					}
				} else {
					alert(xmlhttp.responseText);
					document.getElementById(taigacheckboxid).checked = !document.getElementById(taigacheckboxid).checked;
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
				document.getElementById(taigacheckboxid).checked = !document.getElementById(taigacheckboxid).checked;
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigagroupvisibility(taigacheckboxid, taigagid, taigafid, taigalabelclick) {
	if (document.getElementById(taigacheckboxid).disabled == true) {
		return false;
	}
	var taigachecked = document.getElementById(taigacheckboxid).checked;
	if (taigalabelclick == true) {
		taigachecked = !taigachecked;
	}
	var adminparams;
	(taigachecked == true) ? (adminparams = 'addgroupvisibility') : (adminparams = 'removegroupvisibility');
	var taigaParams = 'taiga=admin&adminparams='+urlencode(adminparams+'='+taigagid+'&name='+taigafid);
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					//document.location=document.location.pathname;
					if (taigalabelclick == true) {
						document.getElementById(taigacheckboxid).checked = !document.getElementById(taigacheckboxid).checked;
					}
				} else {
					alert(xmlhttp.responseText);
					document.getElementById(taigacheckboxid).checked = !document.getElementById(taigacheckboxid).checked;
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
				document.getElementById(taigacheckboxid).checked = !document.getElementById(taigacheckboxid).checked;
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigauseraddremove(taigaadminparam, taigagid, taigainput) { //gid may be fid in the case of adding forum mods
	var taigauid = taigainput;
	
	//confirm removing self from admin
	if ( (taigaadminparam == 'removefromgroup') && (taigagid == '0') && (taigauid == myuid) ) {
		if (!(confirm(lang__you_are_about_to_remove_yourself_as_an_administrator__this_will_lock_you_out_of_the_admin_panel_forever__really_remove_yourself_as_administrator))) {
			return false;
		}
	}
	
	if ( (taigaadminparam == 'addtogroup') || (taigaadminparam == 'addforummod') ) {
		taigauid = document.getElementById(taigainput).value;
		if (taigauid == '') {
			alert(lang__please_enter_a_name);
			return false;
		}
	}
	var taigaParams = 'taiga=admin&adminparams='+urlencode(taigaadminparam+'='+taigagid+'&name='+taigauid);
	//if (taigaadminparam == 'removefromgroup') { alert(taigaParams); return false; }
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					if ( (taigaadminparam == 'removefromgroup') && (taigagid == '0') && (taigauid == myuid) ) {
						document.location='/';
					} else {
						document.location=document.location.pathname;
					}
					//return true;
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigagroupcreatedestroy(taigaadminparam, taigainput) {
	var taigagid = taigainput;
	
	//confirm deleting a usergroup
	if (taigaadminparam == 'deletegroup') {
		if (!(confirm(lang__you_are_about_to_delete_an_enter_usergroup__you_cannot_undo_this_action__really_delete_this_group))) {
			return false;
		}
	}
	
	if (taigaadminparam == 'newgroup') {
		taigagid = document.getElementById(taigainput).value;
		if (taigagid == '') {
			alert(lang__please_enter_a_name);
			return false;
		}
	}
	var taigaParams = 'taiga=admin&adminparams='+urlencode(taigaadminparam+'='+taigagid+'&name='+taigagid);
	//if (taigaadminparam == 'newgroup') { alert(taigaParams); return false; }
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					document.location=document.location.pathname;
					//return true;
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigavote(taigapolloptionid) {
	var taigaParams = 'taiga=vote&polloptionid='+taigapolloptionid;
	//alert(taigaParams); return false;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.status == 200) {
				var taigamessages = xmlhttp.responseText.split("\n");
				if (taigamessages[0] == 'OK') {
					taigamessages.shift();
					document.getElementById('pollarea').innerHTML=taigamessages.join("\n");
				} else {
					alert(xmlhttp.responseText);
					//reset ui
					//var taigaboxtoreset = document.getElementById('polloption'+taigapolloptionid);
					//if (taigaboxtoreset) {
					//	taigaboxtoreset.checked = ((taigaboxtoreset.checked == true) ? false : true);
					//}
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	xmlhttp.send(taigaParams);
}

function taigabanunban(taigauid, taigatobanornottoban) {
	var taigaParams = 'taiga=banunban&uidtoban='+taigauid+'&tobanornottoban='+taigatobanornottoban;
	//alert(taigaParams); return false;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					taigastyleswitch('banuser');
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigadeleteuser(taigauid) {
	var taigaParams = 'taiga=deleteuser&uidtodelete='+taigauid+'&deleteallposts='+(document.getElementById('deleteuserdeleteallposts'+taigauid).checked ? '1' : '0');
	//alert(taigaParams); return false;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					alert(lang__success);
					document.location='/';
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigadeleteattachment(taigahumanform, taigafileid) {
	var taigaParams = 'taiga=deleteattachment&fileid='+taigafileid; //taigafileid is already urlencoded
	//alert(taigaParams); return false;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", taigahumanform, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					document.location=document.location.pathname;
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigasavecaret(taigatextarea) {
	if (taigatextarea.isTextEdit) {
		taigatextarea.caretPos = document.selection.createRange();
	}
	taigalastextarea = taigatextarea.id;
}

function taigabbcodeinsert(taigaid, taigatextareaid, taigadefault, taigasmiley, taigaquotetext) {
	var taigatextarea = document.getElementById(taigatextareaid);
	var taigafirsttag = '['+taigaid+']';
	var taigalasttag = '[/'+taigaid+']';
	if (taigasmiley == true) {
		taigafirsttag = taigaid;
		taigalasttag = '';
	} else if (taigadefault != null) {
		taigafirsttag = '['+taigaid+'='+taigadefault+']';
	}
	if (taigaquotetext != null) {
		taigafirsttag = '['+taigaid+'="'+taigadefault+'"]';
		taigafirsttag = taigafirsttag + taigaquotetext + taigalasttag;
		taigalasttag = '';
	}
	
	//if (taigatextarea.createTextRange) { //ie
	if (taigatextarea.createTextRange && (taigatextarea.isTextEdit && taigatextarea.caretPos)) { //ie
		var taigaloc = taigatextarea.caretPos;
		var taigalength = taigaloc.text.length;
		
		var taigatoinsert = taigaloc.text.charAt(taigaloc.text.length - 1) == ' ' 
			? taigafirsttag + taigaloc.text + taigalasttag + ' ' 
			: taigafirsttag + taigaloc.text + taigalasttag;
		taigaloc.text = taigatoinsert;

		if (taigalength == 0) {
			taigaloc.moveStart("character", -taigalasttag.length);
			taigaloc.moveEnd("character", -taigalasttag.length);
		} else {
			taigaloc.moveStart("character", -taigatoinsert.length);
		}
		taigaloc.select();
	} else if (taigatextarea.createTextRange && (taigatextarea.selectionStart || (taigatextarea.selectionStart == '0'))) { //ie
		var before = taigatextarea.value.substring(0,taigatextarea.selectionStart);
		var after = taigatextarea.value.substring(taigatextarea.selectionEnd,taigatextarea.value.length);
		var selText = taigatextarea.value.substring(taigatextarea.selectionStart,taigatextarea.selectionEnd);
		taigatextarea.value = before+taigafirsttag+selText+taigalasttag+after;
		//}
	} else if (typeof(taigatextarea.selectionStart) != "undefined") { //firefox
		var taigaselection = taigatextarea.value.substr(taigatextarea.selectionStart, taigatextarea.selectionEnd - taigatextarea.selectionStart);
		var taigaloc = taigatextarea.selectionStart;
		var taigascroll = taigatextarea.scrollTop;

		taigatextarea.value = taigatextarea.value.substr(0, taigatextarea.selectionStart) + taigafirsttag + taigaselection + taigalasttag + taigatextarea.value.substr(taigatextarea.selectionEnd);

		if (taigatextarea.setSelectionRange) {
			if (taigaselection.length == 0) {
				taigatextarea.setSelectionRange(taigaloc + taigafirsttag.length, taigaloc + taigafirsttag.length);
			} else {
				taigatextarea.setSelectionRange(taigaloc, taigaloc + taigafirsttag.length + taigaselection.length + taigalasttag.length);
			}
			taigatextarea.focus();
		}
		taigatextarea.scrollTop = taigascroll;
	} else {
		taigatextarea.value += taigafirsttag + taigalasttag;
		taigatextarea.focus(taigatextarea.value.length - 1);
	}
}

function taigaquote(taigahuman, taigausername) {
	var taigatextarea = taigalastextarea ? taigalastextarea : 'posttextarea';
	if (taigatextarea == 'posttextarea') { //have to show it first of all for sending text into it in at least firefox
		taigastyleswitchshow('postbox');
	}
	taigabbcodeinsert('quote', taigatextarea, taigausername, null, document.getElementById(taigahuman+'editbox').value);
	
	document.getElementById(taigatextarea).focus();
	taigastyleswitchhide('editpollarea');
}

function taigasearchsubmit() {
	if (document.getElementById('searchinput').value == '') {
		return false;
	}
}

function taigarecoverpassword() {
	var taigausername;
	if (document.getElementById('loginusernameinput')) {
		taigausername = document.getElementById('loginusernameinput').value;
	} else {
		alert(lang__error__username_element_not_found);
		return false;
	}
	
	if (taigausername == '') {
		alert(lang__please_type_in_your_username);
		return false;
	}
	
	var taigaParams = 'taiga=recoverpassword&sern='+taigausername;
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					alert(lang__weve_sent_an_email_to_the_address_in_your_profile_with_a_new_password_for_you);
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function taigadeletepoll() {
	var taigaParams = 'taiga=deletepoll';
	var xmlhttp = taigaNewAjax();
	xmlhttp.open("POST", document.location.pathname, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			taigastyleswitch('ajaxloading');
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText == 'OK') {
					document.location=document.location.pathname;
				} else {
					alert(xmlhttp.responseText);
				}
			} else {
				alert(lang__sorry_an_error_occurred_please_try_your_request_again);
			}
		}
	}
	taigastyleswitch('ajaxloading');
	xmlhttp.send(taigaParams);
}

function nodigits(e) {
	var keynum;
	var keychar;
	var numcheck;

	if(window.event) { // internet explorer
		keynum = e.keyCode;
	}
	else if(e.which) { // firefox/safari/opera
  		keynum = e.which;
	}

	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	return !numcheck.test(keychar);
}

function onlydigits(e)
{
	var charcode = (e.which) ? e.which : event.keyCode;

	if(charcode == 32 || charcode == 9 || charcode > 57)
		return false;

	return true;
}

function toggle(boxid)
{
	if(document.getElementById(boxid).checked == false)
		document.getElementById(boxid).checked=true;
	else
		document.getElementById(boxid).checked=false;
}

function clearpassfields()
{
	if (document.getElementById('password')) {
		document.getElementById('password').value = "";
	}
	document.getElementById('newpassword1').value = "";
	document.getElementById('newpassword2').value = "";
}

function taigasetitle() {
	if (taigablurred) {
		if (taiganewposts > 0) {
			document.title = '('+taiganewposts+') '+taigatitle;
		} else {
			document.title = taigatitle;
		}
	}
}

function taigacancelnewpost() {
	document.getElementById('posttextarea').value = '';
	if (document.getElementById('posttitlearea')) {
		document.getElementById('posttitlearea').value = '';
	}
	if (document.getElementById('postrecipientarea')) {
		document.getElementById('postrecipientarea').value = '';
	}
	if (document.getElementById('postpreviewarea')) {
		document.getElementById('postpreviewarea').innerHTML='';
	}
	taigaautosave(false);
	taigastyleswitchhide('postbox');
}

function taigaimageresize(taigaimage) {
	if (taigaimage.className=='imgimage') {
		taigaimage.className='imgimagenomaxwidth';
	} else {
		taigaimage.className='imgimage';
	}
}




window.onload = function(){
	if (taigajumptopost) {
		document.location.hash=taigajumptopost;
		if (!taigaloc()) { //not at the bottom
			window.scrollBy( f_scrollLeft(), (-document.getElementById('mainbarcontainer').scrollHeight) );
		}
	}
	
	taigapoll();
	setTimeout("taigaautosave(true)",taigaautosaveinterval);
	
	if (taigathisisthelastpage) {
		taigagetnewposts();
		taigaajaxtimeoutinc();
	}
};

window.onbeforeunload = function() {
	if (taigagetnewpostsajax) {
		//alert('onbeforeunload aborting');
		taigagetnewpostsajax.abort();
	}

	
	if (document.getElementById('posttextarea') && !document.getElementById('posttitlearea')) {
		if (document.getElementById('posttextarea').value != '') {
			taigaonbeforeunloadalreadysaved = true;
			//return 
			taigaautosave(false);
		}
	}
}

window.onunload = function() {
// 	if ( document.getElementById('posttextarea') && (document.getElementById('posttextarea').value != '') && (document.getElementById('newpostbutton').className == 'postbox_show') ) {
// 		return "You have entered text in the post field. You will lose this text if you close the tab.";
// 	}
	if (taigagetnewpostsajax) {
		//alert('onunload aborting');
		taigagetnewpostsajax.abort();
	}
	
	if (document.getElementById('posttextarea') && !document.getElementById('posttitlearea') && !taigaonbeforeunloadalreadysaved) {
		if (document.getElementById('posttextarea').value != '') {
			//return 
			taigaautosave(false);
		}
	}
};


window.onblur = function() {
	taigablurred = true;
	taiganewposts = 0;
};

window.onfocus = function () {
	taigablurred = false;
	taiganewposts = 0;
	document.title = taigatitle;
};
