function form2array(form){
	var array = new Array;
	for(var x=0;x<form.length;x++){
		if(form[x].type != "button" && form[x].type != "submit") array[form[x].name] = form[x].value;
	}
	return array;
}

function connection(){
	try{
		this.xmlhttp = new XMLHttpRequest();
	}
	catch(e){
  		try{
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    	}
		catch(e){
			try{
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				alert("Please use a compatible browser");
				this.xmlhttp = false;
			}
		}
	}
	var _this = this;
	this.xmlhttp.onreadystatechange = function(){_this.handler();}	
	this.handler = function(){
		if(this.xmlhttp.readyState==4 && this.xmlhttp.status==200){
			if(typeof(this.dumpster) == "string") document.getElementById(this.dumpster).innerHTML = this.xmlhttp.responseText;
			else if(typeof(this.dumpster) == "object") this.dumpster.processXML(this.xmlhttp.responseXML);
			this.reload();
		}
		else this.displayLoading();
	}
	this.reload = function(){
		if(this.reconnect != undefined){
			if(this.lastaction == "get") this.get();
			else if(this.lastaction == "post") this.post();
		}
	}
	this.displayLoading = function(){
		if(this.hasdump == undefined && this.dumpster != undefined){
			document.getElementById(this.dumpster).innerHTML = "<center><i>Loading...</i></center>";
			this.hasdump = true;
		}
	}
	this.settings = function(url,dumpster,reconnect){
		if(this.url != url && url != undefined) this.url = url;
		if(this.dumpster == undefined){
			this.dumpster = dumpster;
			this.async = true;
		}
		else this.async = false;
		if(this.reconnect == undefined) this.reconnect = reconnect;
	}
	this.get = function(url,dumpster,reconnect){
		this.settings(url,dumpster,reconnect);
		this.xmlhttp.open("GET",this.url,true);
		this.xmlhttp.send();
		this.lastaction = "get";
	}
	this.post = function(url,array,dumpster,reconnect){
		this.settings(url,dumpster);
		var data = "";
		for(x in array){
			if(data != "") data += "&";	
			data += x+'='+encodeURIComponent(array[x]);
		}
		this.xmlhttp.open("POST",this.url,true);
		this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		this.xmlhttp.send(data);
		this.lastaction = "post";
	}
	this.postform = function(url,form,dumpster,reconnect){
		this.post(url,form2array(form),dumpster,reconnect);
	}
}

function chat(){
	var socket = new connection();
	var socket2 = new connection();
	this.load = function(){
		socket.get(this.room,"output",true);
	}
	this.get_room = function(room){
		this.room = "/chat/?id="+room;	
		this.load();
	}
	this.post = function(){
		socket2.postform(this.room,document.talk);
		document.talk.post.value = "";
	}
	this.remove = function(entry){
		if(confirm("Are you sure you wanna delete this post?")){
			socket2.get(this.room+"&delete="+entry);
		}
	}
}

function component(param,cont){
	var socket =  new connection();
	var bclass = "small_button";
	this.param = param;
	this.param['offset'] = 0;
	this.cont = document.getElementById(cont);
	this.next_button = this.cont.childNodes[2].firstChild.firstChild.childNodes[0].firstChild.firstChild;
	this.previous_button = this.cont.childNodes[2].firstChild.firstChild.childNodes[1].firstChild.firstChild;
	this.load = function(){
		socket.post("/",this.param,this);
	}
	this.processXML = function(xml){
		this.cont.childNodes[1].innerHTML = "";
		node = document.importNode(xml.documentElement,true);
		this.cont.childNodes[1].appendChild(node);
		this.cont.childNodes[1].innerHTML = this.cont.childNodes[1].innerHTML;
	}
	this.next = function(){
		this.param['offset'] -= 1;
		this.load();
		this.enable_previous();
		this.check_buttons();
	}
	this.previous = function(){
		this.param['offset'] += 1;
		this.load();
		this.enable_next();
		this.check_buttons();
	}
	this.check_buttons = function(){
		if(this.param['offset'] <= 0) this.disable_button(this.next_button);
		if(this.param['quantity'] >=  this.param['count'] - (this.param['quantity']*this.param['offset'])) this.disable_button(this.previous_button);
	}
	this.enable_next = function(){
		if(this.param['invert'] != 1) this.next_button.nodeValue = "next";
		else this.next_button.nodeValue = "previous";
		this.next_button.parentNode.className = bclass;
	}
	this.enable_previous = function(){
		if(this.param['invert'] != 1) this.previous_button.nodeValue = "previous";
		else this.previous_button.nodeValue = "next";
		this.previous_button.parentNode.className = bclass;
	}
	this.disable_button = function(button){
		button.nodeValue = "";
		button.parentNode.className = "";
	}
	this.edit = function(id){
		 window.location.assign("/submit?doc="+id);
	}
	this.remove = function(id,type){
		if(confirm("Are you sure you wanna delete this post?")){
			var array = new Array;
			array['doc'] = id;
			array['type'] = type;
			array['button'] = "delete";
			socket.post("./",array);
			this.load();
		}
	}
	if(this.param['count']){
		this.enable_next();
		this.enable_previous();
		this.check_buttons();
	}
	this.load();
}

function vote(id,search,pick){
	if(id != undefined){
		var socket =  new connection();
		var url = "/search/?id="+id+"&s="+search;
		if(pick==true){
			url += "&t=1";
			document.getElementById('pick_'+id).src = "/design/picked.jpg";
		}
		socket.get(url);
	}else alert("You need to login to pick");
}

function upload(){
//	document.getElementById("upload_button").disabled = true;
}

var chat = new chat();
