/*
==============================================================================================
ÇÁ·Î±×·¥¸í :  ezAjax
¸¸µç³¯     :  2005.12.13
¼³¸í       :  Ajax »ç¿ë½Ã 4°¡Áö ¹æ¹ý(µ¿±â get¹æ½Ä, ºñµ¿±â get¹æ½Ä, µ¿±â post¹æ½Ä, ºñµ¿±â post¹æ½Ä)
			  À» ÀÚÀ¯·Ó°Ô »ç¿ëÇÏ±â À§ÇØ 

 	>>>>>>>>>>>>>>>>>>>>>>>>   [ »ç¿ë¹ý ]  <<<<<<<<<<<<<<<<<<<<<<<<<<
 	<script src="ezAjax.js"></script>
	<script>
	function toAjax(){
		ajaxObj.setMode("get",false);	// get¹æ½ÄÀÇ ºñµ¿±â(true - µ¿±â, false - ºñµ¿±â)

		// °á°ú°ªÀ» Æ¯Á¤ÇÔ¼ö¿¡¼­ Á÷Á¢ Ã³¸®ÇÏ±â À§ÇØ ÇÔ¼ö¸í ¼¼ÆÃ
		ajaxObj.setFunction("testFunc()");
		
		// Ã³¸®ÇÒ url À» ¼¼ÆÃ, get ¹æ½ÄÀÏ°æ¿ì ³Ñ±æ ÀÎÀÚ°¡ ÀÖÀ¸¸é "?" µÚ¿¡ ÀÎÀÚ°ªµéÀ» ³Ñ±ä´Ù
		ajaxObj.setUrl("./test.php?id=test");

		// post ¹æ½ÄÀÏ°æ¿ì¿¡¸¸ ÀÌ ÇÔ¼ö¸¦ »ç¿ëÇÏ¿© ³Ñ±æ ÀÎÀÚ¸¦ ¼¼ÆÃ
		//ajaxObj.setParam("id=test");

		ajaxObj.execute();	// ajax ½ÇÇà, °á°ú°ªÀÌ ajaxObj.returnVar ¿¡ ´ã±ä´Ù
	}

	// °á°ú °ªÀ» Á÷Á¢ Ã³¸®ÇÒ »ç¿ëÀÚ Á¤ÀÇ ÇÔ¼ö
	function testFunc(){
		// °á°ú °ªÀÌ µé¾î°£ returnVar ¸¦ ÀÌ¿ëÇÏ¿© ¿øÇÏ´Â °á°ú¹°·Î Ã³¸®
		alert(ajaxObj.returnVar);
	}
	</script>
	>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

*** À¯ÀÇ»çÇ× ***
header("Content-Type: text/html; charset=EUC-KR"); // ÇÑ±Û Ã³¸®´Â jsp ¿¡¼­ ÇØÁà¾ß 
°³¹ß½Ã¿¡ this.debug ¸¦ true ·Î ÇÏ½Ã¸é ÇÁ·Î¼¼½º¸¦ Ã³¸®ÇÏ´Â ÆäÀÌÁö¿¡¼­ ÂïÀº ¿¡ÄÚ°ªµéÀÌ ajax¸¦ µ¹¸®´Â 
ÆäÀÌÁö ÇÏ´Ü¿¡ »Ñ·ÁÁý´Ï´Ù. 
°³¹ß½Ã¿¡¸¸ true ·Î ÇÏ½Ã°í, ½ÇÁ¦ »ç¿ëÇÒ¶§¿¡´Â this.debug °ªÀ» false ·Î ¼¼ÆÃÇÏ¼¼¿ä
==============================================================================================
*/

function _NewObject(e) {return document.createElement(e);}

	// 2007.09.27 : jsp¿¡¼­´Â  ¸®ÅÏ ¹ÞÀ»½Ã °³Çà ¹× °ø¹éÃ³¸®°¡ ¾ÈµÈ´Ù. 
	function trimStr(str){
		var TRIM_PATTERN = /(^\s*)|(\s*$)/g; // ³»¿ëÀÇ °ªÀ» ºó°ø¹éÀ» trimÇÏ±â À§ÇÑ°Í(¾Õ/µÚ)
		return str.replace(TRIM_PATTERN, "");  
	}

	function AjaxObj(){
		this.req = "";			// xmlhttp °´Ã¼
		this.url = "";			// Ã³¸® url
		this.method = "";		// Àü¼Û¹æ¹ý
		this.sync = "";			// ½ÌÅ© ¹æ¹ý - µ¿±â:true, ºñµ¿±â:false
		this.returnVar = "";	// Ã³¸® url ¿¡¼­ ¹ÞÀº °á°ú°ª
		this.param = "";		// url ¿¡ ³Ñ±æ ÀÎÀÚ°ªµé ex. "id=1111&name=hong"
		this.resultFunc = "";	// °á°ú°ªÀ» °®°í Ã³¸®ÇÒ »ç¿ëÀÚ°¡ Á¤ÀÇÇÑ ÇÔ¼ö¸í
		this.debug = false;		// µð¹ö±×¸¦ ÇÒÁö ¾ÈÇÒÁö¿©ºÎ. true ÀÌ¸é div °´Ã¼¸¦ ¸¸µé¾î °á°ú¸¦ ´Ü¼øÈ÷ Âï´Â´Ù
	
		// Àü¼Û¹æ¹ý°ú ½ÌÅ© ¹æ¹ý °áÁ¤
		this.setMode = function(method,sync){
			this.method = method;
			this.sync = sync;
	};

	// xmlhttp °´Ã¼ ¾ò±â
	this.getRequest = function(){
		try {
			this.req = new ActiveXObject("Msxml2.XMLHTTP") ;
		}
		catch (e) {
			try {
				this.req = new ActiveXObject("Microsoft.XMLHTTP") ;
			}
			catch (e) 
			{	
				try {
					this.req = new XMLHttpRequest();
					/*			
					if(window.XMLHttpRequest){
						this.req = new XMLHttpRequest();
					}else {
					 	this.req = false ;
					}*/
				}
				catch(e) 
				{
					this.req = false ;
				}			
			}
		}
		
		if (!this.req && window.XMLHttpRequest) 
			this.req = false;
			//this.req = new XMLHttpRequest();
	}
	
	// Ã³¸® url ¼¼ÆÃ
	this.setUrl = function(url){
		this.url = url;
	}

	// url ¿¡ ³Ñ±æ ÀÎÀÚ°ª ¼¼ÆÃ
	this.setParam = function(param){
		this.param = param;
	}

	// °á°ú¸¦ Ã³¸®ÇÒ »ç¿ëÀÚ Á¤ÀÇ ÇÔ¼ö¸í
	this.setFunction = function(fname){
		this.resultFunc = fname;
	}	

	// ajax ½ÇÇà
	this.execute = function(){
		if(this.url==""){
			alert("½ÇÇàÀü¿¡ ¸ÕÀú setUrl()À» ÀÌ¿ëÇÏ¿© urlÀ» ¼¼ÆÃÇØÁÖ¼¼¿ä");
			return;
		}

		this.getRequest();

		if (this.req) {
			// µ¿±âÀÏ¶§
			if(this.sync){
				this.req.open(this.method, this.url, false);
				if(this.method=="post")
					this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				this.req.send(this.method=="get"?null:this.param);

				if (this.req.status == 200){
					this.returnVar = this.req.responseText;
					this.showResult();
				}
			}
			// ºñµ¿±âÀÏ¶§
			else {
				this.req.open(this.method, this.url, true);
				if(this.method=="post")
					this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				this.req.onreadystatechange = this.getResult;
				try{
					this.req.send(this.method=="get"?null:this.param);
				}
				catch (e){
					alert("ºñµ¿±â Àü¼Û ¿¡·¯");
					return;
				}
			}
		}
	}

	// ºñµ¿±âÀÏ¶§ ÀÀ´äÀ» ¹Þ¾ÒÀ»¶§ Ã³¸®
	this.getResult = function(){
		if(ajaxObj.req.readyState == 4 && ajaxObj.req.status == 200) {
			//ajaxObj.returnVar = ajaxObj.req.responseText;
			ajaxObj.returnVar = trimStr(ajaxObj.req.responseText);
			ajaxObj.showResult();
		}
	}

	// °á°ú¸¦ Ã³¸®
	this.showResult = function(){
		if(this.debug){
			var obj = _NewObject("div");
			obj.innerHTML += "return : " + this.returnVar +"<br>";
			document.body.appendChild(obj);
		}
		if(this.resultFunc!=""){
			// »ç¿ëÀÚ Á¤ÀÇ ÇÔ¼ö°¡ ¼¼ÆÃµÇ¾î ÀÖÀ¸¸é ±× ÇÔ¼ö¸¦ ½ÇÇà
			eval(this.resultFunc);
		}
	}
}

// AjaxObj °´Ã¼¸¦ »ý¼º
var ajaxObj = new AjaxObj();

