// we use a javascript feature here called "inner functions"
// using these means the local variables retain their values after the outer function
// has returned. this is useful for thread safety, so
// reassigning the onreadystatechange function doesn't stomp over earlier requests.
// methodology invented/used from brockweaver on xml.com http://www.xml.com/cs/user/view/cs_msg/2815

// usage:
// foo = function(response) { alert(response.responseText); }
// HttpRequest("/foo/bar.aspx?haha=1", foo, true);

function HttpRequest(url, callback, async) {
	// this function is a 'universal callback'
	// that allows us to store several requests
	// at once without losing callbacks
	// [DEG 1/26/2008, Loading Message]
	// Hack: We're going to cheat a little for this custom calendar. 
	// Ultimately, the ideal situation is to change HttpRequest to accept
	// a new parameter for 'callimmediately'. Using this function one could
	// set the loading message on the entity of choice by calling a function.
	//
	// alert(url);
	
	bodycalload();

	function GetCallback()
	{
		 if (http_request.readyState == 4) {
	        if (http_request.status == 200) {
	            http_callback(http_request);
	        } else {
	            alert('There was a problem with the HTTP request. (HTTP Error ' + http_request.status + ' ' + http_request.statusText + ')');
	            return false;
	        }
	    }
	}
	
	// validate parameters
	if( typeof( async ) == 'undefined' ) {
		async = true;
	}
	
	if( typeof( callback ) != 'function' && async ) {
		alert( "No callback defined. A callback must be defined for async requests." );
		return false;
	}

	// these can have multiple copies over function calls
	// thus isolating them from event handler overwritings
    var http_request = false;
    var http_callback = callback;
	
	
	// create the httprequest object
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            //http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Your browser does not support HTTP requests.');
        return false;
    }
    
    if( async ) {
    	http_request.onreadystatechange = GetCallback;
    }
    http_request.open('GET', url, async);
    http_request.send(null);
    
    // for non-async requests, we just return the request object when we're done
    if( !async ) {
    	if (http_request.readyState == 4) {
	        if (http_request.status == 200) {
	            return http_request;
	        } else {
	            alert('There was a problem with the request. (HTTP Error ' + http_request.status + ' ' + http_request.statusText + ')');
	            return false;
	        }
   		}
    }
}

function filter(date, cal, eventview, custom, end)
{
    // for each type refinement dropdown, add a new var and append it to the filter.
    var location = document.getElementById("location");
    var types = document.getElementById("alltypes");

    var locationFilter = location.options[location.selectedIndex].value ;
    var typesFilter = types.options[types.selectedIndex].value ;
    
    if(''!=locationFilter&&''!=typesFilter) locationFilter = ',' + locationFilter;
    
    var f = typesFilter + locationFilter ;

    if(eventview == true)
    {
        HttpRequest("CustomCalendar/CalView.aspx?date=" + date + "&view=cal&types=" + f, bodycal, true);
        HttpRequest("CustomCalendar/SmallGrid.aspx?startdate=" + date + "&selected=" + date + "&view=cal&types=" + f, sidecal, true);
    }              
    else if(custom == true)
    {
        HttpRequest("CustomCalendar/CustomDate.aspx?startDate=" + date + "&endDate=" + end + "&types=" + f, bodycal, true);
        HttpRequest("CustomCalendar/SmallGrid.aspx?view=custom&startdate=" + date + "&endDate=" + end + "&types=" + f, sidecal, true);
    }
    else if(cal == true)
    {
        HttpRequest("CustomCalendar/CalView.aspx?date=" + date + "&view=cal&types=" + f, bodycal, true);
        HttpRequest("CustomCalendar/SmallGrid.aspx?startdate=" + date + "&selected=" + date + "&view=cal&types=" + f, sidecal, true);
    }
    else
    {
        HttpRequest("CustomCalendar/CalView.aspx?date=" + date + "&view=cal&types=" + f, bodycal, true);
        HttpRequest("CustomCalendar/SmallGrid.aspx?startdate=" + date + "&selected=" + date + "&view=cal&types=" + f, sidecal, true);
    } 
}

function processCustomDate(types)
{
    // Ignore the 'types' input and get it each time.
    // for each type refinement dropdown, add a new var and append it to the filter.
    var location = document.getElementById("location");
    var typesElem = document.getElementById("alltypes");

    var locationFilter = location.options[location.selectedIndex].value ;
    var typesFilter = typesElem.options[typesElem.selectedIndex].value ;
    
    if(''!=locationFilter&&''!=typesFilter) locationFilter = ',' + locationFilter;
    
    types = typesFilter + locationFilter ;
    
    document.getElementById("fromday").value = checkDates(document.getElementById("datefrom").options[document.getElementById("datefrom").selectedIndex].value, document.getElementById("fromday").value);
    document.getElementById("today").value = checkDates(document.getElementById("dateto").options[document.getElementById("dateto").selectedIndex].value, document.getElementById("today").value); 
    
    var start = document.getElementById("datefrom").options[document.getElementById("datefrom").selectedIndex].value + "/" + document.getElementById("fromday").value + "/" + document.getElementById("yearfrom").options[document.getElementById("yearfrom").selectedIndex].value;
    var end = document.getElementById("dateto").options[document.getElementById("dateto").selectedIndex].value + "/" + document.getElementById("today").value + "/" + document.getElementById("yearto").options[document.getElementById("yearto").selectedIndex].value;
    
    HttpRequest("CustomCalendar/CustomDate.aspx?startDate=" + start + "&endDate=" + end + "&types=" + types, bodycal, true);
    HttpRequest("CustomCalendar/SmallGrid.aspx?view=custom&startdate=" + start + "&endDate=" + end + "&types=" + types, sidecal, true);
}

function checkDates(month, day)
{
    if(day < 1) day = 1;
    
    switch(month) {
        case "1":
            if(day > 31) day = 31;
            break;
        case "2":
            if(day > 29) day = 29;
            break;
        case "3":
            if(day > 31) day = 31; 
            break;
        case "4":
            if(day > 30) day = 30;
            break;
        case "5":
            if(day > 31) day = 31;
            break;
        case "6":
            if(day > 30) day = 30;
            break;
        case "7":
            if(day > 31) day = 31;
            break;
        case "8":
            if(day > 31) day = 31;
            break;
        case "9":
            if(day > 30) day = 30;
            break;
        case "10":
            if(day > 31) day = 31;
            break;
        case "11":
            if(day > 30) day = 30;
            break;
        case "12":
            if(day > 31) day = 31;
            break; 
    }
    
    return day;
}

function PutLoading(elem)
{
    newElem = document.createElement('div');
    newElem.id = 'LoadingIndicator';
    newElem.className = 'loading_append';
    newElem.innerHTML = '';
    
    if(!elem.insertBefore(newElem, elem.firstChild)) { elem.appendChild(newElem) ;}
}

function RemoveLoading()
{
    document.getElementById('LoadingIndicator').parentNode.removeChild('LoadingIndicator') ;
}

	var sidecal = function(response) { 

    rText = response.responseText.substring(response.responseText.indexOf('<div>'));

    rText = rText.substring(0, rText.indexOf('</form>'));

    document.getElementById("sidecalbody").innerHTML = rText; 

}

var bodycal = function(response) {

    rText = response.responseText.substring(response.responseText.indexOf('<div>'));

    rText = rText.substring(0, rText.indexOf('</form>'));

    document.getElementById("bodycal").innerHTML = rText;

}

var sidecalload = function() { if(document.getElementById("sidecalbody")) PutLoading(document.getElementById("sidecalbody")) ;}
var bodycalload = function() { if(document.getElementById("bodycal")) PutLoading(document.getElementById("bodycal")) ; }

