// global flag
var isIE = false;

// global request and XML document objects
var req;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function getFormDataByTag(theform, tag)
{
	var thechilds = theform.getElementsByTagName(tag);
	var postit = '';
	for (var k = 0; k < thechilds.length; k++) {
		name = thechilds[k].getAttribute('name');
		type = thechilds[k].getAttribute('type');
		if (type == 'checkbox') {
			if (thechilds[k].checked)
				checked = true;
			else
				checked = false;
		} else if (type == 'radio') {
			if (thechilds[k].checked)
				checked = true;
			else
				checked = false;
		} else
			checked = true;
		if (checked) {
			value = encodeURIComponent(thechilds[k].value);
			postit += name + '=' + value + '&';
		}
	}
	return postit;
}

function getFormData(id)
{
	var theform = document.getElementById(id);
	if (theform == null)
		return '';
	var postit1 = getFormDataByTag(theform, 'input');
	var postit2 = getFormDataByTag(theform, 'textarea');
	var postit3 = getFormDataByTag(theform, 'select');
	return postit1 + postit2 + postit3;
}

function loadXMLDocForm(url, id) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("POST", url, true);
	postit = getFormData(id);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
	req.setRequestHeader('Referer', window.location);
        req.send(postit);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("POST", url, true);
	postit = getFormData(id);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
	req.setRequestHeader('Referer', window.location);
        req.send(postit);
        }
    }
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            loadPage();
	    document.getElementById("loading").style.display = 'none';
         } else {
            alert("There was a problem connecting to the server:\n" +
                req.statusText);
         }
    } else if (req.readyState == 1) {
	document.getElementById("loading").style.display = 'block';
    }
}

function loadPage() {
	response = req.responseText;
	if (req.responseXML == null) {
		alert('There was an error in the server response. Try again later or contact us.');
		alert(response);
	}
	var items = req.responseXML.getElementsByTagName("module");
	for (var i = 0; i < items.length; i++) {
		div = document.getElementById(items[i].getAttribute('id'));
		if (div == null) {
			continue;
		}
		div.innerHTML = "";
		var content = items[i].getElementsByTagName("content");
		var lala = content[0].childNodes;
		for (var k = 0; k < lala.length; k++) {
			if (lala[k].nodeType == 4)
				div.innerHTML = lala[k].nodeValue;
		}
	}
	var error = req.responseXML.getElementsByTagName("error");
	for (var l = 0; l < error.length; l++) {
	if (error[l].hasChildNodes() && error[l].firstChild.nodeValue.length > 1)
		alert (error[l].firstChild.nodeValue);
	}
	var message = req.responseXML.getElementsByTagName("message");
	var thismessage = '';
	for (var l = 0; l < message.length; l++)
		if (message[l].hasChildNodes() && message[l].firstChild.nodeValue.length > 1)
			thismessage += message[l].firstChild.nodeValue;
	div = document.getElementById("statusid");
	if (thismessage.length > 1) {
		if (div != null) {
			div.innerHTML = "";
			div.innerHTML = thismessage;
			div.style.display = 'block';
			window.setTimeout('resetMsg()', 15000);
		} else
			alert(thismessage);
	}
	var javascript = req.responseXML.getElementsByTagName("javascript");
	var thisjavascript = '';
	for (var l = 0; l < javascript.length; l++)
		if (javascript[l].hasChildNodes() && javascript[l].firstChild.nodeValue.length > 1)
			thisjavascript += javascript[l].firstChild.nodeValue;
	if (thisjavascript.length > 1)
		eval(thisjavascript);
}

function resetMsg()
{
	div = document.getElementById("statusid");
	div.innerHTML = "";
	div.style.display = 'none';
}

