//////////////////////////////////////////////////
// This script requires /clients/jslib/utils.js //
//////////////////////////////////////////////////

/** 
 * TabControl Control core javascript file.
 *
 * This file includes two objects: Tab and TabControl
 */

var TAB_CONTROL_DEBUG_MODE = false

var TAB_DISPLAY_TOP           = "top"
var TAB_DISPLAY_LEFT          = "left"
var TAB_DISPLAY_RIGHT         = "right"
var TAB_DISPLAY_BOTTOM        = "bottom"

// Keeps track of the number of TabControl objects are created
// This is so that the class can create a unique form name for each dynamically created form object.
var _formsCreatedCounter = 0

function TabControl() 
{
	// Increment the counter
	_formsCreatedCounter++
	
	// set form name for the object
	this.formName = "dynamicTabForm" + _formsCreatedCounter

	this.count            = 0
	
	// The foundation used by the edit buttons
	this.editSearchLocation = "";
	// The frame name that the edits will be sent to.
	this.frameTarget = "";
	
	this.tabTarget = "";
	this.tabAction = "/clients/controls/tab/TabControl.asp";
	
	this.frameTitle  = "Search Results - powered by LexisNexis(R)"

	this.objectName = "tab";
	
	this.autosubmit = false;

	this.firstTab = true;

	
	//////////////////////////////////////////////////////////////////
	// TAB DISPLAY FIELDS
	
	this.showResults = true
	this.tabDisplay  = TAB_DISPLAY_TOP		// Parameter to determine the layout of the tabs in the frameset.
	this.tabHeight   = "24px"		// The dimension of the tab frame in the frameset
	this.css         = ""

	// Delimited fields
	this.labelArray = new Array()
	this.chunnelArray = new Array() 
	
} // constructor

/////////////////////////////////////////////////////////////////////
// Accessors

TabControl.prototype.getFirstTab = function(){
	return this.firstTab;
}

TabControl.prototype.setFirstTab = function(isFirst){
	this.firstTab = isFirst;
}

TabControl.prototype.edit = function(val, target) 
{
	this.editSearchLocation = val
	this.frameTarget = target
}

/**
 * The target (window) for the tab control. 
 */
TabControl.prototype.setTabTarget = function(val) 
{
	this.tabTarget = val
}

TabControl.prototype.setAuth = function(auth){
	this.auth = auth;
}

TabControl.prototype.setFrameTitle = function(val) 
{
	this.frameTitle += " for " + val
} // setFrameTitle()

TabControl.prototype.setFrom = function(val) 
{
	this.from = val
} // setFrom()

TabControl.prototype.setLayout = function(val) 
{
	this.layout = val
} // setLayout()



////////////////////////////////////////////////////////////
// Accessors for Tab Display

TabControl.prototype.setShowResults = function(val) 
{
	if (val) 
	{
		this.showResults = true
	} else 
	{
		this.showResults = false
	}
} // setShowResults()

TabControl.prototype.setTabDisplay = function(val) 
{
	switch (val) 
	{
		case TAB_DISPLAY_TOP:
			this.tabDisplay = val;
			break;
		case TAB_DISPLAY_LEFT:
			this.tabDisplay = val;
			this.setTabHeight(93);
			break;
		case TAB_DISPLAY_RIGHT:
			this.tabDisplay = val;
			this.setTabHeight(93);
			break;
		case TAB_DISPLAY_BOTTOM:
			this.tabDisplay = val;
			break;
	}
} // setTabDisplay()

TabControl.prototype.setTabHeight = function(val) 
{
	if (!(isNaN(val))) 
	{
		this.tabHeight = val;
	}
	
} // setTabHeight()

TabControl.prototype.setCss = function(val) 
{
	this.css = val;
	
} // setCss()

TabControl.prototype.getLabelArrayElement = function(element, split) 
{
	if (!split) {
		split = false;
	}  
	if (split && (this.tabDisplay == TAB_DISPLAY_LEFT) || (this.tabDisplay == TAB_DISPLAY_RIGHT))
	{
		var text = this.labelArray[element];
		
		switch (this.tabDisplay) {
			case TAB_DISPLAY_LEFT:
			case TAB_DISPLAY_RIGHT:
		}
		return text;
	}

	return this.labelArray[element];
} // getLabelArrayElement()

TabControl.prototype.getLabelArray = function() 
{
	return this.labelArray;
} // getLabelArray()

TabControl.prototype.setLabelArray = function(val) 
{
	this.labelArray = val;
} // setLabelArray()

TabControl.prototype.getChunnelArrayElement = function(element) 
{
	return this.chunnelArray[element];
} // getQueryArrayElement()

TabControl.prototype.getChunnelArray = function() 
{
	return this.chunnelArray;
} // getQueryArray()

TabControl.prototype.setChunnelArray = function(val) 
{
	this.chunnelArray = val;
}

TabControl.prototype.addTab = function(label, chunnel) 
{
	if(chunnel.getFirstTab){
		chunnel.setFirstTab(this.count == 0);
		if(this.count == 0)
			this.firstTab = false;
	}

	this.count++
	this.labelArray.push(label)
	this.chunnelArray.push(chunnel)
}

TabControl.prototype.showTabProperties = function() 
{
	// This only works in development
	if (!isDev() || !this.showResults) 
	{
		return true
	}
	
	var popup = this.chunnelArray[0].toString();
	popup = "These are the values for the first tab only.\nClick the other tabs to view their values.\n\n" + popup + "\n\nClick OK to submit this search, or Cancel."
	
	if (!confirm(popup)) 
	{
		return false
	}	
	return true
	
} // showTabProperties()

TabControl.go = function(tab) 
{
	try
	{
		// Create a dynamic form to hold all of the paramaters

		var f = TabControl.getSubmitFormHTML(tab);
	 
		var divName = "tabContolDynaFormDiv";
	 
		var diver = document.getElementById(divName);

		// Only create a new div if the existing div doesn't exist
		if (diver == null)
		{
			diver = document.createElement("div");
			diver.id = divName;
		}
		diver.innerHTML = f;
		document.body.appendChild(diver);

		if (tab.count > 0) 
		{
			document.getElementById(tab.formName).submit();
		}
	}
	catch(e)
	{
		alert(e.message)
	}
	
} // go()

TabControl.getSubmitFormHTML = function(tab)
{
    var f = "<form method='post' action='";
	f += tab.tabAction;
	f += "' id='" + tab.formName + "' name='" + tab.formName + "' target='";
	if (tab.tabTarget != "")
	{
		f += tab.tabTarget;
	}
	f += "'>";
	f += FormUtil.getHiddenFieldVal('tab',        escape(JSON.stringify(tab)));
	f += FormUtil.getHiddenFieldVal('count',      escape(tab.count));
	f += FormUtil.getHiddenFieldVal('css',        escape(tab.css));
	f += FormUtil.getHiddenFieldVal('frameTitle', tab.frameTitle);
	f += FormUtil.getHiddenFieldVal('tabDisplay', escape(tab.tabDisplay));
	f += FormUtil.getHiddenFieldVal('tabHeight',  escape(tab.tabHeight));
	f += "</form>";
	return f;
}

/**
 * Passes one object's values into another. Used to cast JSON objects 
 * into specific classes. 
 */
TabControl.swiss = function(tab, parent)
{
	for (var i in parent) 
	{
        tab[i] = parent[i];
    }
    return tab;
}

