/*
	Selects all the checkbox whose name starts with the given one
*/
function selectall(nameStartsWith)
{
	var size = document.delForm.elements.length;
	for(i=0; i<size; i++)
	{
		var element = document.delForm.elements[i]; 
		if(element && element.name.indexOf(nameStartsWith) == 0)
			element.checked = element.checked == true  ? element.checked = false : element.checked = true;
	}			
}





/* Ajax small framework */

function AjaxRequest(outCallback)
{
	this.outCallback = outCallback;
	this.SendAsyncRequest = SendAsyncRequest;
	this.InitAjaxRequest = InitAjaxRequest;	
	this.InitAjaxRequest();
};	

/*
	Builds up and return an ajax object or null if the browser doesn't support ajax
*/
function InitAjaxRequest()
{
	try
	{
		// Firefox, Opera 8.0+, Safari
		this.xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{				
				return null;
			}
		}
	}
	this.xmlHttp.onreadystatechange = this.outCallback;	
	return this.xmlHttp;
};

/*
	Sends an asynchronus request to the server through 
	@xmlHttp: the ajax object
	@url: the url to the page
	@isGet: true whether the request should be a get
	@formFields: the form ID that contains the fields to send together with the request
	@nameValuesArray: an array with name/value couples to specify arguments in the get/post request
*/
function SendAsyncRequest(url, isGet, formFields, namesValuesArray)
{
	if(this.xmlHttp == null) return false;
	
	var method = isGet ? "GET" : "POST";
	
	// this variable is used only if the method is POST
	var postFieldsString = null;
	
	// if the array wasn't passed by the user, let's build it..
	// otherwise, further values will be appended to the array
	if(namesValuesArray == null)
		namesValuesArray = new Array();
	
	if(formFields != null)
	{
		// If we have a valid FORM from where to take
		// parameters, let's add them to our array
		var the_form = document.getElementById(formFields);
		if(the_form != null)
		{			
			var size = the_form.elements.length;
			for(var i=0; i<size; i++)
			{
				var element = the_form.elements[i];
				namesValuesArray[element.name] = element.value;
			}
		}
	}
	
	// build the param list	
	if(isGet)
	{		
		var prevKey = false;
		// we have to append the params to the url		
		for(key in namesValuesArray)
		{			
			if(key != 'indexOf')
			{
				if(prevKey) url += '&';
				else url += '?';
				url += key + '=' + namesValuesArray[key];
				prevKey = true;
			}
		}				
	}
	else
	{
		var prevKey = false;
		postFieldsString = "";
		// we have to append the params to the fieldsString variable
		for(key in namesValuesArray)
		{
			if(key != 'indexOf')
			{
				if(prevKey) postFieldsString += '&';
				postFieldsString += key + '=' + namesValuesArray[key];
				prevKey = true;
			}
		}			
	}	
	
	// alert(url);
	// alert(postFieldsString);

	// call the server page to execute the server side operation
	this.xmlHttp.open(method, url, true);
	
	this.xmlHttp.send(postFieldsString);
		
	return true;
};
