/* Script Purpose:
 given a form an a set of fixed vars, submit the form

Note: If you make changes to this script within Wikidpad, you have to reload the page 
for the change to take affect.
*/

window.searchForm = function(text) {
 var thisEng="";
 text = text.toString();
 // if prefix of 1-2 char matches existing engine, just go to homepage
 if (text.match(/^([^:]{1,2})$/) && engines[RegExp.$1]) {
 location.href = engines[RegExp.$1].main;
 return false; // done.

 // no prefix, just basic search-- use default engine
 } else if (text.match(/^[^:]{3,}/)) { 
 thisEng = engines[DEFAULT_ABBREV];

 // if prefix of 1-2 char matches existing engine and following text do search
 } else if (text.match(/([^:]{1,2}):(.+)$/)) { 
 thisEng = engines[RegExp.$1]; 
 text = RegExp.$2; 
 }

 // quit if no engine was found
 if (!thisEng || !thisEng.action) return false;

 // allow init function to do more sophisticated stuff
 var r = new RegExp();
 if (thisEng.init && typeof thisEng.init=="function") {
 r = thisEng.init(text); // function should return a regex substituted into the URL
 } else {
 r = /^(.*)$/; // default-- just capture entire entered string
 }

 if (typeof r == "string") {
 return r; // no regex, just send text back to the browser (e.g., calc)
 } else { // assume r is regex
 url = text.replace(r, thisEng.action);
 }
 location.href=url;

}

DEFAULT_ABBREV = "g";
// add init: function(text) {...} to preprocess input text
// in this case, make sure regexp's are reflected in target URL
engines = {

 // google
 g: {main:"http://www.google.com",
 action:"http://www.google.com/search?q=$1"},

 // map
 m: {main:"http://maps.google.com", 
 action:"http://maps.google.com/?q=$1" },

 // google images
 i: {main:"http://images.google.com", 
 action:"http://images.google.com/images?q=$1" },

 // google i'm feeling lucky
 l: {main:"http://www.google.com",
 action:"http://www.google.com/search?q=$1&btnI=I%27m%20Feeling%20Lucky" },

 // weather forecast
 w: {main:"http://www.weather.gov",
 action:"http://www.srh.noaa.gov/zipcity.php?inputstring=$1"}
}

