
function getDomElement(id) {
	return document.getElementById(id);
}

function setParentCategory(parentId, title) {
	getDomElement("categoryForm").parentId.value = parentId;
	getDomElement("categoryForm").parentTitle.value = title;
	categoryChooser.CloseWindow();
}

function addCategoryOption(category, title) {
	var select = getDomElement("featuredCategoryId");
	var option = document.createElement("option");
	option.id = "featureCategory" + category.value;
	option.value = category.value;
	option.innerHTML = title;
	select.appendChild(option);
}

function removeCategoryOption(category) {
	var select = getDomElement("featuredCategoryId");
	var option = getDomElement("featureCategory" + category.value);
	if (null != option)
		select.removeChild(option);
}

function addRemoveToSelect(category, title) {
 	if (category.checked) {
 		addCategoryOption(category, title);
 	} else {
 		removeCategoryOption(category);
 	}
}

function showFeatureEditor() {
	var featureEditor = new FCKeditor("feature");
	featureEditor.ToolbarSet = "Sirius";
	featureEditor.BasePath = "../fckeditor/";
	featureEditor.Height = "300";
	featureEditor.Width = "100%";		
	featureEditor.ReplaceTextarea();
}

function hideShowFeature(show) {
	if (show) {
		getDomElement("featuredCategoryId").style.display = "table-cell";
		getDomElement("featureLocation").style.display = "table-cell";
		getDomElement("featureEditor").style.display = "table-row";
		showFeatureEditor();
	} else {
		getDomElement("featuredCategoryId").value = -1;
		getDomElement("featuredCategoryId").style.display = "none";
		getDomElement("featureLocation").style.display = "none";
		getDomElement("featureEditor").style.display = "none";
	}
}

function updateCategoryMapping(category, title) {
	updateTitles(title, category.checked);
	addRemoveToSelect(category, title);
	getDomElement("category" + category.value).checked = category.checked;
	getDomElement("priority" + category.value).checked = category.checked;
}

function updateTitles(title, checked) {
	if (checked) {		
		addTitle(title);
	} else {
		removeTitle(title);
	}
}

function addTitle(title) {
	if (getTitlesValue().length > 0) {
		getTitlesInput().value = getTitlesValue() + ", " + title;
	} else {
		getTitlesInput().value = title;
	}
}

function getTitlesValue() {
	return getTitlesInput().value;
}

function removeTitle(title) {
	var categoryTitles = getTitlesValue();
	if (categoryTitles.length == 0 || categoryTitles.indexOf(title) == -1)
		return;
	var ex = new RegExp(", " + title, "g");
	categoryTitles = categoryTitles.replace(ex, "");
	if (categoryTitles.length == title.length)
		categoryTitles = "";
	getTitlesInput().value = categoryTitles;
}

function getTitlesInput() {
	return getDomElement("itemForm").categoryTitles;
}

function moveUp(oldPriority, newPriority) {
	var index = getDomElement("childIndex" + oldPriority).innerHTML;

	var title = getDomElement("priority" + oldPriority).innerHTML;
	var oldTitle = getDomElement("priority" + newPriority).innerHTML;

	var oldIndex = getDomElement("childIndex" + newPriority).innerHTML;

	for (var i = newPriority; i < oldPriority; i++) {
		var newTitle = oldTitle;
		var newIndex = oldIndex;

		var oldTd = getDomElement("priority" + i);
		var newTd = getDomElement("priority" + ((1 * i) + 1));

		var oldIndexTd = getDomElement("childIndex" + i);
		var newIndexTd = getDomElement("childIndex" + ((1 * i) + 1));

		oldTitle = newTd.innerHTML;
		newTd.innerHTML = newTitle;

		oldIndex = newIndexTd.innerHTML;
		newIndexTd.innerHTML = newIndex;
		
		getDomElement("_priority" + newIndex).value = ((1 * i) + 1);
	}
	getDomElement("priority" + newPriority).innerHTML = title;
	getDomElement("_priority" + index).value = newPriority;
}

function moveDown(oldPriority, newPriority) {
	var index = getDomElement("childIndex" + oldPriority).innerHTML;
	
	var title = getDomElement("priority" + oldPriority).innerHTML;
	var oldTitle = getDomElement("priority" + newPriority).innerHTML;

	var oldIndex = getDomElement("childIndex" + newPriority).innerHTML;
	
	for (var i = newPriority; i > oldPriority; i--) {
		var newTitle = oldTitle;
		var newIndex = oldIndex;
		
		var oldTd = getDomElement("priority" + i);
		var newTd = getDomElement("priority" + (i - 1));
		
		var oldIndexTd = getDomElement("childIndex" + i);
		var newIndexTd = getDomElement("childIndex" + (i - 1));
				
		oldTitle = newTd.innerHTML;
		newTd.innerHTML = newTitle;
		
		oldIndex = newIndexTd.innerHTML;
		newIndexTd.innerHTML = newIndex;
		
		getDomElement("_priority" + newIndex).value = (i - 1);
	}
	getDomElement("priority" + newPriority).innerHTML = title;
	getDomElement("_priority" + index).value = newPriority;
}

function changePriority(select) {
	var priorities = getDomElement("priorityCount").value;
	var newPriority = select.value;
	var ex = new RegExp("prioritySelect" , "g");
	oldPriority = select.id.replace(ex, "");
	if (newPriority < oldPriority) {
		moveUp(oldPriority, newPriority);
	} else {
		moveDown(oldPriority, newPriority);
	}
	select.value = oldPriority;
}

function showPassword(change) {
	if (change) {
		if (navigator.appName == "Microsoft Internet Explorer") {
			if (null != getDomElement("passwordRow"))
				getDomElement("passwordRow").style.display = "inline";
			getDomElement("newPasswordRow").style.display = "inline";
			getDomElement("confirmRow").style.display = "inline";
		} else {
			if (null != getDomElement("passwordRow"))
				getDomElement("passwordRow").style.display = "table-row";
			getDomElement("newPasswordRow").style.display = "table-row";
			getDomElement("confirmRow").style.display = "table-row";
		}
	} else {
		if (null != getDomElement("passwordRow"))
			getDomElement("passwordRow").style.display = "none";
		getDomElement("newPasswordRow").style.display = "none";
		getDomElement("confirmRow").style.display = "none";
	}
}

function featureOver(feature) {
	feature.className = "featureHover";	
	for (var i = 0; i < feature.childNodes.length; i++) {
		feature.childNodes[i].className = "featureTitleHover";
	}
}

function featureOut(feature) {
	feature.className = "feature";
	for (var i = 0; i < feature.childNodes.length; i++) {
		feature.childNodes[i].className = "featureTitle";
	}
}
