/* inputArray is an associative array with the properties
year
month
day
hour
minute
calendarRef - Reference to the DHTMLSuite.calendar object.
*/
var calendarObjForForm = new DHTMLSuite.calendar({minuteDropDownInterval:10,numberOfRowsInHourDropDown:5,callbackFunctionOnDayClick:'getDateFromCalendar',isDragable:true,displayTimeBar:false}); 

var userAgent = navigator.userAgent.toLowerCase();  
var isSafari = /webkit/.test(userAgent);

function pickDate(inputObject)
{
    if(isSafari){
        alert('Please enter date as d/m/y (e.g. 31/08/2008)');
        return;
    }
    calendarObjForForm.setCalendarPositionByHTMLElement(inputObject,0,inputObject.offsetHeight+2);  // Position the calendar right below the form input
    calendarObjForForm.setInitialDateFromInput(inputObject,'dd/mm/yyyy');   // Specify that the calendar should set it's initial date from the value of the input field.
    calendarObjForForm.addHtmlElementReference('myDate',inputObject);   // Adding a reference to this element so that I can pick it up in the getDateFromCalendar below(myInput is a unique key)
    if(calendarObjForForm.isVisible()){
        calendarObjForForm.hide();
    }else{
        calendarObjForForm.resetViewDisplayedMonth();   // This line resets the view back to the inital display, i.e. it displays the inital month and not the month it displayed the last time it was open.
        calendarObjForForm.display();
    }       
}   
function getDateFromCalendar(inputArray)
{
    var references = calendarObjForForm.getHtmlElementReferences(); // Get back reference to form field.
    references.myDate.value = inputArray.day + '/' + inputArray.month + '/' + inputArray.year;
    calendarObjForForm.hide();     
    if (document.getElementById("myFilter") != null && document.forms.filter.myFilter){
        document.forms.filter.myFilter.disabled=false;
    }
}
/* Interview popup */
var calendarObjForFormTime;
var currentEl;

var offsetHour = 0;
var offsetMinute = 0;

function getDateFromCalendarTime(inputArray){
    var references = calendarObjForFormTime.getHtmlElementReferences(); // Get back reference to form field.
    references.myDate.value = inputArray.day + '/' + inputArray.month + '/' + inputArray.year + ' ' + inputArray.hour + ':' + inputArray.minute;
    calendarObjForFormTime.displayedMonth = 1;
    if (document.getElementById("myFilter") != null && document.forms.filter.myFilter){
        document.forms.filter.myFilter.disabled=false;
    }
}
function myClose(inputArray){
    if(currentEl == 'starttime'){
        current_h = Math.round(inputArray.hour) + Math.round(offsetHour);
        if(current_h >= 0 && current_h < 10){
            current_h = '0' + current_h;
        }
        current_m = Math.round(inputArray.minute) + Math.round(offsetMinute);
        if(current_m >= 0 && current_m < 10){
            current_m = '0' + current_m;
        }
        document.forms.filter.endtime.value =  inputArray.day + '/' + inputArray.month + '/' + inputArray.year + ' ' + current_h + ':' + current_m;
    }
}
function pickDateTime(inputObject){
    if(isSafari){
        alert('Please enter date as d/m/y (e.g. 31/08/2008)');
        return;
    }
    if(calendarObjForFormTime){
        calendarObjForFormTime.hide();    
    }
    calendarObjForFormTime = new DHTMLSuite.calendar({minuteDropDownInterval:10,numberOfRowsInHourDropDown:5,callbackFunctionOnDayClick:'getDateFromCalendarTime',isDragable:true,displayTimeBar:true}); 
    calendarObjForFormTime.setCallbackFunctionOnHourChange('getDateFromCalendarTime');
    calendarObjForFormTime.setCallbackFunctionOnMinuteChange('getDateFromCalendarTime');
    calendarObjForFormTime.setCallbackFunctionOnClose('myClose');
    currentEl = inputObject.name;
    calendarObjForFormTime.setCalendarPositionByHTMLElement(inputObject,0,inputObject.offsetHeight+2);  // Position the calendar right below the form input
    calendarObjForFormTime.setInitialDateFromInput(inputObject,'dd/mm/yyyy hh:ii');   // Specify that the calendar should set it's initial date from the value of the input field.
    calendarObjForFormTime.addHtmlElementReference('myDate',inputObject);   // Adding a reference to this element so that I can pick it up in the getDateFromCalendar below(myInput is a unique key)

    if(calendarObjForFormTime.isVisible()){
        calendarObjForFormTime.hide();    
    }else{
        calendarObjForFormTime.resetViewDisplayedMonth();   // This line resets the view back to the inital display, i.e. it displays the inital month and not the month it displayed the last time it was open.
        calendarObjForFormTime.display();
    }

}

/* This is called in interview_pop.php */
function setOffset(hour,minute){
    offsetHour = hour;
    offsetMinute = minute;

}