var g_oCalendar;
var ENTER_KEYWORDS = 'enter keywords';
var CALENDAR_DROPDOWNS = 1, CALENDAR_DATES = 2;

Date.prototype.toSearchDateFormat = function(){
	var sMonth = this.getMonth()+1;
	if(sMonth < 10) sMonth = '0' + sMonth; 
	var sDay = this.getDate();
	if(sDay < 10) sDay = '0' + sDay;
	return(this.getFullYear() +  '-' + sMonth + '-' + sDay);
}


function validateCalendarForm(frm){
	var aDate, d;
	var sDatesList = '', sDatesDelim = '';
	var days = frm.theCalendar.calendarDays;
	for(aDate in days){
		if(days[aDate].selected) {
			sDatesList += sDatesDelim + (new Date(days[aDate].valueOf)).toSearchDateFormat();
			sDatesDelim = ',';
		}
	}
	frm.dateList.value = sDatesList;
	
	if(frm.showAll.checked){
		frm.eventCategory.value = "";
	}
	
	if(frm.txt_search.value == ENTER_KEYWORDS){
		frm.txt_search.value = "";
	}
	return(true);
}


//-----------------------------------------------------------------------------
function calendar_onDateClick(dDate, bIsSelected){
	if(this._currentInput == CALENDAR_DATES){
		resetCalendarSelection();
		/*if(bIsSelected){
			//alert('selected : ' + dDate.getFullYear() + '-' + (dDate.getMonth()+1) + '-' + dDate.getDate());
		} else {
			//alert('de-selected : ' + dDate.getFullYear() + '-' + (dDate.getMonth()+1) + '-' + dDate.getDate());
		}//*/
	}//fi
	return(false);
}
//-----------------------------------------------------------------------------
		
//-----------------------------------------------------------------------------
function calendar_onDateLinkWrite(dDateDisplayed){
	return('/SearchController?catSelected=events&amp;dateList=' + dDateDisplayed.toSearchDateFormat());
}
//-----------------------------------------------------------------------------


function drawEventsCalendar(oSearchForm, displayDate){
	var colors = new CalendarColors(document.classes ? '#EEEEEE' : '#FFFFFF', '#FF9900', '#CCCCCC', '#FFCC66');
	var MONDAY = 1; //0 - Sunday, 1 - Monday, etc
	g_oCalendar = new Calendar(MONDAY, 'g_oCalendar', 'cal', 170, colors, displayDate);
	colors = null;
	var initSelection = new CalendarSelection(g_oCalendar.STARTWEEKDAY);
	var now = new Date();
	if(g_oCalendar.displayYear == now.getFullYear() && g_oCalendar.displayMonth == now.getMonth()){
		//only preselect the current week if the current month and year are displayed
		initSelection.type = initSelection.WEEK;
		initSelection.days[initSelection.days.length] = initSelection.CURRENTWEEK;
	}
	g_oCalendar.onDateClick = calendar_onDateClick;
	g_oCalendar.onDateLinkWrite = calendar_onDateLinkWrite;
	//g_oCalendar.onDrawButton = calendar_onDrawButton;
	g_oCalendar._currentInput = CALENDAR_DROPDOWNS;
	g_oCalendar.display(initSelection);
	initSelection = null; 
	g_oCalendar._currentInput = CALENDAR_DATES;
	g_oCalendar._form = oSearchForm;
	oSearchForm.theCalendar = g_oCalendar;
	
	
	if(!g_oCalendar.bFixedMac && !document.classes){		
		var c, cOr;
		if(document.all){
			c = document.all("cal");
			//cOr = document.all("calOrImage");
		} else {
			c = document.getElementById("cal");
			//cOr = document.getElementById("calOrImage");
		}
		c.style.display = "";
		//cOr.style.display = "";
	} else {
		setTimeout("ie5MacPositionCalendar('" + g_oCalendar.divName + "');", 1000);
	}
	setupCalendarEventHandlers(oSearchForm);
	g_oCalendar.canDisplayPrevMonth();
}//drawEventsCalendar()

function ie5MacPositionCalendar(sDivName){
	if(document.all){
		var oDiv = document.all(sDivName);
		with(oDiv.style){
			top = "280px";
			left = parseInt(450 + (document.body.offsetWidth - 750)/2) + "px";
		}
	}//fi
}

function setupCalendarEventHandlers(oSearchForm){
	var oElems = oSearchForm.elements;
	var oElemOptions;
	var iSelectedIndex;
	var sAgt = navigator.userAgent.toLowerCase();
	var bFixDropDownWidth = (document.getElementById && (sAgt.indexOf('mozilla')!=-1) && (sAgt.indexOf('spoofer')==-1) && (sAgt.indexOf('compatible')==-1) && (sAgt.indexOf('opera')==-1) && (sAgt.indexOf('webtv')==-1));
	for(var i=0; i<oElems.length; i++){
		switch(oElems[i].type){ 
			case "select-one" : 
				oElemOptions = oElems[i].options;
				//add the new blank option at the end
				oElemOptions[oElemOptions.length] = new Option("", "");
				//move the text,value by 1 so that the first one is empty
				for(var j=(oElemOptions.length-1); j>0; j--){
					oElemOptions[j].text = oElemOptions[j-1].text;
					oElemOptions[j].value = oElemOptions[j-1].value;
					//select the selected element
					if (oElemOptions[j-1].defaultSelected == true) 
					{         
						oElemOptions[j].selected=true      
					}
				}
				oElemOptions[0].text = "";
				oElemOptions[0].value = "";
				//add the handler
				oElems[i].onchange = resetCalendarSelection;
				if(bFixDropDownWidth){
					oElems[i].style.width = "58px";
				}
				break;
			case "text": 
				if(oElems[i].name == "txt_search") {
					oElems[i].value = ENTER_KEYWORDS;
					oElems[i].onfocus = new Function("if(this.value=='" + ENTER_KEYWORDS + "') this.value='';");
					oElems[i].onblur = new Function("if(this.value=='') this.value='" + ENTER_KEYWORDS + "';");
				}
				break;
			case "checkbox":
				if(oElems[i].name == "showAll") {
					oElems[i].onclick = showAll_onClick;
				} else {
					oElems[i].onclick = eventCategory_onClick;
				}
				break;
		}
	}//for
}//setupCalendarEventHandlers()

function resetCalendarSelection(){
	if(this.type == 'select-one') {
		g_oCalendar._currentInput = CALENDAR_DROPDOWNS;
		g_oCalendar.deselectAll();
		g_oCalendar._currentInput = CALENDAR_DATES;
	} else {
		g_oCalendar._currentInput = CALENDAR_DATES;
		var oElems = g_oCalendar._form.elements;
		for(var i=0; i<oElems.length; i++){
			if(oElems[i].type == "select-one"){ 
				oElems[i].options[0].selected = true; //.selectedIndex = 0;
			}//fi elem type
		}//for
	}//fi type
} //resetCalendarSelection()

function showAll_onClick(){
	if(!this._cats) this._cats = this.form.eventCategory;
	for(var i=0; i<this._cats.length; i++){
        this._cats[i].checked = this.checked;
	}
}

function eventCategory_onClick(){
    var bRes = true;
    if(!this._cats || !this._all) {
		this._cats = this.form.eventCategory;
		this._all = this.form.showAll;
	}
    for(var i=0; (i<this._cats.length) && bRes; i++){
        bRes = this._cats[i].checked;
    }
    this._all.checked = bRes;
}

function dateclick(theDate) {
    if(frm["d"]) {
	    frm["d"].value=theDate;	    
	    frm.action = frm.action.replace(/\?.*$/, ""); //strip the query string on resubmit	    
	    frm.submit();
	}
}

function viewclick(val) {    
    if(frm["vw"]) {
        frm["vw"].value=val;        
        if (val=='y') {            
            $('#day-button').removeClass('selected-view');                        
            $('#week-button').addClass('selected-view');            
        } 
        else {
            $('#week-button').removeClass('selected-view');
            $('#day-button').addClass('selected-view');            
        }
        g_oCalendar.browserDraw();
        calendarWeekHover();
    }
}
