function ShowSubNav(obj){
	var subnav = obj.getElementsByTagName("ul");
	if (subnav){
		if (subnav[0]){
			subnav[0].style.display = "block";
		}
	}
}

function HideSubNav(obj){
	var subnav = obj.getElementsByTagName("ul");
	if (subnav){
		if (subnav[0]){
			subnav[0].style.display = "none";
		}
	}
}

function fsub(item, location, conf){
	if (item.id.substr(item.id.length-3, 3) != "Off" && item.id.substr(item.id.length-8, 8) != "Disabled"){
		document.tbform.action=location;
		if (conf && confirm(conf) == false)	return false;
		document.tbform.submit();
		return true;
	}
	return false;
}

function SetRowColor(item, on){
	if (on){
		var key = document.getElementById("selectedKey");
		if (!(item.oldClass)) item.oldClass = item.className;
		item.className = key.className;
	}
	else {
		item.className=item.oldClass;
		item.oldClass = "";
	}
}

function SelectRow(row){
	var cbox = row.getElementsByTagName("input");
	if (cbox[0].checked) { cbox[0].checked = false; }
	else { cbox[0].checked = true; }
	SetRowColor(row, cbox[0].checked);
	UpdateToolbar();
}

function SelectAllRow(){
	var cbox = document.tbform.getElementsByTagName("input");
	var rows = document.tbform.getElementsByTagName("tr");

	var isChecked = cbox[0].checked;
	for(var i = 1; i < cbox.length; i++){
		cbox[i].checked = isChecked;
		SetRowColor(rows[i], cbox[i].checked);
	}
	UpdateToolbar();
}

function UpdateSelectedRows(){
	var cbox = document.tbform.getElementsByTagName("input");
	var rows = document.tbform.getElementsByTagName("tr");
	for(var i = 1; i < cbox.length; i++){
		if (cbox[i].checked)
			SetRowColor(rows[i], cbox[i].checked);
	}
}

function UpdateToolbar(){
	var cbox = document.tbform.getElementsByTagName("input");
	var total = 0;
	for(var i = 1; i < cbox.length; i++) {
		if (cbox[i].checked) total++;
	}
	if (total == 0){
		TurnOffID(document.getElementById("edit"));
		TurnOffID(document.getElementById("publish"));
		TurnOffID(document.getElementById("unpublish"));
		TurnOffID(document.getElementById("priorityup"));
		TurnOffID(document.getElementById("prioritydown"));
		TurnOffID(document.getElementById("archive"));
		TurnOffID(document.getElementById("unarchive"));
		TurnOffID(document.getElementById("delete"));
	}
	else if (total == 1){
		TurnOnID(document.getElementById("editOff"));
	}
	if (total > 0){
		TurnOnID(document.getElementById("publishOff"));
		TurnOnID(document.getElementById("unpublishOff"));
		TurnOnID(document.getElementById("priorityupOff"));
		TurnOnID(document.getElementById("prioritydownOff"));
		TurnOnID(document.getElementById("archiveOff"));
		TurnOnID(document.getElementById("unarchiveOff"));
		TurnOnID(document.getElementById("deleteOff"));
	}
	if (total > 1){
		TurnOffID(document.getElementById("edit"));
	}
}

function TurnOffID(item){
	if (item && item.id.substr(item.id.length-3, 3) != "Off"){
		item.id = item.id+"Off";
		item.cursor = "default";
	}
}

function TurnOnID(item){
	if (item && item.id.substr(item.id.length-3, 3) == "Off"){
		item.id = item.id.substr(0, item.id.length-3);
		item.cursor = "pointer";
	}
}

function SetCorrField(select){
	if (select && select.name.substr(select.id.length-6, 6) == "Select"){
		var fieldName = select.name.substr(0, select.name.length-6);
		if (document.editForm){
			var field = getFormInputByName(document.editForm, fieldName);
			if (field){
				field.value = select.value;
			}
			else {
				alert("pbth");
			}
		}
	}
}

function getFormInputByName(form, inputname){
	if (form){
		var fields = form.getElementsByTagName("input");
		for (var i = 0; i < fields.length; i++){
			if (fields[i].name == inputname){
				return fields[i];
				alert(fields[i].name);
			}
		}
	}
	return '';
}

function ShowTab(item, name, alltabs){
	if (item){
		//Set tab class
		var tabCont = document.getElementById("tabContainer");
		var tabs  = tabCont.getElementsByTagName("div");
		for (var i = 0; i < tabs.length; i++){
			if (tabs[i].className == "tabActive"){
				tabs[i].className = "tabNormal";
			}
		}
		item.className = "tabActive";
		//Display tab data
	}
	if (name){
		var tab;
		for (var i = 0; i < alltabs.length; i++){
			tab = document.getElementById(alltabs[i]);
			if (tab){
				tab.style.height = "0";
				tab.style.visibility = "hidden";
				tab.style.overflow = "hidden";
			}
		}
		tab = document.getElementById(name);
		if (tab){
			tab.style.height = "";
			tab.style.visibility = "visible";
			tab.style.overflow = "";
		}
	}
}

//Gallery
var oldstyle = '';
var activeDiv = '';
function SetActiveDiv(obj){
	oldstyle = obj.style.backgroundColor;
	obj.style.backgroundColor = "#FF9999";
	activeDiv = obj;
}
function isButtonDiv(id){
	var buttons=Array('stored', 'selected', 'trashed');
	for (var i = 0; i < buttons.length; i++){
		if (id.substr(0, buttons[i].length) == buttons[i]){
			return 1;
		}
	}
	return 0;
}
function SwapActiveDivs(obj){
	activeDiv.style.backgroundColor = oldstyle;
	if (activeDiv != obj){
		if (isButtonDiv(obj.id)){
			var cParent = obj.parentNode;
			cParent.appendChild(activeDiv);
		}
		else {
			var newdiv = document.createElement("div");
			activeDiv.parentNode.insertBefore(newdiv, activeDiv);
			obj.parentNode.insertBefore(activeDiv, obj);
			newdiv.parentNode.replaceChild( obj, newdiv);
		}
	}
	activeDiv = '';
	oldstyle = '';
}
function UseActiveDiv(obj, section){
	if (activeDiv == ''){
		if (!(isButtonDiv(obj.id))){
			SetActiveDiv(obj);
		}
	}
	else {
		SwapActiveDivs(obj);
	}
}

function CompileGallery(group){
	//compile selected
	var cont = document.getElementById('selected_'+group).parentNode;
	var items = cont.getElementsByTagName("div");
	for (var i = 1; i < items.length; i++){
		if (items[i].firstChild != ""){
			var newinput = document.createElement("input");
			newinput.type="checkbox";
			newinput.name="selected_"+group+"[]";
			newinput.value=items[i].firstChild.nodeValue;
			items[i].appendChild(newinput);
			newinput.checked=true;
		}
	}

	//compile trash
	cont = document.getElementById('trashed_'+group).parentNode;
	items = cont.getElementsByTagName("div");
	for (var i = 1; i < items.length; i++){
		if (items[i].firstChild != ""){
			var newinput = document.createElement("input");
			newinput.type="checkbox";
			newinput.name="trashed_"+group+"[]";
			newinput.value=items[i].firstChild.nodeValue;
			items[i].appendChild(newinput);
			newinput.checked=true;
		}

	}
}

// Universal transparent-PNG enabler for MSIE/Win 5.5+
// http://dsandler.org
// From original code: http://www.youngpup.net/?request=/snippets/sleight.xml
// and background-image code: http://www.allinthehead.com/retro/69
// also:
//  * use sizingMethod=crop to avoid scaling PNGs (who would do such a thing?)
//  * only do this once, to make it compatible with CSS rollovers

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	window.attachEvent("onload", enableAlphaImages);
}

function enableAlphaImages(){
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
	if (itsAllGood) {
		for (var i=0; i<document.all.length; i++){
			var obj = document.all[i];
			var bg = obj.currentStyle.backgroundImage;
			var img = document.images[i];
			if (bg && bg.match(/\.png/i) != null) {
				var img = bg.substring(5,bg.length-2);
				var offset = obj.style["background-position"];
				obj.style.filter =
				"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='crop')";
				obj.style.backgroundImage = "url('images/spacer.png')";
				obj.style["background-position"] = offset; // reapply
			} else if (img && img.src.match(/\.png$/i) != null) {
				var src = img.src;
				img.style.width = img.width + "px";
				img.style.height = img.height + "px";
				img.style.filter =
				"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='crop')"
				img.src = "images/spacer.png";
			}

		}
	}
}
