var parameters = '';
var parametersOK = false;


function getData(){
	
	if(document.getElementById('selector').checked) var url = '/guilddata/showMemberData.php?type=online';
	else var url = '/guilddata/showMemberData.php?type=all';
	
	makeRequest(url, 'membercontainer');
	
}

function getRecipies(){

	var url = '/guilddata/searchRecipies.php';
	makeRequest(url, 'searchcontainer');
}


function makeRequest(url, whichcontainer) {
	
	//get params - name given
	try{
		parameters = "recipename=" + encodeURI(document.getElementById("recipename").value);
	}catch(e){}
	
	//get params - list of recipies
	try{
		
		for( i = 0; i < document.getElementById("recipeform").recipeid.length; i++){
			if(document.getElementById("recipeform").recipeid[i].checked){
				
				parameters = "recipeid=" + encodeURI(document.getElementById("recipeform").recipeid[i].value);
				parametersOK = true;
				break;
			}
		}
		
	}catch(e){}
	
	
	//get params - single search result
	try{
		if(!parametersOK) parameters = "recipeid=" + encodeURI(document.getElementById("recipeform").recipeid.value);
	}catch(e){}
	
	
	if(url == '/guilddata/searchRecipies.php' && parameters == '') return;
	
	//show a nice loading animation
	try{
		document.getElementById(whichcontainer).innerHTML = "<img src='/images/ajax-loader.gif' alt='' />";
	}catch(e){
		//nothing
	}

	var httpRequest;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			//httpRequest.overrideMimeType('text/xml');
			httpRequest.overrideMimeType('text/html');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}

	if (!httpRequest) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	httpRequest.onreadystatechange = function() { alertContents(httpRequest, whichcontainer); };
	//httpRequest.open('GET', url, true);
	//httpRequest.send('');
	
	//parameters= "recipename=" + encodeURI(document.getElementById("recipename").value);
	
	httpRequest.open('POST', url, true);
	httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpRequest.setRequestHeader("Content-length", parameters.length);
	httpRequest.setRequestHeader("Connection", "close");
	httpRequest.send(parameters);



}

function alertContents(httpRequest, whichcontainer) {

	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			
			//output
			try{
				document.getElementById(whichcontainer).innerHTML = httpRequest.responseText;
			}catch(e){
				//nothing
			}
			
		} else {
			alert('There was a problem with the request.');
		}
	}

}