/*
 * customSearch.js v1.1 (created: 2007/08/16, updated: 2007/10/09)
 * 
 * Company: Statusfirm
 * Coded by: Chesley Brown
 * Contact: c.brown@statusfirm.com
 * 
 * ***Description***
 * Used for quick searching on the teams page using Ajax (json)
 * ******
 * 
 * *****Requires*****
 * jquery.js (tested with v1.2)
 * switchPage.js (tested with v1.0)
 * ******
 * 
 */
function classCustomSearch() { this.baseHREF = document.getElementsByTagName("base")[0].href; this.jsonLocation = '_jsonSearch.php'; this.ajaxMethod = 'GET'; this.ajaxCache = false; this.ajaxDataType = 'json'; this.ajaxTimeout = 20000; this.jsonData = null; this.jsonStatus = null; this.searchString = ''; this.searchParametersID = new Object(); this.searchParametersString = new Object(); this.searchParametersIDRequired = new Array(); this.searchParametersIDChange = new Array(); this.searchParametersIDChangeValue = new Array(); this.IDExt = ''; this.objectName = 'mySearch'; this.resultsPerPage = 10; this.previousPageNum = 1; this.pageContainerID = 'SearchResults'; this.displayPageID = 'SearchResultsDisplay'; this.pageLoadingID = 'SearchLoading'; this.pageViewMsgID = 'SearchPageView'; this.pageListNumPrevious = ''; this.pageListNumPreviousID = ''; this.pageListID = 'SearchPageList'; this.searchMsgID = 'SearchMsg'; this.switchPageFunc = 'SwitchSearchPage'; this.pageListSeparator = '&nbsp;'; this.useEffects = true; this.useEffectsForSort = false; this.effectsTimeStamp = 0; this.effectsSlideDuration = 500; this.effectsFadeDuration = 500; this.entryHeaders = ''; this.entryResults = ''; this.pageList = ''; this.sortHistory = new Object(); this.inputOnKeyPress = new Array(); this.sortDisplayLeftOnASC = ''; this.sortDisplayLeftOnDESC = ''; this.sortDisplayLeftOff = ''; this.sortDisplayRightOnASC = ' &uarr;'; this.sortDisplayRightOnDESC = ' &darr;'; this.sortDisplayRightOff = ' &nbsp;'; this.useAlternatingRowClass = true; this.attrTable = "cellspacing='0' style='width: 100%;'"; this.classTable = null; this.classRow1 = 'row1'; this.classRow2 = 'row2'; this.searchMsg = ''; this.msgNoResultsFound = 'No Results Found'; this.msgNoResultsFoundLong = 'Sorry, nothing matched your search'; this.msgResultsFound = 'Total Results: '; this.msgPageView = 'page '; this.msgErrorTimeout = 'Timeout Error'; this.msgErrorTimeoutLong = 'Your request has timed out, please try again'; this.msgErrorPageNotFound = 'Page Not Found'; this.msgErrorPageNotFoundLong = 'Sorry, the search page could not be found'; this.msgErrorUnknown = 'Unknown Error'; this.msgErrorUnknownLong = 'Sorry, an unknown error was encountered during your search'; this.pageLoadingLock = false; this.startCustomSearch = startCustomSearch; this.resultsCustomSearch = resultsCustomSearch; this.errorCustomSearch = errorCustomSearch; this.printCustomSearch = printCustomSearch; this.sortCustomSearch = sortCustomSearch; this.switchPageStart = switchPageStart; this.switchPageMiddle = switchPageMiddle; this.switchPageComplete = switchPageComplete; this.switchPage = switchPage; this.switchPageContent = switchPageContent;}
function returnFalse() { return false;}
function startCustomSearch() { if (this.pageLoadingLock == false) { var searchParametersIDCheckStatus = true; for (var i = 0; i < this.searchParametersIDRequired.length; i++) { var objInput = document.getElementById(this.searchParametersID[this.searchParametersIDRequired[i]] + this.IDExt); var searchParametersIDValue = objInput.value.replace(/^\s+|\s+$/g, ''); if (searchParametersIDValue != '' && typeof(searchParametersIDValue) != 'undefined' && searchParametersIDCheckStatus == true) { searchParametersIDCheckStatus = true;} else { searchParametersIDCheckStatus = false;}
}
if (this.jsonStatus == null || this.jsonStatus == 'ok' || this.jsonStatus == 'noresults') { for (var i = 0; i < this.searchParametersIDChange.length; i++) { var objInput = document.getElementById(this.searchParametersID[this.searchParametersIDRequired[i]] + this.IDExt)
var searchParametersIDValue = objInput.value.replace(/^\s+|\s+$/g, ''); if (searchParametersIDValue != this.searchParametersIDChangeValue[i] && searchParametersIDCheckStatus == true) { searchParametersIDCheckStatus = true;} else { searchParametersIDCheckStatus = false;}
}
}
var searchParameters = ''; for (key in this.searchParametersID) { if (searchParameters.length > 0) { searchParameters = searchParameters + '&';}
searchParameters = searchParameters + key + '=' + escape(document.getElementById(this.searchParametersID[key] + this.IDExt).value.replace(/^\s+|\s+$/g, ''));}
for (key in this.searchParametersString) { if (searchParameters.length > 0) { searchParameters = searchParameters + '&';}
searchParameters = searchParameters + key + '=' + escape(this.searchParametersString[key]);}
}
if (searchParametersIDCheckStatus == true) { this.switchPageStart(); this.inputOnKeyPress = new Array(); for (var i = 0; i < this.searchParametersIDRequired.length; i++) { var objInput = document.getElementById(this.searchParametersID[this.searchParametersIDRequired[i]] + this.IDExt); objInput.blur(); objInput.disabled = true;}
var self = this; for (var i = 0; i < this.searchParametersIDChange.length; i++) { var searchParametersIDValue = document.getElementById(this.searchParametersID[this.searchParametersIDRequired[i]] + this.IDExt).value.replace(/^\s+|\s+$/g, ''); this.searchParametersIDChangeValue[i] = searchParametersIDValue;}
var opt = { url: this.baseHREF + this.jsonLocation, type: this.ajaxMethod, dataType: this.ajaxDataType, cache: this.ajaxCache, timeout: this.ajaxTimeout, data: searchParameters, success: function(data) { if ($(data)[0] != null) { self.jsonData = $(data)[0]; self.jsonStatus = self.jsonData['status']['searchStatus']; self.resultsCustomSearch();} else { self.jsonStatus = 'unknown'; self.errorCustomSearch(self.msgErrorUnknown, self.msgErrorUnknownLong);}
self.previousPageNum = 1; self.printCustomSearch(); self.switchPageComplete();}, error: function(data, status) { if (status == 'timeout') { self.jsonStatus = 'timeout'; self.errorCustomSearch(self.msgErrorTimeout, self.msgErrorTimeoutLong);} else { if (data.status == 404) { self.jsonStatus = 'pagenotfound'; self.errorCustomSearch(self.msgErrorPageNotFound, self.msgErrorPageNotFoundLong);}
else { self.jsonStatus = 'unknown'; self.errorCustomSearch(self.msgErrorUnknown, self.msgErrorUnknownLong);}
}
self.printCustomSearch(); self.switchPageComplete();}
}
$.ajax(opt);}
}
function resultsCustomSearch() { var searchStatus = this.jsonData['status']['searchStatus']; var searchString = this.jsonData['status']['searchString']; var classRow = this.classRow2; var entryCount = this.jsonData['content'].length; var pageNum = 0; if (searchStatus == 'ok' && entryCount > 0) { this.entryHeaders = ''; this.entryResults = ''; this.pageList = ''; this.entryHeaders = this.entryHeaders + "<tr>"; for (var headerKey in this.jsonData['headers']) { var entryHeaderAttributes = ''; for (var attributeKey in this.jsonData['headers'][headerKey]['attributes']) { entryHeaderAttributes = entryHeaderAttributes + " " + attributeKey + "='" + this.jsonData['headers'][headerKey]['attributes'][attributeKey] + "' ";}
this.entryHeaders = this.entryHeaders + "<th" + entryHeaderAttributes + ">"; if (this.sortDisplayLeftOnASC != '' && this.sortDisplayRightOnASC != '') { if (this.jsonData['headers'][headerKey]['sorted'] == 'ASC') { this.entryHeaders = this.entryHeaders + this.sortDisplayLeftOnASC;}
else if (this.jsonData['headers'][headerKey]['sorted'] == 'DESC') { this.entryHeaders = this.entryHeaders + this.sortDisplayLeftOnDESC;}
else { this.entryHeaders = this.entryHeaders + this.sortDisplayLeftOff;}
}
this.entryHeaders = this.entryHeaders + "<a href=\"javascript:" + this.objectName + ".sortCustomSearch('" + headerKey + "');\">" + this.jsonData['headers'][headerKey]['title'] + "</a>"; if (this.sortDisplayRightOnASC != '' && this.sortDisplayRightOnDESC != '') { if (this.jsonData['headers'][headerKey]['sorted'] == 'ASC') { this.entryHeaders = this.entryHeaders + this.sortDisplayRightOnASC;}
else if (this.jsonData['headers'][headerKey]['sorted'] == 'DESC') { this.entryHeaders = this.entryHeaders + this.sortDisplayRightOnDESC;}
else { this.entryHeaders = this.entryHeaders + this.sortDisplayRightOff;}
}
this.entryHeaders = this.entryHeaders + "</th>";}
this.entryHeaders = this.entryHeaders + "</tr>"; for (var entryNum = 0; entryNum < entryCount; entryNum++) { if ((pageNum*this.resultsPerPage) == entryNum) { this.entryResults = this.entryResults + "<div id='" + this.pageContainerID + this.IDExt + "_page_" + (pageNum+1) + "' style='display: block;'>"; this.entryResults = this.entryResults + "<table "; if (this.classTable != null) { this.entryResults = this.entryResults + "class='" + this.classTable + "' ";}
if (this.attrTable != null) { this.entryResults = this.entryResults + this.attrTable;}
this.entryResults = this.entryResults + ">"; this.entryResults = this.entryResults + this.entryHeaders;}
if (this.useAlternatingRowClass == true) { classRow = (classRow == this.classRow2 ) ? this.classRow1 : this.classRow2; var entryRowClass = " class='" + classRow + "'";} else { var entryRowClass = '';}
this.entryResults = this.entryResults + "<tr>"; for (var headerKey in this.jsonData['headers']) { this.entryResults = this.entryResults + "<td" + entryRowClass + ">"; for (var contentKey in this.jsonData['content'][entryNum][headerKey]) { this.entryResults = this.entryResults + this.jsonData['content'][entryNum][headerKey][contentKey];}
this.entryResults = this.entryResults + "</td>";}
this.entryResults = this.entryResults + "</tr>"; if ((this.resultsPerPage*(pageNum+1)-1) == entryNum || (entryNum+1) == entryCount) { this.entryResults = this.entryResults + "</table>"; this.entryResults = this.entryResults + "</div>"; this.pageList = this.pageList + "<div style='float: left;' id='" + this.pageListID + this.IDExt + "_page_" + (pageNum+1) + "'><a href=\"javascript:" + this.objectName + ".switchPage('" + this.pageContainerID + this.IDExt + "_page_" + (pageNum+1) + "', " + (pageNum+1) + ")\">" + (pageNum+1) + "</a>" + this.pageListSeparator + "</div>"; pageNum++;}
}
this.searchMsg = this.msgResultsFound + "<b>" + entryCount + "</b>";} else { this.searchMsg = this.msgNoResultsFound; this.errorCustomSearch(this.msgNoResultsFound, this.msgNoResultsFoundLong);}
}
function errorCustomSearch(msgShort, msgLong) { this.searchMsg = msgShort; this.entryResults = "<div id='" + this.pageContainerID + this.IDExt + "_page_1' style='display: block;'><div style='text-align: center; padding: 5px;'>" + msgLong + "</div></div>"; this.pageList = "<div style='float: left;' id='" + this.pageListID + this.IDExt + "_page_1'><a href=\"JavaScript:" + this.objectName + "switchPage('" + this.pageContainerID + this.IDExt + "_page_1', 1)\">1</a>" + this.pageListSeparator + "</div>";}
function printCustomSearch() { document.getElementById(this.searchMsgID + this.IDExt).innerHTML = this.searchMsg
document.getElementById(this.pageContainerID + this.IDExt).innerHTML = this.entryResults; document.getElementById(this.pageListID + this.IDExt).innerHTML = this.pageList; this.switchPageContent(this.pageContainerID + this.IDExt + "_page_" + this.previousPageNum, this.previousPageNum); for (var i = 0; i < this.searchParametersIDRequired.length; i++) { var objInput = document.getElementById(this.searchParametersID[this.searchParametersIDRequired[i]] + this.IDExt); objInput.disabled = false;}
for (var i = 0; i < this.searchParametersIDRequired.length; i++) { var objInput = document.getElementById(this.searchParametersID[this.searchParametersIDRequired[i]] + this.IDExt); objInput.focus(); break;}
}
function sortCustomSearch(sortKey) { if (this.jsonData != null && this.pageLoadingLock == false) { if (this.jsonData['content'].length > 1 && this.jsonData['headers'][sortKey]['sortable'] != 0) { if (this.useEffectsForSort == true) { this.switchPageStart();}
if (this.jsonData['headers'][sortKey]['sorted'] == null || this.jsonData['headers'][sortKey]['sorted'] == 'DESC') { this.jsonData['content'] = this.jsonData['content'].sort( function(a,b) { var avalue = a[sortKey]['value']; var bvalue = b[sortKey]['value']; if (avalue < bvalue) { return -1;}
else if (avalue > bvalue) { return 1;}
else { return 0;}
} ); for (var headerKey in this.jsonData['headers']) { this.jsonData['headers'][headerKey]['sorted'] = null;}
this.jsonData['headers'][sortKey]['sorted'] = 'ASC';}
else if (this.jsonData['headers'][sortKey]['sorted'] == 'ASC') { this.jsonData['content'] = this.jsonData['content'].sort( function(a,b) { var avalue = a[sortKey]['value']; var bvalue = b[sortKey]['value']; if (bvalue < avalue) { return -1;}
else if (bvalue > avalue) { return 1;}
else { return 0;}
} ); for (var headerKey in this.jsonData['headers']) { this.jsonData['headers'][headerKey]['sorted'] = null;}
this.jsonData['headers'][sortKey]['sorted'] = 'DESC';}
this.resultsCustomSearch(); this.printCustomSearch(); if (this.useEffectsForSort == true) { this.switchPageComplete();}
}
}
}