//************************Script Info**************************************************************
//Version: 1.1
//Date: 07/10/07
//Changes:
//Programmed by:  Charles McKinney

//Global variables
var MyLoadArray = new Array();   //array of functions to run onload
var MySubmitArray = new Array();  //array of functions to run onsubmit
var CheckAllArray = new Array(); //array of functions to run when doing data checks
var WindowsArray = new Array();   //array of open windows
var WindowsNameArray = new Array();   //array of open window names

//alert("cfmc_control is running");

//********************AddOnLoad function **************************************************
//  Purpose:  add functions to the MyLoad array or capture existing window.onload
//            functions, remap them to Myload and add them to the array
//
//  Argument: f - the function to add
//
//  Date:     07/10/06
//  Changes:
//*****************************************************************************************

function AddOnLoad(f) {

   var CheckObject = CheckArray(f,MyLoadArray);  //check to see if function is already in array
   if (CheckObject == true) return false;

   if  (window.onload) {
        if (window.onload != MyOnLoad) {
            MyLoadArray[MyLoadArray.length] = window.onload;
            window.onload = MyOnLoad;
        }
        MyLoadArray[MyLoadArray.length] = f;
   } else {
        window.onload = f;
   }
}

//********************MyOnLoad function **************************************************

//  Purpose:  Run through the Myload array when the window loads
//
//  Date:     07/10/06
//  Changes:
//*****************************************************************************************

function MyOnLoad() {

        var startload = TimeNow();

        //alert("running MyOnLoad");
        for (var i=0;i<MyLoadArray.length;i++) {
           //alert(MyLoadArray[i]());
           MyLoadArray[i]();
        }
        
         var endload = TimeNow();
	 timediff = (endload - startload)/1000;
	 
	 //alert("load time was " + timediff);

  return true;
}


//********************AddOnsubmit function **************************************************

//  Purpose:  add functions to the MySubmit array or captures existing window.onsubmit
//            functions, remap them to MySubmit and add them to the array
//
//  Argument: f - the function to add
//
//  Date:     07/10/06
//  Changes:
//*****************************************************************************************

function AddOnSubmit(f) {

    var CheckObject = CheckArray(f,MySubmitArray); //check to see if function is already in array
    //alert(f + " " + CheckObject);
    if (CheckObject == true) return false;

    //alert(f);
    //alert("this document forms length = " + document.forms.length);

    for (var a = 0; a < document.forms.length;a++) {  //loop through all forms in document
        this_form = document.forms[a];
        //alert("this_form_onsubmit");
        if (this_form.onsubmit) {
           //alert("this_form_onsubmit");
           if (this_form.onsubmit != MyOnSubmit) {
               MySubmitArray[MySubmitArray.length] = this_form.onsubmit;
               MyOnSubmit = this_form.onsubmit;
           }
           MySubmitArray[MySubmitArray.length] = f;
        } else {
           var CheckObject = CheckArray(f,MySubmitArray);
           if (CheckObject == true) continue;
           //alert("iteration " + a + " else " + f);
           MySubmitArray[MySubmitArray.length] = f
           this_form.onsubmit = MyOnSubmit;
        }
    }
   //alert(MySubmitArray.length);
}

//********************MyOnSubmit function **************************************************

//  Purpose:  Run through the MySubmit array when the page is submitted.
//            Return false if the function being run returns false (to stop the submit)
//  Note:     if multiple forms on the page you will need to point them at MySubmit
//
//  Date:     07/10/06
//  Changes:
//*****************************************************************************************

function MyOnSubmit() {

        //alert(MySubmitArray.length);
        for (var i=0;i<MySubmitArray.length;i++) {
           //alert(MySubmitArray[i]);
           var sub_ok = MySubmitArray[i]();
           //alert(sub_ok);
           if (sub_ok == false) return false;
        }
  return true;
}


//********************AddCheckAll function **************************************************

//  Purpose:  add functions to the CheckAllArray for multiple data checks on the same event
//            can specify where to put it in the array by passing a value otherwise places
//            the function passed at the end of the array
//
//  Argument: f - the function to add
//            First - if true add to beginning of array.  if false or undefined add to end
//
//  Date:     07/10/06
//  Changes:
//*****************************************************************************************

function AddCheckAll(f,First) {

   var CheckObject = CheckArray(f,CheckAllArray);
   if (CheckObject == true) return false;

   if (First == undefined || First == false) {
     CheckAllArray[CheckAllArray.length] = f;
   } else {
     CheckAllArray.splice(0,0,f);
   }
}

//********************CheckAll function **************************************************

//  Purpose:  Run through the CheckAll_array. Return false if the function being run returns
//            false (to stop further processing).  Make all data check events (onclick, onblur)
//            run Check_all(this).  You can also pass the specific event (onclick, onmouseup)
//            elem.onmousedown =  function(event) { CheckAll(this,event);} for more complex
//            functions like sliders
//  Note:     Make sure that your data check functions check to see if they need to be run.
//            Make sure you return false to stop or true to continue with the next check
//
//  Arguments: ElemObject - Object to run data checks on
//
//  Date:     09/12/06
//  Changes:
//*****************************************************************************************

function CheckAll(ElemObject) {

        //alert(CheckAllArray.length);
        for (var i=0;i<CheckAllArray.length;i++) {
          //alert(CheckAllArray[i]);
          var check_ok = CheckAllArray[i](ElemObject);
          //alert(check_ok);
          
          if (check_ok == false) {
              ElemObject.haserror = true;
              if (ElemObject.container != undefined) {
                //ElemObject.container.opened = false;
                ElemObject.container.haserror = true;
                ElemObject.container.section.haserror = true;
                //alert(ElemObject.container.id + " " + ElemObject.container.haserror);
                HideReveal(ElemObject.container.handler);
              }  
              return false;
          }
        }
  ElemObject.haserror = false;
  if (ElemObject.container != undefined) {
      ElemObject.container.haserror = false;
      ElemObject.container.section.haserror = false;      
  }    
  return true;
}

//********************FindObjects function ***********************
//
//Purpose:  use for setup functions.
//          finds and returns all of the objects associated with a given label
//          also builds link between radio/checkbox buttons
//Note:     always returns an array if the object is found or false if it isn't
//
//Arguments: ElementID - the id of the object to find
//Date:     06/19/06
//Changes:
//
//****************************************************************

function FindObjects(ElementID,UseName) {

  var CheckByName = false;
  if (UseName == true) CheckByName = true;
  
  var ElementObjectArray = new Array();
  var ButtonArray = new Array();
  var CurrentName;

  //alert(UseName + " " + CheckByName);
  
  if (document.getElementById) {
    if (CheckByName == false) {
      if (document.getElementById("" + ElementID + "") != null) {
        ElementObjectArray[ElementObjectArray.length] = document.getElementById("" + ElementID + "");
      }
    }
    
    if (document.getElementById("" + ElementID + "") == null || CheckByName == true) {
      if (document.getElementsByName("" + ElementID + "").length > 0) {
        ElementObjectArray[ElementObjectArray.length] = document.getElementsByName("" + ElementID + "");
      }
    }
  }

 //if (ElementObjectArray[0].id == undefined) alert("ElementID: " + ElementID + " ElementObjectArrayid: " + ElementObjectArray[0].id + " ElementObjectArray: " + ElementObjectArray[0] + " ElementObjectArray length: " + ElementObjectArray.length);

 if (ElementObjectArray.length > 0) {
      //turn internal array returned by getElementByName into a normal array
      for (var a = 0; a < ElementObjectArray.length;a++) {
       ThisObject = ElementObjectArray[a];

       if (ThisObject.length != undefined && ThisObject.type == undefined) {
          for (var b = 0; b < ThisObject.length; b++) {
               if (ThisObject[b].type == undefined) break;
               ElementObjectArray[ElementObjectArray.length] = ThisObject[b];
          }
          ElementObjectArray.splice(a,1);
       }
      }

      //create buttons array for checkbox/radio inputs - for later checking
      for (var a = 0; a < ElementObjectArray.length;a++) {
       ThisObject = ElementObjectArray[a];

        if (ThisObject.type == "checkbox" || ThisObject.type == "radio") {
         if (CurrentName == undefined || CurrentName != ThisObject.name) {
           var CurrentName = ThisObject.name;
           var ButtonArray = new Array();
         }

         if (CurrentName == ThisObject.name) {
            ButtonArray[ButtonArray.length] = ThisObject;
            ThisObject.buttons = ButtonArray;
         }
        }
     }
   return ElementObjectArray;
 } else {
   return false;
 }
}

//********************CheckArray function ***************************
//
//Purpose:  allows you to check an array to see if an item already exists
//          returns true if found and false if not
//
//Arguments:  ArrayObject - the object to add
//            ArrayName - the array you want to add to
//
//Date:     09/06/06
//Changes:
//
//****************************************************************


function CheckArray(ArrayObject,ArrayName) {

  var ObjectFound;

   for (var a = 0; a < ArrayName.length;a++) {

      ObjectFound = false;
      AddObject = ArrayName[a];

      if (ArrayObject == AddObject) {
          ObjectFound = true;
          return true;
      }
   }

  if (ObjectFound == false) return false;
}



//********************AddArray function ***************************
//
//Purpose:  allows you to append move items from one array to another
//          while checking for duplicate array items.
//          arguments are:  array1 - the array to add
//                          array2 - the array you want to add to
//          returns the new array
//Date:     07/19/06
//Changes:
//
//****************************************************************


function AddArray(Array1,Array2) {

   for (var a = 0; a < Array1.length;a++) {

      ObjectFound = false;
      AddObject = Array1[a];

      for (var b = 0; b < Array2.length;b++) {
       CheckObject = Array2[b];
       if (CheckObject == AddObject) {
          ObjectFound = true;
          break;
       }
     }
     if (ObjectFound == false) Array2[Array2.length] = AddObject;
  }
  return Array2;
}


//********************TimeNow function **************************************************

//  Purpose:  returns the time when called.  Good for timing functions, page loads etc...
//  Date:     07/10/06
//  Changes:

//************************************************************************************************

function TimeNow() {
    D = new Date();
    Time = D.getTime();
    return Time;
}


//********************CheckForValues function ***********************
//
//Purpose:  Checks to see if an applied value is a response for
//          a given checkbox, radio or select type question.
//Date:     02/27/07
//Changes:
//
//****************************************************************


function CheckForCheckValues(ElemObject,CheckValue) {

  if (ElemObject.type == "checkbox" || ElemObject.type == "radio") {

     for (var a = 0; a < ElemObject.buttons.length;a++) {

      ThisButton = ElemObject.buttons[a];
      if (ThisButton.value == CheckValue) return true;
     }
  }

  if (ElemObject.type == "select-one" || ElemObject.type == "select-multiple") {

     for (var a = 0; a < ElemObject.options.length;a++) {

      ThisOption = ElemObject.options[a];

      if (ThisOption.value == CheckValue) return true;
     }
  }
return false;
}

//********************RandomSelect ***********************
//
//Purpose:  Randomly selects and returns a number of items from a string
//
//Argument:  Options - the string to select from
//           Len - the number of values to return
//Date:     06/11/07
//Changes:
//
//****************************************************************


function RandomSelect (Options, Len) {
   var SetLen = Options.length;
   var Selection = "";
   for (var i=0 ; i < Len ; ++i) {
      var k = Math.floor(Math.random() * SetLen);
      Selection += Options.substr(k, 1);
      return Selection;
   }
}


//********************StopTimer **************************************************
//  Purpose: To stop a timed process
//
//  Arguments: TimerName - the name of the timed function to stop
//
//  Date:     06/11/07
//  Changes:
//************************************************************************************************


function StopTimer(TimerName) {
   clearTimeout(TimerName);
}


//Chazz - Commment:  Need to add to readme file

//********************OpenPopup **************************************************
//  Purpose: To open a popup window
//
//  Arguments: PopURL =  URL to open
//             WindowName = Name of window
//             Properties = Window Parameters -
//             Parameter values separated by a ":".
//             All values are required.
//             Values in order are ...
//             StatusBar = yes/no
//             ToolBar = yes/no
//             LocationBar = yes/no
//             MenuBar = yes/no
//             ScrollBars = yes/no
//             Resizable = yes/no
//             WinHeight = vertical size in pixels
//             WinWidth = horizontal size in pixels
//             TopPosition = pixels from top
//             LeftPosition = pixels from left
//
//
//  Date:     07/13/07
//  Changes:
//************************************************************************************************


function OpenPopup(PopURL,WindowName,Properties) {


    if (Properties == undefined) {
       var StatusBar = "no";
       var ToolBar = "no";
       var LocationBar = "no";
       var MenuBar = "no";
       var ScrollBars = "yes";
       var Resizable = "yes";
       var WinHeight = "600";
       var WinWidth = "600"
       var TopPosition = "10"
       var LeftPosition = "10"
    }  else {
       ThisProp = Properties.split(":");
       var StatusBar = ThisProp[0];
       var ToolBar = ThisProp[1];
       var LocationBar = ThisProp[2];
       var MenuBar = ThisProp[3];
       var ScrollBars = ThisProp[4];
       var Resizable = ThisProp[5];
       var WinHeight = ThisProp[6];
       var WinWidth = ThisProp[7];
       var TopPosition = ThisProp[8];
       var LeftPosition = ThisProp[9];
    }

    WindowProperties = "status="+StatusBar;
    WindowProperties += ",toolbar="+ToolBar;
    WindowProperties += ",location="+LocationBar;
    WindowProperties += ",menu="+MenuBar;
    WindowProperties += ",scrollbars="+ScrollBars;
    WindowProperties += ",resizable="+Resizable;
    WindowProperties += ",height="+WinHeight;
    WindowProperties += ",width="+WinWidth;
    WindowProperties += ",top="+TopPosition;
    WindowProperties += ",left="+LeftPosition;


    WindowsArray[WindowsArray.length] = window.open(""+PopURL+"",""+WindowName+"",""+WindowProperties+"");
    WindowsNameArray[WindowsNameArray.length] = WindowName;

    //alert("window = " + WindowsArray.length + " Name = " +  WindowsNameArray.length);
}

//********************CheckWindow **************************************************
//  Purpose: Check to see if a window is open.  Return true is open false if closed
//
//  Arguments: WindowName: Name of window to check
//
//  Date:     07/13/07
//  Changes:
//************************************************************************************************


function CheckWindow(WindowName) {

   for (var a = 0; WindowsArray.length; a++) {
     ThisWindow = WindowsArray[a];
     ThisWindowName = WindowsNameArray[a];

     if (ThisWindowName == WindowName && ThisWindow.closed) {
        //alert("not open " + WindowName + " " + ThisWindowName + " " + ThisWindow.closed);
        return false;
     }

     if (ThisWindowName == WindowName && !ThisWindow.closed) {
        //alert("open " + WindowName + " " + ThisWindowName + " " + ThisWindow.closed);
        return true;
     }
   }
}


//********************ClosePopup **************************************************
//  Purpose: To close a popup window
//
//  Arguments: WindowName = Name of window
//
//  Date:     07/13/07
//  Changes:
//************************************************************************************************


function ClosePopup(WindowName) {

   for (var a = 0; WindowsArray.length; a++) {

     ThisWindow = WindowsArray[a];
     ThisWindowName = WindowsNameArray[a];

     if (ThisWindowName == WindowName && ThisWindow.open) {
        ThisWindow.close();
        WindowsArray.splice(a,1);
        WindowsNameArray.splice(a,1);
        break;
     }
   }
}

//********************CloseAllWindows **************************************************
//  Purpose: To close all popup windows that have been opened
//
//  Arguments: None
//
//  Date:     07/13/07
//  Changes:
//************************************************************************************************


function CloseAllWindows() {

   for (var a = 0; WindowsArray.length; a++) {
     ThisWindow = WindowsArray[a];

     if(ThisWindow.open) ThisWindow.close();

     WindowsArray.splice(a,1);
     WindowsNameArray.splice(a,1);
   }
}


//********************FindProperties **************************************************
//  Purpose: You can use this to determine what properties are set on an object
//
//           You will need to have <div id="properties"></div> somewhere in the page
//
//  Arguments: ElemObjectID = the id of the object to check
//
//  Date:     07/18/07
//  Changes:
//************************************************************************************************

function FindProperties(ElemObjectID) {

  var ThisObject = FindObjects(ElemObjectID);
  ThisObject = ThisObject[0];

  var PropertyText;

  for (var i in ThisObject) {
     if (ThisObject[i] != null) {
        PropertyText += "ID: " + ElemObjectID + " label." + i + " = " + ThisObject[i] + "<br>";
        //alert("ID: " + ElemObjectID + " label." + i + " = " + ThisObject[i]);
     }
  }
  InsertText("properties",PropertyText);
}

