//needs the following
//disable dates behind the start date when using the end date
//sets the end time equal to the start time (once)
//end time can be before the start time, because end time can be the following day.
//show event length
//<div style="position:absolute;opacity:0.5;MozOpacity:0.5;KhtmlOpacity:0.5;filter:alpha(opacity=50);">fsdfd</div>
//use above to make an auto-fill with onchange

var firstChange = true;
/* var event_info = $("event_time_info");
event_info.innerHTML = ""; //blank the output */

	/* function setupTimeField(field) {		
		document.getElementById("event_time_help").style.display = "none"; //js is running hide format help
			
        field = document.getElementById(field);
		newdiv = document.getElementById(timelistid);
				
		newdiv.style.display = "none";
		newdiv.style.position = "absolute";				
		
		newdiv.mouseover = false;
		newdiv.onchange = function() {
			this.field.value = this.value;
			if(this.field.onchange) //only one of the fields does			
				this.field.onchange();
			this.style.display = "none";
		}
		newdiv.onmouseover = function() {
			this.mouseover = true;
		}
		newdiv.onmouseout = function() {
			this.mouseover = false;
		}
		newdiv.onblur = function() {
			if(!this.field.mouseover)
				this.style.display = "none";			
		}
		
		field.select = newdiv; //pointer to the select		
		field.mouseover = false;
		field.onmouseover = function() {
			this.mouseover = true;
		}
		field.onmouseout = function() {
			this.mouseover = false;
		}
		field.onfocus = function() {
			this.select.style.display = "block";
            var pos = findPos(this);
            this.select.value = this.value;
            this.select.style.left = pos[0]+"px";
            this.select.style.top = pos[1]+field.offsetHeight+"px";
            this.select.field = this;
		}
		field.onblur = function() {
			if(!this.select.mouseover)
				this.select.style.display = "none";			
		}
		
		if(field.id == "f_startTime") {	
			field.onchange = function() {				
				if(firstChange) {
					endTime = document.getElementById("f_endTime");
					endTime.value = this.value;
					firstChange = false;
				}
			}
		}
	}
	
	function findPos(obj) { //needed because IE doesnt respond with the correct Top and Left otherwise
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
		}
		return [curleft,curtop];
	}
	
    */
    function catcalc(cal) {
        /* var date = cal.date;
        var time = date.getTime();        
        var field = $(enddate); //only update endDate if startDate is changed
        if (field != cal.params.inputField) {			
			field.value = date.print("%m-%d-%Y");
		} */
    }
    
    var configureDateSelectors = function (startDate, startTimePrefix, endDate, endTimePrefix, allday, dateWarning)
    {
        startDate = $(startDate);
        endDate = $(endDate);
        
        var startHour = $(startTimePrefix+'hour');
        var startMin = $(startTimePrefix+'minute');
        var startAMPM = $(startTimePrefix+'ampm');
        
        var endHour = $(endTimePrefix+'hour');
        var endMin = $(endTimePrefix+'minute');
        var endAMPM = $(endTimePrefix+'ampm');
        
        var timeSelects = [ startHour, startMin, startAMPM, endHour, endMin, endAMPM ];
        
        allday = $(allday);
        dateWarning = $(dateWarning);
        
        var allDayChangeFunc = function(e)
        {
            if(allday.checked)
            {
                timeSelects.invoke('writeAttribute', 'disabled', 'disabled');    
            }
            else
            {
                timeSelects.invoke('removeAttribute', 'disabled');
            }
        }
        
        allday.observe('change', allDayChangeFunc);
        allDayChangeFunc();
        
        
        var buildDate = function(date, allday, hour, min, ampm)
        {
            var split = date.getValue().split('\/');
            
            var ed = new Date();
            ed.setMonth(parseInt(split[0], 10)-1);
            ed.setDate(parseInt(split[1], 10));
            ed.setYear(parseInt(split[2], 10));
            ed.setSeconds(0);
            
            
            if(allday && !allday.checked)
            {
                if(hour && ampm)
                {
                    ed.setHours((parseInt(hour.getValue(), 10) % 12) + (ampm.getValue() == 'AM' ? 0 : 12));    
                }
                
                if(min)
                {   
                    ed.setMinutes(parseInt(min.getValue(), 10));
                }
            }
            
            
            return ed;
        }
        
        var startAfterEndMsg = Cognoti.getI18N('calendar', 'error.start_after_end');
        var startEqualsEndMsg = Cognoti.getI18N('calendar', 'error.start_equals_end');
		var startBeforeNowMsg = Cognoti.getI18N('calendar', 'error.start_before_now');
        
        var checkDates = function()
        {
            var start = buildDate(startDate, allday, startHour, startMin, startAMPM);
            var end = buildDate(endDate, allday, endHour, endMin, endAMPM);
            var now = new Date();
			
            if(start.getTime() > end.getTime())
            {
                dateWarning.show().update(startAfterEndMsg);                        
            }
            else if(start.getTime() == end.getTime() && !allday.checked)
            {
                dateWarning.show().update(startEqualsEndMsg);
            }
			else if(start.getTime() < now.getTime())
			{
				dateWarning.show().update(startBeforeNowMsg);
			}
            else
            {
                dateWarning.hide().update('');
            }
        }
        
        var startDateChangeFunction = function(calobj)
        {
            
            var split = endDate.getValue().split('\/');
            
            var ed = buildDate(endDate);
                
            if(ed < calobj.date)
            {
                endDate.setValue(startDate.getValue())    
            }
                
            checkDates();
        };
        
        
        setCalendar(startDate.identify(), startDateChangeFunction);
        setCalendar(endDate.identify(), checkDates);
        
        
        [startDate,endDate,startHour,endHour,startMin,endMin,startAMPM,endAMPM,allday].invoke('observe','change', checkDates);
        [startDate,endDate,startHour,endHour,startMin,endMin,startAMPM,endAMPM,allday].invoke('observe','keyup', checkDates);
        [startDate,endDate,startHour,endHour,startMin,endMin,startAMPM,endAMPM,allday].invoke('observe','blur', checkDates);
        
        checkDates();
        
    }
	
	var configureJumpSelector = function(inputField, dayField, monthField, yearField)
	{
		inputField = $(inputField);
		dayField = $(dayField);
		monthField = $(monthField);
		yearField = $(yearField);
		
        var jumpDateChangeFunction = function(obj)
        {
            inputField.setValue(inputField.getValue()) 
			
            /* var split = inputField.getValue().split('\/');
            
            var ed = new Date();
            ed.setMonth(parseInt(split[0], 10)-1);
            ed.setDate(parseInt(split[1], 10));
            ed.setYear(parseInt(split[2], 10));
            ed.setSeconds(0); */
            
            var ed = obj.date;
			
			dayField.setValue(ed.getDate());
			monthField.setValue(ed.getMonth() + 1);
			yearField.setValue(ed.getFullYear()); //getYear is depracated
        };
		
		setCalendar(inputField.identify(), jumpDateChangeFunction);
	}
     
    var setCalendar = function (inputField, onupdate)
    {
        Calendar.setup({
            inputField     :    inputField,   // id of the input field
            ifFormat       :    "%m/%d/%Y",       // format of the input field
            showsTime      :    false,
            onUpdate       :    onupdate
        });    
     
    }
	
var deleteEvent = function(link)
{
	confirmBubble(link, 'Are you sure you want to delete this event?', ['Yes', 'No'],
		function(val)
	{
		if (val == 'Yes')
		{
			link.parentNode.submit();
		}
	});
	return false;
}

var unregisterEvent = function(link)
{
	confirmBubble(link, 'Are you sure you want to unregister from this event?', ['Yes', 'No'],
		function(val)
	{
		if (val == 'Yes')
		{
			link.parentNode.submit();
		}
	});
	return false;
}

var checkChange = function(checkbox, block)
{
	if (checkbox.checked)
	{
		block.style.display = "block";
	}
	else
	{
		block.style.display = "none";
	}
}
