home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 October / enter-2005-10.iso / files / copernicagentbasic.exe / %SUPPORTTEMP% / CopernicAgentExt.dll / HTML / INTEGRATION_BAND_SEARCHBAR_JS < prev    next >
Encoding:
Text File  |  2005-11-14  |  4.9 KB  |  243 lines

  1. // Copernic Agent helper methods for Internet Explorer 
  2.  
  3.  
  4. // ShowAdvancedSearch & StartSearch result code
  5. copernicAgent_OK = 0;
  6. copernicAgent_Failed = -1;
  7. copernicAgent_QueryTooLong = -2;
  8.  
  9. // Search mode
  10. copernicAgent_ModeAllWords = 0;
  11. copernicAgent_ModeAnyWords = 1;
  12. copernicAgent_ModeExactPhrase = 2;
  13.  
  14. // Internal constants
  15. copernicAgent_ProtocolPrefix = "copernicagentsearch:";
  16. copernicAgent_SearchWindowName = "CopernicAgentSearch";
  17. copernicAgent_ProtocolFlagInteractive = 1;
  18.  
  19. // Internal variable
  20. var copernicAgent = null;
  21.  
  22.  
  23. // Initialize Copernic Agent helper object
  24. function copernicAgent_Init() 
  25. {
  26.   try 
  27.   {
  28.     copernicAgent = new ActiveXObject("CopernicAgentExt.ApplicationHelper");
  29.     return copernicAgent_OK;    
  30.   }
  31.   catch (e) 
  32.   {
  33.     copernicAgent = null;    
  34.     return copernicAgent_Failed;
  35.   }
  36. }
  37.  
  38. // Load Copernic Agent category list
  39. function copernicAgent_LoadCategoryList(comboBox, strDefaultCategory)
  40. {
  41.   if (copernicAgent)
  42.   {
  43.     try
  44.     {
  45.       var categories = copernicAgent.categories;
  46.       var count = categories.count;
  47.       var i = 0;
  48.         
  49.       // Load category list
  50.       comboBox.length = count;
  51.       while (i < count) 
  52.       {
  53.         var category = categories.item(i);
  54.         
  55.         comboBox.options[i].text = category.displayname;
  56.         comboBox.options[i].value = category.id;
  57.        
  58.         i++;        
  59.       }
  60.   
  61.       if (i == 0)
  62.       {
  63.         // No category available
  64.         comboBox.length = 1;
  65.         comboBox.options[0].text = strDefaultCategory;
  66.         comboBox.options[0].value = "";
  67.       }
  68.    
  69.       // Select first category
  70.       if (comboBox.selectedIndex < 0)
  71.        ASelectBox.selectedIndex = 0;
  72.        
  73.       return copernicAgent_OK;
  74.     }
  75.     catch (e)
  76.     {
  77.       return copernicAgent_Failed;
  78.     }
  79.   }
  80.   else
  81.   {
  82.     comboBox.length = 1;
  83.     comboBox.options[0].text = strDefaultCategory;
  84.     comboBox.options[0].value = "";
  85.     
  86.     return copernicAgent_Failed;
  87.   }
  88. }
  89.  
  90.  
  91. // Get Copernic Agent version string
  92. // Returns "" if the version is lower than 6.1 or is unknown
  93. function copernicAgent_GetVersion() 
  94. {
  95.   if (copernicAgent)
  96.   {
  97.     try 
  98.     {
  99.       return copernicAgent.Version;
  100.     }
  101.     catch (e) 
  102.     {
  103.       return "";
  104.     }  
  105.   }
  106.   else
  107.   {
  108.     return "";
  109.   }
  110. }
  111.  
  112.  
  113. // Show advanced search dialog
  114. function copernicAgent_ShowAdvancedSearch(category, query, mode)
  115. {
  116.   if (copernicAgent)
  117.   {
  118.     var url = copernicAgent_ProtocolPrefix + escape(escape(query)) + ":" + escape(escape(category)) + ":" + mode + ":" + copernicAgent_ProtocolFlagInteractive;
  119.  
  120.     if (url.length <= 256) 
  121.     {
  122.       top.window.window.navigate(url);
  123.     
  124.       if (window.name == copernicAgent_SearchWindowName)
  125.       {
  126.         setTimeout("window.close();", 500);
  127.       }
  128.         
  129.       return copernicAgent_OK;
  130.     }
  131.     else
  132.     {
  133.       return copernicAgent_QueryTooLong;
  134.     }
  135.   }
  136.   else
  137.   {
  138.     return copernicAgent_Failed;
  139.   }
  140. }
  141.  
  142.  
  143. // Start search
  144. function copernicAgent_StartSearch(category, query, mode) 
  145. {
  146.   if (copernicAgent)
  147.   {
  148.     var url = copernicAgent_ProtocolPrefix + escape(escape(query)) + ":" + escape(escape(category)) + ":" + mode;
  149.  
  150.     if (url.length <= 256) 
  151.     {
  152.       top.window.window.navigate(url);
  153.     
  154.       if (window.name == copernicAgent_SearchWindowName)
  155.       {
  156.         setTimeout("window.close();", 500);
  157.       }
  158.         
  159.       return copernicAgent_OK;
  160.     }
  161.     else
  162.     {
  163.       return copernicAgent_QueryTooLong;
  164.     }
  165.   }
  166.   else
  167.   {
  168.     return copernicAgent_Failed;
  169.   }    
  170. }
  171.  
  172.  
  173. // Returns true if trimmed string is empty
  174. function isStringEmpty(str) 
  175. {
  176.   if (str.length == 0)
  177.   {
  178.     return(true);
  179.   }
  180.  
  181.   var j = 0;
  182.   for (var i = 0; i < str.length; i++) 
  183.   {
  184.     if (str.charAt(i) == " ")
  185.     {
  186.       j++;
  187.     }
  188.   }
  189.   
  190.   return(j == str.length);
  191. }
  192.  
  193.  
  194. // Returns true if query contains "and" or "or"
  195. function containsBooleanOperators(str) 
  196. {
  197.   var inquote = false;
  198.   var tmp = "";
  199.  
  200.   for (var i = 0; i < str.length; i++) 
  201.   {
  202.     ch = str.charAt(i);
  203.     
  204.     if (ch == '"')
  205.     {
  206.       inquote = !inquote;
  207.       tmp = tmp + " ";
  208.     }
  209.     else if (!inquote)
  210.     {
  211.       tmp = tmp + ch;
  212.     }
  213.   }
  214.  
  215.   tmp = " " + tmp.toLowerCase() + " ";
  216.   
  217.   return((tmp.indexOf(" and ") < 0) && (tmp.indexOf(" or ") < 0));
  218. }
  219.  
  220.  
  221. // Validate query and display error dialog if any error occur
  222. function validateQuery(category, query, mode, strExactWithQuotesError, strBooleanOperatorConfirm, strEmptyCategory)
  223. {
  224.   if (isStringEmpty(category))
  225.   {
  226.     alert(strEmptyCategory);    
  227.     return copernicAgent_Failed;
  228.   }
  229.   
  230.   if ((mode == copernicAgent_ModeExactPhrase) && (query.indexOf('"') >= 0)) 
  231.   {
  232.     alert(strExactWithQuotesError);    
  233.     return copernicAgent_Failed;
  234.   }
  235.  
  236.   if ((!containsBooleanOperators(query)) && (!confirm(strBooleanOperatorConfirm)))
  237.   {
  238.     return copernicAgent_Failed;
  239.   }
  240.  
  241.   return copernicAgent_OK;
  242. }
  243.