﻿var toComma = function(value){
  var reg = /(^[+-]?\d+)(\d{3})/; // 정규식
  value += '';                    // 숫자를 문자열로 변환

  while (reg.test(value)){
    value = value.replace(reg, '$1' + ',' + '$2');
  }
  
  return "￦" + value + '원';
}

/*AJAX라이브러리 ==> start*/
var CLS_DAjaxLIB = function(){
  //생성자
}
  
//지원브라우저에 맞는 HTTPREQUEST객체를 생성합니다.
CLS_DAjaxLIB.prototype.createHttpRequest = function(){
  if(window.ActiveXObject){
	  //Win e4,e5,e6용
		try {
		  return new ActiveXObject("Msxml2.XMLHTTP") ;
		} catch (e) {
		  try {
			  return new ActiveXObject("Microsoft.XMLHTTP") ;
			} catch (e2) {
			  return null ;
	   	}
	 	}
	} else if(window.XMLHttpRequest){
	  //Win Mac Linux m1,f1,o8 Mac s1 Linux k3용
		return new XMLHttpRequest() ;
	} else {
	  return null ;
  }
}


//사용되는 브라우저를 판별합니다.
CLS_DAjaxLIB.prototype.chkAjaxBrowser = function(){
  var a,ua = navigator.userAgent;
	this.bw= { 
		  safari    : ((a=ua.split('AppleWebKit/')[1])?a.split('(')[0]:0)>=124 ,
		  konqueror : ((a=ua.split('Konqueror/')[1])?a.split(';')[0]:0)>=3.3 ,
		  mozes     : ((a=ua.split('Gecko/')[1])?a.split(" ")[0]:0) >= 20011128 ,
		  opera     : (!!window.opera) && ((typeof XMLHttpRequest)=='function') ,
		  msie      : (!!window.ActiveXObject)?(!!this.createHttpRequest):false 
	}
	  
	return (this.bw.safari||this.bw.konqueror||this.bw.mozes||this.bw.opera||this.bw.msie);
}

//요청보내기/처리
CLS_DAjaxLIB.prototype.sendRequest = function(callback,data,method,url,async,sload,user,password){
  //XMLHttpRequest 오브젝트 생성
	var oj = this.createHttpRequest();
	if( oj == null ) return null;
	
		
	//강제 로드의 설정
	var sload = (!!this.sendRequest.arguments[5])?sload:false;
	if(sload || method.toUpperCase() == 'GET')url += "?";
	if(sload)url=url+"t="+(new Date()).getTime();
		
	//브라우저 판정
	var bwoj = new this.chkAjaxBrowser();
	var opera	  = bwoj.bw.opera;
	var safari	  = bwoj.bw.safari;
	var konqueror = bwoj.bw.konqueror;
	var mozes	  = bwoj.bw.mozes ;

  //송신 처리
	if(opera || safari || mozes){
	  oj.onload = function () { callback(oj);}
	}else{
	  oj.onreadystatechange = function(){
		  if ( oj.readyState == 4 ){
			  callback(oj);
			}
		}
  }
   
	//URL 인코딩
	data = this.uriEncode(data)
	if(method.toUpperCase() == 'GET') {
	  url += data
	}
		
	//open 메소드
	oj.open(method,url,async,user,password);

  //헤더 application/x-www-form-urlencoded 설정
	this.setEncHeader(oj)

  //send 메소드
	oj.send(data);

  return oj
}
  
//URI 인코딩 헤더 설정
CLS_DAjaxLIB.prototype.setEncHeader = function(oj){
  var contentTypeUrlenc = 'application/x-www-form-urlencoded; charset=UTF-8';//UTF-8
	if(!window.opera){
	  oj.setRequestHeader('Content-Type',contentTypeUrlenc);
	}else{
	  if((typeof oj.setRequestHeader) == 'function')
		  oj.setRequestHeader('Content-Type',contentTypeUrlenc);
	}	
  return oj
}
  
//URL 인코딩
CLS_DAjaxLIB.prototype.uriEncode = function(data){
  if(data!=""){
	  //&와=로 일단 분해해서 encode
		var encdata = '';
		var datas = data.split('&');
		for(i=1;i<datas.length;i++){
		  var dataq = datas[i].split('=');
			encdata += '&'+encodeURIComponent(dataq[0])+'='+encodeURIComponent(dataq[1]);
	  }
	}else{
	  encdata = "";
	}
  return encdata;
}     
/*AJAX라이브러리 ==> end*/


var CLS_Res = function(){
  this.type    = null;
  this.courtNo = null;
  this.eventNo1 = null;
  this.eventNo2 = null;
  this.objNo = null;
  this.seldate = null;
  this.gammoney = null;
  this.minmoney = null;
  this.objuse = null;
  this.imgurl = null;
  this.address = null;
}

var CLS_MapAuction = function(){
  this.ins_ajx = new CLS_DAjaxLIB();
  this.resultA = new Array();
  this.resultB = new Array();
  this.resultC = new Array();
  this.resultD = new Array();
    
  var ins = this;  
  var type_ = ['A', 'B', 'C', 'D'];
  this.getResult = function(oj){
    var xmlDoc  =  oj.responseXML;    
    
    for(var j = 0; j < type_.length; j++){
        var xIAcs = xmlDoc.getElementsByTagName("item" + type_[j]);
        var xItems = xIAcs.item(0).getElementsByTagName("item")
        for(var i = 0; i < xItems.length; i++){
          var res = new CLS_Res();
          res.type    = type_[j];
          
          if(xItems.item(i).getElementsByTagName("courtNo").item(0).firstChild != null)          
            res.courtNo = xItems.item(i).getElementsByTagName("courtNo").item(0).firstChild.nodeValue;
          
          if(xItems.item(i).getElementsByTagName("eventNo1").item(0).firstChild != null)          
            res.eventNo1 = xItems.item(i).getElementsByTagName("eventNo1").item(0).firstChild.nodeValue;
            
          if(xItems.item(i).getElementsByTagName("eventNo2").item(0).firstChild != null)    
            res.eventNo2 = xItems.item(i).getElementsByTagName("eventNo2").item(0).firstChild.nodeValue;
          
          if(xItems.item(i).getElementsByTagName("objNo").item(0).firstChild != null)
            res.objNo = xItems.item(i).getElementsByTagName("objNo").item(0).firstChild.nodeValue;
          
          if(xItems.item(i).getElementsByTagName("seldate").item(0).firstChild != null)
            res.seldate = xItems.item(i).getElementsByTagName("seldate").item(0).firstChild.nodeValue;
            
          if(xItems.item(i).getElementsByTagName("gammoney").item(0).firstChild != null)
            res.gammoney = xItems.item(i).getElementsByTagName("gammoney").item(0).firstChild.nodeValue;
          
          if(xItems.item(i).getElementsByTagName("minmoney").item(0).firstChild != null)
            res.minmoney = xItems.item(i).getElementsByTagName("minmoney").item(0).firstChild.nodeValue;
          
          if(xItems.item(i).getElementsByTagName("objuse").item(0).firstChild != null)
            res.objuse = xItems.item(i).getElementsByTagName("objuse").item(0).firstChild.nodeValue;
          
          if(xItems.item(i).getElementsByTagName("imgurl").item(0).firstChild != null)
            res.imgurl = xItems.item(i).getElementsByTagName("imgurl").item(0).firstChild.nodeValue;
            
          if(xItems.item(i).getElementsByTagName("address").item(0).firstChild != null)  
            res.address = xItems.item(i).getElementsByTagName("address").item(0).firstChild.nodeValue;
      
          eval("ins.result" + res.type).push(res); 
        }
   } 
 }
  
  this.getInfo = function(type){
    var oj = this.ins_ajx.sendRequest(ins.getResult, '&pcmd=1', 'GET', './mauction_proxy.php', false, true);
    
    var html = '';
    html += "<table width='100px' height='100px' border='0' style='font-size:8pt; margin-top:0px;font:돋움;'>";
    html += "<tbody><tr>";
    for(var i =0; i < eval("this.result" + type + ".length"); i++){
      var juso_ =  eval("this.result" + type + "[" + i + "].address").split(" ");
      html += "<td>";
      html += "<table width='100%' height='100%' border='0' cellpadding='0px' cellspacing='0px' style='font-size:8pt;cursor:hand;' onclick=javascript:window.open('http://www.mapauction.co.kr/easy/info/info_result_switch.php?courtNo=" + eval("this.result" + type + "[" + i + "].courtNo") + "&eventNo1=" + eval("this.result" + type + "[" + i + "].eventNo1") + "&eventNo2=" + eval("this.result" + type + "[" + i + "].eventNo2") + "&objNo=" + eval("this.result" + type + "[" + i + "].objNo") + "')>";
      html += "<tbody>";
      html += "<tr><td id='juso'>" + juso_[0] + juso_[1] + "</td></tr>";
      html += "<tr><td><img src='" + eval("this.result" + type + "[" + i + "].imgurl") + "' border='0' width='100px' height='100px'></td></tr>";
      html += "<tr><th align='center'>" + eval("this.result" + type + "[" + i + "].objuse") + "</th></tr>";
      html += "<tr><td align='right'>" + toComma(eval("this.result" + type + "[" + i + "].gammoney")) + "</td></tr>";
      html += "<tr><td align='right'>" + toComma(eval("this.result" + type + "[" + i + "].minmoney")) + "</td></tr>";
      html += "</tbody>";
      html += "</table>";
      html += "</td>";
    }
    html += "</tr></tbody>";
    html += "</table>";    
    
    return html;
  }
}



