
//window.attachEvent("onresize", "Adjust"); // IE
//window.onresize=Adjust; // Mozilla/Opera

window.onresize=function() {
  Adjust();
}

ScrollY=0;

function SetScrollPosition(n) {
  if(window.pageYOffset) window.setTimeout("window.scrollTo(0, "+n+");", 200);
  if(document.all) window.setTimeout("document.getElementById('maincontentwrapper').scrollTop="+n+";", 200);
  var n=document.getElementById('maincontentwrapper');
  var s="";
  for(p in n) s+=p+" ";
//  alert(s);
}

function XhtmlFormSubmit(Sender) {
  var p=Sender["__pageScrollY"];
  if(p==null || p==undefined) return;

  if(window.pageYOffset) p.value=window.pageYOffset;
  if(document.all) {
    var m=document.getElementById("maincontentwrapper");
    if(m!=null) p.value=m.scrollTop;
  }
  return true;
}

function DisableBackspace(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

  if(code==8) {
//    alert('back');
    if(window.event) window.event.keyCode = 0;
    e.cancelBubble = true; 
    if (e.stopPropagation) e.stopPropagation();
  }
  if(code==13) { 
//    alert('enter');
    SelectElementToClick(e); 
    if(window.event)
    {
      window.event.keyCode = 0; 
    }
    else {	
      e.cancelBubble = true; 
      if (e.stopPropagation) e.stopPropagation(); 
    }
  }
}

// Enterin painamisen kohdistaminen
// Poista alertit kokonaan, kun toimii varmasti
function SelectElementToClick(e) {
  var items=null;
  var i;
  var debug = false;
  // Tarkastetaan, onko olemassa elementtikohtaista kohdistustietoa
/*  if(window.enterItemsTable)
  {
    if(debug) alert('enteritems');
    if(e.srcElement && e.srcElement.name && window.enterItemsTable[e.srcElement.name])
    {
      if(debug) alert('item: ' + window.enterItemsTable[e.srcElement.name]);
      item = document.getElementById(window.enterItemsTable[e.srcElement.name]);
    }
    if(debug) alert('endofenteritems');
  }
  // Seuraavaksi etsitään koko sivulle määrityttyä kohdistustietoa*/
  if(window.defaultEnterItem)
  {
    if(debug) alert('defaultEnterItem is ' + window.defaultEnterItem);
    items = document.getElementsByName(window.defaultEnterItem);
    if(debug) alert('defaultEnterItem found: ' + items);
  }
  if(items!=null) for(i=0;i<items.length;i++) {
	    if(debug) {var s=''; for(c in items.item(i)) s+= c + '-|-';alert('item exists, properties: ' + s); }
		if(items.item(i).click)
		{
		  if(debug) alert('items.item(i).click: ' + items.item(i).click);
		  items.item(i).click();
		}
  }
  if(debug) alert('endofSelectElementToClick');
}

RefreshInterval=-1;
RefreshCount=-1;
LocalPath="";

function InitPage(RefInterval, RefTime, LocalPath) {
  Adjust();

  if(RefInterval<=0 || RefTime<=0) return;
  RefreshInterval=RefInterval;
  RefreshCount=Math.ceil(RefTime/RefreshInterval);

  window.setTimeout("RefreshSession()", RefreshInterval*1000);
}

function TimeOutMessage(Message, Timeout) {
  window.setTimeout("alert('" + Message + "')", Timeout);
}


function findPosY(obj)
{
	if(obj==null) return null;
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


function Adjust() {

  var cn=document.getElementById("copyrightnotice");
  var tt=document.getElementById("end-of-sidebar");
  if(cn && tt) {
    var y=document.body.clientHeight-35;
    if(y<tt.offsetTop) y=tt.offsetTop;
    cn.style.top=y+"px";
    cn.style.visibility="visible";
  }


  var tb=document.getElementById("TopBar");
  if(tb==null) {
	  if(typeof(PageTemplateCustomAdjust)=="function") PageTemplateCustomAdjust();
	  if(typeof(CustomAdjust)=="function") CustomAdjust();
	  return;
  }

  var mc2=document.getElementById("maincontentwrapper");

  if(mc2!=null) {  // ie
//    var h=tb.clientHeight+findPosY(tb);
    var h=tb.offsetHeight+findPosY(tb);
    mc2.style.height=(document.body.clientHeight-h)+"px";
    mc2.style.top=h+"px";

    var mc=document.getElementById("maincontent");
    if(mc==null) {
	  if(typeof(PageTemplateCustomAdjust)=="function") PageTemplateCustomAdjust();
	  if(typeof(CustomAdjust)=="function") CustomAdjust();
	  return;
    }

    var st=mc.style;
    st.height=(document.body.clientHeight-20-h)+"px";

    if(typeof(PageTemplateCustomAdjust)=="function") PageTemplateCustomAdjust();
	if(typeof(CustomAdjust)=="function") CustomAdjust();


    return;
  }
/*
  var minimizemenu = document.getElementById("MinimizeMenu"); 
  var copyrightnotice = document.getElementById("foobar");

  if(copyrightnotice.offsetTop<minimizemenu.offsetTop) {
//	alert('copy: '+copyrightnotice.offsetTop+', minimize: '+minimizemenu.offsetTop+', erotus: '+(copyrightnotice.offsetTop-minimizemenu.offsetTop)+'px');
	copyrightnotice.style.bottom = (copyrightnotice.offsetTop-minimizemenu.offsetTop)+'px';
  } else {
	copyrightnotice.style.bottom = '0px';
  }

*/

  var h=tb.scrollHeight+0+findPosY(tb);
  var mc=document.getElementById("maincontent");
  if(mc==null) return;

  mc.style.top=h+"px";

	
  if(typeof(CustomAdjust)=="function") CustomAdjust();

  
  

}

HidingTimeoutID=-1;
HidingTimeoutOn=false;
MenuDelayTime=500;

function ScrutinizeMouse(e) {

  var x=0;
  if(e) if(e.pageX) x=e.pageX;
  if(document.all) x=window.event.x;
  if(document.layers) x=e.pageX;

  if(x>170 && !HidingTimeoutOn) {
    HidingTimeoutID=window.setTimeout("ShowSidebarNavigation(false);", MenuDelayTime);
    HidingTimeoutOn=true;
  } else {
    if(x<=170 && HidingTimeoutOn) {
      window.clearTimeout(HidingTimeoutID);
      HidingTimeoutOn=false;
    }
  }
}

function ShowSidebarNavigation(state, delay) {
  var m=document.getElementById("MenuHide");
  m.style.visibility=state ? "" : "hidden";

  if(delay!=null) MenuDelayTime=delay;

  if(state) {
    document.onmousemove=ScrutinizeMouse;
  } else {
    document.onmousemove=null;
    HidingTimeoutOn=false;
    window.clearTimeout(HidingTimeoutID);
  }
}

// used to transmit click from div to link within
function SidebarDivClick(Sender) {
  var c=Sender.childNodes;
  for(var i=0;i<c.length;i++) {
    var n=c[i];
    if(n.nodeName.toLowerCase()!="a") continue;

    var s=n.getAttribute("href");
    if(s=="#") {
      eval(n.getAttribute("onclick"));
    } else {
      eval(s);
    }
  }
}

function ReleaseLock(url,type,id) {

  var x=null;
  if(typeof(ActiveXObject)!="undefined") {
    try {
      x=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      x=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  if(typeof(XMLHttpRequest)!="undefined") {
    x=new XMLHttpRequest();
  }

  x.open("GET", url+"?id="+id+"&type="+type+"&release=1&time="+(new Date()).getTime(), true);
  x.send(null);

}


function UpdateLock(url,type,id,timeout,refreshtime) {

  var x=null;
  if(typeof(ActiveXObject)!="undefined") {
    try {
      x=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      x=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  if(typeof(XMLHttpRequest)!="undefined") {
    x=new XMLHttpRequest();
  }

  x.open("GET", url+"?id="+id+"&type="+type+"&timeout="+timeout+"&time="+(new Date()).getTime(), true);
  x.send(null);

  window.setTimeout("UpdateLock('"+url+"','"+type+"','"+id+"','"+timeout+"','"+refreshtime+"')", refreshtime*1000);


}


function RefreshSession() {
  //if(pollwindow) return;
  RefreshCount--;
  if(RefreshCount<0) return;

  var x=null;
  if(typeof(ActiveXObject)!="undefined") {
    try {
      x=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      x=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  if(typeof(XMLHttpRequest)!="undefined") {
    x=new XMLHttpRequest();
  }

  x.open("GET", LocalPath+"keepalive.aspx?"+(new Date()).getTime(), true);
  x.send(null);

  window.setTimeout("RefreshSession()", RefreshInterval*1000);
}

function confirmDataCardSave(message)
{
var agree=confirm(message);
if (agree)
	return true ;
else
	return false ;
}

function CalculateTextLength(message)
{
	textlength = message.length;
	originaltextlength = message.length;
	var i=0;
	while(i<originaltextlength)
	{
		if(('^{}\[~]|?').indexOf(message.charAt(i))>-1)
		{
			textlength=textlength+1;
		}
		i++;
	}
	return textlength+'';
}
