// javascript for dbform processing functions
//
//    Copyright (C) 2003 Tony Blair
//
//    This program 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; 
//
//    This program 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, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//
// @author       Tony Blair <tonanbarbarian@hotmail.com>
// @copyright    (C) Tony Blair 2003
// @package      dbForm
// @version      0.9.2

var dbFormRequesttype="select";

// autoSelect
// function that automatically selects the record in the search
function autoSelect(theObject) {
   // find the form name
   var theForm = theObject.form;
   
   // set autoSelect value
   if (theForm.dbform_autoselect) {
      theForm.dbform_autoselect.value = theObject.options[theObject.selectedIndex].value;
   }
   // determine if we should call validation before going on
   if (theForm.getAttribute("alwaysUpdate")) {
      // alwaysUpdate is set so call validation
      dbFormRequesttype="select";
      if (!validateDBForm(theForm)) return false;
   }
   // submit form
   theForm.submit();
} // autoSelect()


// autoFilter
// function that processes a filter request
function autoFilter(theObject) {
   // find the form name
   var theForm = theObject.form;
   
   // set the appropriate dbform filter value
   theForm.elements['dbform_filters['+theObject.name+']'].value = theObject.options[theObject.selectedIndex].value;
   // submit form
   dbFormRequesttype="filter";
   theForm.dbform_filter_requesttype.value="filter";
   theForm.submit();
} // autofilter()

// imageSubmit
// function that is called on an image submit to set the correct requesttype
function imageSubmit(theObject) {
   // find the form name
   theForm = theObject.form;
   
   // set the imagerequesttype
   if (theForm.dbform_imagerequesttype) {
      theForm.dbform_imagerequesttype.value = theObject.value;
      dbFormRequesttype = theObject.value;
   }
} // imageSubmit()

// not null validation function
function validateDBForm(formName) {
   // find all the elements of the form and check their not null validation
   // and in the case of textarea check maxlength as well
   
   // check to see if there should be some confirmation
   if (!findConfirm(dbFormRequesttype.toLowerCase())) return false;
 
   // check to see if we dont need validation
   switch (dbFormRequesttype.toLowerCase()) {
      case "select":
      case "new":
       if (!formName.getAttribute("alwaysUpdate")) return true;
       break;
      case "delete":
      case "cancel":
      case "filter":
         return true;
         break;
   }
   
   // initialise
   var msg = "";
   var fieldName;
   
   // look at all of the fields
   for(i=0;i < formName.elements.length;i++) {
      // determine the fieldname to display if there is an error
      if (formName.elements[i].getAttribute("title")) {
         fieldName = formName.elements[i].getAttribute("title");
      } else {
         fieldName = formName.elements[i].getAttribute("name");
      }
      // check for textarea
      if (formName.elements[i].type.toLowerCase()=="textarea") {
         // check length
         if (formName.elements[i].getAttribute("maxlength")) {
            if (formName.elements[i].value.length > formName.elements[i].getAttribute("maxlength")) 
               msg += "   " + fieldName + " is too long. Max length is " + formName.elements[i].getAttribute("maxlength") + " chars\n";
         }
      }
      
      // check for not null
      if (formName.elements[i].getAttribute("not_null")) {
         switch (formName.elements[i].type.toLowerCase()) {
            case "text":
            case "textarea":
            case "password":
            case "file":
               if (formName.elements[i].value=="") {
                  msg += "   " + fieldName + " cannot be empty.\n";
               }
               break;
            case "select-one":
               var selectedValue = formName.elements[i].options[formName.elements[i].selectedIndex].value;
               if ((selectedValue=="dbform_null") || (selectedValue=="-1") || (selectedValue=="")) {
                  msg += "   " + fieldName + " must be selected.\n";
               }
               break;
            case "select-multiple":
               var numSelected = 0;
               // loop thru the options and see how many are selected
               for (var loop=0;loop<formName.elements[i].length;loop++) {
                  if (formName.elements[i].options[loop].selected) {
                     var selectedValue = formName.elements[i].options[loop].value;
                     if ((selectedValue!="null") && (selectedValue!="-1") && (selectedValue!="")) numSelected++;
                  }
               }
               if (numSelected==0) {
                  msg += "   " + fieldName + " must be selected.\n";
               }
               break;
         }
      }
   }
   
   // see if the validation failed and display
   if (msg!="") {
      msg = "The following errors occured:\n" + msg;
      alert(msg);
      return false;
   } else {
      return true;
   }
} // validateDBForm()
 
function findConfirm(fieldValue) {
  var buttons = document.getElementsByTagName("input");
  for (i=0;i<buttons.length;i++) {
    if ((buttons[i].nodeName=='INPUT') && 
      ((buttons[i].type.toLowerCase()=='submit') || (buttons[i].type.toLowerCase()=='button') || (buttons[i].type.toLowerCase()=='image'))
      && (buttons[i].getAttribute('confirm')) && (buttons[i].value.toLowerCase()==fieldValue)) {
        return confirm(buttons[i].getAttribute('confirm'));
    }
  }
  return true;
}