/*
 * Ajax API (APIs are same with the prototype.js)
 *   by likejazz
 */
var Ajax = {
	_xmlHttpRequest: function(req) {
//		alert("4");
		// branch for native XMLHttpRequest object
		if(window.XMLHttpRequest) {
			try {
				req = new XMLHttpRequest();
			} catch(e) {
				req = false;
			}
		// branch for IE/Windows ActiveX version
		} else if(window.ActiveXObject) {
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					req = false;
				}
			}
		}
//		alert("5 -  XHR 객체 등록 완료.");
		return req;
	},

	_request: function(url, options) {
//		alert("3");
		var req = this._xmlHttpRequest();
		if (req) {
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
//					alert(req.status);
					//only if "OK" or Browser is "Safari"
					if ((typeof req.status == 'undefined' && 
						navigator.userAgent.match(/Safari/)) 
					|| req.status == 200) {
//					    alert(req.responseText);
						options['onComplete'](req);
					}
				}
			}
		}
		
		
//		alert(options['method']);
		
		if (options['method'] != 'POST') {
//			alert("6");
			if (options['parameters'] != '') {
				url += '?' + options['parameters'];
			}
		}
		
		
		req.open(options['method'], url, true);
		req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
		if (options['method'] == 'POST')
			req.setRequestHeader('Content-Type', options['contentType']);
		req.setRequestHeader('Connection', 'close');
		req.send((options['method'] == 'POST') ? options['parameters'] : null);
	},
	
	_setOptions: function(options) {
//		alert("2");
		_options = {
			method:			'POST',
			contentType:	'application/x-www-form-urlencoded; charset=euc-kr',
//			contentType:	'enctype= multipart/form-data; charset=UTF-8',
			parameters:		'',
			onComplete:		function() {},
			updateDiv:		''
		}

		for (var property in options) {
			if (property == 'method')
				_options[property] = options[property].toUpperCase();
			else
				_options[property] = options[property];
		}
		return _options;
	},
	
	Request: function(url, options) {
//	alert("1");
		this._request(url, this._setOptions(options));
	}

}

function $(id){
	return document.getElementById(id);
}

