function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];

  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}


var display = 0;

function toggleDisplay(which) {
	if (which == 1 && display != 1) {
		display = 1;
		Element.hide('question_text');
		Effect.Appear('question_search');
	} else if (which == 0 && display != 0) {
		display = 0;
		Element.hide('question_search');
		Effect.Appear('question_text');		
	}
}

var cleared = 0;

function clearSearch() {
	if (cleared == 0) {
		document.getElementById('search').value = '';
		cleared = 1;
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
   		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

var questionArray = new Array();
function stateChanged() { 
	if (xmlHttp.readyState == 4) { 
		text = xmlHttp.responseText;
		responseArray = text.split("\n");
		clearList();
		
		for(i = 0; i < responseArray.length; i++) {
			tempArray = responseArray[i].split("|");
			if (tempArray[1] != undefined) {
				questionArray[tempArray[0]] = tempArray[1];
				
				if (tempArray[1].length > 80) {
					tempArray[1] = tempArray[1].substring(0, 78) + '...';
				}
				document.getElementById('questions').options[i] = new Option (tempArray[1], tempArray[0]);
			}
		}
	}
}

var selected = 0;
function displayQuestion(value) {
	if (questionArray[value] != undefined) {
		document.getElementById('selected').innerHTML = questionArray[value];
		Effect.Appear('selected');
		selected = 1;
	}
}

function clearList() {
	questionArray = new Array();
	document.getElementById('questions').options.length = 0;
	if (selected == 1) {
		Effect.Fade('selected');
	}
}

function updateList(model, str, base) {
	if (str.length == 0) {
		setTimeout("clearList()", 1000);
		return;
	}

	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	
	var url = base + "/admin/" + model + "/search";
	var params = "search=" + str;
	xmlHttp.open("POST", url, true);

	xmlHttp.onreadystatechange = stateChanged;

	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
	xmlHttp.send(params);
}