function WamForm_getErrorHeader() {
    var msg = "________________________________________________________________\n\n";
    msg += WamForm_getTranslation( "FormErrorsTitle" ) + "\n";
    msg += "________________________________________________________________";
    return msg;
}

function WamForm_getLabel( id ) {
    var label = WamForm_getTranslation( id );
    if( label.lastIndexOf( ":" ) == label.length - 1 ) {
        label = label.substring( 0, label.length - 1 );
    }
    return label;
}

function WamForm_getTranslation( id ) {
    var e = document.getElementById( "id-tr-" + id );
    if( e ) {
        return e.value;
    }
    switch( id ) {
    case "DateErrors":
        return "The following field(s) require a valid date (entered as YYYY-MM-DD):"
    case "FormErrorsTitle":
        return "The form was not saved because of the following error(s).";
    case "IntErrors":
        return "The following field(s) require an integer value:";
    case "RequiredErrors":
        return "The following field(s) require an entry:";
    case "TimeErrors":
        return "The following field(s) require a valid time (entered as HH:MM):";
    default:
        return "** TRANSLATION NOT FOUND FOR " + id;
    }
}

function WamForm_isDate( s ) {
	if( typeof( s ) != "string" ) {
		s = core_getValue( s );
	}
	if( core_isStringBlank( s ) ) {
        return true;
    }
    return core_isDateValid( s );
}

function WamForm_isInteger( s ) {
	var e = null;
	if( typeof( s ) == "object" && ( s.type == "text" || s.type == "hidden" ) ) {
		e = s;
		s = e.value;
	}

	if( s == null || s == "" )
		return( true );

    var re = new RegExp( "^-?\\d+$" );
    return( re.test( s ) );
}

function WamForm_isPasswordValid( password, userName, firstName, lastName, passwordField ) {

    // Passwords must be at least 6 characters long
    if( password.length < 6 ) {
        return false;
    }

    // Passwords must contain characters from at least 3 of the following 4 classes
    var numberOfClasses = 0;
    var class1 = false;
    var class2 = false;
    var class3 = false;
    var class4 = false;
    for( var i = 0; i < password.length; i++ ) {
        if( password.charAt(i) >= 'A' && password.charAt(i) <= 'Z' && !class1 ) {
            class1 = true;
            numberOfClasses++;
        }
        if( password.charAt(i) >= 'a' && password.charAt(i) <= 'z' && !class2 ) {
            class2 = true;
            numberOfClasses++;
        }
        if( password.charAt(i) >= '0' && password.charAt(i) <= '9' && !class3 ) {
            class3 = true;
            numberOfClasses++;
        }
        if( password.charAt(i) == '.' || password.charAt(i) == ',' || password.charAt(i) == ';'
                || password.charAt(i) == ':' || password.charAt(i) == '*' || password.charAt(i) == '&'
                || password.charAt(i) == '%' || password.charAt(i) == '!' && !class4 ) {
            class4 = true;
            numberOfClasses++;
        }
    }
    if( numberOfClasses < 3 ) {
        return false;
    }

    // Passwords may not contain your user name or any part of your full name
    if( password.toLowerCase().indexOf(userName.toLowerCase()) != -1 ) {
        return false;
    }
    if( password.toLowerCase().indexOf(firstName.toLowerCase()) != -1 ) {
        return false;
    }
    if( password.toLowerCase().indexOf(lastName.toLowerCase()) != -1 ) {
        return false;
    }

    return true;
}

function WamForm_isTime( s ) {
	if( typeof( s ) != "string" ) {
		s = core_getValue( s );
	}
	if( core_isStringBlank( s ) ) {
        return true;
    }
    return core_isTimeValid( s );
}

function WamForm_selectElement( e ) {
	var eSelect = e;

    if( eSelect == undefined )
        return;
    if( eSelect.type == undefined ) {
        // See if it's an array of radio buttons.
        eSelect = eSelect[0];
    }

    if( eSelect.focus ) {
        try {
            eSelect.focus();
        } catch( ex ) {
        }
        return;
    }

    // See if it's in a B-box, and try to open the page and select it again.
    if( window.Bbox_openParents != null ) {
        Bbox_openParents( eSelect );
        Wam_setFocusToControl( eSelect );
    }
}

function WamForm_setValidationInfo( v, fieldName, label, isRequired ) {
    var o = new Object();
    v[ fieldName ] = o;
    o.required = isRequired;
    o.label = label;
    return o;
}

function WamForm_validate( f, additionalErrors, errorFocus ) {

    var required_errors = "";
	var date_errors = "";
	var time_errors = "";
    var integer_errors = "";
	var firstError;
	var valinfo = f.wam_validation;

	for( var i = 0; i < f.length; i++ ) {

        var e = f.elements[i];

		if( e.type == "text" || e.type == "textarea" || e.type == "hidden" ){
			// Strip any leading or trailing white space
			e.value = core_trimString( e.value );
		}

	}

    if( valinfo ) {
        for( elName in valinfo ) {

            var validationInfo = ( (valinfo != null) ? valinfo[ elName ] : null );

            var e = f[ elName ];

            var required = ( validationInfo.required != null && validationInfo.required );

            if( required ) {

                if( core_isEmpty( e ) ) {
                    var s = "\n      " + validationInfo.label;
                    if( required_errors.indexOf( s ) == -1 ) {
                        if( firstError == null ) {
                            firstError = e;
                        }
                        required_errors += s;
                    }
                    continue;
                }
            }

            if( validationInfo.isdate && ! WamForm_isDate( e ) ) {
                if( firstError == null ) {
                    firstError = e;
                }
                date_errors += "\n      " + validationInfo.label;
                continue;
            }

            if( validationInfo.istime && ! WamForm_isTime( e ) ) {
                if( firstError == null ) {
                    firstError = e;
                }
                time_errors += "\n      " + validationInfo.label;
                continue;
            }

            if( validationInfo.isinteger ) {
                if( ! WamForm_isInteger( e ) ) {
                    if( firstError == null ) {
                        firstError = e;
                    }
                    integer_errors += "\n      " + validationInfo.label;
                    continue;
                }
                if( validationInfo.minValue && e.value < validationInfo.minValue ) {
                    if( firstError == null ) {
                        firstError = e;
                    }
                    if( additionalErrors == null ) {
                        additionalErrors = "";
                    }
                    additionalErrors += "\n" + validationInfo.label + " must be >= " + validationInfo.minValue;
                    continue;
                }
                if( validationInfo.maxValue && e.value > validationInfo.maxValue ) {
                    if( firstError == null ) {
                        firstError = e;
                    }
                    if( additionalErrors == null ) {
                        additionalErrors = "";
                    }
                    additionalErrors += "\n" + validationInfo.label + " must be <= " + validationInfo.maxValue;
                    continue;
                }
            }

        }
    }

    if ( required_errors == "" && date_errors == "" && time_errors == "" && integer_errors == "" && additionalErrors == null ) {
        return true;
    }

	var msg = WamForm_getErrorHeader();

	if( required_errors ) {
		msg += "\n\n" + WamForm_getTranslation( "RequiredErrors" ) + "\n" + required_errors;
	}
	if( date_errors ) {
		msg += "\n\n" + WamForm_getTranslation( "DateErrors" ) + "\n" + date_errors;
	}
	if( time_errors ) {
		msg += "\n\n" + WamForm_getTranslation( "TimeErrors" ) + "\n" + time_errors;
	}
	if( integer_errors ) {
        msg += "\n\n" + WamForm_getTranslation( "IntErrors" ) + "\n" + integer_errors;
	}

	if (additionalErrors != null) {
		msg += "\n\n" + additionalErrors;
	}

	alert( msg );

	/*
	 * If we didn't find any errors here, set focus to the field specified by the caller.
	 */
	if( firstError == null ) {
        firstError = errorFocus;
    }

	if( firstError != null ) {
        WamForm_selectElement( firstError );
    }

	return false;
}

/**
 * Function to check/uncheck the Check/Uncheck All checkbox, when any of the select 
 * checkboxes (which the Check/UncheckAll controls) are clicked.
 * @param field - This is the checkbox JS object.
 * @param mastercheckboxid - This is the id for master check box.
 * @return
 */
function WamForm_validateMasterCheckBox(field,mastercheckboxid) {
	
	//get the form name from field object
	var frm = field.form;
	var mastercheckboxfield = document.getElementById(mastercheckboxid);
	
	//function call to check whether all checkboxes are checked.
	var checked = WamForm_allChecked(field.name,mastercheckboxid);
	if(checked == true) {
		mastercheckboxfield.checked = true;
	} else {	   
		mastercheckboxfield.checked = false;
	}

	function WamForm_allChecked(mFieldName,mastercheckboxid) {

	    var allchecked = true;  
	    var lastChar = mastercheckboxid.charAt(mastercheckboxid.length-1);  
	    var divID = "optioncheckboxes" + lastChar;  
	    var divElement = document.getElementById(divID);
	    var el = divElement.getElementsByTagName("input");

	    for(i=0;i<el.length;i++) {  
	     
	        if(el[i].type == "checkbox" && el[i].name==mFieldName) {
	            if( el[i].checked == false) {               
	                allchecked = false
	            }
	        }
	    }   
	    return allchecked;
	}
}


function WamForm_checkAll(field) {	
	
	var fieldID = field.id;
	var lastChar = fieldID.charAt(fieldID.length-1);
	
	var divID = "optioncheckboxes" + lastChar;
	var divElement = document.getElementById(divID);
	
	var inputElements = divElement.getElementsByTagName("input");
    var isMasterCheckBoxChecked = field.checked;	
	for(var i=0;i<inputElements.length;i++) {
		if(inputElements[i].type == "checkbox") {
			if(isMasterCheckBoxChecked == true) {
				inputElements[i].checked = true;
			} else {
				inputElements[i].checked = false;
			}
		}
	}
}
