//myAJAX_class - AJAX handling class - in development

function myAJAX_class(readyState4, readyState1){
	/*
	_Xstate4 and _Xstate1 are just like abstract methods. You can pass two external functions as arguments for myAJAX_class_call to be used as
	prototype methods. Warning: TESTED IN FF 3.6.8 only. Needs to be tested in Chrome, Opera and IE6,7,8
	UPDATE: it seems to be working in IE6!
	*/

	//abstract methods
	if(readyState4){ this._Xstate4 = readyState4;}
	if(readyState1){ this._Xstate1 = readyState1;}

	this.settings = Array();
	this.params = Array();
	this.LV = Array(); //local variables - use setters and getters to read them
	this.xmlHttp = false;
	this.targetLoc = false;
}

myAJAX_class.prototype._setLV = function(key, val){ this.LV[key] = val;} //to be added: if parameter1 is an array, then all vars in array will be put in LV
myAJAX_class.prototype._getLV = function(key){ return this.LV[key];}

myAJAX_class.prototype._addParam = function(key, val){
	if(!this.params[key])	this.params[key] = val;
}
myAJAX_class.prototype._addParamAuto = function(id, type){ //automatically detects input type and gets its value from an html input element
	// type must be set for the following input types:
	// radio: type=1 (id is used as name)

	var val = '';
	if(type==1){ //radio buttons
		var els = document.getElementsByName(id);
		var elsL = els.length;
		for(var a=0;a<elsL;a++){
			if(els[a].checked){ this.params[id] = els[a].value; break;}
		}
	}
	else{
		var el = document.getElementById(id);
		if(el){
			if(el.type=='checkbox'){
				this.params[id] = el.checked ? 1 : 0;
			}
			else{
				this.params[id] = el.value;
			}
		}
		else{ //element not found - dont do anything, simply ignore
			//alert('element "'+id+'" not found!'); //debug
		}
	}
}

myAJAX_class.prototype._getParam = function(key){
	return this.params[key];
}

myAJAX_class.prototype._getParams = function(){
	var params = '';
	var val = '';

	for (var i in this.params){
		if(this.params[i]!=undefined && this.params[i]!=''){
			if(params){ params += '&';}
			val = this.params[i]+"";
			val = encodeURIComponent(val);
			params += i+'='+val;
		}
	}

	return params;
}

myAJAX_class.prototype._call = function(){
	try{ this.xmlHttp = new XMLHttpRequest();} // Firefox, Opera 8.0+, Safari
	catch (e){ // Internet Explorer
		try{ this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e){
			try{ this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
			catch (e){
				this._displayError('Your browser does not support AJAX.');
				return false;
			}
		}
	}

	var xThis = this;
	this.xmlHttp.onreadystatechange = function(){
		if(xThis.xmlHttp.readyState==4){
			if(xThis.xmlHttp.responseXML){
				if(typeof xThis._Xstate4=='function') xThis._Xstate4();
			}
			else{ //display error if there's no response
				xThis._noResponseXML();
			}
		}
		else if(xThis.xmlHttp.readyState==1){
			if(typeof xThis._Xstate1=='function') xThis._Xstate1();
		}
	}

	var params = this._getParams(); //prepare all params
	//*
 	this.xmlHttp.open("POST", this._getTargetLoc(), true);
	this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	this.xmlHttp.setRequestHeader("Content-length", params.length);
	this.xmlHttp.setRequestHeader("Connection", "close");
	this.xmlHttp.send(params);//*/
}

myAJAX_class.prototype._setTargetLoc = function(loc){ this.targetLoc = loc;}
myAJAX_class.prototype._getTargetLoc = function(){ return (this.targetLoc) ? this.targetLoc : document.location;}

myAJAX_class.prototype._displayError = function(message){
	//for now its just an alert, but in the future it might be an abstract class or anything...
	alert(message);
}

myAJAX_class.prototype._noResponseXML = function(){
	//if no responseXML then display connection error
	x = document.createElement('div');
	x.className = 'responseXMLerrorMPC';
	x.style.opacity = '0.75';
	x.style.filter = 'alpha(opacity=75)';
	h1 = (window.innerHeight) ? window.innerHeight : 0;
	h2 = (document.documentElement.clientHeight) ? document.documentElement.clientHeight : 0;
	h3 = (document.body.clientHeight) ? document.body.clientHeight : 0;
	h = Math.max(h1,h2,h3);
	x.style.height = h+'px';
	document.getElementsByTagName('body')[0].appendChild(x);

	x = document.createElement('div');
	x.className = 'responseXMLerror';
	x.innerHTML = '<div class="responseXMLerrorIn">An error has occured while trying to contact the web server. Please reload the page and try again.<br>'+
	'<button class="button2" style="margin-top: 10px" onclick="window.location.href=\''+window.location.href+'\';">Reload<'+'/button><'+'/div>';
	document.getElementsByTagName('body')[0].appendChild(x);
}
