///////////////////////////////////////////////////////////////////////////////////
// The constructor for tab panels
///////////////////////////////////////////////////////////////////////////////////
function TabPanel() {
	if (!hasSupport()) return;
	this.pages = [];
	this.selectedIndex = null;
	this.selectedName = '';
}

TabPanel.prototype.addPage = function (labelElement) {
	page = new TabPanelPage(labelElement);
	this.pages[this.pages.length] = page;
}

TabPanel.prototype.setupAllPages = function () {
	var all = document.getElementsByTagName( "*" );
	for ( var i = 0; i < all.length; i++ ) {
		el = all[i]
		cn = el.className;

		if (cn == 'TabLabelSelectedStyle' || cn == 'TabLabelDefaultStyle') {
			
			this.addPage(el);

			if (cn == 'TabLabelSelectedStyle') {
				this.selectedIndex = this.pages.length-1;
				this.selectedName = el.id;
			}
		}
	}
}

TabPanel.prototype.registerPageOnloadAction = function(pageName, actionStr) {
	with (this) {
		for (var i = 0; i < pages.length; i++ ) {
			var page = pages[i];
			if (page.name==pageName) {
				page.onload.push(actionStr);
				break;
			}
		}
	}
}

TabPanel.prototype.registerPageOnhideAction = function(pageName, actionStr) {
	with (this) {
		for (var i = 0; i < pages.length; i++ ) {
			var page = pages[i];
			if (page.name==pageName) {
				page.onhide.push(actionStr);
				break;
			}
		}
	}
}

TabPanel.prototype.tooglePage = function (name) {
	if (this.selectedName != name){
		this.pages[this.selectedIndex].hide();
		for ( var i = 0; i < this.pages.length; i++ ) {
			var page = this.pages[i];
			var curPageName = page.getName();
			if (name == curPageName) {
				page.show();
				this.selectedIndex = i;
				this.selectedName = name;
			}
		}
	}
}

TabPanel.prototype.isSelectedPage = function (name) {
	return (name == this.selectedName)
}

///////////////////////////////////////////////////////////////////////////////////
// The constructor for tab panels
///////////////////////////////////////////////////////////////////////////////////

function TabPanelPage(labelElement){
	this.name = labelElement.id;
	this.labelElement = labelElement;
	this.contentElement = document.getElementById(labelElement.id+'Content');
	this.leftImg = document.getElementById(labelElement.id+'_IL');
	this.middleImg = document.getElementById(labelElement.id+'_T');
	this.rightImg = document.getElementById(labelElement.id+'_IR');
	this.onload = [];
	this.onhide = [];
}

TabPanelPage.prototype.getName = function() {
	return this.name;
}

TabPanelPage.prototype.show = function() {
	this.contentElement.style.display = "block";
	this.labelElement.style.cursor = "default";
	this.labelElement.className = "TabLabelSelectedStyle";
	this.leftImg.src = imgPath+'tabLeftOn.gif';
	this.middleImg.style.backgroundImage = 'url('+imgPath+'tabMiddleOn.gif)';
	this.rightImg.src = imgPath+'tabRightOn.gif';
	for (var i = 0; i < this.onload.length; ++i) {
		eval(this.onload[i]);
	}
}

TabPanelPage.prototype.hide = function() {
	for (var i = 0; i < this.onhide.length; ++i) {
		eval(this.onhide[i]);
	}
	this.contentElement.style.display = "none";
	this.labelElement.style.cursor = "pointer";
	this.labelElement.className = "TabLabelDefaultStyle";
	this.leftImg.src = imgPath+'tabLeftOff.gif';
	this.middleImg.style.backgroundImage = 'url('+imgPath+'tabMiddleOff.gif)';
	this.rightImg.src = imgPath+'tabRightOff.gif';
}


///////////////////////////////////////////////////////////////////////////////////
// Utils
///////////////////////////////////////////////////////////////////////////////////

function hasSupport() {

	if (typeof hasSupport.support != "undefined")
		return hasSupport.support;
	
	var ie55 = /msie 5\.[56789]/i.test( navigator.userAgent );
	
	hasSupport.support = ( typeof document.implementation != "undefined" &&
			document.implementation.hasFeature( "html", "1.0" ) || ie55 )
			
	// IE55 has a serious DOM1 bug... Patch it!
	if ( ie55 ) {
		document._getElementsByTagName = document.getElementsByTagName;
		document.getElementsByTagName = function ( sTagName ) {
			if ( sTagName == "*" )
				return document.all;
			else
				return document._getElementsByTagName( sTagName );
		};
	}

	return hasSupport.support;
}

function tabPanelToggle(object, tabLabel) {	
	if (!hasSupport()) {
		return ;
	}

	if (true == eval(object+'.isSelectedPage(tabLabel.id);')) {
		return ;
	}
	
	validationResult = true;
 	try {
 		validationResult = validateForm();	
 	} catch (e){}

	if (eval(object)!='undefined' && validationResult) {
		eval(object+'.tooglePage(tabLabel.id);');
	}
}

//sb_panel = new TabPanel();
//sb_panel.setupAllPages();
