function clear_form(form_name) {
	var form = document.forms[form_name];
	for(i=0; i < form.elements.length; i++) {
		field = form.elements[i];
		if(typeof field.selectedIndex != 'undefined') { field.selectedIndex = 0; }
		else if(field.type == 'text' || field.type == 'hidden' ) { field.value = ''; }
	}
}

function encode_form(form_name) {
	var encoded = '';
	var form = document.forms[form_name];
	for(i=0; i < form.elements.length; i++) {
		field = form.elements[i];

		// Determine value
		switch(field.type) {
			case 'text':
			case 'password':
			case 'hidden':
			case 'textarea':
				value = field.value;
			break;
			case 'select-one':
			case 'select-multiple':
				if(field.selectedIndex >= 0) {
					value = field.options[field.selectedIndex].value
				} else {
					value = '';
				}
			break;
			case 'checkbox':
			case 'radio':
				if(field.checked) { value = field.value; }
				else { continue; }
			break;
			default:
				value = '';
		}

		encoded += "&" + field.name + "=" + encodeURIComponent(value)
	}
	return encoded.substr(1);
}

function loadFragmentInToElement(fragment_url, element_id, post_process, loading_text) {
 	   var xmlhttp=false;
 	   /*@cc_on @*/
 	   /*@if (@_jscript_version >= 5)
 	   // JScript gives us Conditional compilation, we can cope with old IE versions.
 	   // and security blocked creation of the objects.
 		try {
 		 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 		} catch (e) {
 		 try {
 		  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		 } catch (E) {
 		  xmlhttp = false;
 		 }
 		}
 	   @end @*/
 	   if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		 xmlhttp = new XMLHttpRequest();
 	   }

	if(element_id != null) {
		var element = document.getElementById(element_id);
 	   if(typeof loading_text != 'undefined') {
		   	if(loading_text != false) element.innerHTML = loading_text;
	   } else {
		   element.innerHTML = 'Loading...';
	   }
	}
	
 	   xmlhttp.open("GET", fragment_url, true);
 	   xmlhttp.onreadystatechange = function() {
 		   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
 			   if(element_id != null) element.innerHTML = xmlhttp.responseText;
			   if(typeof post_process != 'undefined') eval(post_process);
 		   }
 	   }
 	   xmlhttp.send(null);
}
function postFormInToElement(fragment_url, element_id, form_name, post_process) {
 	   var xmlhttp=false;
 	   /*@cc_on @*/
 	   /*@if (@_jscript_version >= 5)
 	   // JScript gives us Conditional compilation, we can cope with old IE versions.
 	   // and security blocked creation of the objects.
 		try {
 		 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 		} catch (e) {
 		 try {
 		  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		 } catch (E) {
 		  xmlhttp = false;
 		 }
 		}
 	   @end @*/
 	   if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		 xmlhttp = new XMLHttpRequest();
 	   }
		var element = document.getElementById(element_id);
		   element.innerHTML = 'Sending data...';
		xmlhttp.open("POST", fragment_url, true);
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.onreadystatechange = function() {
 		   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				element.innerHTML = xmlhttp.responseText;
				if(typeof post_process != 'undefined') eval(post_process);
			}
 	   }
	   xmlhttp.send(encode_form(form_name));
}
function get_url(fragment_url) {
 	   var xmlhttp=false;
 	   /*@cc_on @*/
 	   /*@if (@_jscript_version >= 5)
 	   // JScript gives us Conditional compilation, we can cope with old IE versions.
 	   // and security blocked creation of the objects.
 		try {
 		 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 		} catch (e) {
 		 try {
 		  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		 } catch (E) {
 		  xmlhttp = false;
 		 }
 		}
 	   @end @*/
 	   if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		 xmlhttp = new XMLHttpRequest();
 	   }
 	   xmlhttp.open("GET", fragment_url, false);
 	   xmlhttp.send(null);
 	   return xmlhttp.responseText;
}
function loadXMLInForm(fragment_url, form_name, post_process) {
 	   var xmlhttp=false;
 	   /*@cc_on @*/
 	   /*@if (@_jscript_version >= 5)
 	   // JScript gives us Conditional compilation, we can cope with old IE versions.
 	   // and security blocked creation of the objects.
 		try {
 		 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 		} catch (e) {
 		 try {
 		  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		 } catch (E) {
 		  xmlhttp = false;
 		 }
 		}
 	   @end @*/
 	   if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		 xmlhttp = new XMLHttpRequest();
 	   }
  
 	   xmlhttp.open("GET", fragment_url, true);
 	   xmlhttp.onreadystatechange = function() {
 		   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				// Read XML document
				var x = xmlhttp.responseXML.documentElement;
				var fields = new Array();
				var values = new Array();
				if(x) {
				if(x.hasChildNodes()) {
					for(i=0; i < x.childNodes.length; i++) {
						if(x.childNodes[i].nodeType == 1) {
							fields.push(x.childNodes[i].nodeName);
							if(x.childNodes[i].hasChildNodes()) {
								values.push(x.childNodes[i].firstChild.nodeValue);
							} else {
								values.push('');
							}
						}
					}
				}
				}
				
				// Now populate the form
				clear_form(form_name);

				var f = document.forms[form_name];
				for(i=0; i < fields.length; i++) {
					var fieldname = fields[i];
					if(typeof document.forms[form_name].elements[fieldname] != 'undefined') {
						var element = document.forms[form_name].elements[fieldname];
						if(typeof element.selectedIndex != 'undefined') {
							// Dropdown
							for(j=0; j < element.options.length; j++) {
								if(element.options[j].value == values[i]) {
									element.selectedIndex = j;
									break;
								}
							}
						}
						else if (typeof field.value != 'undefined') {
							// Text or hidden
							element.value = values[i];
						}
					}
				}
				if(typeof post_process != 'undefined') eval(post_process);
			}
 	   }
 	   xmlhttp.send(null);
}
