var Search={
  showTab:function(sTab){
    var oEntitySelected = document.getElementById("entity-selected");
    var sEntitySelected = oEntitySelected.value;
    
    var sSearchText = this.trim(document.getElementById("search-text-" + sEntitySelected).value);
    
    var sSearchEntity = sTab;
    if (sSearchText.indexOf(document.getElementById("text-field-search-A").value)<0 && sSearchText.indexOf(document.getElementById("text-field-search-B").value)<0 && sSearchText!=""){  
      this.doSearch(sSearchEntity,sSearchText);
    }
    else{
      var oPrimaryDiv = document.getElementById("primary");
      var oSecondaryDiv = document.getElementById("secondary");
      var oPrimaryTab = document.getElementById("tab-primary");
      var oSecondaryTab = document.getElementById("tab-secondary");
            
      if (oPrimaryDiv.id==sTab){
        this.hideElement(oPrimaryDiv);
        this.hideElement(oPrimaryTab);
        this.hideElement(oSecondaryDiv);
        this.hideElement(oSecondaryTab);        
        oPrimaryTab.className="on"; 
        oSecondaryTab.className="";        
        oEntitySelected.value = "primary";
        
        if (oPrimaryTab.firstChild.nodeType==1){
          oPrimaryTab.removeChild(oPrimaryTab.firstChild);
        }
        var oPrimaryChild = document.createTextNode(document.getElementById("section1-B").value);
        oPrimaryTab.appendChild(oPrimaryChild);
        oSecondaryTab.removeChild(oSecondaryTab.firstChild);
        var oSecondaryLink = document.createElement("a");
        oSecondaryLink.href="#";
        EventUtil.addObjEventListener(oSecondaryLink,"click",oSecondaryLink,function e(){Search.showTab('secondary')});
        oSecondaryLink.appendChild(document.createTextNode(document.getElementById("section2-B").value));        
        oSecondaryTab.appendChild(oSecondaryLink);
        
        this.showElement(oPrimaryTab);
        this.showElement(oPrimaryDiv);
        this.showElement(oSecondaryTab);
      }
      else if (oSecondaryDiv.id==sTab){
        this.hideElement(oPrimaryDiv);
        this.hideElement(oPrimaryTab);
        this.hideElement(oSecondaryDiv);
        this.hideElement(oSecondaryTab);
        oPrimaryTab.className="";
        oSecondaryTab.className="on";        
        oEntitySelected.value = "secondary";
        
        if (oSecondaryTab.firstChild.nodeType==1){
          oSecondaryTab.removeChild(oSecondaryTab.firstChild);
        }
        var oSecondaryChild = document.createTextNode(document.getElementById("section2-B").value);
        oSecondaryTab.appendChild(oSecondaryChild);
        oPrimaryTab.removeChild(oPrimaryTab.firstChild);
        var oPrimaryLink = document.createElement("a");
        oPrimaryLink.href="#";
        EventUtil.addObjEventListener(oPrimaryLink,"click",oPrimaryLink,function e(){Search.showTab('primary')});
        oPrimaryLink.appendChild(document.createTextNode(document.getElementById("section1-B").value));
        oPrimaryTab.appendChild(oPrimaryLink);
        
        this.showElement(oPrimaryTab);
        this.showElement(oSecondaryDiv);
        this.showElement(oSecondaryTab);
      }
    }
  },
  
  showElement:function(oElement){
    oElement.style.visibility="visible";
    oElement.style.display="block";
  },
  
  hideElement:function(oElement){
    oElement.style.visibility="hidden";
    oElement.style.display="none";
  },
  
  trim:function(s)
  {
    if (s==null) return null;
    var r = s;
    while (r.length>0 && (r.charAt(0)==' '||r.charAt(0)=='\r'||r.charAt(0)=='\n'||r.charAt(0)=='\t')) {
      r = r.substring(1, r.length);
    }
    while (r.length>0 && (r.charAt(r.length-1)==' '||r.charAt(r.length-1)=='\r'||r.charAt(r.length-1)=='\n'||r.charAt(r.length-1)=='\t')) {
      r = r.substring(0, r.length-1);
    }
    return r;
  },
  
  doSearch:function(sSearchEntity,sText){
    var sSearchText = sText;
    if (sSearchText==undefined){
      sSearchText=this.trim(document.getElementById("search-text-" + sSearchEntity).value);
    }
        
    if (sSearchText.indexOf(document.getElementById("text-field-search-A").value)<0 && sSearchText.indexOf(document.getElementById("text-field-search-B").value)<0 && sSearchText!=""){
      var sHost = document.getElementById("host-static").value;
      var tagsSplitted = sSearchText.split(" ");
      var query = "";
      for (var i=0; i<tagsSplitted.length; i++){
        query = query + tagsSplitted[i];
        if (i<tagsSplitted.length - 1){
          query = query + "-";
        }
      }
      
      var sSearchEntityUrl = "";
      if (sSearchEntity=="primary"){
        sSearchEntityUrl = document.getElementById("section1-A").value;
      }
      else{
        sSearchEntityUrl = document.getElementById("section2-A").value;
      }
      window.document.location.href = sHost + "/buscar/" + sSearchEntityUrl + "/" + escape(query);
    }
    else{
      this.showTab(sSearchEntity);
    }
  },
  
  focusSearchBox:function(box){
    if ((box.value.indexOf(document.getElementById("text-field-search-A").value)>=0 || box.value.indexOf(document.getElementById("text-field-search-B").value)>=0) && box.value!=""){
      box.value="";
    }
  },
  
  blurSearchBox:function(box){
    if (box.value==""){
      var sSection = box.id.substring(box.id.indexOf("search-text-")+12);
      var sDefaultText = "";
      
      if (sSection=="primary"){
        sDefaultText = document.getElementById("text-field-search-A").value;
      }
      else{
        sDefaultText = document.getElementById("text-field-search-B").value;
      }
    
      box.value=sDefaultText;
    }
  }
}

function search(sSearchEntity,sText){
  Search.doSearch(sSearchEntity,sText);
}
