/*
 * PostBack (for jQuery)
 * version: 1.0 (12/03/2009)
 * @requires jQuery v1.2 or later
 *
  *
 * Licensed under the MIT:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2009 Byss.pl
 *
 */
 
(function($) {
	
  $.postBack = function() {
  }

  /*
   * Public, $.showme methods
   */

  $.extend($.postBack, {
		
		showErrorMessage: function(text){

			_showme = getObject('showme');
			_showme.html(text);
			$.showme.showDialog(_showme, 400, 100, 'Error');
			
		}
		
	});	
	
  $.fn.postBack = function(options) {
		
		_self = $(this);
		_form = getObject(options.form);
		_update = getObject(options.update);
		_refresh = getObject(options.refresh);
		
		getObject('faction').val(options.action);
		
    options = $.extend({
			referer: options.referer || "dialog",
			dataType: "html",
			beforeSubmit:  function (){
				activateProgress(_self);
			},
			success: function (data, responseText) {
				//alert(data);
				deactivateProgress();
				
				if (parseInt(data)){
					
					if (options.referer == 'dialog')
						_update.dialog('close');
					
					if (_refresh && options.refreshAction){
						_refresh.refresh({url: options.refreshAction});
					}
					
					if( options.callback )
						options.callback.apply();								
						
				}else{
					//alert(data);
					if (options.referer == 'dialog'){
						_update.html(data);
					}else{
						
						_message = data.split('|');
						//alert(data);
						jQuery.showDialogError(_message[1]);
					}
					
					if (options.debug == 1)
						alert(_message[1]);
				}		
				
				//unbind submit event
				if (options.unbind)
					_form.unbind('submit');
				
			}
    }, options || {});
		
		_form.submit(function(){
			
			$(this).ajaxSubmit(options);
			
			return false;
			
		});
					
		_form.submit();
		
	}
})(jQuery);

