function createNewSection(prefix,name) {
	if (prefix == null) prefix = '';
	var name = $F('sectionName');
	if (name != '') {
		var newDiv = Builder.node('div', {id: prefix + (sections.length + 1), className: 'section', style: 'display:none;' }, [
			Builder.node('h3', {className: 'handle',id:name}, name)
		]);

		sections.push(newDiv.id);
		$('page').appendChild(newDiv);
		Effect.Appear(newDiv.id);
		destroyLineItemSortables();
		createLineItemSortables();
		createGroupSortable();
	}
}

function createLineItemSortables() {
	for(var i = 0; i < sections.length; i++) {
		Sortable.create(sections[i],{tag:'div',dropOnEmpty: true, containment: sections,only:'lineitem'});
	}
}

function destroyLineItemSortables() {
	for(var i = 0; i < sections.length; i++) {
		Sortable.destroy(sections[i]);
	}
}

function createGroupSortable() {
	Sortable.create('page',{tag:'div',only:'section',handle:'handle'});
}

/*
Debug Functions for checking the group and item order
*/
function getGroupOrder() {
	var sections = document.getElementsByClassName('section');
	var alerttext = '';
	sections.each(function(section) {
		var sectionID = section.id;
		var order = Sortable.serialize(sectionID);
		alerttext += sectionID + ': ' + Sortable.sequence(section) + '\n';
	});
	alert(alerttext);
	return false;
}

function updateData() {
	var params = '';
	var sections = document.getElementsByClassName('section');
	sections.each(function(section) {
		params = Sortable.serialize(section.id);
		var ajax = new Ajax.Request(page_url,{
			method: 'post',
			parameters: params
		});
		//alert(getCookie('reorder'));
	});
	setCookie('reorder', 1);
}

/*	This version of the script is an adaptation from http://www.webdeveloper.com/forum/showthread.php?p=745951#post745951. it is used on land_sale_list.php */
function updateData2() {
	var inputs = document.getElementById('sortInputs').getElementsByTagName('input');
	var vals = [];
	var divs = document.getElementById('sale_row').getElementsByTagName('div');
	
	for (var i=0, end=divs.length; i<end; i++) {
		inputs[i].value = divs[i].id.replace('item_','');
		vals[i] = inputs[i].value;
	}

	params = 'check_value_hidden[]=' + vals.join('&check_value_hidden[]=');

	var ajax = new Ajax.Request(page_url,{
		method: 'post',
		parameters: params
	});
	alert(params);
	
	setCookie('reorder', 1);
}


/* ORIGINAL SCRIPT **
function updateData() {
	var params = '';
	var sections = document.getElementsByClassName('section');
	sections.each(function(section) {
		params += Sortable.serialize(section.id);
	});
	var ajax = new Ajax.Request(page_url,{
		method: 'post',
		parameters: params
	});
	//alert(params);
}*/

/* Updated script 
function getGroupOrder() {
	var headings = document.getElementsByClassName('handle');
	var sections = document.getElementsByClassName('section');
	var alerttext = '';
	sections.each(function(section) {
		var sectionID = section.id;
		var order = Sortable.serialize(sectionID);
		alerttext += headings[i].id + ', ' + sectionID + ': ' + Sortable.sequence(sections[i]) + '\n';
	});
	alert(alerttext);
	return false;
}*/