//Main Helper Functions

var activeObject=null;
var activeDown=null;
var activeWindow= null;
var activeTimeout=0;

var formChanged=false;

var resizeLastX=0;
var resizeLastY=0;
var resizeDiff=0;

window.onscroll = hideAll;
window.onresize=processResize;

document.onreadystatechange=resizePanels;
document.onkeydown = shortCut;
document.onmousedown = hideAll;
document.onclick=clickHandle;

function clearStatus() {
  window.status="";
}

function clickHandle() {  
  sessionTimeout();
  
  var e=event.srcElement; 
  while (e.tagName!="A" && e.tagName!="BODY" && e.parentElement!=null) e=e.parentElement;

  if (e.tagName=="A" && e.href.indexOf("(")>0 && e.href.indexOf("javascript")<0) {
    eval("windowOpen"+e.href.substr(e.href.indexOf("("),1000));  
    return false;
  }
}

function windowOpen(IsWin, PId, Params, URL, wHeight, wWidth, wTop, wLeft) { 
  var goURL="";
  var goId="";
  var SId="";
  var HId="";
  var RId="";
  
  if (PId==null) PId="";
  if (Params==null) Params="";
  if (URL==null) URL="";
  
  if (PId=="" && URL=="") return;
  if (IsWin=="R" && (wHeight==null || wHeight=="")) {wHeight=screen.height-200;wWidth=screen.width/2;};
  if (wHeight==null || wHeight=="") wHeight="600";
  if (wWidth==null || wWidth=="") wWidth="630";  
  if (wTop==null || wTop=="") wTop=(screen.height-wHeight)/2;
  if (wLeft==null || wLeft=="") wLeft=(screen.width-wWidth)/2;
  
  var s=geto("SID");
  if (s!=null) if (s.tagName=="DIV") SId=s.innerText; else SId=s.value;
  
  var r=geto("RID");    
  if (r!=null && Params.indexOf("RID=")<0) if (r.tagName=="DIV") RId="RID="+r.innerText; else RId="RID="+r.value;
  
  if (URL!="")
    goURL=URL;
  else  
  {
    goId=PId+SId+RId+Params;  
    goId=goId.replace(/[^A-Z0-9]/gi,"");
    
    goURL="default.asp?PID="+PId+"&SID="+SId;
    if (Params!="") goURL=goURL+"&"+Params;
    if (RId!="" && RId!="RID=") goURL=goURL+"&"+RId;   
  }  
  if (URL=="" && (IsWin=="W" || (IsWin!="N" && PId.substr(0,3)=="WIZ") || PId.substr(0,3)=="MAP"))
    var newWindow=window.open(goURL, goId, "top="+wTop+",left="+wLeft+",width="+wWidth+",height="+wHeight+",toolbar=0,location=0,directories=0,status=1,titlebar=0,menuBar=0,scrollBars=0,resizable=0");    
  else
  if (URL=="" && IsWin=="Y")
    var newWindow=window.open(goURL, goId, "top="+wTop+",left="+wLeft+",width="+wWidth+",height="+wHeight+",toolbar=0,location=0,directories=0,status=1,titlebar=0,menuBar=0,scrollBars=0,resizable=1");    
  else
  if (URL!="" && IsWin=="W")
    var newWindow=window.open(goURL, goId, "top="+wTop+",left="+wLeft+",width="+wWidth+",height="+wHeight+",toolbar=0,location=0,directories=0,status=1,titlebar=0,menuBar=0,scrollBars=0,resizable=0");
  else
  if (URL!="" && IsWin=="Y" && (wWidth=="max" || wHeight=="max"))
    var newWindow=window.open(goURL, goId, "toolbar=0,location=0,directories=0,status=1,titlebar=1,menuBar=0,scrollBars=1,resizable=1");
  else
  if (URL!="" && IsWin=="Y" && wWidth!="max")
    var newWindow=window.open(goURL, goId, "top="+wTop+",left="+wLeft+",width="+wWidth+",height="+wHeight+",toolbar=0,location=0,directories=0,status=1,titlebar=0,menuBar=0,scrollBars=0,resizable=1");
  else
  if (URL!="" && IsWin=="R")
    var newWindow=window.open(goURL, goId, "top="+wTop+",left="+wLeft+",width="+wWidth+",height="+wHeight+",toolbar=0,location=0,directories=0,status=1,titlebar=0,menuBar=0,scrollBars=1,resizable=1");
  else {
    window.status=goURL;
    window.location=goURL;
  }
}


function windowClose(nochange) {
  
  if (nochange==null || nochange=='N') {
    var c=getv("FORMCHANGED");
    if (formChanged) if (!Prompt("Are you sure you want to discard all changes?")) return;
  
    if (window.opener!=null) try {
      var f=window.opener.document.getElementsByTagName("FORM");
      if (f.length>0 && (c=="Y" || nochange=='N')) f(0).submit();    
    }
    catch(e){}; 
  }  
  if (IsHardware() && nochange=="EXIT") procClient("EXIT",""); else window.close();
}


function hideAll() {
  sessionTimeout();

  if (activeObject!=null && event!=null && event.srcElement!=null) {
    if (event.srcElement!=activeObject && event.srcElement.parentElement!=activeObject && event.srcElement!=activeDown) {
      activeObject.style.display="none";
      activeObject=null;
      activeDown=null;
      
      var cv=geto("hCover");
      if (cv!=null) cv.style.display = "none";      
    }  
  } 
}


function hideObject(item) {
  item.style.display="none";
  var cv=geto("hCover");
  if (cv!=null) cv.style.display = "none";
    
  activeObject=null;
  activeDown=null;
}


function showObject(item, parentitem, parentY, parentX, itemY, itemX, dY, dX, itemH, nohide) {
  var x=0;
  var y=0;
  var bx=0;
  var s=0;
  
  if (item==null || parentitem==null) return;
  item.style.visibility = "visible";
  item.style.display="block";  
  
  var p=parentitem;  
  for(p; p && p.tagName!="BODY"; p = p.offsetParent){x += p.offsetLeft+p.clientLeft; y += p.offsetTop+p.clientTop; s += p.scrollTop;};
  
  if (parentY=="B") y += parentitem.offsetHeight;
  if (itemY=="B")   y -= item.offsetHeight;
  y += parentitem.offsetParent.clientTop + dY - s;
  if (itemH==null) itemH=item.style.pixelHeight;
  if (y+itemH > document.body.clientHeight) y=y-parentitem.offsetHeight-itemH;

  if (parentX=="R") x += parentitem.offsetWidth; 
  if (itemX=="R") { if (item.width!=null) x -= item.width; else x -= item.offsetWidth;};
  x += parentitem.offsetParent.clientLeft + dX;

  item.style.left = x + "px";
  item.style.top =  y + "px";

  if (nohide==null) {
    activeObject=item;
    activeDown=parentitem;  
    var cv=geto("hCover");
    if (cv!=null && item.tagName=="DIV") {
      cv.style.zIndex = 9000;
      cv.style.left=x + "px";
      cv.style.top= y + "px";
      cv.style.width = item.offsetWidth-1;
      cv.style.height = item.offsetHeight-1;
      cv.style.visibility = "visible";
      cv.style.display = "block";
    }  
  }
}


function shortCut() {
  sessionTimeout();
  
  if (event.keyCode==13) try{buttonDefault();} catch(e){};
  if (event.keyCode<48 && event.keyCode!=27 && event.keyCode!=46) return; 
  var pkey=new String();
  var ekey=new String();
  var keycode=event.keyCode;
    
  if (event.ctrlKey) pkey=pkey+"CTRL";
  if (event.altKey) pkey=pkey+"ALT";
  if (event.shiftKey) pkey=pkey+"SHIFT";  
  if (pkey=="" && keycode<112 && keycode!=27) return;
  
  if (keycode>111 && keycode<128) {
    keycode=keycode-111;
    pkey=pkey+"F"+keycode.toString();
  }    
  if (keycode>46 && keycode<112)
    pkey=pkey+String.fromCharCode(keycode);

  if (keycode==27) pkey="ESC";  
  if (keycode==46) pkey=pkey+"DEL";
  if (pkey=="F1") {helpOpen('',getv("PID"));return;};
 
  for (x=0; x<document.all.length; x++)
    if (document.all(x).key!=null && document.all(x).key!="") {
      ekey=document.all(x).key;ekey=ekey.toUpperCase();ekey=ekey.replace("-","");
      if (ekey==pkey) try{
        if (document.all(x).tagName=="INPUT") document.all(x).focus(); 
        if (document.all(x).tagName=="SELECT") comboNext(document.all(x)); 
        if (document.all(x).tagName=="BUTTON" || document.all(x).tagName=="A") document.all(x).click();
        break;  
      }   
      catch(e){};
    }  
}


function hasClassName(el, name) {
  var i, list;
  
  if (el.className==null || el.className=="") return;
  list = el.className.split(" ");
  for (i = 0; i < list.length; i++)
    if (list[i] == name)
      return true;

  return false;
} 


function removeClassName(el, name) {
  var i, curList, newList;
  if (el.className == null) return;

  newList = new Array();
  curList = el.className.split(" ");
  for (i = 0; i < curList.length; i++)
    if (curList[i] != name)
      newList.push(curList[i]);
  el.className = newList.join(" ");
}


function setFocus(iid) {
  var i=geto(iid);
  
  if (i!=null && i.style.display!="none" && !(i.tagName=="INPUT" && i.type=="hidden"))
    window.setTimeout("try{geto('"+iid+"').focus()} catch(e){};",100);
}


function resizePanels() {
  if (document.readyState!="complete") return;
  
  var pm=document.getElementById("panels");
  if (pm==null) return;

  var plf=document.getElementById("panelleftframe");
  var prf=document.getElementById("panelrightframe");
  
  var pf=document.getElementById("footer");
  var pft=0;
  if (pf!=null) pft=pf.offsetHeight;

  var pr=document.getElementById("panelresize");
  if (pr!=null && pr.length!=null) pr=pr[0];

  if (pr!=null && resizeDiff==0) {
    resizeDiff=pm.offsetHeight-pr.offsetHeight;    
  }   

  var pmh=document.documentElement.clientHeight-getTop(pm)-pft; 
  if (pr!=null) { 
    var prh=pmh-resizeDiff;
    if (prh>0) {
      pr.style.height=prh;
      if (pr.scrollHeight>prh) pr.style.overflowY="scroll"; else pr.style.overflowY="auto";
    }  
  }  
  if (pmh>0) {
    if (plf!=null && (pmh-10)>0) plf.style.height=pmh-10; 
    if (prf!=null && (pmh-10)>0) prf.style.height=pmh-10; 
    pm.style.height=pmh;      
  }  

  resizeLastX=document.documentElement.clientWidth;
  resizeLastY=document.documentElement.clientHeight;

  sessionTimeout();
}


function getTop(pr) {
  var p=pr;  
  var prt=0
  for(p; p && p.tagName!="BODY"; p = p.offsetParent){prt += p.offsetTop+p.clientTop;};

  return prt;
}


function processResize() {
  hideAll();
  if (resizeLastX!=document.documentElement.clientWidth || resizeLastY!=document.documentElement.clientHeight)
    resizePanels();  
}  

                     
function IsHardware() {
  if (document.getElementById("HID")==null || document.getElementById("HID").innerText=="") 
    return false;
  else
    return true;
} 


function quickPrint(IsWin, PId, Params, URL) {
  var m=getv("MARK");

  if (m==null || m=="")
    Msg("E","Please select information before printing.","","W");
  else {      
    URL=URL.replace("@MARK", m.value);    
    windowOpen(IsWin, PId, Params, URL);
  }  
} 


function sessionTimeout() {
  if (window.opener!=null) try{window.opener.sessionTimeout();} catch(e){};
  
  var t=geto("HID");
  if (t==null || t.timeout==null || t.timeout=="") return;

  clearTimeout(activeTimeout);
  clearTimeoutMessage();

  activeTimeout=setTimeout("sessionTimeoutAction()",60000*parseFloat(t.timeout))
}


function sessionTimeoutAction() {
  var m=geto("TIMEOUTMSG");
  if (m==null) {
    var m=document.createElement("<div id='TIMEOUTMSG' name='TIMEOUTMSG' class='msgTO' onclick='sessionTimeout()'/>");  
    m.innerHTML="<div class='msgTT'>Session Timeout<br/>Press any key or click here to continue you Session.</div>";
    document.body.appendChild(m);
  }
  m.style.height=document.body.clientHeight;
  m.style.width=document.body.clientWidth;
  m.style.display="block";
  
  if (window.opener==null)
    activeTimeout=setTimeout("windowOpen('','LOGOUT')",30000);
  else
    activeTimeout=setTimeout("windowClose()",28000);    
}


function clearTimeoutMessage() {
  if (geto("TIMEOUTMSG")!=null) geto("TIMEOUTMSG").style.display="none";
}


function geto(id) {
  var e=document.getElementById(id);
  
  if (e==null) {
    var f=document.getElementsByTagName("FORM");
    for (x=0; x<f.length; x++) {
      e=document.getElementById(f[x].id+"xx"+id);
      if (e!=null) break;           
    }  
  }
  return e;
} 


function getvmul(id) {
  var e=document.getElementById(id);
  var ev="";

  if (e==null) {
    var f=document.getElementsByTagName("FORM");
    for (x=0; x<f.length; x++) {
      e=document.getElementById(f[x].id+"xx"+id);
      if (e!=null) break;           
    }  
  }
  if (e!=null) {
    if (e.multiple!=null) {
      for (x=0; x<e.options.length; x++) {
        if (e.options[x].selected) ev=ev+e.options[x].value+" ";
      }
    }     
    else ev=e.value; 
  }
  return trim(ev);
}   


function getv(id) {
  var e=document.getElementById(id);
  var ev="";

  if (e==null) {
    var f=document.getElementsByTagName("FORM");
    for (x=0; x<f.length; x++) {
      e=document.getElementById(f[x].id+"xx"+id);
      if (e!=null) break;           
    }  
  }
  if (e!=null) ev=e.value; 
  return ev;
} 


function getMark() {
  var m=getv("MARK");
  if (m!="") return "MARK="+m; else return "";

}


function trim(s) {
  return s.replace(/^\s+/g, '').replace(/\s+$/g, '').replace(/&nbsp;/gi, '');
}


function Msg(mtype, mtext, maction, mform) {
  if (mtext==null || mtext=="") return; 
  if (mtext.substr(1,1)=="_") {mtype=mtext.substr(0,1); mtext=mtext.substr(2,200);}
  
  if (mform=="W") {
    if (IsHardware()) procClient("MESSAGE",mtype+"|"+mtext); else alert(mtext);
    return;
  }

  var m=null;   
  if (mform!=null)
    m=geto(mform+"xxMSG");
  else {
    var f=document.getElementsByTagName("FORM");
    for (x=0; x<f.length; x++) {
      m=geto(f(x).id+"xxMSG");
      if (m!=null) break;
    }
  }    
  if (m==null)
    alert(mtext);
  else {
    m.className="msg"+mtype;
    m.innerText=mtext;    
  }
  if (maction!=null) eval(maction);
}


function Prompt(mtext) {
  if (mtext==null || mtext=="") return;
  return confirm(mtext);
}

function procClient(procName, procData) {
  if (IsHardware()) 
    window.external.RaiseEvent2(procName, procData)
  else
    Msg("E","Action possible only in Doma System Client application.");  
}

