var Rent = {
    step: 1,
    
    language: {},
    
    validate: function() {
        div = $('step-' + this.step);
        
        if(div.hasClassName('step-general')) {
            if(!$F('num-people')) {
                new Effect.Shake('num-people');
                return false;
            }
            
            checked = $$('input.input-setup-type').find(function(node) { return node.checked; });
            if(!checked) {
                new Effect.Shake('seminaries-setup-type');
                
                return false;
            }
            
            
            checked = $$('input.input-breakout-type').find(function(node) { return node.checked; });
            if(!checked) {
                new Effect.Shake('seminaries-breakout-rooms');
                return false;
            }
        }
        
        if(div.hasClassName('step-dates')) {
            var dates = this.getDates();
            
            if(!dates || !dates.length) {
                new Effect.Shake(div);
                return false;
            }
        }
        
        if(div.hasClassName('step-remarks')) {
            if(! $F('name-input')) {
                new Effect.Shake('name-input');
                
                return false;
            }
            
            if(! $F('email-input')) {
                new Effect.Shake('email-input');
                
                return false;
            }
        }
        
        return true;
    } ,
    
    next: function() {
        if(this.validate()) {
            if(this.step == 1){
              $('explenation').style.display = 'none';
            }
            nextStep = this.step + 1;
            
            if($('step-' + nextStep)) {
                this.switchToStep(nextStep);
                window.scrollTo(0,0);
            }
            else {
                $('rentform').submit();
            }
        }
    } ,
    
    previous: function() {
        if(this.step > 1) {
            this.switchToStep(this.step - 1);
        }
    } ,
    
    switchToStep: function(step) {
        this.step = step;
        
        $$('div.step').each(function(div) { div.hide(); });
        $('step-' + step).show();
        
        if(step > 1) {
            $('button-previous').show();
        }
        else {
            $('button-previous').hide();
        }
        
        this.updateRightPanel();
    } ,
    
    updateRightPanel: function() {
        var self = this;
        
        html = "";
        
        var hr = '<div class="hrright"><img src="/images/workfloor/dotted_line_right.gif" width="210" height="11" alt="hr" /></div>';
        
        if(checked = $$('input.input-setup-type').find(function(node) { return node.checked; })) {
            label = $('label-setup-type-' + checked.value);
            
            html += '<ul class="rightcol"><li>' + this.language.you_chose_setup_type + ": " + label.innerHTML + "</li></ul>";
            html += hr;
        }
        
        
        if(checked = $$('input.input-breakout-type').find(function(node) { return node.checked; })) {
            html += '<ul class="rightcol"><li>';
            
            switch(checked.value) {
                case 'yes':
                    html += this.language.you_want_breakout_rooms;
                break;
                case 'no':
                    html += this.language.you_dont_want_breakout_rooms;
                break;
                case 'undecided':
                    html += this.language.you_maybe_want_breakout_rooms;
                break;
            }
            
            html += "</li></ul>";
            html += hr;
        }
        
        if(numPeople = $F('num-people')) {
            html += '<ul class="rightcol"><li>' + this.language.you_expect_num_people.replace(':num_people',numPeople) + "</li></ul>";
            html += hr;
        }
        
        if($('input-catering').checked) {
            html += '<ul class="rightcol"><li>' + this.language.you_want_catering + "</li></ul>";
            html += hr;
        }
        else {
            html += '<ul class="rightcol"><li>' + this.language.you_dont_want_catering + "</li></ul>";
            html += hr;
        }
        
        var dates = this.getDates();
        if(dates && dates.length > 0) {
            html += '<ul class="rightcol"><li>' + this.language.you_selected_dates + ":<br />";
            
            dates.each(function(date) { 
                html += date.start.day + " " + self.language['month_' + date.start.month] + " " + date.start.year + " @ " + date.start.hour + ":" + date.start.minute + "<br />";
                html += self.language.until + "<br />";
                html += date.end.day + " " + self.language['month_' + date.end.month] + " " + date.end.year + " @ " + date.end.hour + ":" + date.end.minute + "<br />";
                html += "<br />";
            });
            
            html += "</li></ul>";
            
            html += hr;
        }
        
        $('right-panel').innerHTML = html;
    } ,
    
    getSelectedPartyTypeId: function() {
        if(node = $$('input.input-party-type').find(function(node) { return node.checked; })) {
            return node.value;
        }
    } ,
    
    getSelectedFoodTypeId: function() {
        if(node = $$('input.input-food-type').find(function(node) { return node.checked; })) {
            return node.value;
        }
    } ,
    
    highlightContainer: function(element) {
        var container = element.parentNode;
        var superContainer = container.parentNode;
        
        $A(superContainer.getElementsByTagName('div')).each(function(node) {
            if(node == container) {
                $(node).addClassName('highlight');
            }
            else {
                $(node).removeClassName('highlight');    
            }
        });
    } ,
    
    addDate: function() {
        index = $$('div.dates-period').length
        
        $('dates').appendChild(
            $div(
                {'class':'dates-period'},
                $p(this.language.start_of_your_event),
                this.getDateSelector('start',index),
                $p(this.language.end_of_your_event),
                this.getDateSelector('end',index),
                $img({src:"/images/workfloor/dotted_line.gif", width: "458", height: "12", alt: "hr"})
            )
        );
    } ,
    
    
    removeDate: function() {
       periods = $A($$('div.dates-period'))
       if(periods.length == 1) {
           return;
       }
       else {
           $('dates').removeChild(periods.last());
       }
    } ,
    
    getDateSelector: function(name, index) {
        daySelect = $select({'class':'sane', name: "form[dates]["+index+"]["+name+"][day]"});
        for(i = 1; i <= 31; i++) {
            daySelect.options[daySelect.options.length] = new Option(i,i);
        }
        daySelect.value = new Date().getDate();
        
        monthSelect = $select({'class':'sane', name: "form[dates]["+index+"]["+name+"][month]"});
        for(i = 1; i <= 12; i++) {
            monthSelect.options[monthSelect.options.length] = new Option(this.language['month_' + i], i);
        }
        monthSelect.value = new Date().getMonth() + 1;
        
        yearSelect = $select({'class':'sane', name: "form[dates]["+index+"]["+name+"][year]"});
        for(i = 0; i <= 10; i++) {
            year = new Date().getFullYear() + i;
            yearSelect.options[yearSelect.options.length] = new Option(year, year);
        }
        yearSelect.value = new Date().getFullYear() ;
        
        hourSelect = $select({'class':'sane', name: "form[dates]["+index+"]["+name+"][hour]"});
        for(i = 0; i <= 23; i++) {
            hourSelect.options[hourSelect.options.length] = new Option(i, i);
        }
        
        minuteSelect = $select({'class':'sane', name: "form[dates]["+index+"]["+name+"][minute]"});
        for(i = 0; i < 60; i += 5) {
            minuteSelect.options[minuteSelect.options.length] = new Option(i, i);
        }
        
        return $div({'class':'date-selector'},
            $div(
                daySelect,
                monthSelect,
                yearSelect,
                $span("@ "),
                hourSelect,
                $span(": "),
                minuteSelect
            )
        )
    } ,
    
    getDates: function() {
        var index = $$('div.dates-period').length - 1
        var dates = []
        
        for(var i = 0; i <= index; i++) {
            var date = {start: {}, end: {}};
            
            ['day','month','year','hour','minute'].each(function(field) {
                date.start[field]   = document.rentform.elements["form[dates]["+i+"][start]["+field+"]"].value
                date.end[field]     = document.rentform.elements["form[dates]["+i+"][end]["+field+"]"].value
            });
            
            //only return as valid date if not left at defaults
            if(! ( date.start.day == "1" && date.start.month == "1" && date.start.year == "" + new Date().getFullYear()) ) {
                dates.push(date);
            }
        }
        
        return dates;
    }
  }
