/*
 * $Id: xml.js 115 2009-10-18 02:17:47Z rob $
 *
 * Dit werk is auteursrechtelijk beschermd.
 * Copyright (c) 2008-2009 OhReally.nl <contact@OhReally.nl>
 */

/*
 * XML handling functions
 */

// The file ajax.js should have a function displayContent() defined
// which determines how the retrieved data is displayed.
// This way this file never needs to be edited.

// AJAX should be enabled in script.js.

// The page to call for XML content.
xmlContentUrl = 'xml.php';

// Make the request.
function doRequest(method, url, action, data) {
	var httpRequest;
	try {
		httpRequest = new XMLHttpRequest();
	}
	catch(e) {
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				alert("Please upgrade your browser.");
				return false;
			}
		}
	}
	httpRequest.onreadystatechange = function() {
		switch (httpRequest.readyState) {
			case 4:
				if (httpRequest.status == 200) {
					var xmlobject = httpRequest.responseXML;
					if (! xmlobject.documentElement && httpRequest.responseStream) {
						xmlobject.load(httpRequest.responseStream);
					}
					eval(action + "(xmlobject)");
				}
				else {
					alert("'t Is stuk...\n\n" + httpRequest.status + " : " + httpRequest.statusText);
				}
				break;
			case 3:
				break;
			case 2:
				break;
			case 1:
				break;
			case 0:
				break;
		}
	}
	method = method.toUpperCase();
	httpRequest.open(method, url, true);
	if (method == "GET") {
		httpRequest.send(null);
	}
	else {
		httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpRequest.setRequestHeader("Content-length", data.length);
		httpRequest.setRequestHeader("Connection", "close");
		httpRequest.send(data);
	}
}

// Convert form data to query string.
function formData2QueryString(form) {
	var docForm = form;
	var strSubmitContent = '';
	var formElem;
	var strLastElemName = '';

	for (i = 0; i < docForm.elements.length; i++) {
		formElem = docForm.elements[i];
		switch (formElem.type) {
			// Text fields, hidden form elements
			case 'text':
			case 'hidden':
			case 'password':
			case 'textarea':
			case 'select-one':
				strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
				break;

			// Radio buttons
			case 'radio':
				if (formElem.checked) {
					strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
				}
				break;

			// Checkboxes
			case 'checkbox':
				if (formElem.checked) {
					// Continuing multiple, same-name checkboxes
					//////FIXME
					// should be beautified
					if (formElem.name == strLastElemName) {
						// Strip of end ampersand if there is one
						if (strSubmitContent.lastIndexOf('&') == strSubmitContent.length-1) {
							strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
						}
						// Append value as comma-delimited string
						strSubmitContent += ',' + escape(formElem.value);
					}
					else {
						strSubmitContent += formElem.name + '=' + escape(formElem.value);
					}
					strSubmitContent += '&';
				}
				break;
		}
		strLastElemName = formElem.name;
	}

	// Remove trailing separator
	strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
	return strSubmitContent;
}

// Unused.
function docRequest(method, url, action, query, form) {
	method = method.toUpperCase();
	if (form) {
		var data = formData2QueryString(form);
	}
	if (method == 'GET') {
		if (query && data) {
			query = query + '&' + data;
			data = null;
		}
	}
	if (query) {
		if (query.substr(0, 1) != '?') {
			query = '?' + query;
		}
	}
	doRequest(method, url + query, action, data);
}

// Prepare for a POST request.
function postContent(form, id, moreopts) {
	var data = formData2QueryString(form);
	var qry = '?';
	if (id) {
		qry = '?' + pageName + '=' + id;
	}
    if (moreopts) {
        qry = qry + '&' + moreopts;
    }
    doRequest('POST', xmlContentUrl + qry, 'displayContent', data);
}

// Prepare for a GET request.
function getContent(id, moreopts) {
    var qry = '?';
    if (id) {
        qry = '?' + pageName + '=' + id;
    }
    if (moreopts) {
        qry = qry + '&' + moreopts;
    }
    doRequest('GET', xmlContentUrl + qry, 'displayContent');
}
