var AjaxFormInterface = Class.create();

AjaxFormInterface.prototype = Object.extend( (new CCnfDialog('fdb-container')), {

	initialize: function (config)
	{
		this.ajaxLoadingEffect = null;
		this.body = null;
		this.winContainer = 'win-form';

		this.popupObject = null;
		Event.observe(document, 'dom:loaded', this.init.bindAsEventListener(this) );
		this.callback    = (config.callback)?config.callback:null;
	},

	init: function()
	{
		initializeWindow();
	},

	initAjaxForm: function(formName)
	{
		var form = ($(formName));
		form.onsubmit = function() { return false }
		Event.stopObserving(form, 'submit');
		Event.observe ( form, 'submit', this.save.bindAsEventListener(this) );
		var save = form.getElementsBySelector('a[rel="save"]');
		if ( save )
		{
			save.each(function(el) { el.onclick = function() { return false } } )
            save.invoke('observe', 'click', this.save.bindAsEventListener(this) );
		}
	},

	save: function(evt)
	{
		var f = Event.findElement(evt,'form');
		var formParams = f.serialize();
        //this.setLoadingEffect( $(this.winContainer).down('div[class="popUp1"]').id );
        this.setLoadingEffect( $(this.winContainer).down('div',1));
        //this.setLoadingEffect( this.winContainer );

        if (f.method == 'post')
        {
            new Ajax.Request(f.action, {
                //onCreate: this.onCreateLoading.bind(this),
                //onFailure: this.onFailureLoading.bind(this),
                'onSuccess':this.successLoading.bind(this),
                'postBody':formParams,
                'evalJS':true
            });
        }
        else
        {
            new Ajax.Request(f.action+'?'+formParams, {
                //onCreate: this.onCreateLoading.bind(this),
                //onFailure: this.onFailureLoading.bind(this),
                'onSuccess':this.successLoading.bind(this),
                'evalJS':true
            });
        }
        return false;
	},
	successLoading: function (response)
    {
    	try {
    		
    		if ('object' != typeof(popupWindow) && 'object' == typeof(popupWindowPos))
    		{
    			var popupWindow = popupWindowPos;    			
    		}
			
			this.removeLoadingEffect();		
			popupWindow.successLoading(response);					
			popupWindow.actions();
		} catch(e) {alert($H(e).inspect())}
        return false;        
    },

	removeLoadingEffect: function()
    {
		if(this.ajaxLoadingEffect != null){
			this.ajaxLoadingEffect.removeLoadingEffect();
		}	
    },

	setLoadingEffect: function(objContainer)
    {
		if(this.ajaxLoadingEffect == null)
		{
			this.ajaxLoadingEffect = new AjaxLoadingEffect({'containerId':objContainer,cssLoadingEffect: 'winLoadingEffectTopMenu'});
		}
		this.ajaxLoadingEffect.setLoadingEffect();
	},

    hideInfo: function()
    {
        this.hideCnf();
    }
	
});

var objAjaxForm = new AjaxFormInterface({});

