// create the 'LQP' namespace
if (typeof(LQP) === "undefined") {
	var LQP = {};
}
// add to the window.onload without disrupting other onload functions.
LQP.oldOnLoad = window.onload;
if (typeof window.onload != 'function') {
	window.onload = function() {
		LQP.index.loadAssocAutoComplete();
	};
}
else {
   	window.onload = function() {
     	LQP.oldOnLoad();
     	LQP.index.loadAssocAutoComplete();
	};
}
LQP.index = {
	qryAssociates: {},
	activeLink: {},
	qryResults: {
		RECORDCOUNT:0.0,
		data: {
			GUIASSOCIATEID: [],
			SZFIRSTNAME: [],
			SZLASTNAME: [],
			SZDIRECTWORKPHONENUMBER: [],
			BYTSPECIALIZATION: []
		}
	},
	currentSearchLink: "",
	currentSearchField: "",
	currentPageNumber: 1.0,
	loadAssocAutoComplete: function() {
		// build the Auto Complete datasource array
		var acArray = [];
		for (var i = 0; i < parseInt(LQP.index.qryAssociates.RECORDCOUNT); i++) {
			acArray[i] = this.qryAssociates.data.SZFIRSTNAME[i] + " " + this.qryAssociates.data.SZLASTNAME[i];
			acArray[i + LQP.index.qryAssociates.RECORDCOUNT] = this.qryAssociates.data.SZLASTNAME[i] + ", " + this.qryAssociates.data.SZFIRSTNAME[i];
		}		
		// data source
		var acDatasource = new YAHOO.widget.DS_JSArray(acArray);		
		oAutoComp = new YAHOO.widget.AutoComplete('search-field','searchcontainer', acDatasource);
		oAutoComp.queryDelay = 0;
		oAutoComp.prehighlightClassName = "yui-ac-prehighlight";
		oAutoComp.typeAhead = false;
		oAutoComp.useShadow = true;
		oAutoComp.itemSelectEvent.subscribe(LQP.index.selectHandler);
		var tabs = YAHOO.util.Dom.getElementsBy(this.isNotLetterLink ,"a", "searchTabber");
		YAHOO.util.Event.addListener(tabs, "click", function() {LQP.index.tabClick(this);});
	},
	selectHandler: function(sType, args) {
		var searchTerm = new String(args[2]);
		var tokens = searchTerm.split(/,?\s/);
		for (var i = 0; i < parseInt(LQP.index.qryAssociates.RECORDCOUNT); i++) {
			var matches = true;
			for (var j = 0; j < tokens.length; j++) {
				if (LQP.index.qryAssociates.data.SZFIRSTNAME[i].toUpperCase() != tokens[j].toUpperCase()
				&& LQP.index.qryAssociates.data.SZLASTNAME[i].toUpperCase() != tokens[j].toUpperCase()) {
					matches = false;
					break;
				}
			}
			if (matches) {
				window.location = "/agents/profile.cfm?guiAssociateID=" + LQP.index.qryAssociates.data.GUIASSOCIATEID[i];
			}
		}
	},
	tabClick: function(el) {
		if (el !== this.activeLink) {
			this.sortByClickedTab(el.getAttribute("id"));
			this.clearSearch();
			this.clearSearchTable();
		}
		this.activeLink = el;
	},
	sortByClickedTab: function(clickedTab) {
		if (clickedTab == "searchTabbernav1") {
			var sortColumn = "SZLASTNAME";
			var secondSortColumn = "SZFIRSTNAME";
		}
		else if (clickedTab == "searchTabbernav2") {
			var sortColumn = "SZFIRSTNAME";
			var secondSortColumn = "SZLASTNAME";
		}
		var tempAssoc = {};
		var assocCount = parseInt(LQP.index.qryAssociates.RECORDCOUNT);
		for (var i = 0; i < assocCount - 1; i++) {
			for (var j = i + 1; j < assocCount; j++) {
				if ( this.qryAssociates.data[sortColumn][i] > this.qryAssociates.data[sortColumn][j] ||
					(this.qryAssociates.data[sortColumn][i] == this.qryAssociates.data[sortColumn][j] &&
					 this.qryAssociates.data[secondSortColumn][i] > this.qryAssociates.data[secondSortColumn][j]) ) {
					tempAssoc.GUIASSOCIATEID = this.qryAssociates.data.GUIASSOCIATEID[i];
					tempAssoc.SZFIRSTNAME = this.qryAssociates.data.SZFIRSTNAME[i];
					tempAssoc.SZLASTNAME = this.qryAssociates.data.SZLASTNAME[i];
					tempAssoc.SZDIRECTWORKPHONENUMBER = this.qryAssociates.data.SZDIRECTWORKPHONENUMBER[i];
					tempAssoc.BYTSPECIALIZATION = this.qryAssociates.data.BYTSPECIALIZATION[i];
					
					this.qryAssociates.data.GUIASSOCIATEID[i] = this.qryAssociates.data.GUIASSOCIATEID[j];
					this.qryAssociates.data.SZFIRSTNAME[i] = this.qryAssociates.data.SZFIRSTNAME[j];
					this.qryAssociates.data.SZLASTNAME[i] = this.qryAssociates.data.SZLASTNAME[j];
					this.qryAssociates.data.SZDIRECTWORKPHONENUMBER[i] = this.qryAssociates.data.SZDIRECTWORKPHONENUMBER[j];
					this.qryAssociates.data.BYTSPECIALIZATION[i] = this.qryAssociates.data.BYTSPECIALIZATION[j];
					
					this.qryAssociates.data.GUIASSOCIATEID[j] = tempAssoc.GUIASSOCIATEID;
					this.qryAssociates.data.SZFIRSTNAME[j] = tempAssoc.SZFIRSTNAME;
					this.qryAssociates.data.SZLASTNAME[j] = tempAssoc.SZLASTNAME;
					this.qryAssociates.data.SZDIRECTWORKPHONENUMBER[j] = tempAssoc.SZDIRECTWORKPHONENUMBER;
					this.qryAssociates.data.BYTSPECIALIZATION[j] = tempAssoc.BYTSPECIALIZATION;
				}					
			}
		}
	},
	isNotLetterLink: function(el) {
		return ! YAHOO.util.Dom.hasClass(el,"search-letter");
	},
	clearSearchTable: function() {
		var resultsBody = document.getElementById("resultsBody");
		for (var i = resultsBody.childNodes.length - 1; i >= 0; i--) {
			if (resultsBody.childNodes[i].nodeType == 1) {
				resultsBody.removeChild(resultsBody.childNodes[i]);
			}
		}
	},
	clearSearch: function() {
		if (this.currentSearchLink != "") {
			this.currentSearchLink.style.fontWeight = "";
		}
		this.qryResults.RECORDCOUNT = 0;
		this.qryResults.data.GUIASSOCIATEID = [];
		this.qryResults.data.SZFIRSTNAME = [];
		this.qryResults.data.SZLASTNAME = [];
	},
	navigateBy: function(index) {
		if (index + this.currentPageNumber > 0 && (((this.currentPageNumber + index) - 1) * 10) < this.qryResults.RECORDCOUNT) {
			this.currentPageNumber = this.currentPageNumber + index;
			this.clearSearchTable();
			this.showPage();
		}
	},
	showPage: function() {
		var resultsBody = document.getElementById("resultsBody");
		for (var i = (this.currentPageNumber - 1) * 10; i < this.currentPageNumber * 10; i++) {
			if (i < this.qryResults.RECORDCOUNT) {
				if (this.currentSearchField == "SZLASTNAME") {
					var name = this.qryResults.data.SZLASTNAME[i] + ", " + this.qryResults.data.SZFIRSTNAME[i];
				}
				else {
					var name = this.qryResults.data.SZFIRSTNAME[i] + " " + this.qryResults.data.SZLASTNAME[i];
				}
				var phone = this.qryResults.data.SZDIRECTWORKPHONENUMBER[i];
				var assocID = this.qryResults.data.GUIASSOCIATEID[i];
				var assocType = this.qryResults.data.BYTSPECIALIZATION[i];
				resultsBody.appendChild(this.buildRow(assocID,name,phone,assocType));
			}
		}
		var prevLink = document.getElementById("prevLink");
		var nextLink = document.getElementById("nextLink");
		if (this.currentPageNumber == 1) {
			prevLink.className = "disabled";
		}
		else {
			prevLink.className = "";
		}
		if (this.qryResults.RECORDCOUNT > this.currentPageNumber * 10) {
			nextLink.className = "";
		}
		else {
			nextLink.className = "disabled";
		}
		if (this.qryResults.RECORDCOUNT == 0) {
			var row = document.createElement("tr");
			var nameCol = document.createElement("td");
			var numberCol = document.createElement("td");
			nameCol.innerHTML = "<em>no results</em>";
			numberCol.innerHTML = "&nbsp;";
			row.appendChild(nameCol);
			row.appendChild(numberCol);
			resultsBody.appendChild(row);
		}
	},
	populateResultRow: function(row) {
		var currentRow = this.qryResults.RECORDCOUNT;
		this.qryResults.data.GUIASSOCIATEID[currentRow] = this.qryAssociates.data.GUIASSOCIATEID[row];
		this.qryResults.data.SZFIRSTNAME[currentRow] = this.qryAssociates.data.SZFIRSTNAME[row];
		this.qryResults.data.SZLASTNAME[currentRow] = this.qryAssociates.data.SZLASTNAME[row];
		this.qryResults.data.SZDIRECTWORKPHONENUMBER[currentRow] = this.qryAssociates.data.SZDIRECTWORKPHONENUMBER[row];
		this.qryResults.data.BYTSPECIALIZATION[currentRow] = this.qryAssociates.data.BYTSPECIALIZATION[row];
	},
	searchLetter: function(letter, searchLink, searchField) {
		this.clearSearchTable();
		this.clearSearch();
		searchLink.style.fontWeight = "bold";
		this.currentSearchLink = searchLink;
		this.currentSearchField = searchField;
		for (var i = 0; i < parseInt(LQP.index.qryAssociates.RECORDCOUNT); i++) {
			if (this.qryAssociates.data[searchField][i].substr(0,1).toUpperCase() == letter.toUpperCase()) {
				this.populateResultRow(i);				
				this.qryResults.RECORDCOUNT++;
			}
		}
		this.currentPageNumber = 1;
		this.showPage();
	},
	searchAll: function(searchLink, searchField) {
		this.clearSearchTable();
		this.clearSearch();
		searchLink.style.fontWeight = "bold";
		this.currentSearchLink = searchLink;
		this.currentSearchField = searchField;
		for (var i = 0; i < parseInt(LQP.index.qryAssociates.RECORDCOUNT); i++) {
			this.populateResultRow(i);				
			this.qryResults.RECORDCOUNT++;
		}
		this.currentPageNumber = 1;
		this.showPage();
	},
	searchTerm: function() {
		var searchTerm = document.getElementById("search-field").value;
		this.clearSearchTable();
		this.clearSearch();
		var tokens = searchTerm.split(/,?\s/);
		for (var i = 0; i < parseInt(LQP.index.qryAssociates.RECORDCOUNT); i++) {
			var matches = true;
			for (var j = 0; j < tokens.length; j++) {
				if (this.qryAssociates.data.SZFIRSTNAME[i].toUpperCase() != tokens[j].toUpperCase()
				&& this.qryAssociates.data.SZLASTNAME[i].toUpperCase() != tokens[j].toUpperCase()) {
					matches = false;
					break;
				}
			}
			if (matches) {
				
				this.populateResultRow(i);				
				this.qryResults.RECORDCOUNT++;
			}
		}
		this.currentPageNumber = 1;
		this.showPage();
	},
	buildRow: function(assocID, assocName, assocPhone, assocType) {
		var row = document.createElement("tr");
		var nameCol = document.createElement("td");
		var assocLink = document.createElement("a");
		
		var nameSplit = assocName.toLowerCase().split(",");
		
		assocLink.setAttribute("href","/" + nameSplit[1].replace(/[\s\']/g,"") + nameSplit[0].replace(/[\s\']/g,""));
		assocLink.innerHTML = assocName; 
		var numberCol = document.createElement("td");
		nameCol.appendChild(assocLink);
		numberCol.innerHTML = assocPhone + "&nbsp;";
		var typeCol = document.createElement("td");
		var assocTypeString = "";
		if (assocType == 1) {
			assocTypeString = "Residential";
		}
		else if (assocType == 2) {
			assocTypeString = "Commercial";
		}
		typeCol.innerHTML = assocTypeString + "&nbsp;";
		row.appendChild(nameCol);
		row.appendChild(numberCol);
		row.appendChild(typeCol);
		return row;
	}		  
};