var helpWin;
var newWin;

var mouse_x = 0;
var mouse_y = 0;


window.updateLast = function (left, top) {
  var win = this;
  // while (win.opener && win.opener.lastPopUpLeft) win = win.opener;
  try {
    win.lastPopUpLeft = left;
    win.lastPopUpTop = top;
  }
  catch (e) { }
};


window.getLast = function () {
  var win = this;
  // while (win.opener && win.opener.lastPopUpLeft) win = win.opener;
  return win.lastPopUpLeft ? [win.lastPopUpLeft, win.lastPopUpTop] : undefined;
};


document.onmousemove = function (e) {
  if (!e) e = window.event;
  mouse_x = e.screenX;
  mouse_y = e.screenY;
};

function over() {
        srcElement=window.event.srcElement;
        srcElement.style.color="rgb(0,0,255)";
}

function out() {
        srcElement=window.event.srcElement;
        srcElement.style.color="rgb(0,0,0)";
}

function scrWidth() {
	nn=(document.layers ? true : false);
	return (nn ? innerWidth : screen.availWidth);
}

function scrHeight() {
	nn=(document.layers ? true : false);
	return (nn ? innerHeight : screen.availHeight);
}

function popUpWId(URL, w, h, id) {
	if (!w) w = 800;
	if (!h) h = 600;
	l=(scrWidth()-w-30)/2;
	t=(scrHeight()-h)/2-18;
	if (! URL.match(/^http/i)) URL += URL.match(/\?/) ? "&popup=1" : "?popup=1";
	
	page = window.open(URL, id, 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=' + w + ',height=' + h + ',left = ' + l + ',top =' +  t); 
	page.focus();
}

function popUpW(URL, w, h) {
	day = new Date();
	id = day.getTime();
	popUpWId(URL,w,h,id);
}

function popUpFull(URL, w, h) {
	day = new Date();
	id = day.getTime();
	l=(scrWidth()-w-30)/2;
	t=(scrHeight()-h)/2-18;
	if (! URL.match(/^http/i)) URL += URL.match(/\?/) ? "&popup=1" : "?popup=1";
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,status=yes,menubar=1,resizable=1,width=' + w + ',height=' + h + ',left = ' + l + ',top =' +  t)");
}

function popUpFullTo(URL, w, h,x,y) {
	day = new Date();
	id = day.getTime();
	l=(scrWidth()-w-30)/2;
	t=(scrHeight()-h)/2-18;
	if (! URL.match(/^http/i)) URL += URL.match(/\?/) ? "&popup=1" : "?popup=1";
	pageid = window.open(URL, id, 'toolbar=1,scrollbars=1,location=1,statusbar=1,status=yes,menubar=1,resizable=1,width=' + w
			+ ',height=' + h + ',left = ' + l + ',top =' + t);
	pageid.moveTo(x,y);	 
}

function popUpFullIdTo(URL, id,w,h,x,y) {
	if (! URL.match(/^http/i)) URL += URL.match(/\?/) ? "&popup=1" : "?popup=1";
	page = window.open(URL, id, 'toolbar=1,scrollbars=1,location=1,statusbar=1,status=yes,menubar=1,resizable=1,width=' + w
			+ ',height=' + h + ',left = ' + x + ',top =' + y);
	page.focus();
}


function popUpJsilent (URL, args) {
  popUpJ(URL, args);
}

function popUpJ (URL, args) {
  if (! args) args = {};

  var day = new Date();
  var id = day.getTime();
  var w = args.w || 1024;
  var h = args.h || 675;
  var l = args.mouse ? mouse_x : (args.x || (scrWidth() - w - 30)/2);
  var t = args.mouse ? mouse_y : (args.y || (scrHeight() - h)/2 - 18);

  // if (args.adjust) { l += args.adjust; t += args.adjust; }
  var last_pos = window.getLast();

  if (last_pos) {
    if (!args.x && !args.mouse) l = last_pos[0] + 20;
    if (!args.y && !args.mouse) t = last_pos[1] + 20;
  }

  window.updateLast(l,t);

  popped = window.open(
    URL,
    id,
    'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=' + w + ',height=' + h + ',left = ' + l + ',top =' +  t
  );

  window.updateLast.call(popped, l, t);

  return popped;
}


function popUpFullScr(URL) {
	day = new Date();
	id = day.getTime();
	w=scrWidth();
	h=scrHeight();
	t=0;
	l=0;
	if (! URL.match(/^http/i)) URL += URL.match(/\?/) ? "&popup=1" : "?popup=1";
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,status=yes,menubar=1,resizable=1,width=' + w + ',height=' + h + ',left = ' + l + ',top =' +  t)");
}

function popUpHelp(fn,w,h,s) {
	l=(scrWidth()-w-63)/2;
	t=(scrHeight()-h)/2;
	URL="help/" + fn + ".html";
	if (helpWin == null || helpWin.closed) {
		helpWin=open(URL,'output','scrollbars=' + s + ',toolbar=0,location=0,statusbar=0,menubar=0,resizable=1,width=' + 
		w + ',height=' + h + ',left = ' + l + ',top =' +  t);
	}
	else {
		helpWin.location.href=URL;
	}
	helpWin.focus();
}

function newWindow(file,w,h,s) {
	if (w>0) {
		l=(scrWidth()-w-63)/2;
	}
	else {
		l=scrWidth();
	}
	if (h>0) {
		t=(scrHeight()-h-100)/2;
	}
	else {
		t=scrHeight();
	}
	newWin=open(file,'output','scrollbars=' + s + 
',toolbar=1,location=0,statusbar=0,menubar=1,resizable=1,width=' + 
		w + ',height=' + h + ',left = ' + l + ',top =' +  t);
	newWin.focus();
}

function closeNewWin() {
	if (newWin != null) {
		newWin.close();
	}
}

function moveNewWin(x, y) {
        try {
                newWin.moveTo(x,y);
        } catch (err) {}
}


function focusNewWin() {
	try {
		newWin.focus();
	} catch(err) {}
}
	

// Combines popUpFull with newWindow, opening two window at once.
function doublePop(url, w, h, url2, w2, h2, s) {
	popUpFull(url, w, h);
	newWindow(url2, w2, h2, s);
}

function newBrowse(file,w,h,s) {
	if (w>0) {
		l=(scrWidth()-w-63)/2;
	}
	else {
		l=scrWidth();
	}
	if (h>0) {
		t=(scrHeight()-h)/2;
	}
	else {
		t=scrHeight();
	}
	newBro=open(file,'output','scrollbars=' + s + ',toolbar=0,location=0,statusbar=0,menubar=0,resizable=1,width=' + 
		w + ',height=' + h + ',left = ' + l + ',top =' +  t);
	newBro.focus();
}

function showtip(current,e,text) {
   if (document.all) {
      thetitle=text.split('<br>')
      if (thetitle.length > 1)
      {
        thetitles=""
        for (i=0; i<thetitle.length-1; i++)
           thetitles += thetitle[i] + "\r\n"
        current.title = thetitles
      } else {
		current.title = text;
	}
   } else if (document.layers) {
       document.tooltip.document.write(
//           '<layer bgColor="#FFFFE7" style="border:1px ' +
 //          'solid black; font-size:12px;color:#000000;">' + text + '</layer>')
	"<DIV CLASS='ttip'>"+text+"</DIV>");
       document.tooltip.document.close();
       document.tooltip.left=e.pageX+5;
       document.tooltip.top=e.pageY+5;
       document.tooltip.visibility="show";
   }
}


function hidetip(){
	if (document.layers) {
		document.tooltip.visibility="hidden";
	}
/*
	else {
		if(document.getElementById) {
			elm.style.visibility="hidden";;
		}
	} 
*/
}

function showElement(e, id) {
	var checked = e.checked ? 'visible' : 'hidden';
	document.getElementById(id).style.visibility = checked;
}


function checkCapsLock (e) {
  if (! e) e = window.event;
  if (! e) return;

  var shift = e.shiftKey ? e.shiftKey : (e.modifiers ? (e.modifiers & 4) : 0);
  var key = e.which ? e.which : (e.keyCode ? e.keyCode : -1);

  if ( (key >= 65 && key <= 90 && !shift) || (key >= 97 && key <= 122 && shift) )
    alert("The CAPS LOCK key is currently pressed.");
} 

