function setup_datepicker(period_start, period_end, callback) {
    jQuery("#datepick_wrap").find("#ranges").each(function() {
        //removing jquery added attribute as this is causing the dynamically
        // added DOM elem referring old DOM element from it is copied.
        if (jQuery.browser.msie) {
            var jqaddedattr;
            jQuery(this.attributes).each(function() {
                if (this.name.search(/jQuery/) != -1) {
                    jqaddedattr = this;
                }
            });
            if (jqaddedattr) {
                jQuery(this).removeAttr(jqaddedattr.name);
            }
        }

        jQuery(this).datepicker({
            rangeSelect: true, 
            onSelect: callback,
            showAnim: "slideDown",
            duration: "slow",
            showOn: "button", 
            buttonImage: "/images/calendar.gif", 
            buttonImageOnly: true
        });
        jQuery(this).datepicker("setDate", period_start, period_end);
    });
}

function set_datepicker(datepicker_class, period)
{
    /*
     * set_datepicker
     * @param datepicker_class is just the id of the datepicker text box
     * @param period this is what you want the date to do.  options include
     *          day         (changes the date to today)
     *          day++       (takes current last day of current view and changes date to next day)
     *          day--       (takes current first day of current view and changes date to previous day)
     *          week        (changes the date to current week)
     *          week++      (takes week of last day of current view and changes to next week)
     *          week--      (takes week of first day of current view and changes to previous week)
     *          month       (changes the date to the current month)
     *          month++     (takes last day of current view and changes to next month)
     *          month--     (takes first day of current view and changes to previous month)
     */
    var currentdate = new Date();
    var today = new Date();
    if(period == "day")
    {
        $(datepicker_class).datepicker("setDate",today,today);
    }
    if(period == "day--")
    {
        currentdate =  $(datepicker_class).datepicker("getDate");
        today = currentdate[0];
        today.setDate(today.getDate()-1)
        $(datepicker_class).datepicker("setDate",today,today);
    }
    if(period == "day++")
    {
        currentdate =  $(datepicker_class).datepicker("getDate");
        today = currentdate[1];
        today.setDate(today.getDate()+1)
        $(datepicker_class).datepicker("setDate",today,today);
    }
    if(period == "week")
    {
        // Get this week.
        currentdate = new Date();
        this_week = get_week(currentdate);
        sunday = this_week[0];
        saturday = this_week[1];

        $(datepicker_class).datepicker("setDate", sunday, saturday);
        
    }
    if(period == "week++")
    {
        tempdate = new Date();
        
        // Get Current Dates and add 7 Days.
        currentdate = $(datepicker_class).datepicker("getDate");
        currentdate = currentdate[1];
        currentdate.setDate(currentdate.getDate()+7);
        
        // Get newly found Next week.
        week = get_week(currentdate);
        sunday = week[0];
        saturday = week[1];

        $(datepicker_class).datepicker("setDate", sunday, saturday);
        
    }
    if(period == "week--")
    {
        // Get current date and take away 7 days.
        currentdate = $(datepicker_class).datepicker("getDate");
        currentdate = currentdate[0];
        currentdate.setDate(currentdate.getDate()-7);
        
        // Get Newly found previous week.
        week = get_week(currentdate);

        $(datepicker_class).datepicker("setDate", week[0], week[1]);
        
    }
    if(period == "month")
    {
        // Since we are searching current mont this one is easy.
        currentdate = new Date();
        
        // get month
        days = get_month(currentdate);
        $(datepicker_class).datepicker("setDate", days[0], days[1]);
    }
    if(period == "month++")
    {
        // Pull old dates and add a month.
        currentdate = $(datepicker_class).datepicker("getDate");
        currentdate = currentdate[1];
        // I had to use days here for some reason if I did month +1 it skipps Sept
        currentdate.setDate(currentdate.getDate()+15);
        
        // get newly found month
        days = get_month(currentdate)
        
        $(datepicker_class).datepicker("setDate", days[0], days[1]);
    }
    if(period == "month--")
    {
        // pull old dates and go back a month
        currentdate = $(datepicker_class).datepicker("getDate");
        currentdate = currentdate[0];
        // Changed this to days for the reason above.
        currentdate.setDate(currentdate.getDate()-15);
        // get newly found month.
        days = get_month(currentdate)
        
        $(datepicker_class).datepicker("setDate", days[0], days[1]);
    }
}

function get_month(currentdate)
{
    // New Date Objects.
    first_day = new Date();
    last_day = new Date();
    
    // Copy Date of currentdate to first_day set day = 1
    first_day.setDate(1);
    first_day.setMonth(currentdate.getMonth());
    first_day.setYear(currentdate.getFullYear());
    
    // Copy Date of currentdate to last_day set day = 1
    last_day.setDate(1);
    last_day.setMonth(currentdate.getMonth());
    last_day.setYear(currentdate.getFullYear());
    
    // Calculate Last Day of the month
    while (first_day.getMonth() == last_day.getMonth()) {
        last_day.setDate(last_day.getDate() + 1);
    }
    // We have looped one too many times. This sets it back to
    // the last day of the month.
    last_day.setDate(last_day.getDate() - 1);
    
    return new Array(first_day,last_day);
}
function get_week(currentdate)
{
    var today = new Date();
    var sunday = new Date();
    var saturday = new Date();
    today.setDate(currentdate.getDate());
    today.setMonth(currentdate.getMonth());
    today.setYear(currentdate.getFullYear());
    
    var dow = today.getDay();
    var min = dow;
    var max = 6 - dow;
    
    sunday.setMonth(today.getMonth());
    sunday.setYear(today.getFullYear());
    sunday.setDate(today.getDate()-min);
    
    
    saturday.setMonth(today.getMonth());
    saturday.setYear(today.getFullYear());
    saturday.setDate(today.getDate()+max);
    
    
    return new Array(sunday, saturday);
}

