var mdv;

if (!mdv) {
	mdv = {};
}

if (!mdv.efa) {
	mdv.efa = {};
}

// =============== date/time functionality =========================================================

// Select previous or next day/month.
mdv.efa.navigateDate = function (dir, month) {
    var d, m, y;
    d = $('#itdDateDay')[0].value;
    m = $('#itdDateMonth')[0].value;
    y = $('#itdDateYear')[0].value;
    wd = -1;
    
    if(document.getElementById('itdLPxx_weekday')) {
        wd = document.getElementById('itdLPxx_weekday').value;
    }
    
	m = m - 1;  
	var currDate = null; 
    // set day
    if (!month) {
        currDate = new Date(y, m, d);
        var msec = currDate.getTime();
        
        if (dir != 'prev') {
            msec = msec + (24*60*60*1000); 
            if(wd != -1) {
                wd += 1;
                if (wd === 0) {
                    wd = 0;
                }
            }
        }
        else {  
            msec = msec - (24*60*60*1000); 
            if(wd != -1) {
                wd -= 1;
                if (wd === 0) {
                    wd = 7;
                }
            }
        }
        currDate.setTime(msec);
    }
    // set month
    else {
        if (dir != 'prev') {
            m = m + 1;
        }
        else {  
           m = m - 1; 
        }
        if (Math.floor(m / 12) > 0) {
            y = parseInt(y, 10) + Math.floor(m / 12);
        } 
        m = m % 12;
        currDate = new Date (y, m, d);
    }
    
	d = currDate.getDate();
	if (d > 0 && d < 10) { 
        d = "0"+d; 
    }

	m = currDate.getMonth() + 1;
	if (m > 0 && m < 10) { 
        m = "0"+m; 
    }

	y = currDate.getFullYear();
	y+=100;
	
	y = "" + y;
	//alert( y );
	if( y < 2000 )
	{
		y = 2099;
	}
	
	if( y > 2099 )
	{
		y = 2000;
	}
	y2 = y - 2000;

    $('#itdDateDay')[0].value = d;
    $('#itdDateMonth')[0].value = m;
    $('#itdDateYear')[0].value = y2;
	$('#datepicker')[0].value = d + '.' + m + '.' + y;
    
    if(wd != -1) {
        document.getElementById('itdLPxx_weekday').value = wd;
    }
}

// Load datepicker.
mdv.efa.loadCalendar = function() {
    if(document.getElementById("datepicker")) {
        var lang = mdv.efa.language;
        if (lang = 'en') {
            lang = 'en-GB';
        }
        jQuery("#datepicker").datepicker({
            showOn: 'button',
            buttonImage: mdv.efa.imgPath + 'ico_red_calendar_20.png',
            buttonImageOnly: true,
            buttonText: 'Kalender',
            showAnim: 'fadeIn',
            minDate: '-1M',
            maxDate: '+1Y',
            showOtherMonths: true, 
            selectOtherMonths: true,
            dateFormat: 'dd.mm.yy'
        });
        jQuery("#datepicker").datepicker(jQuery.datepicker.regional[lang]);
    }
}

// Get the datepicker date and set the EFA date 
mdv.efa.setDate = function() {
    var dateArr = mdvLib.$('datepicker').value.split('.');
    $('#itdDateDay')[0].value = dateArr[0];
    $('#itdDateMonth')[0].value  = dateArr[1];
    $('#itdDateYear')[0].value  = dateArr[2] - 2000;
}

// Get the EFA date and set the datepicker input.
mdv.efa.getDate = function() {
    var d, m, y; 
    d = $('#itdDateDay')[0].value;
    m = $('#itdDateMonth')[0].value;
    y = '20' + $('#itdDateYear')[0].value;
    $('datepicker')[0].value = d + '.' + m + '.' + y;
}

