
function sack(file){
	this.AjaxFailedAlert = "The browser you use is not supported.";
	this.requestFile = file;
	this.method = "POST";
	this.block_screen=null;
	this.URLString = "";
	this.encodeURIString = true;
	this.execute = false;
	this.element="";
	this.filesupload=Array();
	this.onLoading = function() { };
	this.onLoaded = function() { };
	this.onInteractive = function() { };
	this.onCompletion = function() { };
	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			

		} catch (e) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err) {
				this.xmlhttp = null;
			}
		}
		if(!this.xmlhttp && typeof XMLHttpRequest != "undefined")
			this.xmlhttp = new XMLHttpRequest();
		if (!this.xmlhttp){
			this.failed = true; 
		}
	};
	
	this.blo_screen = function(){
			//about:blank;
			
			this.iframe = document.createElement("iframe");
			this.iframe.src='about:blank';
			s = this.iframe.style;
			s.position = "absolute";
			s_w=this.block_screen.offsetWidth
			s.width= s_w +"px";
			s_h=this.block_screen.offsetHeight;
			s.height= s_h +"px";
			s_l=offsetLeft(this.block_screen);
			s.left = s_l  +"px";
			s_t =  offsetTop(this.block_screen);
			s.top = s_t +"px";
			//alert(s.width +  " "+ s.height +" " + s.left + " " +s.top);
			s.filter = "Alpha(Opacity="+ 0+")";
			s.zIndex = 6;
			s.display = "";
			document.body.appendChild(this.iframe);
			
			this.aj_screen = document.createElement("DIV");
			this.aj_screen.style.width="300px";
			this.aj_screen.style.height="30px";
			this.aj_screen.style.top= (( s_h-30)/2 + s_t) +"px";
			this.aj_screen.style.left= ((s_w- 300)/2) + s_l + "px";
			this.aj_screen.align="center";
			this.aj_screen.style.position="absolute";
			this.aj_screen.style.padding="3px";
			//this.aj_screen.style.filter = "Alpha(Opacity="+ 0+")";
			this.aj_screen.style.background="#CCCCCC";
			this.aj_screen.style.zIndex="1";
			this.aj_screen.innerHTML ='<b>please wating ...</b><br><img src="images/ajax-loader.gif">';
			document.body.appendChild(this.aj_screen);
	
	}
	
	this.setVar = function(name, value){
		if (this.URLString.length < 3){
			this.URLString = name + "=" + value;
		} else {
			this.URLString += "&" + name + "=" + value;
		}
	}
	this.setFile = function(name, value){
		this.filesupload[name] = value;
	}
	this.setVar_from_form = function(theForm){
		
		for (i = 0; i < theForm.length; i++){

			if(theForm.elements[i].type=="radio" || theForm.elements[i].type=="checkbox"){
				if(theForm.elements[i].checked) this.setVar(theForm.elements[i].name,theForm.elements[i].value);
			}
			if(theForm.elements[i].type=="file"){
				this.setFile(theForm.elements[i].name,theForm.elements[i].value);
			}
			else{
				this.setVar(theForm.elements[i].name,theForm.elements[i].value);
				//alert(theForm.elements[i].value);
			}
				
		}
	}
	this.encVar = function(name, value){
		var varString = encodeURIComponent(name) + "=" + encodeURIComponent(value);
	return varString;
	}
	
	this.encodeURLString = function(string){
		varArray = string.split('&');
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split('=');
			if (urlVars[0].indexOf('amp;') != -1){
				urlVars[0] = urlVars[0].substring(4);
			}
			varArray[i] = this.encVar(urlVars[0],urlVars[1]);
		}
	return varArray.join('&');
	}
	
	this.runResponse = function(){
		eval(this.response);
	}

	this.runAJAX = function(urlstring){
		
		this.responseStatus = new Array(2);
		if(this.failed && this.AjaxFailedAlert){ 
			alert(this.AjaxFailedAlert); 
		} else {
			if (urlstring){ 
				if (this.URLString.length){
					this.URLString = this.URLString + "&" + urlstring; 
				} else {
					this.URLString = urlstring; 
				}
			}
			if (this.encodeURIString){
				var timeval = new Date().getTime(); 
				this.URLString = this.encodeURLString(this.URLString);
				this.setVar("rndval", timeval);
			}
			
			if (this.element) {
				this.elementObj = document.getElementById("div_list_portlet"); 
			}
			if (this.xmlhttp) {
				
				var self = this;
				if (this.method == "GET") {
					var totalurlstring = this.requestFile + "?" + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					   /*ado_stream = new ActiveXObject("ADODB.Stream");
					   try {
							xml_dom = new ActiveXObject("MSXML2.DOMDocument");
						} catch (e) {
							try {
								xml_dom = new ActiveXObject("Microsoft.DOMDocument");
							} catch (err) {
								xml_dom = null;
							}
						}
					   
					   xml_dom.loadXML('<?xml version="1.0" ?> <root/>');
					   xml_dom.documentElement.setAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes");
					   l_node1 = xml_dom.createElement("file1");
					   l_node1.dataType = "bin.base64";
					   ado_stream.Type = 1;  // 1=adTypeBinary 
					   ado_stream.Open();
					   ado_stream.LoadFromFile("C:\\project_sacomreal\\touch_sacomreal\\images\\no_images.gif");
					   l_node1.nodeTypedValue = ado_stream.Read(-1); // -1=adReadAll
					   ado_stream.Close();
					   xml_dom.documentElement.appendChild(l_node1);*/
					   this.xmlhttp.open(this.method, this.requestFile, true);
					  // this.xmlhttp.send(xml_dom);

				}
				if (this.method == "POST"){
  					try {
						this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')  ;
						this.xmlhttp.setRequestHeader('Pragma','no-cache')  ;
						} catch (e) {}
				}

				this.xmlhttp.send(this.URLString);
				if(this.block_screen!=null){
					this.blo_screen();
				}
				//this.xmlhttp.send(this.URLString);
				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState){
						case 1:
							self.onLoading();
						break;
						case 2:
							self.onLoaded();
						break;
						case 3:
							self.onInteractive();
						break;
						case 4:
							
							if(self.block_screen!=null){
								document.body.removeChild(self.iframe);
								document.body.removeChild(self.aj_screen);
								
							}
							
							//self.iframe.style.display="none";
							self.error=true;
							self.response="";
							self.content_return ="";
							if(self.xmlhttp.status==200){
								self.response = self.xmlhttp.responseText;
								error=self.response.match("<error>(.*)</error>");
								self.content_return ="";
								if(error){
									if(error[1]==0){
										pos1=self.response.indexOf("<content_return>");
										pos2=self.response.indexOf("</content_return>");
										if(pos1<pos2) self.content_return =self.response.substr(pos1, pos2-pos1);
										else self.content_return ="";
										self.error=false;
									}
									else{
										self.message="";
										self.error=error[1];
									}
								}
								else{
									self.error=1000;
									self.message=self.response ;
									
								}
													
								self.responseXML = self.xmlhttp.responseXML;
								self.responseStatus[0] = self.xmlhttp.status;
								self.responseStatus[1] = self.xmlhttp.statusText;
								self.onCompletion();
								if(self.execute){ self.runResponse(); }
								if (self.elementObj) {
									var elemNodeName = self.elementObj.nodeName;
									elemNodeName.toLowerCase();
									if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea"){
										self.elementObj.value = self.content_return;
									} else {
										self.elementObj.innerHTML = self.content_return;
										
									}
								}
							}
							else{
								self.message= "làm ơn kiểm tra lại đường truyền internet của bạn.";
							}
							
							self.URLString = "";
						break;
					}
				};
			}
		}
	};
this.createAJAX();
}