var ajax = new sack();
var Sel;
var LocationListArray = new Array();

function onLoadSet(){
document.getElementById('LocationMain').disabled = true;
document.getElementById('LocationSub').disabled = true;
document.getElementById('AddLoc').src = 'images/buttons/advsearch_off_07.gif';
document.getElementById('CatSub').disabled = true;
document.getElementById('LocationSub2').disabled = true;
}

	function getSubCats(selection){
		Sel = selection;
		var ID = Sel.options[Sel.selectedIndex].value;
		ajax = new sack();
		LocationListArray = new Array();
		clearSubLoc();
		popLocationListField();
		document.getElementById('LocationMain').disabled = false;
		document.getElementById('AddLoc').src = 'images/buttons/advsearch_off_07.gif';
		//popLocationListField();
		ajax.requestFile = '_includes/dropdown_populater.php?action=1&ID='+ID;	// Specifying which file to get
		document.getElementById('CatSub').options.length = 0;
		ajax.onCompletion = insertCats;	// Specify function that will be executed after file has been found
		ajax.runAJAX()
		
	}
	
	function getSubCats_refine(selection, subselect){
		Sel = selection;
		var ID = Sel.options[Sel.selectedIndex].value;
		ajax = new sack();
		LocationListArray = new Array();
	//	clearSubLoc();
		//popLocationListField();
		setArray_refine();
		document.getElementById('LocationMain').disabled = false;
		document.getElementById('AddLoc').src = 'images/buttons/advsearch_off_07.gif';
		//popLocationListField();
		ajax.requestFile = '_includes/dropdown_populater.php?action=1&ID='+ID+'&Sel='+subselect;	// Specifying which file to get
		document.getElementById('CatSub').options.length = 0;
		ajax.onCompletion = insertCats;	// Specify function that will be executed after file has been found
		ajax.runAJAX()
		
	}
	
	function delay(){
	setTimeout('setLocations()',1000);
	}
	
	function insertCats(){
		
	document.getElementById('CatSub').disabled = false;
	var obj = document.getElementById('CatSub');
	
	eval(ajax.response)	// Executing the response from Ajax as Javascript code	
	delay();
	}
	
	function setLocations(){
	var ID = Sel.options[Sel.selectedIndex].value;
	ajax = new sack();
	ajax.requestFile = '_includes/dropdown_populater.php?action=2&ID='+ID+'&Par=';	// Specifying which file to get
	ajax.onCompletion = insertLocals;	// Specify function that will be executed after file has been found
	ajax.runAJAX();	
	}
	
	function insertLocals(){
	document.getElementById('LocationMain').options.length = 0;
	var obj = document.getElementById('LocationMain');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	

	}
	
	function setSubLocations(){
		document.getElementById('LocationSub').disabled = false;
		document.getElementById('LocationSub2').disabled = true;
		document.getElementById('AddLoc').src = 'images/buttons/advsearch_07.gif';
		document.getElementById('LocationSub2').options.length = 0;
	var ID = document.getElementById('CatParent');
	ID = ID.options[ID.selectedIndex].value;
	var Par = document.getElementById('LocationMain');
	Par = Par.options[Par.selectedIndex].value;
	ajax = new sack();
	ajax.requestFile = '_includes/dropdown_populater.php?action=2&ID='+ID+'&Par='+Par;	// Specifying which file to get
	ajax.onCompletion = insertSubLocals;	// Specify function that will be executed after file has been found
	ajax.runAJAX();	
	}
	
	function setSubLocationsLower(){
		document.getElementById('LocationSub2').disabled = false;
	var ID = document.getElementById('CatParent');
	ID = ID.options[ID.selectedIndex].value;
	var Par = document.getElementById('LocationSub');
	Par = Par.options[Par.selectedIndex].value;
	ajax = new sack();
	ajax.requestFile = '_includes/dropdown_populater.php?action=2&ID='+ID+'&Par='+Par;	// Specifying which file to get
	ajax.onCompletion = insertSubLocalsLower;	// Specify function that will be executed after file has been found
	ajax.runAJAX();	
	}
	
	
	
	
	function clearSubLoc(){
	document.getElementById('LocationSub').disabled = true;
	document.getElementById('LocationSub').options.length = 0;
	document.getElementById('LocationSub2').disabled = true;
	document.getElementById('LocationSub2').options.length = 0;
	}
	
	function insertSubLocals(){
	document.getElementById('LocationSub').options.length = 0;
	var obj = document.getElementById('LocationSub');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	

	}
	
	function insertSubLocalsLower(){
	document.getElementById('LocationSub2').options.length = 0;
	var obj = document.getElementById('LocationSub2');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	

	}
	
	function checkIfExists(LocID){
	var i =0;
	for(i =0;i<=(LocationListArray.length-1);i++){
	if (LocationListArray[i][0]==LocID){return true};
	} 
	return false;
	}
	
	function addLocation(){
	if(document.getElementById('LocationSub2').disabled == true){
	var Par = document.getElementById('LocationSub');	
	} else {
	var Par = document.getElementById('LocationSub2');
	}
	Par = Par.options[Par.selectedIndex].value;
	Par = Par.split("|")
	if ((Par[0]=='')&&(Par[1]=='')){
	LocationListArray = new Array();
	} else if (!checkIfExists(Par[0])){
	LocationListArray.push(Par);
	}
	popLocationListField();
	
	}
	
	
	
	function popLocationListField(){
	var LocationList = document.getElementById('LocationList');
	LocationList.value = '';
	var i =0;
	var output = '';
	for(i =0;i<=(LocationListArray.length-1);i++){
		output = output + LocationListArray[i][0];
		if ((LocationListArray.length-1)!=i){output = output +','; }
	} 
	LocationList.value = output;
	LoadLocListFE();
	}
	
	function removeFromArray(del){
	var TempArray = new Array;
	for(i =0;i<=(LocationListArray.length-1);i++){
	if (i!=del){
	TempArray.push(LocationListArray[i]);
	}
	}
	LocationListArray = TempArray;
	LoadLocListFE();
	popLocationListField();
	}
	
	function setArray_refine(){
	var LocationList = document.getElementById('LocationList').value;
	var refineLocations=LocationList.split(",");
	var LocationNames = document.getElementById('LocationNames').value;
	var refineLocationsNames=LocationNames.split(", ");
		for(i =0;i<=(refineLocations.length-1);i++){
			var TempArray = new Array;
			TempArray.push(refineLocations[i]);
			TempArray.push(refineLocationsNames[i]);
			
	LocationListArray[i] = TempArray;
		}
	LoadLocListFE();
	}
	
	function LoadLocListFE(){
	var i =0;
	var output = '';
	var locationString = '';
	if (LocationListArray.length<1){output = output + '';}
	for(i =0;i<=(LocationListArray.length-1);i++){
		
		locationString = locationString + LocationListArray[i][1];
		if (i!=(LocationListArray.length-1)){locationString = locationString + ', '; }
	output = output + LocationListArray[i][1] + ' - <a href=\"javascript:;\"onclick="removeFromArray('+i+');">Remove</a><BR>';
	} 
	if (output!=''){showm('LocationOutPut');
	output ='<strong>Location List:</strong><br />' + output;} else {
		hidem('LocationOutPut');}
		
	var LocationOutPut = document.getElementById('LocationOutPut');
		LocationOutPut.innerHTML = '';
		LocationOutPut.innerHTML = output;
	var LocationNames = document.getElementById('LocationNames');	
	LocationNames.value = locationString;
	} 
		
		
		
	// admin section
	function getCatQuestions(){
		document.getElementById('Questions').disabled = false;
	var ID = document.getElementById('CatParent');
	ID = ID.options[ID.selectedIndex].value;
		ajax = new sack();
	ajax.requestFile = '../_includes/dropdown_populater_admin.php?action=1&CatParent='+ID;	// Specifying which file to get
	ajax.onCompletion = CatQuestions;	// Specify function that will be executed after file has been found
	ajax.runAJAX();	
	}
	

		function CatQuestions(){
	document.getElementById('Questions').options.length = 0;
	var obj = document.getElementById('Questions');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
	}
	
	function setResponseOptions(){
		
		document.getElementById('Responses').disabled = false;
		var ID = document.getElementById('Questions');
		ID = ID.options[ID.selectedIndex].value;
		ajax = new sack();
		ajax.requestFile = '../_includes/dropdown_populater_admin.php?action=2&QID='+ID;	// Specifying which file to get
		ajax.onCompletion = CatResponsesQuestions;	// Specify function that will be executed after file has been found
		ajax.runAJAX();	
		
		}
	
		function CatResponsesQuestions(){
	document.getElementById('Responses').options.length = 0;
	var obj = document.getElementById('Responses');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
	}
	
	
	function showm(ce){
	
		 document.getElementById(ce).style.display = 'block';
	
	}
	
	function hidem(ce, but, cl){
	
		document.getElementById(ce).style.display = 'none';
	
	}
	
