/*CB 26/10/05
	This class is now being used serverside (from VBscript) as well as clientside.
	To enable the values to be set from the VBscript, the class has been changed to
	have an empty constructor and a new 'setValues' method which must be called immediatley
	after the constructor and before any othe methods
*/
//-----------------------------------------------------------------------------------------------------------
/*function Controller(rootNode, name, locationID, displayGroup) {	
	this.name = name;
	this.rootNode = rootNode;
	this.locationID = locationID;
	this.displayGroup = displayGroup;
	this.active = ':' + rootNode.id + ':';
}*/

function Controller()
{
}

function Controller_setValues(rootNode, name, locationID,expandedNodes,categoryID)
{
	this.name = name;
	this.rootNode = rootNode;
	this.locationID = locationID;
	
	this.displayGroup = null;
	this.active = expandedNodes;
	//select the node matching the current search term
	if( typeof( categoryID ) != 'undefined' && categoryID != null && categoryID != "" )
	{
		var aNode = this.rootNode.selectNode( categoryID );
		if(aNode != null)
		{
		    aNode.selected = true;
		}
	}
}

//-----------------------------------------------------------------------------------------------------------
function Controller_getName() {
	return this.name;
}
//-----------------------------------------------------------------------------------------------------------
function Controller_junctionClick(id) {
	if (!document.implementation.hasFeature("HTML","1.0")){
		var query = location.href;
		query = unescape(query);
		
		if (query.indexOf('nodecatID=') != -1) {
			var spos = query.indexOf('nodecatID=');
			var len = query.indexOf('&',spos);
			if (len == -1) {
				query = query.substr(0,spos);		
			} else {
				query = query.substr(0,spos) + query.substr(len + spos)
			}
		}	
	
		if (query.substr(query.length -1) == "/") {
			query += "default.asp?";
		} else if (query.substr(query.length -4) == ".asp") {
			query += "?";
		} else if (query.substr(query.length -1) != "?") {
			query += "&";
		}
	
		query += 'nodecatID=' + id;	
		
		if (this.displayGroup != null) 
			query += "&dg=" + this.displayGroup;
		
		location.href = query;
	} else {
		var aNode = this.rootNode.selectNode( id );
		this.expandCollapse(aNode);
	}
}
//-----------------------------------------------------------------------------------------------------------
function Controller_expandCollapse(aNode) {
	var id = aNode.id;
	var s = ":" + id + ":"
	var regExp = new RegExp(s,"gi");
	if( this.active.search( regExp ) == -1 ) {
		if( this.active == '' )
			this.active += ':';
		this.active += id + ':';
		this.expand(aNode);
	} else {
		this.active = this.active.replace(regExp,":");
		if( this.active == ':' )
			this.active == '';
		this.collapse(aNode);
	}
}
//-----------------------------------------------------------------------------------------------------------
function Controller_nodeClick(id) {
	var query = 'searchResponse.asp?categoryID=' + id;	
	
	if (this.displayGroup != null) 
		query += "&dg=" + this.displayGroup;
	
	location.href = query;
}
//-----------------------------------------------------------------------------------------------------------
function Controller_setRenderer( renderer ) {
	this.renderer = renderer;
}
//-----------------------------------------------------------------------------------------------------------
function Controller_tryCollapse(node) {
	var id = node.id;
	var s = ":" + id + ":"
	var regExp = new RegExp(s,"gi");
	if( this.active.search( regExp ) != -1 ) {
		this.active = this.active.replace(regExp,":");
		if( this.active == ':' )
			this.active == '';
		this.collapse(node);
	}
}
//-----------------------------------------------------------------------------------------------------------
function Controller_tryExpand(node) {
	var id = node.id;
	var s = ":" + id + ":"
	var regExp = new RegExp(s,"gi");
	if( this.active.search( regExp ) == -1 ) {
		if( this.active == '' )
			this.active += ':';
		this.active += id + ':';
		this.expand(node);
	}
}
//-----------------------------------------------------------------------------------------------------------
function Controller_collapse(node){
	if( node.hasChildren() ){
	    node.expanded = false;
		this.renderer.collapse(node);
	}
}
//-----------------------------------------------------------------------------------------------------------
function Controller_expand(node) {
	if( node.parent != null )
	{
	    this.tryExpand(node.parent);	
	}
	if( node.hasChildren() ){
		node.expanded = true;
			this.renderer.expand(node);
	}
}
//-----------------------------------------------------------------------------------------------------------
function Controller_tryMarkExpandedNodes(node)
{
	var id = node.id;
	var s = ":" + id + ":";
	var regExp = new RegExp(s,"gi");
	if( this.active.search( regExp ) == -1 ) {
		if( this.active == '' )
			this.active += ':';
		this.active += id + ':';
		this.markAsExpanded(node);
	}
}

//-----------------------------------------------------------------------------------------------------------
function Controller_markAsExpanded(node)
{
	if( node.parent != null )this.tryMarkExpandedNodes(node.parent);	
	if( node.hasChildren() ){
		node.expanded = true;
	}
}

//-----------------------------------------------------------------------------------------------------------
function Controller_init() {
//		this.renderer.build(this.rootNode, this.locationID);
}

function Controller_buildTree(categoryID,viewedCategoryID) {
	if( typeof( categoryID ) != 'undefined' && categoryID != null && categoryID != "" ) {
		var aNode = this.rootNode.selectNode( categoryID );
		aNode.selected = true;
		//this.tryExpand(aNode);
		this.tryMarkExpandedNodes(aNode);
	}
	if( typeof( viewedCategoryID ) != 'undefined' && viewedCategoryID != null && viewedCategoryID != "" )
	{
		var bNode = this.rootNode.selectNode( viewedCategoryID );
		this.tryMarkExpandedNodes(bNode);
	}
	return this.renderer.build(this.rootNode, null);
}

//-----------------------------------------------------------------------------------------------------------
Controller.prototype = new Object();
Controller.prototype.getName = Controller_getName;
Controller.prototype.junctionClick = Controller_junctionClick;
Controller.prototype.nodeClick = Controller_nodeClick;
Controller.prototype.expand = Controller_expand;
Controller.prototype.expandCollapse = Controller_expandCollapse;
Controller.prototype.collapse = Controller_collapse;
Controller.prototype.setRenderer = Controller_setRenderer;
Controller.prototype.init = Controller_init;
Controller.prototype.tryCollapse = Controller_tryCollapse;
Controller.prototype.buildTree = Controller_buildTree;
Controller.prototype.tryExpand = Controller_tryExpand;
Controller.prototype.setValues = Controller_setValues;
Controller.prototype.tryMarkExpandedNodes = Controller_tryMarkExpandedNodes;
Controller.prototype.markAsExpanded = Controller_markAsExpanded;


