// Generic message when no search results are found.
var NoResultsMsg = "<h2>The search returned no results.</h2>";

// Heading for search results.
var SearchResultsHeading = "<h1 class='searchHeading'>Search Results</h1>";

// Error handler for AJAX calls.
//
// Arguments:
//      xhr     The XMLHttpRequest object
//      status  Type of error
//      error   The exception object
var jQueryErrorHandler = function (xhr, status, error) {
	var msg = "";
	if (xhr.status == 0) {
		msg = "Server is offline. Contact the administrator to check the network.";
	}
	else if (xhr.status == 404) {
		msg = "Requested URL not found.";
	}
	else if (xhr.status == 500) {
		var json = JSON.parse(xhr.responseText);
		if (json.Message != null) {
			msg = json.Message;
		}
		else {
			msg = "Internel Server Error.";
		}
	}
	else if (status == "parsererror") {
		msg = "Error: Parsing JSON request failed.";
	}
	else if (status == 'timeout') {
		msg = "Request time out.";
	}
	else {
		msg = "Unknown error: " + xhr.responseText;
	}
	if (msg != "") alert(msg);
}

// Shows the loading icon before sending search request.
var searchBeforeSend = function (xhr) {
	$("#resultsLoadingIcon").show();
}

// Hides the loading icon before sending search request.
var searchComplete = function (xhr, status) {
	$("#resultsLoadingIcon").hide();

}

// Displays the search results and hides billboard and content.
var searchSuccess = function (data, status, xhr) {
	if (data.d != null && data.d != "") {
		$get("pnlSearchResults").innerHTML = SearchResultsHeading + data.d;
	}
	else {
		$get("pnlSearchResults").innerHTML = SearchResultsHeading + NoResultsMsg;
	}
	$("#content").hide();
	$("#BillboardArea").hide();
	$("#pnlSearchResults").show();
}

// The callback to perform a search. This has to be initially triggered from javascript
// and it will pass the function name via AJAX POST so rendered output can trigger
// this function for paging.
//
// Arguments:
//      scopes          List of scopes (comma separated)
//      keywords        Search keywords separated by space (use +/- to include/exclude items)
//      startIndex      Starting index of search results for paging.
//      pageSize        Page size (# rows) of search results for paging.
//      beforeSendFn    The javascript function name to be executed with eval() before sending AJAX request.
//      completeFn      The javascript function name to be executed with eval() after AJAX request completes.
//      successFn       The javascript function name to be executed with eval() when display successful search results.
var Search = function (scopes, keywords, startIndex, pageSize, beforeSendFn, completeFn, successFn) {
	$.ajax({
		type: "POST",
		url: PagePath + "/Search",  // pagePath is defined from code-behind in startup script.
		contentType: "application/json; charset=utf-8",
		data: "{ 'scopes': '" + scopes + "', 'keywords': '" + keywords + "', 'startIndex': '" + startIndex + "', 'pageSize': '" + pageSize + "', 'callback': 'Search', 'beforeSendFn': '" + beforeSendFn + "', 'completeFn': '" + completeFn + "', 'successFn': '" + successFn + "' }",
		dataType: "json",
		beforeSend: function (xhr) {
			eval(beforeSendFn + "(xhr);");
		},
		complete: function (xhr, status) {
			eval(completeFn + "(xhr, status);");
		},
		success: function (data, status, xhr) {
			eval(successFn + "(data, status, xhr);");
		},
		error: jQueryErrorHandler
	});
}

