/************************************************************************
 * Set  of  classes and functions that will help generate AJAX queries using jQuery
 * library and parse responses.
 * 
 * The MIT License
 * 
 * Copyright (c) 2008 Victor Denisenkov aka Mr.V!T
 * 
 * Permission  is hereby granted, free of charge, to any person obtaining a copy of
 * this  software  and  associated documentation files (the "Software"), to deal in
 * the  Software  without  restriction,  including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the  Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * 
 * The  above  copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR  A  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT  HOLDERS  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN  AN  ACTION  OF  CONTRACT,  TORT  OR  OTHERWISE,  ARISING  FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * @package vit-lib
 * @author Victor Denisenkov aka Mr.V!T
 * @version 1.0
\**********************************************************************************/

var ajaxReturn = function (status_place, destination) {
	if (status_place) this.statusPlace = status_place;
	if (destination) this.controller = destination;
} //CONSTRUCTOR ajaxReturn

ajaxReturn.prototype = {
	controller	: '',
	post		: {},
	insertPlace	: '',
	statusPlace	: '',
	
	onSuccess	: function (data) {},
	onError		: function (data) {},
	onFail		: function (data) {},
	onSimple	: function (data) {},
	onStart		: function () {},
	onStatus	: function (html) {
		
	}, //FUNC onStatus

	parseResult	: function (result) {

		var res		= {};
		res._post	= this.post;

		var data		= result.split('-- --=- CUT -=-- --');
		res._message	= data[0];
		res._fullhead	= data.length != 1;
		
		if (res._fullhead) {
			var head		= JSON.parse(data[1]);
			res._major	= head.major;
			res._minor	= head.minor;
			res._status	= head.minor;
			res._fields	= head.fields;
//			res.data	= [];
			var di = 2;
			if ( res._fields.length != 0 ) {
				for (i = 0; i < res._fields.length; i++) {
					res[res._fields[i]] = data[di];
					di++;
				} //FOR each field
			} //IF was fields posted
		} //IF was full result
		return res;
	},//FUNC parseResult

	ajax_success	:  function (res) {
		var data = this.parseResult(res);
		
		if (this.statusPlace) jQuery('#'+this.statusPlace).html(data._message);
		if (data._fullhead) {
			if (data._major) this.onSuccess(data);
			if (!data._major) this.onError(data);
		} else {
			this.onSimple(data);
		}//IF
	}, //FUNC sucess

	ajax_error	: function (res) {
		if (this.statusPlace) jQuery('#'+this.statusPlace).html(status_string('POST Failed: '+res));
		this.onFail(res);
	}, //FUNC error

	postForm	: function (form_id) {
		var _ar = this;

		if (_ar.statusPlace) insert_bar(_ar.statusPlace);
		var form = document.getElementById(form_id);

		if (!form) {
			if (_ar.statusPlace) jQuery('#'+_ar.statusPlace).html(status_string('Can\'t find data to POST. Aborting.'));
			return false;
		} //IF form not found

		if (!_ar.controller) {
			if (_ar.statusPlace) jQuery('#'+_ar.statusPlace).html(status_string('Invalid destination. Aborting.'));
			return false;
		} //IF form not found

		this.onStart();
		
		jQuery.ajaxUpload ({
			type		: 'POST', dataType : 'html',
			url		: _ar.controller,
			secureuri	: false,
			uploadform	: form,
			success		: function (res) {
				_ar.ajax_success(res);
			}, //FUNC success
			error		: function (res,status) {
				_ar.ajax_error(status);
			} //FUNC error
		}); //AJAX
	}, //FUNC postForm

	postData	: function (post_data) {
		this.post = post_data;
		var _ar = this;

		if (_ar.statusPlace) insert_bar(_ar.statusPlace);

		if (!_ar.controller) {
			if (_ar.statusPlace) jQuery('#'+_ar.statusPlace).html(status_string('Invalid destination. Aborting.'));
			return false;
		} //IF form not found

		this.onStart();

		jQuery.ajax({
			type	: 'POST', dataType: 'html',
			url	: _ar.controller,
			data	: post_data,
			success		: function (res) {
				_ar.ajax_success(res);
			}, //FUNC success
			error		: function (res,status) {
				_ar.ajax_error(status);
			} //FUNC error
		}); //AJAX
	} //FUNC postData

} //CLASS ajaxReturn
