// <script> // hack for ZDE to highlight JS syntax

/**
 * class formHelper
 *
 * @author Garcia Hurtado
 *
 * This class provides several functions to work with forms. It has only 
 * been tested with IE 6.+ / Mozilla 5.0
 *
 */

//----------------------------------------------------------------

/**
 * constructor
 *
 */    
function formHelper(formName) {
    this.formName = formName;
    this.formElement = document.forms[formName];
    this.unsaved = false;
 
    /**
     * focusFirstEmpty
     *
	 * @desc
	 * Move the cursor to the first empty field in the form
     */
    this.focusFirstEmpty = function() {
		oForm = document.forms[this.formName];
		
		for(i = 0; i < oForm.elements.length; i++){
			// only if the field is empty
			if(oForm.elements[i].value == ''){
				if(oForm.elements[i].focus){
				    if(oForm.elements[i].type != 'hidden'){
				        oForm.elements[i].focus();
				        break;
				    }
				}
			}
		}
    }
    
    /**
     * handleEnter
     *
	 * @desc
	 * Make the enter key tab to the next field in the form instead of
	 * submitting it. This is useful for fields where a Barcode scanner
	 * might be used, which would otherwise submit the form.
	 *
	 * The field itself must explicitly call this function 'onkeypress'
	 * IE: <input name="serial" onkeypress="return(myForm.handleEnter(this, event))">
	 *
	 * @ref
	 * http://www.faqts.com/knowledge_base/view.phtml/aid/6793
     */
    this.handleEnter = function(field, event) {
        var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
                    
        if (keyCode == 13) { // enter key
            var i;
            for (i = 0; i < field.form.elements.length; i++)
              if (field == field.form.elements[i])
                break;
            i = (i + 1) % field.form.elements.length;
            field.form.elements[i].focus();
            return false;
        }
        else {
            return true;
        }
    }
    
    /**
     * setUnsaved
     */
    this.setUnsaved = function() {
		if(window.onbeforeunload){
    		window.onbeforeunload = this.handleExit;    	
		}
    }
    
    /**
     * setSaved
     */
    this.setSaved = function() {
    	if(window.onbeforeunload){
    		window.onbeforeunload = new function() { return false};
    		return true;
    	}
    }
    
    /**
     * handleExit
     *
     * @desc
     * Check for unsaved data in the current form and prompt the user
     * to confirm before they leave the page.
     *
     * Must be called from the 'onunload' event of the body tag
     */
    this.handleExit = function() {
    	message = 'You have unsaved data on this page';
		return message;
    }   
    
    /**
     * clearField
     *
     * @desc
     * Empty a field in the form
     *
     */
    this.clearField = function(fieldName) {
    	tmpForm = document.forms[this.formName];
    	oField = tmpForm.elements[fieldName];
    	oField.value = '';
    	return false;
    }
        
    /**
     * disableField
     *
     * @desc
     * Disable a field in the form
     *
     */
    this.disableField = function(fieldName) {
    	tmpForm = document.forms[this.formName];
    	oField = tmpForm.elements[fieldName];
    	oField.disabled = true;
    	oField.style.backgroundColor = '#CCCCCC';
    	return false;
    }
            
    /**
     * enableField
     *
     * @desc
     * Disable a field in the form
     *
     */
    this.enableField = function(fieldName) {
    	tmpForm = document.forms[this.formName];
    	oField = tmpForm.elements[fieldName];
    	oField.disabled = false;
    	oField.style.backgroundColor = '#FFFFFF';
    	return false;
    }
    
    /**
	 * showPulldownOptions
	 *
	 * @desc
	 * Dynamically display options for a pulldown based on when
	 * another one changes
	 *
	 * @param pulldown string ID of the pulldown element to change
	 * @param optionsArray array Options array to display in pulldown. The 
	 *		  'id' key on each element sets the 'value' in the pulldown and the
	 * 	      'name' sets the name of the option as its displayed
	 * @param firstBlank bool Whether to add a blank option at the beginning of the pulldown
	 * 		 
	 */
    this.buildPulldown = function(pulldown, optionsArray, firstBlank){
		if(firstBlank == null){ // default param value
			firstBlank = true;
		}
		
		element = document.getElementById(pulldown);
		
		// clear all existing options
		for(i = 0; i < element.options.length; i++){
			element.options[i] = null;
		}
		
		if(firstBlank){
			// first option blank
			element.options[0] = new Option('', '');
		}
		
		for(i = 0; i < optionsArray.length; i++){
			id = optionsArray[i]['id'];
			name = optionsArray[i]['name'];
			if(firstBlank){
				element.options[i + 1] = new Option(name, id); // '+1' because we started with a blank one
			}
			else {
				element.options[i] = new Option(name, id); 
			}
		}
	}
}