﻿
    function SearchBox(){
        this.Init = function(){
            this.FirstItemValue = "";
            this.SecondItemValue = "";
            this.KeywordBoxLength = 0;
            
            //var SearchParameter = location.search;
            
            if(Global_Request("FItem") == "")
                this.DivDisplay("SearchBoxHeader1")
            else
                this.DivDisplay("SearchBoxHeader2")
        }
        
        this.DivDisplayState = function(DivName){
            return $get(DivName).style.display;
        }
        
        this.DivDisplay = function(DivName){
            if(DivName == "FirstItemDiv"){
                this.SetInnerHTMLValue("FirstItemSelectedText", "전체");
                this.SetValue("FirstItemValue", "");
                this.SetValue("SecondItemValue", "");
                this.SetValue("KeywordBoxItemValue", "");
                this.SecondDivClose();
            }
            
            this.SetInnerHTMLValue("SecondItemSelectedText", "전체");
            
            if(this.DivDisplayState(DivName) == "block")
                $get(DivName).style.display = "none";
            else if(this.DivDisplayState(DivName) == "none")
                $get(DivName).style.display = "block";
        }
        
        this.GetInnerHTMLValue = function(ObjectID){
            return $get(ObjectID).innerHTML;
        }
        
        this.SetInnerHTMLValue = function(ObjectID, InputHTML){   
                 
            if(InputHTML.length > 7)
                InputHTML = InputHTML.substring(0,7)+"..";
            
            $get(ObjectID).innerHTML = InputHTML;
        }
        
        this.GetValue = function(ObjectID){
            return $get(ObjectID).value;
        }
        
        this.SetValue = function(ObjectID, InputValue){
            $get(ObjectID).value = InputValue;
        }
        
        this.FirstRequest = function(FirstItemValue){
            var FirstItemText = this.GetInnerHTMLValue("FirstItemText" + FirstItemValue);
            
            this.DivDisplay("FirstItemDiv");
            this.SetInnerHTMLValue("FirstItemSelectedText", FirstItemText);
            this.SetValue("FirstItemValue", FirstItemValue);
            this.SetValue("SecondItemValue", "");
        }
        
        this.SecondDivView = function(){
            this.FirstItemValue = this.GetValue("FirstItemValue");
            
            if(this.FirstItemValue == ""){
                alert("1차 분류를 선택하세요.");
                return;
            }else{
                DivName = "SecondItem" + this.FirstItemValue + "Div";
                
                if($get("SecondItem" + this.FirstItemValue + "Div") == null){
                    this.SecondItemValue = this.GetValue("SecondItemValue");
                    DivName = "SecondItem"+ this.FirstItemValue + "Sub" + this.SecondItemValue + "Div";
                    
                    if($get(DivName) == null)
                        DivName = "SecondItem"+ this.FirstItemValue + "Sub1Div";
                }
                    
                if((this.FirstItemValue == "1" && this.SecondItemValue == "2") || this.FirstItemValue == "2")
                    this.MetaListLoad("dept", "D", "", "0");
                else if(this.FirstItemValue == "1" && this.SecondItemValue == "3")
                    this.MetaListLoad("dept", "I", "", "0");
                else if(this.FirstItemValue == "4")
                    this.MetaListLoad("life", "D", "", "0");
            
                this.DivDisplay(DivName);
            }
        }
        
        this.SecondDivClose = function(){
            for(var i = 1; i <= 6; i++){
                if($get("SecondItem" + i + "Div") != null)
                    $get("SecondItem" + i + "Div").style.display = "none";
                else{
                    for(var j = 1; j <= 3; j++){
                        if($get("SecondItem" + i + "Sub" + j + "Div") != null)
                            $get("SecondItem" + i + "Sub" + j + "Div").style.display = "none";
                    }
                }
            }
        }
        
        this.SecondSubDivView = function(SubDivValue){
            if(SubDivValue != ""){
                this.SecondDivClose();
                this.SetValue("SecondItemValue", SubDivValue);
                this.SecondDivView();
            }
        }
        
        this.SecondRequest = function(SecondItemValue){
            var SecondItemText = this.GetInnerHTMLValue("SecondItemText" + SecondItemValue);
            
            this.SetInnerHTMLValue("SecondItemSelectedText", SecondItemText);
            this.SecondDivClose();
            this.SetValue("SecondItemValue", SecondItemValue);
        }
        
        this.SecondRequest2 = function(SecondIndexItemValue){
            this.SetInnerHTMLValue("SecondItemSelectedText", SecondIndexItemValue);
            this.SecondDivClose();
            this.SetValue("SecondItemValue", SecondIndexItemValue);
        }
        
        this.MetaListLoad = function(Type, Key, Keyword, Group){
            if(Type == null){
                alert("Type값이 없습니다.");
                return;
            }
                
            if(Key == null)
                Key = "";
            
            if(Keyword == null)
                Keyword = "";
            
            if(Group == null)
                Group = "";
                
            if(this.FirstItemValue == "1" && this.SecondItemValue == "2" && Keyword == "")
                WebServiceForSearch.GetMetaList(Type, Key, Keyword, Group, SecondItemMetaList, onError, "SecondItem1MedicalPartDiv");
            else if(this.FirstItemValue == "1" && this.SecondItemValue == "2" && Keyword != "")
                WebServiceForSearch.GetMetaList(Type, Key, Keyword, Group, SecondItemMetaList, onError, "SecondItem1ClinicDiv");
            else if(this.FirstItemValue == "1" && this.SecondItemValue == "3")
                WebServiceForSearch.GetMetaList(Type, Key, Keyword, Group, SecondItemMetaList, onError, "SecondItem1IndexClinicDiv");
            else if(this.FirstItemValue == "2")
                WebServiceForSearch.GetMetaList(Type, Key, Keyword, Group, SecondItemMetaList, onError, "SecondItem2MedicalPartDiv");
            else if(this.FirstItemValue == "4")
                WebServiceForSearch.GetMetaList(Type, Key, Keyword, Group, SecondItemMetaList, onError, "SecondItem4LifeDiv");
        }
     
        this.KeywordBox = function(InputKey){
            this.SetValue("KeywordBoxItemValue", "");
            
            if(InputKey != "")
                WebServiceForSearch.GetKeywordList(InputKey, SecondItemMetaList, onError, "KeywordListDiv")
            else
                this.KeywordBoxOut();
        }
        
        this.KeywordBoxOut = function(){
            $get("keywordBoxDiv").style.display = "none";
            this.KeywordBoxInfoSend();
        }
        
        this.FlashRequest = function(MetaSrno, MetaName){
            this.SetInnerHTMLValue("SecondItemSelectedText", MetaName);
            this.SecondDivClose();
            this.SetValue("SecondItemValue", MetaSrno);
        }
        
        this.KeywordBoxMove = function(EventKeyCode){
            var KeywordBoxFocusNumber = this.GetValue("KeywordBoxItemValue");
            var KeywordBoxLength = this.KeywordBoxLength;
            var Keyword = this.GetValue("Keyword");
            
            if(KeywordBoxLength != 0 && Keyword != ""){
                $get("keywordBoxDiv").style.display = "block";
                
                if(KeywordBoxFocusNumber == ""){
                    KeywordBoxFocusNumber = 0;
                }else if(EventKeyCode == 40)
                    KeywordBoxFocusNumber++;
                else if(EventKeyCode == 38)
                    KeywordBoxFocusNumber--;
                
                if(KeywordBoxFocusNumber == KeywordBoxLength)
                    KeywordBoxFocusNumber--;
                else if(KeywordBoxFocusNumber < 0){
                    this.SetValue("KeywordBoxItemValue", "")
                    this.KeywordBoxOut();
                }else{
                    for(var i = 0; i < KeywordBoxLength; i++){
                        if($get("SearchBoxTr" + i) != null){
                            if(i == KeywordBoxFocusNumber){
                                $get("SearchBoxTr" + i).bgColor = "#cccccc";
                                
                                this.SetValue("KeywordBoxItemValue", i);
                                this.KeywordBoxInfoSend(i)
                            }else
                                $get("SearchBoxTr" + i).bgColor = "#ffffff";
                        }
                    }
                }
            }
        }
        
        this.KeywordBoxInfoSend = function(SendInfoNumber){
            if(SendInfoNumber == null || SendInfoNumber == "")
                var SendInfoNumber = this.GetValue("KeywordBoxItemValue");
            
            if(SendInfoNumber != ""){
                var SendInfo = this.GetInnerHTMLValue("SecondItemText" + SendInfoNumber);
                
                this.SetValue("Keyword", SendInfo);
            }
        }
        
        this.Init();
    }
    
    
    
    
    
    function pageLoad(s, e){
        SBObject = new SearchBox();
    }
    
    function SearchBoxCheck(){
        var InputFirstItemValue = $get("FirstItemValue").value;
        var InputSecondInfo = $get("SecondItemSelectedText").innerHTML;
        var InputKeyword = $get("Keyword").value;
        var KeywordCheck = "";
        var FItem = "", SItem = "", ReSearch = "";
        
        if(InputFirstItemValue != ""){
            switch(InputFirstItemValue){
                case "1":
                    FItem = "C";
                    break;
                case "2":
                    FItem = "D";
                    break;
                case "3":
                    FItem = "B";
                    break;
                case "4":
                    FItem = "L";
                    break;
                case "5":
                    FItem = "K";
                    break;
                case "6":
                    FItem = "E";
                    break;
            }
        }
        
        if(InputSecondInfo != "전체"){
            KeywordCheck = "Y";
            SItem = $get("SecondItemValue").value;
        }
        
        if(KeywordCheck == "" && InputKeyword == ""){
            alert("검색어를 입력하세요.")
            return false;
        }
        
        SearchItems[0] = escape(FItem);
        SearchItems[1] = escape(SItem);
        SearchItems[2] = escape(InputKeyword);
        
        WebServiceForSearch.SetKeywordList(InputKeyword, onSearch, onError, SearchItems);
        
        return false;
    }