/*
 * File:					ajax.js
 * Title:					AJAX Functions
 * Description:		Contains all of our ajax-specific functions and calls
 * Author:				Dan Bair, Florentine Design (www.florentinedesign.com)
 * NOTE:					This file (ajax.js) must be attached AFTER main.js
 */


// our main ajax url-action file
var ajaxurl = 'index.php'; // path relative from js/ajax.js


 /*	AJAX RESPONDERS AND LOADERS
--------------------------------------------------------->>
--------------------------------------------------------->>
 */


function callInProgress (xmlhttp) {
	switch (xmlhttp.readyState) {
		case 1: case 2: case 3:
			return true;
			break;
		default:
			return false;
			break;
	}
}

function showFailureMessage() {
	alert('Oops! The connection timed out, please try your request again.');
}

// attach our loaders and indicators to all AJAX calls
Ajax.Responders.register({
	onCreate: function(request) {
					request['timeoutId'] = window.setTimeout(
					function() {
						if (callInProgress(request.transport)) {
							request.transport.abort();
							showFailureMessage();
							hideLoader();
							if (request.options['onFailure']) {
								request.options['onFailure'](request.transport, request.json);
							}
						}
					},
						50000);
					showLoader();
				}
				,
	onloading: showLoader,
	onComplete: function(request) {
				hideLoader();
				window.clearTimeout(request['timeoutId']);
				}
});

// attach our error reporting
function reportError(request){
	alert("Could not connect!");
	showError();
	hideLoader();
}


 /*	OUR MIGHTY AJAX FUNCTIONS I (for adding/editing data)
--------------------------------------------------------->>
--------------------------------------------------------->>
 */


function editField(id,targetDIV){
	new Ajax.InPlaceEditor(
	targetDIV,
	ajaxurl,{
	okText					: 'Update',
	cancelText			: 'Cancel',
	highlightcolor	: Ajax.InPlaceEditor.defaultHighlightColor,
	savingText			: 'Updating Field...',
	onComplete			: function(form, value){requestEditField(id,targetDIV);},
	callback				: function(form, value) { return 'action=field&text=' + escape(value) + '&id=' + id}});
}

function requesEditField(id, field, targetDIV){
	new Ajax.Updater(
	{success: targetDIV},
	ajaxurl,{
	method			: 'post',
	onLoading		: showLoader(),
	onComplete	: hideLoader(),
	postBody		: 'action=requestOptionInfo&id=' + id + '&f=' + field});
}

function addCustomer(){
	new Ajax.Updater(
	{success: 'theDIV'},
	ajaxurl,{
	postBody		: 'action=addCustomer',
	parameters	: Form.serialize(this),
	onLoading		: showLoader(),
	onComplete	: function(request){hideLoader();displayCustomerBrowser();}});
}


 /*	OUR MIGHTY AJAX FUNCTIONS II (for displaying data/returning HTML/making things purdy)
--------------------------------------------------------->>
--------------------------------------------------------->>
 */


function displayCustomerBrowser(){
	new Ajax.Updater(
	{success: 'theDIV'},
	ajaxurl,{
	method			: 'get',
	onComplete	: hideLoader,
	evalScripts	: true,
	onLoading		: showLoader,
	parameters	: 'action=getCustomerBrowser',
	onFailure		: reportError});
}

function displayCustomerProfile(id){
	new Ajax.Updater(
	{success: 'theDIV'},
	ajaxurl,{
	method			: 'get',
	onComplete	: hideLoader,
	evalScripts	: true,
	onLoading		: showLoader,
	parameters	: 'action=getCustomerProfile&cid=' + id,
	onFailure		: reportError});
}

function searchItems(query,within){
	new Ajax.Updater(
	{success: 'theDIV'},
	ajaxurl,{
	method			: 'get',
	onComplete	: hideLoader,
	evalScripts	: true,
	onLoading		: showLoader,
	parameters	: 'action=search&q='+ encodeSearch(query) + '&sort=' +within,
	onFailure		: reportError});
}

function searchCustomers(query,within){
	new Ajax.Updater(
	{success: 'customer_browse_data'},
	ajaxurl,{
	method			: 'get',
	onComplete	: function(){hideLoader();hide('edit');hide('view');show('customer_browse');},
	evalScripts	: true,
	onLoading		: showLoader,
	parameters	: 'action=searchCustomers&q=' + encodeSearch(query) + '&f=' + within,
	onFailure		: reportError});
}