//alert('svc.js loaded');

var xmlHttp; // creates xmlHttp variables that gets used whenever we make an "ajax" call

/* app variables */
var svc_container;
var svc_trigger;
var svc_select;
var svc_options;
var triggernum = 100;

/* function creates new "ajax object" */
function createXMLHTTPRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}		
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}
/* ajax failure response */
function ajaxFailureResponse() {
	alert('ajax not working!');
}


function initSVCVars() {
	svc_container = document.getElementById("service_container");
	svc_trigger = document.getElementById("service_trigger");
	svc_select = document.getElementById("service_select");
		svc_select.onfocus = function() {this.select();};
	svc_options = document.getElementById("service_options");
}

function getQueryString() { // builds querystring from form fields
	var qs = "?";
	qs += "kwd=" + escape(svc_select.value);
	qs += "&timestamp=" + new Date().getTime();
	return qs;
}

function getServices() {
	if (svc_select.value == "Service List") {
		svc_select.value = "";
	}
	var querystring = getQueryString();
	var	url = "/ClassLibrary/com/Services/ajax_getServices.cfml" + querystring;
	createXMLHTTPRequest();
	xmlHttp.open("GET",url,true);
	xmlHttp.onreadystatechange = displayServices;
	xmlHttp.send(null);
}

function getServices_triggered() {
	if ((svc_select.value.length < triggernum) ||
		(svc_select.value.length === 0) || 
		(svc_select.value.length == 1) || 
		(svc_select.value.length == 2)) {
		triggernum = svc_select.value.length;
		getServices();
	} else if ((svc_select.value.length - triggernum) > 1) {
		triggernum = svc_select.value.length;
		getServices();
	}
}

function displayServices() {
	//alert(xmlHttp.readyState + ' ' + xmlHttp.status);
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			//alert(xmlHttp.responseXML);
			var results = xmlHttp.responseXML.getElementsByTagName("service");
			//alert(results.length);
			clearData(svc_options);
			toggleDisplay(svc_options,true);
			var listitem;
			var itemlink;
			if (results.length > 0) {
				for(var i=0; i<results.length; i++) {
					listitem = document.createElement("li");
					itemlink = document.createElement("a");
					//create href
						itemlink.setAttribute("href",results[i].getElementsByTagName("url")[0].firstChild.nodeValue);
					//create name
						var this_name = results[i].getElementsByTagName("name")[0].firstChild.nodeValue;
						//itemlink.appendChild(document.createTextNode(this_name));
						formatServiceName(itemlink,this_name);
					//create DOM structure and events
						listitem.appendChild(itemlink);
						listitem.onmouseover = function() {updateClassName(this,"on","");};
						listitem.onmouseout = function() {updateClassName(this,"","on");};
						svc_options.appendChild(listitem);
				}
			} else {
				listitem = document.createElement("li");
				listitem.appendChild(document.createTextNode("No Services matched the keyword you entered."));
				svc_options.appendChild(listitem);
			}
		} else {
			ajaxFailureResponse();
		}
	}
}

function formatServiceName(link_obj,link_text) {
	var highlighted = document.createElement("span");
	var search_text = (svc_select.value);
	var pos_begin = link_text.toLowerCase().indexOf(search_text.toLowerCase());
	//alert(pos_begin);
	if ((pos_begin < 0) || (search_text.length === 0)) {
		link_obj.appendChild(document.createTextNode(link_text));
	} else if (pos_begin === 0) {
		highlighted.appendChild(document.createTextNode(link_text.substr(0,search_text.length)));
		link_obj.appendChild(highlighted);
		link_obj.appendChild(document.createTextNode(link_text.substr(search_text.length,(link_text.length-search_text.length))));
		//link_obj.appendChild(document.createTextNode("pending begin placement"));
	} else {
		link_obj.appendChild(document.createTextNode(link_text.substr(0,pos_begin)));
		highlighted.appendChild(document.createTextNode(link_text.substr(pos_begin,search_text.length)));
		link_obj.appendChild(highlighted);
		link_obj.appendChild(document.createTextNode(link_text.substr((pos_begin+search_text.length),(link_text.length-search_text.length))));
		//link_obj.appendChild(document.createTextNode("pending middle placement"));
	}
}

function activateInitialClick() {
	svc_select.value = "Service List";
	svc_select.onclick = function() {activateSubsequentClick();};
}

function activateSubsequentClick() {
	toggleDisplay(svc_options,true);
	svc_select.value = "";
	svc_select.onclick = void(0);
}