
function PostbackControl (window)
	{
	this.window = window ;
	this.action = '' ;
	this.history = new Array () ;
	this.history_index = 0 ;
	}

PostbackControl.prototype.MAX_HISTORY	= 10 ;

PostbackControl.prototype.updateHistory = function ()
	{
	if (this.history_index == this.history.length)
		{
		if ((this.history_index == 0) || (this.history[this.history_index - 1] != this.window.location.href))
			{
			this.history[this.history_index++] = this.window.location.href ;
			if (this.history.length > this.MAX_HISTORY)
				{
				this.history.splice (0, 1) ;
				this.history_index-- ;
				}
			}
		}
	}

PostbackControl.prototype.go = function (displacement)
	{
	var	index ;

	index = Math.max (Math.min (this.history_index + displacement, this.history.length), 1) ;
	if (index != this.history_index)
		{
		this.history_index = index ;
		this.submit ('', this.history[index - 1]) ;
		}
	}

PostbackControl.prototype.back = function ()
	{
	var	displacement ;

	if (arguments.length > 0)
		displacement = 0 - arguments[0] ;
	else
		displacement = -1 ;

	this.go (displacement) ;
	}

PostbackControl.prototype.forward = function ()
	{
	var	displacement ;

	if (arguments.length > 0)
		displacement = arguments[0] ;
	else
		displacement = 1 ;

	this.go (displacement) ;
	}

PostbackControl.prototype.trapLinks = function (){
	var	i ;
	var	link ;

	for (i = 0 ; i != this.window.document.links.length ; i++) {
		link = this.window.document.links[i] ;
		if (!(link.onclick || link.target)) {
			if ((!/#/.test (link.href)) || (link.href.replace (/#.*$/, '') != this.window.location.href)) {
				link.postback = this ;
				link.onclick = PostbackControl_onLink ;
			}
		}
	}
}

function PostbackControl_onLink ()
	{
	this.postback.submit ('', this.href) ;
	return false ;
	}

PostbackControl.prototype.writeValueToForm = function (form, name, value)
	{
	var	input ;
	var	i ;

	if ((typeof value) != 'function')
		{
		if ((typeof value) == 'object')
			{
			for (i in value)
				this.writeValueToForm (form, name + '[' + i + ']', value[i]) ;
			}
		else
			{
			input = this.window.document.createElement ('INPUT') ;
			input.setAttribute ('type', 'hidden') ;
			input.setAttribute ('name', 'postback_' + name) ;
			input.setAttribute ('value', value) ;
			form.appendChild (input) ;
			}
		}
	}

PostbackControl.prototype.postbackForm = function (form)
	{
	if (this.window.windowControl)
		this.window.windowControl.notifyWindowPostback (this.action) ;

	for (name in this)
		if (!name.match (/(?:^_)|(?:^(?:window|MAX_HISTORY)$)/))
			this.writeValueToForm (form, name, this[name]) ;

	form.submit () ;
	}

PostbackControl.prototype.submit = function (action)
	{
	this.action = action ;
	form = this.window.document.createElement ('FORM') ;
	if (arguments.length > 1)
		form.setAttribute ('action', arguments[1]) ;
	form.setAttribute ('name', 'postback') ;
	form.setAttribute ('method', 'post') ;
	this.window.document.body.appendChild (form) ;
	this.postbackForm (form) ;
	}

