//********************************************************************************
//********************************************************************************
// Version 01.00 - 17/01/2007 - Initial version
//
// This file contains some general web form routines
//********************************************************************************
// NoEnter               : Traps the [Enter] key
// checkTextBox          : Validates a textbox value
// checkTextNumeric      : Validates a numeric textbox value
// checkCombo            : Validates a combobox value
// checkRadio            : Validates a radio buttons
// setDynamicList        : Synchronise dependent parent and child lists
// clearDynamicList      : Zap existing child list <option>'s by setting to null
// fillDynamicList       : Populate child list with new values
//********************************************************************************
//********************************************************************************


//********************************************************************************
//    Author: Steve Betts
//      Date: Jan 2007
//      Desc: NoEnter
//            Traps the [Enter] key to prevent a form from being submitted
//
// Amendment:
//********************************************************************************
function NoEnter() {
  return !(window.event && window.event.keyCode == 13); 
};


//********************************************************************************
//    Author: Steve Betts
//      Date: Jan 2007
//      Desc: checkTextBox
//            Validates a textbox value
//
// Amendment:
//********************************************************************************
function checkTextBox (p_sValue, p_nMin, p_nMax, p_sFieldName) {
 var sError = "";
 var sValue = Trim(p_sValue);

 if (sValue == "") {
    sError = p_sFieldName + " is missing.\n";
 }
 else if (sValue.length < p_nMin) {
   sError = p_sFieldName + " must contain at least " + p_nMin + " characters.\n";
   // sError = p_sFieldName + " must contain between " + p_nMin + " and " + p_nMax + " characters.\n";
   // sError = p_sFieldName + " is insufficient.\n";
 }
 else if (sValue.length > p_nMax) {
   sError = p_sFieldName + " must contain no more than " + p_nMax + " characters.\n";
   // sError = p_sFieldName + " must contain between " + p_nMin + " and " + p_nMax + " characters.\n";
   // sError = p_sFieldName + " is too large.\n";
 }

 return sError;
};       


//********************************************************************************
//    Author: Steve Betts
//      Date: Jan 2007
//      Desc: checkTextNumeric
//            Validates a numeric textbox value
//
// Amendment:
//********************************************************************************
function checkTextNumeric (p_sValue, p_nMin, p_nMax, p_sFieldName, p_nIntOnly) {
 var sError = "";
 var sValue = Trim(p_sValue);
 
 if ( sValue == "" ) {
    sError = p_sFieldName + " is missing.\n";
 }
 else {
    if ( IsNumeric(sValue) ) {
       if ( p_nIntOnly == 1 ) {
          if ( ! IsInteger(sValue) ) {
             sError = p_sFieldName + " must be a whole number.\n";
          };
       };

       if ( ! (Number(sValue) >= Number(p_nMin) && Number(sValue) <= Number(p_nMax)) ) {
          sError += p_sFieldName + " must be between " + p_nMin + " and " + p_nMax + ".\n";
       };
    }
    else {
       sError = p_sFieldName + " is not a number.\n";
    };
 };

 return sError;

};       


//********************************************************************************
//    Author: Steve Betts
//      Date: Jan 2007
//      Desc: checkCombo
//            Validates a combobox value
//
// Amendment:
//********************************************************************************
function checkCombo (p_sOption, p_sNoChoice, p_sFieldName) {
 var sError = "";

 if (p_sOption == p_sNoChoice) {
    sError = "No selection has been made for " + p_sFieldName + ".\n";
 };

 return sError;
};       


//********************************************************************************
//    Author: Steve Betts
//      Date: Jan 2007
//      Desc: checkRadio
//            Validates a radio buttons
//
// Amendment:
//********************************************************************************
function checkRadio (p_oRadio, p_sFieldName) {
 var sError = "";
 var nLoop = -1;

 for (var i=p_oRadio.length-1; i > -1; i--) {
   if (p_oRadio[i].checked) {nLoop = i; i = -1;}
 };

 if (nLoop == -1) {
    sError = "No selection has been made for " + p_sFieldName + ".\n";
 };

 return sError;
};       


//********************************************************************************
//    Author: Steve Betts
//      Date: Apr 2007
//      Desc: setDynamicList
//            Synchronise the values of the dependent parent and child lists
//
//      Args: aDL     - A dynamic list definition array
//                      [1] = Name of the parent list
//                      [2] = Name of the form containing the parent list
//                      [3] = Name of the child list
//                      [4] = Name of the form containing the child list
//                      [5] = An array of child:parent mappings
//            sChild  - Initial child value to be selected
//
// Amendment:
//********************************************************************************
function setDynamicList(aDL, sChild){

 var oList1 = document.forms[aDL[2]].elements[aDL[1]];    // Parent list
 var oList2 = document.forms[aDL[4]].elements[aDL[3]];    // Child list
 var aListItems = aDL[5];                                 // Array of child:parent mappings

 // Remove any existing data from the child list
 // --------------------------------------------
 clearDynamicList(oList2);

 // Select first item in parent if nothing selected
 // -----------------------------------------------
 if (oList1.selectedIndex == -1){
  oList1.selectedIndex = 0;
 };

 // Populate the child list
 // -----------------------
 fillDynamicList(oList2, oList1[oList1.selectedIndex].value, aListItems, sChild);
 return true;
};
 

//********************************************************************************
//    Author: Steve Betts
//      Date: Apr 2007
//      Desc: clearDynamicList
//            Zap existing child list <option>'s by setting to null
//
//      Args: oList - Reference to the list (ie. <select> element) to be cleared
//            
// Amendment:
//********************************************************************************
function clearDynamicList(oList){

 for (var i = oList.options.length; i >= 0; i--){
  oList.options[i] = null;
 };
 
 oList.selectedIndex = -1;
};
 

//********************************************************************************
//    Author: Steve Betts
//      Date: Apr 2007
//      Desc: fillDynamicList
//            Populate child list with new values
//
//      Args: oList   - Reference to the child list to be filled
//            sParent - Value of the item currently selected in the parent list
//            aValues - Array of child:parent value mappings
//            sChild  - Value to be initially selected in the child list
//
// Amendment:
//********************************************************************************
function fillDynamicList(oList, sParent, aValues, sChild){

 var nChildIndex = 0;

 // Add the please make a selection option as the first item
 // --------------------------------------------------------
 oList[oList.length]= new Option("Choose..", "000");

 // Locate any child values linked to the current parent value. The elements
 // of aValues are arranged in batches of three; +0 = the parent value,
 // +1 = the child description and +2 = the child value
 // -------------------------------------------------------------------------
 for (var i = 0; i < aValues.length; i= i + 3){
   if (aValues[i] == sParent){
      oList.options[oList.options.length] = new Option(aValues[i + 1], aValues[i + 2]);

      if (aValues[i + 2] == sChild){
         nChildIndex = oList.options.length - 1;
      };
   };
 };

 // Select item in the child list
 // -----------------------------
 oList.selectedIndex = nChildIndex; 
};


