// verify namespace
if (typeof WIREDRIVE != 'object'){
    var WIREDRIVE = function() { 
        return {}; 
    }();
}

WIREDRIVE.forms = function() {
    
    
    var getRequestDemoForm = function(p,my){
    
        var that = {};
        my = my || {};
        
        my.hearaboutselect = document.getElementById('hearabout');
        my.hearaboutspecifyinput = document.getElementById('hearaboutspecify');
        my.hearaboutspecifycontainer = document.getElementById('hearaboutspecifycontainer');
        
        // requires strict form values...
        my.hearaboutactions = {
            Referral : function(){
                my.hearaboutspecifyinput.value = "";
                YAHOO.util.Dom.removeClass(my.hearaboutspecifycontainer, 'hidden'); 
            },
            Other : function(){
                my.hearaboutspecifyinput.value = "";
                YAHOO.util.Dom.removeClass(my.hearaboutspecifycontainer, 'hidden'); 
            },
            ConferenceWorkshop : function(){
                my.hearaboutspecifyinput.value = "";
                YAHOO.util.Dom.removeClass(my.hearaboutspecifycontainer, 'hidden'); 
            }   
        };
        
        my.hearaboutchange = function(e){
            var optionvalue = my.hearaboutselect.value;
            if (typeof my.hearaboutactions[optionvalue] == 'function') {
                my.hearaboutactions[optionvalue]();
            } else {
                my.hearaboutspecifyinput.value = "";
                YAHOO.util.Dom.addClass(my.hearaboutspecifycontainer, 'hidden'); 
            }
        };

        YAHOO.util.Event.on(my.hearaboutselect,"change", my.hearaboutchange,this,true);

        return that;
    };
        

    var getMultiFileInput = function(p,my){
        
        var that = {};
        my = my || {};
        p = p || {};
        
        p.listId = p.listId || 'default_list_target';
        p.max = p.max || 5;
        p.inputId = p.inputId || 'fileInput';        
        my.increment = 0;
        
        
        
        my.fileItem = function(cfg){
            
            cfg = cfg || {};
            cfg.inputBtn = cfg.inputBtn || document.createElement( 'input' );
            cfg.inputBtn.type = 'file';
            cfg.inputBtn.id = cfg.inputBtn.id || 'file' + my.increment;
            cfg.inputBtn.name = cfg.inputBtn.name || 'file' + my.increment;
            
            var item = {
                inputBtn : cfg.inputBtn,
                displayEl : document.createElement( 'div' ),
                hideInput : function(){
                    this.inputBtn.style.position = 'absolute';
                    this.inputBtn.style.left = '-1000px'; 
                }
            };
            
            YAHOO.util.Event.on(item.inputBtn,"change", function(){my.list.add(item);},this,true);

            return item;
        };
        
        
        
        my.list = {
            count : 0,
            items : [],
            listEl : document.getElementById(p.listId),
            add : function(item){
                // ADD A FILE
                my.increment++;
                item.hideInput();
                       
                var removeBtn = document.createElement('input');
                var filename = document.createElement('span');
            	removeBtn.type = 'button';
            	removeBtn.value = 'Remove';
            	removeBtn.className = 'button';
            	//removeBtn.innerHTML = 'Remove';
                filename.innerHTML = item.inputBtn.value;
            	item.displayEl.appendChild(removeBtn);
                item.displayEl.appendChild(filename);
                this.listEl.appendChild(item.displayEl);
                YAHOO.util.Event.on(removeBtn,"click", function(){my.list.remove(item);},this,true);
                
                this.count++;
                this.items[this.increment] = my.fileItem();
                item.inputBtn.parentNode.insertBefore(this.items[this.increment].inputBtn, item.inputBtn);
                if (this.count >= p.max){
                    this.items[this.increment].inputBtn.disabled = true;              
                }
            },
            remove : function(item){
                // REMOVE FILE
                this.count--;
                this.items[this.increment].inputBtn.disabled = false;
                this.listEl.removeChild(item.displayEl);
                item.inputBtn.parentNode.removeChild(item.inputBtn);            
            }
        };
        
        
        my.list.items[0] = my.fileItem({
            inputBtn : document.getElementById(p.inputId)
        }); 

        
        return that;
    };
        
        
       
    return {
        requestDemo : function(p){
            var obj = getRequestDemoForm(p);
            return obj;
        },
        multiFileInput : function(p){
            var obj = getMultiFileInput(p);
            return obj;
        }
    };

}();


