//AJAX HANDLERS
function ajax_json_call(callUrl, callType, callVars, onCompleteFunc) {
	try {
		$.ajax({
			type: callType,
			url: callUrl,
			data: callVars,
			dataType: "json",
			success: function(data) {
				if (data) {
					process_success_ajax_json_call(data);
				} else {
					//error
				}
		 	},
			complete: function() {
				if (eval("typeof " + onCompleteFunc + " == 'function'")) {
					eval(onCompleteFunc+'()');
				}
			}
		});
	} catch (e) {
		//nothing for now
	}
}

function process_success_ajax_json_call(data) {
	if (data) {
		var alert_content="";
		$.each(data, function(id, dataHolder) {
			if ($("#"+id) && id!="alert_msg") {
				if (dataHolder.jAction.indexOf('insert')>-1) {
					$(dataHolder.jContentReturn)[dataHolder.jAction]('#'+dataHolder.jHelpId);
				} else {
					$("#"+id)[dataHolder.jAction](dataHolder.jContentReturn);
				}
			}
			if (id=="alert_msg") alert_content += content+"\n";
		});
		if (alert_content != "") alert(alert_content);
	}
	else {
		//process error here...
	}
}
//
//AJAX HANDLERS

function getMoreSearchResults(sid, iteration, tableName) {
	if ($('#extra_search_button_'+tableName)) {
		$('#extra_search_button_'+tableName).after('<div id="searchRetrievingMsg_'+tableName+'"><img src="filebin/images/getting_results.gif" alt="Retrieving..." style="border:0;margin-top:20px;display:block" /></div>');
		$('#extra_search_button_'+tableName).hide();
	}
	var callVars = "action=getMoreSearchResults&iteration="+iteration+"&sid="+sid+"&table="+tableName;
	ajax_json_call('resources/js/ajax_php/searchAJAX.php', 'POST', callVars, "getMoreSearchResultsComplete('"+tableName+"')");
	return false;
}

function getMoreSearchResultsComplete(tableName) {
	$('#searchRetrievingMsg_'+tableName).remove();
	$('#extra_search_button_'+tableName).show();
}
//
function getMoreNewsResults(rows, iteration, totalRows, url, pid) {
	if (document.getElementById('moreResultsButtonWrapper')) {
		document.getElementById('moreResultsButtonWrapper').innerHTML = '<a href="">Retrieving</a>';
	}
	var callVars = "action=getMoreNewsResults&iteration="+iteration+"&rows="+rows+"&totalRows="+totalRows+"&url="+url+"&pid="+pid;
	//alert(callVars);
	ajax_json_call('resources/js/ajax_php/searchAJAX.php', 'POST', callVars);
	return false;
}

