/*----------------------------------------------------------------------
 * Ajax class libruary
 *
 * LastUpdate : 2007/12/12
 * Write      : Pochi
/*--------------------------------------------------------------------*/

/*********************************************************************/
// [=== WARNING ===]
/*

 Request posted instance is not reuse!!

*/

/*********************************************************************/
// [=== SCRIPT SAMPLE ===]
/*

obj	= new AJAX;

obj.SetOnloadEvent(resule_callback);
obj.Open("POST","example.html",true);
obj.SetRequestHeader("Content-Type","application/x-www-form-urlencoded");
obj.Send("param1=val1&param2=val2");

function resule_callback(){
	var response	= obj.GetResponse();
}

*/

/*********************************************************************/
// [=== GLOBAL VARIABLES ===]


/*********************************************************************/
// [=== CLASS DEFINE ===]

// ajax controll class
function AJAX(){
	this.Object_		= null;
	this.ObjectType_	= 0;
	this.OpenUrl_		= null;
	
	this.FuncOnload_	= null;
	

	// Cunstructer
	this.Object_		= this.CreateHttpObject();
}

/*********************************************************************/
// [=== PRIVATE CLASS MEMBER ===]

// create HttpRequest object
AJAX.prototype.CreateHttpObject = AJAX_CreateHttpObject;
function AJAX_CreateHttpObject(){
	try{
		var	Obj	= new ActiveXObject("Msxml2.XMLHTTP");
		var Self=this;
		Obj.onreadystatechange	= function(){ Self.ResponseCallback(); }
		this.ObjectType_	= 1;
	}
	catch(e1){
		try{
			var	Obj	= new ActiveXObject("Microsoft.XMLHTTP");
			Obj.onreadystatechange	= function(){ Self.ResponseCallback(); }
			this.ObjectType_	= 2;
		}
		catch(e2){
			try{
				var	Obj	= new XMLHttpRequest();
				this.ObjectType_	= 3;
			}
			catch(e3){
				return null;
			}
		}
	}
	return Obj;
}

// status event procedure for InternetExplorer
AJAX.prototype.ResponseCallback = AJAX_ResponseCallback;
function AJAX_ResponseCallback(){
	/*
	readyState Code
		0 = uninitialized	（loading stadby）
		1 = loading			（now loading）
		2 = loaded			（loading complete）
		3 = interactive		（response data analyze）
		4 = complete		（analyze complete） 
	*/
	if(this.Object_.readyState == 4){
		//request succeeded
		if(this.FuncOnload_ != null){
			this.FuncOnload_();
		}
	}
}

// check outer url
AJAX.prototype.IsOuterUrl = AJAX_IsOuterUrl;
function AJAX_IsOuterUrl(url){
	if(url.match(/[a-z]+:\/\/.*/)){
		var	domain = String(url.match(/:\/\/[^\/]+/)).substr(3);
		if(domain != window.domain){
			return true;
		}
	}
	return false;
}

/*********************************************************************/
// [=== PUBLIC CLASS MEMBER ===]

// set onload event handler
AJAX.prototype.SetOnloadEvent = AJAX_SetOnloadEvent;
function AJAX_SetOnloadEvent(Func){
	switch(this.ObjectType_)
	{
	case 1:
	case 2:
		this.FuncOnload_	= Func;
		break;
	case 3:
		this.Object_.onload	= Func;
		break;
	default:
		return false;
	}
	return true;
}

//open() wrapper
AJAX.prototype.Open = AJAX_Open;
function AJAX_Open(Method, Path, Async, Name, Password){
	/*
	[Async]
		true  : pass function(default)
		false : wait for response
	*/
	this.OpenUrl	= null;
	if(this.Object_ != null){
		if(Async == undefined){
			Async	= true;
		}
		// access privilege for netscape
		if(this.ObjectType_ == 3 && this.IsOuterUrl(Path)){
			try{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
			}
			catch(e){
				return false;
			}
		}
		
		this.Object_.open(Method,Path,Async,Name,Password);
		this.OpenUrl	= Path;
		return true;
	}
	return false;
}

//send() wrapper
AJAX.prototype.Send = AJAX_Send;
function AJAX_Send(Content){
	if(this.Object_ != null && this.OpenUrl != null){
		this.Object_.send(Content);
	}
}

//getresponsetext getter
AJAX.prototype.GetResponse = AJAX_GetResponse;
function AJAX_GetResponse(Type){
	if(Type == undefined){
		Type	= "text";
	}
	switch(Type)
	{
	case "text":
		return this.Object_.responseText;
		break;
	case "xml":
		return this.Object_.responseXML;
		break;
	case "stream":
		return this.Object_.responseStream;
		break;
	default:
		return null;
	}
}

// get status code
AJAX.prototype.GetStatusCode = AJAX_GetStatusCode;
function AJAX_GetStatusCode(){
	if(this.ObjectType_ != 0){
		return this.Object_.status;
	}
	return false;
}

//setRequestHeader() wrapper
AJAX.prototype.SetRequestHeader = AJAX_SetRequestHeader;
function AJAX_SetRequestHeader(Key, Val){
	if(this.ObjectType_ != 0){
		this.Object_.setRequestHeader(Key,Val);
	}
	return false;
}

//getResponseHeader() wrapper
AJAX.prototype.GetResponseHeader = AJAX_GetResponseHeader;
function AJAX_GetResponseHeader(Key){
	if(this.ObjectType_ != 0){
		return this.Object_.getResponseHeader(Key);
	}
	return false;
}

//getAllResponseHeaders() wrapper
AJAX.prototype.GetAllResponseHeader = AJAX_GetAllResponseHeader;
function AJAX_GetAllResponseHeader(){
	if(this.ObjectType_ != 0){
		return this.Object_.getAllResponseHeaders();
	}
	return false;
}

//abort() wrapper (cancel for posted request)
AJAX.prototype.Abort = AJAX_Abort;
function AJAX_Abort(){
	if(this.ObjectType_ != 0){
		return this.Object_.abort();
	}
	return false;
}