function URLDecode( str )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = str;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while

   return plaintext;
};

function getRequestBody(formRef)
{
	var aParams = new Array();
	
	for(var i=0; i< formRef.elements.length; i++)
	{
		if(formRef.elements[i].type=="radio")
		{
			if(formRef.elements[i].checked)
			{
				var sParam = encodeURIComponent(formRef.elements[i].name);
				sParam += "=";
				sParam += encodeURIComponent(formRef.elements[i].value);
				aParams.push(sParam);
			}
		}
		else
		{
			var sParam = encodeURIComponent(formRef.elements[i].name);
			sParam += "=";
			sParam += encodeURIComponent(formRef.elements[i].value);
			aParams.push(sParam);
		}
	}
	return aParams.join("&");
}

function ajNewListProcess(ref, divId, normId, testId)
{
	
	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("post", ref.action,true);
	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	oXmlHttp.onreadystatechange = function()
	{
		if(oXmlHttp.readyState==4)
		{
			if(oXmlHttp.status==200)
			{
				
				processData(oXmlHttp.responseText);

				if(valid=="y")
				{
					updateListRows(nListType, nListName, nListId, divId, normId, testId);
				}
				else
				{
				alert(URLDecode(errmsg));	
				}
			}
			else
			{
				alert("error: "+oXmlHttp.statusText);
			}
		}
	}
	
	var toSend = getRequestBody(ref);
	oXmlHttp.send(toSend);
	
	
	return false;
}

function processData(data)
{
	
	var txtexp = data.split("&");
	for(var i=0;i<txtexp.length;i++)
	{
		if(txtexp[i].length>0)
		{
		var txtexp2 = txtexp[i].split("=");
		var txtstr = txtexp2[0]+"='"+txtexp2[1]+"';";
		eval(txtstr);
		}
	}
}

function updateListRows(nListType, nListName, nListId, divId, normId, testId)
{
	
	
	/*if(nListType=="n") 	{ 	document.getElementById(normId).options.add(optn); 	}
	else if(nListType=="t") { document.getElementById(testId).options.add(optn); }*/
	
	
	var b = document.getElementsByTagName('select');	

	for(var i = 0; i < b.length; i++)
	{
		tmpRef = "";
		if(nListType=="n") 	{ tmpRef = normId; }
		else if(nListType=="t") { tmpRef = testId; }
		if(b[i].getAttribute('id') == tmpRef)
		{
			var optn = document.createElement("OPTION");
			optn.text = nListName;
			optn.value = nListId;
			b[i].options.add(optn);
		}
	}
	
	document.getElementById(divId).style.display = "none";
	document.ajNewListForm.reset();
}

