/*
url-loading object and a request queue built on top of it
*/

/* namespacing object */
var net=new Object();

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;


/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function(url,onload,onerror,method,params,contentType){
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.loadXMLDoc(url,method,params,contentType);
}

net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded';
  }
  if (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  } else if (window.ActiveXObject){
    this.req=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
    }catch (err){
      this.onerror.call(this);
    }
  }
}


net.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
      this.onload.call(this);
    }else{
      this.onerror.call(this);
    }
  }
}

net.ContentLoader.prototype.defaultError=function(){
	/*
  alert("error fetching data!"
    +"\n\nreadyState:"+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders());
	*/
}


/* -------------------------------------
 AJAX function (2006/02/27)
-------------------------------------*/
function jsAjax() {
	var xmlHttp=null;
	var a,ua=navigator.userAgent;
	var _urlparse=new Array;

	var url=null;
	var method="GET";
	var id=null;
	var password=null;
	
	// Browser Check
	this.browser={
		safari : ((a=ua.split('AppleWebKit/')[1])?a.split('(')[0]:0)>=124,
		moz : ((a=ua.split('Gecko/')[1])?a.split(" ")[0]:0) >= 20011128,
		opera : (!!window.opera) && ((typeof XMLHttpRequest)=='function')
	} // end browser

	// Option Check
	this.check=function() {
		var error=1;
		if(!this.url) {
			alert('AJAX : ERROR_URL');
			error=0;
		}
		return error;
	} // end check

	// Ajax RUN
	this.run=function(proc_funcname) {
		if(this.check()) {
			if(window.ActiveXObject) {
				// Win e4,e5,e6
				try{
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e){
					try	{
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e2){
						xmlHttp=null;
					}
				}
			}else if(window.XMLHttpRequest) {
				// Win Mac Linix m1,f1,o9 Mac s1 Linux K3
				xmlHttp=new XMLHttpRequest();
			}else{
				xmlHttp=null;
			}


			// method GET
			if (this.method=="GET") {
				xmlHttp.open("GET",this.url,true);
				_urlparse[0]=this.url;
				_urlparse[1]=null;
			}else
			// method POST
			 if(this.method=="POST") {
				if(this.url.indexOf("?")>=0) {
					_urlparse=this.url.split("?");
				}else{
					_urlparse[0]=this.url;
					_urlparse[1]=""; /* no value error */
				}
				// POST AUTH
				if(this.id && this.password) {
					xmlHttp.open("POST",_urlparse[0],true,this.id,this.password);
				}else {
					xmlHttp.open("POST",_urlparse[0],true);
				}
			}

			// onreadystatechange Browser Bug..
			if(this.browser.opera || this.browser.safari || this.browser.moz) {
				xmlHttp.onload=function() {proc_funcname(xmlHttp.responseXML);}
			}else{
				xmlHttp.onreadystatechange = function() {
					if (xmlHttp.readyState == 4) {
						proc_funcname(xmlHttp.responseXML);
					}
				}
			}
			xmlHttp.send(_urlparse[1]);
		}
	} // end Run
} // end AJAX Function

/*
var AJAX=new jsAjax(); 	// jsAjax ÀÎ½ºÅÏ½º AJAX »ý¼º.
AJAX.url="http://coder.nhndesign.com/test.php"; 	// Åë½ÅÇÒ url ÁÖ¼Ò
AJAX.method="POST"; 	// method ¼³Á¤ (GET,POST)
AJAX.run(PROC); 	// ÄÝ¹é ÇÔ¼ö ¼³Á¤ (ÇØ´ç url¿¡¼­ °ªÀ» ¹ÞÀ¸¸é PROC ÇÔ¼ö°¡ ½ÇÇàµÊ)

function PROC(obj) {
  var item=obj.getElementsByTagName("R");
  alert(item.length);
}
*/

