/**
 * Create an Ajax search object.
 * @param searchFieldId Id of the user criteria search field.
 * @param resultCellId Id of the div which stores the results.
 * @param loadingImageId Id of the image which displays loading progress.
 * @param ajaxFunc Pointer to the function which sends ajax request.
 * @param renderFunc Pointer to function which processes the ajax response.
 * @param curPage Current page index.
 * @param timeOut Timout after which to send the ajax request if user didn't press any buttons.
 * @param instanceName Variable name of the object to which the result of this function is assigned.
 */
function AjaxMemberSearch(searchKeywordFieldId,searchByFieldId,countryCodeFieldId,stateCodeFieldId,cityCodeFieldId,genderFieldId, resultCellId, loadingImageId, communityId, ajaxFunc, renderFunc, curPage, timeOut, instanceName) {
	//criteria fields
	this.searchKeywordFieldId = searchKeywordFieldId;
	this.searchByFieldId = searchByFieldId;
	this.countryCodeFieldId = countryCodeFieldId;
	this.stateCodeFieldId = stateCodeFieldId;
	this.cityCodeFieldId = cityCodeFieldId;
	this.genderFieldId = genderFieldId;
	this.resultCellId = resultCellId;
	this.loadingImageId = loadingImageId;
	this.communityId = communityId;
	this.ajaxFunc = ajaxFunc;
	this.renderFunc = renderFunc;
	this.timeOut = timeOut;
	this.instanceName = instanceName;
	this.curPage = curPage;

	this.typeTimer = null;
	this.prevSearchKeyword = '';
	this.prevSearchBy =' ';
	this.prevCountryCode = '';
	this.prevStateCode = '';
	this.prevCityCode = '';
	this.prevGender = '';
	this.prevPage = '';
	this.loadingImage = new Image(32, 32);
	this.loadingImage.src = "/images/loading.gif";
}

AjaxMemberSearch.prototype.clearTypeTimer = function() {
	if (this.typeTimer != null) {
		clearTimeout(this.typeTimer);
		this.typeTimer = null;
	}
}

AjaxMemberSearch.prototype.setTypeTimer = function() {
	this.typeTimer = setTimeout(this.instanceName + ".sendSearchRequest(1)", this.timeOut);
}

AjaxMemberSearch.prototype.queueSearchRequest = function()
{
	
	this.clearTypeTimer();
	this.setTypeTimer();
}

AjaxMemberSearch.prototype.sendSearchRequest = function(pageIdx)
{
	this.clearTypeTimer();
	//var objSearchField = searchKeyword; //document.getElementById(this.searchFieldId);
	var searchKeyword = document.getElementById(this.searchKeywordFieldId).value; //objSearchField.value;
	var searchBy = document.getElementById(this.searchByFieldId).value;//document.getElementById(this.searchBy).index;	
	var countryCode = document.getElementById(this.countryCodeFieldId).value;
	var stateCode = document.getElementById(this.stateCodeFieldId).value;
	var cityCode = document.getElementById(this.cityCodeFieldId).value;
	var gender = '';//this.gender;
	if (document.invitePeople.gender[0].checked){
		gender = 'male';
	}
	if (document.invitePeople.gender[1].checked){
		gender = 'female';
	}
	if (document.invitePeople.gender[2].checked){
		gender = 'all';
	}
	// trim
	var communityId = this.communityId;
	searchKeyword = searchKeyword.replace(/\s*((\S+\s*)*)/, "$1");
	searchKeyword = searchKeyword.replace(/((\s*\S+)*)\s*/, "$1");

	if (this.prevSearchKeyword == searchKeyword && this.prevSearchBy == searchBy && this.prevCountryCode == countryCode && this.prevStateCode == stateCode && this.prevCityCode == cityCode && this.prevGender == gender && pageIdx == this.prevPage) return;
	this.prevSearchKeyword = searchKeyword;
	this.prevSearchBy = searchBy;
	this.prevCountryCode = countryCode;
	this.prevStateCode = stateCode;
	this.prevCityCode = cityCode;
	this.prevGender = gender;
	this.prevPage = pageIdx;

	var loadingImageObj = document.getElementById(this.loadingImageId);
	
		if (loadingImageObj) {
			loadingImageObj.style.visibility = 'visible';
			//loadingImageObj.src = this.loadingImage.src;
		}
		
		this.ajaxFunc(searchKeyword,searchBy,countryCode,stateCode,cityCode,communityId,gender, pageIdx, this.renderFunc);
	
}

AjaxMemberSearch.prototype.createPagingTable = function(tableId, infoId, pagingId) {
	// Paging table
	var pagingTable = document.createElement("table");
	pagingTable.cellPadding = 2;
	pagingTable.cellSpacing = 0;
	pagingTable.border = 0;
	pagingTable.width = "100%";
	//pagingTable.className = "FormTable";
	pagingTable.setAttribute("id", tableId);

	var pagingTbody = document.createElement("tbody");

	var pagingTr = document.createElement("tr");

	var pagingTd1 = document.createElement("td");
	pagingTd1.style.textAlign = "left";
	pagingTd1.style.verticalAlign = "middle";
	pagingTd1.style.paddingLeft = "4px";
	//pagingTd1.className = "FormTd2";
	pagingTd1.width = 200;
	pagingTd1.setAttribute("id", infoId);

	var pagingTd2 = document.createElement("td");
	pagingTd2.style.textAlign = "right";
	pagingTd2.style.verticalAlign = "middle";
	pagingTd2.style.paddingRight = "4px";
	pagingTd2.className = "FormTd2";
	pagingTd2.setAttribute("id", pagingId);

	pagingTr.appendChild(pagingTd1);
	pagingTr.appendChild(pagingTd2);
	pagingTbody.appendChild(pagingTr);
	pagingTable.appendChild(pagingTbody);

	return pagingTable;
}

AjaxMemberSearch.prototype.constructPagingInfo = function(startRec, endRec, totRec, pageIdx, pageCount) {
	/*var pagingInfoNode = document.createElement("span");
	pagingInfoNode.className = "paging";
	pagingInfoNode.appendChild(document.createTextNode('Records ' + startRec + ' - ' + endRec + ' of ' + totRec));

	return pagingInfoNode;*/

	var pagingInfoNode = document.createElement("span");
	pagingInfoNode.className = "paging";

	// Page input
	var inputNode = document.createElement('input');
	inputNode.type = "text";
	inputNode.style.textAlign = "center";
	inputNode.value = pageIdx;
	inputNode.maxLength = (pageCount + "").length;
	inputNode.size = 4;
	inputNode.onkeyup = new Function(this.instanceName + ".curPage = this.value");
	pagingInfoNode.appendChild(document.createTextNode("Page\u00a0"));
	pagingInfoNode.appendChild(inputNode);
	pagingInfoNode.appendChild(document.createTextNode("\u00a0of\u00a0" + pageCount + "\u00a0"));
	inputNode = document.createElement('input');
	inputNode.type = "button";
	inputNode.className = "button";
	inputNode.value = "Go";
	inputNode.onclick = new Function(this.instanceName + ".changePage()");
	pagingInfoNode.appendChild(inputNode);

	return pagingInfoNode;
}

AjaxMemberSearch.prototype.constructPagingNode = function(pageIdx, pageCount) {
	// Paging span
	var pagingNode = document.createElement('span');
	pagingNode.className = "paging";

	// Text nodes
	var firstNode = document.createTextNode("[First]");
	var prevNode = document.createTextNode("[Prev]");
	var nextNode = document.createTextNode("[Next]");
	var lastNode = document.createTextNode("[Last]");

	// First link
	if (pageIdx > 2) {
		var firstAnchor = document.createElement('a');
		firstAnchor.href = "javascript:" + this.instanceName + ".sendSearchRequest(1)";
		firstAnchor.appendChild(firstNode);
		firstNode = firstAnchor;
	}
	pagingNode.appendChild(firstNode);
	pagingNode.appendChild(document.createTextNode("\u00a0\u00a0"));
	
	// Prev link
	if (pageIdx > 1) {
		var prevAnchor = document.createElement('a');
		prevAnchor.href = "javascript:" + this.instanceName + ".sendSearchRequest(" + (pageIdx - 1) + ")";
		prevAnchor.appendChild(prevNode);
		prevNode = prevAnchor;
	}
	pagingNode.appendChild(prevNode);
	pagingNode.appendChild(document.createTextNode("\u00a0\u00a0\u00a0\u00a0"));

	// Next link
	if (pageIdx < pageCount) {
		var nextAnchor = document.createElement('a');
		nextAnchor.href = "javascript:" + this.instanceName + ".sendSearchRequest(" + (pageIdx + 1) + ")";
		nextAnchor.appendChild(nextNode);
		nextNode = nextAnchor;
	}
	pagingNode.appendChild(nextNode);
	pagingNode.appendChild(document.createTextNode("\u00a0\u00a0"));
	
	// Last link
	if (pageIdx < pageCount - 1) {
		var lastAnchor = document.createElement('a');
		lastAnchor.href = "javascript:" + this.instanceName + ".sendSearchRequest(" + pageCount + ")";
		lastAnchor.appendChild(lastNode);
		lastNode = lastAnchor;
	}
	pagingNode.appendChild(lastNode);

	return pagingNode;
}

AjaxMemberSearch.prototype.clearChildNodes = function(objNode) {
	for (var i = objNode.childNodes.length - 1; i >= 0; i--) {
		objNode.removeChild(objNode.childNodes[i]);
	}
}

AjaxMemberSearch.prototype.assignNode = function(objNode, childNode) {
	this.clearChildNodes(objNode);
	objNode.appendChild(childNode);
}

AjaxMemberSearch.prototype.changePage = function() {
	if (this.curPage.length > 0 && !isNaN(this.curPage/1)) {
		this.sendSearchRequest(this.curPage);
	} else {
		alert('Invalid page number!');
	}
}
