/******************************************************************************
 * OpenTerracotta 0.7 (ANASTACIA) (c) 2004 Eternity Technologies
 * http://terracotta.sourceforge.net/
 * Created by: Devraj Mukherjee (devraj@eternitytechnologies.com)
 *
 * This software is published under the GNU/GPL and is free for commercial and
 * non-commercial use. This software comes with absolutely no warranty at all.
 * Any modifications made by the users of this software must be contributed 
 * back to the OpenTerracotta project.
 *
 * For information about the GNU/GPL please read the the license file included
 * this software bundle or visit http://www.gnu.org/licenses/gpl.txt
 * 
 * Filename:     validate.js
 * Created on:   18th January 2004
 * Author:       Devraj Mukherjee (devraj@eternitytechnologies.com)
 *
 * This file contains source code written in JavaScript and access the form
 * data as defined by the Document Object Model. All functions are written to
 * be run at the client side by a JavaScript 1.x compatible web browser.
 * 
 ******************************************************************************/

 // Show this message on startup if it is not blank
 function ShowStartupMessage(message) {
     if(message != "") alert(message);
 }

 // Show a confirm box with the message and redirect if ok was clicked
 function ConfirmAndRedirect(message, url) {

     if(confirm(message)) window.location = url;
     else return;

 }

 // Changes the status of select box using a parameter
 function ChangeSelectStatus(theSelectBox, status) {

     // go through each option in the select box
     // this is to support multi select boxes
     for(counter = 0; counter < theSelectBox.options.length; counter++) {

         // if the value is more than nothing then change the selection
         if(theSelectBox.options[counter].value != "")
         theSelectBox.options[counter].selected = status;

     } // end for going through each name in the box

 } // end function SelectAll

 // Confirm if there is something selected and the user has indeed agreed to delete stuff
 function ConfirmDelete(message, theSelectBox, failureMessage) {

     // How many selected ones are there
     numberselected = 0;

     // go through each option in the select box
     // this is to support multi select boxes
     for(counter = 0; counter < theSelectBox.options.length; counter++) {

         // if the value is more than nothing then change the selection
         if(theSelectBox.options[counter].value != "" && theSelectBox.options[counter].selected == true)
         numberselected++;

     } // end for going through each name in the box

     // If the number of selected ones is more than zero and the users chooses yes
     userchoice = confirm(message);
     // Cancelled by user
     if(!userchoice) return false;

     // All went well delete the selected stuff
     if(numberselected > 0 && userchoice) return true;

     // Nothing was selected
     alert(failureMessage);
     // other wise cancel the delete
     return false;

 } // end function ConfirmDelete

 // This JavaScript function expects the form as a DOM object, the list of fields
 // that it should validate out of that form and the list of messages associated
 // with each of these fields. The arrays are formed in the files containing the
 // forms

 function ValidateForm(theForm, validateList, messageList) {

     // go through each element of the form for validation
     for(counter=0; counter < theForm.length; counter++) {

         // go through the validation list to check if the field name is listed
         for(listcntr = 0; listcntr < validateList.length; listcntr++) {

             // check to see if the name is present in the validation list
             if(theForm[counter].name == validateList[listcntr]) {
 
                 // check to see if the value is empty and react on that basis
                 if(theForm[counter].value == "") {
                     alert(messageList[listcntr]);
                     return false;
                 } // end if for check value

             } // end if for name test
 
         } // end cntr for validate list

     } // end cntr for form items

     return true;

 } // end function ValidateForm

/******************************************************************************
 * End of file
 ******************************************************************************/
