// CPAINT (Cross-Platform Asynchronous INterface Toolkit) - Version 0.7
// Copyright (c) 2005 Boolean Systems, Inc. - www.booleansystems.com

var cpaint_shared_httpobj;
// var cpaint_use_multiple_connections = true;

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) Could not create connection object');
	return cpaint_httpobj_temp;
}


function cpaint_call() { 
	/* Parameters:  
		See documentation for parameters
	*/
	var cpaint_args, cpaint_url, cp_querystring, cp_i, cpaint_httpobj;
	cpaint_args = cpaint_call.arguments;

	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];
	}
	cp_querystring = '';
	for (cp_i = 3; cp_i < cpaint_args.length - 1; cp_i++)
		cp_querystring = cp_querystring + '&cpaint_argument[]=' + escape(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 Debug] Request cannot be completed due to incompatible browser (Opera). Use GET rather than POST.');
		}
	}

	cpaint_httpobj.onreadystatechange = function() {
		if (cpaint_httpobj.readyState != 4) {
			return; 
			}
		var cpaint_status, cpaint_data;
		cpaint_status = cpaint_httpobj.responseText.charAt(0);
		cpaint_data = cpaint_httpobj.responseText;
		if (cpaint_debug == true) alert('[CPAINT Debug] ' + cpaint_data);
		var data_pos = cpaint_data.indexOf('[cpaint_result]');
		if(cpaint_data != ""){
			cpaint_args[cpaint_args.length-1](cpaint_data.substring(1,data_pos)); 
		}
	}

	if (cpaint_args[1] == 'GET') {
		cpaint_httpobj.send(null);
	} else {
		cpaint_httpobj.send(cp_querystring);
	}

}