// setup the namespace
if (typeof onepica == "undefined" || !onepica) {
    var onepica = {};
}

onepica.Form = Class.create(VarienForm, {
    initialize: function(formId, firstFieldFocus){
        this.form       = $(formId);
        if (!this.form) {
            return;
        }
        this.cache      = $A();
        this.currLoader = false;
        this.currDataIndex = false;

		// our addition
        this.validator  = new Validation(this.form, {onElementValidate: this.onElementValidate, useAdvice: false});

        this.elementFocus   = this.elementOnFocus.bindAsEventListener(this);
        this.elementBlur    = this.elementOnBlur.bindAsEventListener(this);
        this.childLoader    = this.onChangeChildLoad.bindAsEventListener(this);
        this.highlightClass = 'highlight';
        this.extraChildParams = '';
        this.firstFieldFocus= firstFieldFocus || false;
        //this.bindElements();  // don't do this.  we don't use highlight functionality.
        if(this.firstFieldFocus){
            try{
                Form.Element.focus(Form.findFirstElement(this.form))
            }
            catch(e){}
        }
    },

	/**
	 * Adds a class name to the label adjacent to the form element if validation fails.
	 */
	onElementValidate: function (result, elm) {
		try {
			var labelClass = 'label-validation-failed';
			var label = elm.up(1).select('label[for=' + elm.id + ']');  // go up two in case the label isn't adjacent
			if (label.length) {
				label = label[0];
				if (result) {
					label.removeClassName(labelClass);
				}
				else {
					label.addClassName(labelClass);
				}
			}			
		}
		catch (e) {
			; // ignore
		}			
	},

	/**
	 * Empty, to avoid the functionality in the parent class.
	 */
	bindElements: function () {}
});
