//****************************************************
//
//****************************************************
function checkField(fieldId, isFunc, errorStr) 
	{
	var field = gebid(fieldId);
	
	if (field == null)
		{
		alert('Unkown field ' + fieldId);
		return '';
		}
		
	var value = '';
	if (field.type == 'select-one')
		value = field.options[field.selectedIndex].value;
	else
		value = field.value;

	var error = '';
	
	if (isFunc(value))
		{
		setErrorClass(fieldId);
		error =  errorStr + '\n';
		}
	else
		clearErrorClass(fieldId);

	return error;
	}
	
//****************************************************
//
//****************************************************
function findLabelForItem(itemId)
	{
	var labels = document.getElementsByTagName('label');
	for (var i=0; i < labels.length; i++)
		{
		if (labels[i].htmlFor == itemId)
			return labels[i]; 
		}

	return null;
	}

//****************************************************
//
//****************************************************
function clearErrorClass(itemId)
	{
	var reg = /inputError/;
	if (reg.test(gebid(itemId).className))
		{
		var cname = gebid(itemId).className;
		gebid(itemId).className = cname.replace(' inputError', '');
		
		var label = findLabelForItem(itemId);
		if (label != null)
			label.className = label.className.replace(' labelHighlight', '');
		}
	return false;
	}

//****************************************************
//
//****************************************************
function setErrorClass(itemId)
	{
	var reg = /inputError/;
	if (!reg.test(gebid(itemId).className))
		{
		gebid(itemId).className +=' inputError';

		var label = findLabelForItem(itemId);
		if (label != null)
			label.className += ' labelHighlight';
		}
	return false;
	}
	
//****************************************************
//
//****************************************************
function gebid(name)
	{
	return document.getElementById(name);
	}


//****************************************************
//
//****************************************************
function removeListEntry(listId, nameId, labelValue) 
	{
	var objList = gebid(listId);
	if (objList == null)
		{
		alert("removeListEntry: unable to find list  " + listId);
		return false;
		}

	var item = gebid(nameId);
	if (item == null)
		{
		alert("removeListEntry: unable to find list item   " + nameId);
		return false;
		}

	objList.removeChild(item);
	
	if (labelValue != '')
		{
		for (var i=0; i < itemHolder[listId].length; i++)
			{
			if (itemHolder[listId][i] == labelValue)
				{
				delete itemHolder[listId][i];
				break;
				}
			}
		}

	return false;
	}

//****************************************************
//
//****************************************************
function addCatListEntry(cat_id)
	{
	return addListEntry('catItem' + cat_id, 'catList', 'cat_list[' + cat_id + ']');
	}

//****************************************************
//
//****************************************************
function addLangListEntry()
	{
	return addListEntry('allLanguages', 'langList', 'lang_list');
	}

//****************************************************
//
//****************************************************
var itemSeq = 0;
var itemHolder = new Object();

function addListEntry(addId, listId, prefix, label, value) 
	{
	var labelName = "";
	var labelValue = "";
	var type = "";
	
	if (addId == "addOpt")
		{
		type = addId;
		labelName = label;
		labelValue = value;
		}
	else if (addId == "addTextarea")
		{
		type = "TEXTAREA";
		labelName = label;
		labelValue = value;
		}
	else
		{
		//	Get selection
		var addObj = gebid(addId);
		if (addObj == null)
			{
			alert("addListEntry: unable to find add item  " + addId);
			return false;
			}
	
		if (addObj.nodeName == "SELECT")
			{
			type = addObj.nodeName;
			
			//	Ignore the first one
			if (addObj.selectedIndex == 0)
				return false;
		
			//	Get selected option
			var langOpt = addObj.options[addObj.selectedIndex];
			
			labelName = langOpt.text;
			labelValue = langOpt.value;
			
			addObj.selectedIndex = 0;
			}
		else if (addObj.nodeName == "INPUT")
			{
			type = addObj.nodeName;
			labelName = addObj.value;
			labelValue = addObj.value;
			addObj.value = '';
			}
		else if (addObj.nodeName == "TEXTAREA")
			{
			type = addObj.nodeName;
			labelName = addObj.value;
			labelValue = addObj.value;
			addObj.value = '';
			}
		else
			{
			alert("addListEntry: not implemented for " + addObj.nodeName);
			return false;
			}
		}

	if (labelValue == '')
		return false;
		
	//
	//	If this item already exists in the list, then ignore the 
	//	request to add it again (does not work)
	//
	if (typeof(itemHolder[listId]) == "undefined")
		itemHolder[listId] = new Array();
		
	var i;
	for (i=0; i < itemHolder[listId].length; i++)
		{
		if (itemHolder[listId][i] == labelValue)
			return false;
		}
	itemHolder[listId].push(labelValue);
	
	
	//
	//	Update the item counter
	//
	var liItemId = 'listItem' + (itemSeq++);

	//
	//	Create the delete image
	//
	var imgElement = document.createElement('img');
	imgElement.setAttribute("src","images/icons/btn_delete.png");
	imgElement.setAttribute("alt","Delete this");

	//
	//	Create the A HREF item to delete the item
	//
	var aElement = document.createElement('a');
	aElement.setAttribute("href","#");
	aElement.setAttribute("class","image");
	
	var rmLabel = '';
	if (listId=='langList')
		rmLabel = labelValue;

	aElement.setAttribute("onClick", "return removeListEntry('" + listId + "','" + liItemId + "','" + rmLabel + "');");

	aElement.appendChild(imgElement);

	//
	//	create LI item and assemble all the parts
	//
	var objListElement = document.createElement('li');
	objListElement.setAttribute("id", liItemId);

	if (type == "TEXTAREA")
		{
		//
		//	Create the form item to store the value
		//
		var inputElement = document.createElement('textarea');
		inputElement.setAttribute("name", prefix + "[]");
		inputElement.setAttribute("rows", "4");


		var objTextNode = document.createTextNode(labelValue);
		inputElement.appendChild(objTextNode);

		//
		//	create LI item and assemble all the parts
		//
		objListElement.appendChild(aElement);
		objListElement.appendChild(inputElement);
		}
	else
		{
		//
		//	Create the label
		//
		var objTextNode = document.createTextNode(labelName);
	
		//
		//	Create the form item to store the value
		//
		var inputElement = document.createElement('input');
		inputElement.setAttribute("type","hidden");
		inputElement.setAttribute("name", prefix + "[]");
		inputElement.setAttribute("value", labelValue);

		//
		//	create LI item and assemble all the parts
		//
		objListElement.appendChild(aElement);
		objListElement.appendChild(objTextNode);
		objListElement.appendChild(inputElement);
		}
	


	//
	//	Add it to the list
	//	
	var objList = gebid(listId);
	if (objList == null)
		{
		alert("addListEntry: unable to find list " + listId);
		return false;
		}
	objList.appendChild(objListElement);

	return false;
	}

//****************************************************
//
//****************************************************
function trim(stringToTrim) 
	{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
	}
	
//****************************************************
//
//****************************************************
function isEmail(email) 
	{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return reg.test(email);
	}

//****************************************************
//
//****************************************************
function isNotEmail(email) 
	{
	return !isEmail(email);
	}

//****************************************************
//
//****************************************************
function isNotBlankAndEmail(email) 
	{
	return !isBlank(email) && !isEmail(email);
	}

	
//****************************************************
//
//****************************************************
function isBlank(str) 
	{
	var reg = /^[ \t\n\r]*$/;
	return reg.test(str);
	}
	
//****************************************************
//
//****************************************************
var keywordIdx = 0;
function addKeyword(str, checkit)
	{
	var keywords = str.split(',');
	
	for (var k =0; k < keywords.length; k++)
		{
		//
		//	Get a keyword
		//
		var key = trim(keywords[k]);
		if (key == '')
			continue;

		if (key == 'Add new keywords here ...')
			continue;
			
		//
		//	Check if the keyword is already in the list
		//
		var found = false;
		var table = gebid('keywordTable');

		var input_list = table.getElementsByTagName('input');

		for (var i=0; i < input_list.length; i++)
			{
			if (input_list[i].getAttribute('type') == 'hidden'  && 
				input_list[i].getAttribute('value') == key)
				{
				input_list[i].checked = true;
				found = true;
				break;
				}
			}

		if (found)
			continue;
			
		//
		//	Add the keyword to the list
		//
		var txt = document.createTextNode(key);
		
		var pag = document.createElement('p');
		pag.appendChild(txt);
		
		keywordIdx++;
		
		var inp = document.createElement('input');
		inp.setAttribute('type','checkbox');
		inp.setAttribute('class','option');
		inp.setAttribute('name', 'keywords_selected[]');
		inp.setAttribute('value', keywordIdx);

		var hinp = document.createElement('input');
		hinp.setAttribute('type','hidden');
		hinp.setAttribute('name','keywords[' + keywordIdx + ']');
		hinp.setAttribute('value', key);
	
		var td = document.createElement('td');
		
		td.appendChild(inp);
		td.appendChild(hinp);
		td.appendChild(pag);
	
		var tr_list = table.getElementsByTagName('tr');
		var last_tr = tr_list[tr_list.length-1];
	
		if (last_tr.getElementsByTagName('td').length >= 5)
			{
			last_tr = document.createElement('tr');
			table.getElementsByTagName('tbody')[0].appendChild(last_tr);
			}
	
		last_tr.appendChild(td);
		
		if (checkit)
			inp.setAttribute('checked', true);
		}
	
	return false;
	}

//****************************************************
//
//****************************************************
function showFormError(error)
	{
	error = trim(error);
	var error_list = error.split("\n");
	error  = error_list.join("<br>"); 

	gebid('adminMessage').innerHTML = "<p>" +  error + "</p>";
	gebid('adminFeedback').className = 'adminFeedback inputError'
	}

//****************************************************
//
//****************************************************
function fixSubNavi()
	{
	var href_parts =  document.location.href.split("?");
	var parts =  href_parts[0].split("/");
	
	var offset=3;
	if (parts.length > 6)
		offset=7;
	
	var topTopic = parts[offset];
	var subTopic = '';
	if (typeof(parts[offset+1]) != 'undefined')
		{
		subTopic = "_" + parts[offset+1];
		}
	
	var id;
	if (topTopic == '') {
		topTopic = base_url;
		id = 'home';
	} else {
		id = topTopic;
	}

	
	topNav = document.getElementById("nav_" + id);
	if (topNav != undefined)
		topNav.className += ' current'

	sideNav = document.getElementById("sidenav_" + topTopic + subTopic);
	if (sideNav != undefined)
		sideNav.className = 'current'
	}
