var tabPrevious = -1;
var tabCurrent  = -1;
var tabLastID   = 0;
var currentFrame = null;
var divTabButtons = null;
var divTabFrame   = null;
var divTabState   = null;

function findObj(theObj, theDoc)
{
	var p, i, foundObj;

	if(!theDoc) theDoc = document;
	if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
	{
		theDoc = parent.frames[theObj.substring(p+1)].document;
		theObj = theObj.substring(0,p);
	}
	if (!(foundObj = theDoc[theObj]) && theDoc.all)
		foundObj = theDoc.all[theObj];
	for (i=0; !foundObj && i < theDoc.forms.length; i++) 
		foundObj = theDoc.forms[i][theObj];
		
	for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
		foundObj = findObj(theObj,theDoc.layers[i].document);
		
	if(!foundObj && document.getElementById)
		foundObj = document.getElementById(theObj);

	return foundObj;
}

function indexOf(objList, objItem)
{
	if (objList.length == 0)
		return -1;
		
	for (var i=0; i < objList.length; i++)
		if (objList[i] == objItem) return i;

	// Not found...
	return -1;
}

function findChild(obj, id)
{
	if (!obj || !id)
		return;
		
	for (var i=0; i < obj.childNodes.length; i++){
		if (obj.childNodes[i].id == id) 
			return obj.childNodes[i];
	}
	
	return null;
}

function findChildRecursive(obj, id)
{
	var ret = null;
	if (!obj || !id)
		return;
		
	for (var i=0; i < obj.childNodes.length; i++){
		if (obj.childNodes[i].id == id) 
			ret = obj.childNodes[i];
		else 
			ret = findChildRecursive(obj.childNodes[i], id)
		
		if (ret != null) break;
	}
	
	return ret;
}

function rehashItems()
{
	if (!divTabButtons) divTabButtons = findChildRecursive(window.top.frames['content'].document, 'divTabButtons');
	if (!divTabFrame)   divTabFrame   = findChildRecursive(window.top.frames['content'].document, 'divTabFrame');
	if (!divTabState)   divTabState	  = findChildRecursive(window.top.frames['content'].document, 'divTabState');
}

function getState()
{
	//debugger;
	var s1 = unescape("%01");
	//var s2 = unescape("%02");
	if (divTabState.innerHTML == "")
	{
		tabCurrent = -1;
		tabPrevious = -1;
		tabLastID = 0;
		return;
	}
	
	var tabState = divTabState.innerHTML.split(s1, 3);
	//var tabStateIdx = tabState[0].split(s2);
	
	tabPrevious = Number(tabState[0]);
	tabCurrent  = Number(tabState[1]);
	tabLastID	= Number(tabState[2]);
//	tabList 	= tabState[1].split(s2);
}

function saveState()
{
	var s1 = unescape("%01");
	//var s2 = unescape("%02");
	
	divTabState.innerHTML = tabPrevious + s1 + tabCurrent + s1 + tabLastID;
	//divTabState.innerHTML = tabPrevious + "#" + tabCurrent;
}

function tabSetStyle(id, enabled)
{
	rehashItems();
	
	var objIFrame, objTab, objClose, objLeft, objRight;
	
	objIFrame = findChildRecursive(divTabFrame, "IFrame"+id);
	objTab = findChildRecursive(divTabButtons, "tab"+id);
	objClose = findChildRecursive(divTabButtons, "close"+id);
	objLeft = findChildRecursive(divTabButtons, "imgLeft"+id);
	objRight = findChildRecursive(divTabButtons, "imgRight"+id);
		
	if (objIFrame)	objIFrame.style.display = enabled?'inline':'none';
	if (objClose)	objClose.style.display  = enabled?'inline':'none';
	if (objTab)		objTab.className = enabled?'tabOn':'tabOff';
	if (objLeft)	objLeft.src  = approot+'icons/content/LeftTab'+(enabled?'Selected':'')+'.gif';
	if (objRight)	objRight.src = approot+'icons/content/RightTab'+(enabled?'Selected':'')+'.gif';
	
//alert(objIFrame);
	
	if (enabled)
		window.top.document.title = (objTab)?objTab.value:apptitle;
		//currentFrame = objIFrame;
		
		// document.body.focus() is implemented only under ie
		//if (document.body && document.body.focus)
		//	document.body.focus();
//	}
}

function tabAdd(name, url)
{
	rehashItems();
	getState();
	
	if (divTabFrame.childNodes.length == 0) tabLastID = 0;
	
	tabLastID++;
	tabPrevious = ((divTabFrame.childNodes.length < 1) ? -1 : tabCurrent);
	tabCurrent = tabLastID; //divTabFrame.childNodes.length;
	if(name.indexOf('%22')>0)
	{
		name= name.replace(/%22/gi,"&quot;");

	}
	
	var htmlTabText=
		'<span id="span'+tabCurrent+'">'+
			'<img id="imgLeft'+tabCurrent+'" src="'+approot+'icons/content/LeftTabSelected.gif" class="imgShift" />'+
			'<input type="button" id="tab'  +tabCurrent+'" class="tabOn" value="'+unescape(name)+'" onClick="tabOnClick(this.id);" />'+
			'<input type="button" id="close'+tabCurrent+'" class="closeTab" onClick="tabClose(this.id);" onMouseOver="this.className=\'closeTabOver\';" onMouseOut="this.className=\'closeTab\';" />'+
			'<img id="imgRight'+tabCurrent+'" src="'+approot+'icons/content/RightTabSelected.gif" class="imgShift" />'+
		'&nbsp;</span>';
		
	var htmlFrameText = '<IFrame id="IFrame'+tabCurrent+'" src="'+url+'" height="100%" frameborder="1" class="tabFrame"></IFrame>';
	
	// Add the new item index to the array
	//tabList.splice(tabList.length, 0, tabCurrent);
	
	//debugger;
	divTabButtons.innerHTML += htmlTabText;
	if (divTabFrame.insertAdjacentHTML) {
		divTabFrame.insertAdjacentHTML("beforeEnd", htmlFrameText); //.innerHTML += htmlFrameText;
	} else {
		var r = divTabFrame.ownerDocument.createRange();
		r.setStartBefore(divTabFrame);
		divTabFrame.appendChild(r.createContextualFragment(htmlFrameText));
	}
	
	// Set the stile of the previous item
	if (tabPrevious > -1)
		tabSetStyle(tabPrevious, false);
	tabSetStyle(tabCurrent, true);
	
//	currentFrame = findChildRecursive(divTabFrame, "IFrame"+tabCurrent);
	
	saveState();
}

function tabOnClick(id)
{
	rehashItems();
	getState();
	
	var idx = Number(id.substring(3, id.length));
	if (tabCurrent != -1 && tabCurrent != idx)
	{
		tabSetStyle(tabCurrent, false);
		tabSetStyle(idx, true);

		tabPrevious = tabCurrent;
		tabCurrent = idx;
	}
	
	saveState();
}

function tabClose(id)
{
//	if (!confirm('Are you sure you want to close this tab?'))
//		return;

	rehashItems();
	getState();
	
	var idx = (id == '-1') ? tabCurrent : Number(id.substring(5, id.length));
	
//	alert('id: '+id+', tabCurrent: '+tabCurrent+', idx:'+idx);
	
	var objSpan   = findChildRecursive(divTabButtons, 'span'+idx);
	var objIFrame = findChildRecursive(divTabFrame, 'IFrame'+idx);
	
	var objConfirmOnClose = null;
	if (objIFrame.contentDocument) {
		objConfirmOnClose = findChildRecursive(objIFrame.contentDocument.forms[0], '__ConfirmOnClose');
	} else {
		objConfirmOnClose = findChildRecursive(window.top.frames['content'].document.frames["IFrame"+tabCurrent].document, '__ConfirmOnClose');
	}
	
	var objDocumentID = null;
	if (objIFrame.contentDocument) {
		objDocumentID = findChildRecursive(objIFrame.contentDocument.forms[0], '__ObjectCode');
	} else {
		objDocumentID = findChildRecursive(window.top.frames['content'].document.frames["IFrame"+tabCurrent].document, '__ObjectCode');
	}
	
	var objCmdSave = null;
	if (objIFrame.contentDocument) {
		objCmdSave = findChildRecursive(objIFrame.contentDocument.forms[0], '__Save');
	} else {
		objCmdSave = findChildRecursive(window.top.frames['content'].document.frames["IFrame"+tabCurrent].document, '__Save');
	}
	
	if (objConfirmOnClose) {
		if (objConfirmOnClose.value == '1') 
		{
			if (confirm('Salvare le modifiche prima della chiusura?')) {
				var xID = objDocumentID.value;
				objCmdSave.click();
				if (xID=='' && objDocumentID.value=='') return;
			}
			//return;
		}
	}
	
	if (objIFrame.nextSibling == null)
		tabLastID--;
	
	if (idx == tabCurrent)
	{
		//User closing a form
		if (tabPrevious != -1) { // if tabPrevious was saved, then use that 
			tabCurrent = tabPrevious;
			tabSetStyle(tabPrevious, true);
		} else if (objSpan.previousSibling != null) { //else if there is an element before, then activate it
			tabCurrent = objSpan.previousSibling.id.substr(4);
			tabSetStyle(tabCurrent, true);
		} else if (objSpan.nextSibling != null) {
			tabCurrent = objSpan.nextSibling.id.substr(4);
			tabSetStyle(tabCurrent, true);
		}
		
		tabPrevious = -1;
		tabSetStyle(idx, false);
	} else {
		//We are closing an invisible item
		tabSetStyle(idx, false);
		tabSetStyle(tabCurrent, true);
		
		if (idx == tabPrevious)
			tabPrevious = -1;
	}
	divTabButtons.removeChild(objSpan);	
	divTabFrame.removeChild(objIFrame);
	
	if (divTabFrame.childNodes.length <= 0){
		window.top.document.title = apptitle;
		tabLastID = 0;
	}
	
/*	var list_index = indexOf(tabList, idx);
	if (list_index != -1)
		tabList.splice(list_index, 1);

	if (tabCurrent  >= 0) tabSetStyle(tabCurrent, true);
	if (tabPrevious >= 0) tabSetStyle(tabPrevious, false);

	tabCurrent = tabPrevious;
	tabPrevious = (tabList.length - 1);
	
	if (tabList.length <= 0) {
		window.top.document.title = apptitle;
		currentFrame = null;
	}*/
	saveState();
}

function tabCloseOnSave(parentid)
{
	debugger;
	rehashItems();
	getState();
	
	var idx = Number(parentid.substring(5, parentid.length));
	var objSpan   = findChildRecursive(divTabButtons, 'span'+tabCurrent);
	var objIFrame = findChildRecursive(divTabFrame, 'IFrame'+tabCurrent);
	
	var strToFill = arguments.length > 1 ? arguments[1] : '';
	
	tabCurrent = (idx > -1 && idx <= tabLastID) ? idx : tabPrevious;
	tabPrevious =  -1;
	
	if (!(tabCurrent > -1 && tabCurrent <= tabLastID))
	{
		if (objSpan.previousSibling != null)
			tabCurrent = objSpan.previousSibling.id.substr(4);
		else if (objSpan.nextSibling != null)
			tabCurrent = objSpan.nextSibling.id.substr(4);
	}
	
	tabSetStyle(tabCurrent, true);
	
	if (objIFrame.nextSibling == null)
		tabLastID--;
	
	var objToClick = null; var getOneStr = '__GetOne';
	var objIForm = findChildRecursive(divTabFrame, parentid);
	
	if (objIForm.contentDocument) {
		objToClick = findChildRecursive(objIForm.contentDocument.forms[0], getOneStr);
	} else if (objIForm.contentWindow) {
		objToClick = findChildRecursive(objIForm.contentWindow.document, getOneStr);
	} else {
		objToClick = findChildRecursive(window.top.frames['content'].document.frames[parentid].document, getOneStr);
		if (objToClick == null)
			objToClick = findChildRecursive(objIForm, getOneStr);
	}
	
	var objToFill = null; 
	var objToFillPrev = null; 
	var objToFillClick = null; 
	
	if (objIForm.contentDocument) {
		objToFill = findChildRecursive(objIForm.contentDocument.forms[0], strToFill + 'thisCode');
		objToFillPrev = findChildRecursive(objIForm.contentDocument.forms[0], strToFill + 'PrevText');
		objToFillClick = findChildRecursive(objIForm.contentDocument.forms[0], strToFill + 'SubmitAfterFill');
	} else if (objIForm.contentWindow) {
		objToFill = findChildRecursive(objIForm.contentWindow.document, strToFill + 'thisCode');
		objToFillPrev = findChildRecursive(objIForm.contentWindow.document, strToFill + 'PrevText');
		objToFillClick = findChildRecursive(objIForm.contentWindow.document, strToFill + 'SubmitAfterFill');
	} else {
		objToFill = findChildRecursive(window.top.frames['content'].document.frames[parentid].document, strToFill + 'thisCode');
		objToFillPrev = findChildRecursive(window.top.frames['content'].document.frames[parentid].document, strToFill + 'PrevText');
		objToFillClick = findChildRecursive(window.top.frames['content'].document.frames[parentid].document, strToFill + 'SubmitAfterFill');
		if (objToFill == null) {
			objToFill = findChildRecursive(objIForm, strToFill + 'thisCode');
			objToFillPrev = findChildRecursive(objIForm, strToFill + 'PrevText');
			objToFillClick = findChildRecursive(objIForm, strToFill + 'SubmitAfterFill');
		}
	}
	
	if (objToClick != null)
		objToClick.click();
		
	if (objToFill != null && objToFillClick != null) {
		objToFill.value = arguments[2];
		if (objToFillPrev != null) objToFillPrev.value = '';
		objToFillClick.click();
	}
	
	divTabButtons.removeChild(objSpan);
	divTabFrame.removeChild(objIFrame);
	
	if (divTabFrame.childNodes.length <= 0){
		window.top.document.title = apptitle;
		tabLastID = 0;
	}
	
	saveState();
}

function menuCloseAllTabs()
{
	rehashItems();
	getState();
	
	while (divTabButtons.childNodes.length > 0)
	{
		var id = divTabButtons.lastChild.id.substr(4);
		var frmCloseClick = findChildRecursive(divTabButtons.lastChild, 'close'+id);
		if (frmCloseClick)
			frmCloseClick.click();
	}
}

function menuClickOper(item)
{
	if (item.toLowerCase() == '__logout')
	{
		if (opener && !opener.closed)
			opener.location.href = 'logout.aspx';
		window.close();
		return;
	} else if (item.toLowerCase() == '__closeall') {
		menuCloseAllTabs();
		return;
	}
	
	rehashItems();
	getState();
	var objToClick = null;
	
	if (tabCurrent <= 0)
	    return true;
	
	var objIForm = findChildRecursive(divTabFrame, "IFrame"+tabCurrent);
	
	
	if (objIForm.contentDocument) {
		objToClick = findChildRecursive(objIForm.contentDocument.forms[0], item);
	} else {
		objToClick = findChildRecursive(window.top.frames['content'].document.frames["IFrame"+tabCurrent].document, item);
	}
	var objConfirmOnClose = null;
	if (objIForm.contentDocument) {
		objConfirmOnClose = findChildRecursive(objIForm.contentDocument.forms[0], '__ConfirmOnClose');
	} else {
		objConfirmOnClose = findChildRecursive(window.top.frames['content'].document.frames["IFrame"+tabCurrent].document, '__ConfirmOnClose');
	}
	
	if (objConfirmOnClose)
		objConfirmOnClose.value = '0';
	
	//debugger;
	if(objToClick!=null)
	objToClick.click();
	return true;
}

function tagGetCurrentId()
{
	rehashItems();
	getState();
	return "IFrame" + tabCurrent; //window.top.frames['content'].tabCurrent;
}