	// remote scripting library
	// (c) copyright 2005 modernmethod, inc
	var sajax_debug_mode = false;
	var sajax_request_type = "GET";

	function sajax_debug(text) {
		if (sajax_debug_mode)
			alert("RSD: " + text)
	}
	function sajax_init_object() {
		sajax_debug("sajax_init_object() called..")

		var A;
		try {
			A=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				A=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (oc) {
				A=null;
			}
		}
		if(!A && typeof XMLHttpRequest != "undefined")
			A = new XMLHttpRequest();
		if (!A)
			sajax_debug("Could not create connection object.");
		return A;
	}
	function sajax_do_call(func_name, args) {
		var i, x, n;
		var uri;
		var post_data;

		uri = "";
		if (sajax_request_type == "GET") {
			if (uri.indexOf("?") == -1)
				uri = uri + "?rs=" + encodeURIComponent(func_name);
			else
				uri = uri + "&rs=" + encodeURIComponent(func_name);
			for (i = 0; i < args.length-1; i++)
				uri = uri + "&rsargs[]=" + encodeURIComponent(args[i]);
			uri = uri + "&rsrnd=" + new Date().getTime();
			post_data = null;
		} else {
			post_data = "rs=" + encodeURIComponent(func_name);
			for (i = 0; i < args.length-1; i++)
				post_data = post_data + "&rsargs[]=" + encodeURIComponent(args[i]);
		}
		x = sajax_init_object();
		x.open(sajax_request_type, uri, true);
		if (sajax_request_type == "POST") {
			x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
			x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		x.onreadystatechange = function() {
			if (x.readyState != 4)
				return;
			sajax_debug("received " + x.responseText);

			var status;
			var data;
			status = x.responseText.charAt(0);
			data = x.responseText.substring(2);
			if (status == "-")
				alert("Error: " + data);
			else
				args[args.length-1](data);
		}
		x.send(post_data);
		sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
		sajax_debug(func_name + " waiting..");
		delete x;
	}


// wrapper for cforms_submitcomment
function x_cforms_submitcomment() {
	sajax_do_call("cforms_submitcomment",
	x_cforms_submitcomment.arguments);
}

// core cforms

function cforms_validate(no) {
	if (!no) no='';
	msgbox = 'usermessage'+no;
	document.getElementById(msgbox).className = "info";

	waiting = decodeURI(document.getElementById('_working'+no).value);
	waiting = waiting.replace(/\\/g,"");

	if( doInnerXHTML(msgbox, waiting) ) {

		var all_valid = true;
		var regexp = new RegExp('^[\\w-_\.]+@[\\w-_]+[\.][\\w-_\.]+$');

		objColl = document.getElementById('cforms'+no+'form').getElementsByTagName('*');

		for (var i = 0, j = objColl.length; i < j; i++) {
				temp = objColl[i].className;
	      newclass = 'default';

				fld = objColl[i].nodeName.toLowerCase();
				typ = objColl[i].type;

				if ( (fld == "input" || fld == "textarea") && !( typ=="hidden" || typ=="checkbox" || typ=="radio" || typ=="submit") ) {

				    if (temp.match(/required/)) {
								newclass = newclass + ' fldrequired';
								if (objColl[i].value=='') {
										newclass = newclass + ' error';
										all_valid=false;
					 			}
						} else if (temp.match(/email/)) {
								newclass = newclass + ' fldemail';
								if (objColl[i].value=='' || !objColl[i].value.match(regexp)) {
										newclass = newclass + ' error';
										all_valid=false;
					 			}
						}
						objColl[i].className = newclass;

				}
		}

			if(all_valid)
				cforms_submitcomment(no);
			else {
				err = decodeURI(document.getElementById('_failure'+no).value);

				stringDOM = '<root>' + err.replace(/(.*)(\r\n|$)/g, '<text>$1</text>') + '</root>';
				stringXHTML = err.replace(/(\r\n)/g, '<br/>');

				document.getElementById(msgbox).className = "info failure";

				doInnerXHTML(msgbox, stringXHTML.replace(/\\/g,""), stringDOM.replace(/\\/g,""));

        err = err.replace(/\\/g,"")
				if ( document.getElementById('_popup'+no).value.charAt(1) == 'y')
							alert( err );
			}

			return false;
	}
	else
		return true;
}

function doInnerXHTML(elementId, stringXHTML, stringDOM) {
	try {
		if ( !stringDOM )
		    stringDOM = '<root><text>'+stringXHTML+'</text></root>';

		var elem = document.getElementById(elementId);
		var children =  elem.childNodes;

		for (var i = 0; i < children.length; i++) {
			elem.removeChild(children[i]);
		}

		if (window.ActiveXObject) {
				var nodes = new ActiveXObject("Microsoft.XMLDOM");
				nodes.loadXML(stringDOM);
		} else {
				var nodes = new DOMParser().parseFromString(stringDOM, 'text/xml');
		}

		var ergebnisse = nodes.getElementsByTagName("text");
		var span = document.createElement("span");
		// alert("made it"); //debug

		elem.appendChild(span);

		for (var i = 0; i < ergebnisse.length; i++) {
			span.appendChild(document.createTextNode(ergebnisse[i].firstChild.nodeValue));
			if (i < ergebnisse.length) span.appendChild(document.createElement("br"));
		}
		return true;
	} catch (e) {
		try {
		// alert("debug");  //debug
			document.getElementById(elementId).innerHTML = stringXHTML;
			return true;
		}
		catch(ee) {
			return false;
		}
	}
}

function cforms_submitcomment(no) {
		var regexp = new RegExp('[$][#][$]', ['g']);
		var prefix = '$#$';

		if ( no=='' ) params = '1'; else params = no;

		objColl = document.getElementById('cforms'+no+'form').getElementsByTagName('*');

		for (var i = 0, j = objColl.length; i < j; i++) {

		    fld = objColl[i].nodeName.toLowerCase();
 				typ = objColl[i].type;

				if ( fld == "input" || fld == "textarea" || fld == "select" ) {

				if ( typ == "checkbox" ) {
						params = params + prefix + (objColl[i].checked?"X":"-");
		 		}	else
				if ( typ == "radio" && objColl[i].checked ) {
						params = params + prefix + objColl[i].value;
			 	} else
				if ( typ != "hidden" && typ != "submit" && typ != "radio") {
						params = params + prefix + objColl[i].value.replace(regexp, '$');
			 		}
		 		}
		}
		x_cforms_submitcomment(params, cforms_setsuccessmessage);
}

function cforms_setsuccessmessage(message) {
		var no = message.charAt(0);
		var pop = message.charAt(1); // check with return val from php call!

		if ( no == '1' ) no='';

		document.getElementById('cforms'+no+'form').reset();

		stringXHTML = message.substring(2,message.indexOf('|'));
		stringDOM = message.substring(message.indexOf('|')+1);
		document.getElementById('usermessage'+no).className = "info success";
		doInnerXHTML('usermessage'+no, stringXHTML, stringDOM);

		if (pop == 'y')
				alert( stringXHTML.replace(/<br\/>/g,'\r\n') );  //debug
}
