/*if(navigator.userAgent.indexOf('Firefox') >= 0){
  (function(){
    var events = ["mousedown", "mouseover", "mouseout", "mousemove", "mousedrag", "click", "dblclick", "onkeyup", "onkeypress", "onkeydown"];
    for (var i = 0; i < events.length; i++){
      window.addEventListener(events[i], function(e){
        window.event = e;
      }, true);
    }
  }());
};*/
var isDOM = (document.getElementById ? true : false);
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);
var isNS = navigator.appName == "Netscape";

function findForm(name){
  return document.forms[name];
}
function getRef(id) {
  if (isDOM) return document.getElementById(id);
  if (isIE4) return document.all[id];
  if (isNS4) return document.layers[id];
}
function getRefNm(name){
  if (isDOM) return document.getElementsByName(name);
  if (isIE4) return document.all[name];
  if (isNS4) return document.layers[name];
}
function getOpenerRef(id){
  if (isDOM) return window.opener.document.getElementById(id);
  if (isIE4) return opener.document.all[id];
  if (isNS4) return opener.document.layers[id];
}
function getOpenerRefNm(name){
  if (isDOM) return window.opener.document.getElementsByName(name);
  if (isIE4) return opener.document.all[name];
  if (isNS4) return opener.document.layers[name];
}
function findElementInForm(obj, name) {
  frm = findForm(obj);
  for(var i=0; i<frm.length; i++) {
    var ele = frm[i];
    if (ele.getAttribute("name") == name || ele.name == name) {
      return ele;
    }
  }
  return null;
}
function ShowMessage(msg, args) {
  var delim = "^";
  var msgs = msg.split(delim);
  var params = args.split(delim);

  var message = "";
  for (var i = 0 ; i < msgs.length ; i++) {
    message += msgs[i];
    if (params.length <= i) continue;
    message += params[i];

    var j = i + 1;
    if (msgs.length <= j) continue;
    if (!(/^.{1}\(.{1}\)/).test(msgs[j])) continue;

    var bStr = msgs[j].substring(0, 4);
    var eStr = msgs[j].substring(4);
    var ch = params[i].charCodeAt(params[i].length - 1);

    if ((ch - 44032) % 28 > 0) msgs[j] = bStr.charAt(0) + eStr;
    else                       msgs[j] = bStr.charAt(2) + eStr;
  }

  alert(message);
}

String.prototype.trim = function()
{
 return this.replace(/(^\s*)|(\s*$)/gi, "");
}

String.prototype.replaceAll=function(searchStr, replaceStr){
    return this.split(searchStr).join(replaceStr);
}


/*
   * title - dialog 제목
   * width - 넓이
   * height - 높이
   * actionUrl - dialog 안에 추가될 페이지 URL
   * actionResizable - 크기조정 가능
   * modal - 모달창으로 할 것이냐?
   */

  function openWindow(title, actionUrl, width, height, resizable, modal, msg){

    if(width == null) width=300;
    if(height == null) height=300;
    if(resizable == null) resizable=false;
    if(modal == null) modal=true;

    $("<div></div>").attr("id", "popupWidnow").appendTo("body").dialog({
      width: width,
      height: height,
      title: title,
      resizable : resizable,
      modal : modal,
      close : closeWindow,
      dialogClass : 'user-dialog',
      draggable : false,
      overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
      buttons: {
              "닫기": function() {
                $(this).dialog("close");
              }
            }
    });

    if(!resizable){
      $("<div></div>").addClass("ui-resizable-s").appendTo(".user-dialog");
      $("<div></div>").addClass("ui-resizable-n").appendTo(".user-dialog");
      $("<div></div>").addClass("ui-resizable-w").appendTo(".user-dialog");
      $("<div></div>").addClass("ui-resizable-e").appendTo(".user-dialog");
      $("<div></div>").addClass("ui-resizable-sw").appendTo(".user-dialog");
      $("<div></div>").addClass("ui-resizable-nw").appendTo(".user-dialog");
      $("<div></div>").addClass("ui-resizable-se").appendTo(".user-dialog");
      $("<div></div>").addClass("ui-resizable-ne").appendTo(".user-dialog");
      $(".ui-resizable-s").css("width", (width));
      $(".ui-resizable-n").css("width", (width));
      $(".ui-resizable-w").css("height", (height-32));
      $(".ui-resizable-e").css("height", (height-32));
    }

    if(actionUrl != ""){
       spliteStr = actionUrl.split("?");
       var url = actionUrl;
       if(spliteStr.length == 2){
        url = spliteStr[0];
        xajax.initParameter();
        var splitPAry = spliteStr[1].split("&");
        for(var i = 0; i < splitPAry.length; i++){
          var key = splitPAry[i].split("=")[0];
          var value = splitPAry[i].split("=")[1];
          xajax.addParameter(key, value);
        }
       }
       xajax.simpleSend(url, function(str){
              $("#popupWidnow").html(str);
      });
    }else{
      $("#popupWidnow").html(msg);
    }
  }

  function closeWindow(){
    $("#popupWidnow").dialog('destroy').remove();
    $("#popupWidnow").dialog('close');
  }


  function alertDialog(title, message, modal, data, funcSuccess){

    if(modal == null) modal=true;

    $(document.body).prepend("<div id='alertDialog'><p align='center'>"+message+"</p></div>");
    $("#alertDialog").dialog({
        dialogClass:"alertDia",
        title: title,
        height: 120,
        width: 400,
        resizable:false,
        modal:modal,
        buttons: { "확인":function(){
                    $(this).dialog("close");
                    if(data != null){
                      funcSuccess(data);
                    }
                 }
          }
    });
  }

  function chkNull(objNm, msg, param){
    if(param == null){
      if(getRef(objNm).value == ""){
        alert(msg);
        getRef(objNm).focus();
        return false;
      }
    }else{
      if(getRef(objNm).value == ""){
        ShowMessage(msg, param);
        getRef(objNm).focus();
        return false;
      }
    }
    return true;
  }

  function chkNullNoFocus(objNm, msg){
    if(getRef(objNm).value == ""){
      alert(msg);
      return false;
    }
    return true;
  }

  /**
   * 금액필드인경우 3자리 마다 콤마
   * 금액을 한글로 변환
   * 숫자만 입력되도록 한다.
   */
  function amountUtil(obj, tObjNm){

    if(event != null && event.type != "click"){
     onlyNum();
    }

    if(obj.value.indexOf("0") == 0) obj.value = obj.value.replace("0", "");

    obj.value = obj.value.setComma();

    if(tObjNm != "" && getRef(tObjNm) != null){
      try{
    	  getRef(tObjNm).innerText = changeAmountHan(obj.value)+"원";
      }catch(e){};
    }

  }

  /**
   * usage :
   * <img src="xxxxxxx_On.gif" onmouseover="imgSwap(this);" onmouseout="imgSwap(this);" /> src가 _On인경우 _Off란 이미지로 교체
   * <img src="xxxxxxx_Off.gif" onmouseover="imgSwap(this);" onmouseout="imgSwap(this);" /> src가 _Off인경우 _On란 이미지로 교체
   */
  function imgSwap(obj){
    var imgSrc = obj.src;
    if(!obj.clicked){
        if(imgSrc.indexOf("On") > 0) obj.src = imgSrc.replace("On", "Off");
        else if (imgSrc.indexOf("Off") > 0) obj.src = imgSrc.replace("Off", "On");
        else if(imgSrc.indexOf("on") > 0) obj.src = imgSrc.replace("on", "off");
        else if (imgSrc.indexOf("off") > 0) obj.src = imgSrc.replace("off", "on");
    }
  }

  function MM_reloadPage(init) {  //reloads the window if Nav4 resized
    if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
      document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
    else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
  }
  MM_reloadPage(true);

  function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
  }

  function MM_preloadImages() { //v3.0
    var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
      var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
      if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
  }

  function MM_findObj(n, d) { //v4.01
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
      d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
  }

  function MM_swapImage() { //v3.0
    var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
     if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
  }

  /* 팝업 띄우기
   * scrollYn = 'yes'/'false'
   *
   * window_open('/example.jsp','EXAMPLE' , 400, 300, false);
   */
  function window_open(_url, _title, _width, _height, scrollYn){
  	LeftPosition=(screen.width)?(screen.width-_width)/2:100;
  	TopPosition=(screen.height)?(screen.height-_height)/2:100;
  	features ="scrollbars="+scrollYn+",status=false,statusbar=false,toolbar=false,resizable=0,location=false,menu=false,width="+_width+",height="+_height+",top="+TopPosition+",left="+LeftPosition+"";
    var curWin = window.open(_url,_title,features);
  	curWin.focus();
    return curWin;
  }


  /*
   * 머니 형태로 지정된 값에 모두 , 를 붙인다.
   */
  function makeFormat(){
	jQuery(".money").each(function(i){
	  val = jQuery(this).val();
	  if(val != ""){
	    jQuery(this).val(val.setComma());
		//alert(jQuery(this).val());
      }else{
    	  jQuery(this).val(0); // 금액은 입력하지 않을시에 무조건 0으로 들어간다.
      }
	});

	jQuery(".date1").each(function(i){
		  val = jQuery(this).val();
		  if(val != ""){
			val = val.split('-').join('');
			val1 = val.substring(0,4);
			val2 = val.substring(4,6);
			val3 = val.substring(6,8);

			jQuery(this).val(val1+"-"+val2+"-"+val3);

	      }
		});


  }

  function removeFormat(){
	jQuery(".money").each(function(i){
	  val = jQuery(this).val();
	  if(val != ""){
	    val = val.replace(/[,]/g,"");
		jQuery(this).val(val);
	  }else{
		jQuery(this).val(0); // 금액은 입력하지 않을시에 무조건 0으로 들어간다.
	  }
	});

	jQuery(".date1").each(function(i){
		  val = jQuery(this).val();
		  if(val != ""){
			val = val.split('-').join('');
			jQuery(this).val(val);
	      }
		});

  }

  /*
   * class가 required인 인풋값이 비었는지 체크
   */
  function simpleValidation(formId){
	  var result = true;
	  jQuery("#"+formId+" .required").each(function(i){
		  if(result){
			  var val = jQuery(this).val();
			  var title = jQuery(this).attr('title');
			  if(title == ""){
				  title = jQuery(this).attr('name');
			  }
			  if(val == ""){
				  alert(title+" 을(를) 입력하세요");
				  title = jQuery(this).focus();
				  result = false;
			  }
		  }
	  })
	  return result;
  }

  /*
   * 자릿수 채워지면 다음으로 이동
   */
  function fillNext(obj, length, nextEleName){
	  if(obj.value.length >= length){
		  if(jQuery(':input[name='+nextEleName+']').length !=0){
			  jQuery(':input[name='+nextEleName+']').focus();
		  }else{
			  jQuery(':input[id='+nextEleName+']').focus();
		  }
	  }

  }


  /**
   * 프린트 팝업창 띄위기
   */
  function openPrint(lonReqNo, docCode, tabCode){
    var url = "/mgaf/searchMain.do?lonReqNo="+lonReqNo+"&docCode="+docCode+"&tabCode="+tabCode;
    var curWin = window_open('', "printWindow", 880, 700, "yes");
    curWin.location = url;
  }

  function getCookie( name ){
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
    }
    return null;
  }
  function getCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
  endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
  }
  // 쿠키를 만듭니다. 아래 closeWin() 함수에서 호출됩니다

  function setCookie( name, value, expiredays )
  {
    var todayDate = new Date();
    todayDate.setDate( todayDate.getDate() + expiredays );
    document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";";
  }




