/**
*   Generic Javascript Form Validation Routine 
*
*   Author:  Cameron Cooke
*   Company: Toucan Resolve Ltd.
*   WebSite: www.toucanresolve.co.uk
*   Email:   cameron@toucanresolve.co.uk
*
*   Last Updated: 21/08/02
    Version: 0.01 (Pre-Alpha)   
*/

function stripControl(ctlName){
//Strip certen tags from the control names
//for example [] and ID will be striped from the name.

  //Define variables
  var aryTags = new Array();
  var aryCount = 0;
  var intIndex;
  var intCtlLength;
  var strFisrtHalf;
  var strSecondHalf;
  var intAryLength;
  var strAryString;
  var strCapitial;
  
  //Build array of strings to be striped
  aryTags[0] = "[]";
  aryTags[1] = "ID";
  
  //Get Array Length
  aryCount = aryTags.length;
  
  //Get Sting Length
  intCtlLength = ctlName.length;
  
  for(i = 0; i < aryCount; i++){
      
      //Get the length of the current array item
      strAryString = aryTags[i]
      intAryLength = strAryString.length;

      intIndex = ctlName.indexOf(aryTags[i]);
      
      if(intIndex !== -1){
          strFirstHalf = ctlName.substr(0, intIndex);
          strSecondHalf = ctlName.substr((intIndex + intAryLength));
            
          //Stick the to halfs together
          //Make the first letter of the string CAP
          strCapitial = strFirstHalf.slice(0,1);
          strCapitial = strCapitial.toUpperCase();
          strFirstHalf = strFirstHalf.substr(1);
          ctlName = strCapitial.concat(strFirstHalf, strSecondHalf);
      }
  }
  
  return ctlName;
}

function validate(form){

  //Define variables and set defaults
  var elmtCount = 0;
  var errString = "";
  var objReqField;
  var aryPrefix = new Array();
  var i;
  var x;

  //Build Prefix Array
  aryPrefix[0] = "txt";
  aryPrefix[1] = "cbo";
  aryPrefix[2] = "sel";
  aryPrefix[3] = "chk";
  aryPrefix[4] = "rdo";

    with(form){
        
        //Get the number of elements on the form
        elmtCount = length;
        
        //Loop through the form
        for(i = 0; i < elmtCount; i++){
            
            //Define variables
            var objElement;
            var ctlName;
            
            //Set the object to the current element
            objElement = elements[i];
            
            //If the element is required then process it
            if(objElement.id == "r"){

                //Build list of Controls to be filled in
                //Check if element name has a type prefix
                for(x = 0; x < aryPrefix.length; x++){

                  if(objElement.name.substring(0,  3) == aryPrefix[x]){

                      //Check to see if data exists
                      switch(objElement.name.substring(0,  3)){
                        case "txt":
                              if(objElement.value == ""){
                                  //Set Required Field
                                  if(!objReqField){ objReqField = objElement; }
                                  //Error String
                                  errString += stripControl(objElement.name.substring(3)) + "\n";
                              }
                              break;
                              
                        case "cbo":
                              if(objElement.selectedIndex == 0){
                                  //Set Required Field
                                  if(!objReqField){ objReqField = objElement; }
                                  //Error String
                                  errString += stripControl(objElement.name.substring(3)) + "\n";
                              }
                              break;
                              
                        case "sel":
                              if(objElement.selectedIndex == 0){
                                  //Set Required Field
                                  if(!objReqField){ objReqField = objElement; }
                                  //Error String
                                  errString += stripControl(objElement.name.substring(3)) + "\n";
                              }
                              break;
                              
                        case "chk":
                              if(objElement.checked == true){
                                  //Set Required Field
                                  if(!objReqField){ objReqField = objElement; }
                                  //Error String
                                  errString += stripControl(objElement.name.substring(3)) + "\n";
                              }
                              break;
                              
                        case "pwd":
                              if(objElement.value == ""){
                                  //Set Required Field
                                  if(!objReqField){ objReqField = objElement; }
                                  //Error String
                                  errString += stripControl(objElement.name.substring(3)) + "\n";
                              }
                              break;
                         
                        default:
                              if(objElement.value == ""){
                                  //Set Required Field
                                  if(!objReqField){ objReqField = objElement; }
                                  //Error String
                                  errString += stripControl(objElement.name.substring(3)) + "\n";
                              }
                              break;
                      }
                  }
                }
            }
             
        }
    } 
    
    if(errString !== ""){
        //Otput alert box to user
        alert("Some of the essential data has been omitted:\n\n" + errString + "\nClick OK to continue");
        
        //Move the control focus to the first required field.
        objReqField.SetFocus;        
        return false;
    }
    else{
        return true;
    }
}