/*
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 *
 * Copyright 1996-2008 by Sven Homburg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 */

var T5COnEvent = Class.create();
T5COnEvent.prototype = {
    initialize: function(eventName, elementId, requestUrl, onCompleteCallback)
    {
        if (!$(elementId))
            throw(elementId + " doesn't exist!");

        this.element = $(elementId);

        this.onCompleteCallback = null

        if (onCompleteCallback.length > 0)
            this.onCompleteCallback = onCompleteCallback;

        this.requestUrl = requestUrl;

        Event.observe(this.element, eventName, this.reflectOnEvent.bind(this, this.element), false);
    },
    reflectOnEvent: function(myEvent)
    {
        var fieldValue;
        var formElement = this.element.form;
        var url = this.requestUrl;
		
		url = url.split(";")[0];
		
        if (typeof formElement != 'undefined')
            fieldValue = $F(this.element);

        if (typeof fieldValue != 'undefined')
            url += "/" + fieldValue;

        new Ajax.Request(url, {
            method: 'post',
            onFailure: function(t)
            {
                alert(t.responseText);
            },
            onException: function(t, exception)
            {
                alert(exception);
            },
            onSuccess: function(t)
            {
                if (this.onCompleteCallback != null)
                    eval(this.onCompleteCallback + "('" + t.responseText + "')");
            }.bind(this)
        });
    }
}


var ButtonEvent = Class.create();

ButtonEvent.prototype =
{
    initialize: function(elementId, requestUrl)
    {
        if (!$(elementId))
            throw(elementId + " doesn't exist!");

        this.element = $(elementId);
        this.requestUrl = requestUrl;

        Event.observe(this.element, "click",
                this.fireEvent.bind(this, this.element),
                false);
    },
    fireEvent: function(myEvent)
    {
        document.location = this.requestUrl;
        return false;
    }
}
