//originally extended the array functionality, but this conflicted with other scripts
//checks to see if a given value is in an array
function inArray(arr, val) {
	var i;
	for (i=0; i < arr.length; i++) {
		if (arr[i] === val) {
			return true;
		}
	}
	return false;
}

//originally extended the array functionality, but this conflicted with other scripts
//removes a given element from a given array
function removeFromArray(arr, val) {
  for(i=0;i<arr.length;i++){
    if(val==arr[i]) arr.splice(i, 1);
  }
}

//returns the position of any element on the screen
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	
	return [curleft,curtop];
}

//standard function utilised in the AJax calls above.
function writeToElement(element, textToWrite) {
	document.getElementById(element).innerHTML = textToWrite;
}

/*******************
 * Searches
 *******************/

var search_originBox;
var search_populateId;
var search_func;
var search_suppress;
var search_mouseIn;

var search_counter = 1;

function hideSearch(func, userText, userId) {
	if(document.getElementById('searchResults')) {
		document.getElementById('searchResults').style.display = 'none';

		if(func==1 && search_func !=2) {
			document.getElementById(search_originBox.id).value = userText;
			document.getElementById(search_populateId).value = userId;
			suppress_search = 1;
			search_mouseIn = 0;
		} else if(func==1 && search_func==2) {
			document.getElementById(search_originBox.id).value = '';
			document.getElementById(search_originBox.id + 'websiteDiv').innerHTML += '<p>'+userText+'</p>';
			document.getElementById(search_populateId).value += ',' + userId;
		}
	}
}

function initSearch(ob, pId, f) {
	if(!document.getElementById('searchResults')) {
		var newDiv = document.createElement("div");
		newDiv.id='searchResults';
		newDiv.onmouseover = function() {search_mouseIn=1;}
		newDiv.onmouseout = function() {search_mouseIn=0;}
		
		document.body.appendChild(newDiv);
	}

	if(!ob.onblur) {
		ob.onkeyup = ajaxSearch;
		ob.onfocus = function() { search_originBox = ob; search_populateId = pId; search_func = f; search_suppress = 0; }
		ob.onblur = function() { if(search_mouseIn==0) {hideSearch(0, '', ''); } }
	}

	search_originBox = ob; 
	search_populateId = pId; 
	search_func = f;
	search_suppress = 0;
	search_mouseIn = 0;
}

/* Search Staff Directory folders */
//function ajaxSearch(originBox, populateId, func) {
function ajaxSearch(event) {
	
	if(search_suppress == 0) {
	
		var searchPhrase = search_originBox.value;

		if (!event && window.event) event = window.event;
		if (event) key = event.keyCode;
		else key = event.which;

		//down
		if(key==38) {
			ajaxSearch_SelectResult(-1);
		
		//up
		} else if(key==40) {
			ajaxSearch_SelectResult(1);

		//enter
		} else if(key==13) {
			if(document.getElementById('searchResultLink' + search_counter)) {
				var popResult = document.getElementById('searchResultLink' + search_counter).name.split('||');
				hideSearch(1, popResult[0], popResult[1]);
			} else {
				hideSearch(0, '', '');
			}
		
		//esc
		} else if(key==27) {
			search_suppress = 1;
			hideSearch(0, '', '');
		
		//check the key is alphanumeric (+ some dash, minus, single quote, backspace and delete
		} else if((key >=48 && key <= 90) || (key>=96 && key<=105) || (key==8 || key==46 || key==109 || key==188 || key==189 || key==222)) { 
			if(searchPhrase.length > 0) {
				var poststr = 'function=search_' + search_func + '&search=' + searchPhrase + '&populateId=' + search_populateId + '&originId=' + search_originBox.id;
				ajaxpack.postAjaxRequest('/curo/ajax.asp', poststr, function t() { if(ajaxIsReady()) { 
					var pos = findPos(document.getElementById(search_originBox.id));
					
					document.getElementById('searchResults').style.top = (pos[1]+22) + 'px';
					document.getElementById('searchResults').style.left = pos[0] + 'px';
					
					writeToElement('searchResults', ajaxpack.ajaxobj.responseText);
					document.getElementById('searchResults').style.display = 'block';

					search_counter = 0;
					ajaxSearch_SelectResult(1);
					search_mouseIn = 0;

				} }, 'html');
			}
		}
	}
}

function ajaxSearch_SelectResult(addSubtract) {
	if( document.getElementById('searchResult' + (search_counter+addSubtract)) ) {
		if(document.getElementById('searchResult' + search_counter)) {
			document.getElementById('searchResult' + search_counter).style.backgroundColor = 'transparent';		
		}
		search_counter += addSubtract;
		document.getElementById('searchResult' + search_counter).style.backgroundColor = '#DDDEFF';
		
		var curtop = document.getElementById('searchResult' + search_counter).offsetTop;

		if(addSubtract > 0 && curtop >= (document.getElementById('searchResults').offsetHeight - 10)) {
			document.getElementById('searchResults').scrollTop = document.getElementById('searchResults').scrollTop + document.getElementById('searchResult' + search_counter).offsetHeight;
		} else if(curtop < document.getElementById('searchResults').scrollTop) {
			document.getElementById('searchResults').scrollTop = document.getElementById('searchResults').scrollTop - document.getElementById('searchResult' + search_counter).offsetHeight;
		}
	}
}
