/*
 * DateFieldHelper.js
 *
 * Used to prefill empty form fields
 * with a format string, then remove that string
 * upon form submission.
 *
 * formName: The name of the form to process
 * formatString: The text to display in each form field
 *
 * Should create a new instance of this helper, then as part
 * of the document.onload event, provide the form fields
 * to be "helped" in the setFormFields() method.
 */
function DateFieldHelper( formName, formatString) {
    var logger = LogFactory.getLog("DateFieldHelper.js");

    var self = this;
    var curForm;
    var fields;

    this.setFormFields = function() {
        logger.debug("Enter init()");

        // First set up the event handler for on submit
        curForm = document.forms[formName];
        if( curForm.onsubmit ) {
            alert("There is already an onsubmit event handler on this form.\n" +
                  "Please call the submitHandler function from your " +
                  " onsubmit handler!");
        }

        curForm.onsubmit = this.submitHandler;

        fields = new Array( arguments.length );

        for( var i=0; i<arguments.length; i++ ) {
            fields[i] = curForm.elements[ arguments[i] ];
            if( ! fields[i].value ) {
                fields[i].value = formatString;
            }
            if( ! fields[i].onclick ) {
                fields[i].onclick = function(e) {
                    var elem = getEventSource(e);
                    elem.select();
                };
            }
        }

        logger.debug("Exit init()");
    }

    /*
     * This removes the format string from any fields that still contain it.
     */
    this.submitHandler = function() {
        logger.debug("Enter submitHandler()");

        for( var i=0; i<fields.length; i++ ) {
            if( fields[i].value == formatString ) {
                fields[i].value = "";
            }
        }

        logger.debug("Exit submitHandler()");
    }

}
