/*

    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: If this is not the top window, then the CaluMath_Page_Maker.html is being used in an insertion, so we need the line below.
if(CM_TopWindow != window){
   document.getElementById("createpage").value="View Inserted Object";
};

CaluMath.PM.DisplayPageControls=function(){ 
   CaluMath.PM.PAGECONTROLCONTAINER.style.display="block";
   CaluMath.PM.PAGEASSISTCONTAINER.style.display="none";
   // CaluMath.PM.TABLEFORHTMLCASMATHOPTIONS.style.visibility="visible";
   CaluMath.PM.MENUSTABLEFORHTMLCASMATHOPTIONS.style.visibility="visible";
   if(CM_TopWindow==window){
      CaluMath.PM.CreateInLineEditSelectMenu();
      document.getElementById("editinlinebutton").onclick=CaluMath.PM.InLineEditButtonOnclickFunction;
   };
   window.scroll(0,-1000);
};

CaluMath.PM.HidePageControls= function(){ //alert("Doing HidePageControls");
   CaluMath.PM.PAGECONTROLCONTAINER.style.display="none";
   CaluMath.PM.PAGEASSISTCONTAINER.style.display="block";
   if(CM_TopWindow==window){
     document.getElementById("editinlinebutton").onclick=null;
   };
   if(arguments[0] =="visible"){
      // CaluMath.PM.TABLEFORHTMLCASMATHOPTIONS.style.visibility="visible";
      CaluMath.PM.MENUSTABLEFORHTMLCASMATHOPTIONS.style.visibility="visible";
   }
   else{
      // CaluMath.PM.TABLEFORHTMLCASMATHOPTIONS.style.visibility="hidden";
      CaluMath.PM.MENUSTABLEFORHTMLCASMATHOPTIONS.style.visibility="hidden";
      window.scroll(0, 10000);
   };
};

//Comment: This determies the path name from the CAS Page Maker File to the folder that says CaluMath. It is used to determine the path name to the files that are necessary for any CaluMath page to operate. This path is used when the created page is saved. 
CaluMath.PM.DetermineLibraryFolder=function(){
//alerttest(decodeURI(document.location.pathname));
   var pathname = decodeURI(document.location.pathname).replace(/^[\/\"\/]/,'');
   var pathnamematch=pathname.match(/CaluMath/);
   if(pathnamematch != null){
      pathname=pathname.slice(pathnamematch.index+9);
      if(pathname.match(/\\/)){ 
         var numberofforwardslashes= pathname.split("\\").length;
         var tempdots="";
         for(var i=1; i<numberofforwardslashes; i++){
            tempdots=tempdots+"../";
         };
         return  tempdots;    
      }
      else if(pathname.match(/\//)){ 
         var numberofforwardslashes= pathname.split("/").length;
         var tempdots="";
         for(var i=1; i<numberofforwardslashes; i++){
            tempdots=tempdots+"../";
         };
         return  tempdots;   //+"cm_library/";
      };
   }
   else{
      alert("You do not have the CaluMath Page Maker in the CaluMath folder. It will not work properly in this case.");
   };
};

CaluMath.PM.DotsInPathToCaluMathFolder=CaluMath.PM.DetermineLibraryFolder();

//Comment: This returns the Head and openning Body statement of the window that is being constructed.//
  
CaluMath.PM.NewWindowStringBeginning=function(){
   var writestyletag= arguments[0];
   if(writestyletag != "no"){
      var tempstring='';
   }
   else if(arguments[1] != null){
      var tempstring=arguments[1];
   }
   else{
      var tempstring='';
   };
   var pathadjustment=CaluMath.PM.DotsInPathToCaluMathFolder;
   if(writestyletag != "no"){
   var tempCM_PageMakerObjectArray=CaluMath.PM.PageMakerObjectArray;
   var tempCM_PageMakerObjectArraylength=tempCM_PageMakerObjectArray.length;
   var tempheadindex=CaluMath.PM.HeadHtmlInsertionPoint();
   var tempstyleindex=CaluMath.PM.StyleInsertionPoint();
   var headarray= new Array();
   var stylearray= new Array();
   var pagename = '';
   for(var i=0; i< tempheadindex; i++){
      if(tempCM_PageMakerObjectArray[i].type=="headhtml"){
         headarray[i]= tempCM_PageMakerObjectArray[i].cm_writehtml();
      }
      else if(tempCM_PageMakerObjectArray[i].type=="webpagename"){
         pagename = tempCM_PageMakerObjectArray[i].cm_writehtml();
      }
   };
   for(var i=tempheadindex; i< tempstyleindex; i++){
      stylearray[i]= tempCM_PageMakerObjectArray[i].cm_writehtml();
   };
   stylearray=stylearray.slice(tempheadindex);
   var tempstylestringjoined='<style type="text/css"> \n'+stylearray.join("\n")+'\n<\/style>';
   }
   else{
   var headarray= new Array();
   var tempstylestringjoined="";
   };
   if(pagename ==null || (pagename.cm_constructorname=="String" && pagename.length==0)){
      pagename="A CaluMath Page";
   };
   return '<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN" \n'+  
   '  "http:\/\/www.w3.org/TR/html4/loose.dtd">  \n'+
   '<html > \n'+
   ' \n'+
   '<!-- This file was created using the CaluMath Page Maker. \n'+
   '     The CaluMath Page Maker, Copyright 2003-2007 Purdue University, is distributed under the GNU General Public License. --> \n'+
   ' \n'+
   '<head><title>' +pagename+'</title> \n'+
   '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> \n'+ 
   '<meta name="generator" content="CaluMath (http://ems.calumet.purdue.edu/mcss/psturbek/Calumath/CaluMath_HomePage.html)">\n'+
   headarray.join("\n")+'\n'+
   '<script type="text/javascript" > \n'+
   'CM_BeginLoadingTime=new Date()\; \n'+
   'CM_BeginLoadingTime=CM_BeginLoadingTime.getTime()\; \n'+
   '<\/script> \n'+
   '<script type="text/javascript" src="place_this_in_folder_with_web_pages.js"><\/script>\n'+
   tempstylestringjoined+'\n'+
   '<style type="text/css"> \n'+
   tempstring+' \n'+
   '<\/style> \n'+
   '</head>\n'+
   ' \n'+
//   '<body>\n<script type="text/javascript"> CaluMath.PM.DotsInPathToCaluMathFolder="'+CaluMath.PM.DotsInPathToCaluMathFolder+'"\; CM_MainWindow=window\; CM_MainWindow.CaluMath.Html.NewWindowArray = new Array()\;';
   '<body>\n<script type="text/javascript"> CaluMath.PM.DotsInPathToCaluMathFolder=CM_DotsToCaluMathFolder\; CM_MainWindow=window\; CM_MainWindow.CaluMath.Html.NewWindowArray = new Array()\;';
};

CaluMath.PM.NewWindowStringClosing= '\n</body>\n</html>';

CM_ScriptTag = '<script type="text/javascript"';
 
//NewWindowString="";
CaluMath.PM.PreviousPageArray=[];

//WriteMode=true;
CM_EditMode=false;

document.getElementById("helpobject").onclick=function(){
   window.open("help/CaluMath_HomePage_For_Help.html");
   return false
};

document.getElementById("quickintroductionbutton").onclick=function(){
   if(CaluMath.PM.QuickIntroductionWindow != null && ! CaluMath.PM.QuickIntroductionWindow.closed && CaluMath.PM.QuickIntroductionWindow.close != null){
      CaluMath.PM.QuickIntroductionWindow.close();
   };
   CaluMath.PM.QuickIntroductionWindow= window.open("pagemakerfiles/QuickIntroduction.html");
   CaluMath.PM.QuickIntroductionWindow.moveTo(0,0);
};



document.getElementById("helpobjecteditmode").onclick=document.getElementById("helpobject").onclick;

//////////////THIS IS TO VIEW THE PAGE THAT IS UNDER CONSTRUCTION////////////////////////////

//Comment: The eventhandler of this button creates the web page, as constructed so far, in a new pop-up window.

document.getElementById("createpage").onclick=function(){

   if(window.CaluMath.PM.VIEWAREABUTTON != null && window.CaluMath.PM.VIEWAREABUTTON.parentNode != null){
     CaluMath.PM.VIEWAREA.style.display="";
     CaluMath.PM.VIEWAREABUTTON.style.display="";
     CaluMath.PM.VIEWAREAPARAGRAPH.style.display="";
   }
   else{
      CaluMath.PM.VIEWAREABUTTON=document.createElement("input");
      CaluMath.PM.VIEWAREABUTTON.setAttribute("type","button");
      CaluMath.PM.VIEWAREABUTTON.setAttribute("value","Remove Source Code");
      document.body.appendChild(CaluMath.PM.VIEWAREABUTTON);
      CaluMath.PM.VIEWAREALINEBREAK=document.createElement("br");
      document.body.appendChild(CaluMath.PM.VIEWAREALINEBREAK);
      CaluMath.PM.VIEWAREAPARAGRAPH=document.createElement("p");
      CaluMath.PM.VIEWAREAPARAGRAPH.style.width="700px";
//      CaluMath.PM.VIEWAREAPARAGRAPH.style.marginLeft="5px";
      CaluMath.PM.VIEWAREAPARAGRAPHTEXT=document.createTextNode("To save the page you created, select all of the text in the box below and copy it. Open a text editor such as Wordpad, and create a new document. Paste the text into the new document. Save it as a text file in the MyWebPages folder (which is contained in the CaluMath folder) and give it a name such as mypage.html. ");
      CaluMath.PM.VIEWAREAPARAGRAPH.appendChild(CaluMath.PM.VIEWAREAPARAGRAPHTEXT);
      document.body.appendChild(CaluMath.PM.VIEWAREAPARAGRAPH);
      CaluMath.PM.VIEWAREABUTTON.onclick=CaluMath.PM.VIEWAREABUTTONOnClickFunction;
      CaluMath.PM.VIEWAREA=document.createElement("textarea");
      CaluMath.PM.VIEWAREA.setAttribute("rows",60);
      CaluMath.PM.VIEWAREA.setAttribute("cols",80);
//      CaluMath.PM.VIEWAREA.style.marginLeft="5px";
      document.body.appendChild(CaluMath.PM.VIEWAREA);
   };

   //Comment: We create the inline edit menu in the main window and activate the inline edit button. 
   if(CM_TopWindow==window){
      CaluMath.PM.CreateInLineEditSelectMenu();
      document.getElementById("editinlinebutton").onclick=CaluMath.PM.InLineEditButtonOnclickFunction;
   };

   if(window.CM_TempWindow != null && !window.CM_TempWindow.closed &&  window.CM_TempWindow.close != null &&  navigator.appVersion.toString().match(/Safari/) != null){
      CM_TempWindow.close();
      //Comment: Note that CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction calls CaluMath.PM.AssignParents.
      CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction();
      CaluMath.PM.AssignCasWindowToTextChildren();
      CM_TopWindow.CM_EditMode=false;
      if(window.outerWidth != null){
         window["CM_TempWindow"]=window.open('','',"height="+Math.round(3*window.outerHeight/4)+", width="+Math.round(3*window.outerWidth/4)+", menubar=yes, scrollbars=yes, resizable=yes");
      }
      else{
         window["CM_TempWindow"]=window.open('','',"height="+Math.round(3*window.screen.availHeight/4)+", width="+Math.round(3*window.screen.availWidth/4)+", menubar=yes, scrollbars=yes, resizable=yes");
      };
      window["CM_TempWindow"].moveTo(0,0);
      setTimeout('window["CM_TempWindow"].document.write(CaluMath.PM.NewWindowStringBeginning()); CaluMath.PM.TempWindowWriteRemainingClosureReplacement();',10);  
   }
   else if(window.CM_TempWindow != null && !window.CM_TempWindow.closed && window.CM_TempWindow.close != null){
      CM_TempWindow.document.open();
      CM_TempWindow.focus();
      setTimeout('window["CM_TempWindow"].document.write(CaluMath.PM.NewWindowStringBeginning()); CaluMath.PM.TempWindowWriteRemainingClosureReplacement();',10);  
   }
   else { 
      //Comment: Note that CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction calls CaluMath.PM.AssignParents.
      CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction();
      CaluMath.PM.AssignCasWindowToTextChildren();
      CM_TopWindow.CM_EditMode=false;
      if(window.outerWidth != null){
         window["CM_TempWindow"]=window.open('','',"height="+Math.round(3*window.outerHeight/4)+", width="+Math.round(3*window.outerWidth/4)+", menubar=yes, scrollbars=yes, resizable=yes");
      }
      else{
         window["CM_TempWindow"]=window.open('','',"height="+Math.round(3*window.screen.availHeight/4)+", width="+Math.round(3*window.screen.availWidth/4)+", menubar=yes, scrollbars=yes, resizable=yes");
      };
      window["CM_TempWindow"].moveTo(0,0);
      setTimeout('window["CM_TempWindow"].document.write(CaluMath.PM.NewWindowStringBeginning()); CaluMath.PM.TempWindowWriteRemainingClosureReplacement();',10);  
   };
   return false;
};

CaluMath.PM.VIEWAREABUTTONOnClickFunction=function(){
     CaluMath.PM.VIEWAREA.style.display="none";
     CaluMath.PM.VIEWAREABUTTON.style.display="none";
     CaluMath.PM.VIEWAREAPARAGRAPH.style.display="none";
};

CaluMath.PM.TempWindowWriteRemainingClosureReplacement= function(){
   if(window["CM_TempWindow"].CM_ClientAddpointsLoaded=="yes"){
      var NewString = CaluMath.PM.WriteNewWindowString();

      var CaluMathPageMakerString= CaluMath.PM.PageMakerObjectArrayToString();
      CaluMath.PM.PreviousPageArray=CaluMath.PM.PreviousPageArray.concat(CaluMathPageMakerString);
      var texteditbutton='\'<br><br><input type="button" id="texteditbutton" value="Click Here to Edit Text" />\'.cm_html()\; ';
      var texteditbuttonscript='\n<\/script>\n\n\n'+CM_ScriptTag+'> if(window.opener != null && window.opener.CaluMath != null && window.opener.CaluMath.PM.PageMakerObjectArray != null ){ '+texteditbutton+'  document.getElementById("texteditbutton").onclick=function(){  if(window.opener != null && window.opener.CaluMath != null && window.opener.CaluMath.PM.VIEWAREABUTTON != null  ){window.opener.CaluMath.PM.VIEWAREABUTTON.onclick();}; window.opener.CaluMath.PM.MakeClickableEdit(window.opener.CM_TempWindow)\; for(var i=1; i<window.opener.CaluMath.PM.PlotlistObject.newwindowlist.length\; i++){  window.opener.CaluMath.PM.MakeClickableEdit(window[window.opener.CaluMath.PM.PlotlistObject.newwindowlist[i][0]])\; }\;  document.getElementById("texteditbutton").onclick=null}; } <\/script>';
//      var temparray=[NewString, texteditbuttonscript, '\n'+CM_ScriptTag+'>CaluMath.PM.HTMLElementIndex='+CaluMath.PM.HTMLElementIndex+'\; CaluMath.PM.PlotNameIndex='+CaluMath.PM.PlotNameIndex+'\;  CaluMath.PM.MathTextIndex='+CaluMath.PM.MathTextIndex+'\; CaluMath.PM.TextIndex='+CaluMath.PM.TextIndex+'\; CaluMath.PM.ContainerIndex='+CaluMath.PM.ContainerIndex+'\; CaluMath.PM.ParagraphIndex='+CaluMath.PM.ParagraphIndex+'\; CaluMath.PM.TitleIndex='+CaluMath.PM.TitleIndex+'\; CaluMath.PM.SectionIndex='+CaluMath.PM.SectionIndex+'\; <\/script>\n'+CM_ScriptTag+'> CaluMath.PM.PageMakerObjectArrayString=\''+CaluMathPageMakerString.replace(/CM_PageMakerStringDelimiter/g," '+\n' CM_PageMakerStringDelimiter ")+'\' <\/script> \n'+CaluMath.PM.NewWindowStringClosing];
      var temparray=[NewString, texteditbuttonscript, '\n'+CM_ScriptTag+'>CaluMath.PM.HTMLElementIndex='+CaluMath.PM.HTMLElementIndex+'\; CaluMath.PM.PlotNameIndex='+CaluMath.PM.PlotNameIndex+'\;  CaluMath.PM.MathTextIndex='+CaluMath.PM.MathTextIndex+'\; CaluMath.PM.TextIndex='+CaluMath.PM.TextIndex+'\; CaluMath.PM.ContainerIndex='+CaluMath.PM.ContainerIndex+'\; CaluMath.PM.ParagraphIndex='+CaluMath.PM.ParagraphIndex+'\; CaluMath.PM.TitleIndex='+CaluMath.PM.TitleIndex+'\; CaluMath.PM.SectionIndex='+CaluMath.PM.SectionIndex+'\; <\/script>\n'+CM_ScriptTag+'> CaluMath.PM.PageMakerObjectArrayString=\''+CaluMathPageMakerString.replace(/CM_PageMakerStringDelimiter/g," '+\n' CM_PageMakerStringDelimiter ")+'\' <\/script> \n'+CaluMath.PM.NewWindowStringClosing];
      var tempjoin= temparray.join(' ');
      window["CM_TempWindow"].document.write(tempjoin);
      window["CM_TempWindow"].document.close();
      CaluMath.PM.VIEWAREA.value=[CaluMath.PM.NewWindowStringBeginning(),tempjoin].join(' ');
   }
   else{
      setTimeout('CaluMath.PM.TempWindowWriteRemainingClosureReplacement();', 10);
   };
};



///////////////////THIS LOADS A PREVIOUS VERSION OF THE PAGE THE USER IS CONSTRUCTING. IT ALLOWS THE USER TO RECOVER IF THEY DO SOMETHING THAT DESTROYS THE DOCUMENT.///////////////////////////////

//Comment: The eventhandler of this button creates the previous web page, as constructed so far, in a new pop-up window.
document.getElementById("createpreviouspage").onclick=function(){

   if(window.CaluMath.PM.VIEWAREABUTTON != null && window.CaluMath.PM.VIEWAREABUTTON.parentNode != null){
      CaluMath.PM.VIEWAREABUTTON.onclick();
   };

   //Comment: We create the inline edit menu in the main window and activate the inline edit button. 
   if(CM_TopWindow==window){
      CaluMath.PM.CreateInLineEditSelectMenu();
      document.getElementById("editinlinebutton").onclick=CaluMath.PM.InLineEditButtonOnclickFunction;
   };

   var templength=CaluMath.PM.PreviousPageArray.length;
   if(templength ==0){
      return ;
   };
   var temparray= new Array();
   var pElement=document.createElement("select");
   //Comment: We will only offer them the previous pages, not the current page. 
   var templengthminus1=templength-1;
   for(var i=0; i<templengthminus1; i++){
      temparray[i]=[i,i];
   };
   CaluMath.PM.MakeSelectMenu(pElement, temparray);
   document.getElementById("createpreviouspage").parentNode.insertBefore(pElement, document.getElementById("createpreviouspage"));

   var tempbuttonElement=document.createElement("input");
   tempbuttonElement.setAttribute("type","button");
   tempbuttonElement.style.backgroundColor="#c6b89e";
   tempbuttonElement.setAttribute("value","Load This Previous Version");
   document.getElementById("createpreviouspage").parentNode.insertBefore(tempbuttonElement, document.getElementById("createpreviouspage"));
   document.getElementById("createpreviouspage").style.display="none";

   var cancelbuttonElement=document.createElement("input");
   cancelbuttonElement.setAttribute("type","button");
   cancelbuttonElement.style.backgroundColor="#c6b89e";
   cancelbuttonElement.setAttribute("value","Cancel Loading Previous Version");
   document.getElementById("createpreviouspage").parentNode.insertBefore(cancelbuttonElement, document.getElementById("createpreviouspage"));

   cm_alert("The dropdown menu contains a list of the pages you viewed or edited. Page 0 is the first page you looked at. The last entry is the page before the the most recent page you viewed or edited. Please choose a page and press the Load This Previous Version button, or press Cancel Loading Previous Version");
   cancelbuttonElement.onclick=function(){
      tempbuttonElement.parentNode.removeChild(tempbuttonElement);
      pElement.parentNode.removeChild(pElement);
      cancelbuttonElement.parentNode.removeChild(cancelbuttonElement);
      document.getElementById("createpreviouspage").style.display="inline";
   };


   tempbuttonElement.onclick=function(){
      var tempindex= pElement.selectedIndex; 
      CaluMath.PM.PageMakerObjectArray=CaluMath.PM.ParsePageMakerPageStringNoWindow(CaluMath.PM.PreviousPageArray[tempindex]);
      tempbuttonElement.parentNode.removeChild(tempbuttonElement);
      pElement.parentNode.removeChild(pElement);
      cancelbuttonElement.parentNode.removeChild(cancelbuttonElement);
      document.getElementById("createpreviouspage").style.display="inline";
      document.getElementById("createpage").onclick();
   };
   return false;
};

//////////////////////////THIS OPENS A POP UP WINDOW OF ALL THE OBJECT THAT HAVE BEEN CREATED ORGANIZED BY DIFFERENT LISTS///////////////////

document.getElementById("viewnamedobjects").onclick=function(){

   if(window.CaluMath.PM.VIEWAREABUTTON != null && window.CaluMath.PM.VIEWAREABUTTON.parentNode != null){
      CaluMath.PM.VIEWAREABUTTON.onclick();
   };

   if(window.ViewWindow != null && !window.ViewWindow.closed && window.ViewWindow.close != null){
      window.ViewWindow.close();
   };

   //Comment: Note that CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction calls CaluMath.PM.AssignParents.
   CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction();
   CaluMath.PM.AssignCasWindowToTextChildren();

   var tempCM_ListOfLists= new Array();
   var tempCM_ListOfListsAlternateNames= new Array();
   for(var i=0; i<CaluMath.PM.ListOfLists.length; i++){
      if(CaluMath.PM.NotViewAbleListOfListsMembers.cm_contains(CaluMath.PM.ListOfLists[i])==false){
         tempCM_ListOfLists=tempCM_ListOfLists.concat(CaluMath.PM.ListOfLists[i]);
         tempCM_ListOfListsAlternateNames=tempCM_ListOfListsAlternateNames.concat(CaluMath.PM.ListOfListsAlternateNames[i]);
      };
   };
   var tempCM_ListOfListslength= tempCM_ListOfLists.length;


   if(!CM_TopWindow.CM_EditMode && (CaluMath.PM.PAGECONTROLCONTAINER.style.display == "block" || CaluMath.PM.PAGECONTROLCONTAINER.style.display == "")){
   //Comment: This allows them to edit objects
      var writestring='<div style="font-size:44px"> Here is a list of the objects you have created.</div><br><br>'; 
      for(var j=0; j<tempCM_ListOfListslength; j++){
         var namelist=CaluMath.PM.PlotlistObject[tempCM_ListOfLists[j]+"list"];
         var namelistlength = namelist.length;
         writestring= writestring+'<div style="font-size:24px"><span style="font-size:36px;color:red">This is the '+ tempCM_ListOfListsAlternateNames[j]+ ' list.</span><br><table cellspacing="20px"><tr><td><b>Number</b></td><td><b>Name</b></td><td><b>Type</b></td><td><b>Click to Edit Object</b></td></tr>';
         for(var i=0; i< namelistlength; i++){
            if(namelist[i]!= null && namelist[i][0] != null){
               if(namelist[i][2].htmlname != undefined){
                  writestring=writestring+'<tr><td>'+(i+1)+'.</td><td>'+namelist[i][0]+'</td><td>'+CaluMath.PM.PageMakerObjectLabelName(namelist[i][2])+'</td><td> <span id="'+namelist[i][2].htmlname+'"> Edit Object</span></td></tr>';
               }
                else{
                  writestring=writestring+'<tr><td>'+(i+1)+'.</td><td>'+namelist[i][0]+'</td><td>'+CaluMath.PM.PageMakerObjectLabelName(namelist[i][2])+'</td><td> <span id="'+namelist[i][2].name+'"> Edit Object</span></td></tr>';
               };
            };
         };
         writestring=writestring+'</table></div><br><br>';
      };

      for(var j=0; j<CaluMath.PM.PlotlistObject.newwindowlist.length; j++){
         if(CaluMath.PM.PlotlistObject[CaluMath.PM.PlotlistObject.newwindowlist[j][0]] && CaluMath.PM.PlotlistObject[CaluMath.PM.PlotlistObject.newwindowlist[j][0]]["namedobjectlist"] != null){
            var namelist=CaluMath.PM.PlotlistObject[CaluMath.PM.PlotlistObject.newwindowlist[j][0]]["namedobjectlist"];
            var namelistlength = namelist.length;
            writestring= writestring+'<div style="font-size:24px"><span style="font-size:36px;color:red">This is the list of all named objects in the window '+ CaluMath.PM.PlotlistObject.newwindowlist[j][0]+ '</span><br><table cellspacing="20px"><tr><td><b>Number</b></td><td><b>Name</b></td><td><b>Type</b></td><td><b>Click to Edit Object</b></td></tr>';
            for(var i=0; i< namelistlength; i++){
               if(namelist[i]!= null && namelist[i][0] != null){
                  if(namelist[i][2].htmlname != undefined){
                     writestring=writestring+'<tr><td>'+(i+1)+'.</td><td>'+namelist[i][0]+'</td><td>'+CaluMath.PM.PageMakerObjectLabelName(namelist[i][2])+'</td><td> <span id="'+namelist[i][2].htmlname+'"> Edit Object</span></td></tr>';
                  }
                  else{
                     writestring=writestring+'<tr><td>'+(i+1)+'.</td><td>'+namelist[i][0]+'</td><td>'+CaluMath.PM.PageMakerObjectLabelName(namelist[i][2])+'</td><td> <span id="'+namelist[i][2].name+'"> Edit Object</span></td></tr>';
                  };
               };
            };
            writestring=writestring+'</table></div><br><br>';
         };
         if(CaluMath.PM.PlotlistObject[CaluMath.PM.PlotlistObject.newwindowlist[j][0]] && CaluMath.PM.PlotlistObject[CaluMath.PM.PlotlistObject.newwindowlist[j][0]]["staticobjectlist"] != null){
            var namelist=CaluMath.PM.PlotlistObject[CaluMath.PM.PlotlistObject.newwindowlist[j][0]]["staticobjectlist"];
            var namelistlength = namelist.length;
            writestring= writestring+'<div style="font-size:24px"><span style="font-size:36px;color:red">This is the list of all static objects in the window '+ CaluMath.PM.PlotlistObject.newwindowlist[j][0]+ '</span><br><table cellspacing="20px"><tr><td><b>Number</b></td><td><b>Name</b></td><td><b>Type</b></td><td><b>Click to Edit Object</b></td></tr>';
            for(var i=0; i< namelistlength; i++){
               if(namelist[i]!= null && namelist[i][0] != null){
                  if(namelist[i][2].htmlname != undefined){
                     writestring=writestring+'<tr><td>'+(i+1)+'.</td><td>'+namelist[i][0]+'</td><td>'+CaluMath.PM.PageMakerObjectLabelName(namelist[i][2])+'</td><td> <span id="'+namelist[i][2].htmlname+'"> Edit Object</span></td></tr>';
                  }
                  else{
                     writestring=writestring+'<tr><td>'+(i+1)+'.</td><td>'+namelist[i][0]+'</td><td>'+CaluMath.PM.PageMakerObjectLabelName(namelist[i][2])+'</td><td> <span id="'+namelist[i][2].name+'"> Edit Object</span></td></tr>';
                  }
               };
            };
            writestring=writestring+'</table></div><br><br>';
         };
      };




      ViewWindow= CaluMath.Html.NewTempWindow(writestring, "widthfraction=.8, heightfraction=.8");
   
     //Comment: Note that we make the window clickable. However, in case the window did not load already when this command is reached, we add it to the onload event handler of the window. 
     ViewWindow.onload=function(){
        setTimeout('CaluMath.PM.MakeClickableEdit(ViewWindow);', 1000)
     };
     setTimeout('CaluMath.PM.MakeClickableEdit(ViewWindow);', 1000)
   }
   else{
   //Comment: This does not allow them to edit objects. When they invoked this button, they were editing already. 
      var writestring='<div style="font-size:44px"> Here is a list of the objects you have created.</div><br><br>'; 
      for(var j=0; j<tempCM_ListOfListslength; j++){
         var namelist=CaluMath.PM.PlotlistObject[tempCM_ListOfLists[j]+"list"];
         var namelistlength = namelist.length;
         writestring= writestring+'<div style="font-size:24px"><span style="font-size:36px;color:red">This is the '+ tempCM_ListOfListsAlternateNames[j]+ ' list.</span><br><table cellspacing="20px"><tr><td><b>Number</b></td><td><b>Name</b></td><td><b>Type</b></td></tr>';
         for(var i=0; i< namelistlength; i++){
            if(namelist[i]!= null && namelist[i][0] != null){
               writestring=writestring+'<tr><td>'+(i+1)+'.</td><td>'+namelist[i][0]+'</td><td>'+CaluMath.PM.PageMakerObjectLabelName(namelist[i][2])+'</td></tr>';
            };
         };
         writestring=writestring+'</table></div><br><br>';
      };
      ViewWindow= CaluMath.Html.NewTempWindow(writestring, "widthfraction=.8, heightfraction=.8");
   };
   return false;
};


document.getElementById("recreateobject").onclick=document.getElementById("viewnamedobjects").onclick; 


////////////////////////////THIS LOADS A PREVIOUSLY SAVED PAGE FROM YOUR HARDRIVE.///////////////////////////////////////

document.getElementById("initialloadpage").onclick=function(){

   if(window.CaluMath.PM.VIEWAREABUTTON != null && window.CaluMath.PM.VIEWAREABUTTON.parentNode != null){
      CaluMath.PM.VIEWAREABUTTON.onclick();
   };


   document.getElementById("loadfile").style.display="";
   document.getElementById("loadpage").style.display="";
   document.getElementById("loadpageweb").style.display="";
   document.getElementById("cancelloadpage").style.display="";
   document.getElementById("initialloadpagetext").style.display="";
   document.getElementById("loadfilebreak1").style.display="";
   document.getElementById("loadpagewebinput").style.display="";
   document.getElementById("loadpagewebinput").value="";
   return false;
};

document.getElementById("cancelloadpage").onclick=function(){
   document.getElementById("loadfile").style.display="none";
   document.getElementById("loadpage").style.display="none";
   document.getElementById("loadpageweb").style.display="none";
   document.getElementById("cancelloadpage").style.display="none";
   document.getElementById("initialloadpagetext").style.display="none";
   document.getElementById("loadfilebreak1").style.display="none";
   document.getElementById("loadpagewebinput").style.display="none";
};

document.getElementById("loadpage").onclick=function(){
   CaluMath.PM.LoadFile("loadpage");
};

document.getElementById("loadpageweb").onclick=function(){
   CaluMath.PM.LoadFile("loadpageweb");
};

CaluMath.PM.LoadFile=function(){
  if(arguments[0]=="loadpage" && !document.getElementById("loadfile").value.match(/\w/)){
      return cm_alert("You did not choose a file. Either choose a file or click Cancel.");
  }
  else if(arguments[0]=="loadpageweb" && !document.getElementById("loadpagewebinput").value.match(/\w/)){
      return cm_alert("You did not choose a file. Either choose a file or click Cancel.");
  };
  document.getElementById("initialloadpagetext").style.display="none";
  if(confirm("This will close your current page and replace it with the saved page you are opening. Please save your work or click cancel before continuing.")){
     if(window.CM_TempWindow != null && !window.CM_TempWindow.closed && window.CM_TempWindow.close != null){
        window.CM_TempWindow.close();
     };
   if(arguments[0]=="loadpage"){
      //Comment: This means the file is on the user's computer
      CaluMath.PM.PageMakerTargetFile=document.getElementById("loadfile").value;
//alerttest(document.getElementById("loadfile").namespaceURI);
      var pathname = decodeURI(document.location.pathname).replace(/^[\/\"\/]/,'');
//alerttest(pathname);
//alerttest(CaluMath.PM.PageMakerTargetFile);
//      var address=CaluMath.PM.PageMakerTargetFile.replace(/\\/g,"/").replace(/^[\/\"\/]/,'');
      var address=decodeURI(CaluMath.PM.PageMakerTargetFile).replace(/\\/g,"/").replace(/^[\/\"\/]/,'');
//alerttest(address);
      while(pathname.charAt(0)==address.charAt(0)){
      pathname=pathname.slice(1); 
         address=address.slice(1);
      };
      if(address.match(/\//)){
         var numberofforwardslashes= address.split("/").length;
         var tempdots="";
         for(var i=1; i<numberofforwardslashes; i++){
            tempdots=tempdots+"../";
         };
         tempdots= (tempdots+pathname).replace(/CaluMath_Page_Maker.html$/,'');
         CaluMath.PM.PageMakerTargetFilePrefix= tempdots; 
      }
      else{
         CaluMath.PM.PageMakerTargetFilePrefix='';
      };
      var openstring= "file:///"+document.getElementById("loadfile").value.replace(/\\/g,"/");
   }
   else{
      var openstring = document.getElementById("loadpagewebinput").value;
      //Comment: This means a web address is entered and will be loaded. The address can be an entire address or a path from the web location of the file that is currently opened
      if(openstring.match(/^\s*http/)){
      }
      else if(openstring.match(/^s*file/)){
      }
      else{
         openstring= "file://localhost/"+document.getElementById("loadpagewebinput").value.replace(/\"/g,"").replace(/\'/g,"").replace(/\\/g,"/");
      };
   };

   document.getElementById("loadfile").style.display="none";
   document.getElementById("loadpage").style.display="none";
   document.getElementById("loadpageweb").style.display="none";
   document.getElementById("cancelloadpage").style.display="none";
   document.getElementById("loadfilebreak1").style.display="none";
   document.getElementById("loadpagewebinput").style.display="none";
     CaluMath.PM.PlotlistObject= new Object();
     CaluMath.PM.MakeLists();

   window["CM_TempWindow"]=window.open(openstring,'',"height="+Math.round(3*window.screen.availHeight/4)+", width="+Math.round(3*window.screen.availWidth/4)+", menubar=yes, scrollbars=yes, resizable=yes");
   window["CM_TempWindow"].moveTo(0,0);
     if(CM_TempWindow.cm_windowloaded !="yes"){ 
        window.CM_TempWindow.cm_onload=function(){ 
//beginTime=new Date()
//beginTime=beginTime.getTime();
 
           CaluMath.PM.ParsePageMakerPage(); 

//endTime=new Date()
//endTime=endTime.getTime();
//alert(endTime-beginTime);

//           CaluMath.PM.PreviousPageArray=CaluMath.PM.PreviousPageArray.concat(CaluMath.PM.PageMakerObjectArrayToString());
           CaluMath.PM.PreviousPageArray=CaluMath.PM.PreviousPageArray.concat(CM_TempWindow.CaluMath.PM.PageMakerObjectArrayString.slice(0))
           CaluMath.PM.HTMLElementIndex=1+CM_TempWindow.CaluMath.PM.HTMLElementIndex; 
           CaluMath.PM.PlotNameIndex=CM_TempWindow.CaluMath.PM.PlotNameIndex;
           CaluMath.PM.TextIndex=CM_TempWindow.CaluMath.PM.TextIndex;
           CaluMath.PM.TitleIndex=CM_TempWindow.CaluMath.PM.TitleIndex;
           CaluMath.PM.SectionIndex=CM_TempWindow.CaluMath.PM.SectionIndex;
           CaluMath.PM.ParagraphIndex=CM_TempWindow.CaluMath.PM.ParagraphIndex;
           CaluMath.PM.SectionIndex=CM_TempWindow.CaluMath.PM.SectionIndex;
           CaluMath.PM.ContainerIndex=CM_TempWindow.CaluMath.PM.ContainerIndex;
           CaluMath.PM.MathTextIndex=CM_TempWindow.CaluMath.PM.MathTextIndex;
        };
        //Comment: For Safari, window.CM_TempWindow.cm_onload is defined, but in the writing to the new window it becomes undefined. So we make it a property of the CaluMath.PM.Page Maker window. When the CM_TempWindow is parsed, it does a cm_onload in client1.js it checks whether the CaluMath.PM.PageMakerObjectArrayString exists and not the cm_onload doesn't. 
        window.CaluMath.PM.PageMakerObjectArrayString= window.CM_TempWindow.cm_onload;
     }
     else{
//beginTime=new Date()
//beginTime=beginTime.getTime();
 
        CaluMath.PM.ParsePageMakerPage(); 

//endTime=new Date()
//endTime=endTime.getTime();
//alert(endTime-beginTime);

//        CaluMath.PM.PreviousPageArray=PreviousPageArray.concat(CaluMath.PM.PageMakerObjectArrayToString());
        CaluMath.PM.PreviousPageArray=CaluMath.PM.PreviousPageArray.concat(CM_TempWindow.CaluMath.PM.PageMakerObjectArrayString.slice(0))
        CaluMath.PM.HTMLElementIndex=1+CM_TempWindow.CaluMath.PM.HTMLElementIndex; 
        CaluMath.PM.PlotNameIndex=CM_TempWindow.CaluMath.PM.PlotNameIndex; 
        CaluMath.PM.TextIndex=CM_TempWindow.CaluMath.PM.TextIndex;
        CaluMath.PM.TitleIndex=CM_TempWindow.CaluMath.PM.TitleIndex;
        CaluMath.PM.SectionIndex=CM_TempWindow.CaluMath.PM.SectionIndex;
        CaluMath.PM.ParagraphIndex=CM_TempWindow.CaluMath.PM.ParagraphIndex;
        CaluMath.PM.SectionIndex=CM_TempWindow.CaluMath.PM.SectionIndex;
        CaluMath.PM.ContainerIndex=CM_TempWindow.CaluMath.PM.ContainerIndex;
    };
  };

};

///////////////////////////BELOW ARE FUNCTIONS THAT SUPPORT THE PAGEMAKER CONTROL BUTTONS FOR OPENING, EDITING ETC. OF WINDOWS.//////////

CaluMath.PM.EditWindowColorArray=["blue", "red", "green", "orange"];
CaluMath.PM.EditWindowColorArrayIndex=0;

////////////////////////////////////FUNCTION THAT WRITES THE PAGE TO A WINDOW FOR VIEWING AND SAVING//////////////////////////

CaluMath.PM.WriteNewWindowString= function(){ 
   if(arguments.length==0){
      var tempnewstring="";

      return CaluMath.PM.WriteNewWindowString(CaluMath.PM.PageMakerObjectArray);
   }
   else if(arguments[0] != null && arguments[0].cm_constructorname != "Array"){
      var tempobject= arguments[0];
      var tempnewstring ="";
      if(tempobject.cm_getsetting("cm_onlyforlists")=="yes" || tempobject.type=="textstylesheet" || tempobject.type=="headhtml"  || tempobject.type=="webpagename"){
         return tempnewstring;
      }
      else if(tempobject.type=="list" ||  tempobject.type=="table" || tempobject.type=="tablerow"  || tempobject.type=="tablecell"  ||  tempobject.type=="iframecontainer" || tempobject.type=="htmltext" || tempobject.type=="text" || tempobject.type=="coordinates" || tempobject.type=="buttonaction" || tempobject.type=="forloop" || tempobject.type=="conditional" || tempobject.type=="routine"){
         tempnewstring=tempnewstring+ tempobject.cm_openingtag()+tempobject.cm_writehtml();
         tempnewstring=tempnewstring + CaluMath.PM.WriteNewWindowString(tempobject.childarray) +tempobject.cm_closingtag();
         return tempnewstring; 
      }
      else if(tempobject.type=="newwindow"){
         var newwindowchildarraylength= tempobject.childarray.length;
         var newwindowstring=' \' \' ';
         for(var newwindowi = 0; newwindowi<newwindowchildarraylength; newwindowi++){
             newwindowstring=newwindowstring+ ', \''+CaluMath.PM.WriteNewWindowString(tempobject.childarray[newwindowi]).cm_newwindowreplacesinglequotes()+'\'';  
         };
         return tempobject.cm_openingtag()+ ' ' + newwindowstring + ',\' \'' +tempobject.cm_closingtag();
      }
      else if(tempobject.type !="newplot"){
         tempnewstring=tempnewstring+ tempobject.cm_openingtag()+tempobject.cm_writehtml()+tempobject.cm_closingtag();
         tempnewstring=tempnewstring + CaluMath.PM.WriteNewWindowString(tempobject.childarray); 
         return tempnewstring; 
      }
      else if(tempobject.type=="newplot"){
         tempnewstring=tempnewstring+tempobject.cm_openingtag(); 

         var xstart=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_xstart");
         var xfinish=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_xfinish");
         var ystart=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_ystart");
         var yfinish=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_yfinish");
         var xaxisat=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_xaxisat");
         var yaxisat=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_yaxisat");

         if(xstart != "default"){
            xstart='"'+CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_xstart")+'".cm_evalmath()';
         };
         if(xfinish != "default"){
            xfinish='"'+CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_xfinish")+'".cm_evalmath()';;
         };
         if(ystart != "default"){
            ystart='"'+CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_ystart")+'".cm_evalmath()';;
         };
         if(yfinish != "default"){
            yfinish='"'+CaluMath.PM.GetPageMakerObjectSetting(tempobject,"cm_yfinish")+'".cm_evalmath()';;
         };

           
         //Comment: This replaces the above. We again have functionplots as children of NewPlots. We had changed this before which was why we had the section above. 
         var childarray= tempobject.childarray; 
         var newchildarray = new Array();

         for(var jjj=0; jjj<childarray.length; jjj++){
            if(childarray[jjj].type =="functionplot" && childarray[jjj].cm_getsetting("cm_onlyforlists") != "yes" ){
               newchildarray.push(childarray[jjj]);
            };
         };

         if(newchildarray.length ==0){
            if(xstart == "default"){
               xstart=-1;
            };
            if(xfinish == "default"){
               xfinish= 1;
            };
            if(ystart == "default"){
               ystart=-1;
            };
            if(yfinish == "default"){
               yfinish= 1;
            };
         }
         else{

         childarray=newchildarray;
         var xstartarray= new Array();
         var xfinisharray= new Array();
         var ystartarray= new Array();
         var yfinisharray= new Array();
         var functionname;
         if(xstart=="default" && xfinish=="default"){   
            var defaultvalueused=true;
            for(var ttavoidingnameconflicts=0; ttavoidingnameconflicts<childarray.length; ttavoidingnameconflicts++){ 
//               functionname=CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"definedfunction").cm_javascript();  
               functionname='eval("'+CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"definedfunction")+'".cm_javascript())';  
//alert([CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"definedfunction"), functionname]);
               xstartarray[ttavoidingnameconflicts]='"'+CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"cm_xstart")+'".cm_evalmath()';
               xfinisharray[ttavoidingnameconflicts]='"'+CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"cm_xfinish")+'".cm_evalmath()';
               ystartarray[ttavoidingnameconflicts]=functionname+'.cm_computemax('+xstartarray[ttavoidingnameconflicts]+','+xfinisharray[ttavoidingnameconflicts]+')[0]'; 
               yfinisharray[ttavoidingnameconflicts]=functionname+'.cm_computemax('+xstartarray[ttavoidingnameconflicts]+','+xfinisharray[ttavoidingnameconflicts]+')[1]'; 
            };
            if(yaxisat != null && yaxisat.length != 0 && yaxisat != "n/a" && yaxisat != "default" && yaxisat != "auto"){
               xstart='CaluMath.Html.AdjustBeginningPlotValues(Math.min.apply(null,['+xstartarray+','+yaxisat.cm_evalmath()+']))';
               xfinish='CaluMath.Html.AdjustEndingPlotValues(Math.max.apply(null,['+xfinisharray+','+yaxisat.cm_evalmath()+']))';
            }
            else{
               xstart='CaluMath.Html.AdjustBeginningPlotValues(Math.min.apply(null,['+xstartarray+']))';
               xfinish='CaluMath.Html.AdjustEndingPlotValues(Math.max.apply(null,['+xfinisharray+']))';
            };
         }
         else if(xstart=="default"){  
            var defaultvalueused=true;
            for(var ttavoidingnameconflicts=0; ttavoidingnameconflicts<childarray.length; ttavoidingnameconflicts++){ 
//               functionname=CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"definedfunction").cm_javascript(); 
               functionname='eval("'+CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"definedfunction")+'".cm_javascript())';  
               xstartarray[ttavoidingnameconflicts]='"'+CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"cm_xstart")+'".cm_evalmath()';
               xfinisharray[ttavoidingnameconflicts]='Math.min('+xfinish+',"'+CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"cm_xfinish")+'".cm_evalmath())';
               ystartarray[ttavoidingnameconflicts]=functionname+'.cm_computemax('+xstartarray[ttavoidingnameconflicts]+','+xfinisharray[ttavoidingnameconflicts]+')[0]'; 
               yfinisharray[ttavoidingnameconflicts]=functionname+'.cm_computemax('+xstartarray[ttavoidingnameconflicts]+','+xfinisharray[ttavoidingnameconflicts]+')[1]'; 
            };
            if(yaxisat != null && yaxisat.length != 0 && yaxisat != "n/a" && yaxisat != "default" && yaxisat != "auto"){
               xstart='CaluMath.Html.AdjustBeginningPlotValues(Math.min.apply(null,['+xstartarray+','+yaxisat.cm_evalmath()+']))';
            }
            else{
               xstart='CaluMath.Html.AdjustBeginningPlotValues(Math.min.apply(null,['+xstartarray+']))';
            };
         }
         else if(xfinish=="default"){   
            var defaultvalueused=true;
            for(var ttavoidingnameconflicts=0; ttavoidingnameconflicts<childarray.length; ttavoidingnameconflicts++){ 
//               functionname=CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"definedfunction").cm_javascript(); 
               functionname='eval("'+CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"definedfunction")+'".cm_javascript())';  
               xstartarray[ttavoidingnameconflicts]='Math.max('+xstart+',"'+CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"cm_xstart")+'".cm_evalmath())';
               xfinisharray[ttavoidingnameconflicts]='"'+CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"cm_xfinish")+'".cm_evalmath()';
               ystartarray[ttavoidingnameconflicts]=functionname+'.cm_computemax('+xstartarray[ttavoidingnameconflicts]+','+xfinisharray[ttavoidingnameconflicts]+')[0]'; 
               yfinisharray[ttavoidingnameconflicts]=functionname+'.cm_computemax('+xstartarray[ttavoidingnameconflicts]+','+xfinisharray[ttavoidingnameconflicts]+')[1]'; 
            };
            if(yaxisat != null && yaxisat.length != 0 && yaxisat != "n/a" && yaxisat != "default" && yaxisat != "auto"){
               xfinish='CaluMath.Html.AdjustEndingPlotValues(Math.max.apply(null,['+xfinisharray+','+yaxisat.cm_evalmath()+']))';
            }
            else{
               xfinish='CaluMath.Html.AdjustEndingPlotValues(Math.max.apply(null,['+xfinisharray+']))';
            };
            if(ystart=="default"){
               if(xaxisat != "n/a" && xaxisat != "default" && xaxisat != "auto"){
                  ystart='1.2*Math.min.apply(null,['+ystartarray+'])';
               }
               else{
                  ystart='1.2*Math.min.apply(null,['+ystartarray+'])';
               };
            }
         }
         else{     //alerttest("case3");    
            for(var ttavoidingnameconflicts=0; ttavoidingnameconflicts<childarray.length; ttavoidingnameconflicts++){ 
//               functionname=CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"definedfunction").cm_javascript(); 
               functionname='eval("'+CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"definedfunction")+'".cm_javascript())';  
               xstartarray[ttavoidingnameconflicts]='Math.max('+xstart+',"'+CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"cm_xstart")+'".cm_evalmath())';
               xfinisharray[ttavoidingnameconflicts]='Math.min('+xfinish+',"'+CaluMath.PM.GetPageMakerObjectSetting(childarray[ttavoidingnameconflicts],"cm_xfinish")+'".cm_evalmath())';
               ystartarray[ttavoidingnameconflicts]=functionname+'.cm_computemax('+xstartarray[ttavoidingnameconflicts]+','+xfinisharray[ttavoidingnameconflicts]+')[0]'; 
               yfinisharray[ttavoidingnameconflicts]=functionname+'.cm_computemax('+xstartarray[ttavoidingnameconflicts]+','+xfinisharray[ttavoidingnameconflicts]+')[1]'; 
            };
         };

         if(ystart=="default"){
            var defaultvalueused=true;
            if(xaxisat != null && xaxisat.length != 0 && xaxisat != "n/a" && xaxisat != "default" && xaxisat != "auto"){
               ystart='CaluMath.Html.AdjustBeginningPlotValues(Math.min.apply(null,['+ystartarray+','+xaxisat.cm_evalmath()+'])) ';
            }
            else{
               ystart='CaluMath.Html.AdjustBeginningPlotValues(Math.min.apply(null,['+ystartarray+'])) ';
            }
         }
         if(yfinish=="default"){
            var defaultvalueused=true;
            if(xaxisat != null && xaxisat.length != 0 && xaxisat != "n/a" && xaxisat != "default" && xaxisat != "auto"){
               yfinish='CaluMath.Html.AdjustEndingPlotValues(Math.max.apply(null,['+yfinisharray+','+xaxisat.cm_evalmath()+'])) ';
            }
            else{
               yfinish='CaluMath.Html.AdjustEndingPlotValues(Math.max.apply(null,['+yfinisharray+'])) ';
            };
         };

         };  //Comment: This is the end else for if childarray.lenght ==0. It is not properly indented. 

         var tempwritestring1= tempobject.cm_writehtml();
         tempwritestring1= tempwritestring1.replace(/\"XSTARTdefaultXSTART\"/, xstart).replace(/\"XFINISHdefaultXFINISH\"/, xfinish).replace(/\"YSTARTdefaultYSTART\"/, ystart).replace(/\"YFINISHdefaultYFINISH\"/, yfinish);
         tempwritestring1=tempwritestring1.replace(/XSTART/g, "").replace(/XFINISH/g, "").replace(/YSTART/g, "").replace(/YFINISH/g, "");
         tempnewstring=tempnewstring+tempwritestring1;
         var tempnewstring2= CaluMath.PM.WriteNewWindowString(tempobject.childarray);
         tempnewstring=tempnewstring+ tempnewstring2 + tempobject.cm_closingtag(); 
         return tempnewstring;
      };
   }
   else if(arguments[0] != null && arguments[0].cm_constructorname == "Array"){
      tempnewstring="";
      var temparray=arguments[0];
      var temparraylength= temparray.length;

      for(var i=0; i<temparraylength; i++){ //alerttest(temparray[i]);
         tempnewstring=tempnewstring+CaluMath.PM.WriteNewWindowString(temparray[i]);
      };
      return tempnewstring;
   };
};   



/////////////// THIS OPENS THE CREATED PAGE IN EDIT MODE///////////////


document.getElementById("editpage").onclick=function(){
   CM_AlreadyAlertedFromEditWindow=false;
   CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction();
   CaluMath.PM.AssignCasWindowToTextChildren();

   CaluMath.PM.OpenTempWindowInEditMode("edit");

   //Comment: We deactivate the inline edit button, so the page can't be edited while an active Edit window is opened.
   document.getElementById("editinlinebutton").onclick=null;

//Comment: It slows down things too much to always be adding to the PreviousPageArray when you are editing. 

   if(window.CaluMath.PM.VIEWAREABUTTON != null && window.CaluMath.PM.VIEWAREABUTTON.parentNode != null){
      CaluMath.PM.VIEWAREABUTTON.onclick();
   };
   return false;
};


////////////////////////////////////////////////FUNCTIONS FOR EDITING THE PAGE////////////////////////////////////////////




CaluMath.PM.OnMouseOverClosureRemovalFunction=function(){
   this.style.cursor="pointer";
};
CaluMath.PM.OnMouseOutClosureRemovalFunction=function(){
   this.style.cursor="";
};


/*
CaluMath.PM.WriteNewWindowStringEditMode= function(){  
   if(arguments.length==0){
      var tempnewstring=['<\/script>\n<h1 style="text-align:center; font-size:52px" > This Displays the Structure of Your Page<\/h1>\n<h2 style="text-align:center; font-size:32px" > Click On Any Item To Edit It<\/h2>\n<ol >'];
      return tempnewstring.concat(CaluMath.PM.WriteNewWindowStringEditMode(CaluMath.PM.PageMakerObjectArray)).join("");
   }
   else if(arguments[0] != null && arguments[0].cm_constructorname != "Array"){
      var tempobject= arguments[0];
      var tempnewstring =new Array();
      if(!( tempobject.type=="linearplot" ||    tempobject.type=="tangent" ||   tempobject.type=="functionplot" ||   tempobject.type=="updatablefunctionplot" || tempobject.type=="arcplot" ||   tempobject.type=="iframe" ||   tempobject.type=="iframecontainer" ||  tempobject.type=="buttonaction" ||  tempobject.type=="forloop" || tempobject.type=="conditional" || tempobject.type=="routine" ||  tempobject.type=="newplot"  ||  tempobject.type=="newwindow" ||  tempobject.type=="table" ||  tempobject.type=="tablerow" ||  tempobject.type=="tablecell" || tempobject.type=="list" || tempobject.type=="clipboard" ) ){
            tempnewstring.push('\n',CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex+5),'<li >',tempobject.cm_writeedithtml(),'<\/li>');
            return tempnewstring.join(""); 
      }
      else{
         var tempobjectchildarray=tempobject.childarray; 
         var tempobjectchildarraylength=tempobjectchildarray.length; 
         if(tempobjectchildarraylength>0){
            tempnewstring.push(' \n<li style="color:',(CaluMath.PM.EditWindowColorArray[CaluMath.PM.EditWindowColorArrayIndex++ % CaluMath.PM.EditWindowColorArray.length]),'">', tempobject.cm_writeedithtml(), CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex),'<ol >');
            for(var i=0; i< tempobjectchildarraylength; i++){
               tempnewstring.push(CaluMath.PM.WriteNewWindowStringEditMode(tempobjectchildarray[i]));
           };
            tempnewstring.push(CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex),'<\/ol><\/li>');
            CaluMath.PM.EditWindowColorArrayIndex--;
         }
         else{
            tempnewstring.push('\n',CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex+5),'<li >',tempobject.cm_writeedithtml(),'<\/li>') ;
         };
         return tempnewstring.join(""); 
      }; 
   }
   else if(arguments[0] != null && arguments[0].cm_constructorname == "Array"){
      tempnewstring=new Array();
      var temparray=arguments[0];
      var temparraylength= temparray.length;

      for(var i=0; i<temparraylength; i++){ //alerttest(temparray[i]);
         tempnewstring.push(CaluMath.PM.WriteNewWindowStringEditMode(temparray[i]));
      };
      return tempnewstring.join("");
   };
};   
*/

CaluMath.PM.WriteNewWindowStringEditMode1= function(){  
   if(arguments.length==0){
      var tempnewstring=['<\/script>\n<h1 style="text-align:center; font-size:52px" > This Displays the Structure of Your Page<\/h1>\n<h2 style="text-align:center; font-size:32px" > Click On Any Item To Edit It<\/h2>\n<ol >', '<input type="button" id="expandallbutton" value="Expand All" /><input type="button" id="collapseallbutton" value="Collapse All" /><script>document.getElementById("expandallbutton").onclick=function(){ var inputlist=document.getElementsByTagName("input"); var inputlistlength= inputlist.length; for(var i=0; i<inputlistlength; i++){   inputlisti= inputlist[i]; if(inputlisti.getAttribute("type")=="button" && inputlisti.id.match(/buttonplus/) != null){inputlisti.onclick();}; }; };    document.getElementById("collapseallbutton").onclick=function(){ var inputlist=document.getElementsByTagName("input"); var inputlistlength= inputlist.length; for(var i=0; i<inputlistlength; i++){   inputlisti= inputlist[i]; if(inputlisti.getAttribute("type")=="button" && inputlisti.id.match(/buttonminus/) != null){inputlisti.onclick();}; };};<\/script>'];
      return tempnewstring.concat(CaluMath.PM.WriteNewWindowStringEditMode(CaluMath.PM.PageMakerObjectArray)).join("");
   }
   else if(arguments[0] != null && arguments[0].cm_constructorname != "Array"){
      var tempobject= arguments[0];
      var tempnewstring =new Array();
      if(!( tempobject.type=="linearplot" ||    tempobject.type=="tangent" ||   tempobject.type=="functionplot" ||   tempobject.type=="updatablefunctionplot" || tempobject.type=="arcplot" ||   tempobject.type=="iframe" ||   tempobject.type=="iframecontainer" ||  tempobject.type=="buttonaction" ||  tempobject.type=="forloop" || tempobject.type=="conditional" || tempobject.type=="routine" ||  tempobject.type=="newplot"  ||  tempobject.type=="newwindow" ||  tempobject.type=="table" ||  tempobject.type=="tablerow" ||  tempobject.type=="tablecell" || tempobject.type=="list" || tempobject.type=="clipboard" ) ){
            tempnewstring=tempnewstring.concat('\n',CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex+5),'<li >',tempobject.cm_writeedithtml1(),'<\/li>');
            return tempnewstring; 
      }
      else{
         var tempobjectchildarray=tempobject.childarray; 
         var tempobjectchildarraylength=tempobjectchildarray.length; 
         if(tempobjectchildarraylength>0){
            tempnewstring=tempnewstring.concat(' \n<li style="color:',(CaluMath.PM.EditWindowColorArray[CaluMath.PM.EditWindowColorArrayIndex++ % CaluMath.PM.EditWindowColorArray.length]),'">', tempobject.cm_writeedithtml1(), CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex),'<ol >');
            for(var i=0; i< tempobjectchildarraylength; i++){
               tempnewstring=tempnewstring.concat(CaluMath.PM.WriteNewWindowStringEditMode(tempobjectchildarray[i]));
           };
            tempnewstring=tempnewstring.concat(CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex),'<\/ol><\/li>');
            CaluMath.PM.EditWindowColorArrayIndex--;
         }
         else{
            tempnewstring=tempnewstring.concat('\n',CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex+5),'<li >',tempobject.cm_writeedithtml1(),'<\/li>') ;
         };
         return tempnewstring; 
      }; 
   }
   else if(arguments[0] != null && arguments[0].cm_constructorname == "Array"){
      tempnewstring=new Array();
      var temparray=arguments[0];
      var temparraylength= temparray.length;

      for(var i=0; i<temparraylength; i++){ //alerttest(temparray[i]);
         tempnewstring=tempnewstring.concat(CaluMath.PM.WriteNewWindowStringEditMode(temparray[i]));
      };
      return tempnewstring;
   };
};   


CaluMath.PM.WriteNewWindowStringEditMode= function(){  
   if(arguments.length==0){
      var tempnewstring='<\/script>\n<h1 style="text-align:center; font-size:52px" > This Displays the Structure of Your Page<\/h1>\n<h2 style="text-align:center; font-size:32px" > Click On Any Item To Edit It<\/h2>\n<ol ><input type="button" id="expandallbutton" value="Expand All" /><input type="button" id="collapseallbutton" value="Collapse All" /><script type="text/javascript">document.getElementById("expandallbutton").onclick=function(){ var inputlist=document.getElementsByTagName("input"); var inputlistlength= inputlist.length; for(var i=0; i<inputlistlength; i++){   inputlisti= inputlist[i]; if(inputlisti.getAttribute("type")=="button" && inputlisti.id.match(/buttonplus/) != null){inputlisti.onclick();}; }; };    document.getElementById("collapseallbutton").onclick=function(){ var inputlist=document.getElementsByTagName("input"); var inputlistlength= inputlist.length; for(var i=0; i<inputlistlength; i++){   inputlisti= inputlist[i]; if(inputlisti.getAttribute("type")=="button" && inputlisti.id.match(/buttonminus/) != null){inputlisti.onclick();}; };};<\/script>';
      return tempnewstring + CaluMath.PM.WriteNewWindowStringEditMode(CaluMath.PM.PageMakerObjectArray);
   }
   else if(arguments[0] != null && arguments[0].cm_constructorname != "Array"){
      var tempobject= arguments[0];
      var tempnewstring ="";
      if(!( tempobject.type=="linearplot" ||    tempobject.type=="tangent" ||   tempobject.type=="functionplot" ||   tempobject.type=="updatablefunctionplot" || tempobject.type=="arcplot" ||   tempobject.type=="iframe" ||   tempobject.type=="iframecontainer" ||  tempobject.type=="buttonaction" ||  tempobject.type=="forloop" || tempobject.type=="conditional" || tempobject.type=="routine" ||  tempobject.type=="newplot"  ||  tempobject.type=="newwindow" ||  tempobject.type=="table" ||  tempobject.type=="tablerow" ||  tempobject.type=="tablecell" || tempobject.type=="list" || tempobject.type=="clipboard" ) ){
            tempnewstring=tempnewstring+'\n'+CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex+5)+'<li >'+ tempobject.cm_writeedithtml() +'<\/li>' ;
            return tempnewstring; 
      }
      else{
         var tempobjectchildarray=tempobject.childarray; 
         var tempobjectchildarraylength=tempobjectchildarray.length; 
         if(tempobjectchildarraylength>0){
//            tempnewstring=tempnewstring+' \n<li style="color:'+(CaluMath.PM.EditWindowColorArray[CaluMath.PM.EditWindowColorArrayIndex++ % CaluMath.PM.EditWindowColorArray.length])+'">'+ tempobject.cm_writeedithtml() +CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex)+'<ol >';
            if( tempobject.type=="buttonaction" ||  tempobject.type=="forloop" || tempobject.type=="conditional" || tempobject.type=="routine" ||  tempobject.type=="newwindow" ||  tempobject.type=="table" || tempobject.type=="list"  ){
               tempnewstring=tempnewstring+' \n<li style="color:'+(CaluMath.PM.EditWindowColorArray[CaluMath.PM.EditWindowColorArrayIndex++ % CaluMath.PM.EditWindowColorArray.length])+'">'+ tempobject.cm_writeedithtml() +CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex)+'<ol id="'+tempobject.htmlname+'orderedlist" style="display:none" >';
            }
            else{
               tempnewstring=tempnewstring+' \n<li style="color:'+(CaluMath.PM.EditWindowColorArray[CaluMath.PM.EditWindowColorArrayIndex++ % CaluMath.PM.EditWindowColorArray.length])+'">'+ tempobject.cm_writeedithtml() +CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex)+'<ol id="'+tempobject.htmlname+'orderedlist" >';
            };
            for(var i=0; i< tempobjectchildarraylength; i++){
               tempnewstring=tempnewstring+''+CaluMath.PM.WriteNewWindowStringEditMode(tempobjectchildarray[i])+'';
           };
            tempnewstring=tempnewstring+''+CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex)+'<\/ol><\/li>';
            CaluMath.PM.EditWindowColorArrayIndex--;
         }
         else{
            tempnewstring=tempnewstring+'\n'+CaluMath.PM.MakeSpaces(5*CaluMath.PM.EditWindowColorArrayIndex+5)+'<li >'+ tempobject.cm_writeedithtml() +'<\/li>' ;
         };
         return tempnewstring; 
      }; 
   }
   else if(arguments[0] != null && arguments[0].cm_constructorname == "Array"){
      tempnewstring="";
      var temparray=arguments[0];
      var temparraylength= temparray.length;

      for(var i=0; i<temparraylength; i++){ //alerttest(temparray[i]);
         tempnewstring=tempnewstring+CaluMath.PM.WriteNewWindowStringEditMode(temparray[i]);
      };
      return tempnewstring;
   };
};   



CaluMath.PM.OpenTempWindowInEditMode=function(){
   //Comment: The expected arguments[0] are either "edit" or "rearrange" or "import". Arguments[1] is the array that is being imported from another window. It is only used for type "import".
   var type=arguments[0];
   var importarray=arguments[1];
   CaluMath.PM.TempWindowWriteRemainingImportPageMode.cm_importarray=importarray; 


   if(window.CM_TempWindow != null && !window.CM_TempWindow.closed &&  window.CM_TempWindow.close != null &&  navigator.appVersion.toString().match(/Safari/) != null){
      CM_TempWindow.close();
      if(window.outerWidth != null){
         window["CM_TempWindow"]=window.open('','',"height="+Math.round(3*window.outerHeight/4)+", width="+Math.round(3*window.outerWidth/4)+", menubar=yes, scrollbars=yes, resizable=yes");
      }
      else{
         window["CM_TempWindow"]=window.open('','',"height="+Math.round(3*window.screen.availHeight/4)+", width="+Math.round(3*window.screen.availWidth/4)+", menubar=yes, scrollbars=yes, resizable=yes");
      };
   }
   else if(window.CM_TempWindow == null || window.CM_TempWindow.closed == true){
      if(window.outerWidth != null){
         window["CM_TempWindow"]=window.open('','',"height="+Math.round(3*window.outerHeight/4)+", width="+Math.round(3*window.outerWidth/4)+", menubar=yes, scrollbars=yes, resizable=yes");
      }
      else{
         window["CM_TempWindow"]=window.open('','',"height="+Math.round(3*window.screen.availHeight/4)+", width="+Math.round(3*window.screen.availWidth/4)+", menubar=yes, scrollbars=yes, resizable=yes");
      };
   }
   else{
      window.CM_TempWindow.blur();
      window.CM_TempWindow.document.open();
      window.CM_TempWindow.focus();
   };
   window["CM_TempWindow"].moveTo(0,0);


   if(type=="edit"){
      setTimeout('window["CM_TempWindow"].document.write(CaluMath.PM.NewWindowStringBeginning("no", ".cm_editentry{font-size:16px}.cm_editentrytype{font-weight:bold}.cm_editentryname{font-style:italic}")); CaluMath.PM.TempWindowWriteRemainingEditMode() ' ,10);  //CM_TempWindow.document.close()
   }
   else if(type=="rearrange"){
      setTimeout('window["CM_TempWindow"].document.write(CaluMath.PM.NewWindowStringBeginning("no",".cm_editentry{font-size:16px}.cm_editentrytype{font-weight:bold}.cm_editentryname{font-style:italic}")); CaluMath.PM.TempWindowWriteRemainingRearrangeMode() ' ,10);  //CM_TempWindow.document.close()
   }
   else{
      setTimeout('window["CM_TempWindow"].document.write(CaluMath.PM.NewWindowStringBeginning("no",".cm_editentry{font-size:16px}.cm_editentrytype{font-weight:bold}.cm_editentryname{font-style:italic}")); CaluMath.PM.TempWindowWriteRemainingImportPageMode() ' ,10);  //CM_TempWindow.document.close()
   }

   return CM_TempWindow;
};

//Comment: This write the closing html and javascript of a document that is being edited. 

CaluMath.PM.TempWindowWriteRemainingEditMode=function(){
   if(window["CM_TempWindow"].CM_ClientAddpointsLoaded=="yes"){

      window["CM_TempWindow"].document.write(' '+CaluMath.PM.WriteNewWindowStringEditMode()+'<\/ol> \n '+CM_ScriptTag+'>window.cm_onload=function(){ window.opener.CaluMath.PM.MakeClickableEdit(window); window.focus(); window.cm_onload=null;} <\/script>\n'+CaluMath.PM.NewWindowStringClosing);
//      window["CM_TempWindow"].document.write('<input type="button" id="expandallbutton" value="Expand All" /><input type="button" id="collapseallbutton" value="Collapse All" /><script type="text/javascript">document.getElementById("expandallbutton").onclick=function(){ var inputlist=document.getElementsByTagName("input"); var inputlistlength= inputlist.length; for(var i=0; i<inputlistlength; i++){   inputlisti= inputlist[i]; if(inputlisti.getAttribute("type")=="button" && inputlisti.id.match(/buttonplus/) != null){inputlisti.onclick();}; }; };    document.getElementById("collapseallbutton").onclick=function(){ var inputlist=document.getElementsByTagName("input"); var inputlistlength= inputlist.length; for(var i=0; i<inputlistlength; i++){   inputlisti= inputlist[i]; if(inputlisti.getAttribute("type")=="button" && inputlisti.id.match(/buttonminus/) != null){inputlisti.onclick();}; };};<\/script> '+CaluMath.PM.WriteNewWindowStringEditMode()+'<\/ol> \n '+CM_ScriptTag+'>window.cm_onload=function(){ window.opener.CaluMath.PM.MakeClickableEdit(window); window.focus(); window.cm_onload=null;} <\/script>\n'+CaluMath.PM.NewWindowStringClosing);
      window["CM_TempWindow"].document.close();
   }
   else{
      setTimeout('CaluMath.PM.TempWindowWriteRemainingEditMode();', 10);
   };
};


CaluMath.PM.MakeClickableEdit=function(){ 
   //Comment: This is the main function for editing text by making it clickable. 
   CM_TopWindow.CM_AlreadyAlertedFromEditWindow=false;
   var tempwindow=arguments[0];
   if(tempwindow==null || tempwindow.closed==true){
      return 
   };

   if(arguments[1] != null){
      var tempnode = arguments[1];
   }
   else{
      for(var i=0; i<tempwindow.frames.length; i++){
         CaluMath.PM.MakeClickableEdit(tempwindow.frames[i], tempwindow.frames[i].document.body);
      };
      tempnode=tempwindow.document.body;
   };

   if(tempwindow != null && !tempwindow.closed && tempnode != null && tempnode.getAttribute != null  && tempnode.getAttribute("id") != null && CM_TopWindow.CaluMath.PM.PageMakerObjectParent[tempnode.id]!= null ){
      tempnode.onmouseover=CaluMath.PM.OnMouseOverClosureRemovalFunction;
      tempnode.onmouseout=CaluMath.PM.OnMouseOutClosureRemovalFunction;
      tempnode.onclick=CaluMath.PM.EditWindowOnClickClosureRemovalFunction;
   };

   if(tempnode != null){
      var children = tempnode.childNodes;
      if(children != null){
         var childrenlength =children.length;
         for(var i=0; i<childrenlength; i++){
            if(tempwindow !=null && !tempwindow.closed ==true){
               CaluMath.PM.MakeClickableEdit(tempwindow, children[i]); 
            };
         };
      };
   }; 
};   

CaluMath.PM.EditWindowOnClickClosureRemovalFunction=function(){ 
   if(!CM_TopWindow.CM_AlreadyAlertedFromEditWindow){  //alerttest([this.getAttribute("id"),CM_TopWindow.CM_AlreadyAlertedFromEditWindow]);
      if(this.getAttribute("id").match(/.*htmlelement[0-9]*/) ){
         if(CM_TopWindow.CaluMath.PM.PageMakerObjectParent[this.getAttribute("id").match(/.*htmlelement[0-9]*/)].type !="buttonaction"){ //alerttest("first");
//            CM_TopWindow.CaluMath.PM.DefineFunctionsInTopWindow(window.CM_TempWindow);
            CM_TopWindow.CaluMath.PM.ReCreateObject(CM_TopWindow.CaluMath.PM.PageMakerObjectParent[this.getAttribute("id").match(/.*htmlelement[0-9]*/)]);                        
            CM_TopWindow.CM_AlreadyAlertedFromEditWindow=true; 
            if(window.CM_TopWindow != null && window.CM_TopWindow.ViewWindow != null){
               CM_TopWindow.ViewWindow.close();
            };
            window.focus();
         }
         else{ //alerttest("second");
//            CM_TopWindow.CaluMath.PM.DefineFunctionsInTopWindow(window.CM_TempWindow);
            CM_TopWindow.CaluMath.PM.ReCreateObject(CM_TopWindow.CaluMath.PM.PageMakerObjectParent[this.getAttribute("id").match(/.*htmlelement[0-9]*/)]);                        
            CM_TopWindow.CM_AlreadyAlertedFromEditWindow=true; 
            if(window.CM_TopWindow != null && window.CM_TopWindow.ViewWindow != null){
               CM_TopWindow.ViewWindow.close();
            };
            window.focus();
         };
      }
      else{ //alerttest("third");
//         CM_TopWindow.CaluMath.PM.DefineFunctionsInTopWindow(window.CM_TempWindow);
         CM_TopWindow.CaluMath.PM.ReCreateObject(CM_TopWindow.CaluMath.PM.PageMakerObjectParent[this.getAttribute("id")]);                        
         CM_TopWindow.CM_AlreadyAlertedFromEditWindow=true; 
         if(window.CM_TopWindow != null && window.CM_TopWindow.ViewWindow != null){
            CM_TopWindow.ViewWindow.close();
         };
         window.focus();
      };
   };
};


///////////////////////////FUNCTIONS FOR CUT AND PASTE  BUTTON (WHICH IS CALLED REARRANGE IN SOME OF THESE FUNCTIONS) //////////////


document.getElementById("cutandpastepage").onclick=function(){
   //Comment: CaluMath.PM.CutAndPasteTransferArrayString contains a string version of what they just copied. If they paste something, they are asked if they would like to paste it again. If they click the cutandpastepage link, however, we delete this reference, so they are not asked to paste this item again; in other words, we assume that if they click the cutandpastepage link, they are starting fresh. 
   CaluMath.PM.CutAndPasteTransferArray= new Array();
   CaluMath.PM.CutAndPasteTransferArrayString= null;

   var cutandpastewindow=CaluMath.PM.OpenTempWindowInEditMode("rearrange");
   if(window.CaluMath.PM.VIEWAREABUTTON != null && window.CaluMath.PM.VIEWAREABUTTON.parentNode != null){
      CaluMath.PM.VIEWAREABUTTON.onclick();
   };

   //Comment: We deactivate the inline edit button, so the page can't be edited while it is being Cut and Pasted.
   document.getElementById("editinlinebutton").onclick=null;

   return false;

};

CaluMath.PM.CutAndPastePageOnClickMimic= function(){

   //Comment: This does everything the cutandpastpagelink onclick action does, except for deleting the references to CaluMath.PM.CutAndPasteTransferArray and CaluMath.PM.CutAndPasteTransferArrayString. CaluMath.PM.CutAndPastePageOnClickMimic is called after they paste something before or after another element. It allows them to past the same thing again; therefore the references to CaluMath.PM.CutAndPasteTransferArray and CaluMath.PM.CutAndPasteTransferArrayString are not deleted. 
   var cutandpastewindow=CaluMath.PM.OpenTempWindowInEditMode("rearrange");
   if(window.CaluMath.PM.VIEWAREABUTTON != null && window.CaluMath.PM.VIEWAREABUTTON.parentNode != null){
      CaluMath.PM.VIEWAREABUTTON.onclick();
   };

   //Comment: We deactivate the inline edit button, so the page can't be edited while it is being Cut and Pasted.
   document.getElementById("editinlinebutton").onclick=null;
};


//Comment: When Cutting and Pasting, it is important to know if the object you are cutting or pasting is from the Clipboard or not.//

CaluMath.PM.IsContainedInClipBoard=function(){
   var tempobject=arguments[0];
   if(tempobject==CaluMath.PM.ClipBoard){
      return true;
   };
   var tempreturn=false;
   var tempobjectparent =tempobject.parent; 
   containedinclipboardloop:
   while(tempobjectparent != null){
     if(tempobjectparent==CaluMath.PM.ClipBoard){
        tempreturn=true;
        break containedinclipboardloop;
     }
     else if(tempobjectparent==CaluMath.PM.PageMakerObjectArray){
        tempreturn=false;
        break containedinclipboardloop;
     }
     else{
        tempobjectparent=tempobjectparent.parent;
     };
   };
   return tempreturn;
};



CaluMath.PM.TempWindowWriteRemainingRearrangeMode= function(){ 
   if(window["CM_TempWindow"].CM_ClientAddpointsLoaded=="yes"){
//      var CaluMathPageMakerString= CaluMath.PM.PageMakerObjectArrayToString();
      var tempnewstring='<\/script>\n<br><div id="instructionscontainer"  style="width:850px; text-align:center;" ><h1 style="font-size:xx-large"> Cut And Paste Window</h1> <input type="button" id="instructionsbutton" value="Click To See Instructions"/><input type="button" id="hideinstructionsbutton" value="Click To Hide Instructions" style="display:none" /><\/div><br><br>\n<input type="button" id="cutbutton" value="Cut" /><input type="button" id="copybutton" value="Copy" /><input type="button" id="pastebeforebutton" value="Paste Before" /><input type="button" id="pasteafterbutton" value="Paste After" /><input type="button" id="deletebutton" value="Delete" /><input type="button" id="undocutbutton" value="Undo Cut" /> <input type="button" id="undopastebutton" value="Undo Paste" /><input type="button" id="expandallbutton" value="Expand All" /><input type="button" id="collapseallbutton" value="Collapse All" /> <input type="button" id="closewindowbutton" value="Close" /> '+CM_ScriptTag+'> document.getElementById("cutbutton").onclick=function(){ return window.opener.CaluMath.PM.CutFunction();}; document.getElementById("copybutton").onclick=function(){ return window.opener.CaluMath.PM.CopyFunction();}; document.getElementById("deletebutton").onclick=function(){ return window.opener.CaluMath.PM.DeleteFunction();};  document.getElementById("pastebeforebutton").onclick=function(){ return window.opener.CaluMath.PM.PasteBeforeFunction();};  document.getElementById("pasteafterbutton").onclick=function(){ return window.opener.CaluMath.PM.PasteAfterFunction();};  document.getElementById("undocutbutton").onclick=function(){ window.opener.CaluMath.PM.UndoCutFunction();}; document.getElementById("undopastebutton").onclick=function(){ window.opener.CaluMath.PM.UndoPasteFunction();}; document.getElementById("closewindowbutton").onclick=function(){  window.close();};   <\/script> <br> <\script type="text/javascript">document.getElementById("instructionsbutton").onclick=function(){ document.getElementById("instructionslist").style.display="block"; document.getElementById("hideinstructionsbutton").style.display="inline"; document.getElementById("instructionsbutton").style.display="none";};  document.getElementById("hideinstructionsbutton").onclick=function(){ document.getElementById("instructionslist").style.display="none"; document.getElementById("instructionsbutton").style.display="inline"; document.getElementById("hideinstructionsbutton").style.display="none";};     document.getElementById("expandallbutton").onclick=function(){ var inputlist=document.getElementsByTagName("input"); var inputlistlength= inputlist.length; for(var i=0; i<inputlistlength; i++){   inputlisti= inputlist[i]; if(inputlisti.getAttribute("type")=="button" && inputlisti.id.match(/buttonplus/) != null){inputlisti.onclick();}; }; };    document.getElementById("collapseallbutton").onclick=function(){ var inputlist=document.getElementsByTagName("input"); var inputlistlength= inputlist.length; for(var i=0; i<inputlistlength; i++){   inputlisti= inputlist[i]; if(inputlisti.getAttribute("type")=="button" && inputlisti.id.match(/buttonminus/) != null){inputlisti.onclick();}; };};       <\/script> \n<div id="instructionslist" style="display:none;">\n    <h3>To delete a section of the document:</h3>  \n     <ol>\n         <li>Put your cursor on the first item you want to delete and hold the left mouse button down. Drag the cursor down over the document and lift the button over the final item you want to delete. The beginning and ending items must be "at the same level" of the document, in other words, they must be top level items of the document or they must be children of the same document element. It is not important that you completely highlight the first and last items you want to delete; you only must highlight a part of them.</li>\n         <li>Go to the top (or bottom) of the page and click the Cut Button</li>\n         <li> You will see the items disappear.</li>\n         <li>Click the Delete Button to remove the items permanently from the document</li>\n         <li>The Cut And Paste window automatically reloads so you can cut and paste again.</li>\n     </ol>\n    <h3>To cut and paste a section of the document:</h3>     <ol>\n         <li>Put your cursor on the first item you want to delete and hold the left mouse button down. Drag the cursor down over the document and lift the button over the final item you want to cut. The beginning and ending items must be "at the same level" of the document, in other words, they must be top level items of the document or they must be children of the same document element. It is not important that you completely highlight the first and last items you want to cut; you only must highlight a part of them. </li>\n         <li>Go to the top (or bottom) of the page and click the Cut Button</li>\n         <li> You will see the items disappear.</li>\n         <li>Go down to the item you want to place the cut items before or after and click on it.</li>\n         <li>Click the Paste Before button to insert the cut items before the item you clicked. Click the Paste After button to insert the cut items after the item you clicked.</li>\n         <li>The Cut And Paste window automatically reloads so you can cut and paste again. </li>\n     </ol>\n     <h3>To copy a section of the document:</h3>  \n     <ol>\n         <li>Put your cursor on the first item you want to copy and hold the left mouse button down. Drag the cursor down over the document and lift the button over the final item you want to copy. The beginning and ending items must be "at the same level" of the document, in other words, they must be top level items of the document or they must be children of the same document element. It is not important that you completely highlight the first and last items you want to copy; you only must highlight a part of them. </li>\n         <li>Go to the top (or bottom) of the page and click the Copy Button</li>\n         <li> It is very bad to have two different objects that have the same name that are created in a web page. (The only exception to this is if they are created using a condition if, else-if, then statement. In this case only one of several possible objects with the same name is created). Therefore, if you copy an item, you should adjust the name in some manner. Using the boxes at the top or bottom of the page, you have the several options. Suppose you are copying a function named <i>Mygoodfunction</i>.\n         <ol style="list-style-type:lower-alpha">\n             <li> You can add a prefix to the name of the object you are copying. For example, if you type <i>NEW</i> in the Add Prefix To Names box, then the copied function will have the name <i>NEWMygoodfunction</i>. \n             <li> You can add a suffix to the name of the object you are copying. For example, if you type <i>NEW</i> in the Add Suffix To Names box, then the copied function will have the name <i>MygoodfunctionNEW</i>. \n             <li> You can replace letters in the name of <i>Mygoodfunction</i> in one of two ways: either you can replace all occurrences of a pattern of letters by some other pattern of letters, or you can replace the first occurrence of a pattern of letters by some other pattern of letters. For example, if you type <i>o</i> in the box Replace This In Names and <i>YYY</i> in the With This box and choose <i>first occurrence</i> in the Replace menu, the result is <i>MygYYYodfunction</i>. If you type the same information, but choose <i>all occurrences</i>, then the result is <i>MygYYYYYYdfunctiYYYn</i>. If you type <i>oo</i> instead <i>o</i> in the Replace This In Names box, the result is <i>MygYYYdfunction</i>.</li>\n             <li>You can replace several items at the same time; the items you want to replace should be separated by commas. For example, if you type <i>o, 4</i> in the Replace This In Names box and <i>u, 5</i> in the With This box, then all occurrences of o will be replaced with u\'s and all occurences of 4 will be replaced by 5\'s.</li>\n             <li> You can add  a prefix and a suffix and replace combinations of letters in an objects name all at the same time.</li>\n             <li> If you copy several items, adding prefixes, suffixes, or replacing letters is done to all of the items copied.</li>\n         </ol>\n         <li>Go down to the item you want to place the copied items before or after and click on it.</li>\n         <li>Click the Paste Before button to insert the cut items before the item you clicked. Click the Paste After button to insert the cut items after the item you clicked.</li>\n         <li style="color:red"><b><i>Be sure that you paste objects in a place that makes sense. For example, do not paste the graph of a function to a place that is before where the axes are created!</i></b></li>\n         <li style="color:red">If you enter something in the top boxes, use the buttons at the top of the page. If you enter something in the boxes at the bottom of the page, you can click either the top or bottom buttons.</li>\n        <li style="color:red">The Cut And Paste window automatically reloads so you can cut and paste again. When it does, it will ask you if you want to paste the same item again. If you want to paste the same item, click OK, add any changes you want to make in the name of the object (using the textboxes), click where you want to insert the item, and click Paste Before or Paste After. If you do not want to paste the same item again, click Cancel. In this case, the cut and paste window will reload "fresh," so you can select the items you want, and then cut or copy them. </li>\n     </ol>\n     <h3>What is the Clipboard?</h3><p>The Clipboard is a place where you can store items without them appearing in the document. Anything placed inside the Clipboard (which is the area at the bottom of the cut and paste window) is not part of your document, however items can be moved freely between the Clipboard and the document. If you Cut and item that you may or may not need in the future, you can place it in the Clipboard. This will allow you to move it back into your document in the future if you find that you need it. There is no way to edit objects in the Clipboard unless you move them so they are again part of your document. When you Save a page, the Clipboard gets saved with it. However it is a good idea to not include junk in the Clipboard, since this makes the size of the web page (in kilobytes) larger, meaning that it may take longer to load.</p>\n     <h3>The other buttons at the top and bottom of the Cut And Paste window:</h3>  \n     <ol>\n        <li>If you cut a set of items, clicking the Undo Cut button returns the item to the page so you can begin again.</li>\n        <li>If you do not like the way some items were pasted, you can click the Undo Paste button. This will return your page to the condition it was before you pasted your last items. <i>Note: you can even return to the page you are creating and add more items and make more changes. If you then return to the Cut And Paste window and click the Undo Paste button, it will return your page to the state it was before the last paste. All of your subsequent changes will be lost. (Of course, you can also return to previous versions using the Previous Version button at the top of the <span class="logo1">C</span><span class="logo2">ALU</span><span class="logo3">M</span><span class="logo4">ATH</span> Page Maker page).</i></li>\n        <li>The Close button closes the Cut And Paste window</li>\n     </ol>\n<h3>KeyBoard Shortcuts:</h3>  \n <p> Pressing the following keys has the same effect as clicking the indicated button. <span style="color:red">Warning: Keyboard shortcuts are not guaranteed to work on all computers. Sometimes, after using a keyboard shortcut, the Cut And Paste Window will be minimized when it reloads; click on it so it is visible again.<\/span></p>     <ol>\n        <li>Left Arrow Key: Cut button</li>\n        <li>Right Arrow Key: Copy button</li>\n        <li>Up Arrow Key: Paste Before button</li>\n        <li>Down Arrow Key: Paste After button</li>\n        <li>End Key: Delete button</li>\n        <li>Home Key: Scrolls window to the place where you just cut or copied something. This is useful if you want to copy something, but need to change its name. You can use this key to quickly return to the place in the page where the item was. </li>\n        <li>Page Up Key: Scrolls Page to Top of Window</li>\n        <li>Page Down Key: Scrolls Page to Bottom of Window</li>     \n     </ol>\n<\/div>\n<br><table border="2px" style="background-color:#F0F0F0" ><tr><td style="text-align:center">Add Prefix To Names</td><td style="text-align:center">Replace This In Names</td><td style="text-align:center">With This</td><td style="text-align:center">Replace</td><td style="text-align:center">Add Suffix To Names</td></tr><tr><td><input id="prefixbox" type="text" size="20" /></td><td><input id="replacebox" type="text" size="20" /></td><td><input id="replacewithbox" type="text" size="20" /></td><td><select id="replacetypemenu"><option  value="first" >first occurrence<\/option><option value="all" >all occurrences<\/option><\/select></td><td><input id="suffixbox" type="text" size="20" /></td></tr></table><br><ol >';  
      var tempnewstringbottom='\n<input type="button" id="bottom_cutbutton" value="Cut" /><input type="button" id="bottom_copybutton" value="Copy" /><input type="button" id="bottom_pastebeforebutton" value="Paste Before" /><input type="button" id="bottom_pasteafterbutton" value="Paste After" /><input type="button" id="bottom_deletebutton" value="Delete" /><input type="button" id="bottom_undocutbutton" value="Undo Cut" /> <input type="button" id="bottom_undopastebutton" value="Undo Paste" /><input type="button" id="bottom_expandallbutton" value="Expand All" /><input type="button" id="bottom_collapseallbutton" value="Collapse All" /> <input type="button" id="bottom_closewindowbutton" value="Close" /> '+CM_ScriptTag+'> document.getElementById("bottom_closewindowbutton").onclick=function(){ window.close();}; document.getElementById("bottom_cutbutton").onclick=function(){ return window.opener.CaluMath.PM.CutFunction();}; document.getElementById("bottom_copybutton").onclick=function(){ return window.opener.CaluMath.PM.CopyFunction();}; document.getElementById("bottom_deletebutton").onclick=function(){ return window.opener.CaluMath.PM.DeleteFunction();};  document.getElementById("bottom_pastebeforebutton").onclick=function(){ return window.opener.CaluMath.PM.PasteBeforeFunction();};  document.getElementById("bottom_pasteafterbutton").onclick=function(){ return window.opener.CaluMath.PM.PasteAfterFunction();};  document.getElementById("bottom_undocutbutton").onclick=function(){ window.opener.CaluMath.PM.UndoCutFunction();}; document.getElementById("bottom_undopastebutton").onclick=function(){ window.opener.CaluMath.PM.UndoPasteFunction();}; document.getElementById("bottom_closewindowbutton").onclick=function(){  window.close();};   <\/script> <br><\script type="text/javascript"> document.getElementById("bottom_expandallbutton").onclick=function(){ var inputlist=document.getElementsByTagName("input"); var inputlistlength= inputlist.length; for(var i=0; i<inputlistlength; i++){   inputlisti= inputlist[i]; if(inputlisti.getAttribute("type")=="button" && inputlisti.id.match(/buttonplus/) != null){inputlisti.onclick();}; }; };    document.getElementById("bottom_collapseallbutton").onclick=function(){ var inputlist=document.getElementsByTagName("input"); var inputlistlength= inputlist.length; for(var i=0; i<inputlistlength; i++){   inputlisti= inputlist[i]; if(inputlisti.getAttribute("type")=="button" && inputlisti.id.match(/buttonminus/) != null){inputlisti.onclick();}; };};       <\/script> <br><table border="2px" style="background-color:#F0F0F0" ><tr><td style="text-align:center">Add Prefix To Names</td><td style="text-align:center">Replace This In Names</td><td style="text-align:center">With This</td><td style="text-align:center">Replace</td><td style="text-align:center">Add Suffix To Names</td></tr><tr><td><input id="bottom_prefixbox" type="text" size="20" /></td><td><input id="bottom_replacebox" type="text" size="20" /></td><td><input id="bottom_replacewithbox" type="text" size="20" /></td><td><select id="bottom_replacetypemenu"><option  value="first" >first occurrence<\/option><option value="all" >all occurrences<\/option><\/select></td><td><input id="bottom_suffixbox" type="text" size="20" /></td></tr></table><br><script type="text/javascript"> document.getElementById("bottom_prefixbox").onchange= function(){ document.getElementById("prefixbox").value=document.getElementById("bottom_prefixbox").value; }; document.getElementById("bottom_replacebox").onchange= function(){ document.getElementById("replacebox").value=document.getElementById("bottom_replacebox").value; }; document.getElementById("bottom_replacewithbox").onchange= function(){ document.getElementById("replacewithbox").value=document.getElementById("bottom_replacewithbox").value; };document.getElementById("bottom_suffixbox").onchange= function(){ document.getElementById("suffixbox").value=document.getElementById("bottom_suffixbox").value; };  document.getElementById("bottom_replacetypemenu").onchange= function(){ document.getElementById("replacetypemenu").selectedIndex=document.getElementById("bottom_replacetypemenu").selectedIndex; };<\/script>'; 
      
         window["CM_TempWindow"].document.write(tempnewstring+' '+CaluMath.PM.WriteNewWindowStringEditMode(CaluMath.PM.PageMakerObjectArray.concat(CaluMath.PM.ClipBoard))+'<\/ol><br> '+tempnewstringbottom+'<br>\n'+CM_ScriptTag+'> window.cm_onload=function(){ window.opener.CaluMath.PM.MakeClickableCutAndPasteI(window);}; window.cm_onunload=function(){ var temparray=document.getElementsByTagName("input"); var temparraylength= temparray.length; for(var i=0; i< temparraylength; i++){ temparray[i].onclick=null;}; var temparray=document.getElementsByTagName("div"); var temparraylength= temparray.length; for(var i=0; i< temparraylength; i++){ temparray[i].onmousedown=null; temparray[i].onmouseup=null; temparray[i].onclick=null;};}; <\/script> \n'+CaluMath.PM.NewWindowStringClosing);
         window["CM_TempWindow"].document.close();
         CM_TempWindow.document.onkeydown=CaluMath.Html.CutAndPasteKeyDownShorts;
   }
   else{
      setTimeout('CaluMath.PM.TempWindowWriteRemainingRearrangeMode();', 10);
   };
};

CaluMath.PM.MakeClickableCutAndPasteI=function(){ 
   //Comment: The window is arguments[0]. If the page is being imported, we have "importpage" as arguments[1]. This tells the CaluMath.PM.CutAndPasteTransferArray have the property import="yes". This tells the PasteBefore and PasteAfter functions to do the CaluMath.PM.AddToLists for each element (since they are coming from a new page, the need to be added to the lists of the current page). 

   //Comment: If they have just copied and pasted something, when the CutAndPasteWindow opens again (automatically, after reflecting the change), the CaluMath.PM.CutAndPasteTransferArray will be the old thing they cut. A confirm pop up asks if they want to copy the same thing. In this case, the conditional below is done. It stops CaluMath.PM.MakeClickableCutAndPasteI from continuing (since it deals with cutting or copying a new section), does what the CopyFunction would do (except with the old copied items), and goes directly to where they can click to paste before or after. 
   if(CM_TopWindow.CaluMath.PM.CutAndPasteTransferArray !=null && CM_TopWindow.CaluMath.PM.CutAndPasteTransferArray.length>0){
      if(CM_TopWindow.CM_TempWindow.confirm("Keep the last Copied Element")){
         var CM_EveryObjectArraylength= CaluMath.PM.EveryObjectArray.length;
         CaluMath.PM.CutAndPasteTransferArray=CaluMath.PM.ParsePageMakerObjectString(CaluMath.PM.CutAndPasteTransferArrayString);
         CaluMath.PM.CutAndPasteTransferArray.copy="yes";
         CaluMath.PM.CutAndPasteTransferArray.EveryObjectArrayIndex= CM_EveryObjectArraylength;
         CaluMath.PM.CutAndPasteTransferArray.parent=new Array();
         CaluMath.PM.CutAndPasteTransferArray.parent.childarray=CaluMath.PM.CutAndPasteTransferArray;
         CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction(CaluMath.PM.CutAndPasteTransferArray); 
         CaluMath.PM.AssignCasWindowToTextChildren(CaluMath.PM.CutAndPasteTransferArray);
         CaluMath.PM.MakeClickableCutAndPasteII(CM_TempWindow);
         return ;
      };
   };   

   //Comment: In this case, they are not automatically pasting something that is previously copied, so they get to select whatever they want to be copied or cut.  
   var tempwindow=arguments[0];
   if(tempwindow != null && tempwindow.document != null && tempwindow.document.body != null){
      tempwindow.document.body.onclick=function(){ };
   };
   CaluMath.PM.CutAndPasteArray=new Array();
   CaluMath.PM.CutAndPasteTransferArray= new Array();
   var specialtype=arguments[1];
   if(specialtype != null){
      CaluMath.PM.CutAndPasteTransferArray[specialtype]="yes";
   };
   CaluMath.PM.AssignParents();
   CaluMath.PM.CancelDown=false;
   CaluMath.PM.CancelUp=false;

   for(var name in CM_TopWindow.CaluMath.PM.PageMakerObjectParent){ 
      if(CM_TopWindow.CaluMath.PM.PageMakerObjectParent.hasOwnProperty(name)){
         var tempnode= tempwindow.document.getElementById(name); 
         if(tempnode == null){   
            if(CM_TopWindow.CM_TempWindow[name] != null && CM_TopWindow.CM_TempWindow[name].cm_node != null){
               tempnode=CM_TopWindow.CM_TempWindow[name].cm_node;  
            };
         };
      };

      if(tempnode != null ){
         tempnode.onclick=null;
         if(tempnode.getAttribute("id") !=null && tempnode.getAttribute("id").length >0){
            if(CM_TopWindow.CaluMath.PM.PageMakerObjectParent[tempnode.getAttribute("id")].type !="clipboard" && CM_TopWindow.CaluMath.PM.PageMakerObjectParent[tempnode.getAttribute("id")].type !="clipboardplaceholder"){  
               tempnode.onmousedown=function(){ 
                  if(!CaluMath.PM.CancelDown || CaluMath.PM.CancelUp ){
                     CaluMath.PM.CancelDown=true;
                     CaluMath.PM.CancelUp=false;
                     CaluMath.PM.CutAndPasteArray.length=0;
                     CaluMath.PM.CutAndPasteTransferArray.length=0;
                     CM_TopWindow.CaluMath.PM.CutAndPasteArray[0]= CM_TopWindow.CaluMath.PM.PageMakerObjectParent[this.getAttribute("id")];
                  };
               };
               tempnode.onmouseup=function(){
                  if( !CaluMath.PM.CancelUp){ //CM_TempWindow.alerttest(this);
                     CaluMath.PM.CancelUp=true;
                     CM_TopWindow.CaluMath.PM.CutAndPasteArray[1]= CM_TopWindow.CaluMath.PM.PageMakerObjectParent[this.getAttribute("id")];
                  };            
                  if(CM_TopWindow.CaluMath.PM.CutAndPasteArray[0]==null){
                     CM_TempWindow.alert("I didn't recognize where you began highlighting the section you wanted to move. Are you sure you did not accidentally start on a number? Please try again.");
                     CaluMath.PM.MakeClickableCutAndPasteI(CM_TempWindow);
                  }
                  else if(CM_TopWindow.CaluMath.PM.CutAndPasteArray[1]==null){
                     CM_TempWindow.alert("I know where you started to highlight the section you wanted to cut or copy, however I didn't recognize where you ended highlighting.  Are you sure you did not accidentally end on a number?  Please try again.");
                     CaluMath.PM.MakeClickableCutAndPasteI(CM_TempWindow);
                  }
                  else if(CM_TopWindow.CaluMath.PM.CutAndPasteArray[1].parent != CM_TopWindow.CaluMath.PM.CutAndPasteArray[0].parent){
                     CM_TempWindow.alert("You must begin and end at the same level. Please try again.");
                     CaluMath.PM.MakeClickableCutAndPasteI(CM_TempWindow);
                  }
                  else{
                     var index0= CaluMath.PM.AssignChildIndex(CM_TopWindow.CaluMath.PM.CutAndPasteArray[0]);
                     var index1= CaluMath.PM.AssignChildIndex(CM_TopWindow.CaluMath.PM.CutAndPasteArray[1]);
                     CaluMath.PM.CutAndPasteTransferArray.index0=index0;
                     CaluMath.PM.CutAndPasteTransferArray.index1=index1;
                     CaluMath.PM.CutAndPasteTransferArray.parent = CM_TopWindow.CaluMath.PM.CutAndPasteArray[0].parent;
                     if(index0<=index1){
                        CaluMath.PM.CutAndPasteTransferArray.length=0;
                        for(var i=index0; i<index1+1; i++){
                           CaluMath.PM.CutAndPasteTransferArray.push(CM_TopWindow.CaluMath.PM.CutAndPasteArray[0].parent.childarray[i]);
                        };
                     }
                     else{
                        CaluMath.PM.CutAndPasteTransferArray.length=0;
                        for(var i=index1; i<index0+1; i++){
                           CaluMath.PM.CutAndPasteTransferArray.push(CM_TopWindow.CaluMath.PM.CaluMath.PM.CutAndPasteArray[0].parent.childarray[i]);
                        };
                     };
                  };
               };
            }
            else{
               tempnode.onclick=null;
               tempnode.onmousedown=function(){tempwindow.alert("You cannot cut or copy the ClipBoard Container or its first element. You can cut or copy other elements in the ClipBoard Container however.")}; 
               tempnode.onmouseup =tempnode.onmousedown;
            };
         };
      };
   };
};

CaluMath.PM.CutFunction=function(){

   //Comment: This stops them from clicking buttons before they highlight sections of the document. 
   if(CM_TopWindow.CaluMath.PM.CutAndPasteTransferArray == null || CM_TopWindow.CaluMath.PM.CutAndPasteTransferArray[0] == null){
      CM_TopWindow.CM_TempWindow.alert("You must highlight a section of the document before you can cut it!");
      return ;
   };  

   //Comment: CaluMath.PM.CutAndPasteTransferArrayPreviousSibling and CaluMath.PM.CutAndPasteTransferArrayNextSibling are used for the keyboard shortcut Home Key (for return to where the cut or copy was done)
   CaluMath.PM.CutAndPasteTransferArrayPreviousSibling=CM_TopWindow.CM_TempWindow.document.getElementById(CaluMath.PM.CutAndPasteTransferArray[0].htmlname).parentNode.previousSibling;
   while(CaluMath.PM.CutAndPasteTransferArrayPreviousSibling != null &&  !(CaluMath.PM.CutAndPasteTransferArrayPreviousSibling.tagName  != null && (CaluMath.PM.CutAndPasteTransferArrayPreviousSibling.tagName =="li" || CaluMath.PM.CutAndPasteTransferArrayPreviousSibling.tagName =="LI"))){
      CaluMath.PM.CutAndPasteTransferArrayPreviousSibling=CaluMath.PM.CutAndPasteTransferArrayPreviousSibling.previousSibling;
   };
   CaluMath.PM.CutAndPasteTransferArrayNextSibling=CM_TopWindow.CM_TempWindow.document.getElementById(CaluMath.PM.CutAndPasteTransferArray[CaluMath.PM.CutAndPasteTransferArray.length-1].htmlname).parentNode.nextSibling;
   while( CaluMath.PM.CutAndPasteTransferArrayNextSibling != null && !(CaluMath.PM.CutAndPasteTransferArrayNextSibling.tagName  != null && (CaluMath.PM.CutAndPasteTransferArrayNextSibling.tagName =="li" || CaluMath.PM.CutAndPasteTransferArrayNextSibling.tagName =="LI"))){
      CaluMath.PM.CutAndPasteTransferArrayNextSibling=CaluMath.PM.CutAndPasteTransferArrayNextSibling.nextSibling;
   };


   for(var i=0; i<CaluMath.PM.CutAndPasteTransferArray.length; i++){
      CM_TopWindow.CM_TempWindow.document.getElementById(CaluMath.PM.CutAndPasteTransferArray[i].htmlname).parentNode.style.visibility="hidden"; 
   }; 
   CaluMath.PM.CutAndPasteTransferArray.cut="yes";
   CaluMath.PM.MakeClickableCutAndPasteII(CM_TempWindow);

};


CaluMath.PM.CopyFunction=function(){

   //Comment: This stops them from clicking buttons before they highlight sections of the document. 
   if(CM_TopWindow.CaluMath.PM.CutAndPasteTransferArray == null || CM_TopWindow.CaluMath.PM.CutAndPasteTransferArray[0] == null){
      CM_TopWindow.CM_TempWindow.alert("You must highlight a section of the document before you can copy it!");
      return ;
   };  

   //Comment: CaluMath.PM.CutAndPasteTransferArrayPreviousSibling and CaluMath.PM.CutAndPasteTransferArrayNextSibling are used for the keyboard shortcut Home Key (for return to where the cut or copy was done)

   CaluMath.PM.CutAndPasteTransferArrayPreviousSibling=CM_TopWindow.CM_TempWindow.document.getElementById(CaluMath.PM.CutAndPasteTransferArray[0].htmlname).parentNode.previousSibling;
   while(CaluMath.PM.CutAndPasteTransferArrayPreviousSibling != null &&  !(CaluMath.PM.CutAndPasteTransferArrayPreviousSibling.tagName  != null && (CaluMath.PM.CutAndPasteTransferArrayPreviousSibling.tagName =="li" || CaluMath.PM.CutAndPasteTransferArrayPreviousSibling.tagName =="LI"))){
      CaluMath.PM.CutAndPasteTransferArrayPreviousSibling=CaluMath.PM.CutAndPasteTransferArrayPreviousSibling.previousSibling;
   };
   CaluMath.PM.CutAndPasteTransferArrayNextSibling=CM_TopWindow.CM_TempWindow.document.getElementById(CaluMath.PM.CutAndPasteTransferArray[CaluMath.PM.CutAndPasteTransferArray.length-1].htmlname).parentNode.nextSibling;
   while( CaluMath.PM.CutAndPasteTransferArrayNextSibling != null && !(CaluMath.PM.CutAndPasteTransferArrayNextSibling.tagName  != null && (CaluMath.PM.CutAndPasteTransferArrayNextSibling.tagName =="li" || CaluMath.PM.CutAndPasteTransferArrayNextSibling.tagName =="LI"))){
      CaluMath.PM.CutAndPasteTransferArrayNextSibling=CaluMath.PM.CutAndPasteTransferArrayNextSibling.nextSibling;
   };

   var CM_EveryObjectArraylength= CaluMath.PM.EveryObjectArray.length;
   CaluMath.PM.CutAndPasteTransferArrayString=CaluMath.PM.PageMakerObjectToString(CaluMath.PM.CutAndPasteTransferArray);
   CaluMath.PM.CutAndPasteTransferArray=CaluMath.PM.ParsePageMakerObjectString(CaluMath.PM.PageMakerObjectToString(CaluMath.PM.CutAndPasteTransferArray));
   CaluMath.PM.CutAndPasteTransferArray.copy="yes";
   CaluMath.PM.CutAndPasteTransferArray.EveryObjectArrayIndex= CM_EveryObjectArraylength;
   CaluMath.PM.CutAndPasteTransferArray.parent=new Array();
   CaluMath.PM.CutAndPasteTransferArray.parent.childarray=CaluMath.PM.CutAndPasteTransferArray;
   CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction(CaluMath.PM.CutAndPasteTransferArray); 
   CaluMath.PM.AssignCasWindowToTextChildren(CaluMath.PM.CutAndPasteTransferArray);
   CaluMath.PM.MakeClickableCutAndPasteII(CM_TempWindow);
};

CaluMath.PM.RemoveEveryObjectArrayElements=function(){
   var elementforremoval= arguments[0];
   CaluMath.PM.EveryObjectArray=CaluMath.PM.EveryObjectArray.cm_removeelements(elementforremoval);
   if(elementforremoval.childarray != null && elementforremoval.childarray.length >0){
      var elementchildarray=elementforremoval.childarray;
      var elementchildarraylength=elementchildarray.length;
      for(var i=0; i< elementchildarraylength; i++){
         CaluMath.PM.RemoveEveryObjectArrayElements(elementchildarray[i]);
      };
   };
};


CaluMath.PM.DeleteFunction=function(){

   //Comment: This stops them from clicking buttons before they highlight sections of the document. 
   if(CM_TopWindow.CaluMath.PM.CutAndPasteTransferArray == null || CM_TopWindow.CaluMath.PM.CutAndPasteTransferArray[0] == null){
      CM_TopWindow.CM_TempWindow.alert("You must highlight a section of the document and cut it before you can delete it!");
      return ;
   };  

   var CutAndPasteTransferArraylength=CaluMath.PM.CutAndPasteTransferArray.length;
   if(!CaluMath.PM.IsContainedInClipBoard(CaluMath.PM.CutAndPasteTransferArray[0])){
      for(var i=0; i< CutAndPasteTransferArraylength; i++){
         var childindex= CaluMath.PM.ArrayIndex(CaluMath.PM.CutAndPasteTransferArray[i].parent.childarray, CaluMath.PM.CutAndPasteTransferArray[i]); 
         CaluMath.PM.CutAndPasteTransferArray[i].parent.childarray.splice(childindex,1); 
         CaluMath.PM.DeleteFromLists(CaluMath.PM.CutAndPasteTransferArray[i]);
         CaluMath.PM.RemoveEveryObjectArrayElements(CaluMath.PM.CutAndPasteTransferArray[i]);
      };
   }
   else{
      for(var i=0; i< CutAndPasteTransferArraylength; i++){
         var childindex= CaluMath.PM.ArrayIndex(CaluMath.PM.CutAndPasteTransferArray[i].parent.childarray, CaluMath.PM.CutAndPasteTransferArray[i]); 
         CaluMath.PM.CutAndPasteTransferArray[i].parent.childarray.splice(childindex,1); 
      };
   };
   setTimeout('document.getElementById("cutandpastepage").onclick()', 100);

};


CaluMath.PM.UndoCutFunction=function(){
   for(var i=0; i<CaluMath.PM.CutAndPasteTransferArray.length; i++){
      CM_TopWindow.CM_TempWindow.document.getElementById(CaluMath.PM.CutAndPasteTransferArray[i].htmlname).parentNode.style.visibility="visible"; 
   }; 
   CaluMath.PM.CutAndPasteTransferArray= new Array();
   CaluMath.PM.MakeClickableCutAndPasteI(CM_TempWindow);

   //Comment: We need to reset the cursor back to normal; since something was cut, they are automatically set to be pointer (in order for them to 
   for(var name in CM_TopWindow.CaluMath.PM.PageMakerObjectParent){ 
      if(CM_TopWindow.CaluMath.PM.PageMakerObjectParent.hasOwnProperty(name)){
         var tempnode= CM_TempWindow.document.getElementById(name); 
         if(tempnode == null){   
            if(CM_TopWindow.CM_TempWindow[name] != null && CM_TopWindow.CM_TempWindow[name].cm_node != null){
               tempnode=CM_TopWindow.CM_TempWindow[name].cm_node;  
            };
         };
      };
      if(tempnode != null){
         tempnode.onmouseover=function(){
            this.style.cursor="";
         };
      };
   };
};

CaluMath.PM.MakeClickableCutAndPasteII=function(){ //alerttest("Doing CaluMath.PM.MakeClickableCutAndPaste");
   var tempwindow=arguments[0];
   CaluMath.PM.CancelClick=false;
   CaluMath.PM.PasteArray=new Array();
   tempwindow.document.body.onclick=null;
      
   for(var name in CM_TopWindow.CaluMath.PM.PageMakerObjectParent){ 
      if(CM_TopWindow.CaluMath.PM.PageMakerObjectParent.hasOwnProperty(name)){
         var tempnode= tempwindow.document.getElementById(name); 
         if(tempnode == null){   
            if(CM_TopWindow.CM_TempWindow[name] != null && CM_TopWindow.CM_TempWindow[name].cm_node != null){
               tempnode=CM_TopWindow.CM_TempWindow[name].cm_node;  
            };
         };
      };

      if(tempnode != null){
         if(CM_TopWindow.CaluMath.PM.PageMakerObjectParent[tempnode.getAttribute("id")].type !="clipboard"){  
            tempnode.onmousedown= null;
            tempnode.onmouseup= null;
            tempnode.onmouseover=function(){
               this.style.cursor="pointer";
            };
            tempnode.onmouseout=function(){
               this.style.cursor="";
            };
   
            if(tempnode.getAttribute("id") !=null && tempnode.getAttribute("id").length >0){
               tempnode.onclick=function(){ 
                  if(!CaluMath.PM.CancelClick){
                     CaluMath.PM.CancelClick=true;
                     CaluMath.PM.PasteArray.push(CM_TopWindow.CaluMath.PM.PageMakerObjectParent[this.getAttribute("id")]);
                  };
               };
            };
         }
         else{
            tempnode.onmousedown= null;
            tempnode.onmouseup= null;
            tempnode.onclick=function(){tempwindow.alert("You cannot paste before or after the ClipBoard Container. You can paste inside the container however.")}; 
         };
      };
   };
};


CaluMath.PM.PasteBeforeFunction=function(){

   var scripttags = CM_TempWindow.document.getElementsByTagName("script");
   var scripttagslength=scripttags.length; 
   CaluMath.PM.PasteUndoString= CaluMath.PM.PageMakerObjectArrayToString(); 

   if(CaluMath.PM.CutAndPasteTransferArray != null && CaluMath.PM.CutAndPasteTransferArray.parent != null && CM_TopWindow.CaluMath.PM.PasteArray != null && CM_TopWindow.CaluMath.PM.PasteArray[0] != null){
      CaluMath.PM.CutAndPasteTransferArray.parent.childarray.splice(CaluMath.PM.CutAndPasteTransferArray.index0,Math.abs(1+CaluMath.PM.CutAndPasteTransferArray.index1-CaluMath.PM.CutAndPasteTransferArray.index0));
      var referencenode= CaluMath.PM.PasteArray[0];
      var referencenodeparent= referencenode.parent;
      var tempindex= CaluMath.PM.AssignChildIndex(referencenode);
      //Comment: We force them to paste after the first child of the CaluMath.PM.Clipboard.
      if(referencenode != CaluMath.PM.ClipBoard.childarray[0]){
         CM_TopWindow.Array.prototype.splice.apply(referencenodeparent.childarray, [tempindex,0].concat(CaluMath.PM.CutAndPasteTransferArray));
      }
      else{
         CM_TopWindow.Array.prototype.splice.apply(referencenodeparent.childarray, [1+1*tempindex,0].concat(CaluMath.PM.CutAndPasteTransferArray));
      };
      if(CaluMath.PM.CutAndPasteTransferArray.importpage=="yes" || CaluMath.PM.CutAndPasteTransferArray.copy=="yes" || CaluMath.PM.IsContainedInClipBoard(CaluMath.PM.CutAndPasteTransferArray[0])==true){
         var CutAndPasteTransferArraylength=CaluMath.PM.CutAndPasteTransferArray.length;
         for(var i=0; i< CaluMath.PM.CutAndPasteTransferArray.length; i++){
            CaluMath.PM.AddToLists(CaluMath.PM.CutAndPasteTransferArray[i]);
         };
         if(CM_TempWindow.document.getElementById("prefixbox").value.length >0 || CM_TempWindow.document.getElementById("replacebox").value.length>0 || CM_TempWindow.document.getElementById("replacewithbox").value.length>0 ||  CM_TempWindow.document.getElementById("suffixbox").value.length>0){
            CaluMath.PM.ChangeImportedNames(CaluMath.PM.CutAndPasteTransferArray, CM_TempWindow.document.getElementById("prefixbox").value, CM_TempWindow.document.getElementById("replacebox").value, CM_TempWindow.document.getElementById("replacewithbox").value, CM_TempWindow.document.getElementById("suffixbox").value, CM_TempWindow.document.getElementById("replacetypemenu")[CM_TempWindow.document.getElementById("replacetypemenu").selectedIndex].value);
         };
      };
      if(CaluMath.PM.CutAndPasteTransferArray.cut=="yes"  && (CM_TempWindow.document.getElementById("prefixbox").value.length >0 || CM_TempWindow.document.getElementById("replacebox").value.length>0 || CM_TempWindow.document.getElementById("replacewithbox").value.length>0 ||  CM_TempWindow.document.getElementById("suffixbox").value.length>0)){
         CaluMath.PM.ChangeImportedNames(CaluMath.PM.CutAndPasteTransferArray, CM_TempWindow.document.getElementById("prefixbox").value, CM_TempWindow.document.getElementById("replacebox").value, CM_TempWindow.document.getElementById("replacewithbox").value, CM_TempWindow.document.getElementById("suffixbox").value, CM_TempWindow.document.getElementById("replacetypemenu")[CM_TempWindow.document.getElementById("replacetypemenu").selectedIndex].value);
      };
      if(CaluMath.PM.IsContainedInClipBoard(referencenode)){
         for(var jj=0; jj<CaluMath.PM.CutAndPasteTransferArray.length; jj++){
            CaluMath.PM.DeleteFromLists(CaluMath.PM.CutAndPasteTransferArray[jj]);
         };
      };
      CaluMath.PM.AssignParents();
      setTimeout('CM_TopWindow.CaluMath.PM.CutAndPastePageOnClickMimic()', 100);
   };
};

CaluMath.PM.PasteAfterFunction=function(){

   var scripttags = CM_TempWindow.document.getElementsByTagName("script");
   var scripttagslength=scripttags.length; 
   CaluMath.PM.PasteUndoString= CaluMath.PM.PageMakerObjectArrayToString();  

   if(CaluMath.PM.CutAndPasteTransferArray != null && CaluMath.PM.CutAndPasteTransferArray.parent != null && CM_TopWindow.CaluMath.PM.PasteArray != null && CM_TopWindow.CaluMath.PM.PasteArray[0] != null){
      CaluMath.PM.CutAndPasteTransferArray.parent.childarray.splice(CaluMath.PM.CutAndPasteTransferArray.index0,Math.abs(1+CaluMath.PM.CutAndPasteTransferArray.index1-CaluMath.PM.CutAndPasteTransferArray.index0));
      var referencenode= CaluMath.PM.PasteArray[0];
      var referencenodeparent= referencenode.parent;
      var tempindex= CaluMath.PM.AssignChildIndex(referencenode)+1;
      CM_TopWindow.Array.prototype.splice.apply(referencenodeparent.childarray, [tempindex,0].concat(CaluMath.PM.CutAndPasteTransferArray));
      if(CaluMath.PM.CutAndPasteTransferArray.importpage=="yes" || CaluMath.PM.CutAndPasteTransferArray.copy=="yes" || CaluMath.PM.IsContainedInClipBoard(CaluMath.PM.CutAndPasteTransferArray[0])==true){
         var CutAndPasteTransferArraylength=CaluMath.PM.CutAndPasteTransferArray.length;
         for(var i=0; i< CutAndPasteTransferArraylength; i++){
            CaluMath.PM.AddToLists(CaluMath.PM.CutAndPasteTransferArray[i]);
         };
         if(CM_TempWindow.document.getElementById("prefixbox").value.length >0 || CM_TempWindow.document.getElementById("replacebox").value.length>0 || CM_TempWindow.document.getElementById("replacewithbox").value.length>0 ||  CM_TempWindow.document.getElementById("suffixbox").value.length>0){
            CaluMath.PM.ChangeImportedNames(CaluMath.PM.CutAndPasteTransferArray, CM_TempWindow.document.getElementById("prefixbox").value, CM_TempWindow.document.getElementById("replacebox").value, CM_TempWindow.document.getElementById("replacewithbox").value, CM_TempWindow.document.getElementById("suffixbox").value, CM_TempWindow.document.getElementById("replacetypemenu")[CM_TempWindow.document.getElementById("replacetypemenu").selectedIndex].value);
         };
      };
      if(CaluMath.PM.CutAndPasteTransferArray.cut=="yes"  && (CM_TempWindow.document.getElementById("prefixbox").value.length >0 || CM_TempWindow.document.getElementById("replacebox").value.length>0 || CM_TempWindow.document.getElementById("replacewithbox").value.length>0 ||  CM_TempWindow.document.getElementById("suffixbox").value.length>0)){
         CaluMath.PM.ChangeImportedNames(CaluMath.PM.CutAndPasteTransferArray, CM_TempWindow.document.getElementById("prefixbox").value, CM_TempWindow.document.getElementById("replacebox").value, CM_TempWindow.document.getElementById("replacewithbox").value, CM_TempWindow.document.getElementById("suffixbox").value, CM_TempWindow.document.getElementById("replacetypemenu")[CM_TempWindow.document.getElementById("replacetypemenu").selectedIndex].value);
      };
      if(CaluMath.PM.IsContainedInClipBoard(referencenode)){
         for(var jj=0; jj<CaluMath.PM.CutAndPasteTransferArray.length; jj++){
            CaluMath.PM.DeleteFromLists(CaluMath.PM.CutAndPasteTransferArray[jj]);
         };
      };

      CaluMath.PM.AssignParents();
      setTimeout('CM_TopWindow.CaluMath.PM.CutAndPastePageOnClickMimic()', 100);
   };
};

CaluMath.Html.CutAndPasteKeyDownShorts = function(){
   if(CM_TempWindow.event != null){ 
      var eventobject=CM_TempWindow.event;
   }
   else{
      var eventobject=arguments[0];
   };

   if(eventobject.keyCode==37){
      // This is leftarrow
      CaluMath.PM.CutFunction();
   }
   else if(eventobject.keyCode==39){
      // This is rightarrow
      CaluMath.PM.CopyFunction();
   }
   else if(eventobject.keyCode==40){
      // This is an a downarrow key
      CaluMath.PM.PasteAfterFunction();
   }
   else if(eventobject.keyCode==38){
      // This is uparrow
      CaluMath.PM.PasteBeforeFunction();
   }
   else if(eventobject.keyCode==35){
      // This is a End Key 
      CaluMath.PM.DeleteFunction();
   }
   else if(eventobject.keyCode==36){


      // This is an Home Key
      if(window.CaluMath.PM.CutAndPasteTransferArrayPreviousSibling != null && window.CaluMath.PM.CutAndPasteTransferArrayPreviousSibling.scrollIntoView != null){ 
         window.CaluMath.PM.CutAndPasteTransferArrayPreviousSibling.scrollIntoView();          
      }
      else if(window.CaluMath.PM.CutAndPasteTransferArrayNextSibling != null && window.CaluMath.PM.CutAndPasteTransferArrayNextSibling.scrollIntoView != null){ 
         window.CaluMath.PM.CutAndPasteTransferArrayNextSibling.scrollIntoView();          
      };
   }
   else if(eventobject.keyCode==33){
      // This is the Page Up key
      if(window.CM_TempWindow != null){
         CM_TempWindow.scrollBy(0,-100000);
      };
   }
   else if(eventobject.keyCode==34){
      // This is the Page Down key
      if(window.CM_TempWindow != null){
         CM_TempWindow.scrollBy(0,100000);
      };
   };
};

/*
//Comment: This is used in cut and paste, when objects are copied and their names are changed, an also when objects are imported from another page (and their names are changed). 
CaluMath.PM.ChangeImportedNames= function(){ 
   //Comment: arguments[0] is the Array that is to be imported. arguments1-4 are prefix, replace, replace with, suffix.
   var temparray=arguments[0];
   var prefix=arguments[1];
   var replacethis=arguments[2];
   var replacewith=arguments[3];
   var suffix=arguments[4];
   var replacetype=arguments[5];
   if(replacetype=="all"){
      var tempregexp= new RegExp(replacethis,"g");
   }
   else{
      var tempregexp= new RegExp(""+replacethis);
   };
   var temparraylength = temparray.length;
   var reducedEveryObjectArray=CaluMath.PM.EveryObjectArray.slice(CaluMath.PM.CutAndPasteTransferArray.EveryObjectArrayIndex);
   for(var i=0; i< temparraylength; i++){
      if(temparray[i].type != "text"){
         CaluMath.PM.AssignOldValues(temparray[i]);
         var tempname= temparray[i].cm_getsetting("name");
         if(tempname.length >0 && temparray[i].type != "comparison" && temparray[i].cm_getsetting("name")!= "none"){
            var tempdeletefunction= eval("CaluMath.PM.DeleteFromLists"+temparray[i].type);
            tempdeletefunction(temparray[i]);
               temparray[i].cm_setrequiredoroption("name", prefix+tempname.replace(tempregexp,replacewith)+suffix);
            var tempaddtofunction= eval("CaluMath.PM.AddToLists"+temparray[i].type);
            tempaddtofunction(temparray[i]);
         };
         CaluMath.PM.NewChangeName(temparray[i], reducedEveryObjectArray );
         CaluMath.PM.ChangeImportedNames(temparray[i].childarray, prefix, replacethis, replacewith, suffix);
      };
   };
};
*/
  
//Comment I revised this to include multiple name changes at once. 
//Comment: This is used in cut and paste, when objects are copied and their names are changed, an also when objects are imported from another page (and their names are changed). 
CaluMath.PM.ChangeImportedNames= function(){ 
   //Comment: arguments[0] is the Array that is to be imported. arguments1-4 are prefix, replace, replace with, suffix.
   var temparray=arguments[0];
   var prefix=arguments[1];
   var replacethis=arguments[2].replace(/\s*/g, "").split(",");
   var replacewith=arguments[3].replace(/\s*/g, "").split(",");
   var suffix=arguments[4];
   var replacetype=arguments[5];
   for(var jj=0; jj< replacethis.length; jj++){
      if(replacetype=="all"){
         var tempregexp= new RegExp(replacethis[jj],"g");
      }
      else{
         var tempregexp= new RegExp(""+replacethis[jj]);
      };
      var temparraylength = temparray.length;
      var reducedEveryObjectArray=CaluMath.PM.EveryObjectArray.slice(CaluMath.PM.CutAndPasteTransferArray.EveryObjectArrayIndex);
      for(var i=0; i< temparraylength; i++){
         if(temparray[i].type != "text"){
            CaluMath.PM.AssignOldValues(temparray[i]);
            var tempname= temparray[i].cm_getsetting("name");
            if(tempname.length >0 && temparray[i].type != "comparison" && temparray[i].cm_getsetting("name")!= "none"){
               var tempdeletefunction= eval("CaluMath.PM.DeleteFromLists"+temparray[i].type);
               tempdeletefunction(temparray[i]);
                  temparray[i].cm_setrequiredoroption("name", prefix+tempname.replace(tempregexp,replacewith[jj])+suffix);
               var tempaddtofunction= eval("CaluMath.PM.AddToLists"+temparray[i].type);
               tempaddtofunction(temparray[i]);
            };
            CaluMath.PM.NewChangeName(temparray[i], reducedEveryObjectArray );
            CaluMath.PM.ChangeImportedNames(temparray[i].childarray, prefix, replacethis[jj], replacewith[jj], suffix);
         };
      };
   };
};
       

CaluMath.PM.UndoPasteFunction=function(){
   if(window.CaluMath.PM.PasteUndoString != null){
      CaluMath.PM.ParsePageMakerPageStringNoWindow(CaluMath.PM.PasteUndoString);
      CaluMath.PM.OpenTempWindowInEditMode("rearrange");
      setTimeout('CaluMath.PM.MakeClickableCutAndPasteI(CM_TempWindow);', 1000);
   };
};


/////////////////////WRITING THE PAGE INFORMATION STRING AT THE END OF A DOCUMENT//////////////

//Comment: When the user creates a CaluMath page, a string, called CaluMath.PM.PageMakerObjectArrayString, appears at the end of the document. It contains a string version of all the information contained in the page. If the user loads a saved page, the loading routine captures the value of this string and parses it according to the function CaluMath.PM.ParsePageMakerPage. This process creates the CaluMath.PM.PageMakerObjectArray which is an array that contains all of the objects created and their relationship (which are children of each other for example).  This allows the CaluMath Page Maker to obtain the entire structure of the loaded page.//
/*
CaluMath.PM.SplitEveryNCharacters=function(){
   var tempstring=arguments[0];
   var n=arguments[1];
   if(n ==null){
      n=200;
   };
   if(tempstring.length < 200){
      return tempstring;
   }
   else{
   var tempreturn = "";
      while(tempstring.length > n){
         tempreturn=tempreturn +"'+\n'"+tempstring.slice(0,n);
         tempstring=tempstring.slice(n);
      };
      tempreturn=tempreturn+"'+\n'"+tempstring;
      return tempreturn; 
   };
};

*/

CaluMath.PM.PageMakerObjectArrayToString=function(){
//beginTime=new Date()
//beginTime=beginTime.getTime();


   //Comment: This is what causes the string at the end of the document to be written. It contains all of the information in the document. It calls the function CaluMath.PM.PageMakerObjectToString, which writes the indiidual entries. It is necessary to have this function (in addition to CaluMath.PM.PageMakerObjectToString), because this adds the Clipboard to the CaluMath.PM.PageMakerObjectArray. 
   var childarray=CaluMath.PM.PageMakerObjectArray.concat(CaluMath.PM.ClipBoard);;
   var childarraylength= childarray.length;
//   if(arguments[0] == null){
//      var tempreturn = ""; 
//      var tempreturn = new Array(); 
//      for(var i=0; i<childarraylength; i++){
//         tempreturn = tempreturn.push(" "+CaluMath.PM.PageMakerObjectToString(childarray[i])+" CM_PageMakerStringDelimiter ");
//      };
//   }
//   else{
      var tempreturn = [" "]; 
      for(var i=0; i<childarraylength; i++){
//         tempreturn = tempreturn.concat(" ",CaluMath.PM.PageMakerObjectToString(childarray[i])," '+\n'CM_PageMakerStringDelimiter ");
         tempreturn = tempreturn.concat(" ",CaluMath.PM.PageMakerObjectToString(childarray[i])," CM_PageMakerStringDelimiter ");
      };
      tempreturn = tempreturn.concat(" ");
//   };
   return tempreturn.join("");
};

CaluMath.PM.PageMakerObjectToString= function(){
   //Comment: If this is passes a CaluMath.PM.PageMakerObject, the string version of it is returned. If it is passed an array of objects, an array, containing string versions is returned. This is very different than what CaluMath.PM.PageMakerObjectArrayToString returns (which is a long string of all the strings of the objects with a delimiter separating the various individual strings). That function is used for writing the string at the end of the HTML document that contains all the information about the string. The array version of this is used for copying objects inside the same document. 
   var tempobject=arguments[0];
   if(tempobject.cm_constructorname != "Array"){
      var childarray = tempobject.childarray;
      if(childarray == null){
         childarray=[];
      };
      var childarraylength = childarray.length;
      var settings=tempobject.settings;
      var settingsstring = "";
   
      var optionsarray=tempobject.cm_optionsarray;
      var requiredarray=tempobject.cm_requiredarray;
   
      var childstring= "";
      if(childarraylength>0){
         for(var i=0; i<childarraylength-1; i++){
            childstring=childstring+CaluMath.PM.PageMakerObjectToString(childarray[i])+",";
         };
         childstring=childstring+CaluMath.PM.PageMakerObjectToString(childarray[childarraylength-1])+" ";
      };
      return 'CaluMath.PM.PageMakerObject("'+tempobject.type+'",'+tempobject.cm_optionsarraytostring()+','+tempobject.cm_requiredarraytostring()+',['+childstring+'])';      
   }
   else{
      var tempobjectlength= tempobject.length;
      returnarray= new Array();
      for(var newi=0; newi<tempobjectlength;newi++){
         returnarray[newi]= CaluMath.PM.PageMakerObjectToString(tempobject[newi]);
      };
      return returnarray;
   };
};


//Comment: This is used in Cutting and Pasting. //

CaluMath.PM.ParsePageMakerObjectString = function(){
   var tempobject=arguments[0];
   if(tempobject.cm_constructorname != "Array"){
      return [eval(tempobject)];
   }
   else{
      var tempobjectlength= tempobject.length;
      returnarray= new Array();
      for(var newi=0; newi<tempobjectlength;newi++){
         returnarray[newi]= eval(tempobject[newi]);
      };
      return returnarray;
   };
};


//Comment: This is used to parse the CaluMath.PM.PageMakerObjectArrayString of a loaded page. Parsing this string yields the CaluMath.PM.PageMakerObjectArray which is an array containing all the objects in the window.//


CaluMath.PM.ParsePageMakerPage=function(){ 
   var tempwindowpagemakerstring= CM_TempWindow.CaluMath.PM.PageMakerObjectArrayString;
   if(tempwindowpagemakerstring==null){
      //Comment: This should be replaced once we redo the Demo Pages not using this technique anymore.
      var scripttags = CM_TempWindow.document.getElementsByTagName("script");
      var scripttagslength=scripttags.length; 
      if((scripttags[scripttagslength-1].text).slice(3).match(/CM_PageMakerStringDelimiter/)){
         var newarray= (scripttags[scripttagslength-1].text).slice(3).split(/CM_PageMakerStringDelimiter/);
      }
      else{
         //Comment: This is for backwards compatability with earlier pages where I carelessly used the delimiter to be ZZZZZZZ.//
         var newarray= (scripttags[scripttagslength-1].text).slice(3).split(/ZZZZZZZ/);
      };
   }
   else{
      tempwindowpagemakerstring=tempwindowpagemakerstring.slice(0);
      if(tempwindowpagemakerstring.match(/CM_PageMakerStringDelimiter/)){
         var newarray= tempwindowpagemakerstring.split(/CM_PageMakerStringDelimiter/);
      }
      else{
         //Comment: This is for backwards compatability with earlier pages where I carelessly used the delimiter to be ZZZZZZZ.//
         var newarray= tempwindowpagemakerstring.split(/ZZZZZZZ/);
      };
   };
   var newarraylength= newarray.length; 
   CaluMath.PM.PageMakerObjectArray= new Array();
   CaluMath.PM.PlotlistObject= new Object();
//      CaluMath.PM.PlotlistObject["CM_ParentObject"]= new Object();
//      CaluMath.PM.PlotlistObject["CM_ParentObject"].draggedobjectslist= new Array();
//      CaluMath.PM.PlotlistObject["CM_ParentObject"].slidingscalelist= new Array();

   CaluMath.PM.MakeLists();


   CaluMath.PM.CreateDependencies();


   for(var i=0; i<newarraylength-1; i++){ 
      var temppagemakerobject = eval(newarray[i]); 
      if(temppagemakerobject.type != "clipboard"){
         CaluMath.PM.PageMakerObjectArray[i]= temppagemakerobject;
      }
      else{
         CaluMath.PM.ClipBoard= temppagemakerobject;
      };
   };



   CM_TopWindow.CaluMath.PM.CSSTextStyleString='\n.cm_math{white-space:nowrap}\n.cm_mathformula{white-space:nowrap}';
   //Comment: This ensures that functions created in the newly loaded window will be defined in the main window.
   for(var i=0; i<CaluMath.PM.EveryObjectArray.length; i++){
      var tempobject= CaluMath.PM.EveryObjectArray[i];
      if( tempobject.type =="definedfunction"){ 
         if(!tempobject.cm_getsetting("definition").match(/\'/) && !tempobject.cm_getsetting("definition").match(/\"/)){ 
            window[tempobject.cm_getsetting("name")]=CaluMath.PM.PlaceholderFunction;
         }
         else{
            window[tempobject.cm_getsetting("name")]=CaluMath.PM.PlaceholderStringFunction;
         };
      };
      if(tempobject.type =="splitfunction"){ 
         if(!tempobject.cm_getsetting("definition").match(/\'/) && !tempobject.cm_getsetting("definition").match(/\"/)){ 
            window[tempobject.cm_getsetting("name")]=CaluMath.PM.PlaceholderFunction;
         }
         else{
            window[tempobject.cm_getsetting("name")]=CaluMath.PM.PlaceholderStringFunction;
         };
      };
/*
      if(tempobject.type =="variablename"){ 
         try{ 
            eval(tempobject.cm_writehtml());
         }
         catch(e){ 
            var name=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"name");
            CM_TopWindow[name]="An object not created yet.";
         };
      };
      if(tempobject.type =="definedconstant"){
         var name=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"name");
         var constantdefinition=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"definition"); 
         try{ 
            window[name]= new Number(1);
            window[name].cm_placeholder="yes";
         }
         catch(e){ 
         };
      };

      if(tempobject.type =="nameclickedpoint"){ 
         var name=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"name");
         CM_TopWindow[name]=[0,0];
         CM_TopWindow[name].x =0;
         CM_TopWindow[name].y =0;
      };
      if(tempobject.type =="namedraggedvalue"){ 
         var name=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"name");
         CM_TopWindow[name]=[0,0];
         CM_TopWindow[name].x =0;
         CM_TopWindow[name].y =0;
      };
*/
      if(tempobject.type =="newplot"){
         var tempname=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"name")
      };
   };

   CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction();
   CaluMath.PM.AssignCasWindowToTextChildren();
   CaluMath.PM.AddListsToParsedString();

   CaluMath.PM.CreateInLineEditSelectMenu();
   document.getElementById("editinlinebutton").onclick=CaluMath.PM.InLineEditButtonOnclickFunction;

   return CaluMath.PM.PageMakerObjectArray;

};



//Comment: CaluMath.PM.ParsePageMakerPageStringNoWindow is used when a stringversion of all or part of the CaluMath.PM.PageMakerObjectArray needs to be parsed, but the corresponding page is not displayed in a window. This occurs when loading a previous version of a page and during CutAndPaste.//

CaluMath.PM.ParsePageMakerPageStringNoWindow=function(){
   if(arguments[0].match(/CM_PageMakerStringDelimiter/)){
      var newarray= arguments[0].split(/CM_PageMakerStringDelimiter/); 
   }
   else{
      var newarray= arguments[0].split(/ZZZZZZZ/); 
   };
   var newarraylength= newarray.length;
   CaluMath.PM.PageMakerObjectArray= new Array();
   CaluMath.PM.PlotlistObject= new Object();
   CaluMath.PM.MakeLists();
   CaluMath.PM.CreateDependencies();


   for(var i=0; i<newarraylength-1; i++){ 
      var temppagemakerobject = eval(newarray[i]); 

      if(temppagemakerobject.type != "clipboard"){
         CaluMath.PM.PageMakerObjectArray[i]= temppagemakerobject;
      }
      else{
         CaluMath.PM.ClipBoard= temppagemakerobject;
      };
   };
   //Comment: This ensures that functions created in the newly loaded window will be defined in the main window.
   for(var i=0; i<CaluMath.PM.EveryObjectArray.length; i++){
      var tempobject= CaluMath.PM.EveryObjectArray[i];
      if(1==1 && tempobject.type =="definedfunction"){ 
         if(!tempobject.cm_getsetting("definition").match(/\'/) && !tempobject.cm_getsetting("definition").match(/\"/)){ 
            window[tempobject.cm_getsetting("name")]=CaluMath.PM.PlaceholderFunction;
         }
         else{
            window[tempobject.cm_getsetting("name")]=CaluMath.PM.PlaceholderStringFunction;
         };
      };
      if(tempobject.type =="splitfunction"){ //alerttest("evaluating the function");
         if(!tempobject.cm_getsetting("definition").match(/\'/) && !tempobject.cm_getsetting("definition").match(/\"/)){ 
            window[tempobject.cm_getsetting("name")]=CaluMath.PM.PlaceholderFunction;
         }
         else{
            window[tempobject.cm_getsetting("name")]=CaluMath.PM.PlaceholderStringFunction;
         };
      };
      if(tempobject.type =="variablename"){ //alerttest("Making the new variable definition");
         try{ 
            eval(tempobject.cm_writehtml());
         }
         catch(e){ 
            var name=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"name");
            CM_TopWindow[name]="An object not created yet.";
         };
      };
      if(tempobject.type =="definedconstant"){
         var name=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"name");
         var constantdefinition=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"definition"); 
         try{ 
            window[name]= new Number(1);
            window[name].cm_placeholder="yes";
         }
         catch(e){ 
         };
      };
      if(tempobject.type =="nameclickedpoint"){ 
         var name=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"name");
         CM_TopWindow[name]=[0,0];
         CM_TopWindow[name].x =0;
         CM_TopWindow[name].y =0;
      };
      if(tempobject.type =="namedraggedvalue"){ 
         var name=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"name");
         CM_TopWindow[name]=[0,0];
         CM_TopWindow[name].x =0;
         CM_TopWindow[name].y =0;
      };
      if(tempobject.type =="newplot"){
         var tempname=CaluMath.PM.GetPageMakerObjectSetting(tempobject,"name")
      };
   };


   CaluMath.PM.AssignCasWindowToTextChildrenPrepFunction();
   CaluMath.PM.AssignCasWindowToTextChildren();

   CaluMath.PM.AddListsToParsedString();

   CaluMath.PM.CreateInLineEditSelectMenu();
   document.getElementById("editinlinebutton").onclick=CaluMath.PM.InLineEditButtonOnclickFunction;

   return CaluMath.PM.PageMakerObjectArray;
};



//Comment: When the CaluMath.PM.PageMakerObjectArrayString is parsed, the created objects need to be added to the correct lists. 

CaluMath.PM.AddListsToParsedString=function(){ 
  var tempobject=arguments[0]; 
  var CaluMathPM= CaluMath.PM;
  var CaluMathPMAddListsToParsedString=CaluMath.PM.AddListsToParsedString;
  if(tempobject==null){
     var childarray=CaluMath.PM.PageMakerObjectArray;
     CaluMath.PM.PageMakerObjectArray.childarray=CaluMath.PM.PageMakerObjectArray;
     var childarraylength=childarray.length;
     for(var i=0; i<childarraylength; i++){
        CaluMathPMAddListsToParsedString(childarray[i]);
     };
  }
  else{
     var tempaddfunction= CaluMath.PM["AddToLists"+tempobject.type];
     if(tempaddfunction !=null){
        tempaddfunction(tempobject);
     };
     var childarray=tempobject.childarray;
     var childarraylength=childarray.length;
     for(var i=0; i<childarraylength; i++){
        CaluMathPMAddListsToParsedString(childarray[i]);
     };
  }; 
};









