var xmlhttp;
var siirrytaan=0;

window.size = function()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}

window.center = function()
{
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};

	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;

	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}

	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2);

	return{x:_x,y:_y};
}



// check for XPath implementation 
if( document.implementation.hasFeature("XPath", "3.0") ) 
{ 
  // prototying the XMLDocument 
  XMLDocument.prototype.selectNodes = function(cXPathString, xNode) 
  { 
    if( !xNode ) 
      xNode = this;
    var oNSResolver = this.createNSResolver(this.documentElement) 
    var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) 
    var aResult = [];
    for( var i = 0; i < aItems.snapshotLength; i++) 
    { 
      aResult[i] = aItems.snapshotItem(i);
    } 
    return aResult;
  } 

  // prototying the Element 
  Element.prototype.selectNodes = function(cXPathString) 
  { 
    if(this.ownerDocument.selectNodes) 
      return this.ownerDocument.selectNodes(cXPathString, this);
    else
      throw "For XML Elements Only";
  } 
}

// check for XPath implementation 
if( document.implementation.hasFeature("XPath", "3.0") ) 
{ 
  // prototying the XMLDocument 
  XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) 
  { 
    if( !xNode ) 
      xNode = this;
    var xItems = this.selectNodes(cXPathString, xNode);
    if( xItems.length > 0 ) 
      return xItems[0];
    else 
      return null;
  } 
} 

// prototying the Element 
if(!document.all)
{
Element.prototype.selectSingleNode = function(cXPathString) 
{
  if(this.ownerDocument.selectSingleNode) 
    return this.ownerDocument.selectSingleNode(cXPathString, this);
  else 
    throw "For XML Elements Only";
} 
}

if(window.XMLHttpRequest)
  xmlhttp = new XMLHttpRequest();
else if(window.ActiveXObject)
  try 
  {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch(e) 
  {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");    
  }
      
function getElementsByClass(elem, classname) {
  classes = new Array();
  alltags = document.getElementsByTagName(elem);
  for (i=0; i<alltags.length; i++)
  {
    allclasses=alltags[i].className.split(" ");
    for(k=0; k<allclasses.length; k++)
      if (allclasses[k] == classname)
          classes[classes.length] = alltags[i];
//      if (alltags[i].className == classname)
//          classes[classes.length] = alltags[i];
  }
  return classes;
}
  
function help(e,teksti)
{
  var posx = 0;
  var posy = 0;
  if (!e) var e = window.event;
  if (e.pageX || e.pageY)
  {
    posx = e.pageX;
    posy = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
    posx = e.clientX + document.body.scrollLeft;
    posy = e.clientY + document.body.scrollTop;
  }

  he=document.getElementById("help");
  he.innerHTML=teksti;
  he.style.left=posx;
  he.style.top=posy;
  he.style.visibility="visible";
}

function lisaarivi(tablename)
{
  return lisaarivi(tablename,0);
}

function lisaarivi(tablename,notempty)
{
  mybody=document.getElementById(tablename).getElementsByTagName("TBODY").item(0);
  myrow=mybody.getElementsByTagName("TR").item(mybody.getElementsByTagName("TR").length-1);
  myrow=mybody.rows[mybody.rows.length-1];
  if(notempty==undefined)
    notempty=0;
  if(isNaN(notempty))
  {
    inpts=myrow.getElementsByTagName("INPUT");
    for(n=0;n<inpts.length;n++)
      if(inpts[n].name==notempty)
      {
        itm=inpts[n];
        break;
      }
  }
  else
  {
    itm=myrow.getElementsByTagName("INPUT").item(notempty);
  }
  if (itm.value!="")
  {
    newrow=myrow.cloneNode(true);
    inputs=newrow.getElementsByTagName("INPUT");
    for (i=0;i<inputs.length;i++)
      inputs.item(i).value="";
    inputs=newrow.getElementsByTagName("SELECT");
    for (i=0;i<inputs.length;i++)
      inputs.item(i).value="";
    mybody.appendChild(newrow);
    inputs=newrow.getElementsByTagName("INPUT");

    for (i=0;i<inputs.length;i++)
      if(inputs[i].type!='hidden')
      {
//        inputs[i].focus();
        break;
      }
    return newrow;
  }
  else 
    return myrow;
}

function changetab(activepage,activetab,tablist)
{
  tabpages=getElementsByClass('div','tabpage2');
  for(t=0;t<tabpages.length;t++)    
  {
    tabpages[t].style.visibility='hidden';
    tabpages[t].style.display='none';
  }
  document.getElementById(activepage).style.visibility='';
  document.getElementById(activepage).style.display='block';
  tablista=document.getElementById(tablist);
  tabs=tablista.getElementsByTagName('li')
  for(t=0;t<tabs.length;t++)
    tabs[t].style.fontWeight='';
  activetab.style.fontWeight='bold';
//    activetab.style.color='red';
}


function muuta()
{
  document.frmTaso.muutettu.value=1;
}

function siirrysivulle(url,sivu)
{ 
//    document.body.style.backgroundColor='silver';
  if(siirrytaan==0)
  {

    siirrytaan=1;
//    document.body.style.opacity='.5';
    formi=document.frmTaso;
	  if (formi&&formi.muutettu.value==1&&confirm("Tallennetaanko muutokset?"))
	  {
	    formi.uusiurl.value=url;
	    formi.uusisivu.value=sivu;
            if(document.getElementById('nytsivu'))
              formi.tallenna.value=document.getElementById('nytsivu').value;
            else
    	      formi.tallenna.value=1;
	    formi.submit();
	    return false;
	  }
	  else
	    document.location=url;
	}
}  
  
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

function trim( value ) {
	return LTrim(RTrim(value));
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function getAjax(url,func)
{
  if(window.XMLHttpRequest)
    xmlhttp = new XMLHttpRequest();
  else if(window.ActiveXObject)
    try 
    {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) 
    {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");    
    }  
  if(func!="")
    xmlhttp.onreadystatechange = func;
  xmlhttp.open("get",url,true);
  xmlhttp.send(null);    
}

function getElementsByName_iefix(tag, name)
{
  var elem = document.getElementsByTagName(tag);
  var arr = new Array();
  for(i = 0,iarr = 0; i < elem.length; i++)
  {
    att = elem[i].getAttribute("name");
    if(att == name)
    {
      arr[iarr] = elem[i];
      iarr++;
    }
 }
 return arr;
}

function naytadialog(dlg)
{
  dlg.style.display="block";
  cent=window.center({width:dlg.offsetWidth,height:dlg.offsetHeight});
  dlg.style.left=cent.x+'px';
  dlg.style.top=cent.y+'px';
}

function piilotadialog(dlg)
{
  dlg.style.display="none";
}

function showpopup()
{
  if(!xmlhttp)
    return;
  if (xmlhttp.readyState == 4)
  {
    if (xmlhttp.status == 200)
    {
      document.getElementById('popup').innerHTML=xmlhttp.responseText+document.getElementById('closepopup').innerHTML;
    }
  }
}

function htmlpopup(href)
{
  fade();
  href=href+"&IFRAME=1";
  getAjax(href,showpopup);
//  alert(href);
return false;
}

function fade()
{
  document.getElementById("fade").style.display="";
  document.getElementById('popup').style.display='';
}

function unfade()
{
  document.getElementById("fade").style.display="none";
  document.getElementById('popup').style.display='none';
}

  var ropinterval=0;
  var outeraccord=null;
  var contentdiv=null;
  
  function rop(r,maxh)
  {
    if(r.offsetHeight<maxh)
	{
	  plus=r.offsetHeight*2;
	  if(plus>50)
	    plus=50;
	  he=r.offsetHeight+plus;
	  if(he==0)
	    he=1;
	  if(he>maxh)
	    he=maxh;
      r.style.height=he+'px';
	}
	else
	  clearInterval(ropinterval);	
  }
  
  function rcl(r)
  {
    if(r.offsetHeight>20)
	{
	  he=Math.floor((r.offsetHeight-15)/2);
      r.style.height=he+'px';
//	  if(r.offsetHeight<30)
	    //r.style.display='none';
	}
	else
	{
	  r.style.display='none';
	  clearInterval(ropinterval);	
	}
  }
 
  function divaccordion(outer,inner)
  {
	s=document.getElementById(outer);
	t=document.getElementById(inner);
	if(s==null || t==null)
	  return true;
	outeraccord=t;
	hideaccordion();
return true;
	if(s.style.display=='none')
	{
		s.style.height='0px';
		s.style.display='';
		maxh=t.offsetHeight;
		ropinterval=setInterval('rop(s,maxh);',10);
	}
	else
	{
		ropinterval=setInterval('rcl(t);',10);	
	}
  }
  
  function hideaccordion()
  {
    document.body.style.cursor='wait';
	if(outeraccord==null)
	  return true;
	outeraccord.style.display='none';
  }

  function hidecontent()
  {
return true;
    if(contentdiv==null)
      return true;
    contentdiv.style.display='none';
    document.body.style.cursor='wait';
  }
var editorInstance;
var editorText;
var editorDiv;
function muokkaakappale(kap,nappi)
{
  if(nappi.innerHTML=="Muokkaa")
  {
    editorDiv=kap;
    editori=new FCKeditor(kap.id);
    editori.BasePath='/taso/fckeditor/';
    editorText=kap.innerHTML;
    kap.innerHTML=editori.CreateHtml();
    nappi.innerHTML='Tallenna';
  }
  else
  {
    var oEditor = FCKeditorAPI.GetInstance(editorInstance.Name) ;
    editorDiv.innerHTML=oEditor.GetHTML();
    nappi.innerHTML='Muokkaa';
  }
}

function FCKeditor_OnComplete( eI)
{
  editorInstance=eI;
  editorInstance.SetHTML(editorText);
}
