/*
created by : จักรัฐ ภู่สวาสดิ์
email : jakarut@gmail.com
version : 3.51
updated date : 2007-11-17
*/

function request(url, target){
	var obj = new ajax("get", url);
	obj.onSuccess = function(e){
		if (typeof(target)=="object"){
			target.innerHTML = e.text;
		}else if(typeof(target)=="string"){
			document.getElementById(target).innerHTML = e.text;
		}
	}
	obj.call();
}

/* ------------------------------------------------- */
// Detection variables..
var is_ie=(window.ActiveXObject)?true:false;
var is_moz=(document.implementation&&document.implementation.createDocument)?true:false;

/* ------------------------------------------------- */

function ajax(method , url) {
  
	var self = this;
	  
	/*----------------------------------*/
	//functions and properties definition
	/*----------------------------------*/
	this.url = url;
	this.method = method
	this.param = new Array();
	this.addParam = addParam;
	this.getParam = getParam;
	this.clearParam = clearParam;
	this.loading = false;
	this.onLoading = function(){};
	this.onSuccess = function(){};
	this.onFailure = function(){};
	this.onAbort = function(){};
	this.call = call_v3;	
	

	/*----------------------------------*/
  
	function createAJAX(){
		try{return new ActiveXObject('Msxml2.XMLHTTP');}catch(e){}
		try{return new ActiveXObject('Microsoft.XMLHTTP');}catch(e){}
		try{return new XMLHttpRequest;}catch(e){}
		
		alert("Your browser does not support AJAX!");
		return ;				
	}//end function
	
	
	/*
	function abort() {
		if (self.loading) {
			self.loading=false;
			self.AJAX.abort();
			self.AJAX=null;
		}
	}//end function
	*/
  
  	function addParam(name, value){
		this.param.push(name + "=" + value);
	}//end function


	function getParam(param){
		var q = "";
		if(param.length > 0){
			for(i=0; i<param.length; i++){
				q += param[i]+"&"
			}			
			q = q.substr(0, q.length-1);
		}
		return q;		
	}//end function
	
	
	function clearParam(){
		this.param.splice(0,this.param.length);	
	}
	
    function call_v3(settings) { 
    	
    	if (self.loading) {return false;}
		
		//Creating XMLHTTP object.
		this.AJAX = null;
		this.AJAX = createAJAX();
			
		if (this.AJAX==null) {
			return false;                               
		} else {

			//Binding events.
			//this.onLoading	= (settings.onLoading)	? settings.onLoading	: function(){};
			//this.onSuccess	= (settings.onSuccess)	? settings.onSuccess	: function(){};
			//this.onFailure	= (settings.onFailure)	? settings.onFailure	: function(){};
			//this.onAbort	= (settings.onAbort)	? settings.onAbort		: function(){};
			
			var isContinue;
			isContinue = this.onLoading();
			if (isContinue==false) {
					this.onAbort();					
					return false;
			}
			
			//Get parameters formating.
			var parameters = getParam(this.param);
			
			this.loading = new Date();

			this.AJAX.onreadystatechange = this.processResponse;
			
			
			if (!this.method){ this.method = "GET"; }
			if(this.method.toUpperCase() == "GET"){
				if (parameters!="")	{ parameters += "&"; }
				this.url += (this.url.match(/\?/) ? '&' : '?') + parameters + "rnd="+Math.random();
				parameters = null;
				this.AJAX.open(this.method, this.url, true);
				//this.AJAX.setRequestHeader("Connection","close");
				this.AJAX.send(null);
				
			} else if(this.method.toUpperCase()=="POST"){				
				this.AJAX.open(this.method, this.url, true);
				this.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
				//this.AJAX.setRequestHeader("Connection","close");
				this.AJAX.send(parameters);
			}
			
			return true;
		}
    }// end send

    
	this.processResponse = function() {
		if (self.AJAX.readyState == 4){
		
			var transport	= {				
				headers		: self.AJAX.getAllResponseHeaders(),
				status			: self.AJAX.status,
				statusText	: self.AJAX.statusText,
				text				: self.AJAX.responseText,
				xmldoc			: self.AJAX.responseXML
			}

			if (self.AJAX.status == 200) {
				self.loading = false;
				self.onSuccess(transport);
			} else {
				self.onFailure(transport);
			}
			
			self.AJAX = null;
		}
	} // end function
	
	
}//end class


/* ---------------------------------------------------------------------------- */
	// XSLTransformer CLASS
	/* ---------------------------------------------------------------------------- */
	function XSLTransformer(){
			
			this.xslt;
					
			//Assign XSL template.
			this.load = function(url){
				
				if (is_ie) {
						var xslt = new ActiveXObject("Msxml2.XSLTemplate");						
						var xsldoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
						xsldoc.async = false;
						xsldoc.load(url);
						xslt.stylesheet = xsldoc;
						this.xslt = xslt;
				} else if(is_moz) {
						var xmlDoc;
						var httpreq = new XMLHttpRequest();
						httpreq.open("GET", url , false);
						httpreq.send(null);
						this.xslt = httpreq.responseXML;					
				}
				
			}
			
			this.xslParameters = new Array();
			this.addPara = function(paraName , paraValue){
				if ( typeof(paraValue) != "undefined" ){
					this.xslParameters[paraName] = paraValue;
				}else{
					this.xslParameters[paraName] = null;
				}				
			}
			
			this.delPara = function(paraName){
				delete this.xslParameters[paraName];
			}
			
			this.transform = function(xmldoc) {
				if (this.xslt){
				
					var xsltProcessor;					
					if (is_ie){
							xsltProcessor = this.xslt.createProcessor();
							xsltProcessor.input = xmldoc;
							//Set parameters
							for (var item in this.xslParameters){
								xsltProcessor.addParameter(item, this.xslParameters[item]);
							}							
							xsltProcessor.transform();
							return xsltProcessor.output;
					}else if(is_moz){
							xsltProcessor = new XSLTProcessor();
							xsltProcessor.importStylesheet(this.xslt);
							// set the parameter using the parameter passed to the outputgroup function
							for (var item in this.xslParameters){
								xsltProcessor.setParameter(null , item, this.xslParameters[item]);
							}
							var div = document.createElement("div");
							div.appendChild( xsltProcessor.transformToFragment(xmldoc , document) );
							return div.innerHTML;
					}
				}
			}			
	}
	/* ---------------------------------------------------------------------------- */
