var currentDomain = 'capitalone.ca';
if(window.location.hostname.match(/eggs/))currentDomain = 'capitalone.com';

function buildForm(config)
{
    if( typeof config.type != 'undefined' )
        eval('var clazz = '+config.type);
    else var clazz = Ca_Form;
    var ret = new clazz(config);
    return ret;
}

function Ca_Form(config) {

    this.__construct = function(config) {
        this.config = config;
        this.processor = {};
        this.inputs = {};
        this.inputsArray = [];
        
        // setup the inputValues so
        // we can share an object containing values
        // amongst all this stuff
        this.inputValues = {};
        for( inputName in config.inputs ) {
            this.inputValues[inputName]='';
        }
            
        this.buildInputs();
        this.buildProcessor();
    }
    
    this.buildProcessor = function() {
        var pConf = this.config.processor;
        this.processor = buildFormProcessor(this,pConf);
    }
    
    this.buildInputs = function() {
        for( var input in this.config.inputs ) {
            var inConf = this.config.inputs[input];
            this.inputs[input] = buildFormInput(this,inConf);
            this.inputs[input].name=input;
            this.inputsArray.push( this.inputs[input] );
        }
    }
    
    this.reset = function() {
        for( input in this.inputValues )
            this.inputValues[input]='';
        this.processor.reset();
    }
    
    this.resetState = function() {
        $.cookie(this.config.stateCookie,'',{path:'/',domain:currentDomain});
        $.cookie(this.config.stateCookie+'_formvals','',{path:'/',domain:currentDomain});
        var idx = 0;
        for( var input in this.config.inputs ) {
            var defaultValue = this.config.inputs[input]['javascript']['defaultValue'];
            var value = (typeof(defaultValue) != "undefined" ? defaultValue : '' );
            this.inputsArray[idx].domElement.setActualValue(value);
	    idx++;
        }



    }
    
    this.storeState = function() {
        var form = this;
        var c = [];
        var d = [];
        for(i = 0; i < form.processor.previousLogic.length - 1; i++){
			var v = form.processor.previousLogic[i].input;
            var val = form.inputs[v].domElement.getActualValue();
            var val2 = form.inputs[v].domElement.getValue();

                
            // if the value is undefined, 
            // then the Ca_Form_Input hasn't set it yet
            // and we'll store it as an empty string
            // so that process() will correctly interpret it
            if( typeof val == 'undefined' ) 
            {
                val = '';
                val2 = '';
            }
                
            c.push( v+'='+val );
            d.push( v+'='+val2 );
        }
        c = c.join(':');
        d = d.join(':');
        $.cookie(this.config.stateCookie,c,{path:'/',domain:currentDomain});
        $.cookie(this.config.stateCookie+'_formvals',d,{path:'/',domain:currentDomain});
    }
    
    this.restoreState = function() {
        var form = this;
        var stateCookie = $.cookie(this.config.stateCookie);
		if(stateCookie == null || stateCookie == '')return;
		else stateCookie = stateCookie.split(':');
		
		var processor = form.processor;
		var passback = {type:''};
		var backupProcessCallback = processor.processCallback;
		
		var stateObject = {};
		var lastQuestion = processor.currentInput;
		
		for(index in stateCookie){
			var values = stateCookie[index].split('=');
			stateObject[values[0]] = {cookieValue:values[1],field:form.inputs[values[0]]};
			lastQuestion = values[0];
		}
		
		for(inputName in form.inputValues){
			if(typeof stateObject[inputName] == 'undefined')continue;
			var state = stateObject[inputName];
			state.field.domElement.setActualValue(state.cookieValue);
			form.inputValues[inputName]=state.field.domElement.getValue();
		}

		while(true){
			var currentInput = processor.currentInput;
			var inputValue = form.inputValues[currentInput];
			if(inputValue == '' && typeof form.inputs[currentInput].config.canSkipTo != 'undefined')
				passback = form.processor.deadProcess(form.inputs[currentInput].config.canSkipTo);
			else if(inputValue == '')break;
			else passback = form.processor.process();
				
			if(typeof passback == 'undefined' || passback.type == 'result' || currentInput == lastQuestion || passback == false)
                break;
		}
		
		processor.processCallback = backupProcessCallback;
		
		// make sure all values after the current question is set to the default value.
		// Sometimes going from gateway to product match, questions 8 and 9 will be set
		// but FAC or Product Match can't use questions 8 and 9 until questions 4-7 are
		// answered.
		var currentInput = processor.currentInput;
		var startResetting = false;
		for(input in form.inputs){
			if(startResetting){
				var formInput = form.inputs[input];
				var defaultValue = formInput.config.javascript.defaultValue;
				var value = (typeof(defaultValue) != "undefined" ? defaultValue : '' );
				formInput.domElement.setActualValue(value);
			}
			else if(input == currentInput){
				var startResetting = true;
				continue;
			}
		}
		
		this.storeState();
		
		return passback;
    }
    
    var here = this;
    
    this.__construct(config);
}
