function Wasp_Component( type, id, parent, parkey ) {
	this.type = type;
	this.id = id;
	this.parent = (parent?parent:"");
	this.parentKey = (parkey?parkey:"");
	this.page = wasp_page;
}

Wasp_Component.prototype.addChild = function( childtype, view ) {
	if ( !view ) view = 'Default';
	// creation view for the child type, ourself as the parent;
	this.page.goView( childtype, 0, view, this.type, this.id, null, 'once' );
}

Wasp_Component.prototype.go = function( view, viewValues, crumb ) {
	this.page.goView( this.type, this.id, view, this.parent, this.parentKey, viewValues, crumb );
}

Wasp_Component.prototype.action = function( action, actionValues, callback ) {
	var rand = Math.random();
	var action = "index.php5?rand="+rand+"&component=" + this.type + "&id=" + this.id + "&action=" + action;
	if ( actionValues ) action += "&actionValues=" + actionValues;
	
	var xmlhttp = this.page.window.getHTTPObject();
	xmlhttp.open("GET", action, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			if ( callback ) {
				if ( typeof(callback) == "function" ) {
					callback( xmlhttp.responseText );
				}
				else {
					eval(callback +  "(xmlhttp.responseText);");
				}
			}
		}
	}
	xmlhttp.send(null);
	return false;	
}

Wasp_Component.prototype.publishSet = function( form_obj ) {
	var args, value;
	for (var i=0; i<form_obj.elements.length; i++) { 
		var elem = form_obj.elements(i);
		if (elem.type == 'checkbox') {			
			if ( elem.defaultChecked != elem.checked ) {
				// the checkbox has changed
				value = elem.value;
				args = value.split(":");
				var comp = new Wasp_Component( args[0], args[1] );
				if ( elem.checked ) {
					comp.action( 'Publish', null, null );
				}
				else {
					comp.action( 'Unpublish', null, null );
				}
			}
		}
	}
	this.page.window.reload();
}

Wasp_Component.prototype.responseBack = function( response ) {
	wasp_page.backCrumb();
}

Wasp_Component.prototype.responseReload = function( response ) {

	wasp_page.window.reload();
}

Wasp_Component.prototype.purge = function( doReload ) {
	if ( doReload ) {
		this.action('Delete', null, this.responseReload );
	}
	else {
		// default action
		this.action('Delete', null, this.responseBack );
	}
}

Wasp_Component.prototype.publish = function() {
	this.action('Publish', null, this.responseReload );	
}

Wasp_Component.prototype.unpublish = function() {	
	this.action('Unpublish', null, this.responseReload );
}

Wasp_Component.prototype.edit = function() {
	if ( this.id == 0 ) {
		this.go( 'Default', '', 'once' );
	}
	else {
		this.go( 'Default', '', 'push' );		
	}
}

Wasp_Component.prototype.newChild = function( childType ) {
	var child = new Wasp_Component( childType, 0, this.type, this.id );
	child.edit();
}