/*
The way this works is, you call makePost(base_url, parameters) to do a post.
You can make as many of these calls as you want, but it won't execute them at the same time.
It waits for the first one to complete, or 5 seconds to pass, before firing the next.
Thus they won't overlap.

Also, if you declare a function response_recieved(), that function will be called when the makepost returns
*/


function urlcode( str, method ){
	var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
	var ret = str.toString();
	
	var replacer = function(search, replace, str) {
		var tmp_arr = [];
		tmp_arr = str.split(search);
		return tmp_arr.join(replace);
	};
	histogram['!']   = '%21';
	histogram['%20'] = '+';
	
	if(method=='en'){
		// Begin with encodeURIComponent, which most resembles PHP's encoding functions
		ret = encodeURIComponent(ret);
		ret = replacer('!', '%21', ret) // Custom replace. No regexing
		ret = replacer('%20', '+', ret) // Custom replace. No regexing
		// Uppercase for full PHP compatibility
		return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
			return '%'+m2.toUpperCase();
			});
		}
	if(method=='de'){
		ret = replacer('!', '%21', ret) // Custom replace. No regexing
		ret = replacer('%20', '+', ret) // Custom replace. No regexing
		// End with decodeURIComponent, which most resembles PHP's encoding functions
		ret = decodeURIComponent(ret);
		}
	
	return ret;
	}
//for sending post requests
var http_request = false;
function makePost(base_url, parameters){
	(new timeStack()).add(base_url, parameters);
	}
function do_makePost(base_url, parameters){
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			http_request.overrideMimeType('text/javascript');
			}
		}
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {http_request=false;}
			}
		}
	if (!http_request) {
		alert('Error: Cannot save article details. Please wait a moment and then try again.');
		return false;
		}
	parameters = urlcode(parameters,'en');
	parameters = parameters.replace(/%3D/g,'=');
	parameters = parameters.replace(/%26/g,'&');
	http_request.onreadystatechange = alertContents;
	http_request.open('POST', base_url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
	parameters = urlcode(parameters,'de');
	}
function alertContents() {
	try {
		if (http_request.readyState == 4) {
			//a do_makePost call has finished:
			if (http_request.status == 200) {
				eval(http_request.responseText);
				//if the user has defined a response function, then call it
				if(self['response_recieved'])response_recieved();
				timeStack.update();
				}
			else {
				save_result='false';
				}
			}
		}
	catch(e){
		//do nothing
		}
	}



function timeStack(){}
timeStack.prototype.timerID=-1;
timeStack.prototype.delay=5000;//time, in milliseconds, between updates are permitted to occur
timeStack.prototype.stack=new Array();//stack is actually a queue...
timeStack.prototype.start=0;//this value is irrelevant; just declaring a variable as an integer
timeStack.prototype.hasData=function(){
	return (this.stack.length>0);
	}
timeStack.prototype.add=function(base_url,params){
	this.stack.unshift(new Array(base_url,params));
	//only want a single entry working
	if(this.stack.length==1){//0 plus the new entry, added (unshift to end) above
		this.start=(new Date()).getTime();
		do_makePost(base_url,params);
		}
	}
timeStack.update=function(){
	var n=new timeStack();
	n.update();
	}
timeStack.prototype.update=function(){
	var time=(new Date()).getTime()-this.start;
	if(time>=this.delay)this.do_update();
	else this.timerID=setTimeout('timeStack.update()',this.delay-time);//wait until timeout finished
	}
timeStack.prototype.do_update=function(){
	this.start=(new Date()).getTime();//reset the timer
	clearTimeout(this.timerID);
	if(this.stack.length==0)return;
	var x=this.stack.pop();//clear off the stack
	do_makePost(x[0],x[1]);//add the new post
	//if there is more to do, then set the next timer.
	}
Array.prototype.peek=function(){
	if(this.length==0)return false;
	var x=this.pop();
	this.push(x);
	return x;//*/
	}
timeStack.prototype.getNextParams=function(){
	var x=this.stack.peek();
	if(x==false)return '';
	return x[1];
	}
timeStack.prototype.setNextParams=function(val){
	if(this.stack.length==0)return;
	var x=this.stack.pop();
	this.stack.push(new Array(x[0],val));
	}
timeStack.prototype.toString=function(){
	var s="[";
	for(var i=0; i<this.stack.length; i++)s+='('+this.stack[i][0]+', '+this.stack[i][1]+')';
	return s+']';
	}


