
AjaxComponent_Field.prototype._domID;
AjaxComponent_Field.prototype._object;
AjaxComponent_Field.prototype._name;
AjaxComponent_Field.prototype._value;

function AjaxComponent_Field(field_name,dom_id,initial_value,set_my_value) {
	if(dom_id) {
		this._object = document.getElementById(dom_id);
	}
	this._domID = dom_id;
	this._name = field_name;
	//this._value = initial_value;
	if(set_my_value) {
		this.set_value(initial_value);
	}
}

AjaxComponent_Field.prototype.get_value = function() { 
	//based on type of node
	if(!this._object) { return 0; }
	switch(this._object.nodeName) {
		case "DIV":
			//this must be parsed to be of much use...
			return this._object.innerHTML;
			break;
		default:
			return this._object.value;
			break;
	}
}

AjaxComponent_Field.prototype.set_value = function(value) { 
	if(!this._object) { if(typeof(_dbg_output) != "undefined") { _dbg_output.innerHTML += "NO SUCH OBJECT."; } return 0; }
	switch(this._object.nodeName) {
		case "SPAN":
		case "DIV":
			this._object.innerHTML	= value;
			break;
		case "input":
			if(this._object.type == "checkbox" || this._object.type == "radio") {
				alert(this._object.type);
				this._object.checked = "checked";
			} else {
				this._object.value = value;
			}
			break;
		default:
			this._object.value		= value;
			break;
	}
	//_dbg_output.innerHTML += "NODENAME=>" + this._object.nodeName + "<br/>";
	return 1;
}



AjaxComponent_Field.prototype.set_object_attribute = function(key,value) {
	this._object.setAttribute(key,value);
}

AjaxComponent_Field.prototype.get_name = function() { return this._name; }
AjaxComponent_Field.prototype.get_dom = function() { if(!this._object) { return 0; } return this._object.id; }
AjaxComponent_Field.prototype.hide = function() { if(this._object) { this._object.style.display = "none"; } } 
AjaxComponent_Field.prototype.show = function() { if(this._object) { this._object.style.display = "block"; } } 

////////////////////////////////////////////////////////////////////

// AjaxMech
AjaxComponent.prototype._mech;

AjaxComponent.prototype._baseURL;
AjaxComponent.prototype._fields;
AjaxComponent.prototype._name;

AjaxComponent.prototype._group;

function AjaxComponent() {
	this.set_base_url("?");
}


function AjaxComponent(refname, target_url, form_fields) {
	this._fields = form_fields;
	this._baseURL = target_url;
	this._name = refname;
	this.set_base_url(target_url);
}


AjaxComponent.prototype.set_base_url = function(url) {
	this._baseURL = url;
	if(!this._baseURL) { return; }
	if(this._baseURL.indexOf("?") == -1) {
		this._baseURL += "?";
	}
	this._baseURL += "ajaxComponent_name=" + this._name;
}


AjaxComponent.prototype.get_base_url = function() {
	return this._baseURL;
}

AjaxComponent.prototype._ajax_response_handler;

AjaxComponent.prototype.set_ajax_handler = function(obj,funref) {
	this._mech.setHandler(this._mech.AJAXRS_DONE,obj,funref);
}

AjaxComponent.prototype.prepare = function() {
	this._mech.connect("get",this.get_url_string(),true);
}
	
AjaxComponent.prototype.send = function() { 
	this._mech.send();
}




AjaxComponent.prototype.get_name = function() { return this._name; }





AjaxComponent.prototype.get_url_string = function() {
	// construct and return appropriate url, with relevant data
	var url = this._baseURL;
	for(var i in this._fields) {
		url += "&" + this._fields[i].get_name() + "=" + this._fields[i].get_value();
	}
	return url;
}


AjaxComponent.prototype.has_field = function(key) {
	for(var i=0;i<this._fields.length;i++) {
		if(this._fields[i].get_name() == key) {
			return true;
		}
	}
	return false;
}

AjaxComponent.prototype.get_field_value = function(key) {
	for(var i in this._fields) {
		if(this._fields[i].get_name() == key) {
			return this._fields[i].get_value();
		}
	}
	return "_AC_error_InvalidKey_";
}

AjaxComponent.prototype.build_tree = function() {
	var dbg = document.getElementById("dbg");
	var elems = this.dump_field_elements();
	for(var p in elems) {
		this._fields.push(
			new AjaxComponent_Field(
				elems[p].name,
				elems[p].id,
				elems[p].value,
				false
			)
		);
		//dbg.innerHTML += elems[p].id + "<Br/>";
	}
	
}


// return associative array of all fields
// [field-type] = array(all,elements,of,field-type)
AjaxComponent.prototype.dump_field_elements = function() {
	var fieldDump = new Array();
	for(var i in this._fields) {
		if(!this._fields[i]._object) { continue; }
		var container_type = this._fields[i]._object.nodeName;
		container_type = container_type.toLowerCase();
		switch(container_type) {
			/*case "input":
			case "select":
			case "file":
				break;*/
			/*case "div":
			case "table":
			case "span":
			case "form":*/
			default:
				var elems = this._fields[i]._object.getElementsByTagName("input");
				for(var p in elems) { fieldDump.push(elems[p]); }
				
				elems = this._fields[i]._object.getElementsByTagName("select");
				for(var p in elems) { fieldDump.push(elems[p]); }
				
				elems = this._fields[i]._object.getElementsByTagName("textarea");
				for(var p in elems) { fieldDump.push(elems[p]); }
				
				elems = this._fields[i]._object.getElementsByTagName("div");
				for(var p in elems) { fieldDump.push(elems[p]); }
			;
		}
	}
	return fieldDump;
}



AjaxComponent.prototype.set_field_value = function(key,value) {
	for(var i in this._fields) {
		if(typeof(_dbg_output) != "undefined") {
			//_dbg_output.innerHTML += "<span style='color:#009900;'>Looking up " + key + "<br/>";
		}
		if(this._fields[i].get_name() == key) {
			if(typeof(_dbg_output) != "undefined") {
				_dbg_output.innerHTML += "<span style='color:#000099;'>OK: requested " + key + " from " + this._name + "</span><br/>";
			}
			return this._fields[i].set_value(value);
		}
	}
	if(typeof(_dbg_output) != "undefined") {
		_dbg_output.innerHTML += "ERROR: requested " + key + " from " + this._name + "<br/>";
	}
	return "_AC_error_InvalidKey_";
}


AjaxComponent.prototype.append_field_value = function(key,value) {
	for(var i in this._fields) {
		if(this._fields[i].get_name() == key) {
			this._fields[i].set_value(this._fields[i].get_value() + value);
		}
	}
	return "_AC_error_InvalidKey_";
}


AjaxComponent.prototype.reset_fields = function() {
	var fieldDump = this.dump_field_elements();
	for(var i in fieldDump) {
		fieldDump[i].value = "";
	}
}

AjaxComponent.prototype.show_field = function(key) {
	for(var i in this._fields) {
		if(this._fields[i].get_name() == key) {
			this._fields[i]._object.style.display = "block";
		}
	}
}


//////////////////////////////////////////////////////////////////





// array of AjaxComponents
AjaxComponentGroup.prototype._children;
AjaxComponentGroup.prototype._currentobj;
AjaxComponentGroup.prototype._name;
AjaxComponentGroup.prototype._def_obj_index;
AjaxComponentGroup.prototype._on_complete;

function AjaxComponentGroup(groupName,component_ar) {
	this._def_obj_index = -1;
	this._children = component_ar;
	this._name = groupName;
	for(var i in this._children) {
		this._children[i]._group = this;
	}
	this._currentobj = -1;
}

AjaxComponentGroup.prototype.get_component = function(key) {
	for(var i in this._children) {
		if(this._children[i].get_name() == key) {
			return this._children[i];
		}
	}
	return 0;
}


AjaxComponentGroup.prototype.fire_next = function() {
	this._currentobj++;
	if(this._currentobj < this._children.length) {
		var showState = false;
		if(this._currentobj == this._def_obj_index) {
			showState = true;
		}
		this._children[this._currentobj].display(showState);
	} else {
		if(this._on_complete) {
			this._on_complete();
		}
	}
}


AjaxComponentGroup.prototype.hide_all = function() {
	for(var i in this._children) {
		this._children[i].hide();
	}
}

AjaxComponentGroup.prototype.show_component = function(name) {
	var comp = this.get_component(name);
	if(comp) {
		this.hide_all();
		comp.show();		
	}
}

AjaxComponentGroup.prototype.set_default = function(index) {
	this._def_obj_index = index;
}


AjaxComponentGroup.prototype.collect_component_markup = function() {
	for(var i in this._children) {
		this._children[i].collect_markup();
	}
}



//////////////////////////////////////////////////////////////////
function serialize( inp ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Arpad Ray (mailto:arpad@php.net)
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
 
    var getType = function( inp ) {
        var type = typeof inp, match;
        if(type == 'object' && !inp)
        {
            return 'null';
        }
        if (type == "object") {
            if(!inp.constructor)
            {
                return 'object';
            }
            var cons = inp.constructor.toString();
            if (match = cons.match(/(\w+)\(/)) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
 
    var type = getType(inp);
    var val;
    switch (type) {
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (inp ? "1" : "0");
            break;
        case "number":
            val = (Math.round(inp) == inp ? "i" : "d") + ":" + inp;
            break;
        case "string":
            val = "s:" + inp.length + ":\"" + inp + "\"";
            break;
        case "array":
            val = "a";
        case "object":
            if (type == "object") {
                var objname = inp.constructor.toString().match(/(\w+)\(\)/);
                if (objname == undefined) {
                    return;
                }
                objname[1] = serialize(objname[1]);
                val = "O" + objname[1].substring(1, objname[1].length - 1);
            }
            var count = 0;
            var vals = "";
            var okey;
            for (key in inp) {
                okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key);
                vals += serialize(okey) +
                        serialize(inp[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") val += ";";
    return val;
}
