
	function wiz_Compare() {

		var f,url,params,o;

		f=document.forms["wiz"];
		url="/ajax-functions/wizard-util.php";
		params="action=compare&sel="+f.elements["sel"].value+"&comp_id="+f.elements["comp_id"].value;

		// we are ready to send our request to the server...
		var http = new OUAC.createRequestObject();
		http.open("POST", url, true);
		http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length",params.length);
		http.setRequestHeader("Connection","close");
		http.onreadystatechange = function() {
			if (http.readyState == 4) {
				if (http.responseText.match(/COMP_ID:(t?\d+)/)) {
					document.location="compare.php?comp_id="+RegExp.$1;
				}
			}
		};
		http.send(params);
	}

	function wiz_UpdateDIV(area,url,params) {

		// we are ready to send our request to the server...
		var http = new OUAC.createRequestObject();
		http.open("POST", url, true);
		http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length",params.length);
		http.setRequestHeader("Connection","close");
		http.onreadystatechange = function() {
			if (http.readyState == 4) {
				if (http.responseText != '""') {
					o=document.getElementById(area);
					if (o) {
						// set the HTML to the new list, and reset behaviors on click.
						o.innerHTML=http.responseText;
						Behavior.apply();

						wiz_Button("btn_save",document.getElementById("btn_addall")?1:0);
					}
				}
			}
		};
		http.send(params);
	}

	function wiz_Clear() {
	// clear the selections and send to server to update 'compare' list column.

		var url,params;

		wiz_Button("btn_search",0);
		wiz_Button("btn_add",0);
		wiz_Button("btn_del",0);
		wiz_Cover("wizard_list_cover",1);

		url="/ajax-functions/wizard-util.php";
		params="action=clear&lang="+curLang();
		wiz_UpdateDIV('wizard_list',url,params);

		wiz_Button("btn_search",1);
		wiz_Button("btn_add",1);
		wiz_Button("btn_del",1);
	}

	function wiz_Delete() {
	// collect the selected items from the 'compare' column for removal and send to server to update 'compare' list column.

		var f,o,i,list,univ_id,prog_id,url,params,numSel;

		f=document.forms["wiz"];
		o=document.getElementById("wizard_list");
		if (!o) { alert('coloun not found'); return; }
		list=o.getElementsByTagName('li');
		url="/ajax-functions/wizard-util.php";
		params="action=delete&lang="+curLang()+"&delete=";
		numSel=0;
		for (i=0;i<list.length;i++) {
			if ((list[i].className.match(/selected/))&&(list[i].id.match(/c(\d+)_\d+/))) {
				var univ_id=RegExp.$1;
				var prog_id=list[i].getAttribute("prog_id");
				params+=(numSel==0)?"":";";
				params+=univ_id+'_'+prog_id;
				numSel++;
			}
		}
		if (numSel>0) {
		// only have to send request if there's something selected...

			wiz_Button("btn_search",0);
			wiz_Button("btn_add",0);
			wiz_Button("btn_addall",0);
			wiz_Button("btn_del",0);
			wiz_Cover("wizard_list_cover",1);

			if (f.elements["sel"]) params+="&current="+f.elements["sel"].value;
			wiz_UpdateDIV('wizard_list',url,params);

			wiz_Button("btn_search",1);
			wiz_Button("btn_add",1);
			wiz_Button("btn_addall",1);
			wiz_Button("btn_del",1);
		}
	}

	function wiz_Add() {
	// collect the selected items from the results column and send to server to update 'compare' list column.

		var f,o,i,list,univ_id,prog_id,url,params,numSel;

		f=document.forms["wiz"];
		o=document.getElementById("wizard_results");
		if (!o) { alert('column not found'); return; }
		list=o.getElementsByTagName('li');
		url="/ajax-functions/wizard-util.php";
		params="action=add&lang="+curLang()+"&add=";
		numSel=0;
		for (i=0;i<list.length;i++) {
			if ((list[i].className.match(/selected/))&&(list[i].id.match(/r(\d+)_\d+/))) {
				var univ_id=RegExp.$1;
				var prog_id=list[i].getAttribute("prog_id");
				params+=(numSel==0)?"":";";
				params+=univ_id+'_'+prog_id;
				numSel++;
			}
		}
		if (numSel>0) {
		// only have to send request if there's something selected...

			wiz_Button("btn_search",0);
			wiz_Button("btn_add",0);
			wiz_Button("btn_addall",0);
			wiz_Button("btn_del",0);
			wiz_Cover("wizard_list_cover",1);

			if (f.elements["sel"]) params+="&current="+f.elements["sel"].value;
			wiz_UpdateDIV('wizard_list',url,params);

			wiz_Button("btn_search",1);
			wiz_Button("btn_add",1);
			wiz_Button("btn_addall",1);
			wiz_Button("btn_del",1);
		}
	}

	function wiz_AddAll() {
	// collect all items from the results column and send to server to update 'compare' list column.

		var f,o,i,list,univ_id,prog_id,url,params,numSel;

		f=document.forms["wiz"];
		o=document.getElementById("wizard_results");
		if (!o) { alert('column not found'); return; }
		list=o.getElementsByTagName('li');
		url="/ajax-functions/wizard-util.php";
		params="action=add&lang="+curLang()+"&add=";
		numSel=0;
		for (i=0;i<list.length;i++) {
			if (list[i].id.match(/r(\d+)_\d+/)) {
				var univ_id=RegExp.$1;
				var prog_id=list[i].getAttribute("prog_id");
				params+=(numSel==0)?"":";";
				params+=univ_id+'_'+prog_id;
				numSel++;
			}
		}
		if (numSel>0) {
		// only have to send request if there's something selected...

			wiz_Button("btn_search",0);
			wiz_Button("btn_add",0);
			wiz_Button("btn_addall",0);
			wiz_Button("btn_del",0);
			wiz_Cover("wizard_list_cover",1);

			if (f.elements["sel"]) params+="&current="+f.elements["sel"].value;
			wiz_UpdateDIV('wizard_list',url,params);

			wiz_Button("btn_search",1);
			wiz_Button("btn_del",1);
		}
	}

	function wiz_GetFilters(doErrors) {
		
		var f,c,i,params,errors,errorTxt;
		f=document.forms["wiz"];
		params="";

		// collect filter settings and prepare URL for server request.
		c=0;
		errors=0;
		numOkay=0;
		errorTxt="";
		numFilters=f.elements["num_filters"].value;
		for (i=1;i<=numFilters;i++) {
			if (f.elements["ft_"+i]) {

				var filterValue,fName;

				fName="ft_"+i;
				var filterType=f.elements[fName].options[f.elements[fName].selectedIndex].value;
				
				switch(filterType) {
					case "text": 
					case "degree":
					case "name":
					case "univ":
					case "ouac_code":
						// free text search...
						if ((f.elements["fv_"+i])&&(!f.elements["fv_"+i].options)) {
							filterValue=f.elements["fv_"+i].value;
							if (!filterValue.match(/\w/)) errorTxt=dict("WIZARD_ERROR_TEXT"); //"Please provide some text to search for.";
							else numOkay++;
						}
						else filterValue="";
						break;
					case "enrolment":
						if (f.elements["fv_"+i+"_1"]) {
							filterValue=f.elements["fv_"+i+"_2"].value;
							if (!filterValue.match(/^\d+$/)) errorTxt=dict("WIZARD_ERROR_ENROLMENT"); //"Please provide a number for the Enrolment filter.";
							else {
								filterValue=f.elements["fv_"+i+"_1"].options[f.elements["fv_"+i+"_1"].selectedIndex].value+filterValue;
								numOkay++;
							}
						}
						else filterValue="";
						break;
					case "coop":
					case "location":
					case "grade_range":
					case "language":
						// select box...
						if ((f.elements["fv_"+i])&&(f.elements["fv_"+i].options)) {
							filterValue=f.elements["fv_"+i].options[f.elements["fv_"+i].selectedIndex].value;
							numOkay++;
						}
						else filterValue="";
						break;
					default:
						errorTxt="Unsupported filter type ("+filterType+")";
						filterValue="";
						break;
				}
				if ((!doErrors)||(filterValue!='')) {
					c++;
					params+="&ft_"+c+"="+filterType;
					params+="&fv_"+c+"="+URLEncode(filterValue);
				}
				if ((errorTxt!="")&&(doErrors)) errors=1;
			}
		}
		if ((doErrors)&&(errors)&&(numOkay==0)) {
			alert(errorTxt);
			return "ERROR";
		}
		return params;
	}

	function wiz_RemoveFilter(num) {

		var url,params;

		url="/ajax-functions/wizard-util.php";
		params="action=del_filter&lang="+curLang()+"&filter="+num;
		params+=wiz_GetFilters(0);

		wiz_UpdateDIV('wizard_filters',url,params);
	}

	function wiz_UpdateFilter(num) {

		var f,fName,fValue,url,params;

		f=document.forms["wiz"];
		fName='ft_'+num;
		fValue=f.elements[fName].options[f.elements[fName].selectedIndex].value;
		if (fValue=="-") {
		// they're removing this filter...

			url="/ajax-functions/wizard-util.php";
			params="action=del_filter&lang="+curLang()+"&filter="+num;
			params+=wiz_GetFilters(0);
		}
		else {
		// they've changed the type of filter...

			url="/ajax-functions/wizard-util.php";
			params="action=update_filters&lang="+curLang()+"&filter="+num;
			params+=wiz_GetFilters(0);
		}
		wiz_UpdateDIV('wizard_filters',url,params);
	}

	function wiz_Search() {
	// collect the filter settings and send a search request.

		var numFilters,url,params,exParams;

		wiz_Button("btn_search",0);
		wiz_Button("btn_add",0);
		wiz_Button("btn_addall",0);
		wiz_Button("btn_del",0);
		wiz_Cover("wizard_results_cover",1);

		url="/ajax-functions/wizard-util.php";
		params="action=search&lang="+curLang();
		exParams=wiz_GetFilters(1);

		if (exParams!="ERROR") {
			wiz_UpdateDIV('wizard_results',url,params+exParams);
		}
		else {
			wiz_Cover("wizard_results_cover",0);
		}

		wiz_Button("btn_search",1);
		wiz_Button("btn_add",1);
		wiz_Button("btn_addall",1);
		wiz_Button("btn_del",1);
	}

	function wiz_Cover(name,mode) {
		o=document.getElementById(name);
		if (o) {
			if (mode==1) {
				o.style.display="block";
				o.style.visibility="visible";
			}
			else {
				o.style.display="none";
				o.style.visibility="hidden";
			}
		}
	}

	function wiz_Button(name,mode) {
		o=document.getElementById(name);
		if (o) o.disabled=(mode==0)?1:0;
	}

	function wiz_AddFilter(obj) {
	// collect the filter settings and send a request to get a new one.

		var url,params;

		obj.disabled=1;
		wiz_Button("btn_search",0);

		url="/ajax-functions/wizard-util.php";
		params="action=add_filter&lang="+curLang();
		params+=wiz_GetFilters(0);

		wiz_UpdateDIV('wizard_filters',url,params);
	}

	function wiz_SaveState() {

		var f;
		params=wiz_GetFilters(1);
		if (params!="ERROR") {
			f=document.forms["wiz"];
			f.elements["action"].value="save";
			f.submit();
		}
	}

	function wiz_SearchOrSave() {

		var f;
		f=document.forms["wiz"];
		if (f.elements["action"].value=="") {
			wiz_Search(); 
			return false;
		}
		return true;
	}
