// request 객체 생성
var req = null;
function create_request() {
    var request = null;
    try {
        request = new XMLHttpRequest();
    } catch (trymicrosoft) {
        try {
            request = new ActiveXObject("Msxml12.XMLHTTP");
        } catch (othermicrosoft) {
            try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (failed) {
                request = null;
            }
        }
    }
    if (request == null)
        alert("Error creating request object!");
    else
        return request;
}

// 트랙백을 사용한다면 토큰을 실시간으로 생성
var trackback_url = "";
function trackback_send_server(url) {
    req = create_request();
    trackback_url = url;
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                var token = req.responseText;
                prompt("아래 주소를 복사하세요. 이 주소는 스팸을 막기 위하여 한번만 사용 가능합니다.", trackback_url+"/"+token);
                trackback_url = "";
            }
        }
    }
    req.open("POST", g4_path+'/'+g4_bbs+'/'+'tb_token.php', true);
    //req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    req.send(null);
}

var ajax = {}; 
ajax.xhr = {};
ajax.xhr.Request = function(sUrl, params, callback, method, applyObj)
{
	this.sUrl = sUrl;
	this.params = params;
	this.callback = callback;
	this.method = method;
	this.applyObj = (applyObj == null) ? null : applyObj;
	
	this.send();
}
ajax.xhr.Request.prototype = {
	getXMLHttpRequest : function()
	{
		if( window.ActiveXObject ){
			try{
				return new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					return new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e1){
					return null;
				}
			}
		}else if( window.XMLHttpRequest){
			return new XMLHttpRequest();
		}else{
			return null;
		}
	}
	,
	send : function()
	{
		this.req = this.getXMLHttpRequest();
		
		var httpMethod = this.method ? this.method : 'GET';
		if( httpMethod != 'GET' && httpMethod != 'POST' ){
			httpMethod = 'GET';
		}
		
		var httpParams = (this.params == null || this.params == '' )? null : this.params;
		
		var httpUrl = this.sUrl;
		if( httpMethod == 'GET' && httpParams != null ){
			httpUrl = httpUrl + "?" + httpParams;
		}
		this.req.open( httpMethod, httpUrl, true ); //비동기 방식으로 서버상의 파일을 연다
		this.req.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded');
				
		var request = this;
		this.req.onreadystatechange = function(){			
			request.onStateChange.call(request); 
		}
		
		this.req.send(httpMethod == 'POST' ? httpParams : null );
	}
	,	
	onStateChange : function()
	{
		if (this.applyObj) {
			this.callback.call(this.applyObj, this.req);
		} else {
			this.callback(this.req);
		}
	}
}