window.onload = function()
{
	if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
		try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
		throw new Error( "This browser does not support XMLHttpRequest." )
	};

	function getPath()
	{
		var ret = false;
		var type = document.getElementById('perfect_placement_type').value;
		var continent = document.getElementById('perfect_placement_continent').value;
		var work = document.getElementById('perfect_placement_work').value;
		var work_experience = '';
		if(document.getElementById('perfect_placement_work_experience')) {
			work_experience = document.getElementById('perfect_placement_work_experience').value;
		}
		if (type && continent && work)
		{
			ret = type + '/' + continent + '/' + work;
			
			if (work == 'Work Experience')
			{
				if (work_experience)
				{
					ret += '/' + work_experience;
				}
				else
				{
					ret = false;
				}
			}
		}
		return ret;
	}

	function getSuggestions()
	{
		var path = getPath();
		if (!path) {
			return;
		}
		request = new XMLHttpRequest();
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				showSuggestions(request.responseText);
			}
		}
		request.open('GET', '/_ajax/perfect-placement/get-suggestions.php?path=' + escape(path), true);
		request.send(null);
	}

	function showSuggestions(response)
	{
		var suggestions = JSON.parse(response);
		var suggestions_container = document.getElementById('suggestions_container');
		suggestions_container.innerHTML = '';
		if (!suggestions.length) {
			suggestions_container.innerHTML = "<p><strong>No placements.</strong></p>";
			return;
		}
		var suggestions_html = '<p><strong>Having looked at your selections on the perfect placement chooser these placements may be the ones which would interest you.</strong></p>';
		for (var i = 0; i < suggestions.length; i++) {
			var suggestion = suggestions[i];
			suggestions_html += '<div class="recommended-project">';
			suggestions_html += '<img src="' + suggestion.image + '" style="float: left; margin: 5px 10px 5px 0px;" />';
			suggestions_html += '<p><a href="' + suggestion.link + '" title="' + suggestion.project_name + '" target="_blank">' + suggestion.project_name + '</a><br />' + suggestion.description + '</p>';
			suggestions_html += '</div>';
			suggestions_html += '<br />';
		}
		suggestions_container.innerHTML = suggestions_html;
	}

	document.getElementById('perfect_placement_work').onchange = function() {
		if (this.value == 'Work Experience') {
			var new_tr = document.createElement('tr');
			new_tr.id = 'work_experience_select';
			var new_td = document.createElement('td');
			new_tr.appendChild(new_td);
			var new_td = document.createElement('td');
			new_tr.appendChild(new_td);
			var new_select = document.createElement('select');
			new_select.name = 'perfect_placement_work_experience';
			new_select.id = 'perfect_placement_work_experience';
			var options = ['Teaching', 'Medicine and Healthcare', 'Journalism', 'Veterinary Medicine', 'Business', 'Law', 'Archaeology', 'Language Courses'];
			for (var i = 0; i < options.length; i++) {
				var new_option = document.createElement('option');
				new_option.value = options[i];
				new_select.appendChild(new_option);
				var new_text = document.createTextNode(options[i]);
				new_option.appendChild(new_text);
			}
			new_td.appendChild(new_select);
			//new_select.value = work_experience_val;
			// new_select.onchange = getSuggestions;
			this.parentNode.parentNode.parentNode.insertBefore(new_tr, this.parentNode.parentNode.nextSibling);
			//document.getElementById('perfect_placement_work_experience').onchange = getSuggestions;
		} else {
			if (document.getElementById('work_experience_select')) {
				document.getElementById('work_experience_select').parentNode.removeChild(document.getElementById('work_experience_select'));
			}
		}
		
		//getSuggestions();
	}
	
	//document.getElementById('perfect_placement_type').onchange = getSuggestions;
	//document.getElementById('perfect_placement_continent').onchange = getSuggestions;
	
	document.getElementById('submitplacementfinder').onclick = getSuggestions;
}

