var regex_variable,regex_var_str,newline_button,x,i,Extractions,sepExtractions,innerExtractions;
undefined;null;
// Speeds up all mentionings of these variables in some browsers

function while_replace(text,replacetext,withtext) {
	while(text.split(replacetext).length>1) {
		text = text.replace(replacetext,withtext);

	}
	return text;
}

function isPtype(ptype) {
	switch(ptype) {
		case 'all': 	if(window.location.href.match(/showtopic=/) || window.location.href.match(/act=ST/) || window.location.href.match(/act=Post/) || document.title.match('-> Posting New Topic') || document.title.match('-> Replying in ') || document.title.match('-> Editing Post')  || document.title.match('-> Quoting a Post') || window.location.href.match(/act=Msg/) || window.location.href.match(/CODE=22/) || window.location.href.match(/act=site/)) return true;
			break;
		case 'topic':	if(window.location.href.match(/showtopic=/) || window.location.href.match(/act=ST/) ) return true; 
			break;
		case 'post':	if(window.location.href.match(/act=Post/) || document.title.match('-> Posting New Topic') || document.title.match('-> Replying in ') || document.title.match('-> Editing Post')  || document.title.match('-> Quoting a Post') || window.location.href.match(/act=Msg/) ) return true; 
			break;
		case 'sig':	if(window.location.href.match(/CODE=22/) ) return true; 
			break;
		case 'profile':	if(window.location.href.match(/showuser=/) ) return true; 
			break;
		case 'portal':	if(window.location.href.match(/act=site/) ) return true;
			break;
	}
	return false;		
}

function ubb_tag(name,beg,wrapHTML,variable,description,signature,button,button_val,old) {
	if( isPtype('all') ) {
		if(beg == wrapHTML || isFinite(description)) {
			if(description == 1) description = -2;
			if (button == undefined){button = 3;}else if(!isFinite(button)){old = button;button = 3;}
			ubb_tag2(name,beg,variable,description,signature,button,'div');
			ubb_tag2(name,beg,variable,description,signature,button,'td');
			ubb_tag2(name,beg,variable,description,signature,button,'span');
			if(button_val == 0) return;
			variable = description;
			description = signature;
			if	(old){		button_val = old;}
			else if	(button_val){	button = button_val;button_val = old;}
			else if	(button){	signature = button;button = button_val;button_val = old;}
		} else {
			if(signature == undefined){ signature = 3;}else if(!isFinite(signature)){button_val=signature;signature=3;}
			ubb_tag2(name,beg,wrapHTML,variable,description,signature,'div');
			ubb_tag2(name,beg,wrapHTML,variable,description,signature,'td');
			ubb_tag2(name,beg,wrapHTML,variable,description,signature,'span');
		}

		if (isPtype('sig') && (signature == 0 || signature == 2) ) return;
		if( (isPtype('sig') || isPtype('post')) && (button == undefined || button > 0 || !isFinite(button)) ) {

			x = document.getElementsByTagName('input');
			for(i=0;i<x.length;i++) {
				if(x[i].value == ' LIST ') {
					if(!(x[i].parentNode.innerHTML.match('@@#@@'))) x[i].parentNode.innerHTML += '<div style="display: none;">@@#@@</div><br>';
					if (button != undefined && !isFinite(button)) {
						button_val = button;
					} else if(button_val == undefined || button_val == '') {
						button_val = name;
					}		
					if(signature == 4 || button == 2) {
						newline_button = '<br />';
					} else {
						newline_button = '';
					} 
					x[i].parentNode.innerHTML += "<button type='button' onclick=\"add_ubb_tag_to_post('" + beg + "'," + variable + ",'" + description + "')\" onmouseover=\"hstat_xanik('" + name + ": " + description.split('||')[0] + "')\" class='codebuttons' />" + button_val + "</button> " + newline_button;
				}
			}
		}
	}
}



function ubb_tag2(name,beg,wrapHTML,variable,description,signature,tagtag) {
	x=document.getElementsByTagName(tagtag);
	i=x.length;
	while(--i>0) { //Faster way to loop in some browsers
		if(x[i].innerHTML.match(beg) && x[i].className == 'postcolor' && x[i].id != 'CODE' || x[i].id == 'QUOTE' || ( x[i].className == 'signature' && (signature == 1 || signature > 2) ) || (tagtag == 'td' &&  (signature >= 2) && ( x[i].childNodes[0].name == 'poll_vote' || x[i].className == 'row1' ) ) ) {
			if(variable == 0) {
				regex_variable = new RegExp ("\\[" + beg + "\\](.*?|\\s*?)\\[\\/" + beg + "\\]","g") ;
	  			while(x[i].innerHTML.match(regex_variable)) {
					x[i].innerHTML = x[i].innerHTML.replace(regex_variable, wrapHTML = wrapHTML.replace(/<% INNER %>/g, '\$1'));
				}
			} else if(variable == 1) {
				regex_variable = new RegExp ("\\[" + beg + "=([^\\]]+)\\](.*?|\\s*)\\[\\/" + beg + "\\]","g") ;
	  			while(x[i].innerHTML.match(regex_variable)) {
					x[i].innerHTML = x[i].innerHTML.replace(regex_variable, wrapHTML = wrapHTML.replace(/<% INNER %>/g, '\$2').replace(/<% VAR(?:\[1\])? %>/g, '\$1'));
				}
			} else if(variable > 1) {
				regex_var_str = "(.+?)";
				for(a=1;a<variable;a++) {
					regex_var_str += ",(.+?)";
				}
				regex_variable = new RegExp ("\\[" + beg + "="+regex_var_str+"\\](.*?|\\s*)\\[\\/" + beg + "\\]","g");
				while(x[i].innerHTML.match(regex_variable) ) {
					x[i].innerHTML = x[i].innerHTML.replace(regex_variable, wrapHTML = wrapHTML.replace(/<% INNER %>/g, '\$' + (variable+1)).replace(/<% VAR(?:\[1\])? %>/g, '\$1').replace(/<% VAR\[(\d+)\] %>/g, "$$$1") );
				}
			} else if(variable == -1) {
				regex_variable = new RegExp ("\\[" + beg + "\\]","g");
				x[i].innerHTML = x[i].innerHTML.replace(regex_variable,wrapHTML);
			} else if(variable == -2) {
				regex_variable = new RegExp ("\\[" + beg + "=([^\\]]+)\\](.*?|\\s*?)\\[\\/" + beg + "\\]","") ;
				while(x[i].innerHTML.match(regex_variable) ) {
					Extractions = regex_variable.exec(x[i].innerHTML);
					innerExtraction = Extractions[2];
					sepExtractions = Extractions[1].split(',');
					wrapHTML = wrapHTML.replace(/<% INNER %>/g,innerExtraction);
					if(sepExtractions.length<1) {
						wrapHTML = wrapHTML.replace(/<% VAR %>/g,sepExtractions);
					} else {
						wrapHTML = wrapHTML.replace(/<% VAR %>/g,sepExtractions[0]);
						for(a=0;a<sepExtractions.length;a++) {
							wrapHTML = wrapHTML.replace(new RegExp("<% VAR\\[" + a + "\\] %>","g"),sepExtractions[a]);
						}
					}
				x[i].innerHTML = while_replace(x[i].innerHTML,'[' + beg + '=' + Extractions[1] + ']' + innerExtraction + '[/' + beg + ']',wrapHTML)
				}
			}
		}
	}
}

// End of BB Replace function

function hstat_xanik(text) {
	document.REPLIER.helpbox.value = text;
}

function add_ubb_tag_to_post(opentag,variable,description) {
	description = description.split('||');
	var input_array = new Array();

	if(variable == -1) {
		doInsert('[' + opentag + ']',"",false);
		return;
	} else if(variable == 0) {
		input_array[0] = prompt(description[0] + '\n' + description[1],'');
		if(!input_array[0]) {
			alert('Action Cancelled');
			return;
		}
	} else {
		for(ggjj=0;ggjj<description.length-1;ggjj++) {
			input_array[ggjj] = prompt(description[0] + '\n' + description[ggjj+1],'');
			if(!input_array[ggjj]) {
				alert('Action Cancelled');
				return;
			}
		}
	}

	if(variable == 0) {
		doInsert('[' + opentag + ']' + input_array[0] + '[/' + opentag + ']',"",false);
	} else if(variable > 0 || (variable == -2 && description.length-1>0) ) {
		doInsert('[' + opentag + '=' + input_array.join(",").replace(input_array[0] + ',','') + ']' + input_array[0] + '[/' + opentag + ']',"",false);
	}
}

document.write('<div style="display: none;"><scr'+'ipt>var sc_project=4106615;var sc_invisible=1;var sc_partition=49;var sc_click_stat=1;var sc_security="e23ef93b";</sc'+'ript><scr'+'ipt type="text/javascript" src="http://www.statcounter.com/counter/counter.js"></scr'+'ipt><noscript><div class="statcounter"><a title="free hit counter" href="http://www.statcounter.com/" target="_blank"><img class="statcounter" src="http://c.statcounter.com/4106615/0/e23ef93b/1/" alt="free hit counter" ></a></div></noscript></div>');
//If you want to remove this, use this instead: http://209.85.12.227/6877/58/upload/p6732418.ibf
