var gLaunched = 'T'
var cookiesAccepted=String(xmlHttpCallSync("testcookie." + global.suffix)).trim()

function blankIt() {return returnPath(global.path.blank)}

String.prototype.quotapos = function() {return this.replace(/\'/g,"\\'").replace(/\"/g,'\\"')}

function getTimeStamp() {
	var z = new Date()
	return z.getTime()
}
function FTStamp() {
	return '&TStamp='+getTimeStamp()
}
function goNSP(pMode) {
	initialiseShow()
	
	switch (pMode) {
	case 'Init':


		if (global.QueryString.IndexLeft!='T') {
			global.Show.IndexClientLeft		= 'none'
		}
		global.Show.IndexClientLeftSpec	= 'none'
		global.Show.IndexClientTop		= 'none'
		global.Show.IndexClientRight	= 'none'
		global.Show.IndexClientBottom	= 'none'
		global.Show.IndexCenter			= ''
		global.Show.IndexCenterSpec		= 'none'
		global.Show.IndexTop			= 'none'
		global.Show.Work				= 'none'
		alternativePages()
		break;
	case 'OpacWeb':
		global.Show.IndexCenterSpec		= 'none'
		global.Show.IndexCenter			= 'none'
		global.Show.IndexTop			= 'none'
		global.Show.Work				= ''
		break;
	case 'Info':
		global.Show.IndexClientTop		= 'none'
		global.Show.IndexTop			= 'none'
		global.Show.IndexCenter			= 'none'
		global.Show.IndexCenterSpec		= 'none'
		global.Show.Work				= ''
		break;
	case 'Help':
		break;
	default:
		break;
	}
}
function goPaprika(pMode) {
	initialiseShow()
	
	switch (pMode) {
	case 'Init':
		global.Show.IndexClientLeft		= 'none'
		global.Show.IndexClientLeftSpec	= 'none'
		global.Show.IndexClientTop		= 'none'
		global.Show.IndexClientRight	= 'none'
		global.Show.IndexClientBottom	= 'none'
		global.Show.IndexCenter			= ''
		global.Show.IndexCenterSpec		= 'none'
		global.Show.IndexTop			= 'none'
		global.Show.Work				= 'none'
		alternativePages()
		break;
	case 'OpacWeb':
		global.Show.IndexClientLeft		= 'none'
		global.Show.IndexClientLeftSpec	= 'none'
		global.Show.IndexClientTop		= 'none'
		global.Show.IndexClientRight	= 'none'
		global.Show.IndexClientBottom	= 'none'
		global.Show.IndexCenter			= 'none'
		global.Show.IndexCenterSpec		= 'none'
		global.Show.IndexTop			= 'none'
		global.Show.Work				= ''
		break;
	case 'Info':
		global.Show.IndexClientLeft		= 'none'
		global.Show.IndexClientLeftSpec	= 'none'
		global.Show.IndexClientTop		= 'none'
		global.Show.IndexTop			= 'none'
		global.Show.IndexCenter			= 'none'
		global.Show.IndexCenterSpec		= 'none'
		global.Show.Work				= ''
		break;
	case 'Help':
		break;
	default:
		break;
	}
}
function OpacWeb(pMode) {
	initialiseShow()
	
	switch (pMode) {
	case 'Init':
		global.Show.IndexCenter			= ''
		global.Show.IndexCenterSpec		= 'none'
		global.Show.IndexClientLeftSpec	= 'none'
		global.Show.Work				= 'none'
		alternativePages()
		break;
	case 'OpacWeb':
		global.Show.IndexTop			= 'none'
		global.Show.IndexCenter			= 'none'
		global.Show.IndexCenterSpec		= 'none'
		global.Show.Work				= ''
		break;
	case 'Info':
		global.Show.IndexClientTop		= 'none'
		global.Show.IndexTop			= 'none'
		global.Show.IndexCenter			= 'none'
		global.Show.IndexCenterSpec		= 'none'
		global.Show.Work				= ''
		break;
	case 'Help':
		break;
	default:
		break;
	}
}
function alternativePages() {
	var zAltCenter = eval('global.Langs.'+global.Lang+'.Dir.center')
	var zAltClientLeft = eval('global.Langs.'+global.Lang+'.Dir.clientleft')
	if ((zAltCenter!='') && (global.Show.IndexCenter=='')) {
		global.Show.IndexCenter		= 'none'
		global.Show.IndexCenterSpec = ''
	}
	if ((zAltClientLeft!='') && (global.Show.IndexClientLeft=='')) {
		global.Show.IndexClientLeft		= 'none'
		global.Show.IndexClientLeftSpec = ''
	}
}
function initialiseShow() {
	global.Show						= new Object()
	global.ShowInt					= new Object()
	
	global.ShowInt.ClientLeftWidth	= initValueSize(global.Style.ClientLeftWidth, global.ClientLeftWidth)
	global.ShowInt.ClientRightWidth	= initValueSize(global.Style.ClientRightWidth, global.ClientRightWidth)
	global.ShowInt.ClientTopHeight	= initValueSize(global.Style.ClientTopHeight, global.ClientTopHeight)
	global.ShowInt.ClientBottomHeight= initValueSize(global.Style.ClientBottomHeight, global.ClientBottomHeight)
	global.ShowInt.TopHeight		= initValueSize(global.Style.TopHeight, global.TopHeight)

	global.Show.IndexClientLeft		= initValue(global.Style.IndexClientLeft,global.IndexClientLeft,global.ShowInt.ClientLeftWidth)
	global.Show.IndexClientTop		= initValue(global.Style.IndexClientTop,global.IndexClientTop,global.ShowInt.ClientTopHeight)
	global.Show.IndexTop			= initValue(global.Style.IndexTop,global.IndexTop,global.ShowInt.TopHeight)
	global.Show.IndexClientRight	= initValue(global.Style.IndexClientRight,global.IndexClientRight,global.ShowInt.ClientRightWidth)
	global.Show.IndexClientBottom	= initValue(global.Style.IndexClientBottom,global.IndexClientBottom,global.ShowInt.ClientBottomHeight)
	global.Show.Work				= 'none'
	global.Show.IndexCenterNSP		= 'none'
	global.Show.IndexCenter			= ''

	global.Show.ClientLeftButtons	= initValue(global.Style.ClientLeftButtons, global.ClientLeftButtons)
	
	global.ShowInt.Home				= initValue(global.Style.Home, global.HomePage)
	
}
function initValue(pStyle, pConfig, pSize) {
	if (pSize=='0') return 'none'
	
	zStyle = ''
	if (pStyle=='hidden') zStyle='none'
	switch (pConfig) {
	case 'T': zStyle='';break;
	case 'F': zStyle='none'; break;
	default: break;
	}
	return zStyle
}
function initValueSize(pStyle, pConfig) {
	zStyle = '0'
	if (pStyle !='') zStyle=pStyle
	if (pConfig!='') zStyle=pConfig
	return zStyle
}

function setDisplay() {
	for (var item in global.Show) {
		document.getElementById(item).style.display = global.Show[item]
	}

	if ((navigator.appName=='Opera') &&
		(document.getElementById("IndexCenter").style.display=='') &&
		(document.getElementById("IndexCenter-TD").style.height=='')) {
		var zHeight = document.getElementById("IndexCenter-TD").offsetHeight
		document.getElementById("IndexCenter-TD").style.height = zHeight
		document.getElementById("workDetail").style.height = zHeight - 8
	}	
}
function home() {
	/* ------------------------------------------------------------- */
	/* What is the Home Page */
	/* ------------------------------------------------------------- */
	switch (global.Application) {
	case 'PPKOPAS':
		window.external.GetLocalBrowserCmd().Index();
		break;
	case 'Paprika':
		window.external.GetppkSVCSessions().CloseSession();
		break;
	case 'Carthame':
		window.external.GetppkSVCSessions().CloseSession();
		break;
	default:
		init()
		break;
	}
}
function setGlobals() {
	global.Application	= gApplication
			
	global.NSP = false;
	if (global.Application=='PPKOPAS') global.NSP = true;
	global.LocalPrint = true
	try {global.LocalPrint = window.external.GetLocalBrowserCmd().PrintButton()} catch (e) {}

	/* ------------------------------------------------------------- */
	/* Paths */
	/* ------------------------------------------------------------- */
	global.path					= new Object()
	global.path.imagePath		= '#site/images/#skin/'
	global.path.imageFull		= '#site/images/#skin'
	global.path.imageLangFull	= '#site/images/#skin/#lang'
	global.path.imageClientCSS	= '#site/images/clientstyle#skin.css'

	global.path.image			= '#site/images/#skin'
	global.path.imageLang		= '#site/images/#skin/#lang'
	global.path.button			= '#site/images/#skin/button'

	global.path.scripts			= '#site/scripts'
	global.path.opacweb			= '#site/bin/opacweb.dll/'
	
	global.path.opacwebInit		= '#site/bin/opacweb.dll/GlobalInfo?XFile=Index&Lang=#lang&Skin=#skin#Params&ForceSite=#ForceSite' // +global.Params
	if (global.Init) global.path.opacwebInit += '&Init='+global.Init
	else global.path.opacwebInit += '&Init=T'
	
	global.path.help			= '#site/server-ppk/Help/?Skin=#skin&Lang=#lang&XP_TSE=#TSE'
	global.path.params			= '&Lang=#lang&Skin=#skin'+global.Params
	
	global.path.blank			= '#site/blank.htm?Skin=#skin&Loading=#loading&Return=#return'
	global.path.info			= '#site/server-client/#lang/info.#suffix?Skin=#skin&Lang=#lang&XP_TSE=#TSE'
}
function returnPath(pPath) {
	if (!pPath) return
	var zPath = pPath.replace(/#site/g,global.Site)
	if (!global.ForceSite) global.ForceSite = global.Site
	zPath = zPath.replace(/#ForceSite/g,global.ForceSite)
	zPath = zPath.replace(/#skin/g,global.Skin)
	zPath = zPath.replace(/#TSE/g,global.XP_TSE)
	zPath = zPath.replace(/#lang/g,global.Lang)
	zPath = zPath.replace(/#suffix/g,global.suffix)
	zPath = zPath.replace(/#AllParams/g,global.AllParams)
	zPath = zPath.replace(/#Params/g,global.Params)
	zPath = zPath.replace(/#loading/g,eval('global.Langs.'+global.Lang+'.Loading'))
	zPath = zPath.replace(/#return/g,eval('global.Langs.'+global.Lang+'.Return'))
	return zPath
}
function returnLangText(pText) {
	var zName = pText.split('/')
	var z = ''
	for (var j=0;j<zName.length-1;j++) {z += zName[j] + '.'}
	z += global.Lang + '.' + zName[zName.length-1]
	z = eval(z)
	return z
}

/* ======================================================== */
/* Write or Update Style.css
/* ======================================================== */
function createLink() {
	document.write('<LINK id="style" rel="stylesheet" href="'+returnPath(global.path.imageFull)+'/style.css">')
	document.write('<LINK id="styleClient" rel="stylesheet" href="'+returnPath(global.path.imageClientCSS)+'">')
}
function updateLink() {
	document.getElementById("style").href=returnPath(global.path.imageFull)+'/style.css'
	document.getElementById("styleClient").href=returnPath(global.path.imageClientCSS)
}

/* ======================================================== */
/* Update SKIN/LANG
/* ======================================================== */
function updateSkin(pSkin) {
	global.Skin = pSkin
	init()
}
function updateLang(pLang) {
	global.Lang = pLang
	init()
}
function updateLangNoChange(pLang) {
	global.Lang = pLang
	init('nochange')
}
function updateLangText(pLang) {
	var o = document.getElementsByTagName("*")
	for (var i=0;i<o.length;i++) {
		var oNode = o[i]
		var zId = oNode.id
		if (zId.substr(0,1)=='#') {
			switch(zId.substr(0,4)) {
			case '#IMG':
				// Create an image node
				zName = zId.substr(4).split('/')
				var zSRC = ''
				for (var j=0;j<zName.length;j++) {
					if (zName[j].substr(0,1)=='#') zSRC += eval(zName[j].substr(1)) 
					else zSRC += zName[j]
					if (j!=zName.length-1) zSRC += '/'
				}
				var zAlt = oNode.title
				if (zAlt.substr(0,1)=='#') {
					zName = zAlt.substr(1).split('/')
					zAlt = ''
					for (var j=0;j<zName.length-1;j++) {zAlt += zName[j] + '.'}
					zAlt += pLang + '.' + zName[zName.length-1]
					zAlt = eval(zAlt)
				}
				// In firefox, when the language is changed, the hint will remain
				// the same!
				if (is_ff) oNode.title=''

				oNode.innerHTML = '<IMG border="0" alt="'+zAlt+'" src="'+zSRC+'"/>'
				break;
			default:
				switch (oNode.tagName) {
				case 'TABLE':
					zName = zId.substr(1).split('/')
					zEval = ''
					for (var j=0;j<zName.length-1;j++) {zEval += zName[j] + '.'}
					zEval += pLang + '.' + zName[zName.length-1]
					oNode.title = eval(zEval)
					break;
				default:
					zName = zId.substr(1).split('/')
					zEval = ''
					for (var j=0;j<zName.length-1;j++) {zEval += zName[j] + '.'}
					zEval += pLang + '.' + zName[zName.length-1]			
					var z = eval(zEval)	
					var zFirst = z.substr(0,1)
					if (String(oNode.innerHTML).indexOf('1st')!=-1)
						zFirst = '<span class="'+oNode.className+'1st">'+z.substr(0,1)+'</span>'
					oNode.innerHTML = zFirst + z.substr(1)
					break;
				}
				break;
			}
		}
	}
}

/* ======================================================== */
/* Execute Links
/* ======================================================== */
function ShowAccount(LogCode,FullName,DataBase) {
	// Make it the same height and width of calling window
	var win = parent
	if (NSP) win = self
	var obj = getWindowDetails(win)
	ShowUniversalModalWindowPath(returnPath(global.path.opacweb),self,'AccountAb?XFile=Account&LoginCAb='+LogCode+'&ConsultCAb='+LogCode+'&DataBaseNames='+DataBase,returnLangText('Lang/Consult'),obj.height,obj.width,'r','F',obj.top,obj.left);
}
function goLogin() {
	var zUserId = returnLangText('Lang/UserId')
	var zLink = 'GlobalInfo?XFile=LoginAb&Init=T&Action=ShowAccount&ChooseBase=T&' + returnPath(global.path.params) + global.opacwebParams
	ShowUniversalModalWindowPath(returnPath(global.path.opacweb),self,zLink,encodeUtf8(zUserId),'310','500','r','T');
}

function goReturn() {
	init('nochange')
}
function goHelp() {
	ShowWindow(returnPath(global.path.help),'500','750',true)
}
function goInfoII() {
	ShowWindow(returnPath(global.path.info),'500','750',true,true)
}
function goInfo() {
	switch (global.Application) {
	case 'Paprika': goPaprika('Info'); break;
	case 'Carthame': goPaprika('Info'); break;
	case 'PPKOPAS': goNSP('Info'); break;
	default: OpacWeb('Info'); break;
	}	
	forNetscapeShow()
	setDisplay()
	document.getElementById("workSRC").src = returnPath(global.path.info)
	forNetscapeHideA()
}
function goOpacWeb(pSource) {
	switch (global.Application) {
	case 'Paprika': goPaprika('OpacWeb'); break;
	case 'Carthame': goPaprika('OpacWeb'); break;
	case 'PPKOPAS': goNSP('OpacWeb'); break;
	default: OpacWeb('OpacWeb'); break;
	}
	forNetscapeShow()
	setDisplay()
	var zSource = returnPath(global.path.opacwebInit)+FTStamp()
	if ((String(pSource)!='undefined') && (String(pSource)!='')) zSource = pSource
	document.getElementById("workSRC").src=zSource
	forNetscapeHideA()
}
function goInit(pType) {

	switch (global.Application) {
	case 'Carthame': goOpacWeb(); return; break;
	case 'Paprika': goOpacWeb(); return; break;
	case 'PPKOPAS': goNSP('Init'); break;
	default: OpacWeb('Init'); break;
	}

	setBlank()	
	
	setDisplay()
	if (String(pType)=='nochange') return;

	var zAltCenter = eval('global.Langs.'+global.Lang+'.Dir.center')
	var zAltClientLeft = eval('global.Langs.'+global.Lang+'.Dir.clientleft')
	if (zAltCenter!='') {
		document.getElementById('IndexClientLeftSpec').src = zAltCenter+'?Skin='+global.Skin
	}
	if (zAltClientLeft!='') {
		document.getElementById('IndexClientLeftSpec').src = zAltClientLeft+'?Skin='+global.Skin
	}

}
function setBlank() {
	var o = document.getElementById("workSRC")
	if (String(o.src).indexOf('blank.htm')==-1) {
		gLaunched = 'T'
		document.getElementById("workSRC").src = returnPath(global.path.blank)
	}
}
function forNetscapeShow() {
	if (!is_ie)
		document.getElementById('forNetscape').style.display=''
}
function forNetscapeHide() {
	if (!is_ie) 
		document.getElementById('forNetscape').style.display='none'
}
function forNetscapeHideA() {
	if (is_ff) 
		document.getElementById('forNetscape').style.display='none'
}
/* ======================================================== */
/* Initialise
/* ======================================================== */
function init(pType) {
	css('D')
	updateLink()
	updateLangText(global.Lang)
	
	//document.title = eval('global.Langs.'+global.Lang+'.PageTitle')
	document.title = eval('global.Langs.'+global.Lang+'.GetTitle')
	
	try {parent.document.title=document.title} catch (e) {}
	try {
		var list = document.getElementById("selCenter").options
		for (var i=0;i<list.length;i++) {
			if (list[i].value==global.Lang) list[i].selected = true
			else list[i].selected=false
		}
	} catch (e) {}
	try {
		var list = document.getElementById("selLeft").options
		for (var i=0;i<list.length;i++) {
			if (list[i].value==global.Lang) list[i].selected = true
			else list[i].selected=false
		}
	} catch (e) {}
	
	if (pType=='nochange') return

	setBlank()	
	
	if ((initValue(global.Style.Home, global.HomePage)=='') || (cookiesAccepted=='F')) {
		goInit(pType)
	} else {
		goOpacWeb()
	}
}
function getStyleValues() {
	global.Style = new Object()
	global.Style.IndexClientLeft	= getStyleValue('PpkClientLeft','visibility')
	global.Style.IndexClientTop		= getStyleValue('PpkClientTop','visibility')
	global.Style.IndexClientRight	= getStyleValue('PpkClientRight','visibility')
	global.Style.IndexClientBottom	= getStyleValue('PpkClientBottom','visibility')
	global.Style.IndexCenter		= ''
	global.Style.IndexTop			= getStyleValue('PpkTop','visibility')
	global.Style.Home				= getStyleValue('PpkHome','visibility')
	global.Style.ClientLeftButtons	= getStyleValue('PpkClientLeftButtons','visibility')

	global.Style.ClientLeftWidth	= getStyleValue('TD.ClientLeft','width')
	global.Style.ClientRightWidth	= getStyleValue('TD.ClientRight','width')
	global.Style.ClientTopHeight	= getStyleValue('TR.ClientTop','height')
	global.Style.ClientBottomHeight	= getStyleValue('TR.ClientBottom','height')
	global.Style.TopHeight			= getStyleValue('TR.Top','height')
}
/* ======================================================== */
/* Show, hide and resize elements 
/* ======================================================== */
function alterElements() {
	// If in NSP mode and there is an NSP page, use it in place of the Center
	if ((document.getElementById("IndexCenterNSP-TD").innerHTML!='') && (global.Application=='PPKOPAS')) {
		document.getElementById("IndexCenter-TD").innerHTML = 
			document.getElementById("IndexCenterNSP-TD").innerHTML
	}	
	
	document.getElementById("IndexClientLeft").style.width	  = global.ShowInt.ClientLeftWidth
	document.getElementById("IndexClientLeftSpec").style.width	  = global.ShowInt.ClientLeftWidth
	document.getElementById("IndexClientRight").style.width   = global.ShowInt.ClientRightWidth	
	document.getElementById("IndexClientTop").style.height	  = global.ShowInt.ClientTopHeight
	document.getElementById("IndexClientBottom").style.height = global.ShowInt.ClientBottomHeight
	document.getElementById("IndexTop").style.height		  = global.ShowInt.TopHeight

	try {
		if ((global.Account!='T') || (global.QueryString.Account=='F')) {
			document.getElementById("ButtonLeftAccount").style.display='none'
			document.getElementById("ButtonCenterAccount").style.display='none'
		}
	} catch (e) {}

	try {
		if (Number(global.LangNo) <= 1) {
			document.getElementById("ButtonLeftLangOption").style.display='none'
			document.getElementById("ButtonCenterLangOption").style.display='none'
		}
	} catch (e) {}

	try {
		if (global.Show.IndexClientLeft=='' && global.Show.ClientLeftButtons=='') {
			document.getElementById("ButtonCenterLangOption").style.display='none'
		}
	} catch (e) {}


}
function startOnLoad() {
	// gLaunched = 'T'
	// document.getElementById("workSRC").src = returnPath(global.path.blank)
	zBlank	= returnPath(global.path.blank)
	
	if (global.Application=='PPKOPAS') {
		for (lang in global.Langs) {
			global.Langs[lang].Dir.center = global.Langs[lang].Dir.centerNSP
		}
	}
	global.opacwebParams = getOpacParams()

	getStyleValues()
	init()
	alterElements()		
}
function deleteFontsizeCookie() {
	document.cookie = "fontSize=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
/* ======================================================== */
/* START
/* ======================================================== */


deleteFontsizeCookie()
setGlobals()
createLink()
