// Generic chkForm function for required fileds and email checking
//
// 05/23/00 RJS Initial Release
//

//
// this function accepts the form obj and an array of required fields
// the function then processes the fiedls based upon their type
// and creates an array of error messages
//
function chkForm(formObj,required) {
    var checks = 0;
    var errcnt = 0;
    var radname="";
    this.problem = new Array();
    this.display = new Array();

    tmpstring = required.join();
    with (formObj) {
        for (i = 0; i < length; i++) {
            if (tmpstring.indexOf(elements[i].name) == -1) continue;
            if ((elements[i].type == 'text') || (elements[i].type == 'textarea') || (elements[i].type == 'password')) {
                if (elements[i].value == '')  {
                    tmp = "A required field, " + elements[i].name + ", is empty.";
                    checkForm_addMssg (elements[i], tmp, errcnt);
                    errcnt++;
                } else if (elements[i].name =='email')  {
// the old pattern .. not sure where it came from
//                    pattern=/[^@_\.\w\d]|@@|\.\.|__|^@|^\.|^_|@$|\.$|_$|@\.|\.@|@_|_@|\._|_\.|(@)[^@]*\1|([\w_\d]+)\.([\w_\d]+)@/g  

// michaels pattern .. tried'n'true from matts script archive
		      pattern=/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

                    if (email.value.match(pattern) == null){

                        tmp = "Invalid Email Adderss.";
                        checkForm_addMssg (elements[i], tmp, errcnt);
                        errcnt++;
                    }
                }     
            } 
            if (elements[i].type == 'radio') {
                radName =  elements[i].name;
                notChecked = true;
                do {
                    if (elements[i].checked)
                        notChecked = false;
                    i++;
                } while (elements[i].name == radName);
                --i;
                if(notChecked) {
                    tmp = "Radio Button, "+ radName + ", is required.";         
                    checkForm_addMssg (elements[i], tmp, errcnt);
                    errcnt++;
                } 
            }
            if (elements[i].type == 'select-one')  {
//            alert ("select one: Name: " + elements[i].name + "  SelectedIndex: " + elements[i].selectedIndex + "  Value: " + elements[i].options[elements[i].selectedIndex].value);
                if (elements[i].options[elements[i].selectedIndex].value == '-1') {
                    tmp = "Selection, "+ elements[i].name + " is required.";         
                    checkForm_addMssg (elements[i], tmp, errcnt);
                    errcnt++;
                }  
            }    
        }
        if (errcnt > 0)  {
            checkForm_showAlrt();
            return false;
        } else  {
            return true;
        }
    }
}          
                   
//          
// this function adds msgs to the alert so that you get all of the alerts in one box
// rather than going from one to the next.  That way the user can see all of the 
// problems at once rather than getting frustrated going back again and again if there 
// are problems.
//
function checkForm_addMssg (field, msg, arrynum) {
    if ( ! this.problem[arrynum]) { 
        if (field.value || field.focus)  field.focus();
        this.problem[arrynum] = msg;
    }
}

//
// This makes the alert and shows the list of problems.
//
function checkForm_showAlrt() {
    var y=0
    for (var x = 0; x < this.problem.length; x++) {
        this.display[y] = this.problem[x];
        y++;
    }
    alert ("_________________________________\n\n" +
           "             Errors in Form      \n" +
           "_________________________________\n\n" + 
           this.display.join("\n") + 
           "\n\nPlease correct errors and Re-submit.\n");
}
          
