var isIE4 = false;
var isNS4 = false;

if (document!=null)
	isIE4 = (document.all ? true : false);
if (!isIE4)
	isNS4 = document.layers ? true : false;

var winW;
var winH;


var refwindow;
var stevec = 0;

function openref(url) {
		stevec++;
		var ime = "okno" + stevec;
		var   opcije = "width=600,height=400,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1,dependent=true,alwaysRaised=true,titlebar=false,z-lock=true";
//		alert ("Odprl bom okno" + ime);
		if(document.layers || document.all || navigator.userAgent.indexOf("Mozilla/5")!=-1)
				window.open(url + "&nobar=1&t=1",ime, opcije);
}

function getObject(field) {
	var ret  = false;
	eval ("ret = window.document.biblija." + field + ";");
	return ret;
}


function setValue(field, content){
	var ret  = getObject (field);
	if (ret)
		ret.value = content;
	else
		alert ("Your browser is old and has no so called 'Document Object Model' required by BIBLIJA.net.\nPlease upgrade it to some newer version to use it with BIBLIJA.net!");
	return ret;
}


function mygoto (field, content){
	var ret  = setValue (field, content);
	if (ret)
		document.biblija.submit();
}


function validate()
{
  var result = 1;
  window.document.biblija.qids.value = "";
  for (var i = 0; i<window.document.ids.elements.length;i++){
	var e = window.document.ids.elements[i];
	if (e.value >= 256 ){
		if (result>=128){
			window.document.biblija.qids.value = window.document.biblija.qids.value + result.toString(16);
			result=1;
		}
		result *=2;
		result += e.checked ? 1 : 0;
	};
  }
  if (result!=2)
	window.document.biblija.qids.value = window.document.biblija.qids.value + result.toString(16);
  window.document.biblija.qall.value = window.document.ids.qall.checked ? 1 : 0;
  return (true);
}


  function izberi(id, level)
  {
	var myref;
	var mycnt = 0;
	for (var i = 0; i<window.document.ids.elements.length;i++)
		if (window.document.ids.elements[i].value % 256 == id){
			myref=window.document.ids.elements[i];
			mycnt = i;
		}
		 var  koncaj = false;
	for (var i=mycnt+1;!koncaj;)
    {
		var e = window.document.ids.elements[i];
		if (e.value / 256 > level + 1 )
			e.checked = myref.checked;
		else
			koncaj=1;
		i++;
    if (i==window.document.ids.elements.length)
		koncaj=true;
    }
  }

  function izberi2(id, level)
  {
	var myref;
	var mycnt = 0;
	var total = window.document.ids.length;
	for (var i = 0; i<total;i++){
			// value: zgornji byte le level, spodnji id
			if (window.document.ids.elements[i].value % 256  == id){
				myref=window.document.ids.elements[i];
				mycnt = i;
			}
	}
	var	koncaj = false;
	if (myref.checked)
		  myref.checked = false;
	 else
		 myref.checked = true;
	 izberi(id,level);

  }

  function revert(id)
  {
    myref=window.document.ids.elements[id];
   if (myref.checked)
	myref.checked = false;
   else
	myref.checked = true;
  }


  function izberivse(change)
 {
	if (change )
		if (window.document.ids.qall.checked)
			window.document.ids.qall.checked = false;
		else
			window.document.ids.qall.checked = true;

	for (var i=0;i<window.document.ids.elements.length;i++)
		window.document.ids.elements[i].checked = window.document.ids.qall.checked;
}



////////// funkcije za Layerje - opombe
////////// naredi jih še za Netscape



// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.

// If NS -- that is, !IE -- then set up for mouse capture
if (isNS4)
	document.captureEvents(Event.MOUSEMOVE);

// Set-up to use getMouseXY function onMouseMove
// if (isIE4)
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0;
var tempY = 0;

function getMouseXY(e) {
  if (isIE4) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0};
  if (tempY < 0){tempY = 0};
  return true;
}


function note (layer,image, g1, g2){
  var g1 = (g1 == null) ? '/images/opomba.gif' : g1;
  var g2 = (g2 == null) ? '/images/opomba2.gif' : g2;

  showhide(layer,image,g1,g2);
}



function xref (layer,image){
  
  var graphic1 = '/images/ref.gif';
  var graphic2 = '/images/ref2.gif';

  showhide(layer,image,graphic1,graphic2);
}


function showhide (layer,image,graphic1,graphic2){
//	if(document.layers || document.all || navigator.userAgent.indexOf("Mozilla/5")!=-1)
//	alert ("stanje od " + num + ": " + document.all[num].style.visibility + ", kaj= " + kaj);

	// to ne sme biti na začetku, ker mora biti body že naložen, kar na začetku še ni
	winW = isNS4 ? window.innerWidth-16 : document.body.offsetWidth-20;
	winH = isNS4 ? window.innerHeight : document.body.offsetHeight;
//	if (!document.all)
//		return;
	var cx = tempX + 10;
	var cy = tempY - 10;
	if (cx + 200 > winW)
		cx = tempX - 220;
	if (cx < 5){
		cx = 5;
		cy = cy + 20;
	}
	if (document.all[layer].style.visibility == "visible"){
		document.all[layer].style.visibility = "hidden";
		document.images[image].src= graphic1;
	}
	else{
		document.images[image].src= graphic2;
		document.all[layer].style.left = cx;
		document.all[layer].style.top = cy;
		document.all[layer].style.visibility = "visible";
	}
}


function wapme(){
	if(document.layers || document.all || navigator.userAgent.indexOf("Mozilla/5")!=-1)
		wapwindow = window.open('http://gelon.net/cgi-bin/wapalizenokia6210.cgi?url=http://wap.biblija.net','nokia6210','width=200,height=430,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=0,resizable=0');
	else
		alert('Žal!\nNepoznan brkljalnik...\n.');
}

function popup(lang){
	var url = '/translations.' + lang + '.html';
	if(document.layers || document.all || navigator.userAgent.indexOf("Mozilla/5")!=-1)
		infowindow = window.open(url,'infowindow','width=500,height=550,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1');
	else
		alert('Žal!\nNepoznan brkljalnik...\n.');
}

//////////////// KOMENTIRANJE - začetek
	function createForm (ref, objid){
		var obj = document.getElementById(objid);
		if (obj.style.visibility == "visible"){
			obj.style.visibility = "hidden";
		}
		else {
			var html = komentar_str;
			html = html.replace ("{kid}", objid);
			html = html.replace ("{kid}", objid);
			html = html.replace ("{kid}", objid);
			html = html.replace ("{kid}", objid);
			html = html.replace ("{text}", "text"+objid);
			obj.innerHTML = html;
			//alert (html);
			//alert ("ref: "+ref+", objid: "+objid);
			var tmp = 'ref'+objid;
			document.getElementById(tmp).value = ref;
			obj.style.visibility = "visible";
			tmp = 'text'+objid;
			document.getElementById(tmp).value="";
			var tmp = 'name'+objid;
			document.getElementById(tmp).focus();
		}
	}
    function makeRequest(url, data) {
        var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
        httpRequest.open('POST', url, true);
		httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpRequest.setRequestHeader("Content-length", data.length);
		httpRequest.setRequestHeader("Connection", "close");
		httpRequest.send(data);
    }

    function alertContents(httpRequest) {

        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
                eval(httpRequest.responseText);
            } else {
                alert('There was a problem with the request.');
            }
        }

    }
	
	function hideme(kid){
			document.getElementById(kid).style.visibility="hidden";
	}
	
	function getData(kid) {
		//alert (kid);
      var ret =  "text=" + encodeURI( document.getElementById("text"+kid).value );
	  ret = ret + "&ref=" + encodeURI( document.getElementById("ref"+kid).value );
	  ret = ret + "&name=" + encodeURI( document.getElementById("name"+kid).value );
	  ret = ret + "&kid=" + kid;
      return ret;
   }
   
   var komentar_str = '<img src="/images/close.gif" title="Zapri!" align="right" onclick=\'hideme("{kid}");\'">Vaše ime/vzdevek: <input id="name{kid}" /><input type="hidden" id="ref{kid}" /><br />Komentar: <textarea class="tarea" id="{text}"></textarea><br />' + 
		'<input type="button" value="Oddaj komentar" onclick=\'makeRequest("/komentar/xml.php", getData("{kid}"))\' />';
		
  /////////////////////////////// komentiranje - konec


