// Copyright 2005, Bensoft Inc.
// Distributed under the terms of the GNU PUBLIC LICENSE
// Contact info@bensoft.com for other licensing terms
// Simple container for a request parameter
var G_COMMON_SCRIPT_URL="http://1co.simpledesign.co.kr/member_proc.php";
function jsReqParam(vname,param){
 this.strName=vname;
 this.strValue=param;
 this.setName=function(vname){this.strName=vname};
 this.setValue=function(value){this.strValue=value};
 this.getName=function(){return(this.strName)};
 this.getValue=function(){return(this.strValue)};
}
// Array of request parameters
function jsReqParams(){
 this.params=new Array();
 this.length=function(){
   return(this.params.length);
 }
 this.enc=function(str){
  if(encodeURIComponent) {
     return encodeURIComponent(str);
     //return this.replace(str,"&","짚amp")
  }else if(escape){
   return escape(str)
  }
 }
 // replace 구조
 this.replace =function(value,aa,bb){
    var arr=value.split(aa);
    return arr.join(bb);
};
 //add param
 this.add=function(vname,value){
  rp=new jsReqParam(vname,value);
  this.params.push(rp);
 };
 //retrieve request string
 this.reqstr=function(){
  var ret="";
  var nm="";
  var val="";
  for(var i=0;i<this.params.length;i++){
   rp=this.params[i];
   nm = rp.getName();
   val= rp.getValue();
	if (nm !=undefined) { 
		   n=this.enc(nm);
		   v=this.enc(val);  
		   if(i>0){ret=ret+"&"};
		   ret=ret+n+"="+v;
   }
  }
   //alert(ret);
    return(ret);
 }
}
//XMLHttp client
function jsHTTPClient(){
// this.strutsmapping = "&S_FORWARD=resultScript"
 this.strutsmapping = ""
 this.args = new Array(); // 어레이 관련 된것 
 this.debug = false;
 this.batchMsg = "";
 this.batchFld = "";
 this.dateConvert = false;
 this.setDebug = function(bvalue) 
 {this.debug = bvalue;}
 this.getDebug= function() 
 { return this.debug; }
 // 
 this.resultset="rs";
 this.reqname="js";//name this request
 this.xmlhttp=false;
 this.strURL=new String(G_COMMON_SCRIPT_URL);
 //this.funcComplete=new Function();
 this.funcComplete="";
 this.reqparams=new jsReqParams();
 // Initialize on IE,Mozilla and Others
 /*@cc_on @*/
 /*@if (@_jscript_version >= 5)
 try {
  this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   this.xmlhttp = false;
  }
 }
 @end @*/
 if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
  this.xmlhttp = new newXMLHttpRequest();
 }
 //setup
 this.setResultSet=function(name){this.resultset=name};
 this.getResultSet=function(){return(this.resultset)};
 this.setName=function(name){this.reqname=name};
 this.getName=function(){return(this.reqname)};
 this.setBatchMsg=function(_msg) { this.batchMsg = _msg; }
 this.setBatchFld=function(_fld) { this.batchFld = _fld; }
 
// this.setComplete=function(f){this.funcComplete=f};
 this.setComplete=function(fval){
	 this.funcComplete=fval;
	 
  };
 this.addParam=function(vname,value){this.reqparams.add(vname,value)};
 //  폼명에서 파라미터를 만들어서 넘긴다.
 this.addForm=function(vfrmname)
 {
	   if (vfrmname=='') {
        return '';
    }
    var form= eval('document.'+vfrmname);
    this.common_makeParam(form);
 };
/*
 파라미터를 모아서 리턴한다.
*/
 this.common_makeParam = function(form){
    var name = new Array(form.elements.length);
    var value = new Array(form.elements.length);
    var j = 0;
   //사용가능한 컨트롤을 배열로 생성한다.
    var len = form.elements.length;
     for (i = 0; i < len; i++) {
      switch (form.elements[i].type) {
        case "button":
        case "reset":
		case "file":
        case "": // IBSheet포함된것 지움
        case "submit":
          break;
        case "radio":
        case "checkbox":
                if (form.elements[i].checked == true) {
                    name[j]  = form.elements[i].name;
                    value[j] = form.elements[i].value;
					this.reqparams.add(name[j],value[j])
                    j++;
                }
                break;
        case "select-one":
                name[j] = form.elements[i].name;
                    var ind = form.elements[i].selectedIndex;
                    if(ind >= 0) {
						value[j] = form.elements[i].options[ind].value;
						/*
                        if (form.elements[i].options[ind].value != '')
                            value[j] = form.elements[i].options[ind].value;
                        else
                            value[j] = form.elements[i].options[ind].text;
							*/
                    } else {
                        value[j] = "";
                    }
					this.reqparams.add(name[j],value[j])
                    j++;
					
                    break;
            case "select-multiple":
                name[j] = form.elements[i].name;
                    var llen = form.elements[i].options.length;
                    var increased = 0;
                    for( k = 0; k < llen; k++) {
                        if (form.elements[i].options[k].selected) {
                            name[j] = form.elements[i].name;
							value[j] = form.elements[i].options[k].value;
							/*
                            if (form.elements[i].options[k].value != '')
                                value[j] = form.elements[i].options[k].value;
                            else
                                value[j] = form.elements[i].options[k].text;
								*/
                            j++;
                            increased++;
                        }
                    }
                    if(increased > 0) {
                        j--;
                    } else {
                        value[j] = "";
                    }
					this.reqparams.add(name[j],value[j])
                    j++;

                    break;
                default :
					name[j] = form.elements[i].name;
						if( form.elements[i].data_format !=undefined)
						{	value[j] =  getUnformat(form.elements[i]);}
						else
						  {value[j] = form.elements[i].value;}
					this.reqparams.add(name[j],value[j])
                    j++;
        }
	 }
  };

 this.setURL=function(u){
   this.strURL=u;
   this.addParam("_js_rs",this.getResultSet()); // Name of our ResultSet
 };
 this.getResult=function(){
	  if(this.debug)
	  {alert(this.xmlhttp.responseText);}
	 return(this.xmlhttp.responseText)
	 };
 this.reset=function(){return(this.xmlhttp.abort())};

//asynch 한글처리 안돼고 싱크 처리만 됨
 this.send=function(){
  if(this.xmlhttp){
     //var req=this.strURL;
	  var req="";
	 var url = this.strURL;
	 if(this.reqparams.length()>0){
		req=req+this.reqparams.reqstr();
     }
	 req+=this.strutsmapping;
	 if( this.debug)
	 {  alert(req); 
	 }
 			this.xmlhttp.Open("POST",url,false);
			this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			this.xmlhttp.send(req);
			try{
			  var func  = this.funcComplete;
			  if( func !="")
			 {
				  var ofunc = eval(func); // 함수 호출 
				  ofunc(this.args, this.xmlhttp.responseText);
			 }
		  } catch (e){}
		  
		return this.xmlhttp.responseText
 
  }
 }
  //asynch 한글처리 되는것 send req
 this.asend=function(){
	  if(this.xmlhttp){
			 //var req=this.strURL;
			 var req="";
			 var url = this.strURL;
			 if(this.reqparams.length()>0){
				req=this.reqparams.reqstr();
			 }
			 req+=this.strutsmapping;
			 if( this.debug)
			 {  alert(req); 
			 }
					this.xmlhttp.open("POST",url,true);
			this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // euc-kr,UTF-8, 
			var me=this;
		    this.xmlhttp.onreadystatechange=function() {
			if (me.xmlhttp.readyState==4) {
				try{
					  var func =me.funcComplete;
					 if( func !="")
					 {
					   var ofunc = eval(func); // 함수 호출 
 					   ofunc(me.args, me.xmlhttp.responseText);
					 }
				  } catch (e){//alert('httpclient.asend() Error Raised');
					}
				//  return me.xmlhttp.responseText
			}
		   }
			this.xmlhttp.send(req);  
	  }//end if
  }
  
 /***
 * 배치 처리시에 메시지 처리 되어서 뜬다..
 *  batch 처리중 메시지 떠서 사용된다.
 */
  this.batchsend=function(){
  if(this.xmlhttp){
	//document.all.common_MSG.innerHTML = this.batchMsg ; //"복리후생작업중입니다....";
	if( this.batchFld !="")
	 {eval("document.all."+this.batchFld).value+=this.batchMsg+"\n";}
	 //var req=this.strURL;
	  var req="";
	 var url = this.strURL;
	 if(this.reqparams.length()>0){
		req=req+this.reqparams.reqstr();
     }
	 req+=this.strutsmapping;
	 if( this.debug)
	 {  alert(req); 
	 }
 			this.xmlhttp.Open("POST",url,false);
			this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			this.xmlhttp.send(req);
			try{
			  var func  = this.funcComplete;
			  if( func !="")
			 {
				  var ofunc = eval(func); // 함수 호출 
				  ofunc(this.args, this.xmlhttp.responseText);
			 }
		  } catch (e){}

		//fn_continueClose();
		return this.xmlhttp.responseText
 
  }
 }
  //asynch  batch 한글처리 되는것 send req
 this.abatchsend=function(){
  if(this.xmlhttp){
     var req=this.strURL;
	 //   var req="";
	//var url = this.strURL;
	 if(this.reqparams.length()>0){
     req=req+"?"+this.reqparams.reqstr();
     }
	 req+=this.strutsmapping;
	 if( this.debug)
	 {  alert(req); 
	 }
		/* 아래의 소스는 한글이 깨져서 올라 갑니당...
			this.xmlhttp.Open("POST",url,false);
			this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.xmlhttp.send(req);
			try{
			  var func  = this.funcComplete;
			  if( func !="")
			 {
				  var ofunc = eval(func); // 함수 호출 
				  ofunc(this.args, this.xmlhttp.responseText);
			 }
		  } catch (e){}
		  
		return this.xmlhttp.responseText
		*/

		   this.xmlhttp.open("POST",req,true);
		   this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
		   var me=this;
		   this.xmlhttp.onreadystatechange=function() {
			if (me.xmlhttp.readyState==4) {
				try{
					  var func =me.funcComplete;
					  if( func !="")
					 {
					   var ofunc = eval(func); // 함수 호출 
					   ofunc(me.args, me.xmlhttp.responseText);
					 }
				  } catch (e){}
				//  return me.xmlhttp.responseText
			}
		   }
			   this.xmlhttp.send(null);
  }
 }
this.newXMLHttpRequest= function() {
 		  var xmlreq = false;
		  if (window.XMLHttpRequest) {

			// Create XMLHttpRequest object in non-Microsoft browsers
			xmlreq = new XMLHttpRequest();

		  } else if (window.ActiveXObject) {

			// Create XMLHttpRequest via MS ActiveX
			try {
			  // Try to create XMLHttpRequest in later versions
			  // of Internet Explorer

			  xmlreq = new ActiveXObject("Msxml2.XMLHTTP");

			} catch (e1) {

			  // Failed to create required ActiveXObject

			  try {
				// Try version supported by older versions
				// of Internet Explorer

				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

			  } catch (e2) {

				// Unable to create an XMLHttpRequest with ActiveX
			  }
			}
		  }

		  return xmlreq;
		}
}
