function gup( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

function urldecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef";
   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;
}

jQuery(document).ready(function(){
	var myform = jQuery('#frmMain');
	var tmp = "";
	jQuery(':input', myform).each(function() {
		switch (this.type)
		{
			case "text":
			case "textarea":
				this.value = urldecode(gup(this.id));
				break;

			case "checkbox":
				if (gup(this.id) == "1") this.checked = true;
				break;

			case "select-one":
				for (var i=0; i < this.options.length; i++)
				{
					if (this.options[i].value == gup(this.id))
						this.selectedIndex = i;
				}
				break;

			case "radio":
				if (gup(this.name) == this.value)
					this.checked = true;
				break;
		}
		//tmp += this.type + " ";
	});

	//alert(tmp);

	if (gup("error").length)
	{
		$frmError = jQuery("#frmError").get(0);
		$frmError.innerHTML = urldecode(gup("error"));
		$frmError.style["display"] = "block";
	}
	else
	{
		$frmError = jQuery("#frmError").get(0);
		$frmError.style["display"] = "none";
	}
});
