//Browser Support Code
function ajaxRequest(act, vars){
        var ajaxRequest;  // The variable that makes Ajax possible!
        var path = '/cke_object_upload/extfrag/';

        try{
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
        } catch (e){
			// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
						ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
						// Something went wrong
						alert("Your browser broke!");
						return false;
				}
			}
        }
		
        // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function()
		{
            if(ajaxRequest.readyState == 4)
			{
				return processResponse(act, vars, ajaxRequest.responseText);
			}
		}
		
		// window.alert('starting calculation: ' + calculating);
        var len = vars.length;
        var pageString = "ajax.php?act=" + act;
        for(var i=0;i<len;i++)
        {
                var value = vars[i].toString();
                value = value.split(".").join(""); //replaceAll(value, ".", "");
                var nrv = i + 1;
                pageString += "&var" + nrv + "=" + value;
        }
        ajaxRequest.open("GET", path + pageString, true);
        ajaxRequest.send(null);
}

function ajaxPostRequest(path, act, vars){
        var ajaxRequest;  /* The variable that makes Ajax possible! */
        try{
                /* Opera 8.0+, Firefox, Safari */
                ajaxRequest = new XMLHttpRequest();
        } catch (e){
                /* Internet Explorer Browsers */
                try{
                        ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
                } catch (e) {
                        try{
                                ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
                        } catch (e){
                                /* Something went wrong */
                                alert('Your browser broke!');
                                return false;
                        }
                }
        }
        /* Create a function that will receive data sent from the server */
        ajaxRequest.onreadystatechange = function(){
                if(ajaxRequest.readyState == 4){
                        processResponse(act, vars, ajaxRequest.responseText);
                }
        }
        ajaxRequest.open('POST', path, true);
        ajaxRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        ajaxRequest.setRequestHeader('Content-length', vars.length);
        ajaxRequest.setRequestHeader('Connection', 'close');
        ajaxRequest.send(vars);
}



// texts
var favouriteplaced = 'Dit aanbod is in je favorieten geplaatst';

function processResponse(act, vars, text)
{
	switch(act)
	{
		case "setrating":
			processRating(text);
			break;
		case "addfavourite":
			processFavourite(text, vars);
			break;
		case "updatehomepageitems":
			processHomepageitems(text);
			reloadSortables();
			break;
		case "getBirthdays":
			processBirthdays(text);
			break;
		case "add_message":
			processMessage(text);
			break;
		case "get_guardian_data":
			processGuardianData(text);
			break;
		case "get_suggestions":
			processSuggestions(text);
			break;
		case "ajaxsearch":
			processSearchResults(text);
			break;
	}
}

function processSearchResults(text){
	document.getElementById("searchresults").innerHTML = text;
    //$(Hand).click(function() { //When trigger is clicked...
	var $j = jQuery.noConflict();
        $j('.cursusgroep_head').click(function() {
        //$(this).parent().attr('div').append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)
                var Hand = "#" + $j(this).attr('id');

         //Following events are applied to the subnav itself (moving subnav up and down)
         $j(Hand).find('div').slideDown('normal').show(); //Drop down the subnav on click

         $j(this).parent().hover(function() {
         }, function(){
                        $j(this).find('div').slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
         });

         //Following events are applied to the trigger (Hover events for the trigger)
         }).hover(function() {
             $j(this).addClass("subhover"); //On hover over, add class "subhover"
         }, function(){  //On Hover Out
             $j(this).removeClass("subhover"); //On hover out, remove class "subhover"
     });
         //});
}

function processBirthdays(text)
{
	document.getElementById('birthdaydiv').innerHTML = text;
}

function processMessage(text)
{
	document.getElementById('message').innerHTML = 'Bericht verzonden';
}

function processRating(text)
{
}

function processFavourite(text, vars)
{
	if ( parseInt(text) > 0  )
	{
		if ( document.getElementById('aanbod_interessant_' + vars[0]) )
		{
			document.getElementById('aanbod_interessant_' + vars[0]).innerHTML = favouriteplaced;
		}
	}	
}

function processHomepageitems(text)
{
	//document.getElementById('selecteditems').innerHTML = text;
}

function processGuardianData(text)
{
	document.getElementById('guardian_data').innerHTML = text;
}

function processSuggestions(text)
{
	if ( document.getElementById('suggestionsd') )
	{
		document.getElementById('suggestionsd').style.display = 'block';
		document.getElementById('suggestionsd').innerHTML = text;
	}

}

function textCounter(field, countfield, maxlimit) 
{
	if (field.value.length > maxlimit) // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else 
	countfield.value = maxlimit - field.value.length;
}


