/**
 * @fileoverview AjaxRequest functions use Dojo to create Ajax request and to recieve responses.
 * @author Sajid
 * Created: 13th Aug '06
 * Last modified: 19th Sep '06
 */


dojo.require("dojo.io.*");



/**
 * Post a form and response to a function
 */
function postForm(action, myform, responseFunction)
{
	if(myform.encoding)
	{
		myform.encoding = "application/x-www-form-urlencoded";
	}
	else
	{
		myform.enctype = "application/x-www-form-urlencoded";
	}

	var myReq = {
	url:		action,
	formNode:	myform,
	load:		function(type, data)	{ responseFunction(data); },
	error:		function(type, error)	{ alert("Error:\nInvalid request or Error occured in your requested page."); },
	method:		"POST"
	};

	dojo.io.bind(myReq);
}

function postData(action, myform, responseFunction)
{
	//alert(myform.encoding);
	if(myform.encoding)
	{
		myform.encoding = "multipart/form-data";
	}
	else
	{
		myform.enctype = "multipart/form-data";
	}

	var myReq = {
	url:		action,
	formNode:	myform,
	load:		function(type, data)	{ responseFunction(data); },
	error:		function(type, error)	{ alert("Error:\nInvalid request or Error occured in your requested page."); },
	method:		"POST" ,
	mimetype:   "text/plain"

	//multipart:	true
	};

	/*myReq.file = [
		{ name: "file1", fileName: myfile }
	];*/

	dojo.io.bind(myReq);
}

/**
 * Post a form and response to a layer (update layer)
 */
function postUpdate(action, myform, responseHere)
{
	myform.enctype = "application/x-www-form-urlencoded";
	var myReq = {
	url:		action,
	formNode:	myform,
	load:		function(type, data)	{ document.getElementById(responseHere).innerHTML = data; },
	error:		function(type, error)	{ alert("Error:\nInvalid request or Error occured in your requested page."); },
	method:		"POST"
	};
	dojo.io.bind(myReq);
}


/**
 * Post no parameter and response to a layer (update layer) / function
 */
function postResponse(action, responseHere)
{

	var myReq = {
	url:		action,
	load:		function(type, data)	{
											if(typeof responseHere == "string")
												document.getElementById(responseHere).innerHTML = data;
											else
												responseHere(data);
										},
	error:		function(type, error)	{ alert("Error:\nInvalid request or Error occured in your requested page."); },
	method:		"POST"
	};
	dojo.io.bind(myReq);
}

/**
 * Get no parameter and response to a layer (update layer)
 */
function getResponse(action, responseHere)
{
	var myReq = {
	url:		action,
	load:		function(type, data)	{ document.getElementById(responseHere).innerHTML = data; },
	error:		function(type, error)	{ alert("Error:\nInvalid request or Error occured in your requested page."); },
	method:		"GET"
	};
	dojo.io.bind(myReq);
}
