/*
 *  JavaScript popup window code © 2001-2011, Horus Web Engineering Ltd
 *
 *  $Id: popup.js,v 1.35 2011-07-12 14:51:03 horus Exp $
 *
 *  licensed under the terms of the GNU Lesser General Public License:
 *    http://www.opensource.org/licenses/lgpl-license.php
 *
 *  needs horus.js
 *
 */


horus.winScroll=
  function ( width, height, scroll, xpos, ypos ) {
    var options=[];

    if (width) options.push('width='+width);
    if (height) options.push('height='+height);
    if (xpos!=null) options.push((horus.ie ? 'left=' : 'screenX=')+xpos);
    if (ypos!=null) options.push((horus.ie ? 'top=' : 'screenY=')+ypos);

    if (scroll)
      if (scroll instanceof Array) {
	if (scroll[0]) options.push('scrollbars=yes');
	if (scroll[1]) options.push('resizable=yes');
      } else if (horus.isBoolean(scroll)) {
	if (Boolean.parse(scroll)) options.push('scrollbars=yes,resizable=yes');
      } else {
	if (horus.isString(scroll)) scroll=horus.options(scroll);
	for (var tag in scroll) options.push(tag+'='+scroll[tag]);
      }

    return options.join(',');
  };


horus.placewin=
  function ( name, source, xpos, ypos, width, height, scroll, rethandle ) {
    if (/^opener\./.test(name)) {
      name=name.replace(/^opener\./, '');

      if (opener && opener.horus)
	return opener.horus.placewin
	  (name, source, xpos, ypos, width, height, scroll, rethandle);
	
    }

    var wh=window.open(source, name, horus.winScroll(width, height, scroll, xpos, ypos));
    horus.focus(wh);
    return rethandle ? wh : false;
  };


horus.openwin=
  function ( name, source, width, height, scroll, rethandle ) {
    return horus.placewin(name, source, null, null, width, height, scroll, rethandle);
  };


horus.closewin=
  function ( dest ) {
    if (window.opener && window.opener!=window) {
      if (dest) window.opener.document.location.replace(dest);
      window.close();
    } else if (dest)
      window.document.location.replace(dest);

    return false;
  };


horus.repop=
  function ( name, opener, width, height, scroll ) {
    if (window.name==name) return;
    horus.openwin(name, window.location.pathname, width, height, scroll);
    window.location.replace(opener);
  };


horus.stylesheet=
  function () {
    if (!horus.stylesheet.saved) {
      var links=document.getElementsByTagName('link');
      var css;
      horus.stylesheet.saved=[];

      for (var l=0; l<links.length; l++) {
	var link=links[l];

	if ('stylesheet'.test(link.getAttribute('rel')) &&
	    link.getAttribute('media')!='print' && !link.disabled)
	  horus.stylesheet.saved.push(link.getAttribute('href'));

      }
    }

    return horus.stylesheet.saved;
  };


horus.writepage=
  function ( wh, style, title, content ) {
    var closer, stylesheet, icon, heading;

    if (wh instanceof Array) {
      closer=wh[1];
      wh=wh[0];
    }

    if (style instanceof Array) {
      if (style.length>2) icon=style[2];
      if (style.length>1) stylesheet=style[1];
      style=style[0];
    }

    style=!style ? '' :
      style.left(1)=='#' ? ' id="'+style.right(-1)+'"' : ' class="'+style+'"';

    if (!stylesheet)
      stylesheet=horus.stylesheet();
    else if (!(stylesheet instanceof Array))
      stylesheet=[ stylesheet ];

    if (!icon) icon='/common/horus/icon.png';

    if (title instanceof Array) {
      heading=title[1];
      title=title[0];
    } else
      heading=title;

    wh.document.write
      ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"',
       ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n',
       '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n',
       '<head>\n',
       '<link rel="icon" type="image/png" href="'+icon+'"/>\n',
       '<title>'+title+'</title>\n');

    for (var s=0; s<stylesheet.length; s++)
      wh.document.write
	('<link rel="stylesheet" type="text/css" href="'+stylesheet[s]+'"/>\n');

    if (closer)
      wh.document.write
	('<script type="text/javascript"><!--//--><![CDATA[//><!--\n',
	 'function cw() {if (opener.'+closer+') close(); else setTimeout(cw, 100);} ',
	 'cw();//--><!]]></script>\n');

    wh.document.write('</head>\n<body'+style+'>\n');
    if (heading) wh.document.write('<h1>'+heading+'</h1>\n');

    if (content)
      if (content instanceof Array)
	for (var line=0; line<content.length; line++) wh.document.writeln(content[line]);
      else if (content.match(/^ *<p/i))
	wh.document.writeln(content);
      else
	wh.document.writeln('<p>'+content+'</p>');

    if (arguments.length>4) {
      var label, className, action;
      wh.document.write('<form>');

      for (var ptr=4; ptr<arguments.length; ptr++) {
	label=arguments[ptr];

	if (label instanceof Array) {
	  className=label[2] || 'button';
	  action=label[1];
	  label=label[0];

	  if (action) {
	    if (!/^(opener|window|parent)\./i.test(action)>=0) action='opener.'+action;
	    action+='; ';
	  } else
	    action='';

	} else {
	  className='button';
	  action='';
	}

	wh.document.write
	  ('<input class="'+className+'" type="button" value="'+label+
	   '" onclick="'+action+'window.close()"/>');

      }

      wh.document.write('</form>\n');
    }

    wh.document.write('</body>\n</html>\n');
  };


horus.$waitBox=false;
horus.$waitText='Updating the information being displayed';
horus.$popflag=false;

horus.waitBox=
  function ( openit ) {
    if (openit) {
      var wh=horus.openwin('waitbox', '', 400, 150, [ false, true ], true);
      horus.$popflag=false;

      horus.writepage
        ([ wh, 'horus.$popflag' ], 'waitbox',
	 [ 'Please wait...', 'Please wait a moment...' ],
	 '<p>'+horus.$waitText+'</p>');

    } else
      horus.$popflag=true;

  };


horus.waitText=
  function ( thetext ) {
    horus.$waitText=thetext;
    horus.pageOptions(horus.HTML_WAITBOX, thetext!='');
  };


// draw confirmation box
//
// [buttontext, (keyname, (keyval))]
// [buttontext, [form, (keyname, (keyval))]]
//
// where buttontext can also be [buttontext, action, class]
//
horus.confirmbox=
  function ( style, title, content ) {
    var height=arguments.length>5 ? arguments[5] : 150;
    var width=height instanceof Array ? height[0] : 400;
    if (height instanceof Array) height=height[1];
    var wh=horus.openwin('confirm', '', width, height, true, true);
    var yesin=arguments.length>3 ? arguments[3] : 'yes';
    var yesout=new Array;
    var yeskey='';
    var yesclass='';

    if (yesin instanceof Array) {
      var action=yesin[0];

      if (action instanceof Array) {
	yesout[0]=action[0];
	if (action.length>2) yesout[2]=action[2];
	action=action[1];
      } else {
	yesout[0]=action;
	action='horus.go';
      }

      if (yesin.length>1) {
	yeskey=yesin[1];

	if (yeskey instanceof Array) {
	  yesin=yeskey;

	  if (yesin.length==0)
	    yeskey='';
	  else {
	    yeskey="'"+yesin[0]+"'";
	    if (yesin.length>1) yeskey+=", '"+yesin[1]+"'";
	  }
	} else
	  yeskey="'"+yeskey+"'";

	if (yesin.length>1) {
	  if (yesin.length>2) {
	    var val=yesin[2];
	    if (typeof val=='string') val="'"+val.replace(/([\'\\\"])/g, '\\$1')+"'";
	  } else
	    val='true';

	  yeskey+=", "+val;
	}
      }
    } else
      yesout[0]=yesin;

    var actionsplit=action.match(/^(.*\()(.*)(\).*)$/);

    if (!actionsplit)
      action=action+'('+yeskey+')';
    else if (yeskey!='') {
      action=actionsplit[1];
      if (actionsplit[2].trim()!='') action+=actionsplit[2]+', ';
      action+=yeskey+actionsplit[3];
    }

    yesout[1]=action;
    var noin=arguments.length>4 ? arguments[4] : 'no';
    horus.writepage(wh, style, title, content, yesout, noin);
  };


horus.script.loaded('popup');

