/* Generic Function & classes */

var eventWatcher = Class.create();

eventWatcher.prototype = {

   initialize: function(event) {
		this.event = $(event);
		this.elementid = $(event).id;
			this.event.onclick = this.formAction.bindAsEventListener(this);
	},

    formAction: function(event) {
		  	//alert('(' + event.type + ') (' + this.elementid + ')');
			processEvent(this.elementid, event, event.type);	
    }
};

function cleanUp(value) {
   	if (typeof(value) == 'object') value = "";
	return value;
}

function buildElement(tagName, attributes, innerHTML, style) { 

    var element = document.createElement(tagName);

    	if (attributes)
        	$H(attributes).each(function(pair){
			element[pair.key] = pair.value;
			if (pair.key == 'id') {	// throw the name in there for form.serialize()
        		    element['name'] = pair.value;
			}
        });
        
    	if (style) $(element).setStyle($H(style));

	    if (innerHTML) { 
		    if (typeof(innerHTML) == 'object') innerHTML = "";

                if ((tagName == "textarea") || (tagName == "input") || (tagName == "select"))  {
                        element.value = innerHTML;
                } else {
                        $(element).update(innerHTML);
                }
                    if ((element.type == "checkbox") && (innerHTML == true)) element.checked = 'checked';


	    }

	return $(element);

};

function displayError(error) {
	$('feedback').innerHTML = error;
}

var ajaxError = function(t) {
    alert('Error ' + t.status + ' -- ' + t.statusText);
}
