
function AjaxRequest() {
	this.rq = false;
		try {
		  this.rq = new XMLHttpRequest();
		} catch (trymicrosoft) {
		  try {
		    this.rq = new ActiveXObject("Msxml2.XMLHTTP");
		  } catch (othermicrosoft) {
		    try {
		      this.rq = new ActiveXObject("Microsoft.XMLHTTP");
		    } catch (failed) {
		      this.rq = false;
		    }
		  }
		}
	
	if (!this.rq) {
	  alert("Cannot create XMLHttpRequest object!");
	}
	return this;
}


AjaxRequest.prototype.getResponse = function () {
  if (this.rq.readyState == 4)
    if (this.rq.status == 200) {
    		//alert(this.rq.responseText);
			if (!this.rq.responseXML) alert(this.rq.responseText);
			return this.rq.responseXML;
    } else {
    	alert('Server error: ' + this.rq.status);
    }
}

AjaxRequest.prototype.get = function (url, func) {
	this.rq.open("GET", url, true);
	this.rq.onreadystatechange = func;
	this.rq.send(null);
}

AjaxRequest.prototype.post = function (url, data, func) {
	this.rq.open("POST", url, true);
	this.rq.onreadystatechange = func;
	this.rq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.rq.send(data);
}



function Ajax() {
	this.request = new AjaxRequest();
	this.url = null;
	this.ROOT = "";
	return this;
}

/*
function Ajax.prototype.initialize(root){
	this.ROOT = root;
}
*/

Ajax.prototype.decodeHTML = function(html) {
	if (!html) return '';
	html = html.replace(/`LT`/g, '<');
	html = html.replace(/`GT`/g, '>');
	html = html.replace(/`AMP`/g, '&');
	html = html.replace(/\n+/g, "\n");
	return html;
}

Ajax.prototype.processForm = function(r) {
	if (!r) return;
	if (r.redirect)
		Dynamic.redirect(r.redirect);
	if (/*r.action == 'alert' && */r.errors.length) 
		alert(r.errors.join("\n"));
	if (/*r.action == 'message' && */r.html /*&& r.form*/)
		Dynamic.createDiv(r.form, r.html);
}


Ajax.prototype.responseForForm = function() {
	var r = this.request.getResponse(); 
	if (!r) return false;

	var obj = {html:false, form:false, redirect:false, ok:false , /*action:false,*/ errors:new Array()};
	
	
	var tags = r.getElementsByTagName('x');
	
	
	var tags = r.getElementsByTagName('html');
	if (tags.length && tags[0].firstChild)
		obj.html = this.decodeHTML(tags[0].firstChild.nodeValue);

	var tags = r.getElementsByTagName('redirect');
	if (tags.length && tags[0].firstChild)
		obj.redirect = this.decodeHTML(tags[0].firstChild.nodeValue);

	if (obj.redirect) {
		Dynamic.redirect(obj.redirect);
		return false;
	}

	//tags = r.getElementsByTagName('action');
	//if (tags.length)
	//	obj.action = tags[0].getAttribute('id');

	tags = r.getElementsByTagName('form_id');
	if (tags.length && tags[0].firstChild)
		obj.form = this.decodeHTML(tags[0].firstChild.nodeValue);

	if (obj.form)
		Dynamic.unlockForm(obj.form);

	tags = r.getElementsByTagName('ok');
	if (tags.length)
		obj.ok = true;

	tags = r.getElementsByTagName('errors');
	if (tags.length) 
	{
		tags = tags[0].childNodes;
		if (tags.length)
		{
			for (i=0; i < tags.length; i++)
			{
				if (tags[i].firstChild && tags[i].firstChild.nodeValue)
					obj.errors[obj.errors.length] = tags[i].firstChild.nodeValue;
			}
		}
	}
	

	return obj;
}

Ajax.prototype.send = function(url, callback, bRawXmlResponse) {
	var self = this;
	self.request.post(this.ROOT + "/", url, 
		callback ? 
		(
			bRawXmlResponse ?
			function () { var r_raw_xml = self.request.getResponse(); if (r_raw_xml) callback(r_raw_xml); } :
			function () { var r = self.responseForForm(); if (r) callback(r); }
		)
		: 
		function () { var r = self.responseForForm(); if (r) self.processForm(r); }
	);
}

Ajax.prototype.validateForm = function(frm, callback, bRawXmlResponse) {
	var req = 'ajax=1&rand=' + Math.random();
	if (frm.id) {
		req += '&form=' + frm.id;
		Dynamic.lockForm(frm.id);
	}

	var self = this;

	els = frm;
	for (i=0; i<els.length; i++) {
		if (els[i] && els[i].name){
			if ((els[i].type == "checkbox" || els[i].type == "radio") && !els[i].checked)
				continue;
			  req += '&' + els[i].name + '=' + self.encode(els[i].value);
		}
	}
	
			
	self.send(req, callback, bRawXmlResponse);

	return false;
}


Ajax.prototype.encode = function(txt) {
	txt = txt.replace(/&/g, '%26');
	txt = txt.replace(/;/g, '%3B');
	txt = txt.replace(/\+/g, '%2B');
	txt = txt.replace(/\?/g, '%3F');
	txt = txt.replace(/\//g, '%2F');
	txt = txt.replace(/#/g,  '%23');
	txt = txt.replace(/"/g,  '%22');

	var text = '';   
	var Ucode;
	var ExitValue;
	var s;
	var ucodes = new Array;
	ucodes[1025] = 168; ucodes[1105] = 184; ucodes[32] = 32;
	for (var i=0; i<txt.length; i++) {
		s = txt.charAt(i);
		Ucode = s.charCodeAt(0);
		var Acode = Ucode;
		if (Ucode > 1039 && Ucode < 1104) {
			Acode -= 848;
			ExitValue = "%" + Acode.toString(16);
		} 
		else {
			ExitValue = ucodes[Ucode] ? '%' + ucodes[Ucode].toString(16) : s;
		}
		text = text + ExitValue;
	}
	return text;
}


function Dynamic() {
	return this;
}

Dynamic.prototype.setDiv = function(div, content) {
	var div = document.getElementById(div);
	if (div) {
		div.className = div.className.replace(/hidden/, 'visible');
		div.innerHTML = content;
	}
}

Dynamic.prototype.createDiv = function(parent, content) {
	try {
		var eid = parent + '_response';
		var ediv = document.getElementById(eid);
		if (!ediv) {
			ediv = document.createElement('div');
			ediv.id = eid;
			var prnt = document.getElementById(parent);
			var tbls = prnt.getElementsByTagName('table');
			prnt.insertBefore(ediv, tbls.length ? tbls[0] : prnt.firstChild);
		}
		ediv.className = 'visible';
		ediv.innerHTML = content;
		return eid;
	} catch (e) {
		return false;
	}
}

Dynamic.prototype.hideDiv = function(id) {
	var d = document.getElementById(id);
	if (d && d.parentNode)
		d.parentNode.removeChild(d);
}

Dynamic.prototype.divHider = function (eid) {
	setTimeout("Dynamic.hideDiv('" + eid + "')", 5000);
}

Dynamic.prototype.redirect = function (url) {
	document.location.href = url;
}

Dynamic.prototype.lockForm = function(id) {
	var s = Dynamic.getSubmit(id);
	if (s)
		s.disabled = true;
	//window.status = "Processing...";
	//document.body.style.cursor = "wait";	
}

Dynamic.prototype.unlockForm = function(id) {
	var s = Dynamic.getSubmit(id);
	if (s)
		s.disabled = false;
	//window.status = "Done";
	//document.body.style.cursor = "";	
}

Dynamic.prototype.getSubmit = function(id) {
		var f = document.getElementById(id);
		if (!f) return false;

		for (i=0; i < f.length; i++)
			if (f[i].type && (f[i].type == 'submit' || f[i].type == 'image'))
				return f[i];

		return false;
}



var Ajax = new Ajax;
var Dynamic = new Dynamic;
