function elHide(div){
  document.getElementById(div).style.display='none';
}
function elShow(div){
  document.getElementById(div).style.display='block';
}
function SlideSH(div){
  if(document.getElementById(div).getAttribute('open') == 1){
    if(openedMeni != ''){
      if(openedMeni != 'menit'){
        Slide(openedMeni).up();
        document.getElementById(openedMeni).setAttribute('open',1);
      }
      document.getElementById('a'+openedMeni).parentNode.className='menuitemsd';
      document.cookie = openedMeni+'=0; expires=Thu, 2 Aug 2020 20:47:11 UTC; path=/';
      openedMeni = '';
    }
    Slide(div).down();
    opacity(div, 0, 100, 200);
    document.getElementById(div).setAttribute('open',2);
    document.getElementById('a'+div).parentNode.className='menuitemsda';
    openedMeni = div;
    document.cookie = div+'=1; expires=Thu, 2 Aug 2020 20:47:11 UTC; path=/';
  }else{
      Slide(div).up();
      opacity(div, 100, 0, 200);
      document.getElementById(div).setAttribute('open',1);
      document.getElementById('a'+div).parentNode.className='menuitemsd';
      document.cookie = div+'=0; expires=Thu, 2 Aug 2020 20:47:11 UTC; path=/';
      openedMeni = '';
  }
  return false;
}

function closeOpen(div){
  Slide(openedMeni).up();
  document.getElementById(openedMeni).setAttribute('open',1);
  document.getElementById('a'+openedMeni).parentNode.className='menuitemsd';
  document.cookie = openedMeni+'=0; expires=Thu, 2 Aug 2020 20:47:11 UTC; path=/';
  openedMeni = div;
  document.cookie = div+'=1; expires=Thu, 2 Aug 2020 20:47:11 UTC; path=/';
}

var slideInUse = new Array();

function Slide(objId, options) {
	this.obj = document.getElementById(objId);
	this.duration = 0.8;
        this.height = parseInt(this.obj.style.height);
//        if(!this.height > 0){
//            this.height = parseInt(this.obj.clientHeight);
//        }
        if(typeof options != 'undefined') { this.options = options; } else { this.options = {}; }
	if(this.options.duration) { this.duration = this.options.duration; }
		
	this.up = function() {
		this.curHeight = this.height;
		this.newHeight = '1';
		if(slideInUse[objId] != true) {
			var finishTime = this.slide();
			window.setTimeout("Slide('"+objId+"').finishup("+this.height+");",finishTime);
		}
	}
	
	this.down = function() {
		this.newHeight = this.height;
		this.curHeight = '1';
		if(slideInUse[objId] != true) {
			this.obj.style.height = '1px';
			this.obj.style.display = 'block';
			this.slide();
		}
	}
	
	this.slide = function() {
		slideInUse[objId] = true;
		var frames = 40 * duration; // Running at 30 fps

		var tIncrement = (duration*1000) / frames;
		tIncrement = Math.round(tIncrement);
		var sIncrement = (this.curHeight-this.newHeight) / frames;

		var frameSizes = new Array();
		for(var i=0; i < frames; i++) {
			if(i < frames/2) {
				frameSizes[i] = (sIncrement * (i/frames))*4;
			} else {
				frameSizes[i] = (sIncrement * (1-(i/frames)))*4;
			}
		}
		
		for(var i=0; i < frames; i++) {
			this.curHeight = this.curHeight - frameSizes[i];
			window.setTimeout("document.getElementById('"+objId+"').style.height='"+Math.round(this.curHeight)+"px';",tIncrement * i);
		}
		
		window.setTimeout("delete(slideInUse['"+objId+"']);",tIncrement * i);
		
		if(this.options.onComplete) {
			window.setTimeout(this.options.onComplete, tIncrement * (i-2));
		}
		
		return tIncrement * i;
	}
	
	this.finishup = function(height) {
		this.obj.style.display = 'none';
		this.obj.style.height = height + 'px';
	}
	
	return this;
}




var useBSNns;
if (useBSNns)
{
	if (typeof(bsn) == "undefined")
		bsn = {}
	var _bsn = bsn;
}
else
{
	var _bsn = this;
}





_bsn.Crossfader = function (divs, fadetime, delay )
{	
	this.nAct = -1;
	this.aDivs = divs;
	
	for (var i=0;i<divs.length;i++)
	{
		document.getElementById(divs[i]).style.opacity = 0;
		document.getElementById(divs[i]).style.position = "absolute";
		document.getElementById(divs[i]).style.filter = "alpha(opacity=0)";
		document.getElementById(divs[i]).style.visibility = "hidden";
	}
	
	this.nDur = fadetime;
	this.nDelay = delay;
		
	this._newfade();
}


_bsn.Crossfader.prototype._newfade = function()
{
	if (this.nID1)
		clearInterval(this.nID1);
	
	this.nOldAct = this.nAct;
	this.nAct++;
	if (!this.aDivs[this.nAct])	this.nAct = 0;
	
	if (this.nAct == this.nOldAct)
		return false;
	
	document.getElementById( this.aDivs[this.nAct] ).style.visibility = "visible";
	
	this.nInt = 50;
	this.nTime = 0;
	
	var p=this;
	this.nID2 = setInterval(function() { p._fade() }, this.nInt);
}


_bsn.Crossfader.prototype._fade = function()
{
	this.nTime += this.nInt;
	
	var ieop = Math.round( this._easeInOut(this.nTime, 0, 1, this.nDur) * 100 );
	var op = ieop / 100;
	document.getElementById( this.aDivs[this.nAct] ).style.opacity = op;
	document.getElementById( this.aDivs[this.nAct] ).style.filter = "alpha(opacity="+ieop+")";
	
	if (this.nOldAct > -1)
	{
		document.getElementById( this.aDivs[this.nOldAct] ).style.opacity = 1 - op;
		document.getElementById( this.aDivs[this.nOldAct] ).style.filter = "alpha(opacity="+(100 - ieop)+")";
	}
	
	if (this.nTime == this.nDur)
	{
		clearInterval( this.nID2 );
		
		if (this.nOldAct > -1)
			document.getElementById( this.aDivs[this.nOldAct] ).style.visibility = "hidden";	
		
		var p=this;
		this.nID1 = setInterval(function() { p._newfade() }, this.nDelay);
	}
}



_bsn.Crossfader.prototype._easeInOut = function(t,b,c,d)
{
	return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
}

function opacity(id, opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;

    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function elWidth(div,width){
  document.getElementById(div).style.width=width+'px';
//alert(div+'|'+width+'|'+document.getElementById(div).offsetWidth);
//alert(document.getElementById(div).style.width);
}

function elHeight(div,height){
  document.getElementById(div).style.height=height+'px';
//alert(div+'|'+width+'|'+document.getElementById(div).offsetWidth);
//alert(document.getElementById(div).style.width);
}

function netioAjax() {
  this.xmlhttp = this.createXMLHttp();
  this.response = '';
  this.callback = '';
}

netioAjax.prototype.createXMLHttp = function() {
  if(window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if(window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return req;
}

netioAjax.prototype.ajaxGet = function(url, callback) {
  this.callback = callback;
  var this_obj = this;
  this.xmlhttp.onreadystatechange = function() {
    if(this_obj.xmlhttp.readyState == 4) {
      if(this_obj.xmlhttp.status == 200) {
        this_obj.response = this_obj.xmlhttp.responseText;
        this_obj.callback(this_obj.response);
      } else {
        alert('Napaka:. ('+this_obj.xmlhttp.status+')');
      }
    }
    return true;
  }
  this.xmlhttp.open("GET", url+'&rnd='+Math.random(), true);
  this.xmlhttp.send(null);
  return true;
}


function Updater(url,div,params,indicator){
  this.url = url;
  this.div = div;
  this.params = params;
  this.indicator = indicator;
  this.ajax = new netioAjax();
  var this_obj = this;
  this.update = function(result){
//      var node = document.createElement('div');
//      node.innerHTML = result;
      obj = document.getElementById(this_obj.div);
      obj.innerHTML = '';
//      obj.appendChild(node);
      obj.innerHTML = result;
    }
  if(this.indicator != null){
    document.getElementById(this.indicator).style.display = 'block';
  }
  this.ajax.ajaxGet(url+'/'+params, this.update);
}

function getVal(element){
//alert(document.getElementById(element).value.replace(/\n/g,'|'));
  return encodeURI(document.getElementById(element).value.replace(/\n/g,'|'));
}
function tryNumberFormat(obj)                                                                                                                                                                                    
{                                                                                                                                                                                                                
  if(obj.value != ''){                                                                                                                                                                                           
    n = new NumberFormat();                                                                                                                                                                                      
    n.setInputDecimal(',');                                                                                                                                                                                      
    n.setNumber(obj.value);                                                                                                                                                                                      
    n.setSeparators(true, '.', '.');                                                                                                                                                                             
    obj.value = n.toFormatted();                                                                                                                                                                                 
  }                                                                                                                                                                                                              
}        

function dynamicSelect(id1, id2) {
    if (document.getElementById && document.getElementsByTagName) {
        var sel1 = document.getElementById(id1);
        var sel2 = document.getElementById(id2);
        //selected item with SELECT in option
        var selv = sel2.options[sel2.selectedIndex].value;
        var clone = sel2.cloneNode(true);
        var clonedOptions = clone.getElementsByTagName("option");
        refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
        sel1.onchange = function() {
            refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
        };
        for(var i = 0; i< sel2.options.length; i++){
            if (sel2.options[i].value == selv){
              sel2.options[i].setAttribute("selected","selected");
            }
        }
//                alert(sel2.selectedIndex);
//        sel2.options[3].setAttribute("selected","selected");

    }

}

function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {

    // Delete all options of the dynamic select box
    while (sel2.options.length) {
        sel2.remove(0);
    }
    // Create regular expression objects for "select" and the value of the selected option of the main select box as class names
    var pattern1 = /( |^)(select)( |$)/;
    var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");
    // Iterate through all cloned options
//alert(clonedOptions.length);
    for (var i = 0; i < clonedOptions.length; i++) {
       	// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
        if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
            // Clone the option from the hidden option pool and append it to the dynamic select box
            sel2.appendChild(clonedOptions[i].cloneNode(true));
        }
    }
}
