/*

    Copyright 2003-2007 Purdue University. 

    Developed by: Peter Turbek, Department of Mathematics, Computer Science, and Statistics, Purdue University Calumet.


    This file is part of CaluMath.

    CaluMath is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    CaluMath is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

//Comment: This file contains the routine invoked if an object has its name changed, or the axes or graph it is associated to changed upon editing.

CaluMath.PM.NewChangeName= function(){ 
   var tempobject=arguments[0];
   //Comment: tempobject is the object being edited, namely the object whose name has been changed.
   if(tempobject.type=="htmltext"){
      CM_TopWindow.CaluMath.PM.PageMakerObjectParent[tempobject.cm_getsetting("name")]=tempobject;
   };
   var newname= tempobject.cm_getsetting("name");
   //Comment: tempCM_EveryObjectArray is the list of objects that contains the objects that potentially depend on temopobject and will need to be changed because the name of tempobject has been changed. In many cases, it is the entire list of all objects created. For graphs however, it is not the entire list. In this case, the correct list is passed as arguments[1]
   var tempCM_EveryObjectArray=arguments[1];
   if(tempCM_EveryObjectArray==null){
      tempCM_EveryObjectArray=CaluMath.PM.EveryObjectArray;
   };
   //Comment: In all cases we recursively call CaluMath.PM.NewChangeName. At first, I was scared of going into infinite loops, but this has never happenned.
   if(dorecursion==null){
      dorecursion=true;
   };
   var dorecursion=true; 

   if(tempobject.type != "text"){
      //Comment: tempDefault is the Default object for tempobject. For example, if tempobject is arcplot then tempDefault is CaluMath.PM.PageMakerarcplotDefault
      var tempDefault= eval("CaluMath.PM.PageMaker"+tempobject.type+"Default");
      //Comment: tempDefaultListArray is the ListArray property of the Default object. It is an array of the names of all the lists that contain tempobject. These arrays are defined in the file cm_addtolists.js.  
      //Comment: For example, for arcplot we have CaluMath.PM.PageMakerarcplotDefault.ListArray=["arcplotlist", "canaddhighlightslist", "canaddtangentslist", "canaddtextplotslist", "graphlist", "cansupportbuttonslist", "cansupportsearcheslist"];
      var tempDefaultListArray= tempDefault.ListArray;
      if(tempDefaultListArray == null){
         tempDefaultListArray=[];
      };
      var tempDefaultListArraylength= tempDefaultListArray.length;
      var changedarray= new Array();

      if(tempobject.type !="newplot" && tempobject.type !="definedfunction" &&  tempobject.type !="coordinates" && tempobject.type !="table" && tempobject.type !="list" && tempobject.type !="definedconstant" && tempobject.type !="variablename" && tempobject.type !="nameclickedpoint" && tempobject.type !="namedraggedvalue" &&  tempobject.type !="gettextboxvalue" && tempobject.type !="getboxindex"){

         for(var i=0; i<tempCM_EveryObjectArray.length; i++){
            for(var j=0; j< tempDefaultListArraylength; j++){
               //Comment: tempDependenceObject is the property object of CaluMath.PM.PlotlistObject which lists the dependencies list of the tempCM_EveryObjectArray. 
               //Comment: For example if we are dealing with arcplot, tempDependenceObject cycles through 
               //Comment: CaluMath.PM.PlotlistObject.arcplotlistdependencies, CaluMath.PM.PlotlistObject.canaddhighlightslistdependencies, CaluMath.PM.PlotlistObject.canaddtangentslistdependencies etc. 
               tempDependenceObject = CaluMath.PM.PlotlistObject[tempDefaultListArray[j]+"dependencies"];
               if(tempDependenceObject != null){
                  if(tempDependenceObject[tempCM_EveryObjectArray[i].type] != null){
                     //Comment: We check the type of the current element in tempCM_EveryObjectArray and look at its property in the listdependencies tempDependenceObject. If this is not null, it tells the property we should check of the current object that might match the old value of tempobject.
                     for(var ii=0; ii< tempDependenceObject[tempCM_EveryObjectArray[i].type].length; ii++){ 
                        ////alerttest(["object is", tempobject, "dependencies is :",tempDefaultListArray[j]+"dependencies","old"+tempDefaultListArray[j], "oldvalue:", tempobject["old"+tempDefaultListArray[j]],"EveryObjectElement:", tempCM_EveryObjectArray[i] , "Checking the Setting for "+tempDependenceObject[tempCM_EveryObjectArray[i].type][ii]+"in the EveryObectArray Element:", tempCM_EveryObjectArray[i].cm_getsetting(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii])]);                 
                        ////alerttest([tempobject["old"+tempDefaultListArray[j]], tempCM_EveryObjectArray[i].cm_getsetting(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii]),tempobject["old"+tempDefaultListArray[j]]==tempCM_EveryObjectArray[i].cm_getsetting(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii])]);
                        if(tempobject["old"+tempDefaultListArray[j]]==tempCM_EveryObjectArray[i].cm_getsetting(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii])){
                           ////alerttest("OK before CaluMath.PM.GetListSetting");
                           ////alerttest([tempDefaultListArray[j],tempobject]);
                           var tempnewlistentry= CaluMath.PM.GetListSetting(tempDefaultListArray[j],tempobject);
                           ////alerttest("OK AFTER CaluMath.PM.GetListSetting");

                           if(tempnewlistentry != tempobject["old"+tempDefaultListArray[j]]){
                              tempCM_EveryObjectArray[i].cm_setrequiredoroption(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii], tempnewlistentry);
                              if(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii]=="graph" && tempCM_EveryObjectArray[i].type=="removegraph"){
                                 tempCM_EveryObjectArray[i].cm_setrequiredoroption("graphfullname", CaluMath.PM.GetPageMakerGraphFullName(tempobject));
                              };
                              if(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii]=="graph" && tempCM_EveryObjectArray[i].type=="hideandunhidegraph"){
                                 tempCM_EveryObjectArray[i].cm_setrequiredoroption("graphfullname", CaluMath.PM.GetPageMakerGraphFullName(tempobject));
                              };
                              if(dorecursion){
                                 changedarray.push(tempCM_EveryObjectArray[i]);
                              };
                           };
                        };
                     };
                  };
               };
            };
         };
      }
      else if(tempobject.type =="newplot"){

         var tempnewplotname= CaluMath.PM.GetPageMakerGraphFullName(tempobject);
         for(var i=0; i<tempCM_EveryObjectArray.length; i++){
            for(var j=0; j< tempDefaultListArraylength; j++){
               tempDependenceObject = CaluMath.PM.PlotlistObject[tempDefaultListArray[j]+"dependencies"];
               if(tempDependenceObject != null){
                  if(tempDependenceObject[tempCM_EveryObjectArray[i].type] != null){
                     for(var ii=0; ii< tempDependenceObject[tempCM_EveryObjectArray[i].type].length; ii++){ 
                        if(tempobject["old"+tempDefaultListArray[j]]==tempCM_EveryObjectArray[i].cm_getsetting(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii])){
                           var tempnewlistentry= CaluMath.PM.GetListSetting(tempDefaultListArray[j],tempobject);
                           if(tempnewlistentry != tempobject["old"+tempDefaultListArray[j]]){
                              tempCM_EveryObjectArray[i].cm_setrequiredoroption(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii], tempnewlistentry);
                              if(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii]=="graph" && tempCM_EveryObjectArray[i].type=="removegraph"){
                                 tempCM_EveryObjectArray[i].cm_setrequiredoroption("graphfullname", CaluMath.PM.GetPageMakerGraphFullName(tempobject));
                              };
                              if(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii]=="graph" && tempCM_EveryObjectArray[i].type=="hideandunhidegraph"){
                                 tempCM_EveryObjectArray[i].cm_setrequiredoroption("graphfullname", CaluMath.PM.GetPageMakerGraphFullName(tempobject));
                              };
                              if(dorecursion){
                                 changedarray.push(tempCM_EveryObjectArray[i]);
                              };
                           };
                        };
                     };
                 };
               };
            }
            if(tempCM_EveryObjectArray[i].type=="hideandunhidegraph" && tempCM_EveryObjectArray[i].cm_getsetting("graphfullname")==tempobject.oldname   && tempCM_EveryObjectArray[i].cm_getsetting("graph").match(/collection/)){
               var tempsplit=tempCM_EveryObjectArray[i].cm_getsetting("graph").split(".");
               var tempsplitlastterm=tempsplit[tempsplit.length-1];
               tempCM_EveryObjectArray[i].cm_setrequiredoroption("graph", tempnewplotname+"."+tempsplitlastterm); 
               tempCM_EveryObjectArray[i].cm_setrequiredoroption("graphfullname",  tempnewplotname); 
            };
         };
      }
      else if(tempobject.type =="definedconstant" || tempobject.type =="coordinates" || tempobject.type =="definedfunction"   || tempobject.type =="variablename" || tempobject.type =="nameclickedpoint" || tempobject.type =="namedraggedvalue" || tempobject.type=="gettextboxvalue" || tempobject.type =="getboxindex"){
//      if(tempobject.type =="definedfunction"){


//         for(var i=0; i<tempCM_EveryObjectArray.length; i++){
//            if(tempCM_EveryObjectArray[i].type=="definedconstant" && tempCM_EveryObjectArray[i].cm_getsetting("name")== tempobject.oldname && tempCM_EveryObjectArray[i] != tempobject){
//               return;
//            };
//         };





         var matchthis= "([^_a-zA-Z0-9])"+tempobject.oldname+"([^_a-zA-Z0-9])";
         var matchthisreg= new RegExp(matchthis, "g");
         var replacewiththis = "$1"+tempobject.cm_getsetting("name")+"$2";

         for(var i=0; i<tempCM_EveryObjectArray.length; i++){
            if(tempCM_EveryObjectArray[i] != tempobject){
            for(var j=0; j< tempDefaultListArraylength; j++){
//alerttest(tempCM_EveryObjectArray[i].cm_getsetting("name"));
               tempDependenceObject = CaluMath.PM.PlotlistObject[tempDefaultListArray[j]+"dependencies"];
               if(tempDependenceObject != null){
                  if(tempDependenceObject[tempCM_EveryObjectArray[i].type] != null){
                     for(var ii=0; ii< tempDependenceObject[tempCM_EveryObjectArray[i].type].length; ii++){ 
                        if(tempobject["old"+tempDefaultListArray[j]]==tempCM_EveryObjectArray[i].cm_getsetting(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii])){
                           var tempnewlistentry= CaluMath.PM.GetListSetting(tempDefaultListArray[j],tempobject);

                           if(tempnewlistentry != tempobject["old"+tempDefaultListArray[j]]){
                              tempCM_EveryObjectArray[i].cm_setrequiredoroption(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii], tempnewlistentry);
                              if(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii]=="graph" && tempCM_EveryObjectArray[i].type=="removegraph"){
                                 tempCM_EveryObjectArray[i].cm_setrequiredoroption("graphfullname", CaluMath.PM.GetPageMakerGraphFullName(tempobject));
                              };
                              if(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii]=="graph" && tempCM_EveryObjectArray[i].type=="hideandunhidegraph"){
                                 tempCM_EveryObjectArray[i].cm_setrequiredoroption("graphfullname", CaluMath.PM.GetPageMakerGraphFullName(tempobject));
                              };
                              if(dorecursion){
                                 changedarray.push(tempCM_EveryObjectArray[i]);
                              };
                           };
                        };
                     };
                  };
               };
//            };
            };
            var tempCM_EveryObjectArrayevalmathinputfields = CaluMath.PM.PlotlistObject.evalmathinputfields[tempCM_EveryObjectArray[i].type];
            if(tempCM_EveryObjectArrayevalmathinputfields != null){
               var tempCM_EveryObjectArrayevalmathinputfieldslength= tempCM_EveryObjectArrayevalmathinputfields.length;
               for(var checkfieldsindex = 0; checkfieldsindex<tempCM_EveryObjectArrayevalmathinputfieldslength; checkfieldsindex++){
//alerttest([tempCM_EveryObjectArray[i].type, tempCM_EveryObjectArrayevalmathinputfields.cm_display(), tempCM_EveryObjectArray[i].cm_getsetting(tempCM_EveryObjectArrayevalmathinputfields[checkfieldsindex]), tempCM_EveryObjectArrayevalmathinputfields[checkfieldsindex], (" "+ tempCM_EveryObjectArray[i].cm_getsetting(tempCM_EveryObjectArrayevalmathinputfields[checkfieldsindex])+" ").replace(matchthisreg,replacewiththis).replace(matchthisreg,replacewiththis).replace(/^\s*/,"").replace(/\s*$/,"")]);
                  tempCM_EveryObjectArray[i].cm_setrequiredoroption(tempCM_EveryObjectArrayevalmathinputfields[checkfieldsindex], (" "+ tempCM_EveryObjectArray[i].cm_getsetting(tempCM_EveryObjectArrayevalmathinputfields[checkfieldsindex])+" ").replace(matchthisreg,replacewiththis).replace(matchthisreg,replacewiththis).replace(/^\s*/,"").replace(/\s*$/,"")); 
               };
            };

            var tempCM_EveryObjectArraystringinputfields = CaluMath.PM.PlotlistObject.stringinputfields[tempCM_EveryObjectArray[i].type];
            if(tempCM_EveryObjectArraystringinputfields != null){
               var tempCM_EveryObjectArraystringinputfieldslength= tempCM_EveryObjectArraystringinputfields.length;
               for(var checkfieldsindex = 0; checkfieldsindex<tempCM_EveryObjectArraystringinputfieldslength; checkfieldsindex++){
                  var valueafterreplacement= CaluMath.PM.CycleThroughCM_EvalOccurrences(tempCM_EveryObjectArray[i].cm_getsetting(tempCM_EveryObjectArraystringinputfields[checkfieldsindex]),tempobject.oldname , tempobject.cm_getsetting("name"));
                  if(tempCM_EveryObjectArraystringinputfields[checkfieldsindex] == "name" && valueafterreplacement != tempCM_EveryObjectArray[i].cm_getsetting(tempCM_EveryObjectArraystringinputfields[checkfieldsindex])){
                     changedarray.push(tempCM_EveryObjectArray[i]);
//alerttest(valueafterreplacement);
                  }
                  tempCM_EveryObjectArray[i].cm_setrequiredoroption(tempCM_EveryObjectArraystringinputfields[checkfieldsindex], valueafterreplacement ); 
               };
            };
            if(tempCM_EveryObjectArray[i].type=="text" && tempCM_EveryObjectArray[i].cm_requiredarray!= null && tempCM_EveryObjectArray[i].cm_requiredarray[0] != null && tempCM_EveryObjectArray[i].cm_requiredarray[0][1] != null){
               tempCM_EveryObjectArray[i].cm_requiredarray[0][1]=CaluMath.PM.CycleThroughCM_EvalOccurrences(tempCM_EveryObjectArray[i].cm_requiredarray[0][1],tempobject.oldname , tempobject.cm_getsetting("name"));
            };
         }; //End of if statement making sure the object is not tempobject
         };
      }
      else if(1==2 && (tempobject.type =="definedconstant" || tempobject.type =="coordinates" || tempobject.type =="definedfunction"   || tempobject.type =="variablename" || tempobject.type =="nameclickedpoint" || tempobject.type =="namedraggedvalue" || tempobject.type=="gettextboxvalue" || tempobject.type =="getboxindex")){

         var constantregexpdot = new RegExp("^"+tempobject.oldname+"\\.","g");
         var constantregexpbracket = new RegExp("^"+tempobject.oldname+"\\[","g");
         var constantregexpdotreplacement= tempobject.cm_getsetting("name")+".";
         var constantregexpbracketreplacement = tempobject.cm_getsetting("name")+"[";

         var matchthis= "([^_a-zA-Z0-9])"+tempobject.oldname+"([^_a-zA-Z0-9])";
         var matchthisreg= new RegExp(matchthis, "g");
         var replacewiththis = "$1"+tempobject.cm_getsetting("name")+"$2";
   
  
         for(var i=0; i<tempCM_EveryObjectArray.length; i++){
            for(var j=0; j< tempDefaultListArraylength; j++){
               tempDependenceObject = CaluMath.PM.PlotlistObject[tempDefaultListArray[j]+"dependencies"];
               if(tempDependenceObject != null){
                  if(tempDependenceObject[tempCM_EveryObjectArray[i].type] != null){
                     for(var ii=0; ii< tempDependenceObject[tempCM_EveryObjectArray[i].type].length; ii++){ 
                        tempCM_EveryObjectArray[i].cm_setrequiredoroption(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii], (" "+tempCM_EveryObjectArray[i].cm_getsetting(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii])+" ").replace(matchthisreg,replacewiththis).replace(matchthisreg,replacewiththis).replace(/^\s*/,"").replace(/\s*$/,"")  );

/*
                        //Comment: The first item means the old value is the value of a setting in the tempCM_EveryObjectArray[i] element. In this case we change it. 
                        if(tempobject["old"+tempDefaultListArray[j]]==tempCM_EveryObjectArray[i].cm_getsetting(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii])){
                           var tempnewlistentry= CaluMath.PM.GetListSetting(tempDefaultListArray[j],tempobject);
                           if(tempnewlistentry != tempobject["old"+tempDefaultListArray[j]]){
                              tempCM_EveryObjectArray[i].cm_setrequiredoroption(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii], tempnewlistentry);
                           };
                        }
                        else{
                           //Comment: If the item doesn't match, we see if the item with a . or [ after match. In this case, we change it. 
                           if(tempCM_EveryObjectArray[i].cm_getsetting(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii]).match(constantregexpdot)){
                              tempCM_EveryObjectArray[i].cm_setrequiredoroption(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii], tempCM_EveryObjectArray[i].cm_getsetting(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii]).replace(constantregexpdot,constantregexpdotreplacement));
                           };
                           if(tempCM_EveryObjectArray[i].cm_getsetting(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii]).match(constantregexpbracket)){
                              tempCM_EveryObjectArray[i].cm_setrequiredoroption(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii], tempCM_EveryObjectArray[i].cm_getsetting(tempDependenceObject[tempCM_EveryObjectArray[i].type][ii]).replace(constantregexpbracket,constantregexpbracketreplacement));
                           };
                        };
*/
                     };
                  };
               };
            };
            var tempCM_EveryObjectArrayevalmathinputfields = CaluMath.PM.PlotlistObject.evalmathinputfields[tempCM_EveryObjectArray[i].type];
            if(tempCM_EveryObjectArrayevalmathinputfields != null){
               var tempCM_EveryObjectArrayevalmathinputfieldslength= tempCM_EveryObjectArrayevalmathinputfields.length;
               for(var checkfieldsindex = 0; checkfieldsindex<tempCM_EveryObjectArrayevalmathinputfieldslength; checkfieldsindex++){
                  tempCM_EveryObjectArray[i].cm_setrequiredoroption(tempCM_EveryObjectArrayevalmathinputfields[checkfieldsindex], (" "+ tempCM_EveryObjectArray[i].cm_getsetting(tempCM_EveryObjectArrayevalmathinputfields[checkfieldsindex])+" ").replace(matchthisreg,replacewiththis).replace(matchthisreg,replacewiththis).replace(/^\s*/,"").replace(/\s*$/,"")); 
               };
            };

            var tempCM_EveryObjectArraystringinputfields = CaluMath.PM.PlotlistObject.stringinputfields[tempCM_EveryObjectArray[i].type];
            if(tempCM_EveryObjectArraystringinputfields != null){
               var tempCM_EveryObjectArraystringinputfieldslength= tempCM_EveryObjectArraystringinputfields.length;
               for(var checkfieldsindex = 0; checkfieldsindex<tempCM_EveryObjectArraystringinputfieldslength; checkfieldsindex++){
                  tempCM_EveryObjectArray[i].cm_setrequiredoroption(tempCM_EveryObjectArraystringinputfields[checkfieldsindex], CaluMath.PM.CycleThroughCM_EvalOccurrences(tempCM_EveryObjectArray[i].cm_getsetting(tempCM_EveryObjectArraystringinputfields[checkfieldsindex]),tempobject.oldname , tempobject.cm_getsetting("name"))); 
               };
            };

//CM_CycleThroughCaluMath.Html.EvalOccurrences(tempCM_EveryObjectArray[i].cm_getsetting(tempCM_EveryObjectArraystringinputfields[checkfieldsindex]),tempobject.oldname , tempobject.cm_getsetting("name"))


            if(tempCM_EveryObjectArray[i].type=="text" && tempCM_EveryObjectArray[i].cm_requiredarray!= null && tempCM_EveryObjectArray[i].cm_requiredarray[0] != null && tempCM_EveryObjectArray[i].cm_requiredarray[0][1] != null){
               tempCM_EveryObjectArray[i].cm_requiredarray[0][1]=CaluMath.PM.CycleThroughCM_EvalOccurrences(tempCM_EveryObjectArray[i].cm_requiredarray[0][1],tempobject.oldname , tempobject.cm_getsetting("name"));
            };
//            else if(tempCM_EveryObjectArray[i].type=="coordinates"){
//               tempCM_EveryObjectArray[i].cm_setrequired("value", (" "+ tempCM_EveryObjectArray[i].cm_getsetting("value")+" ").replace(matchthisreg,replacewiththis).replace(matchthisreg,replacewiththis).replace(/^\s*/,"").replace(/\s*$/,"")); 
//            };
         };
      }
      else if(tempobject.type =="table"){
         for(var i=0; i<tempCM_EveryObjectArray.length; i++){
            var rowandcolregexp= new RegExp(tempobject.oldname+"R(\\d*)C(\\d*)");
            if(tempCM_EveryObjectArray[i].cm_getsetting("cm_inserttarget")== tempobject.oldname){
               tempCM_EveryObjectArray[i].cm_setrequiredoroption("cm_inserttarget", newname);
               if(dorecursion){
                  changedarray.push(tempCM_EveryObjectArray[i]);
               };
            }
            else if(tempCM_EveryObjectArray[i].cm_getsetting("cm_inserttarget").match(rowandcolregexp)){
               tempCM_EveryObjectArray[i].cm_setrequiredoroption("cm_inserttarget", tempCM_EveryObjectArray[i].cm_getsetting("cm_inserttarget").replace(rowandcolregexp, newname+"R$1C$2" ));
               if(dorecursion){
                  changedarray.push(tempCM_EveryObjectArray[i]);
               };
            };
         };
      }
      else if(tempobject.type =="list"){
         if(tempobject.cm_getsetting("listtype") != "dl"){
            for(var i=0; i<tempCM_EveryObjectArray.length; i++){
               var itemregexp= new RegExp("^"+tempobject.oldname+"I(\\d*)$");
               if(tempCM_EveryObjectArray[i].cm_getsetting("cm_inserttarget")== tempobject.oldname){
                  tempCM_EveryObjectArray[i].cm_setrequiredoroption("cm_inserttarget", newname);
                  if(dorecursion){
                     changedarray.push(tempCM_EveryObjectArray[i]);
                  };
               }
               else if(tempCM_EveryObjectArray[i].cm_getsetting("cm_inserttarget").match(itemregexp)){
                  tempCM_EveryObjectArray[i].cm_setrequiredoroption("cm_inserttarget", tempCM_EveryObjectArray[i].cm_getsetting("cm_inserttarget").replace(itemregexp, newname+"I$1" ));
                  if(dorecursion){
                     changedarray.push(tempCM_EveryObjectArray[i]);
                  };
               };
            };
         }
         else{
            for(var i=0; i<tempCM_EveryObjectArray.length; i++){
               var itemregexp1= new RegExp("^"+tempobject.oldname+"T(\\d*)$");
               var itemregexp2= new RegExp("^"+tempobject.oldname+"D(\\d*)$");
               if(tempCM_EveryObjectArray[i].cm_getsetting("cm_inserttarget")== tempobject.oldname){
                  tempCM_EveryObjectArray[i].cm_setrequiredoroption("cm_inserttarget", newname);
                  if(dorecursion){
                     changedarray.push(tempCM_EveryObjectArray[i]);
                  };
               }
               else if(tempCM_EveryObjectArray[i].cm_getsetting("cm_inserttarget").match(itemregexp1)){
                  tempCM_EveryObjectArray[i].cm_setrequiredoroption("cm_inserttarget", tempCM_EveryObjectArray[i].cm_getsetting("cm_inserttarget").replace(itemregexp1, newname+"T$1" ));
                  if(dorecursion){
                     changedarray.push(tempCM_EveryObjectArray[i]);
                  };
               }
               else if(tempCM_EveryObjectArray[i].cm_getsetting("cm_inserttarget").match(itemregexp2)){
                  tempCM_EveryObjectArray[i].cm_setrequiredoroption("cm_inserttarget", tempCM_EveryObjectArray[i].cm_getsetting("cm_inserttarget").replace(itemregexp2, newname+"D$1" ));
                  if(dorecursion){
                     changedarray.push(tempCM_EveryObjectArray[i]);
                  };
               };
            };
         }
      };

      var changedarraylength = changedarray.length;
      for(var j=0; j<changedarraylength; j++){
         CaluMath.PM.AssignOldValues(changedarray[j]);
         tempdeletefunction=eval("CaluMath.PM.DeleteFromLists"+changedarray[j].type);
         tempdeletefunction(changedarray[j]);
         tempaddtofunction=eval("CaluMath.PM.AddToLists"+changedarray[j].type);
         tempaddtofunction(changedarray[j]);
        //Comment: We should put false as the second argument here to stop recursion.
        CaluMath.PM.NewChangeName(changedarray[j],tempCM_EveryObjectArray);
      };
   };
};



CaluMath.PM.ChangePlotOrGraph= function(){ //alert("Doing changeplots or graph");
   var tempobject=arguments[0];
   //Comment: arguments[1] is a list of graph objects that we don't want changed, so they are eliminated by the first if statement in the variable i loop below. 
//alert(["Doing changeplots or graph", tempobject]);
   var dorecursion=arguments[1];

   var changedarray= new Array();
   var oldnewplot=tempobject.oldnewplot;
   var oldgraph=tempobject.oldgraph;
   var oldanimationplot= tempobject.oldanimationplotlist;

   var newnewplot= tempobject.cm_getsetting("newplot");
   var newgraph= tempobject.cm_getsetting("graph");
   var newanimationplot= CaluMath.PM.GetPageMakerGraphFullName(tempobject); 

   if(oldgraph == null){
      var oldgraph=tempobject.olddefinedfunction;
      var newgraph= tempobject.cm_getsetting("definedfunction");
   };
//alerttest(["Hello", arguments[0], arguments[1].length,arguments[2], oldgraph, newgraph])
      for(var i=0; i<CaluMath.PM.EveryObjectArray.length; i++){
         if(arguments[1].cm_contains(CaluMath.PM.EveryObjectArray[i])==false){
            if(tempobject.type.match(/button/) && CaluMath.PM.EveryObjectArray[i].type=="buttonaction" ){
               if(CaluMath.PM.EveryObjectArray[i].cm_getsetting("button")==tempobject.oldbuttonlist){
                   CaluMath.PM.EveryObjectArray[i].cm_setrequired("button", CaluMath.PM.GetListSetting("buttonlist", tempobject));
               };
            }

//            else if( (tempobject.type == "functionplot") && (oldnewplot != newnewplot || oldgraph != newgraph ) && ((CaluMath.PM.EveryObjectArray[i].cm_getsetting("newplot") == oldnewplot && ( CaluMath.PM.EveryObjectArray[i].cm_getsetting("graph").length >0 && CaluMath.PM.EveryObjectArray[i].cm_getsetting("graph") == oldgraph))  || (CaluMath.PM.EveryObjectArray[i].cm_getsetting("newplot") == oldnewplot && (CaluMath.PM.EveryObjectArray[i].cm_getsetting("graph").length > 0 && CaluMath.PM.EveryObjectArray[i].cm_getsetting("graph") == tempobject.cm_getsetting("name"))))  ){
            else if( tempobject.type == "functionplot" && (oldnewplot != newnewplot || oldgraph != newgraph ) && CaluMath.PM.EveryObjectArray[i].cm_getsetting("newplot") == oldnewplot && CaluMath.PM.EveryObjectArray[i].cm_getsetting("graph").length >0 && CaluMath.PM.EveryObjectArray[i].cm_getsetting("graph") == oldgraph){
               CaluMath.PM.EveryObjectArray[i].cm_setrequiredoroption("newplot", newnewplot);   
               CaluMath.PM.EveryObjectArray[i].cm_setrequiredoroption("graph", newgraph);   
               changedarray.push(CaluMath.PM.EveryObjectArray[i]);
            }
            else if( (tempobject.type == "arcplot" || tempobject.type == "linearplot") && (oldnewplot != newnewplot || oldgraph != newgraph ) && CaluMath.PM.EveryObjectArray[i].cm_getsetting("newplot") == oldnewplot && CaluMath.PM.EveryObjectArray[i].cm_getsetting("graph").length > 0 && CaluMath.PM.EveryObjectArray[i].cm_getsetting("graph") == tempobject.oldname ){  //tempobject.cm_getsetting("name") ){
               CaluMath.PM.EveryObjectArray[i].cm_setrequiredoroption("newplot", newnewplot);   
               CaluMath.PM.EveryObjectArray[i].cm_setrequiredoroption("graph", tempobject.oldname); //tempobject.cm_getsetting("name"));   
               changedarray.push(CaluMath.PM.EveryObjectArray[i]);
            }
            else if( ((tempobject.type == "rectangleplot" && CaluMath.PM.EveryObjectArray[i].type=="moverectangle") || ( tempobject.type == "pointplot" && CaluMath.PM.EveryObjectArray[i].type=="movepoint")  || ( tempobject.type == "movetextplot" && CaluMath.PM.EveryObjectArray[i].type=="movetextplot")) && (oldnewplot != newnewplot || oldgraph != newgraph )){
               CaluMath.PM.EveryObjectArray[i].cm_setrequiredoroption("newplot", newnewplot);   
               changedarray.push(CaluMath.PM.EveryObjectArray[i]);
            };
            if(oldanimationplot != newanimationplot){
               if( CaluMath.PM.EveryObjectArray[i].cm_getsetting("animationplot").length >0 && CaluMath.PM.EveryObjectArray[i].cm_getsetting("animationplot") == oldanimationplot){
                  CaluMath.PM.EveryObjectArray[i].cm_setrequiredoroption("animationplot", newanimationplot);   
                  changedarray.push(CaluMath.PM.EveryObjectArray[i]);
               }
               else if(CaluMath.PM.EveryObjectArray[i].type=="compositeanimation"){
                  for(var animationplotindex=1; animationplotindex<6; animationplotindex++){
                     if( CaluMath.PM.EveryObjectArray[i].cm_getsetting("animationplot"+animationplotindex).length >0 && CaluMath.PM.EveryObjectArray[i].cm_getsetting("animationplot"+animationplotindex) == oldanimationplot){
                        CaluMath.PM.EveryObjectArray[i].cm_setrequiredoroption("animationplot"+animationplotindex, newanimationplot);   
                        changedarray.push(CaluMath.PM.EveryObjectArray[i]);
                     };
                  };
               };
            };
         }
         else{
         };
      };

      var changedarraylength = changedarray.length;
      for(var j=0; j<changedarraylength; j++){
         CaluMath.PM.AssignOldValues(changedarray[j]);

         tempdeletefunction=eval("CaluMath.PM.DeleteFromLists"+changedarray[j].type);
         tempdeletefunction(changedarray[j]);
 
         tempaddtofunction=eval("CaluMath.PM.AddToLists"+changedarray[j].type);
         tempaddtofunction(changedarray[j]);
        //Comment: We should put false as the second argument here to stop recursion.
        CaluMath.PM.ChangePlotOrGraphAux(changedarray[j], arguments[1]);
      };

};



CaluMath.PM.ChangePlotOrGraphAux= function(){ 
   var tempobject=arguments[0];
   var dorecursion=arguments[1];



   var tempDefault= tempobject.defaultobject(); 
   var tempDefaultListArray= tempDefault.ListArray;
   if(tempDefaultListArray == null){
      // alerttest("ListArray is undefined for "+ tempobject+" in the Routine CaluMath.PM.NewChangeName." );
      tempDefaultListArray=[];
   };
   var tempDefaultListArraylength= tempDefaultListArray.length;
   var changedarray= new Array();

      for(var i=0; i<CaluMath.PM.EveryObjectArray.length; i++){
         for(var j=0; j< tempDefaultListArraylength; j++){
            tempDependenceObject = CaluMath.PM.PlotlistObject[tempDefaultListArray[j]+"dependencies"];
            if(tempDependenceObject != null){
               if(tempDependenceObject[CaluMath.PM.EveryObjectArray[i].type] != null){
                  for(var ii=0; ii< tempDependenceObject[CaluMath.PM.EveryObjectArray[i].type].length; ii++){ 
//alerttest(["object is", tempobject, "dependencies is :",tempDefaultListArray[j]+"dependencies", "oldvalue:", tempobject["old"+tempDefaultListArray[j]],"EveryObjectElement:", CaluMath.PM.EveryObjectArray[i] , "Checking the Setting for "+tempDependenceObject[CaluMath.PM.EveryObjectArray[i].type][ii]+"in the EveryObectArray Element:", CaluMath.PM.EveryObjectArray[i].cm_getsetting(tempDependenceObject[CaluMath.PM.EveryObjectArray[i].type][ii])]);                 
                    if(tempobject["old"+tempDefaultListArray[j]]==CaluMath.PM.EveryObjectArray[i].cm_getsetting(tempDependenceObject[CaluMath.PM.EveryObjectArray[i].type][ii])){
//alerttest(["Getting the new setting", tempDefaultListArray[j], tempobject]);
                       var tempnewlistentry= CaluMath.PM.GetListSetting(tempDefaultListArray[j],tempobject);
//alerttest(["Yes they match", CaluMath.PM.EveryObjectArray[i],tempnewlistentry]);
                        if(tempnewlistentry != tempobject["old"+tempDefaultListArray[j]]){
                           CaluMath.PM.EveryObjectArray[i].cm_setrequiredoroption(tempDependenceObject[CaluMath.PM.EveryObjectArray[i].type][ii], tempnewlistentry);
//alerttest(["value should be changed for", CaluMath.PM.EveryObjectArray[i].cm_getsetting("name")]);
                              changedarray.push(CaluMath.PM.EveryObjectArray[i]);
                        };
                     };
                  };
              };
            };
         };
     };
      var changedarraylength = changedarray.length;
      for(var j=0; j<changedarraylength; j++){
         CaluMath.PM.AssignOldValues(changedarray[j]);

         tempdeletefunction=eval("CaluMath.PM.DeleteFromLists"+changedarray[j].type);
         tempdeletefunction(changedarray[j]);
 
         tempaddtofunction=eval("CaluMath.PM.AddToLists"+changedarray[j].type);
         tempaddtofunction(changedarray[j]);
        //Comment: We should put false as the second argument here to stop recursion.
        CaluMath.PM.ChangePlotOrGraphAux(changedarray[j], arguments[1]);
      };
};


CaluMath.PM.CycleThroughCM_EvalOccurrences = function(){
   var tempstring= arguments[0];
   var matchthis= "([^_a-zA-Z0-9])"+arguments[1]+"([^_a-zA-Z0-9])";
   var matchthisreg= new RegExp(matchthis, "g");
   var replacewiththis = "$1"+arguments[2]+"$2";

//alert([ matchthis, matchthisreg, replacewiththis]);  
   var tempsubstring= tempstring; 
   var tempstringarray= new Array();
   var doesitmatch=tempsubstring.match(/cm_eval[m]{0,1}\(/);
   while(doesitmatch != null){
      tempstringarray.push(tempsubstring.slice(0,doesitmatch.index+doesitmatch[0].length-1));
//alerttest(["first part ", tempsubstring.slice(0,doesitmatch.index+doesitmatch[0].length-1)]);
      tempsubstring = tempsubstring.slice(doesitmatch.index+doesitmatch[0].length-1)
//alert(["rest of string",tempsubstring]);
      var matchresult = tempsubstring.cm_matchdelimiters(0,/\(/,/\)/);
      var tempmatch= tempsubstring.slice(matchresult[0]+matchresult[2].length-1,matchresult[1]+1);
      tempsubstring=tempsubstring.slice(matchresult[1]+1);
//alert([tempmatch]);
//      if(tempmatch.match(matchthisreg) == null){
//         tempstringarray.push(tempmatch);
//      }
//      else{
//         if(confirm("Replace the "+matchthis+" in "+tempmatch+" with "+replacewiththis+"?")){
            tempmatch= tempmatch.replace(matchthisreg, replacewiththis).replace(matchthisreg, replacewiththis);
            tempstringarray.push(tempmatch);
            doesitmatch=tempsubstring.match(/cm_eval[m]{0,1}\(/);

//         }
//         else{
//            tempstringarray.push(tempmatch);
//         };
//      };
   };
   tempstringarray.push(tempsubstring);
//alert(tempstringarray);
return tempstringarray.join("");
};
   

//Comment: This CaluMath.PM.NewPlotChangeRoutine is used when a graph is drawn on one set of axes, but when it is edited, it is placed on another set of axes. Since the graphs are children of the axes, the childarry of the axes must have the graphs removed from them and made children of the new set of axes. 

CaluMath.PM.NewPlotChangeRoutine= function(){
   var tempobject=arguments[0];
   var temprequired=arguments[1];
   CaluMath.PM.AssignParents();
   var tempreturn=false;
   var tempobjectparenttype=tempobject.parent.type;
      if(!CM_EditMode || tempobjectparenttype=="buttonaction" || tempobjectparenttype=="forloop" || tempobjectparenttype=="newwindow" || tempobjectparenttype=="routine" || tempobjectparenttype=="conditional"){
         return false;
      }; 
      if((tempobject.cm_getsetting("newplot") != tempobject.oldnewplot) && (tempobject.type=="functionplot" || tempobject.type=="animationplot" || tempobject.type=="updatablefunctionplot" || tempobject.type=="arcplot" || tempobject.type=="rectangleplot" || tempobject.type=="labelvertex" || tempobject.type=="labelallvertices" || tempobject.type=="linearplot" || tempobject.type=="pointplot" || tempobject.type=="labelplot" || tempobject.type=="arrow" || tempobject.type=="textplot" || tempobject.type=="bracket" || tempobject.type=="pointbrackets3" || tempobject.type=="pointbrackets2" || tempobject.type=="pointbrackets1")){
            var tempobjectparent=tempobject.parent; 
            var childindex= CaluMath.PM.AssignChildIndex(tempobject); 
            tempobjectparent.childarray.splice(childindex,1);
            tempreturn=true;
      } 
      else if(tempobject.type == "tangent" &&   ((tempobject.cm_getsetting("newplot") != tempobject.oldnewplot) || (tempobject.cm_getsetting("graph") != tempobject.oldgraph))){
         var tempobjectparent=tempobject.parent; 
         var childindex= CaluMath.PM.AssignChildIndex(tempobject); 
         tempobjectparent.childarray.splice(childindex,1);
         tempreturn=true;
      }
      else if(tempobject.type == "highlight" && ((tempobject.cm_getsetting("newplot") != tempobject.oldnewplot) ||(tempobject.cm_getsetting("graph") != tempobject.oldgraph))){
         var tempobjectparent=tempobject.parent; 
         var childindex= CaluMath.PM.AssignChildIndex(tempobject); 
         tempobjectparent.childarray.splice(childindex,1);
         tempreturn=true;
      }
      else if(tempobject.type == "textplot" && ((tempobject.cm_getsetting("newplot") != tempobject.oldnewplot) ||(tempobject.cm_getsetting("graph") != tempobject.oldgraph))){
         var tempobjectparent=tempobject.parent; 
         var childindex= CaluMath.PM.AssignChildIndex(tempobject); 
         tempobjectparent.childarray.splice(childindex,1);
         tempreturn=true;
      };

      CaluMath.PM.NewPlotChangeChildRoutine(tempobject);
   return tempreturn;
};

CaluMath.PM.NewPlotChangeChildRoutine= function(){
   var tempobject=arguments[0];
   var tempnewplot= tempobject.cm_getsetting("newplot");
   var tempdefinedfunction= tempobject.cm_getsetting("definedfunction");
   var tempgraph= tempobject.cm_getsetting("graph");
   var tempobjectchildarray=tempobject.childarray;
   var tempobjectchildarraylength=tempobjectchildarray.length;
   var newgraphlist = CaluMath.PM.GetPageMakerGraphFullName(tempobject);

   for(var j=0; j< CaluMath.PM.EveryObjectArray.length; j++){ 
      if(CaluMath.PM.PlotlistObject.graphlistdependencies != null){ 
         var temppropertyname=CaluMath.PM.PlotlistObject.graphlistdependencies[CaluMath.PM.EveryObjectArray[j].type];
         if(temppropertyname != null && CaluMath.PM.EveryObjectArray[j].cm_getsetting(temppropertyname)==tempobject.oldgraphlist ){
            CaluMath.PM.EveryObjectArray[j].cm_setrequiredoroption(temppropertyname, newgraphlist);
            if(CaluMath.PM.EveryObjectArray[j].type=="removegraph"){
               CaluMath.PM.EveryObjectArray[j].cm_setrequiredoroption("graphfullname", newgraphlist);
            };
         };
      };
   };

     
    if((tempnewplot != tempobject.oldnewplot) || ( tempobject.cm_getsetting("definedfunction") != tempobject.olddefinedfunction) || ( tempobject.cm_getsetting("graph") != tempobject.oldgraph) ){     
       for(var i=0; i< tempobjectchildarraylength; i++){


          CaluMath.PM.AssignOldValues(tempobjectchildarray[i]);
//          CM_TopWindow["CM_DeleteFromLists"+tempobjectchildarray[i].type](tempobjectchildarray[i]);
         tempdeletefunction=eval("CaluMath.PM.DeleteFromLists"+tempobjectchildarray[i].type);
          tempdeletefunction(tempobjectchildarray[i]);
          tempobjectchildarray[i].cm_setrequired("newplot", tempnewplot);
          if(tempobjectchildarray[i].cm_getsetting("graph")==tempobject.oldgraph){
             tempobjectchildarray[i].cm_setrequiredoroption("graph", tempgraph);
          }
          else if(tempobjectchildarray[i].cm_getsetting("graph")==tempobject.olddefinedfunction){
             tempobjectchildarray[i].cm_setrequiredoroption("graph", tempdefinedfunction);
          };
//          CM_TopWindow["CM_AddToLists"+tempobjectchildarray[i].type](tempobjectchildarray[i]);
         tempaddfunction=eval("CaluMath.PM.AddToLists"+tempobjectchildarray[i].type);
         tempaddfunction(tempobjectchildarray[i]);
          var childnewgraphlist = CaluMath.PM.GetPageMakerGraphFullName(tempobjectchildarray[i]);
          for(var j=0; j< CaluMath.PM.EveryObjectArray.length; j++){
             if(CaluMath.PM.PlotlistObject.graphlistdependencies != null){
                var temppropertyname=CaluMath.PM.PlotlistObject.graphlistdependencies[CaluMath.PM.EveryObjectArray[j].type];
                if((tempobjectchildarray[i].oldnewplot== CaluMath.PM.EveryObjectArray[j].cm_getsetting("newplot")) && (temppropertyname != null && CaluMath.PM.EveryObjectArray[j].cm_getsetting(temppropertyname)==tempobjectchildarray[i].oldgraphlist)){
                   CaluMath.PM.EveryObjectArray[j].cm_setrequiredoroption(temppropertyname, childnewgraphlist);
                   if(CaluMath.PM.EveryObjectArray[j].type=="removegraph"){
                      CaluMath.PM.EveryObjectArray[j].cm_setrequiredoroption("graphfullname", childnewgraphlist);
                   };
                   CaluMath.PM.EveryObjectArray[j].cm_setrequiredoroption(temppropertyname, childnewgraphlist);
//alerttest(["Found one in second part", CaluMath.PM.EveryObjectArray[j].cm_getsetting("name"), CaluMath.PM.EveryObjectArray[j].cm_getsetting("newplot"), CaluMath.PM.EveryObjectArray[j].cm_getsetting(temppropertyname)]);
                };
             }
             else{
                alert(["CaluMath.PM.PlotlistObject.graphlistdependencies is undefined in CaluMath.PM.NewPlotChangeChildRoutine"]);
             };
          };
          CaluMath.PM.NewPlotChangeChildRoutine(tempobjectchildarray[i]);

       };
    };
};


CaluMath.PM.AssignOldValues=function(){ 
      var editedtempobject=arguments[0];
   if(editedtempobject.type != "text"){
      var defaultobject=eval("CaluMath.PM.PageMaker"+editedtempobject.type+"Default");
      var tempListArray = defaultobject.ListArray;
      if(tempListArray==null){
         tempListArray =[];
      };
      if(editedtempobject.cm_getsetting != null && editedtempobject.cm_getsetting("name").length>0){
         editedtempobject.oldname=editedtempobject.cm_getsetting("name");
      };
      if(editedtempobject.cm_getsetting != null && editedtempobject.cm_getsetting("definedfunction").length>0){
         editedtempobject.olddefinedfunction=editedtempobject.cm_getsetting("definedfunction");
      };
      if(editedtempobject.cm_getsetting != null && editedtempobject.cm_getsetting("graph").length>0){
         editedtempobject.oldgraph=editedtempobject.cm_getsetting("graph");
      };
      if(editedtempobject.cm_getsetting != null && editedtempobject.cm_getsetting("animationplot").length>0){
         editedtempobject.oldanimationplot=editedtempobject.cm_getsetting("animationplot");
      };
      editedtempobject.oldgraphlist= CaluMath.PM.GetPageMakerGraphFullName(editedtempobject);
      if(editedtempobject.cm_getsetting != null && editedtempobject.cm_getsetting("newplot").length >0){
         editedtempobject.oldnewplot=editedtempobject.cm_getsetting("newplot");
         for(var jj=0; jj<tempListArray.length; jj++){
             if(!(CaluMath.PM.ListsSubordinateToNewPlotArray.cm_contains(tempListArray[jj]))){
             if(CaluMath.PM.PlotlistObject[tempListArray[jj] ] != null){
                var tempindex = CaluMath.PM.ArrayIndex(CaluMath.PM.PlotlistObject[tempListArray[jj] ],editedtempobject,2);
                if(tempindex != null){ 
                   editedtempobject["old"+tempListArray[jj]]= CaluMath.PM.PlotlistObject[tempListArray[jj]][tempindex][1];
                };
             };
           }
           else{
             if(CaluMath.PM.PlotlistObject[editedtempobject.oldnewplot][tempListArray[jj]] != null){
                var tempindex = CaluMath.PM.ArrayIndex(CaluMath.PM.PlotlistObject[editedtempobject.oldnewplot][tempListArray[jj]],editedtempobject,2); 
                if(tempindex != null){ 
                   editedtempobject["old"+ tempListArray[jj]]= CaluMath.PM.PlotlistObject[editedtempobject.oldnewplot][tempListArray[jj]][tempindex][1];
                };
             };
           }
         };
      };
      if(editedtempobject.cm_getsetting("cm_caswindow").length >0){
         editedtempobject.oldcaswindow=editedtempobject.cm_getsetting("cm_caswindow");
         for(var jj=0; jj<tempListArray.length; jj++){
           if(tempListArray[jj] == "namedobjectlist" || tempListArray[jj] == "staticobjectlist"  ){
             if(CaluMath.PM.PlotlistObject[editedtempobject.oldcaswindow]==null){
                alert("It is likely that you changed the name of an object that unfortunately had the effect of changing references to the object in a place you did not want. You should save your work as soon as possible to avoid errors!");
             }
             else if(CaluMath.PM.PlotlistObject[editedtempobject.oldcaswindow][tempListArray[jj]] != null){
                var tempindex = CaluMath.PM.ArrayIndex(CaluMath.PM.PlotlistObject[editedtempobject.oldcaswindow][tempListArray[jj]],editedtempobject,2); 
                if(tempindex != null){ 
                   editedtempobject["old"+ tempListArray[jj]]= CaluMath.PM.PlotlistObject[editedtempobject.oldcaswindow][tempListArray[jj]][tempindex][1];
                }
             };
           }
           else{
             if(CaluMath.PM.PlotlistObject[tempListArray[jj]] != null){
                var tempindex = CaluMath.PM.ArrayIndex(CaluMath.PM.PlotlistObject[tempListArray[jj]],editedtempobject,2);
                if(tempindex != null){ 
                   editedtempobject["old"+ tempListArray[jj]]= CaluMath.PM.PlotlistObject[tempListArray[jj]][tempindex][1];
                }; 
             };
           }
         };
      };
      if(editedtempobject.cm_getsetting("cm_caswindow").length ==0 && editedtempobject.cm_getsetting("newplot").length ==0){
         if(tempListArray != null){
         for(var jj=0; jj<tempListArray.length; jj++){
             if(CaluMath.PM.PlotlistObject[tempListArray[jj]] != null){
                var tempindex = CaluMath.PM.ArrayIndex(CaluMath.PM.PlotlistObject[tempListArray[jj]],editedtempobject,2); 
                if(tempindex != null){
                   editedtempobject["old"+ tempListArray[jj]]= CaluMath.PM.PlotlistObject[tempListArray[jj]][tempindex][1];
                };
             };
         };
         };
      };
   }; 
};




