window.onload = initAll;

var xhr = false;

function initAll() {
	var allLinks = document.getElementsByTagName("A");
	
	for (i=0; i<allLinks.length; i++) {
	
		var filmLink = allLinks[i];
		
		if (filmLink.className == "filmLink") {
		
			filmLink.onclick = getNewFile;
		
		}
		
	}
	
}

function getNewFile() {

	var allLinks = document.getElementsByTagName("A");
	
	for (i=0; i<allLinks.length; i++) {
	
		var filmLink = allLinks[i];
		
		if (filmLink.className == "filmLink") {
		
			filmLink.style.color = "#e6e6e6";
		
		}
		
	}
	
	
	makeRequest(this.href);
	
	this.style.color = "#F46E34";
	
	return false;
	
}

function makeRequest(url) {

	if (window.XMLHttpRequest) {
	
		xhr = new XMLHttpRequest();
		
	}
	else {
	
		if (window.ActiveXObject) {
		
			try {
			
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
				
			}
			
			catch (e) { }
			
		}
		
	}
	
	if (xhr) {
		
		xhr.onreadystatechange = showContents;
		xhr.open("GET", url, true);
		xhr.send(null);
		
	}
	
	else {
	
		documents.getElementById("filmContent").innerHTML = "<p>There was an error retrieving information for this film</p>";
		
	}
	
}

function showContents() {

	if (xhr.readyState == 4) {
	
		if (xhr.status == 200) {
		
			var outMsg = (xhr.responseXML && xhr.responseXML.contentType == "text/xml") ? xhr.responseXML.getElementsByTagName("choices")[0].textContent : xhr.responseText;
			
		}
		
		else {
		
			var outMsg = "There was a problem with the request" + xhr.status;
			
		}
		
		document.getElementById("filmContent").innerHTML = outMsg;
		
	}
	
}