/************************************************************
Funções Genericas - goshme

*************************************************************/

function displaySearch(tipo){
    document.frm.shortType.value = tipo;

    if(tipo != '0'){
      document.frm.operator.value = '3';
    }else{
      document.frm.operator.value = '2';
    }

    var idName = 'link'+tipo;
    var link = getElt(idName);
    link.className = "cssSearchAtivo";

    var tabs = document.getElementsByTagName('a');
    for( var i=0; i<tabs.length; i++ ) {
	var tabi = tabs[i];

	if ( tabi.id == idName && tabi.className == "cssSearch" ) {
	  tabi.className="cssSearchAtivo";
	} else if ( tabi.id == idName && tabi.className=="cssSearchAtivo" ) {
	  tabi.className="cssSearchAtivo";
	} else if ( tabi.className=="cssSearchAtivo" ) {
	  tabi.className="cssSearch";
	}
    }//for

   document.frm.query.focus();
   document.frm.query.style.color ='black';
}

function displayMore(e){
  stopB(e);
  var elems=document.getElementsByName('more');
  //alert(elems.length);
  for(var i=0;i<elems.length;i++){
    var obj=elems[i];
    var dp="";
    if(obj.style.display==""){
      dp="none";
    }
    obj.style.display=dp;
  }//for
  return false;
}

function stopB(e){
  if(!e){
    e=window.event;
  }
  e.cancelBubble=true;
}

function validaSearcher(frm,adv){
  var retorno = false;

  var query = frm.query.value;
  query = query.replace(/^\s+/, '');
  query = query.replace(/\s+$/, '');

  //Valida a query
  if(query.length == 0){
    alert("please, insert query to search!");
    return false;
  }
  else{
    retorno = true;
  }
  //alert(retorno);

  //Advanced
  if(adv == true){
     format(frm);
  }

  //alert(retorno);
  if(query == 'ex: soil fertilizer'){
     frm.query.value = 'soil fertilizer';
  }

  if (retorno == true){
    frm.js.value = 'true';
    return true;
  } else{
    return false;
  }
}

function didYouMeanSearch(query){
  document.frm.query.value = query;
  document.frm.submit();
}

function open_windows(type,url){
    var options='';
    if(type == 'DEBUG')
        options = 'width=980,height=550,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,fullscreen=no';
    else
    if(type == 'TIPS')
        options = 'width=650,height=460,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,fullscreen=no';
    else
    if(type == 'DEBUGLM')
        options = 'width=570,height=850,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,fullscreen=no';
    else
    if(type == 'DEBUGCORI')
        options = 'width=570,height=850,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,fullscreen=no';

    if(options != '')
        window.open(url,type,options);
    else
        window.open(url,type);
}

function feedBack(feed,nameSession){
  url_aux = 'feedback.do?operator=33&feedback='+feed+'&nameSession='+nameSession;
  sendRequest(url_aux,'operator=33','POST');

  var feedback = getElt('divFeeback');
  feedback.innerHTML = '<font face="Verdana" style="font-size: 9pt">Thanks for your feedback!</font>';

}

function openFilter(type,idCategory,category,idSubCategory,subCategory,nameSession){
  var menuAtivo = getMenuAtivo();

  if(idSubCategory){
     var url_aux = 'goshme.do?operator=32&typeFilter='+type+'&filterCategory=' + idCategory +'&filterSubCategory=' + idSubCategory + '&menuAtivo='+menuAtivo + '&nameSession='+nameSession;
  }else{
     var url_aux = 'goshme.do?operator=32&typeFilter='+type+'&filterCategory=' + idCategory + '&menuAtivo='+menuAtivo + '&nameSession='+nameSession;
  }

  //alert(url_aux);
  sendRequest(url_aux,'operator=32','GET');

  var divResultSpecific = getElt('divResultSpecific');
  divResultSpecific.style.display='none';

  var divResultGeneral = getElt('divResultGeneral');
  divResultGeneral.style.display='none';

  var divRodape = getElt('divRodape');
  if(divRodape){
    divRodape.style.display='none';
  }

  var divPaginador = getElt('divPaginador');
  if(divPaginador){
    divPaginador.style.display='none';
  }

  var divGoogle1 = getElt('divGoogle1');
  if(divGoogle1){
    divGoogle1.style.display='none';
  }

  var divGoogle2 = getElt('divGoogle2');
  if(divGoogle2){
    divGoogle2.style.display='none';
  }

  var divInfo = getElt('divInfo');
  divInfo.style.display='';

  var divNoResult = getElt('divNoResult');
  if(divNoResult){
    divNoResult.style.display='none';
  }

  var msg1 = getElt('msg1');
  if(openFilter)
  msg1.innerHTML = 'Extending consults for '+category;
  if(subCategory){
     msg1.innerHTML = 'Extending consults for '+category + ' / ' + subCategory;
  }
  var msg2 = getElt('msg2');
  msg2.innerHTML = 'Rearranging Search Engines';

  //Google Analitics
  urchinTracker('/filter/idCategory='+idCategory);
}

function openShortcut(tipo,nameSession,keywordOriginal){
  var keyword = document.frm.query.value;
  //alert(keyword);

  if(keywordOriginal == keyword){
    var idCategory = '';
    var category = '';
    var idSubCategory = '';
    var subCategory = '';
    if(tipo == '1'){
       idCategory = '5';
       category = 'Audio, Video & Images';
       idSubCategory = '2';
       subCategory == 'Imagens';
    }else if(tipo == '2'){
       idCategory = '5';
       category = 'Audio, Video & Images';
       idSubCategory = '20';
       subCategory = 'Audio';
    }else if(tipo == '3'){
       idCategory = '5';
       category = 'Audio, Video & Images';
       idSubCategory = '80';
       subCategory = 'Videos';
    }else if(tipo == '4'){
       idCategory = '12';
       category = 'News';
       idSubCategory = '';
       subCategory = '';
    }else if(tipo == '5'){
       idCategory = '27';
       category = 'Shopping';
       idSubCategory = '';
       subCategory = '';
    }else if(tipo == '6'){
       idCategory = '4';
       category = 'Economics & Business';
       idSubCategory = '40';
       subCategory = 'Jobs';
    }else if(tipo == '7'){
       idCategory = '72';
       category = 'Blogs';
       idSubCategory = '';
       subCategory = '';
    }

    closeAllMenu();
      //showSub(idCategory,true,nameSession);
    if(idSubCategory){
      ativoVet[idCategory] = true;
    }
    openFilter('FS',idCategory,category,idSubCategory,subCategory,nameSession);
  }else{
    //New Search
    document.frm.shortType.value = tipo;
    document.frm.operator.value = '3';

    //alert(document.frm.shortType.value);
    document.frm.submit();
  }
}

function showShortcut(shot,type,idCategory,idSubCategory){
  var cat = getCategory(idCategory);
  cat.checked = shot.checked;
  var tam = cat.alt;
  if (! tam || tam == '0') return;

  if(type == 'S'){
     showSub(idCategory,true,'','2');
  }

  for (var i=1;i < tam+1;i++) {
    var subItem = getElt('S_'+idCategory+'_'+i);
    if(!subItem)
      continue;
    if(type == 'S'){
       if(subItem.value == idSubCategory){
          subItem.checked = shot.checked;
          break;
       }
    }else{
      subItem.checked = shot.checked;
    }
  }//for
}

function displayShortcut(shortName){
  var shortCutMenu = getElt('Shortcuts');
  var shortCut = getElt(shortName);

  shortCut.style.display='block';
  shortCutMenu.style.display='block';
}

function setTips(){
  document.frm.query.value = 'ex: soil fertilizer';
}

function qs(el){
   if (window.RegExp && window.encodeURIComponent) {
      var ue=el.href;
      var qe=encodeURIComponent(document.frm.query.value);
      if(ue.indexOf("query=") != -1){
         el.href=ue.replace(new RegExp("query=[^&$]*"),"query="+qe);
      }else{
         el.href=ue+"?query="+qe;}
      }
      return 1;
}

function clearFunc(){
  if(document.frm.query.value == 'ex: soil fertilizer'){
    document.frm.query.value = '';
  }
  document.frm.query.style.color ='black';
}

function category_check(obj){
  var tam = obj.alt;
  var idCategory = obj.value;
  if (! tam || tam == '0') return;

  //alert(idCategory+'#');
  //showSub(idCategory,'','','2');

  for (var i=1;i < tam+1;i++) {
     var subItem = getElt('S_'+idCategory+'_'+i);
     if(subItem){
       subItem.checked = obj.checked;
     }
  }//for
}

function subCategory_check(chSub,id){
  var cat = getElt('C'+id);
  //alert(cat);
  if(cat.checked == false){
    cat.checked = true;
  }
}

function specialized_check(valor){
  for (var i=0;i<document.frmAdv.elements.length;i++) {
     var check = document.frmAdv.elements[i];
     if (check.type == 'checkbox' && (check.name.substring(0,1) == 'C' || check.name.substring(0,1) == 'S') ) {
          check.checked = valor.checked;
     }
  }//for
}

function getCategory(idCategory){
  for (var i=0;i<document.frmAdv.elements.length;i++) {
     var check = document.frmAdv.elements[i];
     if (check.type == 'checkbox' && (check.name.substring(0,1) == 'C' && check.value == idCategory) ) {
          return check;
     }
  }//for
}

function format(frm){
  var listCategory='';
  var listSubCategory='';

  for (var i=0;i<frmAdv.elements.length;i++) {
     var check = frmAdv.elements[i];

     if (check.type == 'checkbox' && check.name.substring(0,1) == 'C'){
        if(check.checked){
            var idCategory = check.value;
            var tam = check.alt;
            var countChk=0;
            //alert(idCategory+'-'+tam);
            var listAux='';
            for(var j=1;j<=tam;j++){
                 var subCheck = getElt('S_'+idCategory+'_'+j);
                 //alert(subCheck.name+'-'+subCheck.checked);
                 if(subCheck && subCheck.checked){
                       listAux = listAux + ',' + subCheck.value;
                       countChk++;
                       //alert(listAux+'-'+countChk);
                   }
            }//for
            //alert(countChk+'-'+tam);
            if(countChk == tam || countChk == 0){
               listCategory = listCategory + ',' + idCategory;
            }else{
               listSubCategory = listSubCategory + listAux.substring(1,listAux.length) + ',';
            }
       }//fi

     }else if(check.type == 'checkbox'){
           if (check.name == 'chkGeneral' && check.checked){
              frm.hasGeneral.value = 'true';
           }
     }
  }//for
  //alert(listCategory.substring(1,listCategory.length));
  //alert(listSubCategory.substring(0,listSubCategory.length-1));

  frm.listCategories.value = listCategory.substring(1,listCategory.length);
  frm.listSubCategories.value = listSubCategory.substring(0,listSubCategory.length-1);
}

function getResult(nameSession){
   //alert('teste1');
   var divInfo = getElt('divInfo');
   //alert(divInfo);
   divInfo.style.display = '';
   sendRequest('goshme.do?operator=31&nameSession='+nameSession,'operator=30','GET');
}

var ativoVet = new Array();
function showSub(id,show,nameSession,type){
  var subMenu = getElt('DV'  + id);
  if (! subMenu) return;
  if(!show){
    if(!subMenu.style.display){
      show = false;
    }else{
      show = true;
    }
  }

  if(!type){
    type = '1';
  }

  if(type == '1'){
     var url = 'goshme.do?operator=30&type=1&idCategory='+id + '&nameSession='+nameSession;
  }else{
     var url = 'goshme.do?operator=30&type=2&idCategory='+id;
  }

  if (show) {
    var menu = subMenu.innerHTML;
    //alert(menu);
    if(menu == 'null'){
      sendRequest(url,'operator=30','GET');
    }else{
      setMenuAtivo(id,true);
    }
    //fi
  }else{
    setMenuAtivo(id,false);
  }
  //fi
}

function error_session(){
  location = '/index.jsp?error=1';
}

function setMenuAtivo(id,show){
   //alert(id);
   var subMenu = getElt('DV'  + id);
   var img = getElt('img_'  + id);
   if (! subMenu) return;

   /*if(!show){
     alert(subMenu.style.display);
     if(!subMenu.style.display){
       show = false;
     }else{
       show = true;
     }
   }*/

   if (show) {
      subMenu.style.display = '';
      img.src = "images/minus.gif";
      ativoVet[id] = true;
   } else{
     if(subMenu.style.display != 'none'){
       subMenu.style.display = 'none';
     }
     img.src = "images/more2.gif";
     ativoVet[id] = false;
   }
}

function closeAllMenu(){
  //alert(ativoVet.length);
  for(i=0;i<=ativoVet.length -1;i++){
     if(ativoVet[i] == true){
       showSub(i,false);
     }
  }
}

function getMenuAtivo(){
  var div = '';
  for(i=0;i<=ativoVet.length -1;i++){
     if(ativoVet[i] == true){
       div = div + i + ',';
     }
  }
  if(ativoVet.length == 0){
    return div;
  }else{
    return div.substring(0,div.length - 1);
  }
}

function openMore(show,idsMenu){
  var idMenu = '';
  for(i=0;i<=idsMenu.length -1;i++){
     idMenu = idsMenu[i];
     var divMenu = getElt('menu'+idMenu);

     var more  = getElt('moreCategory');
     var minus = getElt('minimizeCategory');
     if(show){
        divMenu.style.display = '';
        more.style.display = 'none';
        minus.style.display = '';
     }else{
        setMenuAtivo(idMenu,false);
        divMenu.style.display = 'none';
        more.style.display = '';
        minus.style.display = 'none';
     }
  }
}

function validaEmail(email1,email2){
  if (email1.indexOf("@") < 1 || email1.indexOf(".") < 1){
    alert("Please, fill correct e-mail");
    return false;
  }

  if(email1 != email2){
    alert("Different e-mails.")
    return false;
  }

  return true;
}

/******** PREVIEW *********/
function getElt(id) {
  return document.getElementById ? document.getElementById(id) :
           (document.all ? document.all[id] : 0);
}

function toggle(id, show) {
  var elt = getElt(id);
  if (! elt) return;
  if (show > 0 || (! show && elt.style.display == 'none')) {
    elt.style.display = '';
    toggled = displayed = id;
  } else {
    elt.style.display = 'none';
    toggled = displayed = 0;
  }
}

function oc(){}

function display(id, show) {
  var elt = getElt(id);
  if (toggled && (toggled != id)) {
    toggle(toggled);
  }
  if (! elt) return;
  if (show) {
    elt.style.display = '';
    displayed = id;
  } else if (! toggled) {
    elt.style.display = 'none';
    displayed = 0;
  }
}

function pview(id, show, post) {
  var lk = getElt('po-' + id);
  var fr = getElt('pf-' + id);
  if (! lk || ! fr) return false;
  toggle('pf-' + id, show);
  toggle('po-' + id, - show);
  toggle('pc-' + id, show);
  if (! show) return;
  if (fr.src) ;
  else if (post) postlink(lk, 'pf-'+id);
  else fr.src = lk.href;
  return false;
}

function posturl(url,target) {
  var form = document.createElement("form");
  var u = url.split('?', 2);
  form.action = u[0];
  form.method = "post";
  if (target) form.target = target;
  form.style.display = "none";
  var qs = new Parse(u[1],"&","=",1);
  for (var i = 0; i < qs.params.length; i+=2) {
    var input = document.createElement("input");
    form.appendChild(input);
    input.name = qs.params[i];
    input.value = qs.params[i+1];
  }
  document.body.appendChild(form);
  form.submit();
  return false;
}

function postlink(link, target) {
  return posturl(link.href, target ? target : link.target);
}
