// CPAINT - Cross-Platform Asynchronous INterface Toolkit - version 1.3-SP
// Copyright (c) 2005 Boolean Systems, Inc. - http://cpaint.sourceforge.net

// $Id$
// $Log$

var cpaint_shared_httpobj;

function cpaint_get_connection_object() {
	try {
		cpaint_httpobj_temp = new ActiveXObject('Msxml2.XMLHTTP');
	} catch (e) {
		try {  
			cpaint_httpobj_temp = new ActiveXObject('Microsoft.XMLHTTP');
		} catch (oc) {
			cpaint_httpobj_temp = null;
		} 
	}
	if (!cpaint_httpobj_temp && typeof XMLHttpRequest != 'undefined') 
		cpaint_httpobj_temp = new XMLHttpRequest();
	if (!cpaint_httpobj_temp) alert('[CPAINT Error] Could not create connection object');
	return cpaint_httpobj_temp;
}

function cpaint_call() { 
	// Arguments:  {url}, {method}, {backend function name}, {argument1} ... {argumentN}, {JS callback function}, {returnType - OPTIONAL = TEXT | XML (default is 'TEXT')}
	var cpaint_args = cpaint_call.arguments, cpaint_url ='', cp_querystring = '', cp_i, cpaint_httpobj;
	var cpaint_cbfunction = '', cpaint_lastargument = 0, cpaint_returntype = '';
	if ((cpaint_args[cpaint_args.length - 1] == 'TEXT') || (cpaint_args[cpaint_args.length - 1] == 'XML')) {
		cpaint_cbfunction = cpaint_args[cpaint_args.length - 2];
		cpaint_lastargument = cpaint_args.length - 2;
		cpaint_returntype = cpaint_args[cpaint_args.length - 1];
	} else {
		cpaint_cbfunction = cpaint_args[cpaint_args.length - 1];
		cpaint_lastargument = cpaint_args.length - 1;
		cpaint_returntype = 'TEXT';
	}
	if (typeof(cpaint_use_multiple_connections) == 'undefined') cpaint_use_multiple_connections = false;
	if (typeof(cpaint_debug) == 'undefined') cpaint_debug = false;
	if (cpaint_args[0] == 'SELF') {
		cpaint_url = document.location.href;
	} else {
		cpaint_url = cpaint_args[0];
	}
	if (cpaint_returntype == 'XML') cp_querystring = cp_querystring + '&cpaint_returnxml=true';
	for (cp_i = 3; cp_i < cpaint_lastargument; cp_i++) 
		cp_querystring = cp_querystring + '&cpaint_argument[]=' + encodeURIComponent(cpaint_args[cp_i]);
	if (cpaint_args[1] == 'GET') {
		cpaint_url = cpaint_url + '?cpaint_function=' + cpaint_args[2] + cp_querystring;
	} else {
		cp_querystring = 'cpaint_function=' + cpaint_args[2] + cp_querystring;
	}
	
	if (cpaint_use_multiple_connections == true)
	{
		if (cpaint_debug == true) alert('[CPAINT Debug] Using new connection object');
		cpaint_httpobj = cpaint_get_connection_object();
	} else {
		if (cpaint_debug == true) alert('[CPAINT Debug] Using shared connection object.');
		if (typeof(cpaint_shared_httpobj) == 'undefined') {
			if (cpaint_debug == true) alert('[CPAINT Debug] Getting new shared connection object.');
			cpaint_shared_httpobj = cpaint_get_connection_object();
		}
		cpaint_httpobj = cpaint_shared_httpobj;
	}
	if (cpaint_httpobj.readyState != 4) cpaint_httpobj.abort();
	cpaint_httpobj.open(cpaint_args[1], cpaint_url, true);
	if (cpaint_args[1] == "POST") {
		try {
			cpaint_httpobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		} catch(cp_err) {
			alert('[CPAINT Error] POST cannot be completed due to incompatible browser.  Use GET as your request method.');
		}
	}
	cpaint_httpobj.onreadystatechange = function() {
		if (cpaint_httpobj.readyState != 4) return; 
		if (cpaint_debug == true) alert('[CPAINT Debug] ' + cpaint_httpobj.responseText);
		if (cpaint_returntype == 'XML') {
			cpaint_cbfunction(cpaint_httpobj.responseXML); 
		} else {
			cpaint_cbfunction(cpaint_httpobj.responseText);
		}
	}
	if (cpaint_args[1] == 'GET') {
		cpaint_httpobj.send(null);
	} else {
		cpaint_httpobj.send(cp_querystring);
	}
} 				

function cpaint_get_remote_file() {
	// Arguments:  {proxy_file}, {remoteURL}, {method}, {returnType}, {JS callback function}, {param1_name}, {param1_value} ... {paramN_name}, {paramN_value}
	// {proxy_file} = cpaint_proxy.asp | cpaint_proxy.php
	// {method} = GET | POST
	// {returnType} = TEXT | XML
	var cpaint_args = cpaint_get_remote_file.arguments;
	var cpaint_url = cpaint_args[0];
	var cp_querystring = '', cp_i = 5, cpaint_httpobj;
	if (typeof(cpaint_use_multiple_connections) == 'undefined') cpaint_use_multiple_connections = false;
	if (typeof(cpaint_debug) == 'undefined') cpaint_debug = false;
	while (cp_i <= cpaint_args.length - 1) {
		cp_querystring = cp_querystring + encodeURIComponent(cpaint_args[cp_i] + '=' + cpaint_args[cp_i + 1] + '&');
		cp_i = cp_i + 2;
	}
	if (cpaint_args[2] == 'GET') {
		cpaint_url = cpaint_url + '?cpaint_remote_url=' + encodeURIComponent(cpaint_args[1]) + '&cpaint_remote_method=' + cpaint_args[2] + '&cpaint_remote_returntype=' + cpaint_args[3] + '&cpaint_remote_query=' + cp_querystring;
	} else {
		cp_querystring = 'cpaint_remote_url=' + encodeURIComponent(cpaint_args[1]) + '&cpaint_remote_method=' + cpaint_args[2] + '&cpaint_remote_returntype=' + cpaint_args[3] + '&cpaint_remote_query=' + cp_querystring;
	}
	if (cpaint_use_multiple_connections == true)
	{
		if (cpaint_debug == true) alert('[CPAINT Debug] Using new connection object');
		cpaint_httpobj = cpaint_get_connection_object();
	} else {
		if (cpaint_debug == true) alert('[CPAINT Debug] Using shared connection object.');
		if (typeof(cpaint_shared_httpobj) == 'undefined') {
			if (cpaint_debug == true) alert('[CPAINT Debug] Getting new shared connection object.');
			cpaint_shared_httpobj = cpaint_get_connection_object();
		}
		cpaint_httpobj = cpaint_shared_httpobj;
	}
	if (cpaint_httpobj.readyState != 4) cpaint_httpobj.abort();
	cpaint_httpobj.open(cpaint_args[2], cpaint_url, true);
	if (cpaint_args[2] == "POST") {
		try {
			cpaint_httpobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		} catch(cp_err) {
			alert('[CPAINT Error] POST cannot be completed due to incompatible browser.  Use GET as your request method.');
		}
	}
	cpaint_httpobj.onreadystatechange = function() {
		if (cpaint_httpobj.readyState != 4) return; 
		if (cpaint_debug == true) alert('[CPAINT Debug] ' + cpaint_httpobj.responseText);
		if (cpaint_args[3] == 'TEXT') {
			cpaint_args[4](cpaint_httpobj.responseText); 
		} else {
			cpaint_args[4](cpaint_httpobj.responseXML); 
		}
	}
	if (cpaint_args[2] == 'GET') {
		cpaint_httpobj.send(null);
	} else {
		cpaint_httpobj.send(cp_querystring);
	}
}

function cpaint_parse_ajax_xml(xmlDocument) {
	// See documentation for usage information!
	// get only the AJAX-RESULT part of the XML document
	var ajaxResults = xmlDocument.getElementsByTagName('AJAX-RESULT');
	// get the number of results & create an array w/ that number of elements
	var xmlResult = new cpaint_result_object();
	xmlResult['AJAXRESULT'] = new Array();
	
	for (var cntNodes = 0; cntNodes < ajaxResults.length; cntNodes++) {
		var tmpNode	= cpaint_create_object_structure(ajaxResults[cntNodes]);
		tmpNode.id	= ajaxResults[cntNodes].getAttribute('ID')

		xmlResult['AJAXRESULT'].push(tmpNode);
	}

	return xmlResult;
}

function cpaint_result_object() {

	// This is the basic prototype for the object
	this.id		= 0;
	this.data	= 0;

	this.find_item_by_id = function() {
		var return_value	= null;
		var type		= arguments[0];
		var id			= arguments[1];

		if (this[type]) {

			for (var i = 0; i < this[type].length; i++) {

				if (this[type][i].id == id) {
					return_value = this[type][i];
					break;
				}
			}
		}

		return return_value;
	}
}

/**
* this method takes a HTML / XML node object and creates a
* JavaScript Object structure from it.
*
* every childNode will become a parameter of its parent node object:
* e.g.: <ul><li id="first">one</li><li id="second">two</li></ul> 
* will become something like this:
*   ul[0].id            = '';
*   ul[0].data			= '';
*   ul[0].li[0].id		= 'first';
*   ul[0].li[0].data	= 'one';
*   ul[0].li[1].id		= 'second';
*   ul[0].li[1].data	= 'two'
*
* @access	public
* @author	Dominique Stender 		<dstender@st-webdevelopment.de>
* @param	object		stream		XMLHTTPObject structure
* @return	object					a JavaScript object structure
*/
function cpaint_create_object_structure(stream) {
	var returnValue = new cpaint_result_object();
	var nodeName	= '';
	
	if (stream.hasChildNodes() == true) {

		for (var i = 0; i < stream.childNodes.length; i++) {

			nodeName = stream.childNodes[i].nodeName;
			nodeName = nodeName.replace(/[^a-zA-Z0-9]*/g, '');
			
			// reset / create subnode
			if (typeof returnValue[nodeName] != 'object') {
				returnValue[nodeName] = new Array();
			}
			
			if (stream.childNodes[i].nodeType == 1) {
				var tmpNode = cpaint_create_object_structure(stream.childNodes[i]);
				tmpNode.id	= stream.childNodes[i].getAttribute('ID')
				
				returnValue[nodeName].push(tmpNode);
			
			} else if (stream.childNodes[i].nodeType == 3) {
				returnValue.data	= stream.firstChild.data;
			}
		}
	}
	
	return returnValue;
}

