function dateChanged(calendar) {
    // Calendar script to handle redirects when date is Clicked.

    // Beware that this function is called even if the end-user only
    // changed the month/year.  In order to determine if a date was
    // clicked you can use the dateClicked property of the calendar:
    if (calendar.dateClicked) {
      // OK, a date was clicked, do a javascript redirect
      var y = calendar.date.getFullYear();
      var m = calendar.date.getMonth()+1;     // integer, 0..11
      var d = calendar.date.getDate();      // integer, 1..31
      // redirect...
      window.location = "?site=default&tpl=EventSearch&SearchDate=" + y + "/" + m + "/" + d + "&SearchDateEnd=" + y + "/" + m + "/" + d;
    }
};

Calendar.setup(
    {
      flat         : "calendar-container", // ID of the parent element
      flatCallback : dateChanged           // our callback function
    }
);

function setUpFromDateCal() {
Calendar.setup({
	inputField:"SearchDate",
	ifFormat:"%m/%d/%Y",
	button:"fromdateCal",
	singleClick:true,
	onSelect:selectDate,
	weekNumbers:false
	});
}

function setUpToDateCal() {
Calendar.setup({
	inputField:"SearchDateEnd",
	ifFormat:"%m/%d/%Y",
	button:"todateCal",
	singleClick:true,
	onSelect:selectDate,
	weekNumbers:false
	});
}

function categoryClick(redirect){
    if(document.eventSearchForm.SearchEventLocation[document.eventSearchForm.SearchEventLocation.selectedIndex].value != ""){ redirect += "&SearchEventLocation="+escape(document.eventSearchForm.SearchEventLocation[document.eventSearchForm.SearchEventLocation.selectedIndex].value); }
    window.location = redirect;
}

function eventSearch(form,count){
    redirect = "?site=default&tpl=EventSearch";
    if(form.SearchEventKeyword.value != ""){ redirect += "&SearchEventKeyword="+escape(form.SearchEventKeyword.value); }
    if(form.SearchEventCategory[form.SearchEventCategory.selectedIndex].value != ""){ redirect += "&SearchEventCategory="+escape(form.SearchEventCategory[form.SearchEventCategory.selectedIndex].value); }
    if(form.SearchEventLocation[form.SearchEventLocation.selectedIndex].value != ""){ redirect += "&SearchEventLocation="+escape(form.SearchEventLocation[form.SearchEventLocation.selectedIndex].value); }
    if(form.SearchDate.value != "" && validateDate(form.SearchDate.value)){ redirect += "&SearchDate="+escape(form.SearchDate.value); }
    if(form.SearchDateEnd.value != "" && validateDate(form.SearchDateEnd.value)){ redirect += "&SearchDateEnd="+escape(form.SearchDateEnd.value); }
    if(form.SearchDate.value == ""){ redirect += "&SearchDate="+escape(form.SearchDateEnd.value); }
    if(form.SearchDateEnd.value == ""){ redirect += "&SearchDateEnd="+escape(form.SearchDate.value); }
    if(form.SearchDate.value == "today" || form.SearchDateEnd.value == "today"){ var today = new Date(); }
    if(form.SearchDate.value == "today"){ redirect += "&SearchDate="+escape((today.getMonth()+1)+"/"+today.getDate()+"/"+today.getFullYear()); }
    if(form.SearchDateEnd.value == "today"){ redirect += "&SearchDateEnd="+escape((today.getMonth()+1)+"/"+today.getDate()+"/"+today.getFullYear()); }
    if(count != ""){ redirect += "&start=0&count="+escape(count); }
    if((((form.SearchDate.value == "" || form.SearchDate.value == "today") || (form.SearchDate.value != "" && form.SearchDate.value != "today" && validateDate(form.SearchDate.value) == true))) && (((form.SearchDateEnd.value == "" || form.SearchDateEnd.value == "today") || (form.SearchDateEnd.value != "" && form.SearchDateEnd.value != "today" && validateDate(form.SearchDateEnd.value) == true)))){
        window.location = redirect;
    }else{
        alert("Please choose a valid date.");
    }
}

function validateDate(date){
    var validDate = false;
    var date = date.split("/",3);
    if(date.length == 3){
        if(date[0].length == 1 || date[0].length == 2){
            if(date[1].length == 1 || date[1].length == 2){
                if(date[2].length == 2 || date[2].length == 4){
                    if(date[0] >= 1 && date[0] <= 12){
                        if(date[1] >= 1 && date[1] <= 31){
                            if(date[2] >= 0 && date[2] <= 2037){
                                if((date[2].length == 2 && (date[2] <= 37 || date[2] >= 80)) || date[2].length == 4){
                                    validDate = true;
}}}}}}}} return validDate;
}

function qs(queryStringName){
    arrqs = window.location.search.substring(1).split("&");
    for(var i=0;i<arrqs.length;i++){
        var arrquery = arrqs[i].split("=");
        if(arrquery[0] == queryStringName){
            return arrquery[1];
        }
    }
}

function itemPerPage(formObj,type,site,tpl,keyword,category,location,datestart,dateend,sort){
    if(formObj){
        if(type == 'event'){
            var redirect= "?site="+escape(site)+"&tpl="+escape(tpl)+"&SearchEventKeyword="+escape(keyword)+"&SearchEventCategory="+escape(category)+"&SearchEventLocation="+escape(location)+"&SearchDate="+escape(datestart)+"&SearchDateEnd="+escape(dateend)+"&start=0&count="+escape(formObj[formObj.selectedIndex].value)+"&sort="+escape(sort);
        }else if(type == 'venue'){
        //                  query=Venue&site=default&tpl=Venue&ID=20&start=10&count=10
            var redirect= "?query=Venue&site="+escape(site)+"&tpl="+escape(tpl)+"&ID="+qs('ID')+"&start=0&count="+escape(formObj[formObj.selectedIndex].value)+"&sort="+escape(sort);
        }
        window.location = redirect;
    }
}/*
function whatsHappening(day,visible){
    if(visible){
        document.getElementById("wh_"+day).style.display = "none";
        document.getElementById("whhead_"+day).innerHTML = "<a class=\"nolink\" style=\"cursor:pointer !important;\" href=\"javascript:whatsHappening('"+day+"',false);\">"+day.toUpperCase()+"</a>";
    }else{
        document.getElementById("wh_"+day).style.display = "block";
        document.getElementById("whhead_"+day).innerHTML = "<a class=\"nolink\" style=\"cursor:pointer !important;\" href=\"javascript:whatsHappening('"+day+"',true);\">"+day.toUpperCase()+"</a>";
    }
}
function setWHTableWidth(){
    if(!qs("site") && !qs("tpl")){
        for(var i=0;i<2;i++){
            if(document.getElementById('column_time'+i+'') && document.getElementById('column_time'+i+'m')){ document.getElementById('column_time'+i+'').style.width = document.getElementById('column_time'+i+'m').offsetWidth+"px"; }
            if(document.getElementById('column_event'+i+'') && document.getElementById('column_event'+i+'m')){ document.getElementById('column_event'+i+'').style.width = document.getElementById('column_event'+i+'m').offsetWidth+"px"; }
            if(document.getElementById('column_category'+i+'') && document.getElementById('column_category'+i+'m')){ document.getElementById('column_category'+i+'').style.width = document.getElementById('column_category'+i+'m').offsetWidth+"px"; }
            if(document.getElementById('column_venue'+i+'') && document.getElementById('column_venue'+i+'m')){ document.getElementById('column_venue'+i+'').style.width = document.getElementById('column_venue'+i+'m').offsetWidth+"px"; }
            if(document.getElementById('column_city'+i+'') && document.getElementById('column_city'+i+'m')){ document.getElementById('column_city'+i+'').style.width = document.getElementById('column_city'+i+'m').offsetWidth+"px"; }
            //document.getElementById('column_legend'+i+'').style.width = document.getElementById('column_legend'+i+'m').offsetWidth+"px";
        }
    }
}setWHTableWidth();*/

function validateCategory() {
    if (document.getElementById('CategoryID').value > 0) {
        document.loginForm.submit();
    } else {
        alert('Please select a category for your event.');
    }
}