/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * Basada en jshttpConexion.js FcoDiaz wariodiaz@gmail.com
 */
//jsRqst = jsRequest

function jsRqst(URL,onResponse,PostVars,autoRequest){	
	
    try {
	   this.httpR = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
	    try {
		    this.httpR = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (E) {
		    this.httpR = false;
//		    /alert('no se construllo');
	    }
    }
    if (!this.httpR && typeof XMLHttpRequest!='undefined') {
	    this.httpR = new XMLHttpRequest();
    }
    if(PostVars.tagName){
        var jsonvars=new Object();
        for(i=0;i<PostVars.length;i++){

            var campo=PostVars[i];
            if(campo.name){
                switch(campo.type){
                    default:
                        jsonvars[campo.name]=campo.value;
                    break;
                }
            }
        }
        PostVars=jsonvars;
    }    
    this.PostVars=(PostVars!=undefined)?PostVars:"";
    this.URL=URL;

    this.gethttpRon=function(){
        return this.httpR;
    }
    this.getUrl=function(){
        return this.URL+(
            (this.URL.indexOf("?")==-1)?"?":""
            )+this.jSon2UrlEncode(this.GetVars);

    }
    this.getVarsPost = function(){
        return this.jSon2UrlEncode(this.PostVars);
    }
    this.jSon2UrlEncode=function(jSon,name){
        var url="";
        for(i in jSon){        	
            if((typeof jSon[i])=='object'){
                url+=  this.jSon2UrlEncode(jSon[i],i+"[]")+"&";                
            }else
                url+=((name==undefined)?escape(i):name)+'='+escape(jSon[i])+"&";
        }        
        return url.substr(0,url.length-1);
    }
    this.onResponse=onResponse;
    this.onError=function(){
        alert("Error:"+this.httpR.status);
    }
    this.request=function(){    	 
        var evitaCache=Math.floor(Math.random() * (100000000000000));         
        var url = this.getUrl()+((this.getUrl().indexOf('?')==-1)?"?":"&")+evitaCache;        
        if(this.getVarsPost()){
        	
            this.httpR.open("POST",url,true);
            this.httpR.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); //cabeceras del post
            this.httpR.send(this.getVarsPost());
            
        }else{        	
        	this.httpR.open("GET",url,true);
            this.httpR.send(null);//hay que mandar null por que si no da broncas 
        }
        var jsRqst=this;
        var httpR=this.httpR;
      
        this.httpR.onreadystatechange=function(){        	
            if (httpR.readyState==4){            	
                if(httpR.status==200){
                    jsRqst.onResponse();
                }else{
                    jsRqst.onError();
                }
            }
        }
        this.getResponse = function(){
            return this.httpR.responseText;
        }
	}
    if(autoRequest)this.request();
    
}

jsRqst.rHTML=function(URL,div,onload,postVars){
	jsRqst(URL,function(){
		obj(div).innerHTML=this.getResponse();
		if(onload)onload();
	},((postVars==undefined)?{}:postVars),1);
}