//var requestType="";
//var placeholder=null;

/**
*	sends an ajax request
*
* @param txtURL: url to request
* @param txtParams: parameters to send, in par1=val1&par2=val2 format
* @param rtype: what to do when the request is done. possible values:
*		load_in_placeholder: loads the content of the response into the given placeholder.
*			anything inside <script> tags inside the content of the response is evaluated as javascript.
*		overlay: loads the content of the response in an overlay box
*			anything inside <script> tags inside the content of the response is evaluated as javascript.
*		execute: content of the response is evaluated as javascript
*		run_code: given javascript code is run
*		run_function: given javascript function is run, with the content of the response as parameter
*		dev: content of the response is displayed in an alert box
*		load_in_wall: content of the response is loaded into commitment/user wall. not exactly sure why this is a special case.
*		parse: anything inside <script> tags inside the content of the response is evaluated as javascript.
* @param rph: 
*		if rtype is 'load_in_placeholder', this is the id of the placeholder
*		if rtype is 'run_code', this is the code to run
*		if rtype is 'run_code', this is the name of the function to run
*	@param notLoginRedirect: if type is 'execute' and the user is not logged in, browser is redirected to this URL
*
**/
function send(txtURL, txtParams, rtype, rph)
{
  //alert("function send()\n\ntxtURL:" + txtURL + "\ntxtParams:" + txtParams + "\nrtype:" + rtype + "\nrph:" + rph)
  
  //excute doesn't run javascript so another send needs to be done to check if login is required
  if(rtype == 'execute')
  {
		// for 'execute', don't add the 'wait' cursor
		var onCreate = function() 
			{
				document.body.style.cursor = "wait";
			}
  }

	new Ajax.Request(txtURL, {
		method: 'get', 
		parameters: txtParams, 
		onCreate: onCreate,
		onSuccess: function(transport) {
		  receive(transport, rtype, rph);
		}
//    ,on302: function(transport) {
//      receive(transport, rtype, rph);
//    }
	
	});
}


function sendPost(txtURL, txtParams, rtype, rph)
{
	//alert("function sendPost()\n\ntxtURL:" + txtURL + "\ntxtParams:" + txtParams + "\nrtype:" + rtype + "\nrph:" + rph)

  new Ajax.Request(txtURL, {
		method: 'post', 
		parameters: txtParams, 
		onComplete: function(transport) {
		  receive(transport, rtype, rph);
		}
	
	});
}

function receive(originalRequest, rtype, rph)
{
  //alert("function receive()\n\noriginalRequest:" + originalRequest + "\nrtype:" + rtype + "\nrph:" + rph)
  
  switch (rtype)
  {
		case 'load_in_wall':
			//alert("lw:" + lastWall);
			window.setTimeout("recallTxt('"+lastWall+"',0);",500);
    
    // Yes there's a break; missing here, it's intended, leave it like that!
		case 'load_in_placeholder':
			//alert(originalRequest.responseText);
			if (rph!="")
			{
				var txt = originalRequest.responseText;

				$(rph).innerHTML = txt;
				
				parseScript(txt);
			}
		break;
    
		case 'overlay':
			var txt = originalRequest.responseText + ' ';
			
			displayOverlay(txt,0);
			parseScript(txt); 
		break;
    
    case 'alert':
        var txt = originalRequest.responseText + ' ';
        
        displayAlert(txt);
        
        parseScript(txt); 
        
        eval(rph);
        
    break;
		
    case 'parse':
			var txt = originalRequest.responseText + ' ';
			
			parseScript(txt);
		break;
		
    case 'execute':
			eval(originalRequest.responseText); 
		break;
		
    case '':
		break;
    
		case 'dev':
			alert(originalRequest.responseText);
		break;
		
    case 'run_code':
			eval(rph);
		break;
		
    case 'run_function':
			eval(rph + '(' + originalRequest.responseText + ')');
		break;
	}
}

//parse and execute script in the txt variable
function parseScript(txt)
{
	var myRE = /<[s]cript.*?>([\s\S]*?)<\/[s]cript.*?>/im;

	var scripts = []; // extract script tags
	while (1)
	{
		var res = myRE.exec(txt);
		if (res)
		{
			scripts[scripts.length] = res[1]
			var n = txt.indexOf(res[0]);
			txt = txt.substr(0, n) + txt.substr(n + res[0].length);
		}
		else
			break;
	}
	
	// run script tags
	for (var i=0; i<scripts.length; i++)
	{
		var o = document.createElement('SCRIPT');
		o = document.body.appendChild(o);
		o.text = scripts[i];
	}
	
	if (chkObject('execute'))
	{
		//alert("Will run:" + $('execute').value);
		eval($('execute').value);
	}
	else
	{
		//alert("No run");
	}			
}

// registers responders for all ajax requests, so that when a request starts, 
// the cursor is set to "wait" and when the request ends, it goes back to normal
function registerAjaxResponders()
{
	// show "wait" mouse pointer while processing ajax request
	Ajax.Responders.register(
		{
			onCreate: function() 
			{
				document.body.style.cursor = "progress";
			},   
			onComplete: function() 
			{
				document.body.style.cursor = "auto";
			} 
		}); 
}

registerAjaxResponders();
