//Constructor
function calendar(id, d) {
    this.id = id;
    this.dateObject = d;
    //this.pix = p;
    this.write = writeCalendar;
    this.length = getLength;
    this.month = d.getMonth();
    this.date = d.getDate();
    this.day = d.getDay();
    this.year = d.getFullYear();
    this.getFormattedDate = getFormattedDate;
    //get the first day of the month's day
    d.setDate(1);
    this.firstDay = d.getDay();
    //then reset the date object to the correct date
    d.setDate(this.date);
    
}

if($.cookie('lang') == 'E'){   
   var days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
   var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

}else{
   var days = new Array('Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag');
   var months = new Array('Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember');

}

function getFormattedDate() {
    //	return days[this.day] + ', ' + months[this.month] + ' ' + this.date + ', ' + this.year;
    var formateDay = this.month;
    if (this.month < 10) {
        formateDay = "0" + this.month + 1;
    } else {
        formateDay = this.month + 1;
    }
    
    
    return this.month + 1 + '/' + this.date + '/' + this.year;

}

function writeCalendar() {

    var calString = '<div id="calContainer">';
    //write month and year at top of table
    calString += '<table id="cal' + this.id + '" cellspacing="0" width="150" height="150" style="border:1px #ffffff solid; background:white">';

    //write the month
    //calString += '<tr class="select"><td ><select id="daysselect"></select></td><td colspan="6" id="zu" onclick=""><select id="yearsselect"></select></td></tr>';
    calString += '<tr><th colspan="6" class="month">' + months[this.month] + ', ' + this.year + ' </th><th class="zu" id="zu" onclick="zu()">X</th></tr>';
    //write a row containing days of the week
    calString += '<tr>';

    for (i = 0; i < days.length; i++) {
        calString += '<th class="dayHeader">' + days[i].substring(0, 2) + '</th>';
    }

    //write the body of the calendar
    calString += '<tr>';
    //create 6 rows so that the calendar doesn't resize
    for (j = 0; j < 42; j++) {
        var displayNum = (j - this.firstDay + 1);
        if (j < this.firstDay) {
            //write the leading empty cells
            calString += '<td class="empty">&nbsp;</td>';
        } else if (displayNum == this.date) {
            calString += '<td id="' + this.id + 'selected" class="date" onClick="javascript:changeDate(this,\'' + this.id +'\')">' + displayNum + '</td>';
        } else if (displayNum > this.length()) {
            //Empty cells at bottom of calendar
            calString += '<td>&nbsp;</td>';
        } else {
            //the rest of the numbered cells
            calString += '<td id="" class="days" onClick="javascript:changeDate(this,\'' + this.id + '\')">' + displayNum + '</td>';
        }
        if (j % 7 == 6) {
            calString += '</tr><tr>';
        }
    }
    //close the last number row
    calString += '</tr>';
    //write the nav row
    calString += '<tr>';
    calString += '<td class="nav" style="text-decoration:underline;" onClick="changeMonth(-12,\'' + this.id + '\')">&lt;</td>';
    calString += '<td class="nav" onClick="changeMonth(-1,\'' + this.id + '\')">&lt;</td>';
    calString += '<td class="month" colspan="3">&nbsp;</td>';
    calString += '<td class="nav" onClick="changeMonth(1,\'' + this.id + '\')">&gt;</td>';
    calString += '<td class="nav" style="text-decoration:underline;text-align:right;" onClick="changeMonth(12,\'' + this.id + '\')">&gt;</td>';
    calString += '</tr>';

    calString += '</table>';
    calString += '</div>';
    return calString;
}

function getLength() {
    //thirty days has September...
    switch (this.month) {
        case 1:
            if ((this.dateObject.getFullYear() % 4 == 0 && this.dateObject.getFullYear() % 100 != 0) || this.dateObject.getFullYear() % 400 == 0)
                return 29; //leap year
            else
                return 28;
        case 3:
            return 30;
        case 5:
            return 30;
        case 8:
            return 30;
        case 10:
            return 30
        default:
            return 31;
    }
}
function changeDate(td, cal) {
    //Some JavaScript trickery
    //Change the cal argument to the existing calendar object
    //This is why the first argument in the constructor must match the variable name
    //The cal reference also allows for multiple calendars on a page
    
    cal = eval(cal);
    document.getElementById(cal.id + "selected").className = "days";
    document.getElementById(cal.id + "selected").id = "";
    td.className = "date";

    td.id = cal.id + "selected";
    //set the calendar object to the new date
    cal.dateObject.setDate(td.firstChild.nodeValue);
   
    
    cal = new calendar(cal.id, cal.dateObject);
    //here is where you could react to a date change - I'll just display the formatted date
    
    m = cal.getFormattedDate();
    
    ar = m.split("/");
    if($.cookie('lang') == 'E'){
        if(ar[0]<10){
            $("#dayfrom").val("0"+ar[0]);
            
        }else{
            $("#dayfrom").val(ar[0]);
        }
         if(ar[1]<10){
            $("#monthfrom").val("0"+ar[1]);
            
        }else{
            $("#monthfrom").val(ar[1]);
        }
        
        $("#yearfrom").val(ar[2]);
    }
    else
    {
        if(ar[1]<10){
            $("#dayfrom").val("0"+ar[1]);
            
        }else{
            $("#dayfrom").val(ar[1]);
        }
         if(ar[0]<10){
            $("#monthfrom").val("0"+ar[0]);
            
        }else{
            $("#monthfrom").val(ar[0]);
        }
        $("#yearfrom").val(ar[2]);
    }
    
    $("#calContainer").hide();

}

function changeMonth(mo, cal) {
    //more trickery!
    cal = eval(cal);
    //The Date object is smart enough to know that it should roll over in December
    //when going forward and in January when going back
    cal.dateObject.setMonth(cal.dateObject.getMonth() + mo);
    cal = new calendar(cal.id, cal.dateObject);
    cal.formattedDate = cal.getFormattedDate();
    document.getElementById('calContainer').innerHTML = cal.write();
    m = cal.getFormattedDate();
    ar = m.split("/");
    if($.cookie('lang') == 'E'){
        if(ar[0]<10){
            $("#dayfrom").val("0"+ar[0]);
            
        }else{
            $("#dayfrom").val(ar[0]);
        }
         if(ar[1]<10){
            $("#monthfrom").val("0"+ar[1]);
            
        }else{
            $("#monthfrom").val(ar[1]);
        }
        
        $("#yearfrom").val(ar[2]);
    }
    else
    {
        if(ar[1]<10){
            $("#dayfrom").val("0"+ar[1]);
            
        }else{
            $("#dayfrom").val(ar[1]);
        }
         if(ar[0]<10){
            $("#monthfrom").val("0"+ar[0]);
            
        }else{
            $("#monthfrom").val(ar[0]);
        }
        $("#yearfrom").val(ar[2]);
    }

}
function zu() {
    $("#calContainer").hide();
}

//################################### Second Calendar ##############################################

//Constructor
function calendarSecond(id, d) {
    this.id = id;
    this.dateObject = d;
    //this.pix = p;
    this.write = writeCalendarSecond;
    this.length = getLength;
    this.month = d.getMonth();
    this.date = d.getDate();
    this.day = d.getDay();
    this.year = d.getFullYear();
    this.getFormattedDateSecond = getFormattedDateSecond;
    //get the first day of the month's day
    d.setDate(1);
    this.firstDay = d.getDay();
    //then reset the date object to the correct date
    d.setDate(this.date);
}
function writeCalendarSecond() {

    var calString = '<div id="calContainer">';
    //write month and year at top of table
    calString += '<table id="cal' + this.id + '" cellspacing="0" width="150" height="150" style="border:1px #ffffff solid; background:white">';

    //write the month
    //calString += '<tr class="select"><td ><select id="daysselect"></select></td><td colspan="6" id="zu" onclick=""><select id="yearsselect"></select></td></tr>';
    calString += '<tr><th colspan="6" class="month">' + months[this.month] + ', ' + this.year + ' </th><th class="zu" id="zu" onclick="zu()">X</th></tr>';
    //write a row containing days of the week
    calString += '<tr>';

    for (i = 0; i < days.length; i++) {
        calString += '<th class="dayHeader">' + days[i].substring(0, 2) + '</th>';
    }

    //write the body of the calendar
    calString += '<tr>';
    //create 6 rows so that the calendar doesn't resize
    for (j = 0; j < 42; j++) {
        var displayNum = (j - this.firstDay + 1);
        if (j < this.firstDay) {
            //write the leading empty cells
            calString += '<td class="empty">&nbsp;</td>';
        } else if (displayNum == this.date) {
            calString += '<td id="' + this.id + 'selected" class="date" onClick="javascript:changeDateSecond(this,\'' + this.id +'\')">' + displayNum + '</td>';
        } else if (displayNum > this.length()) {
            //Empty cells at bottom of calendar
            calString += '<td>&nbsp;</td>';
        } else {
            //the rest of the numbered cells
            calString += '<td id="" class="days" onClick="javascript:changeDateSecond(this,\'' + this.id + '\')">' + displayNum + '</td>';
        }
        if (j % 7 == 6) {
            calString += '</tr><tr>';
        }
    }
    //close the last number row
    calString += '</tr>';
    //write the nav row
    calString += '<tr>';
    calString += '<td class="nav" style="text-decoration:underline;" onClick="changeMonthSecond(-12,\'' + this.id + '\')">&lt;</td>';
    calString += '<td class="nav" onClick="changeMonthSecond(-1,\'' + this.id + '\')">&lt;</td>';
    calString += '<td class="month" colspan="3">&nbsp;</td>';
    calString += '<td class="nav" onClick="changeMonthSecond(1,\'' + this.id + '\')">&gt;</td>';
    calString += '<td class="nav" style="text-decoration:underline;text-align:right;" onClick="changeMonthSecond(12,\'' + this.id + '\')">&gt;</td>';
    calString += '</tr>';

    calString += '</table>';
    calString += '</div>';
    return calString;
}
function getFormattedDateSecond() {
    //	return days[this.day] + ', ' + months[this.month] + ' ' + this.date + ', ' + this.year;
    var formateDay = this.month;
    if (this.month < 10) {
        formateDay = "0" + this.month + 1;
    } else {
        formateDay = this.month + 1;
    }
    
    
    return this.month + 1 + '/' + this.date + '/' + this.year;

}
function changeDateSecond(td, cal) {
    //Some JavaScript trickery
    //Change the cal argument to the existing calendar object
    //This is why the first argument in the constructor must match the variable name
    //The cal reference also allows for multiple calendars on a page
    
    cal = eval(cal);
    document.getElementById(cal.id + "selected").className = "days";
    document.getElementById(cal.id + "selected").id = "";
    td.className = "date";

    td.id = cal.id + "selected";
    //set the calendar object to the new date
    cal.dateObject.setDate(td.firstChild.nodeValue);
   
    
    cal = new calendarSecond(cal.id, cal.dateObject);
    //here is where you could react to a date change - I'll just display the formatted date
    
    m = cal.getFormattedDateSecond();
    
    ar = m.split("/");
    
    if($.cookie('lang') == 'E'){
        if(ar[0]<10){
            $("#dayto").val("0"+ar[0]);
            
        }else{
            $("#dayto").val(ar[0]);
        }
         if(ar[1]<10){
            $("#monthto").val("0"+ar[1]);
            
        }else{
            $("#monthto").val(ar[1]);
        }
        
        $("#yearto").val(ar[2]);
    }
    else
    {
        if(ar[1]<10){
            $("#dayto").val("0"+ar[1]);
            
        }else{
            $("#dayto").val(ar[1]);
        }
         if(ar[0]<10){
            $("#monthto").val("0"+ar[0]);
            
        }else{
            $("#monthto").val(ar[0]);
        }
        $("#yearto").val(ar[2]);
    }
    
    $("#calContainer").hide();

}

function changeMonthSecond(mo, cal) {
    //more trickery!
    cal = eval(cal);
    //The Date object is smart enough to know that it should roll over in December
    //when going forward and in January when going back
    cal.dateObject.setMonth(cal.dateObject.getMonth() + mo);
    cal = new calendarSecond(cal.id, cal.dateObject);
    cal.formattedDate = cal.getFormattedDateSecond();
    document.getElementById('calContainer').innerHTML = cal.write();
    m = cal.getFormattedDateSecond();
    ar = m.split("/");
     if($.cookie('lang') == 'E'){
        if(ar[0]<10){
            $("#dayto").val("0"+ar[0]);
            
        }else{
            $("#dayto").val(ar[0]);
        }
         if(ar[1]<10){
            $("#monthto").val("0"+ar[1]);
            
        }else{
            $("#monthto").val(ar[1]);
        }
        
        $("#yearto").val(ar[2]);
    }
    else
    {
        if(ar[1]<10){
            $("#dayto").val("0"+ar[1]);
            
        }else{
            $("#dayto").val(ar[1]);
        }
         if(ar[0]<10){
            $("#monthto").val("0"+ar[0]);
            
        }else{
            $("#monthto").val(ar[0]);
        }
        $("#yearto").val(ar[2]);
    }

}
