// Grab browser type
n = (document.layers) ? 1:0
ie = (document.all) ? 1:0

//Globals
var showdiv = "";

var showwidth = 200;
var showheight = 200;
var fullscreen = "N";

var showdiv1 = "<DIV class=show style=\"width:";
var showdiv2 = "px; height:"
var showdiv3 = "px; left:0px; top:0px; visibility=visible;\">";
var showdiv4 = "</DIV>";

var divpattern = /THISDIVNAME/g
var qpattern = /\?/g
var formpattern = /Start of FORM/i
var namepattern = /name=/i
var endformpattern = / /g

count = 0;


//----------------------

function closeform() {

//----------------------

for (i=0;i<arguments.length;i++) {
	if (eval("typeof top.document.getElementById(\'"+arguments[i]+"\')") == "object"
	   && eval("top.document.getElementById(\'"+arguments[i]+"\').innerHTML") != "") {
	   eval("top.document.getElementById(\'"+arguments[i]+"\').innerHTML = \"\"");
  	   eval("top.document.getElementById(\'"+arguments[i]+"\').style.visibility = \"hidden\""); }}

}

//------------------------

function addshowHTML(txt) {

//------------------------

// Set width and height to no more than 
// available screen area

theWidth = document.body.clientWidth; 
theHeight = document.body.clientHeight; 
showleft = showdiv.style.posLeft;
showtop = showdiv.style.posTop;
showwidth = showdiv.style.width.replace("px","");
showheight = showdiv.style.height.replace("px","");

if (theWidth - showleft - showwidth < 0) {
   showdiv.style.width = Math.max(document.body.clientWidth - showdiv.style.posLeft - 120,0); }
if (theHeight - showtop - showheight < 0) {
   showdiv.style.height = Math.max(document.body.clientHeight - showdiv.style.posTop,0); }

showdiv.innerHTML = txt; //'<div id=123><frameset cols=/"3" rows=/"1/"></div>';
increment_zindex(showdiv);

}

//----------------------------

function show(URL,DIVNAME) {

//----------------------------

showinit(DIVNAME);

doAjaxShow(URL,DIVNAME); 
	 
}


//--------------------------

function getshowpos(ptxt) {

//--------------------------

if (ptxt.match("<!--COORDS")) {
	poscookie = ptxt.slice(ptxt.search("<!--COORDS")+10,ptxt.search("-->")); }
else {
	poscookie = "0~0"; }

if (poscookie.match("NOK")) {
	return false; }

// string out the x and y pos
pos_array = poscookie.split("~");

if (pos_array[0] < 0) pos_array[0] = 0;
if (pos_array[1] < 0) pos_array[1] = 0;

//showdiv.style.posLeft = pos_array[0];
//showdiv.style.posTop = pos_array[1];

top.document.getElementById(showdiv.id).style.left = pos_array[0];
top.document.getElementById(showdiv.id).style.top = pos_array[1];

}

//--------------------------

function setshowpos(pdiv) {

//--------------------------
dragit = 0;
pdiv.zIndex = 1;
setDbCookie(pdiv.id,pdiv.style.posLeft,pdiv.style.posTop);
}


//--------------------------------

  function doAjaxShow(URL,DIVNAME) {  
  
//--------------------------------

  if (URL != "") {
     URL = URL.replace(".jsp",".htm");
     if (URL.search(qpattern) > 0) {
        var urlsep = "&"; }
     else {
      var urlsep = "?"; }
  
     URL = URL
     +urlsep
     +"userId="
     +getBrowserCookie("userId")
     +"&tokenValue="
     +getBrowserCookie("tokenValue")
     +"&rand="+Math.random()*5;
     
  }
//alert(URL);

    var req = initRequest();
    req.onreadystatechange = function() {
       if (req.readyState == 4) {
           parseMessages(req, DIVNAME);
       }
    };
    req.open("GET", URL, true);
    req.send(null);
  }
  

//-------------------------------
   
   function initRequest() {

//-------------------------------

       if (window.XMLHttpRequest) {
           return new XMLHttpRequest();
       } else if (window.ActiveXObject) {
           isIE = true;
           return new ActiveXObject("Microsoft.XMLHTTP");
       }
   }
   

//-------------------------------

function parseMessages(req, DIVNAME) {

//-------------------------------

// If showdiv is already populated, just increment zIndex
if (showdiv.innerHTML != "") {
   increment_zindex(showdiv);
   return true; }

// Append tokenValue onto the end of every show URL,
// for use in obtaining and setting cookies etc
// NB: use & separator where parameters already
// included in URL 

var txt = req.responseText;

   addshowHTML(txt); 

       // Set position
       getshowpos(txt);
       DIVNAME.visibility = true;
   
       // Focus the form by stringing out the form name (if there is one)
       var subtxt=showdiv.innerHTML.slice(showdiv.innerHTML.search(formpattern),showdiv.innerHTML.search(formpattern)+1000);
       var subtxt=subtxt.slice(subtxt.search(namepattern),subtxt.search(namepattern)+40);
       var formname=subtxt.slice(5,subtxt.search(endformpattern));
   //    alert('form name = ' + formname + ',' + DIVNAME);
       focus_form(formname);	
    
   }
   

//--------------------------

// Function to set DIV name

//--------------------------
function showinit(p_div) {

if (typeof p_div == "object") {
   showdiv = p_div;}
else {
	 alert("showinit(): Invalid DIV");
	 return false; }   

// Close open LOVDIV and ACTIONDIV
closeform('LOVDIV');
closeform('ACTIONDIV');

//close forms before we open them:
closeform(showdiv.id);

}
