var fontface = "Verdana";
var fontsize = 2;

var vWinSel;
isNav = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;


function wrt(p_doc, p_text)
{
   p_doc.writeln(p_text);
}

function popup_arrayselect()
{
	/*
		p_item            : Form Item Name to populate (CS list)
		p_array           : Array to be shown
		p_title           : Window Title
		p_prompt          : Prompt at top of window
		p_returntype      : Type of information to return (CS list)
		p_displaytype     : Type of information to display (CS list)
		p_windowoptions   : Override options for the popup window
		p_recordsep       : Text to put between records (CS list)
		p_cellalign       : Alignments for cells (CS list)
		p_cellvalign      : valigns for cells (CS list)
	*/

   // Close any previous popup selection windows (IE...)
   if (!(vWinSel == null))
      vWinSel.close();

   // Grab function arguments
	p_item            = arguments[0];
	p_array           = arguments[1];
	p_title           = arguments[2];
	p_prompt          = arguments[3];
	p_returntype      = arguments[4];
	p_displaytype     = arguments[5];
	p_windowoptions   = arguments[6];
	p_recordsep       = arguments[7];
	p_cellalign       = arguments[8];
	p_cellvalign      = arguments[9];

	// Default argument values...
	if (p_returntype == null || p_returntype.length ==0)
	   p_returntype = 'KEY';
	if (p_displaytype == null || p_displaytype.length == 0)
	   p_displaytype = 'VALUE';
	if (p_windowoptions == null || p_windowoptions.length == 0)
	   p_windowoptions = "width=500,height=300,status=no,scrollbars=yes,resizable=yes,top=200,left=200";
	if (p_recordsep == null || p_recordsep.length == 0)
	   p_recordsep = '';
	if (p_cellalign == null || p_cellalign.length == 0)
	   p_cellalign = '';
	if (p_cellvalign == null || p_cellvalign.length == 0)
	   p_cellvalign = '';

   // Parse out display required
   var arrdispvalues = new Array();
   var tmp = p_displaytype;
   while (tmp.indexOf(',') >= 0)
   {
      arrdispvalues[arrdispvalues.length] = tmp.substr(0, tmp.indexOf(','));
      tmp = tmp.substr(tmp.indexOf(',')+1);
   }
   if (tmp.length)
      arrdispvalues[arrdispvalues.length] = tmp;

   // Parse out return fields required
   var returnfocusfield = '';
   var arrreturnfields = new Array();
   tmp = p_item;
   while (tmp.indexOf(',') >= 0)
   {
      arrreturnfields[arrreturnfields.length] = tmp.substr(0, tmp.indexOf(','));
      tmp = tmp.substr(tmp.indexOf(',')+1);
   }
   if (tmp.length)
      arrreturnfields[arrreturnfields.length] = tmp;
   if (arrreturnfields.length)
      returnfocusfield = arrreturnfields[0];

   // Parse out return values required
   var arrreturnvalues = new Array();
   tmp = p_returntype;
   while (tmp.indexOf(',') >= 0)
   {
      arrreturnvalues[arrreturnvalues.length] = tmp.substr(0, tmp.indexOf(','));
      tmp = tmp.substr(tmp.indexOf(',')+1);
   }
   if (tmp.length)
      arrreturnvalues[arrreturnvalues.length] = tmp;
   while(arrreturnvalues.length < arrreturnfields.length)
      arrreturnvalues[arrreturnvalues.length] = '';

   // Parse out record separators required
   var arrrecsep = new Array();
   tmp = p_recordsep;
   while (tmp.indexOf(',') >= 0)
   {
      arrrecsep[arrrecsep.length] = tmp.substr(0, tmp.indexOf(','));
      tmp = tmp.substr(tmp.indexOf(',')+1);
   }
   if (tmp.length)
      arrrecsep[arrrecsep.length] = tmp;
   while(arrrecsep.length < arrdispvalues.length)
      arrrecsep[arrrecsep.length] = '';

   // Parse out cell alignments required
   var arrcellalign = new Array();
   tmp = p_cellalign;
   while (tmp.indexOf(',') >= 0)
   {
      arrcellalign[arrcellalign.length] = tmp.substr(0, tmp.indexOf(','));
      tmp = tmp.substr(tmp.indexOf(',')+1);
   }
   if (tmp.length)
      arrcellalign[arrcellalign.length] = tmp;
   while(arrcellalign.length < arrreturnfields.length)
      arrcellalign[arrcellalign.length] = 'left';

   // Parse out cell valignments required
   var arrcellvalign = new Array();
   tmp = p_cellvalign;
   while (tmp.indexOf(',') >= 0)
   {
      arrcellvalign[arrcellvalign.length] = tmp.substr(0, tmp.indexOf(','));
      tmp = tmp.substr(tmp.indexOf(',')+1);
   }
   if (tmp.length)
      arrcellvalign[arrcellvalign.length] = tmp;
   while(arrcellvalign.length < arrreturnfields.length)
      arrcellvalign[arrcellvalign.length] = 'middle';

   // Create and configure popup selection window
	vWinSel = window.open("", "popup_arrayselect", p_windowoptions);
	vWinSel.opener = self;

	wrt(vWinSel.document, '<html>');
	wrt(vWinSel.document, '<head>');
	wrt(vWinSel.document, '<link rel="stylesheet" href="app.css" type="text/css">');
   wrt(vWinSel.document, '<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">');
	wrt(vWinSel.document, '<title>' + p_title + '</title>');
	wrt(vWinSel.document, '<head>');

   wrt(vWinSel.document, '<script>');
   wrt(vWinSel.document, 'var formhasloaded = false;');
   wrt(vWinSel.document, 'function FormBlur()');
   wrt(vWinSel.document, '{');
   wrt(vWinSel.document, '  if (formhasloaded)');
   wrt(vWinSel.document, '  {');
   wrt(vWinSel.document, '    this.close();');
   wrt(vWinSel.document, '    self.opener.document.' + returnfocusfield + '.focus();');
   wrt(vWinSel.document, '  }');
   wrt(vWinSel.document, '}');
   wrt(vWinSel.document, 'function FormLoad()');
   wrt(vWinSel.document, '{');
   wrt(vWinSel.document, '  formhasloaded = true;');
   wrt(vWinSel.document, '}');
   wrt(vWinSel.document, 'function FormKeypress()');
   wrt(vWinSel.document, '{');
   if (isIE)
      wrt(vWinSel.document, '  if (event.keyCode == 27)');
   else
      wrt(vWinSel.document, '  if (event.which == 27)');
   wrt(vWinSel.document, '  {');
   wrt(vWinSel.document, '    FormBlur();');
   wrt(vWinSel.document, '  }');
   wrt(vWinSel.document, '}');
   wrt(vWinSel.document, '</script>');

	wrt(vWinSel.document, '<body onBlur="FormBlur();" onLoad="FormLoad();" onKeypress="FormKeypress();">');
	wrt(vWinSel.document, '<font face="' + fontface + '" size="' + fontsize + '">');
	wrt(vWinSel.document, '<table width="100%" border=0>');
	wrt(vWinSel.document, '<tr valign=middle>');
	wrt(vWinSel.document, '<td>');
	wrt(vWinSel.document, '<h2>' + p_prompt + '</h2>');
	wrt(vWinSel.document, '</td>');
	wrt(vWinSel.document, '<td align=right>');
   wrt(vWinSel.document, '[<a href="#" onClick="window.close();self.opener.document.' + returnfocusfield + '.focus();" title="Cancel">Cancel</a>]');
	wrt(vWinSel.document, '</td>');
	wrt(vWinSel.document, '</tr>');
	wrt(vWinSel.document, '<tr><td colspan=2>&nbsp;</td></tr>');
	wrt(vWinSel.document, '<tr><td colspan=2>');
	wrt(vWinSel.document, '<table>');

   // Build Pick list items
   var itemcount = 0;
	for (var aKey in p_array)
	{
	   itemcount++;

      /*
	   retval = aKey;
	   if (p_returntype == 'VALUE')
	      retval = p_array[aKey];
	   */

      wrt(vWinSel.document, '<tr>');

      for (var i=0; i < arrdispvalues.length; i++)
      {
         var dispval = arrdispvalues[i];

         var align = arrcellalign[i];
         var valign = arrcellvalign[i];
         var recsep = '';
         if (itemcount > 1)
            recsep = arrrecsep[i];

         // Link and Content for Link
         var lnk = '';
         var content = dispval;

         if (dispval == 'KEY')
         {
         	content = aKey;

           	lnk = '<a href="#" onClick="window.close();';
            for (var j=0; j < arrreturnfields.length; j++)
            {
               fld = arrreturnfields[j];
               valtype = arrreturnvalues[j];
               val = '';
               if (valtype == 'KEY')
                  val = aKey;
               else if (valtype == 'VALUE')
                  val = p_array[aKey];
               else if (valtype == 'POS' || valtype == 'NUM')
                  val = itemcount;
               else if (valtype.substring(0, 6) == 'VALUE:')
               {
                  var idx = new Number(valtype.substring(6));
                  val = p_array[aKey][idx];
               }

//  line below must escape quotes as htmlspecialchars

      //        val=escape(val);   // done below 23/4/2006

           //    val = val.replace(/'/g,"&quot;");


               lnk += 'self.opener.document.' + fld + '.value=\'' + val + '\';';
            }
            lnk += 'self.opener.document.' + returnfocusfield + '.focus();" title="' + val + '">';
         }

         else if (dispval == 'VALUE')
         {
         	content = p_array[aKey];

           	lnk = '<a href="#" onClick="window.close();';
            for (var j=0; j < arrreturnfields.length; j++)
            {
               fld = arrreturnfields[j];
               valtype = arrreturnvalues[j];
               val = '';
               if (valtype == 'KEY')
                  val = aKey;
               else if (valtype == 'VALUE')
                  val = p_array[aKey];
               else if (valtype == 'POS' || valtype == 'NUM')
                  val = itemcount;
               else if (valtype.substring(0, 6) == 'VALUE:')
               {
                  var idx = new Number(valtype.substring(6));
                  val = p_array[aKey][idx];
               }

        //      val='Injecting variable location 2'               does nothing here

               lnk += 'self.opener.document.' + fld + '.value=\'' + val + '\';';
            }
            lnk += 'self.opener.document.' + returnfocusfield + '.focus();" title="' + val + '">';
         }

         else if (dispval == 'POS' || dispval == 'NUM')
         {
            content = itemcount;
            lnk = '';
         }

         else if (dispval.substring(0, 6) == 'VALUE:')
         {
            var idx = new Number(dispval.substring(6));
         	content = p_array[aKey][idx];

           	lnk = '<a href="#" onClick="window.close();';
            for (var j=0; j < arrreturnfields.length; j++)
            {
               fld = arrreturnfields[j];
               valtype = arrreturnvalues[j];
               val = '';
               if (valtype == 'KEY')
                  val = aKey;
               else if (valtype == 'VALUE')
                  val = p_array[aKey];
               else if (valtype == 'POS' || valtype == 'NUM')
                  val = itemcount;
               else if (valtype.substring(0, 6) == 'VALUE:')
               {
                  var idx = new Number(valtype.substring(6));
                  val = p_array[aKey][idx];
               }

  //    This quote correction below took AGES to work out, both search and replace string


        val=val.replace("&#039;","\\'");


               lnk += 'self.opener.document.' + fld + '.value=\'' + val + '\';';
            }
            lnk += 'self.opener.document.' + returnfocusfield + '.focus();" title="' + val + '">';
         }

         // Write Cell
         var lnkend = '';
         if (lnk.length)
            lnkend = '</a>';

      	wrt(vWinSel.document,
      	      '<td'
      	      + ' align="' + align + '"'
      	      + ' valign="' + valign + '"'
      	      + '>'
      	      + recsep
      	      + lnk
      	      + content
      	      + lnkend
     	         + '</td>'
      	      );
      }

      wrt(vWinSel.document, '</tr>');
   }

   // Complete page
	wrt(vWinSel.document, '</table>');
	wrt(vWinSel.document, '</td></tr>');
	wrt(vWinSel.document, '<tr><td></td></tr>');
	wrt(vWinSel.document, '<tr valign=middle>');
	wrt(vWinSel.document, '<td width=75%>');
	wrt(vWinSel.document, '</td>');
	wrt(vWinSel.document, '<td align=right>');
   wrt(vWinSel.document, '[<a href="#" onClick="window.close();self.opener.document.' + returnfocusfield + '.focus();" title="Cancel">Cancel</a>]');
	wrt(vWinSel.document, '</td>');
	wrt(vWinSel.document, '</tr>');
	wrt(vWinSel.document, '</table>');

	wrt(vWinSel.document, '</body>');
	wrt(vWinSel.document, '</html>');
}

