﻿// XMLHttpRequest 생성
//document.domain = "ucware.net";


var reqHttp_UCMSG;
var reqHttpPOOL_UCMSG;
function newXMLHttpRequest() {
  var reqHttp;
  if ( typeof reqHttp_UCMSG == "object" ){
    return reqHttp_UCMSG;
  }
  if (window.ActiveXObject) {	 // IE
    try {
      reqHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        reqHttp =  new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e1) {
        reqHttp =  null;
      }
    }
  } else if (window.XMLHttpRequest){  // IE 이외
    try {
      reqHttp =  new XMLHttpRequest();
    } catch (e) {
      reqHttp =  null;
    }
  }
  if (reqHttp == null) errorMessage();   //XMLHttpRequest 생성 실패
  
  reqHttp_UCMSG = reqHttp;
  return reqHttp_UCMSG;
} 

function newPOOLXMLHttpRequest() { 
  var reqHttp;
  if ( typeof reqHttpPOOL_UCMSG == "object" ){
    return reqHttpPOOL_UCMSG;
  }
  if (window.ActiveXObject) {	 // IE
    try {
      reqHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        reqHttp =  new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e1) {
        reqHttp =  null;
      }
    }
  } else if (window.XMLHttpRequest){  // IE 이외
    try {
      reqHttp =  new XMLHttpRequest();
    } catch (e) {
      reqHttp =  null;
    }
  }
  if (reqHttp == null) errorMessage();   //XMLHttpRequest 생성 실패
  
  reqHttpPOOL_UCMSG = reqHttp;
  return reqHttpPOOL_UCMSG; 
}

// 지원할 수 없는 브라우저 사용
function errorMessage() {
    alert("지원할 수 없는 브라우저입니다.");
}

// readyState와 status 체크
function openSendStatus(getPost, send_url, trueFalse, sendData, cbFunction) {  
  var msg = "";
  msg += " getPost = " + getPost ;
  msg += "\n send_url = " + send_url ;
  msg += "\n trueFalse = " + trueFalse ;
  msg += "\n sendData = " + sendData ; 
  
  var xmlHttp = newXMLHttpRequest();               //XMLHttpRequest 생성
  msg += "\n xmlHttp1=" + xmlHttp;
  //alert(msg);
  //xmlHttp.onreadystatechange = handleStateChange; 
  
  xmlHttp.open(getPost, send_url, trueFalse);    //송신방법,URL,통신방법 ( IE OK)
  //xmlHttp.open(getPost, "test.txt", trueFalse);    //송신방법,URL,통신방법 
  //xmlHttp.open("GET", "test.txt", false);     
  //xmlHttp.open("GET", "test.txt", true );    //송신방법,URL,통신방법 ( FireFox OK)
  //xmlHttp.open("GET", "test.xml", true );    //송신방법,URL,통신방법 ( FireFox OK)
  
  //xmlHttp.open("GET", send_url, true );    //송신방법,URL,통신방법 ( FireFox OK)
  
  xmlHttp.onreadystatechange = function() {        //처리상태 변경 발생
    //alert(xmlHttp.readyState)
    if (xmlHttp.readyState == 4) {                     //서버 처리 완료 
      if (xmlHttp.status == 200) {                     //파일 수신 성공
        cbFunction(xmlHttp);                        //callback 함수 
      } else { 
        exceptionControl(xmlHttp);                //예외 처리
      }
    }
  }
   
  var conType = "application/x-www-form-urlencoded; charset=utf-8";
  xmlHttp.setRequestHeader("Content-Type", conType);

  // 참고: http://bytes.com/forum/thread522354.html
  // this should prevent the browser from caching
  xmlHttp.setRequestHeader("Pragma", "no-cache"); // HTTP/1.0
  xmlHttp.setRequestHeader("Cache-Control", "no-cache");
  xmlHttp.setRequestHeader("If-Modified-Since", "Wed, 31 Dec 1980 00:00:00 GMT");
  xmlHttp.setRequestHeader("Expires", "Wed, 31 Dec 1980 00:00:00 GMT");

  // this should make sure the connection is closed after
  // receiving the response from the server
  xmlHttp.setRequestHeader("Connection", "close");

  xmlHttp.send(sendData);      //처리 데이터 송신
}


function handleStateChange() {        //처리상태 변경 발생 
  if (xmlHttp.readyState == 4) {                     //서버 처리 완료
    if (xmlHttp.status == 200) {                    //파일 수신 성공
    	userReceive(xmlHttp);                        //callback 함수
		xmlHttp.abort();
    } else {
      exceptionControl(xmlHttp);                //예외 처리
    }
  }
} 


// readyState와 status 체크
/* MSG_POLL */
function openSendPOLLStatus(getPost, send_url, trueFalse, sendData, cbFunction) {
	var xmlHttp = newPOOLXMLHttpRequest();               //XMLHttpRequest 생성
	xmlHttp.open(getPost, send_url, trueFalse);    //송신방법,URL,통신방법
    xmlHttp.onreadystatechange = function() {        //처리상태 변경 발생
    	if (xmlHttp.readyState == 4) {                     //서버 처리 완료
            //alert("4");
        	if (xmlHttp.status == 200) {                    //파일 수신 성공
                cbFunction(xmlHttp);                        //callback 함수
				xmlHttp.abort();
            } else {
                exceptionPOOLControl(xmlHttp);                //예외 처리
            }
        }
    }
    var conType = "application/x-www-form-urlencoded; charset=utf-8";
    xmlHttp.setRequestHeader("Content-Type", conType);

    // 참고: http://bytes.com/forum/thread522354.html
    // this should prevent the browser from caching
    xmlHttp.setRequestHeader("Pragma", "no-cache"); // HTTP/1.0
    xmlHttp.setRequestHeader("Cache-Control", "no-cache");
    xmlHttp.setRequestHeader("If-Modified-Since", "Wed, 31 Dec 1980 00:00:00 GMT");
    xmlHttp.setRequestHeader("Expires", "Wed, 31 Dec 1980 00:00:00 GMT");

    // this should make sure the connection is closed after
    // receiving the response from the server
    xmlHttp.setRequestHeader("Connection", "close");

    xmlHttp.send(sendData);      //처리 데이터 송신
}


// 예외 처리 (status != 200)
function exceptionControl(xmlHttp) {
    var exceptShow = "상태 코드: " + xmlHttp.status;
    exceptShow += ",  비정상으로 종료되었습니다.";
    //alert(exceptShow);
}

// 예외 처리 (status != 200)
function exceptionPOOLControl(xmlHttp) {
    var exceptShow = "상태 코드: " + xmlHttp.status;
    exceptShow += ",  비정상으로 종료되었습니다.";
    //alert(exceptShow);
}



