var dow = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
$(document).ready(function() {
    setCal();
});

function monthLength(month, year) { var dd = new Date(year, month, 0); return dd.getDate(); }
function setCell(f, day, col) {
   
    var c = [];
    var t = '<li';
    if (f == 0) t += '>&nbsp;<\/li>';
    if (f == 9) t += '>&nbsp;<\/li>';
    if (f == 1) t += '><a href="#" id="' + day + '" rel="' + dow[col] + '">' + day + '<\/a><\/li>';
    
    return t;
}
var theDate;
theDate = new Date(); // get the date
function setCal() {
    var m = parseInt(theDate.getMonth(), 10); // set month
    var y = parseInt(2010, 10); //set year
    if (y < 1901 || y > 2100) { alert('year must be after 1900 and before 2101'); return false; }
    var c = new Date();
    c.setDate(1);
    c.setMonth(m);
    c.setFullYear(y);
    var x = parseInt(1, 10); //set start day valButton
    var s = (c.getDay() - x) % 7; if (s < 0) s += 7;
    var dm = monthLength(m, y);

    var h = '<ul>';
    
    //creates the previous dates
    for (var i = s; i > 0; i--) {
        h += setCell(0, dm - i + 1, (s - i + x) % 7);
    }

    //creates the current dates
    dm = monthLength(m + 1, y);
    for (var i = 1; i <= dm; i++) {
        if ((s % 7) == 0) { h += ''; s = 0; }
        h += setCell(1, i, (s + x) % 7);
        s++;
    }

    //creates the future dates
    var j = 1;
    for (var i = s; i < 7; i++) {
        h += setCell(9, j, (i + x) % 7);
        j++;
    }
    h += '<\/ul>';
  
    $('#days').html('');
    $('#days').html(h);
}

