function pop(theurl, w, h, scroll)
{
 var the_atts = "width="+w+", height="+h+", top=20, screenY=20, left=20, screenX=20,  toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars="+scroll+", resizable=yes, copyhistory=no";
 // open window
 window.open(theurl,'pop',the_atts);
}

function more_info(sid) {
   if(document.getElementById('more_' + sid).style.display == 'none') {
       document.getElementById('more_' + sid).style.display = 'block';
       document.getElementById('info_' + sid).style.display = 'none';
   }
   else {
       document.getElementById('info_' + sid).style.display = 'block';
       document.getElementById('more_' + sid).style.display = 'none';
   }
}

function category(pid, catid, name, locked, order) {
	this.pid = pid;
	this.catid = catid;
	this.name = name;
	this.locked = locked;
	this.order = order;
}

function character(charid, catid, charname) { 
	this.charid = charid;
	this.catid = catid;
	this.charname = charname;
}

function resetCats(catoptions) {
	var selItem = document.getElementById(catoptions).selectedIndex;
	var selValue = document.getElementById(catoptions).options[selItem].value;
	var element = document.getElementById(catoptions);
	element.options.length = 0;
	element.options[element.options.length] = new Option(lang['Back2Cats'], -1);
	for(z = 0; z < categories.length; z++) {
		if(categories[z].pid == selValue || categories[z].catid == selValue) 
			element.options[element.options.length] = new Option(categories[z].name, categories[z].catid);
	}
	if(selValue != -1) element.options.selectedIndex = 1;
}

function addCat(fromGroup, toGroup) {
	var strValues = "";
	var listLength = document.getElementById(toGroup).length;
	var selItem = document.getElementById(fromGroup).selectedIndex;
	var selValue = document.getElementById(fromGroup).options[selItem].value;
	var selText = document.getElementById(fromGroup).options[selItem].text;
	var newItem = true;
	strValues = selValue;
	for(i = 0; i < listLength; i++) {
		strValues = strValues + "," + document.getElementById(toGroup).options[i].value;
		if(document.getElementById(toGroup).options[i].text == selText) {
			newItem = false;
			break;
		}
	}
	if(newItem) {
		document.getElementById(toGroup).options[listLength] = new Option(selText, selValue);
		document.getElementById(fromGroup).options[selItem] = null;
		document.getElementById("catid").value = strValues;
	}
	buildCharacters(toGroup);
}

function browseCategories( cats ) {
	var selItem = document.getElementById(cats).selectedIndex;
	var selValue = document.getElementById(cats).options[selItem].value;
	var selText = document.getElementById(cats).options[selItem].text;
	var element = document.getElementById(cats);
	var charlist1 = document.getElementById('charlist1');
	var charlist2 = document.getElementById('charlist2');
	var copyOption1 = new Array( );
	var copyOption2 = new Array( );
	element.options.length = 0;
	if(selValue != -1) element.options[element.options.length] = new Option(lang['Back2Cats'], -1);
	else element.options[element.options.length] = new Option(lang['Categories'], -1);
	for(z = 0; z < categories.length; z++) {
		if(categories[z].pid == selValue || categories[z].catid == selValue) 
			element.options[element.options.length] = new Option(categories[z].name, categories[z].catid);
	}
	if(selValue != -1) element.options.selectedIndex = 1;
	var copyOption = new Array( );
	
	var a = 0;
	for(x = 0; x < characters.length; x++) {
		if(characters[x].catid == -1 || characters[x].catid == selValue) {

			copyOption1[a] = new Array(characters[x].charname, characters[x].charid, false, false);
			copyOption2[a] = new Array(characters[x].charname, characters[x].charid, false, false);
			a++;
			continue;
		}
	}
	for(x = 0; x < copyOption.length; x++) {
		for(y = 0; y < charlist1.length; y++) {
			if(charlist1.options[y].selected == true && charlist1.options[y].value == copyOption1[x][1]) {
				copyOption1[x][2] = false;
				copyOption1[x][3] = true;
			}
			if(charlist2.options[y].selected == true && charlist1.options[y].value == copyOption2[x][1]) {
				copyOption2[x][2] = false;
				copyOption2[x][3] = true;
			}

		}
	}
	copyOption1.sort( );
	copyOption2.sort( );
	charlist1.length = 0;
	charlist2.length = 0;
		charlist1.options[charlist1.options.length] = new Option(lang['Characters'], '', false, false);
		charlist2.options[charlist2.options.length] = new Option(lang['Characters'], '', false, false);

	for(i = 0; i < copyOption1.length; i++) {
		charlist1.options[charlist1.options.length] = new Option(copyOption1[i][0], copyOption1[i][1], copyOption1[i][2], copyOption1[i][3]);
		charlist2.options[charlist2.options.length] = new Option(copyOption2[i][0], copyOption2[i][1], copyOption2[i][2], copyOption2[i][3]);
	}
	
}	

function buildCharacters( cats ) {
	if(document.form.formname.value == "admins") return;
	var charid = document.form.charid;
	var catslist = document.getElementById(cats);
	var copyOption = new Array( );
	
	var a = 0;
	for(x = 0; x < characters.length; x++) {
		if(characters[x].catid == -1) {
			copyOption[a] = new Array(characters[x].charname, characters[x].charid, false, false);
			a++;
			continue;
		}
		for(z = 0; z < catslist.length; z++) {
			if(catslist.options[z].value == characters[x].catid) {
				copyOption[a] = new Array(characters[x].charname, characters[x].charid, false, false);
				a++;
			}
		}
	}
	for(x = 0; x < copyOption.length; x++) {
		for(y = 0; y < charid.length; y++) {
			if(charid.options[y].selected == true && charid.options[y].value == copyOption[x][1]) {
				copyOption[x][2] = false;
				copyOption[x][3] = true;
			}
		}
	}
	copyOption.sort( );
	document.form.charid.length = 0;
	for(i = 0; i < copyOption.length; i++) {
		charid.options[charid.options.length] = new Option(copyOption[i][0], copyOption[i][1], copyOption[i][2], copyOption[i][3]);
	}
}

function removeCat( catid ) {
	var selitem = document.getElementById(catid).selectedIndex;
	var strValues = "";
	document.getElementById(catid).options[selitem] = null;
	for(i = 0; i < document.getElementById(catid).length; i++) {
		strValues = strValues + "," + document.getElementById(catid).options[i].value;
	}
	document.getElementById("catid").value = strValues;
	buildCharacters(catid);
}

function displayCatRows(parent) {
	if(navigator.appName.indexOf('Microsoft') > -1) var canSee = 'block';
	else var canSee = 'table-row';
	var start = document.images['c_' + parent].src.lastIndexOf(".") - 3;
	if(document.images['c_' + parent].src.indexOf('on', start) == -1) {
		var state = 'off';
		tmp = document.images['c_' + parent].src;
		document.images['c_' + parent].src = document.images['c_' + parent].src.replace('_off', '_on');
	}
	else {
		var state = 'on';
		tmp = document.images['c_' + parent].src;
		document.images['c_' + parent].src = document.images['c_' + parent].src.replace('_on', '_off');
	}
	for(x = 0; x < categories.length; x++) {
		if(categories[x].pid == parent) {
			if(state == 'off')
				document.getElementById('catid_' + categories[x].catid).style.display = 'none';
			else
				document.getElementById('catid_' + categories[x].catid).style.display = canSee;
		}
	}			
}

function setCategoryForm( chosen ) {
	var category = chosen.options[chosen.selectedIndex].value; 
	var count = 0;
		chosen.options.length = 0;
		document.form.orderafter.options.length = 0;
		for(x = 0; x < categories.length; x++) {
			if(categories[x].pid == category || categories[x].catid == category) { 
				chosen.options[chosen.options.length] = new Option(categories[x].name, categories[x].catid); 
			}
			if(categories[x].pid == category) {
				document.form.orderafter.options[document.form.orderafter.options.length] = new Option(categories[x].name, categories[x].order); 
				count++;
			}
		}
		if(category != -1) { 
			chosen.options[chosen.options.length] = new Option(lang['Back2Cats'], '-1');
			chosen.options.selectedIndex = 0;
		}
		else { 
			chosen.options[chosen.options.length] = new Option(lang['TopLevel'], '-1');
			chosen.options.selectedIndex = chosen.options.length - 1;
			
		}
		document.form.orderafter.options[document.form.orderafter.options.length] = new Option(lang['MoveTop'], '0');
		document.form.orderafter.options.selectedIndex= document.form.orderafter.options.length - 1;
}

function displayTypeOpts() {
	var choice = document.getElementById('field_type').selectedIndex;
	var type = document.getElementById('field_type').options[choice].value;
	if(window.tinyMCE) tinyMCE.execCommand('mceResetDesignMode'); 

	for(i = 1; i < 6; i++) {
		if(document.getElementById('opt_' + i)) {
			document.getElementById('opt_' + i).style.display = 'none';
		}
	}
	document.getElementById('opt_' + type).style.display = 'block';
	if(window.tinyMCE) tinyMCE.execCommand('mceResetDesignMode'); 
}


