// ##########################################
// Common pages functions
// ##########################################



function ErrorHandler(msg, url, linenumber) {
  alert('Error message= '+msg+'\nURL= '+url+'\nLine Number= '+linenumber);
}

window.onerror= null;




function confirmLink(theLink, theText) {
    is_confirmed = confirm(theText);
    if (is_confirmed) {
	  A= theLink.href.split('&#'); // save anchor link at end of url
	  if(A.length>1) theLink.href = A[0] + '&ProcConf=1&#' + A[1];
		else theLink.href += '&ProcConf=1';
    }
    return is_confirmed;
}

function confirmAndGoto(theText, address) {
    is_confirmed = confirm(theText);
    if (is_confirmed) {
	  A= address.split('&#'); // save anchor link at end of url
	  if(A.length>1) document.location.href= A[0] + '&ProcConf=1&#' + A[1];
		else document.location.href= address + '&ProcConf=1';
    }
}

function PromptSimpleValue(theLink, theText) {
	Txt= prompt(theText,"");
	if(Txt==null || Txt=="") return false;
	theLink.href += "&Prompted="+encodeURI(Txt);
	return true;
}

function LocaliseNumber(Num, ThSeparator) {
	Num= ""+Num; // conv to string
	Num.replace(/^\s+/, '');
	Num.replace(/\s+$/, '');
	for(x=9;x>0;x-=3)
	  if(Num.length>x) Num= Num.substring(0,Num.length-x)+ThSeparator+Num.substring(Num.length-x);
	return Num;
}

function jsSetLang(url, sel){
  exp=new Date();
  time=365*60*60*24*1000*5;
  exp.setTime(exp.getTime()+time);
  s= sel.options[sel.selectedIndex].value;
  document.cookie="Lang="+s+"; expires="+exp.toGMTString()+"; path=/";
  document.location.href = url+'SetLang='+s;
}

function GoBack() {
  history.back();
  return false;
}

function PreviewInFrame(what) {
   window.open(what,"PreviewFrame","")
}

function FocusDivOnPos(DivId,Poz,Total) {
  var objDiv = document.getElementById(DivId);
  if (!objDiv) return;
  H1= objDiv.scrollHeight / Total;
  P= (Poz-2)*H1;
  if (P<0) P= 0;
  if (P>objDiv.scrollHeight) P= objDiv.scrollHeight;
  objDiv.scrollTop = P;
}

function swapNavigation(div) {  // for collapsing menu
  displayType = ( document.getElementById( div ).style.display == "none" ) ? "block" : "none";
  document.getElementById( div ).style.display = displayType;
  document.cookie = div + "=" + displayType;
}

function HtmlBlinker(Num) {
  if(HtmlBlinker_State==1) { newcolor= "#FF0000"; HtmlBlinker_State= 0; }
			else { newcolor= ""; HtmlBlinker_State= 1; }
  for(x=1;x<=Num;x++) {
    var obj = document.getElementById("HtmlBlinker"+x);
    obj.style.color= newcolor;
  }
}

function PicPopup(url,w,h) {
  window.open(url,"flyout","resizable=no,scrollbars=no,margin=0,width="+w+",height="+h);
}

function ToggleDisplay(ObjId) {
  Obj= document.getElementById(ObjId);
  Obj.style.display= (Obj.style.display=="none" ) ? "block" : "none";
}

function ToggleDisplayByParam(ObjId, Param) {
  Obj= document.getElementById(ObjId);
  Obj.style.display= (Param) ? "block" : "none";
}

var TipTogglerState= 0;
function TipsToggleClick(TextShowTip,TextHideTip) {
  TipTogglerState++;
  if(TipTogglerState>1) TipTogglerState= 0;
  if(TipTogglerState) document.getElementById("aTipsToggler").innerHTML= TextHideTip;
    else document.getElementById("aTipsToggler").innerHTML= TextShowTip;
  document.getElementById("TipsTogglePanel").style.display= (TipTogglerState) ? "block" : "none";
}

function IsAnySubChecked(FormName,ElementsName) {
  List= eval('document.'+FormName+'.'+ElementsName);
  checked= false;
  for(x=0; x<List.length; x++) if(List[x].checked) checked= true;
  return checked;
}

function miSCSS_OnLoad() {
   if (document.all&&document.getElementById) {
  	var ParentLIS= document.getElementById("miSCSS");
  	var LIS= ParentLIS.getElementsByTagName("li");
    for (i=0; i<LIS.length; i++) {
  	  var Obj= LIS[i];
	  Obj.onmouseover=function() { this.className+=" over"; }
	  Obj.onmouseout=function() { this.className=this.className.replace(" over", ""); }
   }}
}


function ColorizeAndAlignTable2(Tbl, Aligns, HoverClass, NormClass, Color1, Color2) {
    Tab= document.getElementById(Tbl);
    Trs= Tab.getElementsByTagName("tr");
    for (r=0; r<Trs.length; r++) {
  	Tds= Trs[r].getElementsByTagName("td");
      for (d=0; d<Tds.length; d++) {
		Tds[d].align= Aligns[d];
		Tds[d].className= NormClass;
		color= (r % 2 == 0) ? Color1 : Color2;
		Tds[d].bgColor= color;
    		Tds[d].onmouseover=function() {
			Tds= this.parentNode.getElementsByTagName("td");
			for(d=0; d<Tds.length; d++) Tds[d].className= HoverClass;
		}
		Tds[d].onmouseout=function() {
			Tds= this.parentNode.getElementsByTagName("td");
			for(d=0; d<Tds.length; d++) Tds[d].className= NormClass;
		}
      }
    }
}
function ColorizeAndAlignTable(Tbl, Aligns, HoverClass) {
	ColorizeAndAlignTable2(Tbl, Aligns, HoverClass, "", "#eaeaea", "#f6f6f6");
}


function WbtCorrection() {
  if(obj= document.getElementById("WbtDiv")) {
	obj.style.position= "absolute";
	obj.style.left= "-18px";
  }
}


function getWindowSize() {
	if(typeof(window.innerWidth)=='number') {
		return { width: window.innerWidth, height: window.innerHeight};
	} else if(document.documentElement && (document.documentElement.clientWidth ||document.documentElement.clientHeight) ) {
		return { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight};
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		return { width: document.body.clientWidth, height: document.body.clientHeight};
	}
	return {width:0,height:0};
}



// #####################
// ##  Click counter  ##
// #####################

var ClickCounterPhpLink= '';
function ClickCounterInit(PhpLink) {
  ClickCounterPhpLink= PhpLink; // use real path
  var aTags= document.getElementsByTagName('a');
  for (i=0; i<aTags.length; i++) {
    var Obj= aTags[i];
    if(!Obj.name||!(Obj.name.substring(0,7)=='aBanner'||Obj.name.substring(0,8)=='aExtLink')) continue;
    if(document.addEventListener) Obj.addEventListener('click', ClickCounterEvent, false);
     else if(document.attachEvent) Obj.attachEvent('onclick', ClickCounterEvent);
      else Obj.onclick= ClickCounterEvent;
    // correction of left position for swf mask
    if(Obj.name.substring(0,7)=='aBanner'&&Obj.parentNode.tagName=='DIV'&&parseInt(Obj.parentNode.style.marginLeft)<0) {
      if(!document.all) {
 	    ppObj = Obj.parentNode.parentNode;
  	    W= (document.defaultView && document.defaultView.getComputedStyle)
  	     ? document.defaultView.getComputedStyle(ppObj,null).getPropertyValue("width")
   	     : ppObj.offsetWidth.toString();
   	    NewWidth= parseInt(W);
    	Obj.parentNode.style.marginLeft= 0-NewWidth+'px'; // div
    	Obj.firstChild.style.width= NewWidth+'px'; // spacer img
     }
     if(!document.all) Obj.parentNode.style.position= "relative";
    }
  }
}
function ClickCounterConnector(param) {
  var img= new Image();
  img.src= param;
}
function ClickCounterEvent(env) {
  var clicksrc= (document.all) ? window.event.srcElement : env.target;
  var tag= (clicksrc.tagName!="A") ? clicksrc.parentNode : clicksrc;
  if(!tag||tag.tagName!="A") {alert('click counter: '+tag.tagName); return;}
  if(!tag.name) {alert('click counter: no tag.name'); return;}
  if(tag.name.substring(0,7)=='aBanner') param= 'b='+tag.name.substring(7, 99);
  if(tag.name.substring(0,8)=='aExtLink') param= 'e='+tag.name.substring(8, 99);
  setTimeout("ClickCounterConnector('"+ClickCounterPhpLink+"?"+param+"')", 10);
  return;
}


function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}



function calcAbsolutePosition(Obj) {
    var valueT = 0, valueL = 0;
    do {
      valueT += Obj.offsetTop;
      valueL += Obj.offsetLeft;
alert(valueL+'-'+valueT);
      Obj = Obj.offsetParent;
      if (Obj) {
        if (Obj.tagName == 'BODY') break;
    	var p = Obj.style.position;
    	if (!p) {
      		var css = document.defaultView.getComputedStyle(Obj, null);
      		p = css ? css.position : null;
    	}
        if (p == 'relative' || p == 'absolute') break;
      }
    } while (Obj);
    return [valueL, valueT];
}

function getDimensions(Obj) {
	// kalkulisane dimenzije
	if(document.defaultView && document.defaultView.getComputedStyle) { // FF
		var W = document.defaultView.getComputedStyle(Obj,null).getPropertyValue("width");
		var H = document.defaultView.getComputedStyle(Obj,null).getPropertyValue("height");
		return [parseInt(W)-2,parseInt(H)-2]; // ove korekcije još treba proveriti
	}
	var W = Obj.offsetWidth.toString();	//IE
	var H = Obj.offsetHeight.toString();
	return [parseInt(W)-1,parseInt(H)-1];
}


function SetElementHeightByRatio(ElementId, Ratio, Addition) {
	// f. za setovanje visine prema kalkulisanoj širini
	var E= document.getElementById(ElementId);
	var D= getDimensions(E);
	E.height= Math.round(D[0] * Ratio)+Addition;
}


var BusyDiv = {
	show: function(elementId, shadePath, aniPath) {
		// make and show busy animantion over existing element
		var eDiv= document.getElementById(elementId);
alert(eDiv.id+':'+eDiv.offsetTop);
		var eDivDim= getDimensions(eDiv);
		var eDivPos= calcAbsolutePosition(eDiv);
alert(eDivPos);
		var bDiv= document.createElement('div');
		bDiv.setAttribute('id', elementId+'_BusyDiv');
		document.body.appendChild(bDiv);
		bDiv.style.cssText= 'display:block;position:absolute;left:'+eDivPos[0]+'px;top:'+eDivPos[1]+'px;width:'+eDivDim[0]+'px;height:'+eDivDim[1]+'px;background-image:url('+shadePath+');z-index:1001;margin:auto auto;';
		bDiv.innerHTML= '<img src="'+aniPath+'" />';
	},

	hide: function(elementId) {
		var bDiv= document.getElementById(elementId+'_BusyDiv');
		if(!bDiv) return;
		bDiv.parentNode.removeChild(bDiv);
	}
}


function StaticImageOnLoad(I,w,h) {
	// patch za IE i Operu koji ne kontaju stil max-width
	// pozvati sa: <img src=".." style="max-width:54px;max-height:46px" onload="StaticImageOnLoad(this,54,46)" />, dakle bez width i height atributa
    if(!(window.attachEvent)) return; // zaustavi FF, propusti IE i Opera
  	I.style.visibility= "hidden";
	I.removeAttribute("width");
	I.removeAttribute("height");
  	//alert("Orig size: "+I.width+"x"+I.height);
  	var s= [I.width, I.height];
    var ratio=  s[0]/s[1];
    if(s[0]>w) s= [w, w/ratio];
    if(s[1]>h) s= [h*ratio, h];
    I.width=s[0];	I.height=s[1];
    I.style.visibility= "visible";
}


function MakeVideoPlayer(PlayerId, Link, PlayerSrc, AP) {
  	 var Opts= { src:PlayerSrc };
  	 if((window.attachEvent)||(window.opera)) {
	 	var D= getDimensions(document.getElementById(PlayerId));
	 	Opts.height= D[0]*0.8; // patch for IE & Opera
  	 }
	 flashembed(PlayerId, Opts,
		{config: {	autoPlay:AP,			controlBarBackgroundColor:"0x535353",
					initialScale: "fit",	showFullScreenButton: false,
					showMenu: false,		videoFile: Link
		}}	);
  }

function MakeAudioPlayer(PlayerId, Link, PlayerSrc, AP) {
  	 var E= document.getElementById(PlayerId);
  	 var Dim= getDimensions(E.parentNode);
  	 var W= parseInt(Dim[0]*0.98);
  	 if((window.attachEvent)||(window.opera)) W -= 6; // Stupid IE and Opera
  	 AP= (AP) ? '&amp;autoplay=1' : '&amp;autoload=1'; // nemože zajedno i AP i AL
  	 E.innerHTML= '<object type="application/x-shockwave-flash" width="'+W+'" height="20" data="'+PlayerSrc+'"><param name="wmode" value="transparent" /><param name="movie" value="'+PlayerSrc+'" /><param name="FlashVars" value="mp3='+Link+'&amp;width='+W+AP+'&amp;showvolume=1&amp;loop=1" /><\/object>';
  }

function MakeMP3Album(PlayerId, Link, PlayerSrc, ObjHeight) {
  	 var E= document.getElementById(PlayerId);
  	 var Dim= getDimensions(E.parentNode);
  	 var W= parseInt(Dim[0]*0.98);
  	 if((window.attachEvent)||(window.opera)) W -= 6; // Stupid IE and Opera
  	 E.innerHTML= '<object type="application/x-shockwave-flash" width="'+W+'" height="'+ObjHeight+'" data="'+PlayerSrc+'"><param name="wmode" value="transparent" /><param name="movie" value="'+PlayerSrc+'" /><param name="FlashVars" value="mp3='+Link+'&amp;width='+W+'&amp;height='+ObjHeight+'&amp;showvolume=1" /><\/object>';
  }

  
