$(function() {

            jQuery('#next_pay_date').datepicker({
            numberOfMonths: 1,
            showButtonPanel: false,
            minDate: 'd',
            maxDate: '+31d',
	     beforeShowDay: noWeekendsOrHolidays,
            hideIfNoPrevNext: true,
            onSelect: function(dateText, inst) {
            if ((jQuery("#pay_period").val() == 'weekly' || jQuery("#pay_period").val() == 'bi-weekly' || jQuery("#pay_period").val() == 'twice_monthly'  || jQuery("#pay_period").val() == 'monthly') && jQuery("#next_pay_date").val().length==10){
                var datestr = jQuery("#next_pay_date").val();
                var months = ~~datestr.substr(0,2);
                var day = ~~datestr.substr(3,2);
                var year = ~~datestr.substr(6,4);
                months = months-1;
                var date = new Date(year,months,day);
                var setdate = false;
                if (jQuery("#pay_period").val() == 'twice_monthly'){
                    var dayCount = 32 - new Date(year, months, 32).getDate();
                    if (day!=1 && day!=5 && day!=15 && day!=20 && day!=30 && day!=dayCount){
                        var checkingdate = new Date(year,months,day);
                        var true_frequency = false;
                        checkingdate.setDate(checkingdate.getDate()-1);
                        _validdate = [1,5,20,30,dayCount];
                        if (checkingdate.getDay() == 0 || checkingdate.getDay() == 6 || jQuery.inArray(checkingdate.toDateString(),_hollidays)>=0){
                            if (jQuery.inArray(checkingdate.getDate(),_validdate)>=0){
                                true_frequency = true;    
                            }else{
                                checkingdate.setDate(checkingdate.getDate()-1);
                                if (checkingdate.getDay() == 0 || checkingdate.getDay() == 6 || jQuery.inArray(checkingdate.toDateString(),_hollidays)>=0){
                                    if (jQuery.inArray(checkingdate.getDate(),_validdate)>=0){
                                        true_frequency = true;    
                                    }else{
                                        checkingdate.setDate(checkingdate.getDate()-1);
                                        if (checkingdate.getDay() == 0 || checkingdate.getDay() == 6 || jQuery.inArray(checkingdate.toDateString(),_hollidays)>=0){
                                            if (jQuery.inArray(checkingdate.getDate(),_validdate)>=0){
                                                true_frequency = true;    
                                            }
                                        }    
                                    }
                                }   
                            }
                        }
                        
                        if (!true_frequency){
                            alert("You have selected Twice A Month as your pay frequency.  Please confirm you are paid on specific dates of the month for example 1st and 15th , 15th and 30th, 15th and last business day, 5th and 20th");
                        }else{                    
                            if (day<=2){
                                date = new Date(year,months,15);    
                            }else if (day<=8 && day>=3){
                                date = new Date(year,months,20);    
                            }else if (day<=17 && day>=12){
                                if (dayCount<30){
                                    date = new Date(year,months,dayCount);     
                                }else{
                                    date = new Date(year,months,30);    
                                }    
                            }else if (day<=23 && day>=18){
                                date = new Date(year,months+1,5);    
                            }else if (day>=27){
                                date = new Date(year,months+1,15);    
                            }else{
                                date.setDate(date.getDate()+14);    
                            }
                            setdate = true;
                        }                        
                    }else{
                        if (day==1 || day==5){
                            date.setDate(date.getDate()+14);    
                        }else if(day == 15){
                            if (dayCount<30){
                                date = new Date(year,months,dayCount);     
                            }else{
                                date.setDate(date.getDate()+14);    
                            }
                        }else if (day==20){
                            date = new Date(year,months+1,5);    
                        }else if (day==30 || day==dayCount){
                            date = new Date(year,months+1,15);    
                        }
                        setdate = true;    
                    }
                    
                    date = checkdate(date,1);
                }else if (jQuery("#pay_period").val() == 'weekly'){
                    date.setDate(date.getDate()+7);
                    date = checkdate(date,0);
                    setdate = true;
                }else if (jQuery("#pay_period").val() == 'bi-weekly'){
                    date.setDate(date.getDate()+14);
                    date = checkdate(date,1);
                    setdate = true;
                }else if (jQuery("#pay_period").val() == 'monthly'){
                    var dayCount = 32 - new Date(year, months, 32).getDate();
                    var nexdayCount = 32 - new Date(year, months+1, 32).getDate();
                    if (nexdayCount<dayCount){
                        dayCount = nexdayCount;    
                    }
                    date.setDate(date.getDate()+dayCount);    
                    date = checkdate(date,1);
                    setdate = true;
                }
                
                if (setdate){
                    months = date.getMonth();
                    months = months+1;
                    if (months<10){
                        months = '0'+months;
                    }

                    day = date.getDate();
                    if (day<10){
                        day = '0'+day;
                    }
                    jQuery('#second_pay_date').attr('value', months+'/'+day+'/'+date.getFullYear());
                }
            }
            }
        });

        jQuery('#second_pay_date').datepicker({
            numberOfMonths: 1,
            showButtonPanel: false,
            minDate: '+1d',
            maxDate: '+61d',
	     beforeShowDay: noWeekendsOrHolidays,
            hideIfNoPrevNext: true,
            onSelect: function(dateText, inst) {
            var date_select = new Date(dateText);
            jQuery('#second_pay_date').datepicker('option', 'maxDate', new Date(date_select.setDate(date_select.getDate()-1)));
            }
        });
        
        jQuery("#pay_period").bind('change',function(event){
            var pay1set = false;
            if ((jQuery(this).val() == 'twice_monthly' || jQuery(this).val() == 'weekly' || jQuery(this).val() == 'bi-weekly' || jQuery(this).val() == 'monthly') && jQuery("#next_pay_date").val().length==10){
                jQuery('#next_pay_date').attr('value', '');
                jQuery('#second_pay_date').attr('value', '');
            }
         });



	});




    
        





var _hollidays = [
        new Date(2011,10,11).toDateString(), // (2011-11-11) Veterans Day
        new Date(2011,10,24).toDateString(), // (2011-11-24) Thanksgiving Day
        new Date(2011,11,26).toDateString(), // (2011-12-26) Christmas Day
        new Date(2012, 0, 2).toDateString(), // (2012-01-02) New Year's Day
        new Date(2012, 0,16).toDateString(), // (2012-01-16) Birthday of Martin Luther King, Jr.
        new Date(2012, 1,20).toDateString(), // (2012-02-20) Washington's Birthday
        new Date(2012, 2,28).toDateString(), // (2012-03-28) Memorial Day
        new Date(2012, 6, 4).toDateString(), // (2012-07-04) Independence Day
        new Date(2012, 8, 3).toDateString(), // (2012-09-03) Labor Day
        new Date(2012, 9, 8).toDateString(), // (2012-10-08) Columbus Day
        new Date(2012,10,12).toDateString(), // (2012-11-12) Veterans Day
        new Date(2012,10,22).toDateString(), // (2012-11-22) Thanksgiving Day
        new Date(2012,11,25).toDateString()  // (2012-12-25) Christmas Day
    ];
    
    function checkdate(date,direction){
        

        if (jQuery.inArray(date.toDateString(),_hollidays)>=0){
            if (direction){
                date.setDate(date.getDate()+1);    
            }else{
                date.setDate(date.getDate()-1);    
            }    
        }
        var curday =  date.getDay();
        if (curday==0){//voskresenia
            if (direction){
                date.setDate(date.getDate()+1);    
            }else{
                date.setDate(date.getDate()-2);    
            }
            
        }else if (curday==6){//subbota
            if (direction){
                date.setDate(date.getDate()+2);    
            }else{
                date.setDate(date.getDate()-1);    
            }
        }
        
        if (jQuery.inArray(date.toDateString(),_hollidays)>=0){
            if (direction){
                date.setDate(date.getDate()+1);    
            }else{
                date.setDate(date.getDate()-1);    
            }    
        }
        return date;    
    }





        new Date(2012, 1,20).toDateString(), // (2012-02-20) Washington's Birthday
        new Date(2012, 2,28).toDateString(), // (2012-03-28) Memorial Day
        new Date(2012, 6, 4).toDateString(), // (2012-07-04) Independence Day
        new Date(2012, 8, 3).toDateString(), // (2012-09-03) Labor Day
        new Date(2012, 9, 8).toDateString(), // (2012-10-08) Columbus Day
        new Date(2012,10,12).toDateString(), // (2012-11-12) Veterans Day
        new Date(2012,10,22).toDateString(), // (2012-11-22) Thanksgiving Day
        new Date(2012,11,25).toDateString()  // (2012-12-25) Christmas Day


	natDays = [
    [11, 11, 2011],
    [11, 24, 2011],
    [12, 26, 2011],
    [01, 02, 2012],
    [01, 16, 2011],
    [01, 02, 2012],
    [01, 16, 2012],
    [02, 20, 2012],
    [03, 28, 2012],
    [07, 04, 2012],
    [09, 03, 2012],
    [10, 08, 2012],
    [11, 12, 2012],
    [11, 22, 2012],
    [12, 25, 2012]

  ];
  function nationalDays(date) {
    for (i = 0; i < natDays.length; i++) {
      if (date.getMonth() == natDays[i][0] - 1
        && date.getDate() == natDays[i][1]
        && date.getFullYear() == natDays[i][2]) {
          return [false, 'a'];
      }
    }
    return [true, ''];
  }
	function noWeekendsOrHolidays(date) {
    var noWeekend = $.datepicker.noWeekends(date);
    if (noWeekend[0]) {
      return nationalDays(date);
    } else {
      return noWeekend;
    }
  }

