/*

    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 different functions that checks the validity of user inputs. 

//Comment: In addition to checking input, they determine the type of searches that should be done on the field when a constant, function, or variable name changes. If the field contains string input, a search is made of all expressions of the form cm_eval(  ) and cm_evalm(  ). If the field is checked by one of the versions of CaluMath.PM.IsEvalMath, every occurrence of that name (delimeted by nonnumeric, nonletter, and non-underscored characters) is replaced. 

//Comment: Sometimes we want to do all replacements (not just those in cm_eval(  ) expression) in a String input. For these, we have appended ReplaceAll to the Input Check name. This new name is detected in the CaluMath.PM.CreateDependencies routine in the cm_defaults.js file and ensure that the proper replacements are done if a constant, function, or variable changes name. 

CaluMath.PM.IsNonEmptyString=function(){
   var tempstring= arguments[0];
//   if(tempstring==null || tempstring.cm_constructorname != "String" || tempstring.length==0){
   if(tempstring==null || (tempstring.cm_constructorname == "String" && tempstring.match(/\S/)==null)){
      return false;
   }
   else{
      return true;
   };
};

CaluMath.PM.IsNonEmptyStringReplaceAll = function(){
   var tempstring= arguments[0];
   return CaluMath.PM.IsNonEmptyString(tempstring); 
};

CaluMath.PM.IsNonEmptyStringReplaceAll.cm_message="non empty string of characters";

CaluMath.PM.IsNonEmptyString.cm_message="non empty string of characters";

CaluMath.PM.IsBlankOrNonEmptyString=function(){
   var tempstring= arguments[0];
//   if(tempstring==null ||  (tempstring.cm_constructorname == "String" && tempstring.length==0)){
   if(tempstring==null ||  (tempstring.cm_constructorname == "String" && tempstring.match(/\S/)==null)){
      return true;
   }
   else{
     return CaluMath.PM.IsNonEmptyString(tempstring);
   };
};

CaluMath.PM.IsBlankOrNonEmptyStringReplaceAll=function(){
   var tempstring= arguments[0];
   return CaluMath.PM.IsBlankOrNonEmptyString(tempstring);
};


CaluMath.PM.IsBlankOrNonEmptyString.cm_message="blank or a non empty string of characters";

CaluMath.PM.IsValidName=function(){
   var tempstring= arguments[0];
   if(tempstring==null || tempstring.cm_constructorname != "String" || tempstring.length==0 || tempstring.match(/^[a-zA-Z]/)==null || tempstring.match(/\w\s+\w/)){
      return false;
   }
   else{
      return true;
   };
};

CaluMath.PM.IsValidName.cm_message="non empty string of characters that begins with a letter. The name should include no spaces";

CaluMath.PM.IsBlankOrValidName=function(){
   var tempstring= arguments[0];
   if(tempstring==null || tempstring.cm_constructorname != "String"){
      return false;
   }
   else if(tempstring.length==0 || tempstring.match(/^[a-zA-Z]/)!=null){
     return true;
   }
   else{
      return true;
   };
};

CaluMath.PM.IsBlankOrValidName.cm_message="blank or a non empty string of characters that begins with a letter";


CaluMath.PM.IsNumber=function(){
   var tempstring= arguments[0];
   if(CM_TopWindow.CaluMath.PM.IgnoreMathInputChecks != true){
      if(tempstring==null){
         return false;
      }
      else{
         try{
            tempeval= eval(tempstring);
            if(tempeval.cm_constructorname=="Number"){
               return true;
            }
            else{
               return false;
            };
         }
         catch(e){ return false;};
      };
   }
   else{
      return true;
   };
};

CaluMath.PM.IsNumberReplaceAll=function(){
   var tempstring= arguments[0];
   return CaluMath.PM.IsNumber(tempstring);
};


CaluMath.PM.IsNumber.cm_message="a number";

CaluMath.PM.IsBlankOrNumber=function(){
   var tempstring= arguments[0];
   if(CM_TopWindow.CaluMath.PM.IgnoreMathInputChecks != true){
      if(tempstring==null || (tempstring.cm_constructorname=="String" && tempstring.match(/\S/)==null) ){
         return true;
      }
      else{
         return CaluMath.PM.IsNumber(tempstring);
      };
   }
   else{
      return true;
   };
};

CaluMath.PM.IsBlankOrNumber.cm_message="a blank or a number";

CaluMath.PM.IsDefaultOrNumber=function(){
   var tempstring= arguments[0];
   if(CM_TopWindow.CaluMath.PM.IgnoreMathInputChecks != true){
      if(tempstring=="n/a" || tempstring=="default" ||  (tempstring.cm_constructorname=="String" && tempstring.match(/\S/)==null) ){
         return true;
      }
      else{
         return CaluMath.PM.IsNumber(tempstring);
      };
   }
   else{
      return true;
   };
};

CaluMath.PM.IsDefaultOrNumber.cm_message="n/a, default or a number";


CaluMath.PM.IsEvalMath=function(){
   var tempstring= arguments[0];
   if(tempstring==null){
      return false;
   }
   else{
      if(CM_TopWindow.CaluMath.PM.IgnoreMathInputChecks != true){
         try{
            tempeval= CaluMath.Html.EvalMath(tempstring);
            if(tempeval.cm_constructorname=="Number"){
               return true;
            }
            else{
               return false;
            };
         }
         catch(e){ return false;};
      }
      else{
         return true;
      };
   };
   
};

CaluMath.PM.IsEvalMath.cm_message="a mathematical expression that evaluates to a number";

CaluMath.PM.IsDefaultOrEvalMath=function(){
   var tempstring= arguments[0];
   if(tempstring==null){
      return false;
   }
   else if(tempstring=="default"){
      return true;
   }
   else{
      if(CM_TopWindow.CaluMath.PM.IgnoreMathInputChecks != true){
         try{
            tempeval= CaluMath.Html.EvalMath(tempstring);
            if(tempeval.cm_constructorname=="Number"){
               return true;
            }
            else{
               return false;
            };
         }
         catch(e){ return false;};
      }
      else{
         return true;
      };
   };
};

CaluMath.PM.IsDefaultOrEvalMath.cm_message="either the word default, or a mathematical expression that evaluates to a number";

CaluMath.PM.IsBlankOrEvalMath=function(){
   var tempstring= arguments[0];
   if(tempstring==null){
      return true;
   }
   else{
      if(CM_TopWindow.CaluMath.PM.IgnoreMathInputChecks != true){
         try{
            tempeval= CaluMath.Html.EvalMath(tempstring);
            if(tempeval.cm_constructorname=="Number"){
               return true;
            }
            else{
               return false;
            };
         }
         catch(e){ return false;};
      }
      else{
         return true;
      };
   };
};


CaluMath.PM.IsListOfPoints=function(){
   var tempstring= arguments[0];
   tempstring=tempstring.replace(/\/\,/g, "CM_COMMAREPLACEMENT");
   if(tempstring==null || tempstring.match(/\(/)==null){
      return false;
   }
   else{
      try{
         var pointsarray=new Array();

         while(tempstring.match(/\(/)){
            var tempreturn =tempstring.cm_matchdelimiters(0,"(",")");
            if(tempreturn =="cm_fail"){
               return false;
            }
            else{
               var pointstring= tempstring.slice(tempreturn[0]+1,tempreturn[1]);
               if(pointstring.match(/\,/)==null){
                  return false;
               }
               else{ 
                  var onepoint=pointstring.split(/\,/);
                  if(CM_TopWindow.CaluMath.PM.IgnoreMathInputChecks !=true){
                     var xcoord=onepoint[0].cm_evalmath();
                     var ycoord=onepoint[1].cm_evalmath();
                     if(isNaN(xcoord) || isNaN(ycoord)){
                        return false;
                     }
                     else{
                        pointsarray=pointsarray.concat(['"'+onepoint[0]+'".cm_evalmath()','"'+onepoint[1]+'".cm_evalmath()']);
                        tempstring=tempstring.slice(tempreturn[1]+1);
                     };
                  }
                  else{
                     pointsarray=pointsarray.concat(['"'+onepoint[0]+'".cm_evalmath()','"'+onepoint[1]+'".cm_evalmath()']);
                     tempstring=tempstring.slice(tempreturn[1]+1);
                  };
               };
            };
         };

         var pointsarraylength = pointsarray.length;
         for(var i=0; i< pointsarray.length; i++){
            pointsarray[i]=pointsarray[i].replace(/CM_COMMAREPLACEMENT/g,',');
         };
         return pointsarray;
      }
      catch(e){ return false;};
   };
};

CaluMath.PM.IsListOfPoints.cm_message="a list of points";



CaluMath.PM.ConvertArrayEntriesToStringElements=function(){
   //Comment: This isn't an input check. This is used in Line Segment Plots. It takes array input (written as a String), and searches for the entries. It puts double quotes around each entry and appends .cm_evalmath() to it, to each entry is evaluated mathematically. This is what is written to the web page in cm_pagemakerobjectcreatorroutines.js.
   var tempstring= arguments[0].replace(/\[\s*\[/g, "[[").replace(/\]\s*\]/g, "]]");
   tempstring=tempstring.replace(/\/\,/g,"CM_COMMAREPLACEMENT");
   tempstring=tempstring.replace(/\s*\,\s*/g, ",").replace(/\[\s*\[/g,"[[").replace(/\[\s*\[/g,"[[");
   tempstring=tempstring.replace(/\[([^\[])/g,'["$1');
   tempstring=tempstring.replace(/([^\]])\]/g,'$1".cm_evalmath()]');
   tempstring=tempstring.replace(/\,/g, '".cm_evalmath(),"');
   tempstring=tempstring.replace(/\]\"\.cm_evalmath\(\)\,/g, '],');
   tempstring=tempstring.replace(/\,\"\[/g, ',[');
   tempstring=tempstring.replace(/\,\"([^\[\,]+)\[/g, ',$1[');
   tempstring=tempstring.replace(/\[\"([^\[\,]+)\[/g, '[$1[');
   tempstring=tempstring.replace(/CM_COMMAREPLACEMENT/g, ',');
   return tempstring;
};


/*
//Comment: This isn't an input check, it just prepares the points in LinearPlot, if they are entered as some sort of array, to have the entries made Strings and append cm_javascript to them, so math expressions can be entered. 
CaluMath.PM.ConvertArrayEntriesToStringElements=function(){
   var tempstring= arguments[0];
   if(tempstring==null || tempstring.match(/\[/)==null){
      return arguments[0];
   }
   else{
      var tempreturn = arguments[0].replace(/\/\,/g, "CM_COMMAREPLACEMENT");
      tempreturn = tempreturn.replace(/\]\s*\,\s*\[/g,"]ConvertArrayEntriesToStringElementsCommaReplacement[");
      tempreturn=tempreturn.replace(/\[\s*([^\[])/g, '["$1');
      tempreturn=tempreturn.replace(/\,/g, '"\.cm_evalmath(),"');
      tempreturn=tempreturn.replace(/\]ConvertArrayEntriesToStringElementsCommaReplacement\[/g,'],[');
      tempreturn=tempreturn.replace(/([^\]])\s*\]/g, '$1"\.cm_evalmath()]');
      tempreturn=tempreturn.replace(/CM_COMMAREPLACEMENT/g, ',');
   };
   return tempreturn;
};

CaluMath.PM.ConvertArrayEntriesToStringElements=function(){
   var tempstring= arguments[0];
   if(tempstring==null || tempstring.match(/\[/)==null){
      return arguments[0];
   }
   else{
      var tempreturn = arguments[0].replace(/\/\,/g, "CM_COMMAREPLACEMENT");
      tempreturn = tempreturn.replace(/\]\s*\,\s*\[/g,"]ConvertArrayEntriesToStringElementsCommaReplacement[");
      tempreturn=tempreturn.replace(/\[\s*([^\[])/g, '["$1');
      tempreturn=tempreturn.replace(/\,/g, '"\.cm_evalmath(),"');
      tempreturn=tempreturn.replace(/\]ConvertArrayEntriesToStringElementsCommaReplacement\[/g,'],[');
      tempreturn=tempreturn.replace(/([^\]])\s*\]/g, '$1"\.cm_evalmath()]');
      tempreturn=tempreturn.replace(/CM_COMMAREPLACEMENT/g, ',');
   };
   return tempreturn;
};


CaluMath.PM.ConvertArrayEntriesToStringElements=function(){
   var tempstring= arguments[0];
   if(tempstring==null || tempstring.match(/\[/)==null){
      return arguments[0];
   }
   else{
      var tempreturn = arguments[0].replace(/\/\,/g, "CM_COMMAREPLACEMENT");
      var index=tempreturn.match(/\[/).index;
    while(index != null && index < tempreturn.length){
alert(["before",index,tempreturn]);
      var matchresult= tempreturn.cm_matchdelimiters(index, "[","]");
      var substring=tempreturn.slice(index+1, matchresult[1]);
alert(["substring",substring]);
      if(substring.match(/\,/) !=null && substring.match(/^\s*\[/) == null){
         tempreturn=tempreturn.slice(0,index)+'["'+substring+'".cm_evalm()'+tempreturn.slice(matchresult[1]);
//alert(tempreturn.slice(0,index+1));
         var tempmatch=tempreturn.slice(index+1).match(/\[/);
         if(tempmatch != null){
            index=index+1+tempmatch.index;
         }
         else{
            index=null;
         };
      }
      else if(substring.match(/\,/) !=null ){
         if(substring.match(/\[/) != null){
            index=index+1+substring.match(/\[/).index;
         }
         else{
            index=null;
         };
      }
      else{
//         var tempmatch=tempreturn.slice(index+1).match(/\[/);
         index=matchresult[1];
      };
//alert(["after",index,tempreturn]);
    };
    tempreturn=tempreturn.replace(/\]\s*\,\s*\[/g, 'CM_COMMAINARRAYREPLACEMENT');
    tempreturn=tempreturn.replace(/\,/g, '".cm_evalmath(),"');
    tempreturn=tempreturn.replace(/CM_COMMAREPLACEMENT/g, ',');
    tempreturn=tempreturn.replace(/CM_COMMAINARRAYREPLACEMENT/g, '],[');
    return tempreturn;
  };
};
*/