YAHOO.namespace("tmm");

YAHOO.tmm.PayDateCalculator = function(payCycleId, nextPayDateId, nextAfterNextPayDateId, initialNextPayDate, initialNextAfterNextPayDate, currentTimeMillis, passedNetworkId){

    this.payCycle = YAHOO.util.Dom.get(payCycleId);
    this.nextPayDate = YAHOO.util.Dom.get(nextPayDateId);
    this.nextAfterNextPayDate = YAHOO.util.Dom.get(nextAfterNextPayDateId);
    this.initialNextPayDate = initialNextPayDate;
    this.initialNextAfterNextPayDate = initialNextAfterNextPayDate;
    this.today = new Date();
    this.networkId = 1;
    
    if(currentTimeMillis != null) {

       this.today.setTime(currentTimeMillis);
    }
    
    if (passedNetworkId != null) {
      this.networkId = passedNetworkId;
    }  
    
    YAHOO.util.Event.on(this.payCycle, "change", this.handleFieldChange, this, true);
    this.calculateDates();
};

YAHOO.tmm.PayDateCalculator.prototype = {

    handleFieldChange: function(ev){
        YAHOO.util.Event.stopEvent(ev);
        this.calculateDates();
    },
    
    calculateDates: function(){
         
         var thePayCycle = this.payCycle.value;         
         var date = new Date();
         date.setTime(this.today.getTime());
         
         // clear out dropdown
         this.clearSelectBox(this.nextPayDate);
         this.clearSelectBox(this.nextAfterNextPayDate);
              
         if(thePayCycle == 'WEEKLY') {
            this.addDatesToSelect(date, this.nextPayDate, 7, this.initialNextPayDate);
            this.addDatesToSelect(date, this.nextAfterNextPayDate, 7, this.initialNextAfterNextPayDate);
         } else if (thePayCycle == 'TWO_WEEKS' || thePayCycle == 'BI_WEEKLY') {
            this.addDatesToSelect(date, this.nextPayDate, 14, this.initialNextPayDate);
            this.addDatesToSelect(date, this.nextAfterNextPayDate, 14, this.initialNextAfterNextPayDate);
         } else if(thePayCycle == 'LAST_WORKING_DAY_MONTH' || thePayCycle == 'LAST_DAY_MONTH' || thePayCycle == 'LAST_WORKING_DAY'){
         	var datePayCycle = new Date();
         	datePayCycle.setTime(this.today.getTime() + ( 24*60*60*1000*(this.today.getDay()) + 24*60*60*1000*6 )); 
			this.addDatesToSelectEveryDay(datePayCycle, this.nextPayDate, 5, this.initialNextPayDate);
           	datePayCycle.setTime(this.today.getTime() + ( 24*60*60*1000*(this.today.getDay()+29) + 24*60*60*1000*6));
         	this.addDatesToSelectEveryDay(datePayCycle, this.nextAfterNextPayDate, 5,  this.initialNextPayDate);
         }else if (thePayCycle == 'MONTHLY') {
            this.addDatesToSelect(date, this.nextPayDate, 31, this.initialNextPayDate);
            this.addDatesToSelect(date, this.nextAfterNextPayDate, 31, this.initialNextAfterNextPayDate);
         } else if (thePayCycle == 'TWICE_MONTHLY') {
            this.addDatesToSelect(date, this.nextPayDate, 15, this.initialNextPayDate);
            this.addDatesToSelect(date, this.nextAfterNextPayDate, 16, this.initialNextAfterNextPayDate);
         } else if (thePayCycle == 'SPECIFIC_DAY' || thePayCycle != ''){
         	var datePayCycle = new Date();
         	datePayCycle.setTime(this.today.getTime());
           	this.addDatesToSelectEveryDay(datePayCycle, this.nextPayDate, 30, this.initialNextPayDate);
           	datePayCycle.setTime(this.today.getTime() + 34560000);
         	this.addDatesToSelectEveryDay(datePayCycle, this.nextAfterNextPayDate, 120,  this.initialNextPayDate);         
         } 
         else { 
            this.addSelectOption(this.nextPayDate, '  -  ', '');
            this.addSelectOption(this.nextAfterNextPayDate, '  -  ', '');
         }
    }, 
    
    clearSelectBox: function(selectBox){
        for(i=selectBox.options.length; i>=0; i--) {
            selectBox.remove(i);
        }
    },
    
    addSelectOption: function(selectBox, text, value, selected) {
    
        var newOption = document.createElement('option');
        newOption.text = text;
        newOption.value = value;
        newOption.selected = selected;
        try {
            selectBox.add(newOption, null);
        } catch (ex) {
            // IE Bug
            selectBox.add(newOption);
        }
    },
   
    addDatesToSelectEveryDay: function(date, selectBox, noDays, initialValue){
    
    for (i = 0; i < noDays; i++) {           
            
            date.setDate(date.getDate() + 1);
            // filter out the weekends - Sunday is 0, Saturday is 6
            if (date.getDay() != 0 && date.getDay() != 6) {                 
                // add to dropdown
                var dateValue = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
                var dateLabel = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
                if (this.networkId == 2) {
                	var dateLabel = date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
                }	
                this.addSelectOption(selectBox, dateLabel, dateValue, (dateValue==initialValue));
            } // end if not weekend
        } // for
    
    },
    addDatesToSelect: function(date, selectBox, numDays, initialValue){
    
        for (i = 0; i < numDays; i++) {           
            
            date.setDate(date.getDate() + 1);
            // filter out the weekends - Sunday is 0, Saturday is 6
            if (date.getDay() != 0 && date.getDay() != 6) {                 
                // add to dropdown
                var dateValue = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
                var dateLabel = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
                if (this.networkId == 2) {
                	var dateLabel = date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
                }
                this.addSelectOption(selectBox, dateLabel, dateValue,  (dateValue == initialValue));
            } // end if not weekend
        } // for
    }
};