/* window.js
 *
 * Copyright (c) 2006 Heaps of Flavour Pty Ltd
 * $Id: window.js,v 1.16 2009-12-11 02:35:47 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;
/* the cover obscuring the parent window. */
var coverDiv = null;
var parentFocused = false;
var refreshOnClose = 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 && parentFocused) {
         try {
            window.focus();
            parentFocused = false;
         } 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 (arguments.length > 0) {
		refreshOnClose = arguments[0];
	}

   if (!_modal) _initModal();
   _modal.skipcycle = false;
   _modal.focusOnThisWindow();
   
   eventOnObject(window, "unload", makeNonModal);
   eventOnObject(opener, "focus", setParentFocused);
   
   coverDiv = opener.document.createElement('div');
   
   if (opener.document.body != null) {
   	coverDiv.style.height = Math.max(opener.document.documentElement.clientHeight, opener.document.body.clientHeight);
   	coverDiv.style.width = Math.max(opener.document.documentElement.clientWidth, opener.document.body.clientWidth);
   	coverDiv.style.position = 'absolute';
   	coverDiv.style.top = 0;
   	coverDiv.style.left = 0;
   	coverDiv.style.display = 'inline';
   	coverDiv.style.zIndex = 10000;
   
   	opener.document.body.appendChild(coverDiv);
   }
}

/* function to make the current window non-modal */
function makeNonModal() {

   if (_modal) _modal.skipcycle = true;

   eventOffObject(window, "unload", makeNonModal);
   eventOffObject(opener, "focus", setParentFocused);

   if (coverDiv != null) {
      try {
   	   opener.document.body.removeChild(coverDiv);
   	} catch (e) {
   	}
      coverDiv = null;
   }

   if (refreshOnClose) {
       opener.on_submit('success');
   }

}

function setParentFocused() {
	parentFocused = true;
}