// JavaScript Document
function textCounterUpdate(fieldName, maxlimit) {
	var field, txt_field;
	field = getObject( fieldName );
	txt_field = getObject(fieldName+'_char_count');
	txt_field.innerText = maxlimit - field.value.length;
	txt_field.innerHTML = maxlimit - field.value.length;
	if (field.value.length >= maxlimit) { field.value = field.value.substring(0, maxlimit); }
}

function submitFormOnEnter(e) {
	var keyCode;
	if (window.event) {
	  keyCode = window.event.keyCode;
	}
	else if (e) {
	  keyCode = e.which;
	}
	else { return; }
	//if the key was the enter key, submit!
	if(keyCode == 13) { //13 = enter key
	  loadSearchResults(); 
	}
}

function HighLightWords ( objName, objDef, task ) {
	var oSpan = getObject(objName);
	var oDef = getObject(objDef);
	if (oDef == null || oSpan == null) { return; }
	var maxwidth = document.body.clientWidth;
	
	var newLeft, newTop;
	if (task == "over") {
		newLeft = getObjectLeft(oSpan);
		if (maxwidth - newLeft < 200) { newLeft = maxwidth - 210; } //The "definition" div width is 200.
		newTop = getObjectTop(oSpan) + 20;
		oSpan.style.backgroundColor = "#FFFFCC";
		oDef.style.visibility = "visible";
		oDef.style.display = "block";
		oDef.style.left = newLeft + "px";
		oDef.style.top = newTop + "px";
	} else {
		oSpan.style.backgroundColor = "transparent";
		oDef.style.visibility = "hidden";
		oDef.style.display = "none";
		oDef.style.left = "0px";
		oDef.style.top = "0px";
	}
	return;
}

function getObjectLeft ( obj ) {
	var curleft = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
		} while (obj = obj.offsetParent);
	}
	return curleft;
}

function getObjectTop ( obj ) {
	var curtop = 0;
	if (obj.offsetParent) {
		do {
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return curtop;
}

function getObject( sObj ) {
	var oElement;
	if ( document.getElementById ) {
		oElement = document.getElementById( sObj );
	} else if ( document.all ) {
		oElement = document.all.item( sObj );
	} else {
		oElement = null;
	}
	return oElement;
}

//CATALOG:
//Functions for the custom collapse/expand menus:
function flipCategory(numCat) {
	if (numCat == null) { return; }
	var objExpander = getObject("exp_" + numCat);
	if (objExpander.innerHTML == "+") {
		expandCat(numCat);
	} else {
		collapseCat(numCat);
	}
}
function expandCat(numCat) {
	var objCat = getObject("cat_items_box_" + numCat);
	var objExpander = getObject("exp_" + numCat);
	objExpander.innerHTML = "&ndash;";

	if (objCat == null) { return; }		
	objCat.style.visibility = "visible";
	objCat.style.display = "block";		
	return;
}
function collapseCat(numCat) {
	var objCat = getObject("cat_items_box_" + numCat);
	var objExpander = getObject("exp_" + numCat);
	objExpander.innerHTML = "+";
	
	if (objCat == null) { return; }		
	objCat.style.visibility = "hidden";
	objCat.style.display = "none";		
	return;
}
function flipAll() {
	if (aryCats == null) { return; }
	var objFlipper = getObject("expand_collapse");
	
	if (objFlipper.innerHTML == "Expand all") {
		for (var i = 0; i < aryCats.length; i++) {
			expandCat(i + 1);
		}
		objFlipper.innerHTML = "Collapse all";
	} else {
		for (var i = 0; i < aryCats.length; i++) {
			collapseCat(i + 1);
		}
		objFlipper.innerHTML = "Expand all";
	}
}
function loadEntry(data_id) {
	var objDisplayBox = getObject("catalog_entry");
	//if (objDisplayBox == null) {return;}
	var objEntryTitle = getObject("catItemTitle"); 
	var objEntryTab = getObject("mainEntryTab");
	var objPreviewTab = getObject("previewTab");
	var objServicesTab = getObject("servicesTab");
	var objDownloadsTab = getObject("downloadsTab");
	var objMetaDataTab = getObject("metadataTab");
	var oXHR = createXHR();
	if (oXHR == null) {return;}

	// Pre-select the preview container:
	var dijTabContainer = dijit.byId("catItemTabContainer");
	dijTabContainer.selectChild("previewTab");

	oXHR.open("get", "_cat_item.cfm?data_id="+data_id, false); //Synchronous - wait for the results so we can see the preview if there is one.
	oXHR.send(null);
	//oXHR.onreadystatechange = function () { 
	if (oXHR.status == 200 || oXHR.status == 400) {
		aData = oXHR.responseText.split("||");
		objEntryTitle.innerHTML = aData[0];
		objEntryTab.innerHTML = aData[1];
		objPreviewTab.innerHTML = aData[2]
		objServicesTab.innerHTML = aData[3];
		objDownloadsTab.innerHTML = aData[4];
		objMetaDataTab.innerHTML = aData[5];
	} else {
		objEntryTitle.innerHTML = "<p>ERROR!</p>";
		objEntryTab.innerHTML = "<p><b>An error occurred while loading this record.<br /><br /><em>Please contact the site administrator.</em></b></p>";
	}
	//};

	var regexPatrn = /No preview image available/; //we have preview
	if (objPreviewTab.innerHTML.search(regexPatrn) > 0) {
		//If there's no preview, display the main entry instead:
		dijTabContainer.selectChild("mainEntryTab");
	}
}
function loadEntryForEdit(data_id, action) {
	window.location = "add_update.cfm?data_id=" + data_id+ "&new=edit";
}
function loadSearchResults() {
	var objSearchText = getObject("search_string");
	if (objSearchText.value == null || objSearchText.value.length == 0) { return; }
	var objTabContainer = dijit.byId("catNavTabContainer"); //use the Dojo "dijit" in order to be able to focus the search tab.
	var objSearchTab = getObject("searchTab");
	var oXHR = createXHR();
	if (oXHR == null) {return;}
	
	oXHR.open("get", "_get_catalog.cfm?type=search&search_string="+objSearchText.value, true);
	oXHR.onreadystatechange = function () {
		if (oXHR.readyState == 4) {
			if (oXHR.status == 200 || oXHR.status == 400) {
				objSearchTab.innerHTML = oXHR.responseText;
			} else {
				objSearchTab.innerHTML = "<p>ERROR!</p><p><b>An error occurred while loading this record.<br /><br /><em>Please contact the site administrator.</em></b></p>";
			}
		}
	};
	oXHR.send(null);
	objTabContainer.selectChild("searchTab");
}
function recordUserDownload(user_id, file_id) {
	var oXHR = createXHR();
	if (oXHR == null) {return;}
	
	oXHR.open("get", "_report_download.cfm?user_id="+user_id+"&file_id="+file_id, true); //true = asynchronous - if this fails, the user still gets the file.
	//Don't worry about waiting for the response.
	oXHR.send(null);
	return;
}

//Cross-browser-compatible function to create an XMLHttpRequest object
//Copied from "Professional AJAX 2nd Ed" by N. Zakas, et al, Wrox Pub, (c)2007 - page 39
function createXHR() {
	if (typeof XMLHttpRequest != "undefined") {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		var aVersions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0"];
		for (var i=0; i < aVersions.length; i++) {
			try {
				return new ActiveXObject( aVersions[i] );
			} catch(oError) { //do nothing
			}
		}
	}
	throw new Error("XMLHttp object could not be created."); 
}

//MULTIPLE SELECT:
// Move items from one select box to another
		
//Pre-load the multi-select lists (in the header script of the file with the two select boxes):
//dojo.addOnLoad(function() { createSelectLists("all_assns", "user_assns"); });
var allList;
var addList;
var addIndex = -1;
var delIndex = -1;
//var stayHere = true; //trick for making sure we don't submit the form til we're ready.

function createSelectLists(all_name, add_name) {
	allList = getObject(all_name);
	addList = getObject(add_name);
	return; }

function addItem() {
	addIndex = allList.selectedIndex;
	if(addIndex < 0)
		return;
	addList.appendChild( allList.options.item(addIndex) );
	sortList(addList);
	//selectNone(addList,allList);
	addList.selectedIndex = -1;
	allList.focus();
	allList.selectedIndex = addIndex - 1;
	return; }

function delItem() {
	delIndex = addList.selectedIndex;
	if(delIndex < 0)
		return;
	allList.appendChild( addList.options.item(delIndex) );
	sortList(allList);
	selectNone(addList,allList);
	return; }

function selectNone( list1, list2 ) {
	list1.selectedIndex = -1;
	list2.selectedIndex = -1;
	addIndex = -1;
	delIndex = -1;
	return; }

function sortList( list ) {
	arrItems = new Array();
	for(i=0; i<list.length; i++)  {
		arrItems[i] = list.options[i].text;
	}
	arrItems.sort();
	for(i=0; i<list.length; i++)  {
		list.options[i].text = arrItems[i];
	}
	return; }

//This forces the entire select list to be chosen so the form will be populated
// when it is submitted to the server.
function selectAllToAdd() {
	addList.multiple = true;
	var len = addList.childNodes.length;
	for (var i = 0; i < len; i++ ) {
		if(addList.childNodes.item(i).nodeType==1) addList.childNodes.item(i).selected = true;
	}
	//stayHere = false;
	return; }

