Emv.VideodeFormvalidation = {

    validateUri: '/tools/validate-value',
    Dom        : YAHOO.util.Dom,
    //Event      : YAHOO.util.Event,
    Connect    : YAHOO.util.Connect, 

    /**
     * Validate and display error message overlay (on error).
     * 
     * Example:
     * <code>
     *   var options = {
     *     'submitForm': this.Dom.get('newsletter-teaser1-form'),
     *     'validate': [
     *       {
     *         'element'  : 'email-adresse-input-id',
     *         'label'    : 'E-Mail-Adresse',
     *         'validator': 'email',
     *         'value'    : this.Dom.get('email-adresse-input-id').value
     *       },
     *       {
     *         'element'  : 'mobil-input-id',
     *         'label'    : 'Handy-Nr.',
     *         'validator': 'phone',
     *         'value'    : this.Dom.get('mobil-input-id').value
     *       }
     *     ]
     *   };
     * 
     *   Emv.VideodeFormvalidation.ValidateSubmit(options));
     * </code>
     * @param object options
     * @return boolean
     */
	validateSubmit: function(options) {

        if (typeof(options.validate) == "undefined" || options.validate.length <= 0 ||
            typeof(options.submitForm) == "undefined") {

            return true;
        }

        var submitForm = options.submitForm;
        var callbackValidate = {
            success: function(o) {

                // Process the response
                var response = {};
                try {
                    var response = YAHOO.lang.JSON.parse(o.responseText);

                    if (!response.validation.valid) {

                        // >> ToDo open overlay with error message
                        for (i=0; i<response.validation.fields.length; i++) {

                            if (!response.validation.fields[i].valid) {

                                if (typeof(response.validation.fields[i].errormsg) != "undefined" && response.validation.fields[i].errormsg != "") {
                                
                                    if (typeof(Emv.VideodeJsoverlay) != "undefined") {
                                        
                                        Emv.VideodeJsoverlay.get(options.submitForm).show("Fehler", response.validation.fields[i].errormsg);
                                    } else {
                                        
                                        alert(response.validation.fields[i].errormsg);
                                    }
                                } else {
                                    
                                    if (typeof(Emv.VideodeJsoverlay) != "undefined") {
                                        
                                        Emv.VideodeJsoverlay.get(options.submitForm).show("Fehler", response.validation.fields[i].label + " " + response.validation.fields[i].value + " ist nicht gültig.");
                                    } else {
                                        
                                        alert(response.validation.fields[i].label + " " + response.validation.fields[i].value + " ist nicht gültig.");
                                    }
                                }
                            }
                        }
                    } else {
                        
                        submitForm.submit();
                    }
                } catch (e) {

                }
            },
            failure: function() {

                // asyncRequest failure
                // >> ToDo open overlay with error message "Fomrular konnte nicht geprüft werden".
                return false;
            },
            scope: this
        };

		// Validate the field(s).
        this.Connect.asyncRequest('POST', this.validateUri, callbackValidate, "options=" + YAHOO.lang.JSON.stringify(options.validate));
	}
}
