/*
==============================================================================================
프로그램명 : ezAjax
만든이     : 강민식
만든날     : 2005.12.13
이메일     : aquazen@nate.com 
설명       : 
 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 실행, 결과값이 returnVar 에 담긴다
	}

	// 결과 값을 직접 처리할 사용자 정의 함수
	function testFunc(){
		// 결과 값이 들어간 returnVar 를 이용하여 원하는 결과물로 처리
		alert(ajaxObj.returnVar);
	}
	</script>
	>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

*** 유의사항 ***
개발시에만 true 로 하시고, 실제 사용할때에는 this.debug 값을 false 로 세팅하세요

==============================================================================================
*/

function _NewObject(e) {return document.createElement(e)}

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 = true;		// 디버그를 할지 안할지여부. 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) {
				this.req = false ;
			}
		}
		
		if (!this.req && window.XMLHttpRequest) 
			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);
				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);
				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.showResult();
		}
	}

	// 결과를 처리
	this.showResult = function(){
		if(this.resultFunc==""){
			if(this.debug){
				var obj = _NewObject("div");
				obj.innerHTML += this.returnVar +"<br>";
				document.body.appendChild(obj);
			}
		}
		else {
			// 사용자 정의 함수가 세팅되어 있으면 그 함수를 실행
			eval(this.resultFunc);
		}
	}
}

// AjaxObj 객체를 생성
var ajaxObj = new AjaxObj();