
/*

    Copyright 2003-2007 Purdue University. 

    Developed by: Peter Turbek, Department of Mathematics, Computer Science, and Statistics, Purdue University Calumet.


    This file is part of CaluMath.

    CaluMath is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    CaluMath is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

//Comment: This file begins naming fields contained in the text creation and edit screen and the creation of some objects relating to the menus. //
//Comment: Most of the file is the CaluMath.PM.WriteAndEdit function. This is invoked when the user selects Text and Html in the Main Menu and Text from the submenu. It deals with all aspects of making the text menus visible, putting in default or previous values, and creating the cm_requiredarray and cm_optionsarry for the text objects that are created. //


CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction = function(){
   CaluMath.PM.AssignParents();
   var tempCM_EveryObjectArray=CM_TopWindow.CaluMath.PM.EveryObjectArray;
   var tempCM_EveryObjectArraylength=tempCM_EveryObjectArray.length;
   for(var i=0; i< tempCM_EveryObjectArraylength; i++){
         tempCM_EveryObjectArray[i].cm_caswindow=undefined;
   };
};

CaluMath.PM.AssignCasWindowToTextChildren= function(){

   var tempobject= arguments[0];
   if(tempobject== null){
      return CaluMath.PM.AssignCasWindowToTextChildren(CaluMath.PM.PageMakerObjectArray);
   };
 
   if(tempobject.cm_constructorname != "Array"){
      if((tempobject.type=="htmltext" || tempobject.type=="text" ||  tempobject.type=="coordinates") && tempobject.cm_getsetting("cm_caswindow").length >0){
         var tempcaswindow=tempobject.cm_getsetting("cm_caswindow");
         if(tempobject.childarray != null && tempobject.childarray.length>0){
            for(var i=0; i<tempobject.childarray.length; i++){
               tempobject.childarray[i].cm_caswindow=tempcaswindow;
               CaluMath.PM.AssignCasWindowToTextChildren(tempobject.childarray[i]);
            };
         }
         else{
            return;
         };
      } 
      else if((tempobject.type=="htmltext" || tempobject.type=="text" ||  tempobject.type=="coordinates") && tempobject.cm_caswindow != null){
         var tempcaswindow=tempobject.cm_caswindow; 
         if(tempobject.childarray != null && tempobject.childarray.length>0){
            for(var i=0; i<tempobject.childarray.length; i++){
               tempobject.childarray[i].cm_caswindow=tempcaswindow;
               CaluMath.PM.AssignCasWindowToTextChildren(tempobject.childarray[i]);
            };
         }
         else{
            return;
         };
      }
      else{
         if(tempobject.childarray != null && tempobject.childarray.length>0){
            for(var i=0; i<tempobject.childarray.length; i++){
               CaluMath.PM.AssignCasWindowToTextChildren(tempobject.childarray[i]);
            };
         }
      };
   }
   else{
      if(tempobject.length>0){
         for(var i=0; i<tempobject.length; i++){
            CaluMath.PM.AssignCasWindowToTextChildren(tempobject[i]);
         };
      }
      else{
         return;
      };
   };
};


CaluMath.PM.InPageWriteHtmlString = function(){ 
   //Comment: This is used to when inserting special text into a text field. When this occurs, a new window of CAS Page Maker is opened and the text (with whatever format is required) is entered there. When the user clicks the finish button, the window is closed and the user is returned to the main window (the CM_TopWindow). The text the user constructed is then displayed in the screen for the continuation of the text. //
   var tempobject=arguments[0];
   if(tempobject.cm_constructorname != "Array"){
      if(tempobject.type != "coordinates"){ 
         var tempstring= tempobject.cm_inpageopeningtag() + ' '+tempobject.cm_writehtml() ;
         var childtempstring='';
         if(tempobject.childarray != null && tempobject.childarray.length>0){
            for(var i=0; i< tempobject.childarray.length; i++){
               childtempstring=childtempstring+' '+CaluMath.PM.InPageWriteHtmlString(tempobject.childarray[i]);
            };
         };
         tempstring=tempstring+' '+childtempstring+' '+tempobject.cm_inpageclosingtag();
      }
      else{ 
         var tempdisplay = CaluMath.PM.COORDINATESOPTIONSLIST[CaluMath.PM.ArrayIndex(CaluMath.PM.COORDINATESOPTIONSLIST,tempobject.cm_requiredarray[1][1],1)][0]; 
         var tempevaluation = CaluMath.PM.COORDINATESTYPELIST[CaluMath.PM.ArrayIndex(CaluMath.PM.COORDINATESTYPELIST,tempobject.cm_requiredarray[2][1],1)][0]; 
         tempstring= tempobject.cm_inpageopeningtag() +" "+ tempobject.cm_requiredarray[4][1].replace(/\_/g,"__")+":  "+tempobject.cm_requiredarray[0][1].replace(/\_/g,"__")+" evaluated at:  "+tempobject.cm_requiredarray[3][1].replace(/\_/g,"__")+" style: "+tempdisplay.replace(/\_/g,"__")+" evaluation type: "+tempevaluation.replace(/\_/g,"__")+  tempobject.cm_inpageclosingtag();
      };
   };
   return tempstring;
};

//Comment: This examines a htmltext object, for example, a paragraph or title. These objects contain the settings for any these objects, but the actual textobject is a child in the object's childarray. This function examines if two textobjects are adjacent in the parent's childarray. If they are, they are combined. //
CaluMath.PM.NormalizeTextChildren=function(){
  var tempobject=arguments[0];
  var temparray= new Array();
  var childarray=tempobject.childarray;
  var childarraylengthminus1=childarray.length-1;
  for(var j=0; j<childarraylengthminus1; j++){
     if(childarray[j] != null){
        if(childarray[j].type !="text"){
           temparray=temparray.concat(childarray[j]);
        }
        else if(childarray[j].type=="text" && childarray[j+1].type=="text"){ 
           temparray=temparray.concat(childarray[j]);
           childarray[j].writestring= childarray[j].writestring + " " + childarray[j+1].writestring; 
           childarray[j].alternatewritestring= childarray[j].alternatewritestring + " " + childarray[j+1].alternatewritestring; 
           delete childarray[j+1];
        }
        else{
           temparray=temparray.concat(childarray[j]);
        };
     };
  };
  if(childarray[childarraylengthminus1] != null){
     temparray=temparray.concat(childarray[childarraylengthminus1]);
  };
  return tempobject.childarray=temparray;
};

//Comment: This seems to only be used for htmltext. When the finish button is clicked the htmltext object is deleted from this lists it is part of, so it's entry can be updated, since the information might change.//

CaluMath.PM.DeleteFromListsParsedString=function(){  
  var args0=arguments[0]; 
  if(args0==null){
     var childarray=CaluMath.PM.PageMakerObjectArray;
     CaluMath.PM.PageMakerObjectArray.childarray=CaluMath.PM.PageMakerObjectArray;
     var childarraylength=childarray.length;
     for(var i=0; i<childarraylength; i++){
        CaluMath.PM.DeleteFromListsParsedString(childarray[i]);
     };
  }
  else{
     var tempobject=args0;
     var type=tempobject.type;  
     if(window["CaluMath"]["PM"]["DeleteFromLists"+type] !=null){
        window["CaluMath"]["PM"]["DeleteFromLists"+type](tempobject);
     };
     var childarray=tempobject.childarray;
     var childarraylength=childarray.length;
     for(var i=0; i<childarraylength; i++){
        CaluMath.PM.DeleteFromListsParsedString(childarray[i]);
     };
  }; 
};


//////WRITE AND EDIT FUNCTION/////////////

CaluMath.PM.WriteAndEdit=function(){   
   //Comment CM_InsertCanceled is a Global Variable. If the user is in the middle of an insert and cancels it, InseertCanceled informs the window that opened the insert that the insertion has been cancelled, so that objects normally displayed or constructed as part of the insert should not be created. It is reset to null everytime a new HTML object in CM_TopWindow is chosen to be constructed.
   CaluMath.PM.HidePageControls();

  
   if(window.CM_EditMode==false ){
      window.CM_InsertCanceled=null; 

      //Comment: We immediately construct an object corresponding to the tag. We define CM_CurrentParentOfInsertedObject to be this object. This is so that if something is to be inserted into it, the new window that does the insertion can refer to it.
      var tempobject=  CaluMath.PM.PageMakerObject("", []);

//      CaluMath.PM.HTMLSELECTCHOICES.style.display="block";
      CaluMath.PM.HTMLSELECTCHOICES.style.display="";


      CaluMath.PM.MakeDefaultOption(CaluMath.PM.stylefontSizeSELECT, "");
      CaluMath.PM.MakeDefaultOption(CaluMath.PM.stylefontFamilySELECT, "");
//      CaluMath.PM.MakeDefaultOption(CaluMath.PM.stylebackgroundColorSELECT,"");
//      CaluMath.PM.MakeDefaultOption(CaluMath.PM.stylecolorSELECT, "");
      CaluMath.PM.MakeDefaultOption(CaluMath.PM.stylefontWeightSELECT, "");
      CaluMath.PM.MakeDefaultOption(CaluMath.PM.stylefontStyleSELECT, "");
      CaluMath.PM.MakeDefaultOption(CaluMath.PM.styletextAlignSELECT, "");
      CaluMath.PM.MakeDefaultOption(CaluMath.PM.stylewidthSELECT, "");
      CaluMath.PM.MakeDefaultOption(CaluMath.PM.styledisplaySELECT, "");

      if(CM_TopWindow==window){

         CaluMath.PM.texttagSELECT.style.display="inline";  
         CaluMath.PM.textcaswindowSELECT.style.display="inline";  
         document.getElementById("textcaswindowlabel").style.display="inline";
         CaluMath.PM.textinsertoptionSELECT.style.display="inline";  
         document.getElementById("textinsertoptionlabel").style.display="inline";
         CaluMath.PM.textinserttargetSELECT.style.display="inline"; 
         document.getElementById("textinserttargetlabel").style.display="inline";

         CaluMath.PM.MakeSelectMenu(CaluMath.PM.textinserttargetSELECT, [["n/a", "n/a"]].concat(CaluMath.PM.PlotlistObject.CM_MainWindow.namedobjectlist));
         
         CaluMath.PM.MakeSelectMenu(CaluMath.PM.textcaswindowSELECT,CaluMath.PM.PlotlistObject.newwindowlist); 
         CaluMath.PM.MakeDefaultOption(CaluMath.PM.textcaswindowSELECT, CaluMath.PM.PlotlistObject.newwindowlist[0][0]);

         CaluMath.PM.MakeSelectMenu(CaluMath.PM.textinsertoptionSELECT,[["current point", "n/a"],["insert before", "cm_insertbefore"] ,["insert in", "cm_insertin"],["insert after","cm_insertafter"]], "current point", "Insert Options");
         CaluMath.PM.MakeDefaultOption(CaluMath.PM.textinsertoptionSELECT, "current point");
         CaluMath.PM.MakeDefaultOption(CaluMath.PM.textinserttargetSELECT, "n/a");

         CaluMath.PM.textcaswindowSELECT.onchange=function(){
            CaluMath.PM.MakeSelectMenu(CaluMath.PM.textinserttargetSELECT, [["n/a", "n/a"]].concat(CaluMath.PM.PlotlistObject[CaluMath.PM.textcaswindowSELECT[CaluMath.PM.textcaswindowSELECT.selectedIndex].value].namedobjectlist));
            CaluMath.PM.MakeDefaultOption(CaluMath.PM.textinserttargetSELECT, "n/a");
         };
      }
       else{

         //Comment: In this case window is not Topwindow.
         CaluMath.PM.texttagSELECT.style.display="none";  
         CaluMath.PM.textcaswindowSELECT.style.display="none";  
         document.getElementById("textcaswindowlabel").style.display="none";
         CaluMath.PM.textinsertoptionSELECT.style.display="none";  
         document.getElementById("textinsertoptionlabel").style.display="none";
         CaluMath.PM.textinserttargetSELECT.style.display="none"; 
         document.getElementById("textinserttargetlabel").style.display="none";

      };   

      var rows= 8;

      CM_CurrentParentOfInsertedObject=tempobject;

      //Comment: Recall that CaluMath.PM.namesymbolsSELECT is the TextBox that contains the name of the text that is being constructed.

      if(window==CM_TopWindow){
   
         //Comment: In this case, we don't know what tag they will eventually pick. So we adjust the names as they switch tags. 
         CaluMath.PM.texttagSELECT.onchange=function(){
            var tempselectedvalue= CaluMath.PM.texttagSELECT[CaluMath.PM.texttagSELECT.selectedIndex].value;
            if(tempselectedvalue=="p"){
                  CaluMath.PM.namesymbolsSELECT.value="Paragraph"+CM_TopWindow.CaluMath.PM.ParagraphIndex;
            }
            else if(tempselectedvalue=="div"){
                  CaluMath.PM.namesymbolsSELECT.value="Container"+CM_TopWindow.CaluMath.PM.ContainerIndex;
            }
            else if(tempselectedvalue=="h1"){
                  CaluMath.PM.namesymbolsSELECT.value="Title"+CM_TopWindow.CaluMath.PM.TitleIndex;
            }
            else if(tempselectedvalue=="h2"){
                  CaluMath.PM.namesymbolsSELECT.value="Section"+CM_TopWindow.CaluMath.PM.SectionIndex;
            }
            else if(tempselectedvalue=="span"){ 
                  CaluMath.PM.namesymbolsSELECT.value="Text"+CM_TopWindow.CaluMath.PM.TextIndex;
            };
         };
         CaluMath.PM.texttagSELECT.onchange();  
      }
      else{
         //Comment: In this case, since it is not the top window (and we are not in edit mode) it must be inserted text.
         CaluMath.PM.namesymbolsSELECT.value="Text"+CM_TopWindow.CaluMath.PM.TextIndex;
      };

   
      //Comment: We keep track of the textareas with textareaarray. We construct text objects corresponding to each textarea. When the paragraph is finally constructed, this array is examined to determine the text written in each textarea. This allows the user to make changes in each textarea until the moment it is written.
      var textareaarray= new Array();
      var textareaarrayindex=0;
      var textareaobjectarray= new Array();
      
      //Comment: We keep track of any temporary text or linebreaks that must be written to the window. This will be removed when the creation of the HTML object is complete.
      var junkarray= new Array();
      var textareaarrayindex=0;
      var textareaobjectarray= new Array();
   

      //Comment: This is the first textarea for the user to enter text for the paragraph, title, span, etc. We add it to the textareaarray. 
      var pElement=document.createElement("textarea");
      pElement.setAttribute("rows",rows);
      pElement.setAttribute("cols","120");
      document.getElementById("displayleft").appendChild(pElement);
      textareaarray[textareaarrayindex]=pElement;
      pElement.value=" ";
      
      var p1Element=document.createElement("br");
      document.body.appendChild(p1Element);
      junkarray=junkarray.concat(p1Element);
 
   }  
   else if(window.CM_EditMode==true){ 
  
      var tempobject=arguments[0];

      //Comment: We keep track of these critical original values. Some of them cannot be changed, so we use these values (I think) to make sure the new values are consistent with the old values. 
      tempobject.oldtag=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"tag"); 
      tempobject.oldname=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"name"); 
      tempobject.oldcaswindow=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_caswindow");
      var oldtag = tempobject.oldtag;
      var oldname = tempobject.oldname;
      var oldcaswindow = tempobject.oldcaswindow;
      tempobject.oldinserttarget= CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_inserttarget"); 
      var oldinserttarget=tempobject.oldinserttarget;

      //Comment: I am not sure exactly where the above get used. I still need to use the CaluMath.PM.AssignOldValues routine to correctly do name changes, the above does not work correctly for name changes. However I do not want to get rid of the above, since the internal coding for htmltext objects may depend on it. 
      CaluMath.PM.AssignOldValues(tempobject);

      CM_CurrentParentOfInsertedObject=tempobject;
      if(tempobject.cm_getsetting("cm_caswindow").length >0){
         //Comment: This means it is not a child. So we want all the options displayed. 
         var oldinserttarget=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_inserttarget");
         var oldinsertoption= CaluMath.PM.GetPageMakerObjectSetting(tempobject, "cm_insertoption");

         CaluMath.PM.texttagSELECT.style.display="inline";  
         CaluMath.PM.MakeDefaultOption(CaluMath.PM.texttagSELECT, oldtag, 1);

         CaluMath.PM.textcaswindowSELECT.style.display="inline";  
         document.getElementById("textcaswindowlabel").style.display="inline";
         CaluMath.PM.textinsertoptionSELECT.style.display="inline";  
         document.getElementById("textinsertoptionlabel").style.display="inline";
         CaluMath.PM.textinserttargetSELECT.style.display="inline"; 
         document.getElementById("textinserttargetlabel").style.display="inline";


       
         CaluMath.PM.MakeSelectMenu(CaluMath.PM.textcaswindowSELECT,CaluMath.PM.PlotlistObject.newwindowlist); 
         CaluMath.PM.MakeDefaultOption(CaluMath.PM.textcaswindowSELECT, tempobject.cm_getsetting("cm_caswindow"));

         CaluMath.PM.MakeSelectMenu(CaluMath.PM.textinsertoptionSELECT,[["current point", "n/a"],["insert before", "cm_insertbefore"] ,["insert in", "cm_insertin"],["insert after","cm_insertafter"]]);
         CaluMath.PM.MakeDefaultOption(CaluMath.PM.textinsertoptionSELECT, tempobject.cm_getsetting("cm_insertoption"));
         CaluMath.PM.MakeSelectMenu(CaluMath.PM.textinserttargetSELECT,[["n/a", "n/a"]].concat(CaluMath.PM.PlotlistObject[tempobject.cm_getsetting("cm_caswindow")].namedobjectlist));
         CaluMath.PM.MakeDefaultOption(CaluMath.PM.textinserttargetSELECT, tempobject.cm_getsetting("cm_inserttarget"));

         CaluMath.PM.textcaswindowSELECT.onchange=function(){
            CaluMath.PM.MakeSelectMenu(CaluMath.PM.textinserttargetSELECT, [["n/a", "n/a"]].concat(CaluMath.PM.PlotlistObject[CaluMath.PM.textcaswindowSELECT[CaluMath.PM.textcaswindowSELECT.selectedIndex].value].namedobjectlist));
            CaluMath.PM.MakeDefaultOption(CaluMath.PM.textinsertoptionSELECT, tempobject.cm_getsetting("cm_insertoption"),"value");
            CaluMath.PM.MakeDefaultOption(CaluMath.PM.textinserttargetSELECT, tempobject.cm_getsetting("cm_inserttarget"),"value");
         };
         CaluMath.PM.textcaswindowSELECT.onchange(); 
      }
      else{
         //Comment: So it is a child. 
         CaluMath.PM.texttagSELECT.style.display="none";  
         CaluMath.PM.textcaswindowSELECT.style.display="none";  
         document.getElementById("textcaswindowlabel").style.display="none";
         CaluMath.PM.textinsertoptionSELECT.style.display="none";  
         document.getElementById("textinsertoptionlabel").style.display="none";
         CaluMath.PM.textinserttargetSELECT.style.display="none"; 
         document.getElementById("textinserttargetlabel").style.display="none";
      };
      var childarray=tempobject.childarray;

//      CaluMath.PM.HTMLSELECTCHOICES.style.display="block";
      CaluMath.PM.HTMLSELECTCHOICES.style.display="";
 
      var htmltag= tempobject.type;   
      var rows= 8;
   
      //Comment: This is a way to populate the menus for the font size and color etc. 
      for(var i = 0; i< tempobject.cm_optionsarray.length; i++){ 
         if(tempobject.cm_optionsarray[i][0].match(/SELECT/) && tempobject.cm_optionsarray[i][0].match(/color/i)==null  && window["CaluMath"]["PM"][tempobject.cm_optionsarray[i][0]]!=null){ 
            CaluMath.PM.MakeDefaultOption(window["CaluMath"]["PM"][tempobject.cm_optionsarray[i][0]], tempobject.cm_optionsarray[i][1], "value");
         }
         else if(tempobject.cm_optionsarray[i][0].match(/SELECT/) && tempobject.cm_optionsarray[i][0].match(/color/i) && window["CaluMath"]["PM"][tempobject.cm_optionsarray[i][0]]!=null){ 
            window["CaluMath"]["PM"][tempobject.cm_optionsarray[i][0]].value= tempobject.cm_optionsarray[i][1];
         };
      };

      for(var i = 0; i< tempobject.cm_requiredarray.length; i++){ 
         if(tempobject.cm_requiredarray[i][0].match(/SELECT/)  && window["CaluMath"]["PM"][tempobject.cm_requiredarray[i][0]]!=null){ 
            CaluMath.PM.MakeDefaultOption(window["CaluMath"]["PM"][tempobject.cm_requiredarray[i][0]], tempobject.cm_requiredarray[i][1], "value");
         };
      };


      //Comment: Recall that CaluMath.PM.namesymbolsSELECT is the TextBox that contains the name of the text that is being constructed.
      CaluMath.PM.namesymbolsSELECT.value=CaluMath.PM.GetPageMakerObjectSetting(tempobject, "name")


      CaluMath.PM.AssignParents(tempobject);
      var textareaarray=new Array();
      textareaobjectarray=new Array();
      var junkarray= new Array();
      var childarraylength= childarray.length;
      for(var i=0; i< childarraylength; i++){  
         if(childarray[i].type=="text"){
            textareaobjectarray=textareaobjectarray.concat(childarray[i]);
            var pElement=document.createElement("textarea");
            pElement.setAttribute("cols","120");
            document.getElementById("displayleft").appendChild(pElement);
            //Comment: We make the rows the number of newline characters plus a fraction of the number of characters.
            var tempnumberofrows=CaluMath.PM.GetPageMakerObjectSetting(childarray[i],"text").match(/CM_NEWLINEREPLACEMENT/g);
            if(tempnumberofrows != null){
               tempnumberofrows=tempnumberofrows.length;
            }
            else{
               tempnumberofrows=0;
            };
            pElement.setAttribute("rows",Math.max(3+tempnumberofrows + Math.round((3/2)*pElement.value.length/80),8));
            pElement.value= CaluMath.PM.GetPageMakerObjectSetting(childarray[i],"text").cm_unescapedoublequotes().replace(/CM_SINGLEQUOTE/g, "'").replace(/CM_DOUBLEQUOTE/g, '"').replace(/CM_NEWLINEREPLACEMENT/g,"\n").replace(/CM_BACKSLASH/g, '\\').cm_unreplaceinequalitysymbols();
            pElement.cm_pagemakerobject=childarray[i];
            textareaarray=textareaarray.concat(pElement); 
         }
         else if(childarray[i].type =="htmltext" || childarray[i].type == "text" || childarray[i].type == "coordinates" || childarray[i].type == "h1" || childarray[i].type == "h2" || childarray[i].type == "p" ||  childarray[i].type == "span" || childarray[i].type == "div"){   
            NewString=""; 
            junkarray=junkarray.concat(('<br><span>Special Inserted Object </span>'+CaluMath.PM.InPageWriteHtmlString(childarray[i])+'<br>').cm_html("cm_insertin=displayleft, dojavascript=no"));
         }
         else{
            NewString=""; 
            junkarray=junkarray.concat(('<br><span>Special Inserted Object: a '+childarray[i].type+' with the name '+CaluMath.PM.GetPageMakerObjectSetting(childarray[i],"name")+' </span><br>').cm_html("cm_insertin=displayleft"));
         };
      };
      var textareaarrayindex=textareaarray.length-1;

   }; //Comment: This is the end of CM_EditMode.


   //Comment: This button finishes the creation of paragraph.
   var finishbuttonElement=document.createElement("input");
   finishbuttonElement.setAttribute("type","button");
   if(CM_TopWindow ==window){
      finishbuttonElement.setAttribute("value","Finish Text");
   }
   else{
      finishbuttonElement.setAttribute("value","Finish Inserted Text");
   };
   CaluMath.PM.BUTTONCONTAINER.appendChild(finishbuttonElement);

   //Comment: This is to insert a Special Characters into the paragraph. We create it, but do not display it (it remains hidden) for alerts, since text in an cm_oldalert box is not html text.
   var specialobjectbuttonElement=document.createElement("input");
   specialobjectbuttonElement.setAttribute("type","button");
   specialobjectbuttonElement.setAttribute("value","Insert Special Character");
   CaluMath.PM.specialsymbolsSELECTwithbrbr.style.display="inline";
   CaluMath.PM.BUTTONCONTAINER.insertBefore(specialobjectbuttonElement, CaluMath.PM.specialsymbolsSELECTwithbrbr);

   specialobjectbuttonElement.onclick=function(){
      if(CM_TopWindow.ShowSpecialObjectAlert != "no"){
         CaluMath.Html.NewTempWindow("Select a special symbol from the dropdown menu and click on the text input box. The symbol will appear in the textbox. You can cut and paste the symbol to the exact location that you want.", "checkbox=yes");
      };
      for(var itextarea = 0; itextarea<textareaarray.length; itextarea++){
         textareaarray[itextarea].onfocus=function(){
//            var character = CaluMath.PM.specialsymbolsSELECT.options[CaluMath.PM.specialsymbolsSELECT.selectedIndex].value.replace(/^\s*/g,""); 
            var character = CaluMath.PM.specialsymbolsSELECT.options[CaluMath.PM.specialsymbolsSELECT.selectedIndex].value.replace(/^\s*/g,""); 
            this.value = this.value + character+" ";
            this.blur();
            for(var jtextarea = 0; jtextarea<textareaarray.length; jtextarea++){
               textareaarray[jtextarea].onfocus=CaluMath.Html.NullFunction;
            };
            if(window.instructionmessage=="no"){
               CM_TopWindow.ShowSpecialObjectAlert ="no";
            };
            window.instructionmessage=null;
         };
      };
   };


   //Comment: This is to insert a Special Object into the paragraph. We create it, but do not display it (it remains hidden) for alerts, since text in an cm_oldalert box is not html text.
   var insertmathorformattedtextbuttonElement=document.createElement("input");
   insertmathorformattedtextbuttonElement.setAttribute("type","button");
   insertmathorformattedtextbuttonElement.setAttribute("value","Insert Math or New Formatted Text");
   CaluMath.PM.BUTTONCONTAINER.insertBefore(insertmathorformattedtextbuttonElement,specialobjectbuttonElement);

   //Comment: This button is clicked after the type of inserted object is chosen from a select menu.
   var continueinsertbuttonElement=document.createElement("input");
   continueinsertbuttonElement.setAttribute("type","button");
   continueinsertbuttonElement.setAttribute("value","Continue with Inserted Text");
   CaluMath.PM.BUTTONCONTAINER.appendChild(continueinsertbuttonElement);
   continueinsertbuttonElement.style.display="none";

   //Comment: This button is clicked after the type of inserted object is chosen from a select menu.
   var cancelinsertbuttonElement=document.createElement("input");
   cancelinsertbuttonElement.setAttribute("type","button");
   cancelinsertbuttonElement.setAttribute("value","Cancel Insert");
   CaluMath.PM.BUTTONCONTAINER.appendChild(cancelinsertbuttonElement);
   cancelinsertbuttonElement.style.display="none";


   //Comment: This button finishes the creation of the inserted object.
   var finishinsertbuttonElement=document.createElement("input");
   finishinsertbuttonElement.setAttribute("type","button");
   finishinsertbuttonElement.setAttribute("value","Finish Inserted Text");
   CaluMath.PM.BUTTONCONTAINER.appendChild(finishinsertbuttonElement);
   finishinsertbuttonElement.style.display="none";

   if(window.CM_EditMode==false){
      //Comment: Now that the first textarea for the paragraph has been created, we construct the first text object associated to it.
      var temptextobject=  CaluMath.PM.PageMakerObject("text");
      tempobject.childarray[0]=temptextobject;
      temptextobject.parent=tempobject;
      textareaobjectarray[textareaarrayindex]=temptextobject;
      textareaarray[0].cm_pagemakerobject=temptextobject;

   };

   var coordinatesjunkarray=new Array();  


   //Comment: This is the cancelchanges button. It only appears in  CM_TopWindow in CM_EditMode and cancels whatever changes you made and leaves the original document as it was. It is different from the delete button, which will actually delete whatever you have been working on.
   if(CM_TopWindow==window && CM_TopWindow.CM_EditMode){
      var cancelchangesbuttonElement=document.createElement("input");
      cancelchangesbuttonElement.setAttribute("type","button");
      cancelchangesbuttonElement.setAttribute("value","Cancel Changes to Text");
      CaluMath.PM.BUTTONCONTAINER.appendChild(cancelchangesbuttonElement);

      cancelchangesbuttonElement.onclick=function(){ 
         if(window.opener != null){
            window.opener.CM_InsertCanceled=true;
         };
   
         if(finishbuttonElement!= null && finishbuttonElement.parentNode != null){
            finishbuttonElement.parentNode.removeChild(finishbuttonElement);
         };

         if(deletebuttonElement!= null && deletebuttonElement.parentNode != null){
            deletebuttonElement.parentNode.removeChild(deletebuttonElement);
         };
         if(insertmathorformattedtextbuttonElement!= null && insertmathorformattedtextbuttonElement.parentNode != null){
            insertmathorformattedtextbuttonElement.parentNode.removeChild(insertmathorformattedtextbuttonElement);
         };
         if(continueinsertbuttonElement!= null && continueinsertbuttonElement.parentNode != null){
            continueinsertbuttonElement.parentNode.removeChild(continueinsertbuttonElement);
         };
         if(cancelinsertbuttonElement!= null && cancelinsertbuttonElement.parentNode != null){
            cancelinsertbuttonElement.parentNode.removeChild(cancelinsertbuttonElement);
         };
         if(finishinsertbuttonElement!= null && finishinsertbuttonElement.parentNode != null){
            finishinsertbuttonElement.parentNode.removeChild(finishinsertbuttonElement);
         };
         if(specialobjectbuttonElement!= null && specialobjectbuttonElement.parentNode != null){
            specialobjectbuttonElement.parentNode.removeChild(specialobjectbuttonElement);
         };
         CaluMath.PM.specialsymbolsSELECTwithbrbr.style.display="none";

         if(cancelchangesbuttonElement!= null && cancelchangesbuttonElement.parentNode != null){
            cancelchangesbuttonElement.parentNode.removeChild(cancelchangesbuttonElement);
         };
   
         for(var qqq=0; qqq<textareaobjectarray.length; qqq++){
            if(textareaarray[qqq].parentNode != null){
               textareaarray[qqq].parentNode.removeChild(textareaarray[qqq]);
            };
         };
         for(var qjunkarray=0; qjunkarray<junkarray.length; qjunkarray++){
            if(junkarray[qjunkarray] != null){ 
               if(junkarray[qjunkarray].cm_objecttype=="CaluMath.Html.HtmlBlock"){ 
                  junkarray[qjunkarray].remove();
               }
               else{  
                  if(junkarray[qjunkarray].parentNode!= null){
                     junkarray[qjunkarray].parentNode.removeChild(junkarray[qjunkarray]); 
                  };
               };
            };
         };    

         //Comment: We set all of the options to be blank, so when a different text element is drawn or edited the previous values do not appear.

//         CaluMath.PM.stylecolorSELECT.selectedIndex=0;  
         CaluMath.PM.stylecolorSELECT.value="";  
         CaluMath.PM.stylefontFamilySELECT.selectedIndex=0; 
         CaluMath.PM.stylebackgroundColorSELECT.value="";
         CaluMath.PM.stylefontSizeSELECT.selectedIndex = 0;
         CaluMath.PM.stylefontWeightSELECT.selectedIndex = 0;
         CaluMath.PM.stylefontStyleSELECT.selectedIndex = 0;
         CaluMath.PM.styletextAlignSELECT.selectedIndex = 0;
         CaluMath.PM.styledisplaySELECT.selectedIndex = 0;
         CaluMath.PM.stylewidthSELECT.selectedIndex = 0;
         CaluMath.PM.textcm_onlyforlistsoptionsSELECT.selectedIndex = 0;


         CaluMath.PM.HTMLSELECTCHOICES.style.display="none";
         if(CM_TopWindow != window){
            window.close();
         }
         else{
            CaluMath.PM.InsertionObjectAlerts(tempobject);
            window.scroll(0,-1000);
         };
         CaluMath.PM.DisplayPageControls();

         coordinatesjunkarray.cm_remove();
          if(window.CM_EditMode != true || tempobject.parent == null || !(tempobject.parent.type == "buttonaction" || tempobject.parent.type == "forloop" ||  tempobject.parent.type == "conditional" || tempobject.parent.type == "routine")){
            window.CM_EditMode=false;
          }
          else{
             if(tempobject.parent.parent.makecontrolsvisible != null){
                CaluMath.PM.HidePageControls();

             };
          };
        CM_TopWindow.CM_CurrentParentOfInsertedObject =null;
      };
   };

   //Comment: This button deletes the creation of the HTML object at any point. In CM_EditMode it deletes the element being edited.
   var deletebuttonElement=document.createElement("input");
   deletebuttonElement.setAttribute("type","button");
   deletebuttonElement.setAttribute("value","Delete Text");


   CaluMath.PM.BUTTONCONTAINER.appendChild(deletebuttonElement);

   deletebuttonElement.onclick=function(){ 
      if(window.opener != null){
         window.opener.CM_InsertCanceled=true;
      };

      finishbuttonElement.parentNode.removeChild(finishbuttonElement);
      deletebuttonElement.parentNode.removeChild(deletebuttonElement);
      CaluMath.PM.specialsymbolsSELECTwithbrbr.style.display="none";

      if(insertmathorformattedtextbuttonElement!= null && insertmathorformattedtextbuttonElement.parentNode != null){
         insertmathorformattedtextbuttonElement.parentNode.removeChild(insertmathorformattedtextbuttonElement);
      };
      if(continueinsertbuttonElement!= null && continueinsertbuttonElement.parentNode != null){
         continueinsertbuttonElement.parentNode.removeChild(continueinsertbuttonElement);
      };
      if(finishinsertbuttonElement!= null && finishinsertbuttonElement.parentNode != null){
         finishinsertbuttonElement.parentNode.removeChild(finishinsertbuttonElement);
      };
      if(specialobjectbuttonElement!= null && specialobjectbuttonElement.parentNode != null){
         specialobjectbuttonElement.parentNode.removeChild(specialobjectbuttonElement);
      };

      if(cancelchangesbuttonElement!= null && cancelchangesbuttonElement.parentNode != null){
         cancelchangesbuttonElement.parentNode.removeChild(cancelchangesbuttonElement);
      };

         if(finishbuttonElement != null){
            finishbuttonElement.onclick = null;
         };

         if(deletebuttonElement!= null ){
            deletebuttonElement.onclick= null 
         };
         if(insertmathorformattedtextbuttonElement!= null){
            insertmathorformattedtextbuttonElement.onclick = null
         };
         if(continueinsertbuttonElement!= null){
            continueinsertbuttonElement.onclick= null
         };
         if(cancelinsertbuttonElement!= null ){
            cancelinsertbuttonElement.onclick = function(){
               this.style.display="none";
            }; 
         };
         if(finishinsertbuttonElement!= null ){
            finishinsertbuttonElement.onclick = function(){
               this.style.display="none";
            }; 
         };
         if(specialobjectbuttonElement!= null ){
            specialobjectbuttonElement.onclick= null; 
         };

         if(cancelchangesbuttonElement!= null){
            cancelchangesbuttonElement.onclick=null; 
         };
         if(window.CaluMath.PM.TEXTTEMPINSERTWINDOW !=null){
            CaluMath.PM.TEXTTEMPINSERTWINDOW=null;
         };

      for(var qqq=0; qqq<textareaobjectarray.length; qqq++){
         if(textareaarray[qqq].parentNode != null){
            textareaarray[qqq].parentNode.removeChild(textareaarray[qqq]);
         };
      };
      for(var qjunkarray=0; qjunkarray<junkarray.length; qjunkarray++){
         if(junkarray[qjunkarray] != null){ 
            if(junkarray[qjunkarray].cm_objecttype=="CaluMath.Html.HtmlBlock"){ 
               junkarray[qjunkarray].remove();
            }
            else{  
               if(junkarray[qjunkarray].parentNode!= null){
                  junkarray[qjunkarray].parentNode.removeChild(junkarray[qjunkarray]); 
               };
            };
         };
      };    

      //Comment: We set all of the options to be blank, so when a different text element is drawn or edited the previous values do not appear.
//      CaluMath.PM.stylecolorSELECT.selectedIndex=0;  
      CaluMath.PM.stylecolorSELECT.value="";  
      CaluMath.PM.stylefontFamilySELECT.selectedIndex=0; 
      CaluMath.PM.stylebackgroundColorSELECT.value="";
      CaluMath.PM.stylefontSizeSELECT.selectedIndex = 0;
      CaluMath.PM.stylefontWeightSELECT.selectedIndex = 0;
      CaluMath.PM.stylefontStyleSELECT.selectedIndex = 0;
      CaluMath.PM.styletextAlignSELECT.selectedIndex = 0;
      CaluMath.PM.styledisplaySELECT.selectedIndex = 0;
      CaluMath.PM.stylewidthSELECT.selectedIndex = 0;
      CaluMath.PM.textcm_onlyforlistsoptionsSELECT.selectedIndex = 0;

      CaluMath.PM.HTMLSELECTCHOICES.style.display="none";
      if(CM_TopWindow != window){
         window.close();
         window.finishinsertbuttonElement.onclick();
      };
      CaluMath.PM.DisplayPageControls();

      if(CM_TopWindow ==window && window.CM_EditMode){ 
         CaluMath.PM.AssignParents();
         var parent=tempobject.parent;
         var tempchildindex=CaluMath.PM.AssignChildIndex(tempobject);
         if(parent==null){
            parent=CaluMath.PM.PageMakerObjectArray;
            var childarray = CaluMath.PM.PageMakerObjectArray;
         }
         else{
            var childarray= parent.childarray;
         };
         childarray.splice(tempchildindex,1);   
         if(parent==CaluMath.PM.PageMakerObjectArray){
            CaluMath.PM.PageMakerObjectArray=childarray;
         }
         else{
            parent.childarray=childarray;
            CaluMath.PM.NormalizeTextChildren(parent);
         };
      };


      //Comment: Possibly we need to add new window to this?
      if(window.CM_EditMode != true || tempobject.parent == null ||  !( tempobject.parent.type == "buttonaction" || tempobject.parent.type == "forloop" || tempobject.parent.type == "conditional" || tempobject.parent.type == "routine")){
         window.CM_EditMode=false;
      }
      else{
         if(tempobject.parent.parent.makecontrolsvisible != null){
            CaluMath.PM.HidePageControls(); 

         };
      };
      CM_TopWindow.CM_CurrentParentOfInsertedObject =null;
      if(CM_TopWindow==window){
         if(window.CM_EditMode != true){
//            var tempalertwindow=alert("Your Text has been canceled and will not be added to your web page.");
         }
         else{
//            var tempalertwindow=alert("Your Text has been deleted from your web page.");
         };
         CaluMath.PM.InsertionObjectAlerts(tempobject);
         window.scroll(0,-1000);
      };
   };  //Comment: End of deletebutton.
 
      if(textareaarray != null && textareaarray[textareaarrayindex] != null){ 
         textareaarray[textareaarrayindex].focus();
      };

//   CM_TopWindow.location.hash= "#endofdocument"; 
     window.location.hash= "#endofdocument"; 



   finishbuttonElement.onclick=function(){

      if(CM_TopWindow.CM_EditMode && CM_TopWindow==window){
         CaluMath.PM.DeleteFromListsParsedString(tempobject);
      };

      if(CaluMath.PM.texttagSELECT.style.display=="inline"){
         var selectedvalue= CaluMath.PM.texttagSELECT[CaluMath.PM.texttagSELECT.selectedIndex].value;
         tempobject.type="htmltext";
         var htmltag= selectedvalue;   
      }
      else{
         tempobject.type="htmltext";
         var htmltag= "span";   
      };

      if(htmltag =="p"){
         var selectedlabel="Paragraph";
      }
      else if(htmltag =="div"){
         var selectedlabel="Container";
      }
      else if(htmltag =="h1"){
         var selectedlabel="Title";
      }
      else if(htmltag =="h2"){
         var selectedlabel="Section Heading";
      }
      else if(htmltag =="span"){
         var selectedlabel="Formatted Text";
      }
      else{
         alert("THIS SHOULD BE CORRECTED. IN EDITMODE OF THE GLOBAL CaluMath.PM.WriteAndEdit FUNCTION, THERE IS NO SELECTEDLABEL OF YOUR HTMLTAG "+tempobject.type); 
      };
     
      if(CM_TopWindow==window  && !CM_TopWindow.CM_EditMode){
         if(selectedvalue=="p"){
            CaluMath.PM.ParagraphIndex++;
         }
         else if(selectedvalue=="div"){
            CaluMath.PM.ContainerIndex++;
         }
         else if(selectedvalue=="h1"){
            CaluMath.PM.TitleIndex++;
         }
         else if(selectedvalue=="h2"){
            CaluMath.PM.SectionIndex++;
         }
         else if(selectedvalue=="span"){ 
            CaluMath.PM.TextIndex++;
         };
         CaluMath.PM.TextIndex++;
      };

     //Comment: We want them to have some flexibility to change the names of the text objects they create. However, these names are used in lists and the names are used when a math formula ("coordinates") is inserted into the text. In this case, the name cannot be changed because there is no way to associate the math formula with the new name to ensure it is put in the correct place. Therefore, we only allow them to change the name of the text object if it does not contain any math formulas. 
     //Comment: We check to see if the text object contains any math formulas. 


      var selectedname=CaluMath.PM.htmloptionnamebox.cm_node.value.replace(/\s*/g ,"");


//      var selectedcolor=CaluMath.PM.stylecolorSELECT.options[CaluMath.PM.stylecolorSELECT.selectedIndex].value;  
      var selectedcolor=CaluMath.PM.stylecolorSELECT.value;  
      var selectedfontfamily=CaluMath.PM.stylefontFamilySELECT.options[CaluMath.PM.stylefontFamilySELECT.selectedIndex].value;
      var selectedbackgroundcolor=CaluMath.PM.stylebackgroundColorSELECT.value; 
      var selectedfontsizes=CaluMath.PM.stylefontSizeSELECT.options[CaluMath.PM.stylefontSizeSELECT.selectedIndex].value;
      var selectedbold=CaluMath.PM.stylefontWeightSELECT.options[CaluMath.PM.stylefontWeightSELECT.selectedIndex].value;
      var selecteditalics=CaluMath.PM.stylefontStyleSELECT.options[CaluMath.PM.stylefontStyleSELECT.selectedIndex].value;
      var selectedalignment=CaluMath.PM.styletextAlignSELECT.options[CaluMath.PM.styletextAlignSELECT.selectedIndex].value;
      var selecteddisplay=CaluMath.PM.styledisplaySELECT.options[CaluMath.PM.styledisplaySELECT.selectedIndex].value;
      var selectedwidth=CaluMath.PM.stylewidthSELECT.options[CaluMath.PM.stylewidthSELECT.selectedIndex].value;
      var selectedtextcm_onlyforlistsoptions=CaluMath.PM.textcm_onlyforlistsoptionsSELECT.options[CaluMath.PM.textcm_onlyforlistsoptionsSELECT.selectedIndex].value;
    
      //Comment: We set all of the options to be blank, so when a different text element is drawn or edited the previous values do not appear.
//      CaluMath.PM.stylecolorSELECT.selectedIndex=0;  
      CaluMath.PM.stylecolorSELECT.value="";  
      CaluMath.PM.stylefontFamilySELECT.selectedIndex=0; 
      CaluMath.PM.stylebackgroundColorSELECT.value="";
      CaluMath.PM.stylefontSizeSELECT.selectedIndex = 0;
      CaluMath.PM.stylefontWeightSELECT.selectedIndex = 0;
      CaluMath.PM.stylefontStyleSELECT.selectedIndex = 0;
      CaluMath.PM.styletextAlignSELECT.selectedIndex = 0;
      CaluMath.PM.styledisplaySELECT.selectedIndex = 0;
      CaluMath.PM.stylewidthSELECT.selectedIndex = 0;
      CaluMath.PM.textcm_onlyforlistsoptionsSELECT.selectedIndex = 0;


      if(CM_TopWindow==window && CaluMath.PM.textcaswindowSELECT.style.display != "none" && CaluMath.PM.textcaswindowSELECT.selectedIndex>=0){
         var selectedtextcaswindow=CaluMath.PM.textcaswindowSELECT.options[CaluMath.PM.textcaswindowSELECT.selectedIndex].value;
      }
      else{
         var selectedtextcaswindow='';
      };

      if(CM_TopWindow==window && CaluMath.PM.textinsertoptionSELECT.style.display != "none"){
         var selectedtextinsertoption=CaluMath.PM.textinsertoptionSELECT.options[CaluMath.PM.textinsertoptionSELECT.selectedIndex].value;
         var selectedtextinserttarget=CaluMath.PM.textinserttargetSELECT.options[CaluMath.PM.textinserttargetSELECT.selectedIndex].value;
         //Comment: This part checks that they are not inappropriately inserting inside or before or after an object.
         if(selectedtextinsertoption != "n/a" && selectedtextinserttarget != "n/a"){
            var tempobjecttype = CaluMath.PM.PlotlistObject[selectedtextcaswindow].namedobjectlist[CaluMath.PM.ArrayIndex(CaluMath.PM.PlotlistObject[selectedtextcaswindow].namedobjectlist,selectedtextinserttarget,1)][2].type; 
            if(selectedtextinsertoption=="cm_insertin" && (CaluMath.PM.CannotInsertInArray.cm_contains(tempobjecttype) || tempobjecttype.match(/button/))){
               return alert("You cannot insert your object inside a "+CaluMath.PM.PageMakerObjectLabelName(tempobjecttype));
            };
            if((selectedtextinsertoption=="cm_insertbefore" || selectedtextinsertoption=="cm_insertafter") && (CaluMath.PM.CannotInsertAfterArray.cm_contains(tempobjecttype)) ){
               return alert("You cannot insert your object before or after a "+CaluMath.PM.PageMakerObjectLabelName(tempobjecttype));
            };
         };

      }
      else{
         var selectedtextinsertoption='';
         var selectedtextinserttarget='';
      };
      if(CaluMath.PM.texttagSELECT.style.display=="inline"){
         var selectedtexttag=CaluMath.PM.texttagSELECT[CaluMath.PM.texttagSELECT.selectedIndex].value;
      }
      else{
         var selectedtexttag="span";
      };

         var tempoptionsarray=[["stylecolorSELECT", selectedcolor],["stylefontFamilySELECT",selectedfontfamily], ["stylebackgroundColorSELECT",selectedbackgroundcolor], ["stylefontSizeSELECT",selectedfontsizes], ["stylefontWeightSELECT",selectedbold], ["stylefontStyleSELECT", selecteditalics], ["styletextAlignSELECT", selectedalignment], ["styledisplaySELECT", selecteddisplay],  ["stylewidthSELECT", selectedwidth], ["cm_caswindow", selectedtextcaswindow], ["cm_insertoption", selectedtextinsertoption], ["cm_inserttarget", selectedtextinserttarget], ["cm_onlyforlists", selectedtextcm_onlyforlistsoptions]]; 
         var temprequiredarray =[["tag", selectedtexttag],  ["name", selectedname]];      
 

      tempobject.cm_optionsarray=CaluMath.PM.CopyArray(tempoptionsarray);
      tempobject.cm_requiredarray=CaluMath.PM.CopyArray(temprequiredarray);


         for(var qqq=0; qqq<textareaobjectarray.length; qqq++){
            temptextobject = textareaobjectarray[qqq];
            temptextobject.cm_requiredarray= CM_TopWindow.Array();
            temptextobject.cm_requiredarray[0]= CM_TopWindow.Array();
            temptextobject.cm_requiredarray[0][0]="text";
            temptextobject.cm_requiredarray[0][1]=textareaarray[qqq].value.cm_escapebackslashes().replace(/\'/g, "CM_SINGLEQUOTE").replace(/\"/g, "CM_DOUBLEQUOTE").cm_replaceendoflinecharacters().cm_replaceinequalitysymbols().replace(/\&lt\;\s*br\s*\&gt\;/g,"<br>");
            textareaarray[qqq].parentNode.removeChild(textareaarray[qqq]);
         };

      if(window.CM_EditMode==false){ 

         if(CM_TopWindow==window){
            var tempinsertoption=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_insertoption");
            var tempinserttarget=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_inserttarget");
            var tempcaswindow=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_caswindow");
            if(CaluMath.PM.AutomaticallyPlaceObjectAsChild(tempobject)=="no" ){  
               CaluMath.PM.PageMakerObjectArray=CaluMath.PM.PageMakerObjectArray.concat(tempobject);
            };
         }
         else if(CM_TopWindow != window){
            CaluMath.PM.PageMakerObjectArray=CaluMath.PM.PageMakerObjectArray.concat(tempobject);
            if(!window.CM_InEditMode){ 
               if(window.opener != null){
                  var CM_PageMakerObjectArraylength=CaluMath.PM.PageMakerObjectArray.length;
                  for(var t=0; t<CM_PageMakerObjectArraylength; t++){
                     window.opener.CM_CurrentParentOfInsertedObject.childarray= window.opener.CM_CurrentParentOfInsertedObject.childarray.concat(CaluMath.PM.PageMakerObjectArray[t]);
                  };
               };
            }
            else{ 
               var CM_PageMakerObjectArraylength=CaluMath.PM.PageMakerObjectArray.length; 
               for(var t=0; t<CM_PageMakerObjectArraylength; t++){ 
                  window.CM_InsertInto.childarray= window.CM_InsertInto.childarray.slice(0,window.CM_InsertIntoIndex+1).concat(CaluMath.PM.PageMakerObjectArray[t]).concat( (window.CM_InsertInto.childarray.slice(window.CM_InsertIntoIndex+1)));
               };
            };
         }
      }
      else if(window.CM_EditMode==true){ 
         if(window.opener != null && CM_TopWindow !=window){ 
            var CM_PageMakerObjectArraylength=CaluMath.PM.PageMakerObjectArray.length; 
            for(var t=0; t<CM_PageMakerObjectArraylength; t++){ 
               tempobject.childarray= tempobject.childarray.concat(CaluMath.PM.PageMakerObjectArray[t]);
            };
         }
         else if(CM_TopWindow==window){   
            var tempinsertoption=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_insertoption");
            var tempinserttarget=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_inserttarget");
            var tempcaswindow=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_caswindow");
         };  //Comment: End of if else for window.opener != null 
      };
      CaluMath.PM.AssignParents(tempobject);

      finishbuttonElement.parentNode.removeChild(finishbuttonElement);
      deletebuttonElement.parentNode.removeChild(deletebuttonElement);
      CaluMath.PM.specialsymbolsSELECTwithbrbr.style.display="none";
      specialobjectbuttonElement.parentNode.removeChild(specialobjectbuttonElement);


      if(insertmathorformattedtextbuttonElement!= null && insertmathorformattedtextbuttonElement.parentNode != null){
         insertmathorformattedtextbuttonElement.parentNode.removeChild(insertmathorformattedtextbuttonElement);
      };
   
      if(cancelchangesbuttonElement != null && cancelchangesbuttonElement.parentNode != null){
         cancelchangesbuttonElement.parentNode.removeChild(cancelchangesbuttonElement);
      };
   
   
  
      junkarray.cm_remove();
      CaluMath.PM.AssignParents();
      if(CM_TopWindow.CaluMath.PM.InsertionArray.length == 0){
         CaluMath.PM.DisplayPageControls();
      }
      else{
         CaluMath.PM.HidePageControls("visible");
      };


      CaluMath.PM.HTMLSELECTCHOICES.style.display="none";

      if(CM_TopWindow==window){
         CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction();
         CaluMath.PM.AssignCasWindowToTextChildren();
         CaluMath.PM.AddListsToParsedString(tempobject);
         window.scroll(0,-1000);
      };

      if(CM_TopWindow != window){
         window.cancelinsertbuttonElement.style.display="none";
         window.finishinsertbuttonElement.onclick();
         if(window.opener != null){
            window.opener.location.hash="#endofdocument"; 
         };
         window.close();
      }
      else{
         if(window.CM_EditMode){
            if(oldname.length >0 && oldname != selectedname){ 
                 CaluMath.PM.NewChangeName(tempobject);
            };
         };
         CaluMath.PM.InsertionObjectAlerts(tempobject);
      };
      CM_TopWindow.CM_CurrentParentOfInsertedObject=null;
      CaluMath.PM.textinserttargetSELECT.style.display="inline"; 
      CaluMath.PM.textinsertoptionSELECT.style.display="inline";  
      if(tempobject.cm_getsetting("name").length >0){
         CM_TopWindow.CaluMath.PM.PageMakerObjectParent[tempobject.cm_getsetting("name")]=tempobject;
      };

 

         if(finishbuttonElement != null){
            finishbuttonElement.onclick = null;
         };

         if(deletebuttonElement!= null ){
            deletebuttonElement.onclick= null 
         };
         if(insertmathorformattedtextbuttonElement!= null){
            insertmathorformattedtextbuttonElement.onclick = null
         };
         if(continueinsertbuttonElement!= null){
            continueinsertbuttonElement.onclick= null
         };
         if(cancelinsertbuttonElement!= null ){
            cancelinsertbuttonElement.onclick = function(){
               this.style.display="none";
            }; 
         };
         if(finishinsertbuttonElement!= null ){
            finishinsertbuttonElement.onclick = function(){
               this.style.display="none";
            }; 
         };
         if(specialobjectbuttonElement!= null ){
            specialobjectbuttonElement.onclick= null; 
         };

         if(cancelchangesbuttonElement!= null){
            cancelchangesbuttonElement.onclick=null; 
         };
         if(window.CaluMath.PM.TEXTTEMPINSERTWINDOW !=null){
            CaluMath.PM.TEXTTEMPINSERTWINDOW=null;
         };


   }; //Comment: End of finishbuttonElement.onclick
 

   insertmathorformattedtextbuttonElement.onclick=function(){
   CaluMath.PM.HTMLSELECTCHOICES.style.display="none";
   finishbuttonElement.style.display="none";
   continueinsertbuttonElement.style.display="inline";
   cancelinsertbuttonElement.style.display="inline";
   insertmathorformattedtextbuttonElement.style.display="none";
   specialobjectbuttonElement.style.display="none";
   CaluMath.PM.specialsymbolsSELECTwithbrbr.style.display="none";
   CM_TopWindow.location.hash= "#endofdocument"; 

   //Comment: We place the cancelinsert onclick function here, because it must remove objects that are created by the insertmathorformatted textbutton.
   cancelinsertbuttonElement.onclick= function(){
//      CaluMath.PM.HTMLSELECTCHOICES.style.display="inline";
      CaluMath.PM.HTMLSELECTCHOICES.style.display="";
      finishbuttonElement.style.display="inline";
      continueinsertbuttonElement.style.display="none";
      cancelinsertbuttonElement.style.display="none";
      insertmathorformattedtextbuttonElement.style.display="inline";
      specialobjectbuttonElement.style.display="inline";
      CaluMath.PM.specialsymbolsSELECTwithbrbr.style.display="inline";
      tempselectmenu.remove();
      temptextbeforetempselectmenu.remove();
      CM_TopWindow.location.hash= "#endofdocument"; 
   };

    
   
   
   var temptextbeforetempselectmenu=('<p style="font-size:20px">Please choose the type of item you want to insert into the Text.</p>').cm_html("cm_insertin=displayleft, fontsize=16");
   var tempselectmenu='<select><\/select>'.cm_html("cm_insertin=displayleft");
   CaluMath.PM.MakeSelectMenu(tempselectmenu.cm_node[0], CaluMath.PM.PARAGRAPHINSERTLISTFUNCTION());

   junkarray=junkarray.concat(tempselectmenu);
   junkarray=junkarray.concat(temptextbeforetempselectmenu);



   
   continueinsertbuttonElement.onclick=function(){
      var tempselectedoption=tempselectmenu.cm_node[0].options[tempselectmenu.cm_node[0].selectedIndex];
      var tempselectedoptionvalue=tempselectedoption.value;
      if(cancelchangesbuttonElement != null){
         cancelchangesbuttonElement.style.display="none";
      };

      switch(tempselectedoptionvalue){
         case "cm_coordinates": 
         
            var coordinateschoicefunction=function(){
               var tempchildarrayinsertindex= CaluMath.PM.AssignChildIndex(this.cm_pagemakerobject);
               tempobject.tempchildarrayinsertindex=tempchildarrayinsertindex;
               textareaarray.textareainsertafterindex = CaluMath.PM.ArrayIndex(textareaarray,this);
              
               //Comment: We also don't want them typing in any textareas that defines the paragraph while the insert is going on. So we have:
               for(var qtext=0; qtext<textareaarray.length; qtext++){
                  textareaarray[qtext].onfocus=function(){ 
                     this.blur();
                  };
               };
   
               var p10Element=document.createElement("br")
               coordinatesjunkarray=coordinatesjunkarray.concat(p10Element);
               document.getElementById("displayleft").appendChild(p10Element);
               var p10Element=document.createElement("br")
               coordinatesjunkarray=coordinatesjunkarray.concat(p10Element);
               document.getElementById("displayleft").appendChild(p10Element);
   
               coordinatesjunkarray=coordinatesjunkarray.concat('<p style="width:750px;">Choose the function you are interested in. In the X Value box below, enter a number or a symbol. Choose whether you want both sides, the left hand side, the right hand side, or a graphical representation of the function, or the coordinates of a point corresponding to X Value. Choose whether the value should be evaluated numerically, symbolically, or functionally.<br> As an example, suppose the function f(x) = x^2 is already defined. If you choose: </p>  <table style="width:800px;"><tbody><tr style="vertical-align:top"><td><b>Numeric</b> and enter X Value = 4 and Y Value=y then<br> <i>display both sides</i> yields f(4) = 4^2, <br><i>left hand side</i> yields f(4),  <br><i>right hand side</i> yields 16,  <br><i>display graphically</i> yields y=16, <br><i>coordinates</i> yields (4,16).</td><td>  <b>Symbolic</b> and enter X Value = t and Y Value=y then<br> <i>display both sides</i> yields f(t) = t^2, <br><i>left hand side</i> yields f(t),  <br><i>right hand side</i> yields t^2,  <br><i>display graphically</i> yields y=t^2, <br><i>coordinates</i> yields (t,t^2). </td></tr><tr style="vertical-align:top"><td>  <b>Functional</b> and enter X Value = t and Y Value=y then<br> <i>display both sides</i> yields f(t) = f(t) (which is not very useful), <br><i>left hand side</i> yields f(t),  <br><i>right hand side</i> yields f(t),  <br><i>display graphically</i> yields y=f(t), <br><i>coordinates</i> yields (t,f(t)). </td><td style="width:400px"><b>Once you created your mathematical text</b> the only way to edit it is to click <span class="headernav">Lists</span> and click on the entry in the Math Text list in the window that opens.</td></tr></tbody></table><br>'.cm_html("cm_insertin=displayleft"));

               coordinatesjunkarray=coordinatesjunkarray.concat('<span style="font-size:20px">Name:</span>'.cm_html("cm_insertin=displayleft"));
               var nameinput = document.createElement("input")
               coordinatesjunkarray=coordinatesjunkarray.concat(nameinput);
               document.getElementById("displayleft").appendChild(nameinput);
               nameinput.setAttribute("type","text");
               nameinput.setAttribute("size","40");
               var tempnumber= CaluMath.PM.MathTextIndex++;
               nameinput.value = "MathText"+tempnumber;
               coordinatesjunkarray=coordinatesjunkarray.concat('<br>'.cm_html("cm_insertin=displayleft"));
   
               var functionselectmenu = document.createElement("select")
               coordinatesjunkarray=coordinatesjunkarray.concat(functionselectmenu);
               document.getElementById("displayleft").appendChild(functionselectmenu);
               CaluMath.PM.MakeSelectMenu(functionselectmenu, CM_TopWindow.CaluMath.PM.PlotlistObject.definedfunctionlist);
      
               var coordinatesoptionsselectmenu = document.createElement("select");
               coordinatesjunkarray=coordinatesjunkarray.concat(coordinatesoptionsselectmenu);
               document.getElementById("displayleft").appendChild(coordinatesoptionsselectmenu);
               CaluMath.PM.MakeSelectMenu(coordinatesoptionsselectmenu, CaluMath.PM.COORDINATESOPTIONSLIST);
      
               var coordinatestypeselectmenu = document.createElement("select")
               coordinatesjunkarray=coordinatesjunkarray.concat(coordinatestypeselectmenu);
               document.getElementById("displayleft").appendChild(coordinatestypeselectmenu);
               CaluMath.PM.MakeSelectMenu(coordinatestypeselectmenu, CaluMath.PM.COORDINATESTYPELIST);
   
               coordinatesjunkarray=coordinatesjunkarray.concat('<br><br>'.cm_html("cm_insertin=displayleft"));

               coordinatesjunkarray=coordinatesjunkarray.concat('<span>X Value:</span>'.cm_html("cm_insertin=displayleft"));
      
               var coordinateinput = document.createElement("input")
               coordinatesjunkarray=coordinatesjunkarray.concat(coordinateinput);
               document.getElementById("displayleft").appendChild(coordinateinput);
               coordinateinput.setAttribute("type","text");
               coordinateinput.setAttribute("size","40");

               coordinatesjunkarray=coordinatesjunkarray.concat('<span>Y Value:</span>'.cm_html("cm_insertin=displayleft"));


               var ycoordinateinput = document.createElement("input")
               coordinatesjunkarray=coordinatesjunkarray.concat(ycoordinateinput);
               document.getElementById("displayleft").appendChild(ycoordinateinput);
               ycoordinateinput.setAttribute("type","text");
               ycoordinateinput.setAttribute("size","40");
 
  
               coordinatesjunkarray=coordinatesjunkarray.concat('<br><br>'.cm_html("cm_insertin=displayleft"));

               var newspecialobjectbuttonElement=document.createElement("input");
               newspecialobjectbuttonElement.setAttribute("type","button");
               newspecialobjectbuttonElement.setAttribute("value","Insert Special Character");
               document.getElementById("displayleft").appendChild(newspecialobjectbuttonElement);

               var coordinatesspecialsymbolsSELECT=CaluMath.PM.specialsymbolsSELECT.cloneNode(true);
               coordinatesjunkarray=coordinatesjunkarray.concat(document.getElementById("displayleft").appendChild(coordinatesspecialsymbolsSELECT));

               coordinatesjunkarray=coordinatesjunkarray.concat('<br><br>'.cm_html("cm_insertin=displayleft"));

               var p11Element=document.createElement("input");
               p11Element.setAttribute("type","button");
               p11Element.setAttribute("value","Finish Math");
               document.getElementById("displayleft").appendChild(p11Element);
               coordinatesjunkarray=coordinatesjunkarray.concat(p11Element);

               continueinsertbuttonElement.style.display="none";
               cancelinsertbuttonElement.style.display="none";
               deletebuttonElement.style.display="none";
               tempselectmenu.cm_node[0].parentNode.removeChild(tempselectmenu.cm_node[0]);
               temptextbeforetempselectmenu.remove();


              
               newspecialobjectbuttonElement.onclick=function(){
//                  var character = coordinatesspecialsymbolsSELECT.options[coordinatesspecialsymbolsSELECT.selectedIndex].label.replace(/^\s*/g,""); 
                  var character = coordinatesspecialsymbolsSELECT.options[coordinatesspecialsymbolsSELECT.selectedIndex].value.replace(/^\s*/g,""); 
                  coordinateinput.value = coordinateinput.value +character;
               };

               window.location.hash="#endofdocument"; 
               coordinateinput.focus();

   
               p11Element.onclick=function(){

                  try{
                     var chosenfunction = functionselectmenu.options[functionselectmenu.selectedIndex].value.replace(/^\s*/g,""); 
                     var chosentype = coordinatesoptionsselectmenu.options[coordinatesoptionsselectmenu.selectedIndex].value.replace(/^\s*/g,""); 
                     var chosenevaluation = coordinatestypeselectmenu.options[coordinatestypeselectmenu.selectedIndex].value.replace(/^\s*/g,""); 
                     var chosenvalue= coordinateinput.value.replace(/^\s*/g,""); 
                     var chosenyvalue= ycoordinateinput.value.replace(/^\s*/g,""); 
                     var chosenname= nameinput.value.replace(/^\s*/g,""); 
                     if(chosenvalue.length == 0){
                        return alert("You must enter a coordinate in the input box.");
                     };
                     if(chosenname.length == 0){
                        return alert("You must enter a name for this Math Text.");
                     };
                     if(chosenevaluation=="e"){
                        chosenvalue.cm_evalmath();
                     };

//                     if(chosenevaluation=="e" && chosenfunction.match(/cm_eval/)==null && CM_TopWindow[chosenfunction].cm_definedfunction.cm_rightside.match(/\'/)==null && CM_TopWindow[chosenfunction].cm_definedfunction.cm_rightside.match(/\"/)==null ){ 
//                        eval(chosenvalue);
//                     };
                  }
                  catch(e){
//                     return cm_alert('The value you entered is not a number. Therefore you should not choose the option "evaluated". Choose "symbolic" or "functional" instead.');
                     if(!confirm('The value you entered is not a number. Perhaps you should not choose the option "evaluated" and should choose "symbolic" or "functional" instead. If you are sure you want to continue, click the OK button')){
                        return ;
                     };
                  };

                  //Comment: For the purposes of seeing if the function makes sense, we defined chosenfunction above which evaluates the function in terms of CM_TopWindow above. However in the window that is actually written, we want to evaluate the function (which is defined in CaluMath.PM.MainWindow) in that context. Therefore we append CaluMath.PM.MainWindow to the function.
                  var coordinatesobject=CaluMath.PM.PageMakerObject("coordinates");
           
                  var specialsymbolsunicodeLISTlength=CaluMath.PM.specialsymbolsunicodeLIST.length;
//                  smallsymbolloop:
//                  for(var i=0; i<specialsymbolsunicodeLISTlength; i++){
//                     if(specialsymbolsunicodeLIST[i] != null){
//                        var tempregexpression= new RegExp(specialsymbolsunicodeLIST[i][0],"g");
//                        chosenvalue=chosenvalue.replace(tempregexpression, specialsymbolsunicodeLIST[i][1]);
//                     };
//                  };

                  coordinatesobject.cm_requiredarray=CM_TopWindow.Array();
                  coordinatesobject.cm_requiredarray[0] = CM_TopWindow.Array(); 
                  coordinatesobject.cm_requiredarray[1] = CM_TopWindow.Array();
                  coordinatesobject.cm_requiredarray[2] = CM_TopWindow.Array();
                  coordinatesobject.cm_requiredarray[3] = CM_TopWindow.Array();
                  coordinatesobject.cm_requiredarray[4] = CM_TopWindow.Array();
                  coordinatesobject.cm_requiredarray[5] = CM_TopWindow.Array();
                  coordinatesobject.cm_requiredarray[0][0]="definedfunction";
                  coordinatesobject.cm_requiredarray[0][1]=chosenfunction;
                  coordinatesobject.cm_requiredarray[1][0]="type";
                  coordinatesobject.cm_requiredarray[1][1]=chosentype;
                  coordinatesobject.cm_requiredarray[2][0]="evaluation";
                  coordinatesobject.cm_requiredarray[2][1]=chosenevaluation;
                  coordinatesobject.cm_requiredarray[3][0]="value";
                  coordinatesobject.cm_requiredarray[3][1]=chosenvalue;
                  coordinatesobject.cm_requiredarray[4][0]="name";
                  coordinatesobject.cm_requiredarray[4][1]=chosenname;
                  coordinatesobject.cm_requiredarray[5][0]="yvalue";
                  coordinatesobject.cm_requiredarray[5][1]=chosenyvalue;

                  coordinatesobject.cm_optionsarray=CM_TopWindow.Array();
                  tempobject.childarray.splice(1+1*tempobject.tempchildarrayinsertindex,0,coordinatesobject);
                  coordinatesjunkarray.cm_remove();
                  newspecialobjectbuttonElement.parentNode.removeChild(newspecialobjectbuttonElement);
                  continueinsertbuttonElement.style.display="none"; 
                  deletebuttonElement.style.display="inline";
                  if(p11Element != null && p11Element.parentNode != null){
                     p11Element.parentNode.removeChild(p11Element);
                  };
                  finishinsertbuttonElement.style.display="inline";
                  insertmathorformattedtextbuttonElement.style.display="inline";
                  specialobjectbuttonElement.style.display="inline";
                  CaluMath.PM.specialsymbolsSELECTwithbrbr.style.display="inline";

                  finishinsertbuttonElement.onclick();
                  if(cancelchangesbuttonElement != null && cancelchangesbuttonElement.parentNode != null){
                     cancelchangesbuttonElement.parentNode.removeChild(cancelchangesbuttonElement);
                  };
               };
   
               //Comment: This button is clicked to cancel the insertion of math.
               var cancelmathinsertbuttonElement=document.createElement("input");
               cancelmathinsertbuttonElement.setAttribute("type","button");
               cancelmathinsertbuttonElement.setAttribute("value","Cancel Insert");
               CaluMath.PM.BUTTONCONTAINER.appendChild(cancelmathinsertbuttonElement);
               coordinatesjunkarray=coordinatesjunkarray.concat(cancelmathinsertbuttonElement);

               cancelmathinsertbuttonElement.onclick=function(){
                  coordinatesjunkarray.cm_remove();
                  newspecialobjectbuttonElement.parentNode.removeChild(newspecialobjectbuttonElement);
                  continueinsertbuttonElement.style.display="none"; 
                  finishbuttonElement.style.display="inline";
                  deletebuttonElement.style.display="inline";
                  if(p11Element != null && p11Element.parentNode != null){
                     p11Element.parentNode.removeChild(p11Element);
                  };
                  insertmathorformattedtextbuttonElement.style.display="inline";
                  specialobjectbuttonElement.style.display="inline";
                  CaluMath.PM.specialsymbolsSELECTwithbrbr.style.display="inline";
                  if(cancelchangesbuttonElement != null){
                     cancelchangesbuttonElement.style.display="inline";
                  };
                  //Comment: When we cancel Math insertion, we allow them to type in the textareas again.
                  for(var qtext=0; qtext<textareaarray.length; qtext++){
                     textareaarray[qtext].onfocus=null;
                  };
               };
            };
            //Comment: The textarea that they click in is the one that we want the inserted object to appear after. textblockfunction is the function that will do this. 

            for(var qtext=0; qtext<textareaarray.length; qtext++){
               textareaarray[qtext].onfocus=coordinateschoicefunction;
            };

            if(textareaarray.length >1){ 
               cm_alert("Please click on the block of text above that you want your inserted "+tempselectedoptionvalue+" to appear after");              
            }
            else{
               textareaarray[0].focus();
            };

            break;

         case "span": 
            tempselectmenu.cm_node[0].parentNode.removeChild(tempselectmenu.cm_node[0]);
            temptextbeforetempselectmenu.remove();

            textblockfunction=function(){
               var tempchildarrayinsertindex= CaluMath.PM.AssignChildIndex(this.cm_pagemakerobject);
               tempobject.tempchildarrayinsertindex=tempchildarrayinsertindex;
               textareaarray.textareainsertafterindex = CaluMath.PM.ArrayIndex(textareaarray,this);
            
               //Comment: We also don't want them typing in any textareas that defines the paragraph while the insert is going on. So we have:
               for(var qtext=0; qtext<textareaarray.length; qtext++){
                  textareaarray[qtext].onfocus=function(){ 
                     this.blur();
                  };
               };

               continueinsertbuttonElement.style.display="none";
//               finishinsertbuttonElement.style.display="inline";

               CM_TopWindow.CaluMath.PM.TextIndex++;
      
               CaluMath.PM.TEXTTEMPINSERTWINDOW=window.open("CaluMath_Page_Maker.html", '', "height="+Math.round(screen.height*3/4)+", width="+Math.round(screen.width*3/4)+", scrollbars=yes, resizable=yes");

               CaluMath.PM.TEXTTEMPINSERTWINDOW.CM_EditMode=false;
               CaluMath.PM.TEXTTEMPINSERTWINDOW.CM_InEditMode=true;
               CaluMath.PM.TEXTTEMPINSERTWINDOW.CM_InsertInto=tempobject;
               CaluMath.PM.TEXTTEMPINSERTWINDOW.CM_InsertIntoIndex=tempchildarrayinsertindex;

               CaluMath.PM.TEXTTEMPINSERTWINDOWAFTERLOADINGFUNCTION=function(){
                  if(CaluMath.PM.TEXTTEMPINSERTWINDOW.cm_windowloaded == "yes"){
                     //Comment: We want to highlight the choice "span" in the CaluMath.PM.HTMLSELECT select menu. We then invoke CaluMath.PM.HTMLBUTTON.onclick() so that the user can start inputting text. In Opera, an error occurs when trying to highlight a choice if the menu has display="none". Therefore we invoke the CaluMath.PM.MASTERBUTTON.onclick, which creates the CaluMath.PM.HTMLSELECT, give it the default action, and then we invoke the CaluMath.PM.HTMLBUTTON.onclick routine. Note that if we change the label "Text" in MASTERSELECT, then we would have to change the label "Text" here to get the correct default option.
//Commented out now                     CaluMath.PM.TEXTTEMPINSERTWINDOW.CaluMath.PM.MakeDefaultOption(CaluMath.PM.TEXTTEMPINSERTWINDOW.CaluMath.PM.MASTERSELECT,"Text"); 
//                     CaluMath.PM.TEXTTEMPINSERTWINDOW.CaluMath.PM.MASTERBUTTON.onclick();
//Commented out now                     CaluMath.PM.TEXTTEMPINSERTWINDOW.CaluMath.PM.MakeDefaultOption(CaluMath.PM.TEXTTEMPINSERTWINDOW.CaluMath.PM.HTMLSELECT,"Formatted Text"); 
//                     CaluMath.PM.TEXTTEMPINSERTWINDOW.CaluMath.PM.HTMLBUTTON.onclick();
//                     CaluMath.PM.TEXTTEMPINSERTWINDOW.CaluMath.PM.HTMLBUTTON.style.display="none";
                       CaluMath.PM.TEXTTEMPINSERTWINDOW.CaluMath.PM.WriteAndEdit("text", "table");
// Just done.                      CaluMath.PM.TEXTTEMPINSERTWINDOW.CaluMath.PM.TABLEFORHTMLCASMATHOPTIONS.style.display="none"; 
                     CaluMath.PM.TEXTTEMPINSERTWINDOW.finishinsertbuttonElement=finishinsertbuttonElement; 
                     CaluMath.PM.TEXTTEMPINSERTWINDOW.cancelinsertbuttonElement= cancelinsertbuttonElement;
                     CaluMath.PM.TEXTTEMPINSERTWINDOWAFTERLOADINGFUNCTION=null;
                     CaluMath.PM.TEXTTEMPINSERTWINDOW.document.getElementById("htmlselectchoices").style.display="";
CaluMath.PM.TEXTTEMPINSERTWINDOW.cm_onunloadarray.push(function(){  
cancelinsertbuttonElement.onclick();
//   cancelinsertbuttonElement.style.display="none";
//   finishinsertbuttonElement.style.display="none";
//alert("Hi");
});
//   CaluMath.PM.TEXTTEMPINSERTWINDOW.CaluMath.Html.BreakReferences();
//alert("Hi");
//}; 
                  }
                  else{
                     window.setTimeout('CaluMath.PM.TEXTTEMPINSERTWINDOWAFTERLOADINGFUNCTION();',50);
                  };
               };  
               CaluMath.PM.TEXTTEMPINSERTWINDOWAFTERLOADINGFUNCTION();
            }; //Comment: End of textblockfunction
           
            //Comment: The textarea that they click in is the one that we want the inserted object to appear after. textblockfunction is the function that will do this. 
            for(var qtext=0; qtext<textareaarray.length; qtext++){
               textareaarray[qtext].onfocus=textblockfunction;
            };

            if(textareaarray.length >1){ 
               cm_alert("Please click on the block of text above that you want your inserted "+tempselectedoptionvalue+" to appear after");              
            }
            else{
               textareaarray[0].focus();
            };
            break; //Comment: End of switch case span.
         };  //Comment: End of switch.

      }; //Comment: End of continueinsertbuttonElement.onclick
   
      finishinsertbuttonElement.onclick=function(){ 
         window.onfocus=CaluMath.Html.NullFunction;
//         CaluMath.PM.HTMLSELECTCHOICES.style.display="block";
         CaluMath.PM.HTMLSELECTCHOICES.style.display="";
         finishinsertbuttonElement.style.display="none";
         finishbuttonElement.style.display="inline";
         insertmathorformattedtextbuttonElement.style.display="inline";
         specialobjectbuttonElement.style.display="inline";
         CaluMath.PM.specialsymbolsSELECTwithbrbr.style.display="inline";


         
         if(window.CM_InsertCanceled != true){      
            NewString="";
            var textareainsertafterindex=textareaarray.textareainsertafterindex;
            var nextsibling= textareaarray[textareainsertafterindex].nextSibling;
//alert([tempobject.childarray[tempobject.tempchildarrayinsertindex+1].type != "coordinates" , tempobject.childarray[tempobject.tempchildarrayinsertindex+1].type=="coordinates" , tempobject.childarray[tempobject.tempchildarrayinsertindex+1].cm_requiredarray[0][1]]);


            //Comment: We used to try to evaluate the math expression and display it while making the text. We no longer do this, so we do not need the conditional anymore. 
//            if(tempobject.childarray[tempobject.tempchildarrayinsertindex+1].type != "coordinates" || (tempobject.childarray[tempobject.tempchildarrayinsertindex+1].type=="coordinates" && CM_TopWindow[tempobject.childarray[tempobject.tempchildarrayinsertindex+1].cm_requiredarray[0][1]] != null)){ 
//               junkarray=junkarray.concat(('<br><br><span>Special Inserted Object </span>'+CaluMath.PM.InPageWriteHtmlString(tempobject.childarray[tempobject.tempchildarrayinsertindex+1])+'<br>').cm_html("cm_insertin=displayleft, dojavascript=no"));
//            }
//            else{ 
//               junkarray=junkarray.concat(('<br><br><span>Special Inserted Object </span><br>').cm_html("cm_insertin=displayleft"));
//            };

          junkarray=junkarray.concat(('<br><br><span>Special Inserted Object </span>'+CaluMath.PM.InPageWriteHtmlString(tempobject.childarray[tempobject.tempchildarrayinsertindex+1])+'<br>').cm_html("cm_insertin=displayleft, dojavascript=no"));

            if(nextsibling != null){
               for(var t=1; t<junkarray[junkarray.length-1].cm_node.length; t++){
                  textareaarray[textareainsertafterindex].parentNode.insertBefore(junkarray[junkarray.length-1].cm_node[t], nextsibling);
               };
            };
            textareaarrayindex++;
            var pElement=document.createElement("textarea");
            pElement.setAttribute("rows",rows);
            pElement.setAttribute("cols","80");
            textareaarray[textareainsertafterindex].parentNode.insertBefore(pElement, nextsibling);
            textareaarray[textareaarrayindex]=pElement;
            pElement.value=" ";
      
            var temptextobject=  CaluMath.PM.PageMakerObject("text");
            tempobject.childarray=tempobject.childarray.slice(0,tempobject.tempchildarrayinsertindex+2).concat(temptextobject).concat(tempobject.childarray.slice(tempobject.tempchildarrayinsertindex+2));


            temptextobject.writestring=pElement.value;
            textareaobjectarray[textareaarrayindex]=temptextobject;
            pElement.cm_pagemakerobject=temptextobject;
            temptextobject.parent=tempobject;
            pElement.focus();
         };
         for(var qtext=0; qtext<textareaarray.length; qtext++){
            textareaarray[qtext].onfocus=CaluMath.Html.NullFunction; 
         };
         window.CM_InsertCanceled = null;

      }; //End of finishinsertbuttonElement.onclick

      CM_TopWindow.location.hash= "#endofdocument"; 

   }; //End of continueinsertbuttonElement.onclick
   
};//Comment: End of CaluMath.PM.WriteAndEdit function
