/* Ajax stuff */

/**
/* Ajax object for multiple Ajax instances
*/

function callAjax() {
	var xmlreqs=new Array();
	//this.xmlreqs=xmlreqs;
	this.CXMLReq=CXMLReq;
	this.xmlreqGET=xmlreqGET;
	this.xmlreqPOST=xmlreqPOST;
	this.xmlhttpChange=xmlhttpChange;

	function CXMLReq(type,xmlhttp) {
		this.type=type;
		this.xmlhttp=xmlhttp;
	}

	function xmlreqGET(url) {
		this.xmlhttp=false;
		if (window.XMLHttpRequest) { // Mozilla, etc.
			this.xmlhttp=new XMLHttpRequest();
			this.xmlhttp.onreadystatechange = xmlhttpChange;
			this.xmlhttp.open("GET",url,true);
			this.xmlhttp.send(null);
		} else if (window.ActiveXObject) { // IE
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			if (this.xmlhttp) {
				this.xmlhttp.onreadystatechange = xmlhttpChange;
				this.xmlhttp.open("GET",url,true);
				this.xmlhttp.send();
			}
		}

		xmlreq = new CXMLReq('', this.xmlhttp);
		this.xmlreq=xmlreq;
		xmlreqs.push(this.xmlreq);
	}

	function xmlreqPOST(url,str) {
		this.xmlhttp=false;
		if (window.XMLHttpRequest) { // Mozilla, etc.
			this.xmlhttp=new XMLHttpRequest();
			this.xmlhttp.onreadystatechange = xmlhttpChange;
			this.xmlhttp.open("POST",url,true);
			this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			this.xmlhttp.send(str);
		} else if (window.ActiveXObject) { // IE
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			if (this.xmlhttp) {
				this.xmlhttp.onreadystatechange = xmlhttpChange;
				this.xmlhttp.open("POST",url,true);
				this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
				this.xmlhttp.send(str);
			}
		}
		xmlreq = new CXMLReq('', this.xmlhttp);
		this.xmlreq=xmlreq;
		xmlreqs.push(this.xmlreq);
	}

	function xmlhttpChange() {
		//if (typeof(window['xmlreqs']) == "undefined") return;
		for (var i=0; i < xmlreqs.length; i++) {
			if (xmlreqs[i].xmlhttp.readyState == 4) {
				if (xmlreqs[i].xmlhttp.status == 200 || xmlreqs[i].xmlhttp.status == 304) {
					// 200 OK
					// get response info here before splicing - see below on creating an xml object
					this.result=xmlreqs[i].xmlhttp.responseText;
					//alert("Got result");
					eval(this.result);
					//Process result here
					//alert(this.result);
					xmlreqs.splice(i,1);
					i--;
				} else {
					// error
					xmlreqs.splice(i,1);
					i--;
				}
			}
		}
	}
}

function catchClick(url,reqtype,str) {
	ajax=new callAjax();
	if (reqtype!="post") {
		ajax.xmlreqGET(url);
	} else {
		ajax.xmlreqPOST(url,str);
	}
}

function changeInner(senderid,newval) {
	sender=document.getElementById(senderid);
	sender.innerHTML=newval;
}

function changeImg(senderid,newval) {
	sender=document.getElementById(senderid);
	sender.src=newval;
}

function switchInner(sender,val1,val2) {
	if (sender.innerHTML==val1) {
		changeInner(sender,val2);
	} else {
		changeInner(sender.val1);
	}
}

function encodeForm(formid) {
	f=document.getElementById(formid);
	s="";
	opts=new Array();
	for(x=0;x<f.elements.length;x++) {
		if (f.elements[x].value) {
			if (f.elements[x].type=="checkbox") {
				if (f.elements[x].checked) {
					opts[opts.length]=f.elements[x].name+"="+escape(encodeURIComponent(f.elements[x].value));
				}
			} else {
				opts[opts.length]=f.elements[x].name+"="+escape(encodeURIComponent(f.elements[x].value));
			}
		} else {
			opts[opts.length]=f.elements[x].name+"=";
		}
	}
	s=opts.join("&");
	return s;
}