function findGamesByPrefix(){
	//alert("call to findGamesByPrefix");
	//get platform
	var pl_element = document.getElementById("platform_list");
	//alert(pl_element.value);
	var platform = pl_element.value;

	//get prefix
	var prefix_element = document.getElementById("game_prefix");
	//alert(prefix_element.value);
	//alert("prefix length is: "+ prefix_element.value.length);
	var prefix=prefix_element.value;

	if(prefix.length >= 3){
		var xmlHttp;
		try{
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}catch (e){
			// Internet Explorer
			try{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}catch (e){
				try{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					alert("Your browser does not support AJAX!");
				}catch (e){
					return false;
				}
			}
		}

		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4){
				updateGameList(document.getElementById("select_game_list"),xmlHttp.responseText);
			}
		}
		
		xmlHttp.open("GET","/submit-cheat/find-games?platform="+platform+"&prefix="+prefix.toUpperCase(),true); //true means asynchronous
		xmlHttp.send(null);

	}
  }

function updateGameList(game_options, responseText){
	//alert("call to updateGameList:" + responseText);
	//clear list
	for (var i=game_options.options.length-1; i >= 0; i--){
		game_options.remove(i);
	}
	
	//parse responseText to generate new list
	var cleaned_text = responseText.substring(responseText.indexOf("#")+1,responseText.length);
	var intIndexOfMatch = cleaned_text.indexOf("\n");
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		cleaned_text = cleaned_text.replace( "\n", "" );
	
		// Get the index of any next matching substring.
		intIndexOfMatch = cleaned_text.indexOf( "\n" );
	}
	
	var unitized_array = cleaned_text.split(";");
	var cur_option;
	var cur_unit;
	for(var i=0; i < unitized_array.length-1; i++){
		cur_unit = unitized_array[i].split(",");
		cur_option = document.createElement("option");
		cur_option.setAttribute("value", cur_unit[0]);
		cur_option.appendChild(document.createTextNode(cur_unit[1]));
		game_options.appendChild(cur_option);
	}
  }

function select_game(){
  	//alert("call to output_value");
  	var game_list = document.getElementById("select_game_list");
  	var selected_game = document.getElementById("selected_game");
  	var releaseId = document.getElementById("releaseId");
  	var options = game_list.options;
  	var selected_option;

	//find the selected option
	for(var i = 0; i < options.length; i++){
		if(options[i].selected){
			selected_game.value = options[i].text;
			releaseId.value=options[i].value;
		}
	}

    document.getElementById("gameDescription").value = selected_game.value;
    
	// reconfigure displayed divs appropriately
	document.getElementById("platform_list").setAttribute("disabled", null);
	document.getElementById("game_name_ajax_div").style.display = "none";
	document.getElementById("select_game_div").style.display = "none";
	document.getElementById("error1").style.display = "none";
	document.getElementById("error2").style.display = "none";
	document.getElementById("error3").style.display = "none";
	document.getElementById("error4").style.display = "none";
	document.getElementById("error5").style.display = "none";
	document.getElementById("cheat_terms_div").style.display = "block";
	document.getElementById("selected_game_div").style.display = "block";
	document.getElementById("data_entry_div").style.display = "block";
  }
  
function doPrePopulatedFields(){
	// reconfigure displayed divs appropriately
	document.getElementById("platform_list").setAttribute("disabled", null);
	document.getElementById("game_name_ajax_div").style.display = "none";
	document.getElementById("select_game_div").style.display = "none";
	document.getElementById("error1").style.display = "none";
	document.getElementById("error2").style.display = "none";
	document.getElementById("error3").style.display = "none";
	document.getElementById("error4").style.display = "none";
	document.getElementById("error5").style.display = "none";
	document.getElementById("cheat_terms_div").style.display = "block";
	document.getElementById("selected_game_div").style.display = "block";
	document.getElementById("data_entry_div").style.display = "block";
}
  
function submit_cheat(){
	var pass_validation = true;
	var curData = document.getElementById("cheat_type");
	if(curData.value == ""){
		document.getElementById("error1").style.display = "inline";
		pass_validation = false;
	}else{ document.getElementById("error1").style.display = "none"; }
	
	curData = document.getElementById("cheat_title");
	if(curData.value == ""){
		document.getElementById("error2").style.display = "inline";
		pass_validation = false;
	}else{ document.getElementById("error2").style.display = "none"; }
	
	curData = document.getElementById("cheat_body");
	if(curData.value == ""){
		document.getElementById("error3").style.display = "inline";
		pass_validation = false;
	}else{ document.getElementById("error3").style.display = "none"; }
	
	curData = document.getElementById("cheat_submitter");
	if(curData.value == ""){
		document.getElementById("error4").style.display = "inline";
		pass_validation = false;
	}else{ document.getElementById("error4").style.display = "none"; }
	
	curData = document.getElementById("cheat_terms");
	//alert(curData.checked);
	if(!curData.checked){
		document.getElementById("error5").style.display = "inline";
		pass_validation = false;
	}else{ document.getElementById("error5").style.display = "none"; }
	
	if(pass_validation){
		document.getElementById("error_div").style.display = "none";
		return true;
	}else{
		document.getElementById("error_div").style.display = "block";
		return false;
	}
}
