function Wasp_PageRequest( win, component ) {
	this.window = win;  // instance of Wasp_Window;
	this.component = component; // instance of Wasp_Component for the current component.
	component.page = this;
	
	this.crumbs = new Array();
	this.viewValues = new Array();
	this.forms = new Array();
	
	this.lastSubmittedKey = 0;	
}

Wasp_PageRequest.prototype.goView = function( comp, id, view, parent, parentKey, viewValues, crumb ) {
	var url = 'index.php5?component=' + comp + '&id=' + id + '&view=' + view;
	if ( parent ) url += '&parent=' + parent;
	if ( parentKey ) url += '&parentKey=' + parentKey;
	if ( viewValues ) url += '&viewValues=' + viewValues;
	if ( crumb ) url += '&crumb=' + crumb;
	this.window.go( url );
}

Wasp_PageRequest.prototype.getViewValue = function( variable ) {
	return this.viewValues[variable];
}

Wasp_PageRequest.prototype.pushCrumb = function( crumb ) {
	this.crumbs.push( crumb );
}

Wasp_PageRequest.prototype.popCrumb = function( crumb ) {
	return this.crumbs.pop();
}

Wasp_PageRequest.prototype.goCrumb = function( index ) {
	var crumb = this.crumbs[index];
	if ( crumb ) {
		crumb.go();
	}
	else {
		this.window.reload();
	}
}

Wasp_PageRequest.prototype.backCrumb = function() {
	this.crumbs.pop();
	var crumb = this.crumbs.pop();
	if ( crumb ) {
		crumb.go();
	}
	else {
		this.window.go("index.php5?");
	}
}

Wasp_PageRequest.prototype.goGridPage = function( pageNum ) {
	var crumb = this.crumbs.pop();
	crumb.viewValues = 'pageNum:'+pageNum;
	crumb.go();	
}

Wasp_PageRequest.prototype.setViewValue = function( variable, val ) {
	this.viewValues[variable] = val;
}

Wasp_PageRequest.prototype.pushForm = function() {	
	this.forms.push( document.forms[document.forms.length-1].id );
}

Wasp_PageRequest.prototype.popForm = function() {
	return this.forms.pop();
}

Wasp_PageRequest.prototype.getNextForm = function() {
	return this.forms[this.forms.length-1];
}

Wasp_PageRequest.prototype.submitAllForms = function( dopop ) {
	if ( dopop ) {	
		this.popForm();
	}
	var wasp_form = new Wasp_Form( this.getNextForm() );	
	if ( wasp_form.isValid() ) {
		// loop through the elements, and only submit if one of them has been modified.
		// the main form should be submitted first
		if ( wasp_form.isModified() ) {
			// a value is different
			this.onSubmitOK = this.submitSuccess;
			this.onSubmitError = this.submitError;
			return wasp_form.submit();				
		}
		//  there was no difference, so go to the next
		this.submitAllForms(1);
	}
	else {
		if ( this.submitComplete ) {
			this.submitComplete()
		}
		else {
			// default behavior is to go back one crumb
			this.backCrumb();
		}
	}
}

Wasp_PageRequest.prototype.submitSuccess = function() {
	this.submitAllForms(1);
}

Wasp_PageRequest.prototype.submitError = function() {
	alert( 'Error: ' + this.lastErrorMessage );
	var form = document.getElementById( this.getNextForm() );
	if ( form && form.parentElement ) {
		form.parentElement.style.background = '#F5E1B8';
	}
}

function Wasp_Crumb( page, comp, id, view, par, parkey, viewvals ) {
	this.page = page;
	this.component = comp;
	this.id = id;
	this.view = view;
	this.parent = par;
	this.parentKey = parkey;
	this.viewValues = viewvals;
	this.page.pushCrumb( this );
}

Wasp_Crumb.prototype.go = function() {
	this.page.goView( this.component, this.id, this.view, this.parent, this.parentKey, this.viewValues, null);
}
