var last_location_id = '';
var arSelected = []; 


function getMultiple(ob) 
{ 
	while (ob.selectedIndex !== -1) 
	{
		arSelected.push(ob.options[ob.selectedIndex]); 
		ob.removeChild(ob.options[ob.selectedIndex]); 
	}
}

function cmp(a, b) { return a.text < b.text ? -1 : a.text > b.text ? 1 : 0; }
function sortlist(id) 
{
	var lb = document.getElementById(id);
	arrTexts = [];
	
	for(i=0; i<lb.length; i++)
	{
	  arrTexts[i] = lb.options[i];
	}
	arrTexts.sort(cmp);
	arrTexts = arrTexts.uniq();
	//lb.length=0;
	
	for(i=0; i<arrTexts.length; i++) {
		lb.appendChild(arrTexts[i]);
	}
}

function addToAvailList(fromList, toList)
{
	from = document.getElementById(fromList);
	to = document.getElementById(toList);

	// Iterate selected elements
	getMultiple(from);
	
	// Empty box
	for (i = 0; i < arSelected.length; i++)
	{
		el = document.createElement('option');
		el.value = arSelected[i].value;
		text = document.createTextNode(arSelected[i].innerHTML);
		el.appendChild(text);
		to.appendChild(el);
	}
	if (to.id == 'availableLocations') {
		if (to.length > 10)	{
			alert('Du kan ikke legge ut en annonse i mer enn 10 kommuner.');
			
			// Move the fields back again
			for (i = 0; i < arSelected.length; i++)	{
				el = document.createElement('option');
				el.value = arSelected[i].value;
				text = document.createTextNode(arSelected[i].innerHTML);
				el.appendChild(text);
				from.appendChild(el);
			}
			to.length = 10;
		}
	}
	sortlist(from.id);
	sortlist(to.id);
	
	from = null;
	to = null;
	arSelected.length = 0;
}

function deleteList(location_id) 
{
	el = $('locations');
	thisChild = el.firstChild;
	
	while (thisChild != el.lastChild ) {
		if (thisChild.nodeType == 1) {
			nextChild = thisChild.nextSibling;
			el.removeChild(thisChild);
		}
		thisChild = nextChild;
	}
	if (thisChild !== null)
	{
		el.removeChild(thisChild);
	}
}
function deleteListJSON() 
{
	el = $('location');
	thisChild = el.firstChild;
	
	while (thisChild != el.lastChild ) {
		if (thisChild.nodeType == 1) {
			nextChild = thisChild.nextSibling;
			el.removeChild(thisChild);
		}
		thisChild = nextChild;
	}
	if (thisChild !== null)
	{
		el.removeChild(thisChild);
	}
}

function createList(location_id, multiple, selectedId) 
{
    multiple = (multiple === undefined) ? true : multiple;
    //multiple = (multiple === undefined) ? 0 : multiple;
    
	el = document.getElementById('location_parents');

	if(location_id === undefined) {
		thisChild = el.firstChild;
		i = 0;
		
		while (thisChild != el.lastChild ) {
			if (thisChild.nodeType == 1 ) {
				if(i == el.selectedIndex) {
					location_id = thisChild.value;
				}
				i++;
			}
			thisChild = thisChild.nextSibling;
		}
	}
	

	selected_location_id = selectedId;
	
	ajaxlol = new Ajax.Request('/annonsor/locations.php', 
	{
		parameters: 'location_id='+location_id+'',
		method: 'get',
		onSuccess: function(x)
		{
			if(last_location_id !== '') 
			{
				deleteList(last_location_id);
			}
			
			main_el = document.getElementById('geolocation');
			
			el = document.getElementById('locations');
			//el = document.createElement('select');
			//el.id = 'locations';
			if (multiple === true)
			{
				el.name = 'locations[]';
				el.multiple = 'multiple';
			}
			else
			{
				el.name = 'location';
			}
			el.ondblclick = function() { addToAvailList('locations', 'availableLocations'); };
			
			xml = new XMLReader(x.responseText);
			while (xml.findNextRow('<location>')) 
			{
				var location = xml.getValue('<name>');
				var id 		 = parseInt(xml.getValue('<id>'), 10);
				opt = document.createElement('option');
				opt.value = id;
				if(id == selected_location_id) 
				{
					opt.selected = 'selected';
				}
				text = document.createTextNode(location);
				opt.appendChild(text);
				el.appendChild(opt);
			}
			//main_el.appendChild(el);
			last_location_id = location_id;
		}
	});
}

function createListJSON(location_id) 
{
	el = document.getElementById('location_parents');

	if(location_id === undefined) {
		thisChild = el.firstChild;
		i = 0;
		
		while (thisChild != el.lastChild ) {
			if (thisChild.nodeType == 1 ) {
				if(i == el.selectedIndex) {
					location_id = thisChild.value;
				}
				i++;
			}
			thisChild = thisChild.nextSibling;
		}
		
		selected_location_id = 0;
	} else {
		selected_location_id = location_id;
	}
	
	
	ajaxlol = new Ajax.Request('/annonse/locations.php', 
	{
		parameters: 'location_id='+location_id+'',
		method: 'get',
		onSuccess: function(transport)
		{
			if(last_location_id !== '') 
			{
				deleteListJSON();
			}
			
			main_el = document.getElementById('geolocation');
			
			el = document.getElementById('location');
			
			if(transport.responseJSON) {
		  		if(typeof transport.responseJSON === 'object') {
		  			transport.responseJSON.each(function(location_row) {
		  				var opt = document.createElement('option');
		  				opt.value = location_row.id;
		  				
		  				if(location_row.id == selected_location_id) {
							opt.selected = 'selected';
						}
						
						var text = document.createTextNode(location_row.location);
						opt.appendChild(text);
						el.appendChild(opt);
		  			}.bind(this));
		  		}
		  	}
			//main_el.appendChild(el);
			last_location_id = location_id;
		}
	});
}

function onSubmitForm(lists)
{	
	var x;
	
	for (x in lists) {
		if (x !== null) {
			list = document.getElementById(lists[x]);
			
			if (Object.isElement(list) == true) {
				// Select all options
				for (i = 0; i < list.length; i++) {
					list.options[i].selected = 'selected';
				}
			}
		}
	}
}

function selectAll(selectBox, selectAllVar) {
	// have we been passed an ID
	if (typeof selectBox == "string") {
		selectBox = document.getElementById(selectBox);
	}
	// is the select box a multiple select box?
	if (selectBox.type == "select-multiple") {
		for (var i = 0; i < selectBox.options.length; i++) {
			selectBox.options[i].selected = selectAllVar;
		}
	}
}