/**
 * XMLHttp Request..
 *@author Administrator
 */


// È¯°æ¼³Á¤ »ó¼ö..
var oXML = {
	useActiveX:(typeof ActiveXObject!="undefined"),
	useDom:document.implementation&&document.implementation.createDocument,
	useXmlHttp:(typeof XMLHttpRequest!="undefined"),
	ARR_XMLHTTP_VERS: ["MSXML2.XmlHttp.5.0","MSXML2.XmlHttp.4.0","MSXML2.XmlHttp.3.0","MSXML2.XmlHttp","Microsoft.XmlHttp"],
	ARR_DOM_VERS: ["MSXML2.DOMDocument.5.0","MSXML2.DOMDocument.4.0","MSXML2.DOMDocument.3.0","MSXML2.DOMDocument","Microsoft.XmlDom"],
	createRequest: function(){
		if(this.useXmlHttp){
			return new XMLHttpRequest();
		}else if(this.useActiveX){
			if(!this.XMLHTTP_VER){
				for(var i=0;i<this.ARR_XMLHTTP_VERS.length;i++){
					try{
						new ActiveXObject(this.ARR_XMLHTTP_VERS[i]);
						this.XMLHTTP_VER=this.ARR_XMLHTTP_VERS[i];
						break;
					}catch(oError){;}
				}
			}
				
			if(this.XMLHTTP_VER){
				return new ActiveXObject(this.XMLHTTP_VER);
			}else{
				throw new Error("Could not create XML HTTP Request.");
			}
		}else{
			throw new Error("Your browser doesn't support an XML HTTP Request.");
		}
	}
};
	


/* oXMLHttp ±â´ÉÁ¤ÀÇ ½ÃÀÛ..	*/
var oXMLHttp = function(){
	this.http = null;
	this.isSupported = oXML.useXmlHttp||oXML.useActiveX;
	
	if(this.isSupported){
		this.http = this.createRequest();
	}
};


// XMLHttp °´Ã¼ »ý¼º
oXMLHttp.prototype.createRequest = function(){
	if(oXML.useXmlHttp){
		return new XMLHttpRequest();
	}else if(oXML.useActiveX){
		if(!oXML.XMLHTTP_VER){
			for(var i=0;i<oXML.ARR_XMLHTTP_VERS.length;i++){
				try{
					new ActiveXObject(oXML.ARR_XMLHTTP_VERS[i]);
					oXML.XMLHTTP_VER=oXML.ARR_XMLHTTP_VERS[i];
					break;
				}catch(oError){;}
			}
		}
			
		if(oXML.XMLHTTP_VER){
			return new ActiveXObject(oXML.XMLHTTP_VER);
		}else{
			throw new Error("Could not create XML HTTP Request.");
		}
	}else{
		throw new Error("Your browser doesn't support an XML HTTP Request.");
	}
};


// ºñµ¿±â ¿äÃ»
oXMLHttp.prototype.sendRequest = function(source, oForm, resultFunction){
	var oThis = this;
	var _http = oThis.http;
	var params = null;

	if(!_http){
		_http = oThis.createRequest();
		oThis.http = _http;
	}

	if(!source||(source.length == 0)){
		alert("¼Ò½ºÆÄÀÏÀ» ÁöÁ¤ÇØÁÖ»ï!");
		return;

	}
	
	if(_http.readyState != 0){
		_http.abort();

	}

	if(oForm){
		params = oThis.encodeForm(oForm);
	}




	_http.open("post", source, true);
	_http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	_http.onreadystatechange = function(){

		if(_http.readyState == 4){
			// »óÅÂÃ¢ ¼û±â±â
			oThis.hideLoading();

			switch(_http.status){
				case 0:
					break;

				case 200:
					if(resultFunction){
						if( typeof(resultFunction) == "function" ){
							resultFunction(_http);
						} else{
							eval(resultFunction + "(_http);");
						}
					}

					break;
				
				case 404:
					alert("[¿À·ù] : " + source + "°¡ Á¸ÀçÇÏÁö ¾Ê½À´Ï´Ù.");
					break;

				case 500:
					alert("[¿À·ù] : " + _http.responseText);
					var oDebugWindow = window.open("about:blank", "debug");
					oDebugWindow.document.write( _http.responseText );
					break;
				
				default:
					alert("[¿À·ù] : " + _http.status);
					break;
			}
		}

	}

	oThis.showLoading();											// »óÅÂÃ¢ Ç¥½Ã..
	_http.send(params);
};



// Æûµ¥ÀÌÅÍ ÀÎÄÚµù..
oXMLHttp.prototype.encodeForm = function(oForm){
    var submitString = '';
    var formElement = '';
    var lastElementName = '';
    
    for(i = 0 ; i < oForm.elements.length ; i++){
        formElement = oForm.elements[i];

		switch(formElement.type){
            case "text" :
            case "select-one" :
            case "hidden" :
            case "password" :
            case "textarea" :
                submitString += formElement.name + '=' + encodeURIComponent(formElement.value) + '&';
                break;

			case "radio" :     
                if(formElement.checked){
                    submitString += formElement.name + '=' + encodeURIComponent(formElement.value) + '&';
                }
                break;

			case "checkbox" :
                if(formElement.checked){
                    if(formElement.name == lastElementName){
                        if(submitString.lastIndexOf('&') == submitString.length - 1){
                            submitString = submitString.substring(0, submitString.length - 1);
                        }

						submitString += ',' + encodeURIComponent(formElement.value);
                    }else{
                        submitString += formElement.name + '=' + encodeURIComponent(formElement.value); 
                    }

					submitString += '&';
                    lastElementName = formElement.name;
                }
				
				
                break;  
        }                                                                                                                  
    }

	submitString = submitString.substring(0, submitString.length - 1);

	return submitString;
};



// ºñµ¿±â Åë½ÅÁß Áßº¹ ¿äÃ»À» ¹æÁöÇÏ±â À§ÇÑ »óÅÂÃ¢ Ç¥½Ã
oXMLHttp.prototype.showLoading = function(){
	var oDisableZone = document.getElementById("Disable_Zone");
		
	if(!oDisableZone){
		oDisableZone = document.createElement("div");
		oDisableZone.id = "Disable_Zone";

		oDisableZone.style.zIndex = 1000;
		oDisableZone.style.left = "0px";
		oDisableZone.style.top = "0px";
		oDisableZone.style.width = document.body.scrollWidth;
		oDisableZone.style.height = document.body.scrollHeight;
		oDisableZone.style.display = "none";
		oDisableZone.style.position = "absolute";


		var oImage = new Image();

		oImage.src = "/js/images/loading.gif";
		oImage.align = "absmiddle";
		oImage.style.zIndex = oDisableZone.style.zIndex + 10;
		oImage.style.left = (document.body.scrollWidth / 2) + "px";
		oImage.style.top = (document.body.scrollHeight / 2) + 50 + "px";
		oImage.style.position = "absolute";

		oDisableZone.appendChild( oImage );
		document.body.appendChild( oDisableZone );
	}

	oDisableZone.style.display = "block";
};


// »óÅÂÃ¢ ¼û±â±â..
oXMLHttp.prototype.hideLoading = function(){
	var oDisableZone = document.getElementById("Disable_Zone");

	if(oDisableZone)
		oDisableZone.style.display = "none";
};
