function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

String.prototype.trim = trimString;

function delConfirm(theName,theID,type)
{
	if (type=="delCat")
	{
		var confirmMessage="Are you sure you want to delete the category '"+theName+"' (ID:"+theID+")?  Note: articles in this category WILL NOT be deleted from other categories. Ophaned articles will be accessible in the [no category] list.";
	}

	if (type=="delResource")
	{
		var confirmMessage="Are you sure you want to delete the resource '"+theName+"' (ID:"+theID+")?  Note: this action WILL delete entries of this resource in all categories.";
	}

	if (type=="delFile")
	{
		var confirmMessage="Are you sure you want to delete this file '"+theName+"' (ID:"+theID+")?  Note: this action is permenant.";
	}

	if(type=="remResource")
	{
		var confirmMessage="Are you sure you want to remove the resource '"+theName+"' (ID:"+theID+")?  Note: this action WILL NOT remove entries of this resource from any other categories.";
	}

	if(type=="delUser")
	{
		var confirmMessage="Are you sure you want to delete user '"+theName+"' (ID:"+theID+")?  Note: this action will prevent the user from accessing the administration site.";
	}

	var confirmIt = confirm(confirmMessage);
	if (confirmIt)
		return true;
	else
		return false;
}

function changeCol(rowName)
{
	//rowName.backgroundColor='#D2DFE6'
}




// for list in categories in edit form

function getSelectedString(selectItem) {
	var i;
	var j = 0;
	var outlist = "";

	for (i = 0; i < selectItem.options.length; i++) {
	if (j > 0) {
		outlist = outlist + ", ";
		}
		outlist = outlist + selectItem.options[i].value;
		j++;
	}
	return outlist;
}


function addItems(fromItem, toCtrl) {
	var i;
	var j;
	var itemexists;
	var nextitem;

	// step through all items in fromItem
	for (i = 0; i < fromItem.options.length; i++) {
		if (fromItem.options[i].selected) {
			// search toCtrl to see if duplicate
			j = 0;
			itemexists = false;
			while ((j < toCtrl.options.length) && (!(itemexists))) {
				if (toCtrl.options[j].value == fromItem.options[i].value) {
					itemexists = true;
					//alert("Photo already in this category!");
					}
				j++;
			}
			if (!(itemexists)) {
				// add the item
				nextitem = toCtrl.options.length;
				toCtrl.options[nextitem] = new Option(fromItem.options[i].text.trim());
				toCtrl.options[nextitem].value = fromItem.options[i].value;
			}
		}
	}
}


function addOneItem(fromItem, toCtrl, item) { // my own funtion to take val from hidden field - is this actually being used??

	var i;

	// step through all items in fromItem
	for (i = 0; i < fromItem.options.length; i++) {
		
		if (fromItem.options[i].value == item) {

			// add the item
			toCtrl.options[0] = new Option(fromItem.options[i].text);
			toCtrl.options[0].value = fromItem.options[i].value;
		
		}
	}



}


function removeItems(fromItem) {
	var i = 0;
	var j;
	var k = 0;

	while (i < (fromItem.options.length - k)) {
		if (fromItem.options[i].selected) {
		// remove the item
			for (j = i; j < (fromItem.options.length - 1); j++) {
				fromItem.options[j].text = fromItem.options[j+1].text;
				fromItem.options[j].value = fromItem.options[j+1].value;
				fromItem.options[j].selected = fromItem.options[j+1].selected;
			}
			k++;
		}
		else {
			i++;
		}
	}
	for (i = 0; i < k; i++) {
		fromItem.options[fromItem.options.length - 1] = null;
	}
}



function make_invisible(parent) {
	document.getElementById(parent).style.display = "none";
	//document.getElementById(parent).childNodes.style.display = "none";

	var children = document.getElementById(parent).childNodes;

	for(var i=0; i < children.length; i++) {	
		if (children[i].style)
		{
			children[i].style.display = "none";
		}
	}

}

function make_visible(parent){

	document.getElementById(parent).style.display = "";

	var children = document.getElementById(parent).childNodes;
    
	for(var i=0; i < children.length; i++) {	
		if (children[i].style)
		{
			children[i].style.display = "";
		}
	}

}

function reset_resource_form(){

	make_invisible('row_type');
	make_invisible('row_visibility');
	make_invisible('row_title');
	make_invisible('row_created');
	make_invisible('row_updated');
	make_invisible('row_author');
	make_invisible('row_location');
	make_invisible('row_desc');
	make_invisible('row_summary');
	make_invisible('row_url');
	make_invisible('row_html');
	make_invisible('row_categories');
	make_invisible('row_tags');
	make_invisible('row_uploads');
	make_invisible('row_submit_new');
	make_invisible('row_submit_existing');
}


// Set resource form sections appropriately

function set_resource_form(type){

	reset_resource_form();

	if (type=='int_resource') {
		make_visible("row_type");
		make_visible('row_visibility');
		make_visible('row_title');
		make_visible('row_created');
		make_visible('row_updated');
		make_visible('row_author');
		make_visible('row_location');
		make_visible('row_desc');
		make_visible('row_summary');
		make_visible('row_html');
		make_visible('row_categories');
		make_visible('row_tags');
		make_visible('row_uploads');
		make_visible('row_submit_existing');
	} else if (type == 'ext_resource') {

		make_visible('row_type');
		make_visible('row_visibility');
		make_visible('row_title');
		make_visible('row_created');
		make_visible('row_updated');
		make_visible('row_author');
		make_visible('row_location');
		make_visible('row_desc');
		make_visible('row_summary');
		make_visible('row_url');
		make_visible('row_categories');
		make_visible('row_tags');
		make_visible('row_submit_existing');

	} else if (type=='int_resource_new')	{

		make_visible("row_type");
		make_visible('row_visibility');
		make_visible('row_title');
		make_visible('row_author');
		make_visible('row_location');
		make_visible('row_desc');
		make_visible('row_summary');
		make_visible('row_html');
		make_visible('row_categories');
		make_visible('row_tags');
		make_visible('row_uploads');
		make_visible('row_submit_new');

	} else if (type=='ext_resource_new'){

		make_visible('row_type');
		make_visible('row_visibility');
		make_visible('row_title');
		make_visible('row_author');
		make_visible('row_location');
		make_visible('row_desc');
		make_visible('row_summary');
		make_visible('row_url');
		make_visible('row_categories');
		make_visible('row_tags');
		make_visible('row_submit_new');
	}
}

