//Form validation script
//Jayesh Sheth, for Champion Electronics Online
//isformvalid.js
//JavaScript form validation code
//based on script written by Paul McFedries, and explained in his book.
//http://www.mcfedries.com/
//Version 0.2, October 29, 2002
// New in version 0.2: now the validation works in Opera
// The validate function now returns true or false -
// depending on whether an error occured (false) or not (true)
// Also - the validation uses onClick and so is less intrusive
// onSubmit somehow does not work at all (in IE or Netscape 7)

//begin code


//main validation function
//the validate function is called from an onmouseover event handler present on the Submit button
function validate(current_form) {

    

    var missing_fields = new Array()

    var total_missing = 0

    

    // Loop through all the form elements

    for (var counter = 0; counter < current_form.length; counter++) {



        // Is this a visible text field that's mandatory?

        if ((current_form[counter].type == "text" ||

            current_form[counter].type == "textarea" ||

            current_form[counter].type == "password") && 

            current_form[counter].mandatory) {

            

            

            // Is it whitespace-only?

            if (its_whitespace(current_form[counter].value) ||

                its_empty(current_form[counter].value)) {

              

                // If so, add the field to the array of missing fields

                missing_fields[total_missing] = current_form[counter]

                total_missing++

            }

        }

    }



    // Were there any fields missing?

    if (total_missing > 0) {

    

        // Start the message

        var missing_message = "Sorry, you must fill in the following " +

                              (total_missing == 1 ? " field:" : " fields:") +

                              "\n______________________________\n\n"

        

        // Loop through the missing fields

        for (counter = 0; counter < missing_fields.length; counter++) {
// added October 29, 2002 - using the input name is a little crude
// how about using a label which matches the description 
// on the webpage? - J.S.
// Replacing missing_fields[counter].name with
// missing_fields[counter].desclabel
            missing_message += missing_fields[counter].desclabel + "\n"

        }

    

        // Finish up and display the message

        missing_message += "\n______________________________\n\n" +

                       "Please fill in these fields and then press the Submit button."

        alert(missing_message)
        //new - added October 29, 2002. J.S.
        //make it return false, to prevent Netscape 7 from
        // prematurely submitting the form
         return false
        

        // For emphasis, put the focus on the first missing field

        //missing_fields[0].focus()

    }
    
    else
	//new - added October 29, 2002. J.S.
	// if all required fields are filled out, return true
    {
      return true
    }
    

}



function its_empty(string_value) {



    // Check for the empty string and null

    if (string_value == "" || string_value == null) {

    

        // If either, it's empty so return true

        return true

    }

    

    // Otherwise, it's not empty so return false

    return false

}



function its_whitespace(string_value) {



    // These are the whitespace characters

    var whitespace = " \n\r\t"



    // Run through each character in the string

    for (var counter = 0; counter < string_value.length; counter++) {

        

        // Get the current character

        current_char = string_value.charAt(counter)

        

        // If it's not in the whitespace characters string,

        // return false because we found a non-whitespace character

        if (whitespace.indexOf(current_char) == -1) {

            return false

        }

    }

    

    // Otherwise, the string has nothing but

    // whitespace characters, so return true

    return true

}
