/* window.js
 *
 * Copyright (c) 2006 Heaps of Flavour Pty Ltd
 * $Id: window.js,v 1.12 2007-09-12 06:37:59 steve Exp $
 */

/* new window function */
function NewWindow(strURL, strWindowName, intHeight, intWidth) {

    var resize = 'true';
    var scrollers = 'true';
    if (NewWindow.arguments.length == 5) {
       resize = NewWindow.arguments[4];
    } else if (NewWindow.arguments.length == 6) {
       resize = NewWindow.arguments[4];
       scrollers = NewWindow.arguments[5];
    }

	var objNewWindow;
	var intScrHeight = screen.height;
	var intScrWidth = screen.width;
	var scrollers;
	var intX = (intScrHeight / 2) - (intHeight / 2);
	var intY = (intScrWidth / 2) - (intWidth / 2);

	var winParams = 'toolbar=0,location=0,status=0,menubar=0,width=' + intWidth + ',height=' + intHeight;

	if (resize == "true")
	   winParams += ',resizable=1';
	else
	   winParams += ',resizable=0';

	if (scrollers == "true")
	   winParams += ',scrollbars=1';
	else
	   winParams += ',scrollbars=0';   

	objNewWindow = window.open(strURL, strWindowName, winParams);
	objNewWindow.moveTo(intY, intX);

}

/* private modal object */
var _modal = false;

/* initialise the window and any forms for modality */
function _initModal() {
   _modal = new Object();
   _modal.skipcycle = true; // don't focus until ready

   var el;
   for (var i = 0; i < document.forms.length; i++) {
      for (var j = 0; j < document.forms[i].elements.length; j++) {
         el = document.forms[i].elements[j];
         if (el.type == 'hidden') continue;
         if (el.type == 'button') continue;
         el.modalfocus = el.onfocus;
         el.onfocus = function(event) { _modal.skipcycle = true; if (this.modalfocus) this.modalfocus(event); };
         el.modalmousedown = el.onmousedown;
         el.onmousedown = function(event) { _modal.skipcycle = true; if (this.modalmousedown) this.modalmousedown(event); };
         el.modalblur = el.onblur;
         el.onblur = function(event) { _modal.skipcycle = false; if (this.modalblur) this.modalblur(event); };
      }
   }

   _modal.focusOnThisWindow = function() {
      if (!this.skipcycle) {
         try {
            window.focus();
         } catch (e) {};
      }
      this.timer = setTimeout('_modal.focusOnThisWindow()', 100);
   };
}

function initFocus(el) {
   el.modalfocus = el.onfocus;
   el.onfocus = function(event) { _modal.skipcycle = true; if (this.modalfocus) this.modalfocus(event); };
   el.modalmousedown = el.onmousedown;
   el.onmousedown = function(event) { _modal.skipcycle = true; if (this.modalmousedown) this.modalmousedown(event); };
   el.modalblur = el.onblur;
   el.onblur = function(event) { _modal.skipcycle = false; if (this.modalblur) this.modalblur(event); };
}

/* function to make the current window modal */
function makeModal() {
   if (!_modal) _initModal();
   _modal.skipcycle = false;
   _modal.focusOnThisWindow();
}

/* function to make the current window non-modal */
function makeNonModal() {
   if (_modal) _modal.skipcycle = true;
}

