/*------  Engine v2  ------*/
/*        by T. Neijman    */
/* ------------------------*/

/*------ TpsLocation (PRIVATE OBJECT) ------*/

/* CONSTRUCTOR */
function TpsLocation(pHref) {this.initLocation(pHref);}

/* INTERFACE TpsLocation */
TpsLocation.prototype.initLocation=TpsLocation_initialize;
TpsLocation.prototype.getLocation=TpsLocation_getLocation;
TpsLocation.prototype.setLocation=TpsLocation_setLocation;
TpsLocation.prototype.getBase=TpsLocation_getBase;
TpsLocation.prototype.getPath=TpsLocation_getPath;
TpsLocation.prototype.getFilename=TpsLocation_getFilename;
TpsLocation.prototype.getExtension=TpsLocation_getExtension;
TpsLocation.prototype.getSearch=TpsLocation_getSearch;
TpsLocation.prototype.getAnchor=TpsLocation_getAnchor;
TpsLocation.prototype.getRedirect=TpsLocation_getRedirect;
TpsLocation.prototype.getLevelPath=TpsLocation_getLevelPath;
TpsLocation.prototype.getRoot=TpsLocation_getRoot;

function TpsLocation_initialize(pHref) {
	if (pHref) {
		pHref = pHref.replace(/\\/gi, "/");
		this.value=pHref;
		this.base=this.getBase(pHref);
		this.root=this.getRoot(pHref);
		this.path=this.getPath(pHref);
		this.filename=this.getFilename(pHref);
		this.extension=this.getExtension(pHref);
		this.file=this.filename+"."+this.extension;
		this.search=this.getSearch(pHref);
		this.anchor=this.getAnchor(pHref);
		this.redirect="";
	}
}

function TpsLocation_getLocation() {
	var sRetval

	sRetval = this.value;
	return sRetval;
}

function TpsLocation_getRoot(pHref) {
	var theWebDef= new TpsWebDef(pHref)
	var theRoot = theWebDef.homePath;
	return theRoot;
}

function TpsLocation_setLocation(pHref) {
	this.init(pHref);
}

function TpsLocation_getBase(pHref) {
	var sRetval;
	var srch=pHref.indexOf("?");
	var hash=pHref.indexOf("#");

	if (hash>0) {
		srch=hash;
	}
	else if (srch<1) {
		srch=pHref.length;
	}
	sRetval = pHref.substring(0,srch);

	return sRetval
}

function TpsLocation_getPath(pHref) {
	var sRetval;
	var theHrefTbl=this.getBase(pHref).split("/");

	theHrefTbl.length=theHrefTbl.length-1;
	sRetval = theHrefTbl.join("/")
	return sRetval;
}

function TpsLocation_getFilename(pHref) {
	var sRetval;
	var theHrefTbl=this.getBase(pHref).split("/");

	sRetval = theHrefTbl[theHrefTbl.length-1].substring(0,theHrefTbl[theHrefTbl.length-1].indexOf("."));

	return sRetval;
}

function TpsLocation_getExtension(pHref) {
	var sRetval;
	var theHrefTbl=this.getBase(pHref).split("/"); var theFileNm=theHrefTbl[theHrefTbl.length-1];

	sRetval = theFileNm.substring((theFileNm.indexOf(".")+1),theFileNm.length);
	return sRetval
}

function TpsLocation_getSearch(pHref) {
	var theSrch=""; var srch=pHref.indexOf("?");
	if (srch>0) {theSrch=pHref.substring((srch+1),pHref.length);}
	return theSrch;
}

function TpsLocation_getAnchor(pHref) {
	var theAnch=""; var srch=pHref.indexOf("?"); var hash=pHref.indexOf("#");
	if (srch<1) {srch=pHref.length;}
	if ((hash>0)&&(pHref.substring((hash+1),1)!="@")) {theAnch=pHref.substring((hash+1),srch);}
	return theAnch;
}

function TpsLocation_getRedirect(pHref) {
	var theRedir=""; var hash=pHref.indexOf("#@");
	if (hash>0) {theRedir=pHref.substring((hash+2),pHref.length);}
	return theRedir;
}

function TpsLocation_getLevelPath(sURL, theLevel) {
	sURL = sURL.replace(/\\/gi, "/");
	var theRoot = this.root.split("/");
	var thePath = this.getPath(sURL);
	var thePath = thePath.split("/");


	var theRootLength = theRoot.length;
	var thePathLength = thePath.length;
	if ((thePathLength - theRootLength) >= theLevel) {
		thePath.length = (theRoot.length + theLevel);
	}

 	thePath=thePath.join("/");
	return thePath;
}

function TpsgetNode(pNode,pId,pTag) {
	if (pId) {

		if (pNode[pId]) {return pNode[pId];}
		if (pNode[pTag]) {var theBranch=pNode[pTag];}
		else if (pNode.document[pTag]) {var theBranch=pNode.document[pTag];}
		if (theBranch) {
			for (var i=0; i<theBranch.length; i++) {
				var theResult = TpsgetNode(theBranch[i],pId,pTag);
				if (theResult) {return theResult;}
	}	}	}
	return null;
}

/*------  TpsWebsite (GLOBAL OBJECT) ------*/

/* CONSTRUCTOR */
function TpsWebSite(sName) {this.initWebSite(sName);}

/* INTERFACE TpsWebsite */
TpsWebSite.prototype.initWebSite=TpsWebSite_initialize;
TpsWebSite.prototype.signOn=TpsWebSite_signOn;
TpsWebSite.prototype.synchronize=TpsWebsite_synchronize;

function TpsWebSite_initialize(sName) {
   this.value = sName;
	this.location = new TpsLocation(top.window.document.URL);
	this.frameSet = new TpsFrameSet();
	this.history = (this.location.path+gsTpsStartPage);
}

function TpsWebSite_signOn(sLocation) {
	this.frameSet.synchronize(sLocation);
	this.history = sLocation;
}

function TpsWebsite_synchronize() {
	var theUrl = top.window.document.URL;
	
	theUrl = theUrl.replace(/\\/gi, "/");
	var theRedirectUrl = this.location.getRedirect(theUrl);
	if (theRedirectUrl) {
		this.frameSet.synchronize(theRedirectUrl);
		this.history = theRedirectUrl;
	}
	else {
		this.frameSet.synchronize(this.history);
	}
}

/*------  TpsFrameSet - (PRIVATE OBJECT)  ------*/

/* CONSTRUCTOR */
function TpsFrameSet() {this.initFrameSet();}

/* INTERFACE  TpsFrameSet */
TpsFrameSet.prototype.initFrameSet=TpsFrameSet_initialize;
TpsFrameSet.prototype.synchronize=TpsFrameSet_synchronize;

function TpsFrameSet_initialize() {
	this.location = new TpsLocation(top.window.document.URL);
	this.frames = new Array ();
       	this.frames["banner"] = new TpsFrame("banner", "banner.html", 1);
        this.frames["nnoutput"] = new TpsFrame("nnoutput", "nvt", -1);
        this.frames["menu"] = new TpsFrame("menu", "menu.html", 1);
}

function TpsFrameSet_synchronize(pHref) {
	for (var i in this.frames) {
		if (this.frames[i].synchronize) {
			this.frames[i].synchronize(pHref);
		}
	}
}

/*------  TpsFrame (PRIVATE OBJECT) ------*/

/* CONSTRUCTOR */
function TpsFrame(sName, sFile, iLevel) {this.initFrame(sName, sFile, iLevel);}

/* INTERFACE TpsFrame */
TpsFrame.prototype.initFrame=TpsFrame_initialize;
TpsFrame.prototype.synchronize=TpsFrame_sync;

function TpsFrame_initialize(sName, sFile, iLevel) {
	this.value=sName;
	this.file=sFile;
	this.location = new TpsLocation("t/p/s");
	this.level=iLevel;
}

function TpsFrame_sync(sURL) {
	var theGoalObj = TpsgetNode(top.window,this.value,"frames");
	if (theGoalObj) {
		if (this.level >  0) {
			this.location.initLocation(sURL);
			var thePath = this.location.getLevelPath(sURL, this.level);
			sURL = (thePath + "/" + this.file);
		}
		if (theGoalObj.document.URL != sURL) {
			theGoalObj.document.location.replace(sURL);
		}
	}
}
