function buildFormInput(form,config) { 
    if( typeof config.type != 'undefined' )
        eval('var clazz = '+config.type);
    else var clazz = Ca_Form_Input;
    return new clazz(form, config);
}

/**
 * Each Ca_Form_Input must provide setActualValue, getActualValue, and getValue
 * on the instance.domElement property
 */
function Ca_Form_Input(form,config) {
    this.__construct = function(form,config) {
        this.form = form;
        this.config = config;

        if( typeof this.config == 'undefined' )
            return;
            
        // create a bi-directional association
        // between the dom element of the input
        // and this class instance
        var sel = $(this.config.inputSelector);

        if( sel.size()==1 ) {
            sel.get(0).Ca_Form_Input = this;
            this.domElement = sel.get(0);
        }
    }
    
    this.__construct(form,config);
}

function Ca_Form_Input_RadioList(form,config) {
    this.__parentConstruct = this.__construct;
    this.__construct = function(form,config)
    {
        this.__parentConstruct(form,config);
        var useSprites = ( typeof(this.config.javascript) != "undefined" && typeof(this.config.javascript.sprite)!="undefined" );
        $(this.domElement).wizard_radio(
        {
    	    spritePath : useSprites ? this.config.javascript.sprite.path : null,
       	    spriteValues : useSprites ? this.config.javascript.sprite.values : null
        });
    }

    this.__construct(form,config);

}
Ca_Form_Input_RadioList.prototype = ( function(form,config) { return new Ca_Form_Input(form,config); } )();

function Ca_Form_Input_Zip(form,config) {
    this.__parentConstruct = this.__construct;
	
    this.__construct = function(form,config)
    {
        this.__parentConstruct(form,config);
        this.domElement.getValue = function() {
			return $('#ca-field-zipcode',this).val();
        }
        this.domElement.getActualValue = function() {
            return this.getValue();
        }
		
		this.domElement.getEvaluatorValue = function() {
			var value = this.getValue();
			if(value == '')return '';
			match = /^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/;
			if(match.test(value))return 'valid';
			else return 'invalid';
        }
		
        this.domElement.setActualValue = function(value) {
			$('#ca-field-zipcode',this).val(value);
        }
		this.notValidValue = function(){
			$('#ca-field-zipcode-error').show();
			if (navigator.appVersion.indexOf('MSIE 6.0') != -1) {
				$('#ca-field-zipcode-error .tt_text img').ifixpng();
			}
		}
    }

    this.__construct(form,config);

}
Ca_Form_Input_Zip.prototype = ( function(form,config) { return new Ca_Form_Input(form,config); } )();

function Ca_Form_Input_Slider(form,config) {
    this.__parentConstruct = this.__construct;
    this.__construct = function(form,config)
    {
        this.__parentConstruct(form,config);
        
        var here = this;
        eval('here.config.javascript.valueMethod = '+here.config.javascript.valueMethod);
        if( typeof here.config.javascript.valueDisplayMethod != 'undefined' )
            eval('here.config.javascript.valueDisplayMethod = '+here.config.javascript.valueDisplayMethod);
        
        this.domElement.handleWrap = function(val)
        {
            if( typeof here.config.javascript.valueDisplayMethod != 'undefined' )
                return here.config.javascript.valueDisplayMethod(val);
            var ret = val;
            if( typeof here.config.javascript.prepend != 'undefined' )
                ret = here.config.javascript.prepend + val;
            if( typeof here.config.javascript.append != 'undefined' )
                ret = ret + here.config.javascript.prepend;
            return ret;
        }
        this.domElement.getValue = function() {
            return here.config.javascript.valueMethod( jQuery('.slider',this).slider('value') );
        }
        this.domElement.getActualValue = function() {
            return jQuery('.slider',this).slider('value');
        }
        this.domElement.setActualValue = function(value) {
            jQuery('.slider',this).slider('value',value);
            $('.amount div',this).html( this.handleWrap(value) );
        }
            
        $('.cust_input',this.domElement).wizard_radio();
        $('.slider',this.domElement).slider(
        {
            animate : true,
            value   : this.config.javascript.defaultValue,
            min     : this.config.javascript.min,
            max     : this.config.javascript.max,
            step    : this.config.javascript.step,
            slide   : function(event, ui) { here.domElement.setActualValue(ui.value); }				
        });
        $('.ui-slider-handle',this.domElement).append('<div class="amount pngimage"><div class="no-underline"></div></div>');
        $('.amount div',this.domElement)
            .html( here.domElement.handleWrap( jQuery('.slider',this.domElement).slider('value') ) );
    }
    this.__construct(form,config);
}
Ca_Form_Input_Slider.prototype = ( function(form,config) { return new Ca_Form_Input(form,config); } )();

