var asadalAjax	= function (strName)
{
	if (window.asadalAjax ==null) { window.asadalAjax = []; }
	this.name					= strName || window.asadalAjax.length;
	window.asadalAjax[this.name]= this;
	this.request				= null;			// XMLHttpRequest Object
	this.type					= null;			// GET POST
	this.host					= null;			// Server Name (ex http://asadal.com)
	this.path					= null;			// index.htm
	this.dataType				= null;			// XML, TEXT
	this.ajaxDebug				= '';

	this.onload					= null;
	this.onLoad					= null;
	this.onComplete				= null;

	this.onchange				= null;
	this.onChange				= null;

	this.paramKey				= [];			// param key (ex &code)
	this.paramVal				= [];			// param val (ex =1)

	/*
	* Function prototype 생성. XMLHttpRequest Object의 onreadystatechange 프로퍼티에 생성된 컨트롤 객체를 참조하기 위함
	* 출처 : Prototype JavaScript framework
	*/
	if (typeof Function.prototype.bind == 'undefined')
	{
		Function.prototype.bind = function ()
		{
			var _$=this, args =[], object=null;
			for (var i=0, len=arguments.length; i<len; i++)
			{
				args[i]	= arguments[i];
				object	= args.shift();
			}
			return function () { return _$.apply(object, args); }
		}
	}
}

asadalAjax.prototype.createRequest = function ()
{
	try
	{
		return new XMLHttpRequest();
	}
	catch (trymicrosoft)
	{
		try { return new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (othermicrosoft)
		{
			try { return new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (failed) { return null; }
		}
	}
}

asadalAjax.prototype.ajaxSubmit = function ()
{
	this.onload		= this.onload   || this.onLoad    || this.onComplete;
	this.onchange	= this.onchange || this.onChange;
	this.type		= this.type.toUpperCase();
	this.request	= this.createRequest();

	if (this.request == null)
	{
		this.onErrorMsg('Ajax Object를 생성 실패[Ajax를 사용할 수 없는 브라우져입니다.');
		return false;
	}

	var autoTimeStamp	= "&autoTimeStamp=" + new Date().getTime();
	var queryString		= (this.paramKey.length)
						? this.encodeParam() + autoTimeStamp : "autoTimeStamp=" + new Date().getTime();

	var Headers			=
	{
		'Content-type'		: 'application/x-www-form-urlencoded; charset=UTF-8',
		'Expires'			: 'Sat, 01 Jan 2000 00:00:00 GMT',
		'Cache-Control'		: 'no-store, no-cache, must-revalidate,post-check=0, pre-check=0',
		'Pragma'			: 'no-cache',
		'X-Requested-By'	: 'XMLHttpRequest',
		'Accept'			: 'text/plain, text/html, text/javascript, application/x-javascript, text/xml, application/xml, */*'
	};


	//alert( this.type );
	// document.write( queryString );
	//return;

	this.request.onreadystatechange = this.ondataavailable.bind(this);
	switch (this.type)
	{
		case 'GET' :
			this.request.open(this.type, this.host + this.path + '?' + queryString, true);
			for (var n in Headers) { this.request.setRequestHeader(n, Headers[n]); }
			this.request.send("");
			break;
		case 'POST' :
			this.request.open(this.type, this.host + this.path, true);
			for (var n in Headers) { this.request.setRequestHeader(n, Headers[n]); }
			this.request.send(queryString);
			break;
		default :
			alert('요청은 GET OR POST으로 가능합니다');
			return false;
			break;
	}
	this.ondataavailable();
}

asadalAjax.prototype.ondataavailable = function ()
{
	var dataType	= this.dataType.toUpperCase();
	var reqData		= null;

	if (typeof this.onchange == 'function') { this.onchange(this.request.readyState); }

	if (this.request.readyState == 4)
	{
		if(this.request.status == 200)
		{
			if (typeof this.onload == 'function')
			{
				switch (dataType)
				{
					case 'TEXT' :
						reqData	= this.request.responseText;
						break;
					case 'XML' :
						reqData	= this.request.responseXML;
						break;
					default :
						this.onErrorMsg('데이터를 가져올 타입을 설정하세요.\n(ex ajax.type = "TEXT || XML")\n하나를 선택하세요');
						return false;
						break;
				}
				if (this.ajaxDebug == true)
				{
					this.debugToolOpenWin(this.request.responseText);
				}
				this.onload(reqData);
				this.request	= null;
			}
		}
		else
		{
			this.onErrorMsg('데이터 로드 실패1[데이터 오브젝트 명(' + this.name + ')]');
			return false;
		}
	}
}

asadalAjax.prototype.onErrorMsg = function (msg) { alert(msg); }

asadalAjax.prototype.setParam = function (key, val)
{
	this.paramKey.push(key);
	this.paramVal.push(val);
}

asadalAjax.prototype.encodeParam = function ()
{
	var queryString	= [];
	var cnt			= this.paramKey.length;
	for (var i=0; i<cnt; i++)
	{
		queryString[i]	= this.paramKey[i] + "=" + encodeURIComponent(this.paramVal[i]);
	}
	return queryString.join('&');
}

asadalAjax.prototype.debugToolOpenWin = function (errorText)
{
	var size		= "width=" + 600 + ", height=" + 700 + ", scrollbars=yes";
	var NFW			= window.open("", 'ajaxDebug', size);
	var string		= [];
		string[0]	= '<html>';
		string[1]	= '<head>';
		string[2]	= '<title>전시모듈 디버깅</title>';
		string[3]	= '</head>';
		string[4]	= '<body style="margin: 5px;">';
		string[5]	= '<table border="0" cellpadding="0" cellspacing="1" width="100%" bgcolor="#868686">';
		string[6]	= '<tr>';
		string[7]	= '	<td bgcolor="#FFFFFF">&nbsp;<span style="color: blue; size: 12pt;">' + errorText + '</span></td>';
		string[8]	= '</tr>';
		string[9]	= '</table>';
		string[10]	= '</body>';
		string[11]	= '</html>';
	NFW.window.focus();
	NFW.document.write(string.join("\n"));
}
