home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 November / Chip_2003-11_cd1.bin / software / dave / dqsd.exe / defer_tools.js < prev    next >
Text File  |  2003-04-20  |  13KB  |  412 lines

  1. // Open a search window either with the default browser or with IE
  2. function openSearchWindow(url)
  3. {
  4.   openNamedSearchWindow(url, getReuseBrowserWindowMode() ? DQSD_BROWSER_WINDOW_NAME : "_blank");
  5. }
  6.  
  7. // Open a search window in an existing frame
  8. function openNamedSearchWindow(url, name)
  9. {
  10.   if (useExternalBrowser && DQSDLauncher)
  11.     DQSDLauncher.OpenDocument(url);
  12.   else if (typeof windowOpenFeatures != "undefined") {
  13.     window.open(url, name, windowOpenFeatures);
  14.   } else {
  15.     window.open(url, name);
  16.   }
  17. }
  18.  
  19. // direct shellexecute
  20. function openDocument(path)
  21. {
  22.   ensureLauncher();
  23.   if (DQSDLauncher)
  24.   {
  25.     var params = path.match(/^(("[^"]*")|\S*)\s*(.*)/);
  26.     if (params)
  27.     {
  28.       DQSDLauncher.OpenDocument(params[1], params[3]);
  29.     }
  30.     else
  31.     {
  32.       DQSDLauncher.OpenDocument(path);
  33.     }
  34.   }
  35. }
  36.  
  37. var submitcount = 0;
  38.  
  39. // Submit a form either with the default browser or with IE
  40. function submitForm(form, dontChangeTarget)
  41. {
  42.   var oldtarget;
  43.   var newtarget;
  44.  
  45.   //allow the Override
  46.   if ( !dontChangeTarget )
  47.   {
  48.     // Here's a safeguard for forgetting to put a target in the FORM
  49.     if ( (getReuseBrowserWindowMode() == 0) || !form.attributes["target"].nodeValue || (form.attributes["target"].nodeValue && form.attributes["target"].nodeValue == '') )
  50.     {
  51.       form.attributes["target"].nodeValue = '_blank';
  52.     }
  53.  
  54.     // Reuse a single window if the user wants to and the target is _blank
  55.     // Don't override targets with any other name because some searches may want
  56.     // their own window.
  57.  
  58.     oldtarget = form.attributes["target"].nodeValue;
  59.     newtarget = oldtarget;
  60.  
  61.     if ((getReuseBrowserWindowMode() > 0) && oldtarget && (oldtarget == '_blank'))
  62.     {
  63.       // 1=same window always; 2=new window for each search type
  64.       newtarget = ((getReuseBrowserWindowMode() == 1) ? DQSD_BROWSER_WINDOW_NAME : (DQSD_BROWSER_WINDOW_NAME + '_' + form.name));
  65.     }
  66.     else
  67.     {
  68.       newtarget = DQSD_BROWSER_WINDOW_NAME + "_" + submitcount;
  69.       submitcount = submitcount + 1;
  70.     }
  71.     form.attributes["target"].nodeValue = newtarget;
  72.   }
  73.  
  74.   if (useExternalBrowser && DQSDLauncher)
  75.     DQSDLauncher.SubmitForm(form);
  76.   else
  77.   {
  78.     if (newtarget && pagetemplate)
  79.     {
  80.       var w = null;
  81.       if (typeof windowOpenFeatures != "undefined") {
  82.         w = window.open(url, name, windowOpenFeatures);
  83.       } else {
  84.         w = window.open(url, name);
  85.       }
  86.       w.history.back(1);
  87.     }
  88.     if (typeof form.onsubmit == "function") {
  89.       form.fireEvent("onsubmit");
  90.     } else {
  91.       form.submit();
  92.     }
  93.   }
  94.  
  95.   if (oldtarget)
  96.     form.attributes["target"].nodeValue = oldtarget;
  97. }
  98.  
  99. // Used by the calendar and the menu (or anything that uses IE window.createPopup method), which cannot call
  100. // the stylesheet directly.
  101. function convertStylesToInline()
  102. {
  103.   var stylestring = new String();
  104.   for (var j = 0; j < document.styleSheets.length; j++)
  105.   {
  106.     var intermediateString = '';
  107.     var rules = document.styleSheets[j].rules;
  108.     var rulesLength = rules.length;
  109.     
  110.     for (var i = 0; i < rulesLength; i++)
  111.     {
  112.       intermediateString += rules[i].selectorText + ' {' + rules[i].style.cssText + '}'
  113.     }
  114.     
  115.     stylestring += intermediateString;
  116.   }
  117.   return(stylestring);
  118. }
  119.  
  120.  
  121. function protocolHandled(url)
  122. {
  123.   if (!ensureLauncher())
  124.     return true; // what else can we do except assume the protocol is handled?
  125.  
  126.   var results = url.match(/(\w+):/);
  127.   if (!results)
  128.     return false; // not a valid url
  129.  
  130.   try
  131.   {
  132.     var handler = DQSDLauncher.GetProtocolHandler(results[1]);
  133.   }
  134.   catch (e)
  135.   {
  136.     return false;
  137.   }
  138.  
  139.   return true;
  140. }
  141.  
  142. // isURL
  143. //
  144. // Takes a string as a parameter and determines if it is a http URL (using either http or https protocol)
  145. // If the string is not a URL, then it attempts to detect obvious dns names, and if so, it prepends http://
  146. // to the string.  In either of the above to cases, the final string is returned, otherwise, 'false' is
  147. // returned.
  148. function isURL(t)
  149. {
  150.   // detect strings that look like URLs or filenames
  151.   prot = new RegExp("^(http://|https://|ftp://)([\\-a-z0-9]+\\.)*[\\-a-z0-9]+" +
  152.           "|^[a-z]:($|\\\\)" +
  153.           "|^\\\\\\\\[a-z0-9]+($|\\\\($|[a-z0-9]+($|\\\\)))", "i");
  154.   if (prot.exec(t))
  155.     return t;
  156.  
  157.   // detect strings that look like DNS names
  158.   dns = new RegExp("^www\.([\\-a-z0-9]+\\.)+[\\-a-z0-9]+(/\\S*)?$" +
  159.           "|^([\\-a-z0-9]+\\.)+(com|net|org|edu|gov|mil|[a-z]{2})(/\\S*)?$", "i");
  160.   if (dns.exec(t))
  161.   {
  162.     t = "http://" + t;
  163.     return t;
  164.   }
  165.   return false;
  166. }
  167.  
  168. // Used to parse standard switches (/foo or /foo:bar).  Takes the following parameters:
  169. //    q - string from the search function
  170. //    expectedSwitches - list or array of the expected switch values
  171. //    expandSwitches - optional parameter used to determine if the switch shortcuts should be expanded (i.e. /f becomes /foo)
  172. // Function returns an object with three properties
  173. //    q - the input string with the switches removed
  174. //    switches - array of objects with these two properties:
  175. //           name:   expanded name of the matched switch (i.e. foo as in /foo:bar)
  176. //            value:  value of switch (i.e. bar as in /foo:bar)
  177. //    switch_val - associative array with the switch name as the key with the switch value as the value. (i.e. switch_val["foo"] = "bar" as in /foo:bar)
  178. //
  179. function parseArgs(q, expectedSwitches, expandSwitches)
  180. {
  181.   // In case the caller does not pass in a value
  182.   if (typeof expandSwitches == 'undefined')
  183.     expandSwitches = 1;
  184.  
  185.   // In case the caller uses a delimited (;,<space>) string
  186.   if (typeof expectedSwitches[0] == 'undefined')
  187.     expectedSwitches = expectedSwitches.split( /[,;/\s]/ );
  188.  
  189.   var switches = [];
  190.   var switch_val = [];
  191.  
  192.   // Split the query to allow for easy looping.
  193.   var args_array = q.split(' ');
  194.  
  195.   // Regular expression that defines switches
  196.   var re_switch = /\/(([-.\w]+)(?::?(\S*)))\s*/;
  197.   var re_res_args;
  198.   var re_res_switch;
  199.  
  200.   // Loop through the q array and see if any of the q's look like switches
  201.   for (var i = 0; i < args_array.length; i++)
  202.   {
  203.     // If a q looks like a switch, loop through the switch array and see if any of the switches match.
  204.     re_res_args = args_array[i].match(re_switch);
  205.     if (re_res_args)
  206.     {
  207.       for (var j = 0; j < expectedSwitches.length && !re_res_switch; j++)
  208.       {
  209.         var expect_regex = new RegExp(
  210.             '^(' + re_res_args[2].replace('.', '\\.') +
  211.              ')' + (expandSwitches ? '' : '$'), 'i');
  212.  
  213.         re_res_switch = expectedSwitches[j].match(expect_regex);
  214.  
  215.         //  If there is a match, adjust the args_array, and save the values.
  216.         if (re_res_switch)
  217.         {
  218.           switch_val[expectedSwitches[j]] = re_res_args[3];
  219.           switches.push( {name:expectedSwitches[j].toLowerCase(), value:re_res_args[3]} );
  220.           args_array.splice(i, 1);
  221.           i--;
  222.         }
  223.       }
  224.       re_res_switch = "";
  225.     }
  226.   }
  227.   q = args_array.join(' ');
  228.   return { q:q, switches:switches, switch_val:switch_val };
  229. }
  230.  
  231. /*
  232.  * parseArgsEx is similar to parseArgs except that
  233.  * it isn't split on spaces but instead the /
  234.  * This means that args.q will be the value before the
  235.  * first slash and the parameter value is everything after
  236.  * the arg name until the next slash.  Also the arg name
  237.  * and value are separated by a space not a : like parseArgs.
  238.  * This will also allow you to have spaces in the argument value
  239.  * as well.
  240.  * [Brent Beardsley - 10/31/2002]
  241.  */
  242. function parseArgsEx(q, expectedSwitches, expandSwitches)
  243. {
  244.   // In case the caller does not pass in a value
  245.   if (typeof expandSwitches == 'undefined')
  246.     expandSwitches = 1;
  247.  
  248.   // In case the caller uses a delimited (;,<space>) string
  249.   if (typeof expectedSwitches[0] == 'undefined')
  250.     expectedSwitches = expectedSwitches.split( /[,;/\s]/ );
  251.  
  252.   var switches = [];
  253.   var switch_val = [];
  254.   var args_array = q.split('/');
  255.   for (var i=0; i < args_array.length; i++)
  256.   {
  257.     var one_arg = trimWhitespace(args_array[i]);
  258.     if (one_arg == "") 
  259.     {
  260.       continue;
  261.     }
  262.     var argRegExp = /(\S+)(\s+(.*))?/g;
  263.     var reResult = argRegExp.exec(one_arg);
  264.     if (reResult == null || reResult[1] == "") 
  265.     {
  266.       continue;
  267.     }
  268.     var argName = reResult[1];
  269.     var argValue = trimWhitespace(reResult[2]);
  270.     var re_res_switch;
  271.  
  272.     for (var j = 0; j < expectedSwitches.length && !re_res_switch; j++)
  273.     {
  274.       var expect_regex = new RegExp(
  275.           '^(' + argName.replace('.', '\\.') +
  276.            ')' + (expandSwitches ? '' : '$'), 'i');
  277.  
  278.       re_res_switch = expectedSwitches[j].match(expect_regex);
  279.  
  280.       //  If there is a match, adjust the args_array, and save the values.
  281.       if (re_res_switch) 
  282.       {
  283.         argName = expectedSwitches[j];
  284.         switch_val[argName] = argValue;
  285.         switches.push( {name:argName.toLowerCase(), value:argValue} );
  286.       }
  287.     }
  288.     re_res_switch = "";
  289.   }
  290.  
  291.   if (q.match(/^\s*\//))
  292.   {
  293.     // if it starts with a slash there are no non-switch values
  294.     q = "";
  295.   } 
  296.   else 
  297.   {
  298.     // only the part before the first slash is returned in args.q
  299.     var firstSlash = q.indexOf('/');
  300.     if (firstSlash != -1) 
  301.     {
  302.       q = q.substring(0, firstSlash);
  303.     }
  304.     q = trimWhitespace(q);
  305.   }
  306.   return { q:q, switches:switches, switch_val:switch_val };
  307. }
  308.  
  309.  
  310. function trimWhitespace( s )
  311. {
  312.   return s.replace(/^\s+/, "").replace(/\s+$/, "");
  313. }
  314.  
  315.  
  316. // nullArgs (Neel Doshi - 04/09/2002)
  317. //
  318. // Many functions will open the <link> url when the query is null or the description if the query is "?".  This function automates this process.
  319. //    func - string name of the function
  320. //    q - string from the search function
  321. // The function returns false if the query was not null or "?" and true otherwise
  322. //
  323. function nullArgs(func, q)
  324. {
  325.   if (q == "")
  326.   {
  327.     if (searches[func].link)
  328.     {
  329.       openSearchWindow(searches[func].link);
  330.       return true;
  331.     }
  332.   }
  333.   if (q == "?" || q == "")
  334.   {
  335.     if (searches[func].desc)
  336.     {
  337.  
  338.       if ((typeof qsfind) == 'function')
  339.       {
  340.         // If QSFind exists use that to display the help
  341.         var qsarg = func + " /function";
  342.         qsfind(qsarg);
  343.       }
  344.       else
  345.       {
  346.         // Otherwise use an alert box
  347.         var search_desc = searches[func].desc;
  348.  
  349.         // convery <br> tags to newlines
  350.         var re = new RegExp("(<br)( )?(\/>)", "g");
  351.         search_desc = search_desc.replace(re, "\n");
  352.  
  353.         // Replace tabs and multiple spaces in the description with one space
  354.         var re = new RegExp("((\\t)|( ))+", "g");
  355.         search_desc = search_desc.replace(re, " ");
  356.  
  357.         // Replace newline space with nothing
  358.         var re = new RegExp("\n( )", "g");
  359.         search_desc = search_desc.replace(re, "");
  360.  
  361.         // Remove any more HTML tags
  362.         var re = new RegExp("<(\/)?\\w+( )?[\\w\"\=\ ]*(\/)?>", "g");
  363.         var search_desc = search_desc.replace(re, "");
  364.         alert(search_desc);
  365.       }
  366.       return true;
  367.     }
  368.   }
  369.   else
  370.     return false;
  371. }
  372.  
  373. function getReuseBrowserWindowMode()
  374. {
  375.   return reuseBrowserWindowModeOverride;
  376. }
  377.  
  378. function displayPopupMessage( msg, headHTML )
  379. {
  380.   var dqsdMessagePopup = window.createPopup();
  381.   var windowW = 350;
  382.   var dqsdMessagePopup = window.createPopup();
  383.   var dqsdMessagePopupBody = dqsdMessagePopup.document.body;
  384.   var dqsdMessageBodyCode = "<html><head>" + (typeof headHTML == 'undefined' ? '' : headHTML ) + "</head><body>";
  385.   dqsdMessageBodyCode += "<table id=qstable border=0 cellspacing=1 cellpadding=2 width=100% height=100%>";
  386.   dqsdMessageBodyCode += "<tr><td valign=top><style>" + convertStylesToInline() + "</style>";
  387.   dqsdMessageBodyCode += "<tr><td valign=top style='text-align: center' class=helpboxDescriptions>" + msg;
  388.   dqsdMessageBodyCode += "</tr></td></table></body></html>";
  389.   dqsdMessagePopupBody.innerHTML = dqsdMessageBodyCode;
  390.   dqsdMessagePopup.document.body.style.border="outset 2px";
  391.   dqsdMessagePopup.document.body.style.background='menu';
  392.   dqsdMessagePopup.document.body.style.overflowY='auto';
  393.  
  394.   // Temporatily show the popup to determine the proper final
  395.   // height for the popup.
  396.   dqsdMessagePopup.show(0, 0, windowW, 0);
  397.   var windowH = dqsdMessagePopupBody.scrollHeight + 6;
  398.   dqsdMessagePopup.hide();
  399.  
  400.   // Put a cap on the popup height
  401.   windowH = windowH > window.screen.height-100 ? window.screen.height-100 : windowH;
  402.  
  403.   dqsdMessagePopup.show((buttonalign == "left" ? 0 : document.body.clientWidth - windowW), -windowH, windowW, windowH, document.body);
  404. }
  405.  
  406. function setSearchWindowText(text, autoselect)
  407. {
  408.   document.deff.q.value = text;
  409.   if (typeof autoselect != "undefined" && autoselect == true) {
  410.     document.deff.q.createTextRange().select();
  411.   }
  412. }