//////////////////// XMLHTTP module

//var XMLHTTP = function(){};
//var XMLHTTP = new Object();
var XMLHTTP = {};

//////////////////// XMLHTTP module functions

XMLHTTP._probe = function (){
    // create functions

    var _iframe = function(){
	return new XMLHTTP.IFRAMEHttpRequest();
    };

    var _msxml2 = function(){
	return new ActiveXObject("Msxml2.XMLHTTP");
    };

    var _msxml = function(){
	return new ActiveXObject("Microsoft.XMLHTTP");
    };

    var _xmlhttp_proc = function() {
	return new XMLHttpRequest();
    }

    // xmlhttprequest object check

    if(typeof USE_IFRAME != 'undefined' && USE_IFRAME){
	return _iframe;
    }

    try{
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	return _msxml2;
    } catch (e){
	try {
	    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	    return _msxml;
	} catch (E){
	    if (typeof XMLHttpRequest != 'undefined'){
		return _xmlhttp_proc;
	    } else {
		return _iframe;
	    }
	}
    }
}

XMLHTTP.make = XMLHTTP._probe();

XMLHTTP.get_responseText = function(url,f){
    var xmlhttp = XMLHTTP.make();
    xmlhttp.open("GET",url,true);
    xmlhttp.onreadystatechange= function() {
	if (xmlhttp.readyState==4) {
	    f(xmlhttp.responseText);
	}
    }
    xmlhttp.send(null);
}

XMLHTTP.get_json = function(url,f){
    function str2object(str){
	var obj = null;
	eval("obj = " + str);
	return obj;
    }
    
    var pure_callback = function(text){
	var obj = str2object(text);
	f(obj);
    }
    XMLHTTP.get_responseText(url,pure_callback);
}

//////////////////// XMLHTTP::IFRAMEHttpRequest

XMLHTTP.IFRAMEHttpRequest = function(){
    // property initialize    
    this.div = this._make_div();
    this.ifm = this._make_iframe();
    this.div.appendChild(this.ifm);
    this._header = new Object();
    this.onreadystatechange = function(){};
    
    return this;
}

// class variable
XMLHTTP.IFRAMEHttpRequest.count = 0;

// method define

with(XMLHTTP.IFRAMEHttpRequest){
// private
    prototype._make_iframe = function(){
	var ifm = document.createElement("IFRAME");
	this.constructor.count ++;
	ifm.id = "IFRAME_LOADER"+ this.constructor.count;
	return ifm;
    }

    prototype._make_div = function(){
	var div = document.createElement("DIV");
	with(div.style){
	    visibility="hidden";
	    position="absolute";
	    left="-10";
	    top="-10";
	    width="100";
	    height="100";
	}
	return div;
    }
    
    prototype._make_onload_func = function(xmlobj){
	function getFrameDoc(id){
	    var doc = null;
	    if(document.frames){
		doc = document.frames[id].document;
	    }
	    if(doc == null){
		var f = document.getElementById(id);
		doc = f.contentWindow.document;
	    }
	    return doc;
	}
	
	return function() {
	    xmlobj.readyState = 4;	    
	    
	    var doc = getFrameDoc(xmlobj.ifm.id);
	    var src = doc.body.innerHTML;
	    src = src.replace(/<pre>/i,"").replace(/<\/pre>/i,"").
	    replace(/&lt;/g,"<").replace(/&gt;/g,">").
	    replace(/&amp;/g,'&').replace(/&quot;/g,'"');
	    
	    xmlobj.responseText = src;
	    xmlobj._header["Last-Modified"] = doc.lastModified;
	    xmlobj.onreadystatechange();
	    document.body.removeChild(xmlobj.div);
	};
    };
    
    prototype._addEventListener2Ifm = function(ifm,func){
	var event = 'load'
	if (ifm.addEventListener){ //É¸½àDOM
	    ifm.addEventListener(event, func, false);
	}else if(ifm.attachEvent){ //IE
	    ifm.attachEvent('on'+event,func);
	}
    }

    // interface
    prototype.getResponseHeader = function(key){
	return this._header[key];
    };

    prototype.open = function(method,url,sync){
	this.method  = method;
	this.requestURL = url;
    };
    
    prototype.send = function(query){
	if(this.method == "GET"){
	    this.ifm.src = "./" + this.requestURL;
	    var func = this._make_onload_func(this);
	    this._addEventListener2Ifm(this.ifm,func);
	    document.body.appendChild(this.div);
	}else{
	    //
	}
    };
    
}

