// NexusBB BASE SCRIPTS
// Not much annotation yet

var wth,hgt; // onresize layer stuffup fix for stupid nn4
if (document.layers) {
	wth = window.innerWidth; hgt = window.innerHeight;
	window.onresize = function() {
		if (window.innerWidth != wth || window.innerHeight != hgt)
			history.go(0);
	}
}

// Reference an element by ID
function byID(id) {
	return document.getElementById ?  document.getElementById(id) : document.all[id];
}

// Reference an element style by ID (NS4 compatible)
function byIDNS4(id) {
	return document.layers ? document.layers[id] :
	document.getElementById ?  document.getElementById(id).style : 
	document.all[id].style;
}

// Show or hide layers, may as well make this work in NS4
function showhide(id,status) {
	var ele = byIDNS4(id);
	ele.display = (ele.display == '' && status != 1) ? 'none' : '';
}

// Update status of image depending on state
function updatestatus(id,status1,status2) {
	var ele = byID(id);
	ele.src = (ele.src.match(new RegExp(status2))) ? 'themes/nxf/'+status1+'.gif' : 'themes/nxf/'+status2+'.gif';
}

// small window for info windows etc.
function barewin(url,hei,wid){
  window.open(url, '', 'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=yes, scrollbars=yes, width='+hei+', height='+wid+', left=20, top=20');
}

// full size window
function link(url,hei,wid){
  window.open(url, '', 'toolbar=yes, directories=yes, location=yes, status=yes, menubar=yes, resizable=yes, scrollbars=yes, width='+hei+', height='+wid+', left=15, top=15');
}

// Put text directly at cursor position, ie. for smilies
function atcursor (fld,val) {
	if (document.selection){ // IE
		var start=fld.selectionStart;
		fld.focus(); document.selection.createRange().text = val; }
	else if (fld.selectionStart || fld.selectionStart == '0'){ // Moz/NN
		var start=fld.selectionStart;
		fld.value = fld.value.substring(0,start) + val +
			fld.value.substring(fld.selectionEnd);
	}else{	fld.value += val; var start=fld.length-val.length }
	fld.setSelectionRange(start+val.length,start+val.length); fld.focus(); }

// BBcode insertion
// Using a flag (the state of the element used to trigger BBcode), this puts
// BBcode into the post box or wraps a selection with the appropiate code.
function bbcode (ele,fld,val1,val2) {
	if (1==1) {
		// IE compatible (a pain in the ass)
		fld.focus();
		if (document.selection) {
			if (document.selection.createRange().text){
				var seltext = document.selection.createRange().text;
				document.selection.createRange().text = val1 + seltext;
			} else {
	  			if (ele.style.fontWeight != 'bold') {
					document.selection.createRange().text = val1;
					ele.style.fontWeight = 'bold';
				} else {
					document.selection.createRange().text = val2;
					ele.style.fontWeight = '';
				}
			}
		// Moz/NN compatible (nice and simple)
		} else if (fld.selectionStart || fld.selectionStart == '0') {
			var start=fld.selectionStart;
			var end=fld.selectionEnd;
			if (end > start) {
				fld.value = fld.value.substring(0,start) + 
					val1 + fld.value.substring(start,end) + 
					val2 + fld.value.substring(end);
				fld.setSelectionRange(end+val1.length+val2.length,
									end+val1.length+val2.length);
			} else {
	  			if (ele.style.fontWeight != 'bold') {
					fld.value = fld.value.substring(0,start) + 
						val1 + fld.value.substring(end);
					ele.style.fontWeight = 'bold';
				} else {
					fld.value = fld.value.substring(0,start) + 
						val2 + fld.value.substring(end);
					ele.style.fontWeight = '';
				}
				fld.setSelectionRange(end+val2.length,end+val2.length);
			}
		// Alternative method
		} else {
			fld.value += val1 + val2;
		}
	} else {
		// Use open and close tags at once method placing cursor in between
		// Moz/NN compatible (IE doesn't support this - no surprise)
		if (fld.selectionStart || fld.selectionStart == '0') {
			var start=fld.selectionStart;
			var end=fld.selectionEnd;
			fld.value = fld.value.substring(0,start) + 
				val1 + fld.value.substring(start,end) + 
				val2 + fld.value.substring(end);
			fld.setSelectionRange(end+val1.length,end+val1.length);
		}
	}
}

// IE set cursor position
function setCurPos(fld,pos){
	if (fld.createTextRange) {
		var rng = fld.createTextRange();
		rng.moveStart('character',pos);
		rng.collapse();
		rng.select();
	}
}

// I know, page specific functions should go into a different JS file
function validate(form){
	var error = '';
	switch(form){
		case 'searchform':
			if(document.searchform.searchfor.value.length < 4) error += 'Search string must be 4 or more characters\n';
			if(document.searchform.searchfor.value == '****') error += 'You must enter a keyword\n';
			break;
		case 'battlejoin':
			if(!document.battlejoin.onlyarmy.checked) error += 'You must agree to have only one army\n';
			break;
	}
}