/*  TabData.js
 *
 *  Javascript for use with the Block Authorization including Ajax calls 
/*--------------------------------------------------------------------------*/

function StringBuffer() { 
   this.buffer = []; 
}

StringBuffer.prototype.append = function append(string) { 
   this.buffer.push(string); 
   return this; 
}; 

StringBuffer.prototype.toString = function toString() { 
   return this.buffer.join(""); 
};

function encodeRefer(refer) {
   return escape(refer);
}

function showprogress(divName) {

	var html=new StringBuffer();
	var contextPath= "";
	if(window.contextPath) contextPath = window.contextPath;
	html.append("<img src='"+contextPath+"img9/indicator.gif' alt='Processing...' /> Processing...");

	var OUTPUT = document.getElementById(divName);
	OUTPUT.innerHTML = html.toString();
}

function processTabContentXMLResponse(req)
{   //alert( "got here" );
	var xmlDoc = req.responseXML;
	var blockDivTags = xmlDoc.getElementsByTagName("blockDiv");
	var blockDivName = blockDivTags[0].firstChild.nodeValue;
	var blockIdTags = xmlDoc.getElementsByTagName("blockId");
	var blockId = blockIdTags[0].firstChild.nodeValue;
	var blockContentTags = xmlDoc.getElementsByTagName("blockContent");
	var blockContent = blockContentTags[0].firstChild.nodeValue;
	var OUTPUT = document.getElementById(blockDivName);
	
	var html=new StringBuffer();
	html.append(blockContent);
	OUTPUT.innerHTML = html.toString();
	evalScript(blockContent);
} 

function getTabContent(preview, blockId, nodeId, siteId, divName, blockType)
{   
	showprogress(divName);
	var contentDiv = document.getElementById("tab" + blockId);
	var encoding = "UTF-8";
	if(siteId == 10) { 
		encoding = "Shift_JIS";
	} 
	var contextPath= "";
	if(window.contextPath) contextPath = window.contextPath;
	
	var parms = 'preview=' + preview + '&section=M'+ '&blockId=' + blockId + '&blockType=' + blockType + '&nodeId=' + nodeId + '&siteId=' + siteId + '&divName=' + divName + '&encoding=' + encoding + '&pid=0&refer=';
	var myAjax = new Ajax.Request(
		contextPath + 'getTabData.jsp', 
		{
			method: 'get', 
			parameters: parms, 
			onComplete: processTabContentXMLResponse
		});
}

function changeTab(element, htab) {
	var oldCurrent = document.getElementById("currentTab");
	if (oldCurrent) {
		oldCurrent.removeAttribute('id');
		changeTabStyle(oldCurrent, "Selected", "");
	}
	var notFound = true;
	var newTab = element;
	while(notFound) {
		if(newTab.className == "htabDiv"){
			notFound = false;
		} else {
			newTab = newTab.parentNode;
			if(newTab == null) {
				return;
			}
		}
	}
	newTab.setAttribute('id', 'currentTab');
	changeTabStyle(newTab, "", "Selected");
}

function changeTabStyle(tab, oldStyle, newStyle) {
	changeStyle("htab1", oldStyle, newStyle, tab);
	changeStyle("htab2", oldStyle, newStyle, tab);
	changeStyle("htab3", oldStyle, newStyle, tab);
	changeStyle("htabBottom1", oldStyle, newStyle, tab);
	changeStyle("htabBottom2", oldStyle, newStyle, tab);
	changeStyle("htabBottom3", oldStyle, newStyle, tab);
}

function changeStyle(className, oldStyle, newStyle, tab) {
	var elements;
	var oldClassName = className + oldStyle;
	try {
		elements = document.getElementsByClassName(className + oldStyle, tab);
		if(elements) elements[0].className = className + newStyle;
	} catch(e) {
		if(tab) {
			for (i=0; i < tab.childNodes.length; i++) {
				child = tab.childNodes[i];
				if (child.className == oldClassName) {
					child.className = className + newStyle;
					return;
				} else {
					for(var g=0; g < child.childNodes.length; g++) {
						grandchild = child.childNodes[g];
						if(grandchild.className == oldClassName) {
							grandchild.className = className + newStyle;
							return;
						}
					}
				}
			}
		}
	}
	
}

function changeVTab(element, htab, vtab) {
	var oldVCurrent = document.getElementById("currentVTab");
	if(oldVCurrent) {
		oldVCurrent.removeAttribute('id');
	}
	var notFound = true;
	var newTab = element;
	while(notFound) {
		if(newTab.className == "vTabRows"){
			notFound = false;
		} else {
			newTab = newTab.parentNode;
			if(newTab == null) {
				return;
			}
		}
	}
	newTab.setAttribute('id', 'currentVTab');
	
}

/************ www.sybase.ee *************/
function getHTabContent( page, htab, divName, locale)
{   
	showprogress(divName);
	var encoding = "UTF-8";
	var contextPath= "";
	if(window.contextPath) contextPath = window.contextPath;
	
	var parms = 'page=' + page + '&htab=' + htab + '&divName=' + divName + '&locale=' + locale;
	var myAjax = new Ajax.Request(
		contextPath + 'ajaxgethtabdata.jsp', 
		{
			method: 'get', 
			parameters: parms, 
			onComplete: processTabContentXMLResponse
		});
}
function getVTabContent( page, htab, vtab, divName, locale)
{   
	showprogress(divName);
	var encoding = "UTF-8";
	var contextPath= "";
	if(window.contextPath) contextPath = window.contextPath;
	
	var parms = 'page=' + page + '&htab=' + htab + '&vtab=' + vtab + '&divName=' + divName + '&locale=' + locale;
	var myAjax = new Ajax.Request(
		contextPath + 'ajaxgetvtabdata.jsp', 
		{
			method: 'get', 
			parameters: parms, 
			onComplete: processTabContentXMLResponse
		});
}
