// 23.02.2009.jjh
//   writeLog(fileName)

// 30.03.2008.jjh
//   In showCurrent(): ",curLang," instead of ",getlang(),"

// 2.3.2008.jjh
//   function showMain(str,lang) -- deleted --
//   function showById(id,fileName,lang)
//   function writeHTML(req, id)

// 7.1.17.b.jjh 
//   new version of getXmlHttpObject().

var xHttpMenu=null
var xHttpMain=null
var xHttpIngr=null
var xHttpNow=null
var curPage=null
var curLang=null


////////////////////////////////////////////////////////////
//
// Used in index.html, menu_xx.html and ajankohtaista_xx.html
//
function showPage(page) {
	setPage(page);
	show1ById(xHttpMain,'idMainTxt',getPage(),getLang(),'yes');
}


////////////////////////////////////////////////////////////
//
// Used in "body onload="
//
function showCurrent() {
	var id=null;
	var file=null;
	var xHttp=null;
	
	if (curPage==null) {
		setPage('Etusivu');
	}
	// 7.10.2010.jjh.begin
	/*
	if (curLang==null) {
		setLang('fi');
	} else {
		showMotto();
		show1ById(xHttpMenu  ,'idMenu'         ,'menu'         ,curLang,'no');
		show1ById(xHttpIngr, 'idIngress'      ,'ingress'      ,curLang,'no');
		show1ById(xHttpNow   ,'idAjankohtaista','ajankohtaista',curLang,'no');
		show1ById(xHttpMain  ,'idMainTxt'      ,getPage()      ,curLang,'yes');
	}
	*/
	if (curLang==null) {
		setLang('fi');
	} else {
		show1ById(xHttpMain ,'idMainTxt'      ,getPage()      ,curLang,'yes');
		show1ById(xHttpMenu ,'idMenu'         ,'menu'         ,curLang,'no');
		show1ById(xHttpNow  ,'idAjankohtaista','ajankohtaista',curLang,'no');
		show1ById(xHttpIngr ,'idIngress'      ,'ingress'      ,curLang,'no');
		showMotto();
	}
	show1ById(xHttpMain ,'idMainTxt'      ,getPage()      ,curLang,'no');
	show1ById(xHttpNow  ,'idAjankohtaista','ajankohtaista',curLang,'no');
	show1ById(xHttpMenu ,'idMenu'         ,'menu'         ,curLang,'no');
	
	id="idAjankohtaista"
	file="ajankohtaista"
	xHttp=xHttpNow
	if (document.getElementById(id).innerHTML=="...") {
//		alert(id+"...");//jjhtst
		show1ById(xHttp,id,file,curLang,'no');
	}
	
	id="idIngress"
	file="ingress"
	xHttp=xHttpIngr
	if (document.getElementById(id).innerHTML=="...") {
//		alert(id+"...");//jjhtst
		show1ById(xHttp,id,file,curLang,'no');
	}
	// 7.10.2010.jjh.end
}


////////////////////////////////////////////////////////////
function show1ById(xmlHttpObj,id,fileName,lang,doLog) {
	if (fileName.length==0) {
		document.getElementById(id).innerHTML="";
		return;
	}

	xmlHttpObj=getXmlHttpObject();

	if (xmlHttpObj==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

//	var url="php/getByIdTxt.php";
	var url="";
	url=url+"?fileName="+fileName;
	url=url+"&lang="+lang;
	url=url+"&log="+doLog;
	url=url+"&sid="+Math.random();
//	alert(url);//jjhtst
	url="php/getByIdTxt.php"+url;
//	var timeout=setTimeout("writeLog('"+fileName+"/2s')",2000);//jjhtst

	xmlHttpObj.onreadystatechange= function() {
  		if (xmlHttpObj.readyState==4)
  			if (xmlHttpObj.status==200)
  				writeHTML(xmlHttpObj, id);
	}
	xmlHttpObj.open("GET",url,true);
	xmlHttpObj.send(null);
}


////////////////////////////////////////////////////////////
function writeLog(fileName) {
//	alert ("writeLog()");

	xmlHttpObj=getXmlHttpObject();
	if (xmlHttpObj==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url="php/writeLog.php";
	url=url+"?fileName="+fileName;
	url=url+"&lang="+getLang();
//	url=url+"&log=yes";
//	url=url+"&sid="+Math.random();

	xmlHttpObj.onreadystatechange= function() {
  		if (xmlHttpObj.readyState==4)
  			if (xmlHttpObj.status==200)
  				writeHTML(xmlHttpObj, id);
	}
	xmlHttpObj.open("GET",url,true);
	xmlHttpObj.send(null);
}


////////////////////////////////////////////////////////////
function showMotto() {
	if (getLang()=='fi') {
		document.getElementById('idMottoTxt').innerHTML = 'Hyvinvointia luonnollisesti';
	} else {
		document.getElementById('idMottoTxt').innerHTML = 'Wellbeing, naturally';
	}
}


////////////////////////////////////////////////////////////
function setLang(lang) {
	curLang=lang;
	showCurrent();
//	alert(curLang+":"+curPage); //jjhtst
}


////////////////////////////////////////////////////////////
function getLang() {
	return curLang;
}


////////////////////////////////////////////////////////////
function setPage(page) {
	curPage=page;
}


////////////////////////////////////////////////////////////
function getPage() {
	return curPage;
}


////////////////////////////////////////////////////////////
function writeHTML(req, id) {
	document.getElementById(id).innerHTML = req.responseText;
}


////////////////////////////////////////////////////////////
function getXmlHttpObject() { 
// return null; //jjhtst// => "Browser does not support HTTP Request"

 try { return new XMLHttpRequest(); } catch(e) {}
 try { return new ActiveXObject("Msxml2.xmlHttp"); } catch(e) {}
 try { return new ActiveXObject("Microsoft.xmlHttp"); } catch(e) {}
 return null;
}

