var xmlHttp;

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP")
} catch (e) {
	try {
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
	} catch (E) {
		xmlHttp=false
	}
}
@else
xmlHttp=false
@end @*/
if (!xmlHttp) {
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		xmlHttp=false
	}
}

function initialize() {
	var list;
	var i;
	var j;
	
	list = document.getElementsByTagName("form");
	for (i=0; i<list.length; i++) {
		if (list[i].firstChild && list[i].firstChild.nodeType == 8) {
			list[i].setAttribute("action",list[i].firstChild.nodeValue);
			list[i].removeChild(list[i].firstChild);
		}
		list[i].onsubmit = checkForm;
	}
	list = document.getElementsByTagName("input");
	for (i=0; i<list.length; i++) {
		if (list[i].type == "submit" && list[i].disabled) {
			list[i].disabled = false;
		}
	}
}
function checkData(node) {
	if (node.previousSibling && node.previousSibling.nodeName == "#comment" && node.previousSibling.nodeValue.match(/^(req|req:match:.+|match:.*)$/i)) {
		return node.previousSibling.nodeValue;
	}
	return false;
}
function checkForm(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "");
	var form = (evt.target) ? evt.target : evt.srcElement;
	var list = form.getElementsByTagName("input");
	var radios = new Object();
	for (var i=0; i<list.length; i++) {
		if (!checkData(list[i]) || !list[i].getAttribute("name")) continue;
		if (list[i].getAttribute("type").match(/radio/i) || list[i].getAttribute("type").match(/checkbox/i)) {
			if (!radios[list[i].getAttribute("name")]) {
				radios[list[i].getAttribute("name")] = new Array(list[i]);
			}
			else {
				radios[list[i].getAttribute("name")][radios[list[i].getAttribute("name")].length] = list[i];
			}
		}
		else if (list[i].name && !checkNode(list[i])) {
			alert("Invalid or missing entry");
			return false;
		}
	}

	var list = form.getElementsByTagName("select");
	for (var i=0; i<list.length; i++) {
		if (!checkData(list[i]) || !list[i].getAttribute("name")) continue;
		if (!checkNode(list[i])) {
			alert("Selection required");
			return false;
		}
	}
	var list = form.getElementsByTagName("textarea");
	for (var i=0; i<list.length; i++) {
		if (!checkData(list[i]) || !list[i].getAttribute("name")) continue;
		if (!checkNode(list[i])) {
			alert("Invalid or missing entry");
			return false;
		}
	}
	for (list in radios) {
		var checked = false;
		for (i=0; i<radios[list].length; i++) {
			if (radios[list][i].checked) {
				checked = true;
				break;
			}
		}
		if (!checked) {
			for (i=0; i<radios[list].length; i++) {
				highlight(radios[list][i].parentNode,'error');
			}
			radios[list][0].focus();
			alert("Selection required");
			return false;		
		}
	}
	var input = document.createElement("input");
	input.setAttribute("type","hidden");
	input.setAttribute("name","valid");
	input.setAttribute("value",valid);
	form.appendChild(input);
	if (submitmessage) alert(submitmessage);
	return true;
} 
function checkNode(node) {
	var check = checkData(node);
	if (check.match(/^req/) && node.value == "") {
		highlight(node,'error');
		return false;
	} 
	if (!node) return;
	if (node.value != "") {
		var test;
		if (check.match(/^(?:req:)?nomatch:(.*)/i) && (testexp = new RegExp(RegExp.$1,"i")) && testexp.test(node.value)) {
			highlight(node,'error');
			return false;
		}
		else if (check.match(/^(?:req:)?match:(.*)/i) && (testexp = new RegExp(RegExp.$1,"i")) && !testexp.test(node.value)) {
			highlight(node,'error');
			return false;
		}
	}
	return true;
}
function highlight (node,newclass) {
	var test = node.className;
	if (test == newclass || (test == 'text' && newclass == '') ) return;
	node.setAttribute("oldclass",test);
	node.onmouseover = resetHighlight;
	node.onkeydown = resetHighlight;
	node.className = newclass;
	node.focus();
}
function resetHighlight(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
	if (!evt) return;
	var node = (evt.target) ? evt.target : evt.srcElement;
	if (!node.nodeName.match(/(input|textarea|select)/i)) {
		node = node.getElementsByTagName("input")[0];
	}
	if (node.getAttribute("name") && node.getAttribute("type") && (node.getAttribute("type").match(/radio/i) || node.getAttribute("type").match(/checkbox/i))) {
		var list = document.getElementsByName(node.getAttribute("name"));
		for (var i=0; i<list.length; i++) {
			list[i].parentNode.className = this.getAttribute('oldclass');
			list[i].parentNode.onkeydown = "";
			list[i].parentNode.onmouseover = "";
			list[i].parentNode.removeAttribute("oldclass");
		}
	}
	else {
		node.className = this.getAttribute('oldclass');
		node.removeAttribute("onkeydown");
		node.removeAttribute("onmouseover");
		node.removeAttribute("oldclass");
	}
}
function input_element_onclick(node) {
	if (node.firstChild.type == "checkbox") {
		node.firstChild.checked=!node.firstChild.checked;
	}
	if (node.firstChild.type == "radio") {
		node.firstChild.checked=1;
	}
}
function input_toggle (node){
	if (node.type == "checkbox")
		node.checked = ! node.checked;
	if (node.onchange)
		window.setTimeout( function() { node.onchange(); },100);
}
function hide_show_long_text(node) {
	while (node.className != "hide_show_long_text") 
		node = node.parentNode;
	node = node.rows[0].cells[0];
	if (node.firstChild.style.display == "none") { 
		node.firstChild.style.display = "block";
		node.lastChild.style.display = "none";
		node.nextSibling.firstChild.firstChild.nodeValue = "+";
	} else {
		node.lastChild.style.display = "block";
		node.firstChild.style.display = "none";
		node.nextSibling.firstChild.firstChild.nodeValue = "\u2013";
	} 
	node.focus();
}
function getFees(form,url) {
	if (!xmlHttp) return -1;
	var qs = "",e,i,j;
	for (i=0; i<form.elements.length; i++) {
		e = form.elements[i];
		if (e.tagName == "INPUT") {
			if (e.type == "text" || e.type == "hidden" || e.type == "password" || e.checked) {
				qs += e.name + "=" + encodeURI(e.value);
			}

		}   
		if (e.tagName == "SELECT") {
			for (j=0; j<e.options.length; j++) {
				if (e.options[j].selected) {
					qs += e.name + "=" + encodeURI(e.options[j].value);
				}
			}
		}
		if (e.tagName == "TEXTAREA") {
			qs += e.name + "=" + encodeURI(e.value);
		}
		qs += "&";
	}
    qs+="\n";

    xmlHttp.open("POST", url, false);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xmlHttp.setRequestHeader("Content-length", qs.length);
	xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(qs); 
    return xmlHttp.responseText;
}
