var currentPaneStyle = 0;
var currentTab = 0;
var tabstripArray = 0;

function RegisterTabStrip(tabstripName)
{
	if (tabstripArray == 0) 
	{
		tabstripArray = new Array();
		tabstripArray[0] = tabstripName;
		currentTab = new Array();
		currentTab[0] = 0;
		currentPaneStyle = new Array();
		currentPaneStyle[0] = 0;
	}
	else
	{
		for (var i=0; i<tabstripArray.length; i++)
		{
			if (tabstripArray[i] == tabstripName) return;
		}
		tabstripArray[tabstripArray.length] = tabstripName;
		currentTab[currentTab.length] = 0;
		currentPaneStyle[currentPaneStyle.length] = 0;
	}	
}

function GetTabStripIndex(tabstripName)
{
	for (var i=0; i<tabstripArray.length; i++)
	{
		if (tabstripArray[i] == tabstripName) return i;
	}
	return -1;
}

function setCurrentTab(tabstripName, tab)
{
	currentTab[GetTabStripIndex(tabstripName)] = tab;
}

function getCurrentTab(tabstripName)
{
	return currentTab[GetTabStripIndex(tabstripName)];
}
function setCurrentPaneStyle(tabstripName, style)
{
	currentPaneStyle[GetTabStripIndex(tabstripName)] = style;
}

function getCurrentPaneStyle(tabstripName)
{
	return currentPaneStyle[GetTabStripIndex(tabstripName)];
}
function writeTabstrip(view, tabstripName, divElementName, command)
{
	RegisterTabStrip(tabstripName);
		
	var divElement = document.getElementById(divElementName);
	
	var showLastTab;// = command.getAttribute("RememberSelectedTab");
	
	var tabs = command.getElementsByTagName("Tab");
	var innerHtml = "";
	var tag = "";
	
	innerHtml += "<table border='0' cellspacing='0' cellpadding='0'><tr><td><table border='0' cellspacing='0' cellpadding='0'><tr>";
	
	for(var i = 0; i < tabs.length; i++)
	{
		var tab = tabs[i];
		var tabTabStripName = tab.parentNode.getAttribute('TabstripName');
		if (showLastTab == null) showLastTab = tab.parentNode.getAttribute('RememberSelectedTab');
		if (tabTabStripName == tabstripName)
		{
			var controlId = "";
			eval('controlId = ' + view + 'Controls.' + tab.getAttribute("PanelName") + ';');
				
//			tag = "<td class='tabs' id='[TAB]'><div class='tabs' onclick='showPane(\"[TABSTRIPNAME]\", \"[TAB]\", \"[PANEL]\")'>&nbsp;[TITLE]<IMG id='[IMG]' alt='' src='../images/tabright.gif'></div></td>";
			tag = "<td class='tabs' id='[TAB]'><div class='tabs' onclick='showPane(\"[TABSTRIPNAME]\", \"[TAB]\", \"[PANEL]\")'><span class='tabs'>&nbsp;[TITLE]</span><IMG id='[IMG]' alt='' src='../images/tabright.gif'></div></td>";
			
			tag = tag.replace(/\[TABSTRIPNAME\]/g, tabstripName);
			tag = tag.replace(/\[PANEL\]/g, controlId);
			tag = tag.replace(/\[TAB\]/g, controlId + "_tab");
			tag = tag.replace(/\[IMG\]/g, controlId + "_img");
			tag = tag.replace(/\[TITLE\]/g, tab.getAttribute("Title"));
			
			//alert(controlId + "_img");
			el = document.getElementById(controlId);
			try
			{
				el.style.display = "none";
			}
			catch(e)
			{
			}
			innerHtml += tag;
		}
	}
	innerHtml += "</tr></table></td></tr></table>";
	
	divElement.innerHTML = innerHtml;
	
	var controlId = "";
	eval('controlId = ' + view + 'Controls.' + tabs[0].getAttribute("PanelName") + ';');
	
	if (showLastTab == 'True')
	{ 
		var lastTab = getCookie(tabstripName); 
		//alert('Get cookie value for ' + tabstripName + ', last tab is: ' + lastTab);
		if (lastTab != null)
		{
			showPane(tabstripName, lastTab + "_tab", lastTab);
		}
		else
		{
			showPane(tabstripName, controlId + "_tab", controlId);
		}
	}
	else
	{
		showPane(tabstripName, controlId + "_tab", controlId);
	}
	//initiate();
	
}

function tabstrip()
{
   this.tabs = new Array();
   this.add = addTab;
   this.write = writeTabstrip;
}

function tab(caption,content)
{
  this.setId = setId;
  this.caption = caption;
  this.content = content;
  this.write = writeTab;
  this.writeContent = writePane;
}



function addTab(tab)
{
  tab.setId("tab" + this.tabs.length);
  this.tabs[this.tabs.length] = tab;
}

function setId(id)
{
  this.id = id;
}


function showPane(tabstripName, tabName, divName)
{
	// remember selected pane
	//deleteCookie(tabstripName);
	
	var expires = addDays(new Date(), 1);
	//alert('Set tab ' + tabName + ' as selected untill ' + expires);
	setCookie(tabstripName, divName);
	
		
	var div = document.getElementById(divName);
	var tab = document.getElementById(tabName);
	var crTab = getCurrentTab(tabstripName);
	if(crTab != 0)
	{
		crTab.className ="tabs";
		setTabImage(crTab, "../images/tabright.gif");
	}
	tab.className = "selectedTab";
	setCurrentTab(tabstripName, tab);

	var crPaneStyle = getCurrentPaneStyle(tabstripName);
	if(crPaneStyle != 0) crPaneStyle.display = "none";
	
	var objPaneStyle = document.getElementById(divName).style;
	objPaneStyle.display = "block";
	setCurrentPaneStyle(tabstripName, objPaneStyle);
	
	// change image
	setTabImage(tab, "../images/seltabright.gif");
}

function setTabImage(tab, src)
{
	var img = document.getElementById(tab.id.replace(/\_tab/g, "_img"));
	if (img!=null) img.src = src;
}

function SubmitForm()
{
   window.alert("Form submitted.  This would normally take you to another page");
   // normally, you would here check the form and submit it.
   // if the form has the name 'tabform', then it is submitted
   // with tabform.submit();
}

function writePane()
{
  document.write("<div class='pane' id='pn_" + this.id + "'>" + this.content + "</div>");
}

function writeTab()
{
   document.write("<td class='tabs'><div class='tabs' id='" + this.id + "' onclick='showPane(\'" + this.id + "\', this)'>" + this.caption + "</div></td>");
}

 // Sets a Cookie with the given name and value.
 //
 // name       Name of the cookie
 // value      Value of the cookie
 // [expires]  Expiration date of the cookie (default: end of current session)
 // [path]     Path where the cookie is valid (default: path of calling document)
 // [domain]   Domain where the cookie is valid
 //              (default: domain of calling document)
 // [secure]   Boolean value indicating if the cookie transmission requires a
 //              secure transmission
function setCookie(name, value, expires, path, domain, secure) { //alert('cookie: ' + name + ': ' + value);
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

 // Gets the value of the specified cookie.
 //
 // name  Name of the desired cookie.
 //
 // Returns a string containing value of specified cookie,
 //   or null if cookie does not exist.
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

 // Deletes the specified cookie.
 //
 // name      name of the cookie
 // [path]    path of the cookie (must be same as path used to create cookie)
 // [domain]  domain of the cookie (must be same as domain used to create cookie)
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


function addDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}


