/**
 * This file is part of WebWorx javascript widget library. All rights reserver.
 *
 * Form handler class.
 */
 
if (!wxLib) {
    throw Exception("Please include webworx_lib.js first");
} else if (wxLib.version < 1.0) {
    throw Exception("wxLib version 1.0 required");
}

/**
 * Constructor for form handler instance.
 */
function wxFormHandler() {    
}

/**
 * Validatation reason constant, indicates that input value was correctly validated,
 * that is either no restriction was present or restriction was satisfied.
 */
wxFormHandler.VALUE_OK = 0;
/**
 * Validatation reason constant, field value was empty althought field was marked as
 * required.
 */
wxFormHandler.VALUE_EMPTY = 1;
/**
 * Validation reason constant, field value do not math mask which was passed in
 * required attribute.
 */
wxFormHandler.VALUE_NOT_MATCH = 2;
/**
 * Validation reason constant, field value was shorter that minimum length specified
 * in minLength attribute.
 */
wxFormHandler.VALUE_TOO_SHORT = 3;

/**
 * Attaches handler to passed form element. Attached form will be validated, that
 * is during form submition input values are checked against "required" and "minLength" attributes
 * and a validation handler (onValidate method) is called which should process validation
 * process (eg. display error message). 
 *
 * @param form HTMLFormElement instance that the handler should be attached to
 * @see wxFormHandler.prototype.onValidate
 */
wxFormHandler.prototype.attach = function(form) {        
    // attach submit handler
    wxLib.addEventListener(form, "submit", this);
}

wxFormHandler.prototype.detach = function(form) {        
    // attach submit handler
    wxLib.removeEventListener(form, "submit", null);
}

/**
 * Called to pefrom action on validate passed field. Default implementation
 * displays appropiate message if field value is incorrect and prevents form submition.
 *
 * @param reason one of reason constants
 * @param element instance of element that is being validated
 */
wxFormHandler.prototype.onValidate = function(reason, element) {
    var errorText = "";
    
    switch (reason) {
    case wxFormHandler.VALUE_OK:
    	//element.style.background = "#ffffff";
        return true;        
    case wxFormHandler.VALUE_EMPTY:
        errorText = "is empty";
        break;        
    case wxFormHandler.VALUE_NOT_MATCH:
        errorText = "has invalid format";
        break;        
    case wxFormHandler.VALUE_TOO_SHORT:
        errorText = "is too short";
        break;
    }
    
    var labels = element.form.getElementsByTagName("label");
    var elementLabel = element.id ? element.id : element.name;
    
    for (var j = 0; j < labels.length; j++) {
        if (element.id === labels.item(j).htmlFor) {
            elementLabel = labels.item(j).firstChild.nodeValue;
        }
    }
    
    element.style.background = "#ff3030";
    alert("The field " + elementLabel + " " + errorText);
    element.focus();
    return false;
}

wxFormHandler.prototype.handleEvent = function(event) {    
    var form = event.currentTarget;
    with (form) {        
        for (var i = 0; i < elements.length; i++) {
            var element = elements.item(i);
            var req = element.attributes.getNamedItem("required");
            var minLen = element.attributes.getNamedItem("minLength");
                        
            var reason = wxFormHandler.VALUE_OK;
            
            if (null != req) 
            {
                if (!element.value) 
                {
                    reason = wxFormHandler.VALUE_EMPTY;
                } 
                else 
                if (req.value) 
                {
               		var str = element.value;
                    //if ((new RegExp(req.value).exec(element.value)) != element.value) 
                   	if ((str.match(req.value)) == null)                
                    {
                        reason = wxFormHandler.VALUE_NOT_MATCH;
                    }
                }
            }
            
            if (wxFormHandler.VALUE_OK == reason) {
                if (null != minLen) {
                    if (element.value.length < minLen.value) {
                        reason = wxFormHandler.VALUE_TOO_SHORT;  
                    }
                }
            }
            
            if (!this.onValidate(reason, element)) {
                event.preventDefault();
                break;
            }            
        }
    }    
}
