function PreferredLocations() {
	function getLocationByNumber(number) {
		return getLocationByRow(getRowByNumber(number));
	}
	
	function getRowByNumber(number) {
		var table = document.getElementById("prefLocations");
		
		if(number >= table.getElementsByTagName('tbody')[0].getElementsByTagName('tr').length-2)
			return NULL;
		
		var row = table.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[number+2];
		return row;
	}
	
	function getNumberByRow(row) {
		var table = document.getElementById("prefLocations");
	
		for(var i=0;i<table.getElementsByTagName('tbody')[0].getElementsByTagName('tr').length;i++) {
			if(table.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[i]==row) {
				return i-2;
			}
		}
		
		return -1;
	}
	
	function addRow() {	
		var table = document.getElementById("prefLocations").getElementsByTagName('tbody')[0];
		var exampleLocation = getLocationByNumber(0);
		
		
		var row = document.createElement('tr');
		var col1 = document.createElement('td');
		var col2 = document.createElement('td');
		var col3 = document.createElement('td');
		var col4 = document.createElement('td');
		var sel1 = document.createElement('select');
		var sel2 = document.createElement('select');
		var sel3 = document.createElement('select');
		var img1 = document.createElement('img');
		
		for(var i=0;i<exampleLocation.country.options.length;i++) {
			var option = document.createElement('option');
			option.text = exampleLocation.country.options[i].text;
			option.value = exampleLocation.country.options[i].value;
			sel1.options[i] = option;
		}
		sel1.style.width = "130px";
		sel2.style.width = "130px";
		sel3.style.width = "130px";
		sel3.name = "locations[]";
		img1.src="/images/error_cross.jpg";
		img1.style.height = "14px";
		img1.style.width = "14px";
		
		col1.appendChild(sel1);
		col2.appendChild(sel2);
		col3.appendChild(sel3);
		col4.appendChild(img1);
		row.appendChild(col1);
		row.appendChild(col2);
		row.appendChild(col3);
		row.appendChild(col4);
		
		table.appendChild(row);
		
		Event.observe(sel1,'change',countryChange.bindAsEventListener());
		Event.observe(sel2,'change',cityChange.bindAsEventListener());
		Event.observe(sel3,'change',locationChange.bindAsEventListener());
		Event.observe(img1,'click',deleteRow.bindAsEventListener());
	}
	
	function getNumberOfRows() {
		var table = document.getElementById("prefLocations");
		return (table.getElementsByTagName('tbody')[0].getElementsByTagName('tr').length - 2);
	}
	
	function getLocationByRow(row) {
		var returnArray = new Object();
		
		returnArray.country = $(row).select("select")[0];
		returnArray.city = $(row).select("select")[1];
		returnArray.location = $(row).select("select")[2];

		return returnArray;
	}
	
	this.countryChange = countryChange;
	function countryChange(source) {
		if(source.parentNode == undefined)
			source = Event.element(source);
	
		var location = getLocationByRow(source.parentNode.parentNode);
		
		var country = location.country.value;
		
		if(country!="") {
			setSelectContent(location.city, "/cityList/"+URLEncode(country));
			location.location.options.length = 1;
		}
	}
	
	this.cityChange = cityChange;
	function cityChange(source) {
		if(source.parentNode == undefined)
			source = Event.element(source);
	
		var location = getLocationByRow(source.parentNode.parentNode);
		
		var country = location.country.value;
		var city = location.city.value;
		
		if(country!="" && city!="") {
			setSelectContent(location.location, "/locationList/"+URLEncode(country)+"/"+URLEncode(city));
		}
	}
	
	this.locationChange = locationChange;
	function locationChange(source) {
		if(source.parentNode == undefined)
			source = Event.element(source);
	
		var location = getLocationByRow(source.parentNode.parentNode);
		
		if(location.country.value!="") {
			var rowNumber = getNumberByRow(source.parentNode.parentNode);
			
			document.getElementById("country").value = location.country.value;
			document.getElementById("city").value = location.city.value;
			document.getElementById("location").value = location.location.value;
			
			/** Old, add a new row on location change.
			var size = getNumberOfRows();
			
			if(rowNumber==size-1) {
				addRow();
			}
			**/
		}
	}
	
	this.deleteRow = deleteRow;
	function deleteRow(source) {
		if(source.parentNode == undefined)
			source = Event.element(source);
	
		if(getNumberByRow(source.parentNode.parentNode) +1 == getNumberOfRows())
			return;
	
		var table = document.getElementById("prefLocations").getElementsByTagName('tbody')[0];
		table.removeChild(source.parentNode.parentNode);
	}
	
	function setSelectContent(selectBox, url) {
		selectBox.options.length = 0;
		
		var newOption = document.createElement('option');
		newOption.text  = "";
		newOption.value = "";
		
		selectBox.options[0] = newOption;
		
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: function(transport) {
		    var result = eval("("+transport.responseText+")");
		    for(var i=0;i<result.length;i++) {
		    	var row = result[i];
		    	var newOption = document.createElement('option');
  				newOption.text  = row[1];
 				newOption.value = row[0];
 				
 				selectBox.options[i+1] = newOption;
 			}
		  }
		});	
	}
	
	function URLEncode (clearString) {
	var output = '';
	var x = 0;
	clearString = clearString.toString();
	var regex = /(^[a-zA-Z0-9_.]*)/;
	while (x < clearString.length) {
		var match = regex.exec(clearString.substr(x));
		if (match != null && match.length > 1 && match[1] != '') {
			output += match[1];
		x += match[1].length;
		} else {
		if (clearString[x] == ' ')
			output += '+';
		else {
			var charCode = clearString.charCodeAt(x);
			var hexVal = charCode.toString(16);
			output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
		}
		x++;
		}
	}
	return output;
	}
}

var preferredLocations = new PreferredLocations();