var gwCal = false;
// JavaScript Document
function gwHandleSelect(type,args,obj) {
    var dates = args[0];
    var date = dates[0];
    var year = date[0], month = date[1], day = date[2];

    var objIDArray = obj.id.split( '-' );

    var id = objIDArray[0] + '-' + objIDArray[1] + '-' + objIDArray[3];

    var textField = document.getElementById( id );
    textField.value = day + '/' + month + '/' + year;
    if ( gwCal )
        gwCal.hide();
}

function gwDestroyDatePicker()
{
    if ( gwCal )
    {
        gwCal.destroy();
        gwCal = false;
    }
}

function gwShowDateSalesObjectPicker( id, containerID )
{
    var gwCalContainerID = containerID;
    var gwCalContainer = document.getElementById( containerID );

    var currentDate = new Date();
    var pageDate = document.getElementById('input-hidden-month').value + '/' + document.getElementById('input-hidden-year').value;

    gwCalContainer.style.display = 'block';

    var inTxt = YAHOO.util.Dom.get( "in" ),
        outTxt = YAHOO.util.Dom.get( "out"),
        language = YAHOO.util.Dom.get( "calendar-content-language" ),
        inDate, outDate, interval, languageVal;

    inTxt.value = "";
    outTxt.value = "";
    languageVal = language.value;

    var cal = new YAHOO.example.calendar.IntervalCalendar( containerID, { pages: 2,
                                                                          pagedate: pageDate,
                                                                          start_weekday: 1,
                                                                          close: true} );

    if ( languageVal == 'nor-NO' )
    {
        cal.cfg.setProperty( "MONTHS_SHORT",   ["Jan", "Feb", "Mar", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Des"] );
        cal.cfg.setProperty( "MONTHS_LONG",    ["Januar", "Februar", "Mars", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Desember"] );
        cal.cfg.setProperty( "WEEKDAYS_1CHAR", ["S", "M", "D", "M", "D", "F", "S"] );
        cal.cfg.setProperty( "WEEKDAYS_SHORT", ["S\u00F8", "Ma", "Ti", "On", "To", "Fr", "L\u00F8"] );
        cal.cfg.setProperty( "WEEKDAYS_MEDIUM",["S\u00F8n", "Man", "Tir", "Ons", "Tor", "Fre", "L\u00F8r"] );
        cal.cfg.setProperty( "WEEKDAYS_LONG",  ["S\u00F8ndag", "Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag", "L\u00F8rdag"] );
    }

    cal.selectEvent.subscribe(function() {
        interval = this.getInterval();

        if (interval.length == 2) {
            inDate = interval[0];
            inTxt.value = (inDate.getMonth() + 1) + "/" + inDate.getDate() + "/" + inDate.getFullYear();

            if (interval[0].getTime() != interval[1].getTime()) {
                outDate = interval[1];
                outTxt.value = (outDate.getMonth() + 1) + "/" + outDate.getDate() + "/" + outDate.getFullYear();
            } else {
                outTxt.value = "";
            }

            if ( inTxt.value != '' && outTxt.value != '' )
            {
                var fromValue = inDate.getDate() + '-' + (inDate.getMonth() + 1) + "-" + inDate.getFullYear();
                var toValue = outDate.getDate() + "-" + (outDate.getMonth() + 1) + "-" + outDate.getFullYear();
                gwSetAjaxEventMonth( inDate.getMonth() + 1, inDate.getFullYear() );
                gwSetAjaxEventPeriod( fromValue, toValue );
                gwAjaxUpdateForm();
            }
        }
    }, cal, true);

    cal.render();
}

function gwShowDatePicker( id, containerID )
{
    var gwCalContainerID = containerID;
    var gwCalContainer = document.getElementById( gwCalContainerID );
    var currentDate = new Date();

    var textField = document.getElementById( id );
    var valueArray = textField.value.split( '/' );
    var selectedDate = currentDate.getMonth() + 1;
    var pageDate = currentDate.getMonth() + 1;

    if ( valueArray[1] )
    {
        selectedDate = valueArray[1];
    }

    if ( valueArray[0] )
    {
        selectedDate += '/' + valueArray[0];
    }
    else
    {
        selectedDate += '/' + currentDate.getDate();
    }

    if ( valueArray[1] )
    {
        pageDate = valueArray[1];
    }

    if ( valueArray[2] )
    {
        pageDate += '/' + valueArray[2];
        selectedDate += '/' + valueArray[2];
    }
    else
    {
        pageDate += '/' + currentDate.getFullYear();
        selectedDate += '/' + currentDate.getFullYear();
    }
    gwDestroyDatePicker();
    gwCalContainer.style.display = 'block';

    gwCal = new YAHOO.widget.Calendar( containerID , gwCalContainerID, {
        start_weekday: 1,
        pagedate: pageDate,
        selected: selectedDate,
        monthFormat: YAHOO.widget.Calendar.SHORT,
        LOCALE_WEEKDAYS: "medium",
        initialFocus: "year"
	} );

    gwCal.render();
    gwCal.selectEvent.subscribe( gwHandleSelect, gwCal, true );

    document.getElementById( gwCalContainerID ).onmouseover = function() { document.onmousedown = null; }
    document.getElementById( gwCalContainerID ).onmouseout = function() { document.onmousedown = gwDestroyDatePicker; }
    document.onmousedown = gwDestroyDatePicker;
}

