var TAG_REQUEST = "r"
var TAG_LOAD = "load"
var TAG_SAVE = "save"
var TAG_NEXT_LOAD = "nextLoad"
var TAG_ID = "id"
var ATTR_VAL = "value"

var eNavLoad = 1
var eNavSave = 2
var eNavDelete = 3
var eNavSaveAndLoad = 4


function Go(sPage) {
	var oURL = new BBNavRequestJS()
	var sURL = ''
	
	oURL.BeginBuildRequest(eNavLoad, sPage)
	if (Go.arguments.length > 1) {
		for (var arg=0;arg<Go.arguments.length;arg++){
			oURL.AddRecordID(Go.arguments[arg])
		}
	}
	sURL = oURL.EndBuildRequest()

	//window.location.replace(sURL)
	window.location.href = sURL
	
}


function BBNavRequestJS(){

	this.bXfer = false;
	this.bSave = false;
	this.bLoad = false;
	this.bDelete = false;
	this.bNextLoad = false;
	this.lRecIDs = new Array();
	this.sBaseURL = '';
	this.sNextURL = '';
	this.oArgs = null;
	this.bDirty = false;
	this.sAdditionalTags = '';
	this.iRequestType=1
	this.EndBuildRequest = _EndBuildRequest;	
	this.BeginBuildRequest = _BeginBuildRequest;	
	this.AddRecordID = _AddRecordID;	
}

function _BeginBuildRequest(RequestType, baseURL, nextURL) {

	this.sBaseURL = baseURL
	this.sNextURL = nextURL
	this.iRequestType = RequestType
	this.lRecIDs = new Array()
}

function _AddRecordID(id) {
	this.lRecIDs[this.lRecIDs.length] = id
}

function _EndBuildRequest() {

	var sRequest = ''
	
	sRequest = this.sBaseURL + '?r=<r>'

	switch(this.iRequestType){

		case eNavLoad:
			sRequest = sRequest + '<load>'
			for (var i=1;i<this.lRecIDs.length;i++){
				sRequest = sRequest + '<id value="' + this.lRecIDs[i] + '"/>'
			}
			sRequest = sRequest + '</load>'
			this.bLoad = true	
			break;

		case eNavSave:
			sRequest = sRequest + '<save/>' 
			this.bSave = true	
			break;

		case eNavDelete:
			break;
			
		case eNavSaveAndLoad:
			sRequest = sRequest + '<save/><nextLoad>' + this.sNextURL
			for (var i=1;i<this.lRecIDs.length;i++){
				sRequest = sRequest + '<id value="' + this.lRecIDs[i] + '"/>'
			}
			sRequest = sRequest + '</nextLoad>'
			this.bNextLoad = true	
			break;
		
	}		

	
	if (this.oArgs) {
		this.sAdditionalTags += ArgsToTags(this.oArgs)
	}
	
	if (this.sAdditionalTags.length>0) {
		sRequest = 	sRequest + this.sAdditionalTags
	}
	sRequest = 	sRequest + '</r>'
	
	//alert(sRequest)

	return sRequest

}

function ArgsToTags(o) {
	
	var sTags = '<args>'

	for (var i in o) {
		sTags+='<arg name="' + i + '" value="' + o[i] + '"/>'
	}
	sTags+= '</args>'

	return sTags
}
