var CONTEXT_NAME;

function getContextName(){
    // TODO cookie를 안쓸 경우엔 무조건 값이 "" 이 되어 버림.
    if (CONTEXT_NAME == null){
    	CONTEXT_NAME = getCookie("j_context_name");
    }
    return CONTEXT_NAME;
}

var global = new JGlobal();

var messages = new JMessage();

function JGlobal() {
    this.pageNo = 1;    
    this.help;
    this.query;
    this.orderIndex;
    this.orderTheadIndex;
    this.orderMode;
    this.orderColumn;
}

function JMessage() {
    this.init;
    this.messages;
    
    this.get = function(id, args) {
                   if (!this.init) {
                       this.messages = new Array();
                       this.messages["JSM-1001"] = "저장하시겠습니까?";
                       this.messages["JSM-1002"] = "아이디";
                       this.messages["JSM-1003"] = "삭제하시겠습니까?";
                       this.messages["JSM-1004"] = "{0} 입력해야 합니다.";
                       this.messages["JSM-1005"] = "{0}의 크기는 {1}이여야 합니다";
                       this.messages["JSM-1006"] = "{0}의 크기는 {1}과 {2} 사이여야 합니다";
                       this.messages["JSM-1007"] = "{0}에는 다음(', ., \") 특수 문자는 입력할 수 없습니다";
                       this.messages["JSM-1008"] = "{0}에는 숫자만을 입력해야 합니다";
                       this.messages["JSM-1009"] = "{0}의 숫자는 {1}이여야 합니다";
                       this.messages["JSM-1010"] = "{0}의 숫자는 {1} {2} 사이여야 합니다";
                       this.messages["JSM-1011"] = "{0} 선택해야 합니다";
                       this.messages["JSM-1012"] = "선택할 수 있는 {0}의 수는 {1} 입니다";
                       this.messages["JSM-1013"] = "선택할 수 있는 {0}의 수는 {1} {2} 사이여야 합니다";
                       this.messages["JSM-1014"] = "{0}의 금액은 {1}이여야 합니다";
                       this.messages["JSM-1015"] = "{0}의 금액는 {1} {2} 사이여야 합니다";
                       this.messages["JSM-1016"] = "{0} 올바르게 입력하여 주십시오";
                       this.messages["JSM-1017"] = "입력하신 {0} 전자우편주소로 유효하지 않습니다. 다시 입력하여 주십시오!";
                       this.messages["JSM-1018"] = "입력하신 {0} 날짜로 유효하지 않습니다. 다시 입력하여 주십시오!";
                       this.messages["JSM-1019"] = "{0}의 날짜는 {1} 이전 이어야 합니다";
                       this.messages["JSM-1020"] = "{0}의 날짜는 {1} 이후 이어야 합니다";
                       this.messages["JSM-1021"] = "선택할 수 있는 {0}의 날짜는 {1} {2} 사이여야 합니다";
                       this.messages["JSM-1022"] = "올바른 파일명이 아닙니다. 파일을 다시 입력해 주시기 바랍니다";
                       this.messages["JSM-1023"] = "{0}에 대한 파일을 첨부해야 합니다";
                       this.messages["JSM-1024"] = "{0}에 대한 첨부 가능한 파일의 확장자는 {1} 입니다";
                       this.messages["JSM-1025"] = "{0}에 대하여 확장자가 {1}인 파일은 첨부가 금지되어 있습니다";
                       this.messages["JSM-1026"] = "입력하신 {0} Crontab 표현식으로 유효하지 않습니다. 다시 입력하여 주십시오!";
                       this.messages["JSM-1027"] = "{0}의  {1} 올바른 속성 표현식이 아닙니다";
                       this.messages["JSM-1028"] = "{0}의 속성  {1} 필요합니다";
                       this.messages["JSM-1029"] = "관련 답글도 함께 삭제됩니다. 삭제하시겠습니까?";
                       this.init = true;
                   }
                   var message = this.messages[id];
                   if (!message) {
                       return id;
                   }
                   if (args) {
                       if (typeof args == "object" && args.length) {
                           for (var i = 0; i < args.length; i++) {
                               var pattern = new RegExp("\\{" + i + "\\}", "g"); 
                               message = message.replace(pattern, args[i]);
                           }
                       } else {
                           message = message.replace(/\{0\}/g, args);
                       }
                   }
                   return message;
               };

    this.alert = function(id, args) {
                     alert(this.get(id, args));
                 }
}

function JAdd() {
    this.params;

    this.param = jcontrol_param;
    
    this.addParam = jcontrol_addParam;

    this.run = function() {
                   var nextPage = jcontrol_getJspName() + "Form.jsp";
                   var query = jcontrol_getQueryString();
                   if (query != "") {
                       nextPage += "?" + query;
                   }
                   if (this.params) {
                       if (query != "") {
                           nextPage += "&" + this.params;
                       } else {
                           nextPage += "?" + this.params;
                       }
                   }
                   window.location.href = encodeURI(nextPage);
               };

}

function JAjax() {
    this.params;
    
    this.addParam = jcontrol_addParam;

    this.mode = function(mode) {
                    return this.addParam("mode", mode);
                };

    this.run = function(element, url, params) {
                   JLoadingMessage.show(element);
                   if (params) {
                       var callback = params.onComplete;
                       params.onComplete = function(originalRequest) {
                                            JLoadingMessage.hide();
                                            if (callback) {
                                                callback(originalRequest);
                                            }
                                        };
                       if (this.params) {
                           params.parameters = this.params;
                       }
                   } else if (this.params) {
                       params = {parameters: this.params, onComplete: JLoadingMessage.hide};
                   }
                   new Ajax.Request(url, params);
               };


    this.update = function(element, url, target, params) {
                      JLoadingMessage.show(element);
                      if (params) {
                          var callback = params.onComplete;
                          params.onComplete = function(originalRequest) {
                                                  JLoadingMessage.hide();
                                                  if (callback) {
                                                      callback(originalRequest);
                                                  }
                                              };
                          if (this.params) {
                              params.parameters = this.params;
                          }
                      } else if (this.params) {
                          params = {parameters: this.params, onComplete: JLoadingMessage.hide};
                      }
                      new Ajax.Updater(target, url, params);
                  };
}

function JBehind() {
    this.formIndex = 0;

    this.form = jcontrol_form;

    this.run = function(mode) {
                   if (window.validate && !validate(mode)) {
                       return;
                   }
                   var form = document.forms[this.formIndex];
                   form.action = form.action + jcontrol_getQuery();
                   jcontrol_addMode(form, mode);
                   form.submit();
               };
}

function JClose() {
    this.run = function() {
                   window.close();
               };
}

//TODO after 함수에 기본적으로 적용될 파라미터는?
//TODO firefox가 modal 창을 IE처럼 지원하지 않아 편법을 썼음. 리팩토링의 여지가 있음. 이와 관련 참고 참고 : http://www.gtalbot.org/BugzillaSection/Bug195867GDR_WindowOpen.html

var jcontrol_tempDatePopupMethod = null;

function JDatePopup() {
    this.after;
    this.popup = function(event, element, after) {
                     var url = getContextName() + "/popup/DatePopup.jsp";
                     var xfield = element.previousSibling;
                     /* IE */
                     if (window.showModalDialog) {
                         var ws = jcontrol_formatModalWindow(Event.pointerX(event) + window.screenLeft - 105,
                                                    Event.pointerY(event) + window.screenTop + 10,
                                                    250, 230);
                         var dt = showModalDialog(url, window, ws);
                         if (dt != null) {
                             if (dt.year == "") {
                                 xfield.value = "";
                             } else {
                                 xfield.value = dt.year + "-" + dt.month + "-" + dt.day;
                             }
                         }
                         if (after) after();
                     } else {
                         var ws = jcontrol_formatModalWindow(Event.pointerX(event) + window.screenX,
                                                    Event.pointerY(event) + window.screenY,
                                                    250, 230);
                         url += "?formName=" + xfield.form.getAttribute("name") + "&fieldName=" + xfield.name;
                         var win1 = window.open(url, "date", ws);
                         jcontrol_tempDatePopupMethod = after;
                     }
                 };
}

function addEvent(obj, evType, fn){
try{
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
 }catch(e){alert("catch");}
}

function JDelete() {
    this.formIndex = 0;
    this.idValue = "id";

    this.form = jcontrol_form;

    this.id = function(id) {
                  this.idValue = id;
                  return this;
              };

    this.run = function() {
                   var form = document.forms[this.formIndex];
                   if (new JCheck(messages.get("JSM-1002"), form[this.idValue]).validate()
                           && confirm(messages.get("JSM-1003"))) {
                       jcontrol_addMode(form, "delete");
                       form.action = form.action + jcontrol_getQuery();
                       form.submit();
                   }
               };
}

function JExcel() {
    this.formIndex = 0;

    this.form = jcontrol_form;

    this.run = function(mode) {
                   if (!mode) {
                       mode = "excel";
                   }
                   var form = document.forms[this.formIndex];
                   var input = jcontrol_addMode(form, mode);
                   form.target = "_new";
                   form.submit();
                   form.removeChild(input);
               };
}

function JHelp() {
    this.targetValue;

    this.target = function(target) {
                      this.targetValue = target;
                      return this;
                  };

    this.run = function(id) {
                   var target = "help-message";
                   if (this.targetValue) {
                       target = this.targetValue;
                   }
                   if (!$(target)) {
                       alert(target + "를 아이디로 하는 태그가 없습니다.");
                       return;
                   }
                   if (global.help == null || global.help != id) {
                       global.help = id;
                       var onComplete = function() {
                                            $(target).style.display = "block";
                                        }
                       new Ajax.Updater(target,
                                        getContextName() + "/HelpAjax.do?id=" + id,
                                        {asynchronous:true, onComplete:onComplete});
                   } else {
                       if ($(target).style.display == "block") {
                           $(target).style.display = "none";
                           global.help =  null;
                           return;
                       }
                   }
               };
}

function JLink() {
    this.nextPage;
    this.params;

    this.next = function(page) {
                    this.nextPage = page;
                    return this;
                }

    this.param = jcontrol_param;

    this.addParam = jcontrol_addParam;

    this.run = function(id) {
                   if (!this.nextPage) {
                       this.nextPage = jcontrol_getJspName() + "Form.jsp";
                   }
                   var part = "";
                   if (arguments.length == 1) {
                       part += "p_id=" + id;
                   } else {
                       for (var i = 0; i < arguments.length; i++) {
                           part += "&p_id" + (i + 1) + "=" + arguments[i];
                       }
                   }
                   if (this.params) {
                       part += "&" + this.params;
                   }
                   var query = jcontrol_getQueryString();
                   if (query != "") {
                       part += "&" + query;
                   }
                   window.location.href = encodeURI(this.nextPage + "?" + part.replace(/^&/, ""));
               };
}

function JList() {
    this.run = function() {
                   var nextPage = jcontrol_getJspName() + "List.jsp";
                   window.location.href = nextPage + jcontrol_getQuery();
               };
}

function JLoad() {
    this.formIndex = 0;

    this.form = jcontrol_form;

    this.run = function() {
                   var query = window.location.search;
                   query = decodeURI(query);
                   query = query.replace(/^\?(&)*/, "");
                   query = query.split("&");
                   var form = document.forms[this.formIndex];
                   for (var i = 0; i < query.length; i++) {
                       var index = query[i].indexOf("=");
                       var name = query[i].substring(0, index);
                       var value = query[i].substring(index + 1);
                       if (name.substring(0, 2) == "q_") {
                           if (form[name]) {
                               if (form[name].length){ // 배열로 전달되면
                                   for (var j = 0; j < form[name].length; j++) {
                                       if (form[name][j].value == value) {
                                           if (form[name][0].nodeName.toLowerCase() == "option") {
                                               form[name][j].selected = true;
                                           } else {
                                               form[name][j].checked = true;
                                           }
                                       }
                                   }
                               } else { // 배열이 아닌 input이면
                                   form[name].value = value;
                               }
                           } // if (form[name])
                       } else {
                           if (name == "orderIndex") {
                               global.orderIndex = value;
                           } else if (name == "orderTheadIndex") {
                               global.orderTheadIndex = value;
                           } else if (name == "orderBy") {
                               global.orderColumn = value;
                           } else if (name == "orderMode") {
                               global.orderMode = value;
                           } else if (name == "pageNo") {
                               global.pageNo = value;
                           }
                       }
                   }
                   jcontrol_sortToggle();
               };
}

var JLoadingMessage = {
    show : function(target) {
               var id = "loading-message";
               if (!$(id)) {
                   var div = document.createElement('div');
                   div.id = id;
                   document.body.appendChild(div);
                   Element.hide(id);
                   new Insertion.Bottom(id, "<img src='" + getContextName() + "/ext/image/loading.gif'/>");
                   $(id).style.position = "absolute";
               }
               var dim = Element.getDimensions(id);
               var targetDim = Element.getDimensions(target);
               var offsetTop, offsetLeft;
               offsetTop = (targetDim.height - dim.height) / 2;
               offsetLeft = (targetDim.width - dim.width) / 2;

               Position.clone(target,id,
                              {setLeft: true,
                               setTop: true,
                               setWidth: false,
                               setHeight: false,
                               offsetTop: offsetTop,
                               offsetLeft: offsetLeft
                              });
               Element.show(id);
           },

    hide: function() {
              var id = "loading-message";
              if ($(id)) {
                  Element.hide(id);
                  jcontrol_sortToggle();
              }
          }
};

function JNavigation() {
    this.params;

    this.run = function(pageNo, params) {
                   global.pageNo = pageNo;
                   if (params && params.pageNo) {
                       document.forms[0][params.pageNo].value = pageNo;
                   }
				   JNavigation.nextPage=params.nextPage;
                   var nextPage = JNavigation.nextPage ? JNavigation.nextPage + "?" : 
                                                         jcontrol_getJspName() + "List.jsp?";
                   global.query = nextPage + jcontrol_getQueryString();
                   if (isAjax) {
                       var target = "list";
                       var mode = "&mode=ajaxList";
                       if (params) {
                           if (params.target) {
                               target = params.target;
                           }
                           if (params.param) {
                               global.query += ("&" + params.param);
                           }
                           if (params.mode) {
                               mode = "&mode=" + params.mode;
                           }
                       }
                       JLoadingMessage.show(target);
                       new Ajax.Updater(target, global.query + mode, {asynchronous:true, onComplete:JLoadingMessage.hide});
                   } else {
                       if (params) {
                           if (params.param) {
                               global.query += ("&" + params.param);
                           }
                       }
                       window.location.href = encodeURI(global.query);
                   }
               };
}

function JSave() {
    this.formIndex = 0;

    this.form = jcontrol_form;

    this.run = function() {
                   if (window.validate && !validate("SAVE")) {
                           return;
                   }
                   if (confirm(messages.get("JSM-1001"))) {
                       var form = document.forms[this.formIndex];
                       form.action = form.action + jcontrol_getQuery();
                       var mode = "create";
                       if (window.location.search.indexOf("p_id") > 0 || 
                               form.getElementsByTagName("input")[0].type == "hidden") {
                           mode = "update";
                       }
                       jcontrol_addMode(form, mode);
                       form.submit();
                   }
               };
}

function JSearch() {
    this.run = function(params) {
                   new JNavigation().run(1, params);
               };
}

function JSort() {
    this.run = function(columnName, element, params) {
                   var row = element.parentNode.parentNode;
                   var elements = row.getElementsByTagName("th");
                   for (var i = 0; i < elements.length; i++) {
                       if (element.parentNode == elements[i]) {
                           global.orderIndex = i;
                           break;
                       }
                   }
                   var thead = row.parentNode;
                   elements = document.getElementsByTagName("thead");
                   for (var i = 0; i < elements.length; i++) {
                       if (thead == elements[i]) {
                           global.orderTheadIndex = i;
                           break;
                       }
                   }
                   global.orderColumn = columnName;
                   if (global.orderMode) {
                       global.orderMode = (global.orderMode == "ASC") ? "DESC" : "ASC";
                   } else {
                       global.orderMode = "DESC";
                   }
                   if (!global.pageNo) {
                       global.pageNo = 1;
                   }
                   new JNavigation().run(global.pageNo, params);
               };
}

function jcontrol_addMode(form, mode) {
    if (!form.mode) {
        var input = document.createElement("input");
        input.name = "mode";
        input.type = "hidden";
        input.value = mode;
        form.appendChild(input); // MS IE에서는 추가된 INPUT에 바로 접근할 수 없음.
        return input;
    } else {
        form.mode.value = mode;
        return form.mode;
    }
}

function jcontrol_addParam(name, value) {
    if (this.params) {
        this.params += "&";
    } else {
        this.params = "";                            
    }
    this.params += (name + "=" + value);
    return this;
}

function jcontrol_form(index) {
    this.formIndex = index;
    return this;
}

function jcontrol_formatModalWindow(left, top, width, height) {
    if (window.showModalDialog) {
        return "status:no; dialogLeft:" + left + "px; dialogTop:" + top + 
               "px; dialogWidth:" + width + "px; dialogHeight:" + height + "px";
    } else {
        return "modal=yes, left=" + left + "px, top=" + top + ", width=" + width + "px, height=" + height + "px";
    }
}

function jcontrol_getJspName() {
    var path = window.location.pathname;
    if (path.indexOf("Popup.jsp") > 0) {
        return path.replace("Popup.jsp", "Popup");
    } else if (path.indexOf("List.jsp") > 0) {
        return path.replace("List.jsp", "");
    } else if (path.indexOf("Form.jsp") > 0) {
        return path.replace("Form.jsp", "");
    } else if (path.indexOf("Detail.jsp") > 0) {
        return path.replace("Detail.jsp", "");
    }
    return path;
}

function jcontrol_getQuery() {
    var result = "";
    var query = window.location.search;
    if (!query || query.length == 0) {
        return result;
    }
    
    query = query.replace(/^\?(&)*/, "");
    query = query.split("&");
    for (var i = 0; i < query.length; i++) {
        if (query[i].substring(0, 2) != "p_") {
            if (result != "") {
                result += "&";
            } else {
                result += "?";
            }
            result += query[i];
        }
    }
    return result;
}

function jcontrol_getQueryString() {
    var result = "";
    var queryTags = document.getElementsByTagName("input");
    
    for (var i = 0; i < queryTags.length; i++) {

        var name = queryTags[i].name;
        var type = queryTags[i].type;
        var checked = queryTags[i].checked;
        
		if (name && name.substring(0, 2) == "q_") {
		    if (!((type == "checkbox" || type == "radio") && checked == false)){		                
	    		if (queryTags[i].value != null && queryTags[i].value != "") {
	                result += "&" + name + "=" + queryTags[i].value;
	            }		    
		    }  
		}     
    }

    queryTags = document.getElementsByTagName("select");
    for (var i = 0; i < queryTags.length; i++) {
        var name = queryTags[i].name;
        if (name && name.substring(0, 2) == "q_") {
            if (queryTags[i].value != null && queryTags[i].value != "") {
                result += "&" + name + "=" + queryTags[i].value;
            }
        }
    }
    
    if (global.pageNo != 1) {
        result += "&pageNo=" + global.pageNo;
    }
    
    if (global.orderColumn) {
        result += "&orderBy=" + global.orderColumn +
                      "&orderMode=" + global.orderMode +
                          "&orderIndex=" + global.orderIndex +
                          "&orderTheadIndex=" + global.orderTheadIndex;
    }
    return result != "" ? result.replace(/^&/, "") : result;
}

function jcontrol_param() {
    for (var i = 0 ; i < arguments.length; i++) {
        if (i == 0) {
            if (!this.params) {
                this.params = "";
            }
        }
        this.params += "&p_value" + (i + 1) + "=" + arguments[i];
    }
    return this;
}

function jcontrol_sortToggle() {
    if (global.orderColumn) {
        var thead = document.getElementsByTagName("thead")[global.orderTheadIndex];
        if (thead) {
            var elements = thead.getElementsByTagName("th");
            if (global.orderMode == "DESC") {
                elements[global.orderIndex].className = "desc";
            } else {
                elements[global.orderIndex].className = "asc";
            }
        }
    }
}

