//  MadLib.js
//  Written on 2001-12-17 by James Alarie  <jalarie@umich.edu>
//    http://spruce.flint.umich.edu/~jalarie/
//
//  You must create a separate piece of JavaScript code holding the story 
//  itself.  The first line will be used as a title, all lines must be enclosed
//  in quote marks, and every line except the last must end with a comma.  An
//  item to be read from the user or used as an option must be enclosed in square
//  brackets and may have any one of the following formats:
//
//    [prompt]                         display a prompt and input box.
//    [prompt,label]                   display prompt and input box and remember
//                                       the user's response for later reference.
//    [=label]                         no display.  pick up labeled response.
//    [#number]                        no display.  pick up response by number.
//    [(opt-1,opt-2,...,opt-n)]        display a select list.
//    [(opt-1,opt-2,...,opt-n),label]  display select list and remember the user's
//                                       response for later reference.
//    [*comment]                       display this comment.
//    [`comment]                       non-displayed comment.
//    [@option=value]                  set an option.
//
//  Options:
//    [@width=??,height=??]            sets width and height of popup.
//    [@background=filename]           sets the background source file.
//    [@bgcolor=color]                 sets the background color.
//    [@class=classname]               sets the <body> class
//    [@text=color]                    sets the text color.
//    [@link=color]                    sets link color.
//    [@vlink=color]                   sets visited link color.
//    [@alink=color]                   sets active link color.
//
//  Example:
//      MadLib_Lines=new Array(
//        "A Love Story",
//        "[@bgcolor=pink][@text=lime]",
//        "Once upon a time there was a [adjective,beautiful] [noun,princess].  ",
//        "The [=beautiful] [#1] fell in love with a [adjective,handsome] ",
//        "[noun,prince].  The [=beautiful] [=princess] married the [=handsome] ",
//        "[=prince] and they lived [(happily,poorly,richly)] for [time span].",
//        "[*<div class="center">The End<\/div>]"
//      );


        MadLib_background='';                       // default background
        MadLib_bgcolor='#ffffee';                   // ...background color
        MadLib_class='';                            // ...body class
        MadLib_text='black';                        // ...text color
        MadLib_link='blue';                         // ...link color
        MadLib_vlink='#800088';                     // ...visited link color
        MadLib_alink='red';                         // ...active link color
        MadLib_PUOpts='resizable,scrollbars';       // ...popup characteristics
        
        MadLib_Work=new Array();
        MadLib_Names=new Array();
        ixW=-1;
        ixI=-1;

        Out ='';
        Out+='<div class="center">\n';
        Out+='  <form name="MadLib_Form" id="MadLib_Form">\n';
        Out+='    <table border="1" cellspacing="2" cellpadding="2" class="table1">\n';
        Out+='      <caption><font color="red"><b>'+MadLib_Lines[0]+'<\/b><\/font><\/caption>\n';
        Out+='      <tbody>\n';
          document.write(Out);

        for (ix1=1; ix1<MadLib_Lines.length; ix1++) {
          MadLib_Line=MadLib_Lines[ix1];
          if (MadLib_Line == '') {
            MadLib_Line='<\/p><p>\n';
          }
          while ((ix2=MadLib_Line.indexOf('[')) > -1) {
            ix3=MadLib_Line.indexOf(']',ix2);
            if (ix3 > -1) {                         // a ML item exists
              if (ix2 > 0) {                        // leading text exists
                ixW=ixW*1+1;
                MadLib_Work[ixW]=MadLib_Line.substring(0,ix2);
              }
              MadLib_Item=MadLib_Line.substring(ix2+1,ix3);
              MadLib_Line=MadLib_Line.substring(ix3+1);
              MadLib_Code=MadLib_Item.substring(0,1);
              MadLib_Found='no';
              if (MadLib_Code == '#') {             // back-reference by number
                MadLib_Found='yes';
                MadLib_Item=MadLib_Item.substring(1);
                MadLib_Code=MadLib_Item;
                ixW=ixW*1+1;
                MadLib_Work[ixW]='['+MadLib_Code;
              }
              if (MadLib_Code == '=') {             // back-reference by name
                MadLib_Found='yes';
                MadLib_Item=MadLib_Item.substring(1);
                ixW=ixW*1+1;
                for (ix4=0; ix4<MadLib_Names.length; ix4++) {
                  if (MadLib_Names[ix4] == MadLib_Item) {
                    MadLib_Work[ixW]='['+ix4;
                  }
                }
              }
              if (MadLib_Code == '(') {             // a select list
                MadLib_Found='yes';
                MadLib_Item=MadLib_Item.substring(1);
                ixW=ixW*1+1;
                ixI=ixI*1+1;
                ix4=MadLib_Item.indexOf(')');
                MadLib_Name=MadLib_Item.substring(ix4+2);
                MadLib_Item=MadLib_Item.substring(0,ix4);
                if (MadLib_Name != '') {
                  MadLib_Names[ixI]=MadLib_Name;
                }
                MadLib_Options=MadLib_Item.split(',');
                Out ='';
                Out+='        <tr><td align="left">Pick One <\/td>\n';
                Out+='          <td align="left"><select>\n';
                for (ix5=0; ix5<MadLib_Options.length; ix5++) {
                  MadLib_Option=MadLib_Options[ix5];
                  Out+='            <option value="'+MadLib_Option+'">'+MadLib_Option+'<\/option>\n';
                }
                Out+='          <\/select> <\/td><\/tr>\n';
                  document.write(Out);
                MadLib_Work[ixW]='['+ixI;
              }
              if (MadLib_Code == '*') {             // a comment
                MadLib_Found='yes';
                MadLib_Item=MadLib_Item.substring(1);
                Out ='';
                Out+='        <tr>\n';
                Out+='          <td align="left" colspan="2">\n';
                Out+='            '+MadLib_Item+'\n';
                Out+='          <\/td>\n';
                Out+='        <\/tr>\n';
                  document.write(Out);
              }
              if (MadLib_Code == '`') {             // non-displayed comment
                MadLib_Found='yes';
              }
              if (MadLib_Code == '@') {             // an option
                MadLib_Item=MadLib_Item.substring(1);
                MadLib_Option=MadLib_Item.split('=');
                if (MadLib_Option[0] == 'width') {
                  MadLib_Found='yes';
                  MadLib_PUOpts=MadLib_PUOpts+','+MadLib_Item;
                }
                if (MadLib_Option[0] == 'background') {
                  MadLib_Found='yes';
                  MadLib_background=MadLib_Option[1];
                }
                if (MadLib_Option[0] == 'bgcolor') {
                  MadLib_Found='yes';
                  MadLib_bgcolor=MadLib_Option[1];
                }
                if (MadLib_Option[0] == 'class') {
                  MadLib_Found='yes';
                  MadLib_class=MadLib_Option[1];
                }
                if (MadLib_Option[0] == 'text') {
                  MadLib_Found='yes';
                  MadLib_text=MadLib_Option[1];
                }
                if (MadLib_Option[0] == 'link') {
                  MadLib_Found='yes';
                  MadLib_link=MadLib_Option[1];
                }
                if (MadLib_Option[0] == 'vlink') {
                  MadLib_Found='yes';
                  MadLib_vlink=MadLib_Option[1];
                }
                if (MadLib_Option[0] == 'alink') {
                  MadLib_Found='yes';
                  MadLib_alink=MadLib_Option[1];
                }
              }
              if (MadLib_Found == 'no') {           // must be a text item
                MadLib_Found='yes';
                ixW=ixW*1+1;
                ixI=ixI*1+1;
                ix4=MadLib_Item.indexOf(',');
                if (ix4 > -1) {                     // a name was found
                  MadLib_Names[ixI]=MadLib_Item.substring(ix4+1);
                  MadLib_Item=MadLib_Item.substring(0,ix4);
                }
                MadLib_Work[ixW]='['+ixI;
                Out ='';
                Out+='        <tr>\n';
                Out+='          <td align="left">'+MadLib_Item+' <\/td>\n';
                Out+='          <td align="left">\n';
                Out+='            <input type="text" value="?" onfocus="this.select();" \/>\n';
                Out+='          <\/td>\n';
                Out+='        <\/tr>\n';
                  document.write(Out);
              }
            } else {                                // no more ML items on this line
              if (MadLib_Line.length > 0) {
                ixW=ixW*1+1;
                MadLib_Work[ixW]=MadLib_Line;
                MadLib_Line='';
              }
            }
          }
          if (MadLib_Line.length > 0) {
            ixW=ixW*1+1;
            MadLib_Work[ixW]=MadLib_Line;
          }
        }

        Out ='';
        Out+='        <tr>\n';
        Out+='          <td align="center" colspan="2">\n';
        Out+='            <input type="button" value=" Ready " alt="Ready" title="Ready" onclick="MadLib();" \/>&nbsp; \n';
        Out+='            <input type="reset" \/> \n';
        Out+='          <\/td>\n';
        Out+='        <\/tr>\n';
        Out+='      <\/tbody>\n';
        Out+='    <\/table>\n';
        Out+='  <\/form>\n';
        Out+='<\/div><!\-\- center \-\->\n';
          document.write(Out);


        function MadLib() {
          f1=document.forms['MadLib_Form'];         // abbreviation
          MadLib_Values=new Array();
          for (var ix1=0; ix1<f1.length; ix1++) {
            MadLib_Type=f1[ix1].type;
            if (MadLib_Type == 'text') {
              MadLib_Values[ix1]=f1[ix1].value;
            }
            if (MadLib_Type == 'select-one') {
              MadLib_Values[ix1]=f1[ix1].options[f1[ix1].selectedIndex].value;
            }
          }
          
          PopUp1=window.open('','MadLib',MadLib_PUOpts);
          Out ='';
          Out+='<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN"';
          Out+='  "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">';
          Out+='<html xmlns="http:\/\/www.w3.org\/1999\/xhtml" xml:lang="en-US" lang="en-US">';
          Out+='  <head>\n';
          Out+='    <title>MadLib<\/title>\n';
          Out+='    <link href="style1.css" rel="stylesheet" type="text\/css" \/>\n';
          Out+='  <\/head>\n';
          Out+='  <body';
          if (MadLib_background != '') {
            Out+=' background="'+MadLib_background+'"';
          }
          if (MadLib_class != '') {
            Out+=' class="'     +MadLib_class     +'"';
          }
          Out+=' bgcolor="'   +MadLib_bgcolor   +'"';
          Out+=' text="'      +MadLib_text      +'"';
          Out+=' link="'      +MadLib_link      +'"';
          Out+=' vlink="'     +MadLib_vlink     +'"';
          Out+=' alink="'     +MadLib_alink     +'"';
          Out+='>\n';
          Out+='   <div id="body">\n';
          Out+='<div class="center"><h2><font color="red">'+MadLib_Lines[0]+'<\/font><\/h2><\/div>\n';
            PopUp1.document.write(Out);
          Out ='';
          Out+='<p>\n';
            PopUp1.document.write(Out);
          for (ix1=0; ix1<MadLib_Work.length; ix1++) {
            MadLib_Line=MadLib_Work[ix1];
            if (MadLib_Line.substring(0,1) == '[') {
              ix2=MadLib_Line.substring(1)*1;
              Out=MadLib_Values[ix2];
            } else {
              Out=MadLib_Line;
            }
            Out=Out.replace(/  /g,'&nbsp; ');
              PopUp1.document.write(Out);
          }
          Out ='';
          Out+='<\/p>\n';
            PopUp1.document.write(Out);
          Out ='';
          Out+='<div class="center">\n';
          Out+='  [<a href="javascript:void(0);" onclick="window.close();">Close<\/a>]\n';
          Out+='<\/div><!\-\- center \-\->\n';
            PopUp1.document.write(Out);
          Out ='';
          Out+='   <\/div>\n';
          Out+='  <\/body>\n<\/html>\n';
            PopUp1.document.write(Out);
          PopUp1.document.close();
        }
