/*
version: 0.9
autor: Marc Palau
url: http://www.javascript.es | http://cali.perudatanet.com

HELP ABOUT USE: http://livedocs.macromedia.com/flash/mx2004/main_7_2/00001408.html#wp3994770
AYUDA DE USO: http://www.cristalab.com/tutoriales/6/tutorial-de-LoadVars-en-flash-mx-2004

cambios:
0.8 implementada multitarea (puede hacer tantas peticiones al servidor como se quiera simultaneamente)
0.9 perfecta compatibilidad con Flash LoadVars!

Obj
*# LoadVars class

Methods
*# LoadVars.addRequestHeader()
*# LoadVars.decode()
*# LoadVars.getBytesLoaded()
*# LoadVars.getBytesTotal()
*# LoadVars.load()
*# LoadVars.send()
*# LoadVars.sendAndLoad()
*# LoadVars.toString() 

Attributes
*# LoadVars.contentType *
*# LoadVars.loaded *

Events
*# LoadVars.onData
*# LoadVars.onLoad

*/
LoadVars=function(){
	this.loaded=false;
	this.sandlobj={};
	this.sandlobj.onData=function(){};
	this.sandlobj.onLoad=function(){};
	this.onData=function(){};
	this.onLoad=function(){};
	this.headers={};
	this.addRequestHeader("content-type","application/x-www-form-urlencoded");
	this.excepcions = ["","contentType","loaded","onData","onLoad","headers","cconex","parseVars","processOnLoad","setHeaders","getVariables","addRequestHeader","decode","load","send","sendAndLoad","toString","ruta","variables","valor","rutaArg","creaForm","addVars","loadPost","sandlobj","getBytesLoaded","getBytesTotal"];
	this.compatible=this.cconex()?true:false;
}
//Private methods
LoadVars.prototype.cconex=function(){
	this.conex = "";
	if(window.XMLHttpRequest){this.conex=new XMLHttpRequest();}
	else if(window.ActiveXObject){
		try{this.conex = new ActiveXObject("Msxml2.XMLHTTP");}
		catch(e){try{this.conex = new ActiveXObject("Microsoft.XMLHTTP");}
		catch(e){this.conex=false;}
		}
	}else{
		return false;
	}
	return true;
}
LoadVars.prototype.parseVars=function(oConex){
	this.decode(this.valor);
	this.loaded=true;
	if(oConex.sandlobj){
		try{oConex.sandlobj.onLoad(true);}catch(e){}
		try{oConex.sandlobj.onData(oConex.valor);}catch(e){}
	}
	oConex.onLoad(true);
	oConex.onData(oConex.valor);	
}
LoadVarsProcessOnLoad=function(e,obj){
	var oConex=obj;
	var cConex=e;
	if(cConex.readyState == 4){
		if(cConex.status==200){
			oConex.valor=cConex.responseText
			oConex.parseVars(oConex);
		}else if(cConex.status==404){
			alert("[processOnLoad.1] No se ha encontrado el archivo requerido: "+oConex.ruta);
			if(oConex.sandlobj){
				try{oConex.sandlobj.onLoad(false);}catch(e){}
				try{oConex.sandlobj.onData(false);}catch(e){}
			}
			oConex.onLoad(false);
			oConex.onData(false);
		}
	}
}
LoadVars.prototype.setHeaders=function(){
	for(var x in this.headers){
		var vari=x;
		var valu=this.headers[x];
		this.conex.setRequestHeader(vari,valu);
	}
}
LoadVars.prototype.addVars=function(obj){
	for(var i in obj){
		var isin=false;
		for(var j in this.excepcions){
			if(i==this.excepcions[j]){isin=true;break;}
		}
		if(!isin){this[i]=obj[i];}
	}
}
LoadVars.prototype.getVariables=function(bool){
	var retorn="";
	if(bool){var o={};}
	for(var i in this){
		var isin=false;
		for(var j in this.excepcions){
			if(i==this.excepcions[j]){isin=true;}
		}
		if(!isin){
			if(bool){o[i]=this[i];
			}else{retorn += (i + "=" + this[i] + "&");}
		}
	}
	if(bool){return o;
	}else{return retorn.substring(0,retorn.length-1);}
}
LoadVars.prototype.creaForm=function(action,objVars,method){
	var form=document.createElement("form");
	form.action=action;
	form.method=method;
	form.style.display="none";
	for(var i in objVars){
		var inp=document.createElement("input");
		inp.type="hidden";
		inp.name=i;
		inp.value=objVars[i];
		form.appendChild(inp);
	}
	return form;
}
LoadVars.prototype.loadPost=function(ruta){
	var conexio=this.conex;
	this.ruta=ruta;
	if(conexio){
		var obj=this;
		conexio.onreadystatechange=function(){
			LoadVarsProcessOnLoad(conexio,obj);
		}
		conexio.open("POST",this.ruta);
		this.setHeaders();
		var varPost=this.getVariables();
		conexio.send(varPost);
	}
}
//Public methods
LoadVars.prototype.addRequestHeader=function(variable,value){
	if(arguments.length==2){
		this.headers[variable]=value;
	}else if(arguments.length==1){
		for(var x=0;x<variable.length;x+=2){
			var vari = variable[x];
			var valu = variable[x+1];
			this.headers[vari]=valu;
		}
	}
}
LoadVars.prototype.decode=function(valor){
	if(valor.indexOf("=")!=-1){
		this.variables = valor.split("&");
		for(var i=0;i<this.variables.length;i++){
			var ara=this.variables[i];
			var primigual=ara.indexOf("=");
			var _v=ara.substring(0,primigual);
			var _k=ara.substring(primigual+1);
			this[_v]=_k;
		}
	}
}
LoadVars.prototype.load=function(ruta){
	var conexio=this.conex;
	this.ruta=ruta;
	this.rutaArg=ruta+"?"+this.getVariables();
	if(conexio){
		var obj=this;
		conexio.onreadystatechange=function(){
			LoadVarsProcessOnLoad(conexio,obj);
		}
		conexio.open("GET",this.rutaArg);
		this.setHeaders();
		conexio.send(null);
	}
}
LoadVars.prototype.send=function(ruta,target,method){
	method=method.toUpperCase();
	if(method!="GET")method="POST";
	var vars = this.getVariables(true)
	var formulari=this.creaForm(ruta,vars,method);
	formulari.target=target;
	document.body.appendChild(formulari);
	formulari.submit();
	formulari.parentNode.removeChild(formulari);
}
LoadVars.prototype.sendAndLoad=function(ruta,obj,method){
	var conexio=this.conex;
	this.conexio=conexio;
	method=method.toUpperCase();

	if(method!="GET")method="POST";
	if(method=="GET"){
		this.addVars(obj);
		this.load(ruta);
	}else{
		this.addVars(obj);
		this.loadPost(ruta);
	}

	if(obj!==this){
		this.sandlobj=obj;
	}
}
LoadVars.prototype.toString=function(){
	return this.getVariables();
}
LoadVars.prototype.getBytesLoaded=function(){
	try{
	if(this.conex.readyState > 2){
		return this.conex.responseText.length;
	}else{
		return 0;
	}
	}catch(e){}
	return false;
}
LoadVars.prototype.getBytesTotal=function(){
	try{
	var v=this.conex.getResponseHeader("Content-Length");
	if(!v){v=0;}
	return v;
	}catch(e){}
	return false;
}