/* Copyright 2006-2008  Marlin Arms Corporation.  All rights reserved. */

/* 今のところコードをきれいにする努力はほとんどしていませんので、
   ご承知の上ご参考になさってください。m(_ _)m */

/* linked to:  dictjuggler.net/dictjuggler.js
               widget/src/DictJugglerMini.js
               Mac/Linbrary/widget/DictJugglerMini/DictJugglerMini.js
*/

var GTimeUpdateInterval = 20;  /* 秒単位 */
var GTimerRunning = false;
var GTimeColor = "#333333";

var defaultEJDict = "Eijiro";
var defaultJEDict = "Yahoodicts";
var defaultJJDict = "Yahoodicts";
var defaultEEDict = "english-english";
var defaultZipMap = "Livedoor-map";
var defaultEncyclopedia = "Weblio";

var dateTimeIsBeeingDisplayed = false;
var GPreviousSearchString = "";

function dictJugglerSearch() {
  var searchWord = document.getElementById('dictjugglerString').value;
  //  var dict = document.getElementById('engine-selection').value;
  dictJugglerSearchString(searchWord);
}



function guessDict( value ) {
  return "Weblio";
  
}


function dictJugglerDoSearch(dict, value) {
  var url = helpURL;
  log("will doSearch with '" + dict + "'; word=" + value);

  if (isYahooWidget) {
    value = escape(value); // 漢字はうまくいかない
  }

  switch (dict) {

  case "URL":
    url = value;
    break;

  case "AmazonJuggler":
    url = "http://www.dictjuggler.net/juggle/amazon-juggler.php?kind=keyword&searchString=" + value + "";
    break;

  case "AmazonTitle":
    url = "http://www.dictjuggler.net/juggle/amazon-juggler.php?kind=title&searchString=" + value + "";
    break;

  case "AmazonAuthor":
    url = "http://www.dictjuggler.net/juggle/amazon-juggler.php?kind=author&searchString=" + value + "";
    break;


    /* 11/14/2008 (Fri) */
  case "transportation":
    if (value.match("^([^ ]+)[ ]+([^ ]+)$")) {

      from = RegExp.$1;
      to = RegExp.$2;

      url = "http://www.google.co.jp/maps?hl=ja&ie=UTF8&f=d&saddr="
	+ from + "&daddr=" + to + "&dirflg=r";
/*
      from = escape(RegExp.$1);
      to = escape(RegExp.$2);
      url = "http://www.ekikara.jp/cgi-bin/route.cgi?intext=" + from
	+ "&outtext=" + to;
*/
    }
    else {
      url = "http://www.google.co.jp/search?rls=ja-jp&q=乗り換え%20"
	+ value + "ie=UTF-8&oe=UTF-8";
    }
    break;
  case "Yahoodicts":
    url = "http://dic.yahoo.co.jp/bin/dsearch?p=" + value + "&stype=0&dtype=2&enc=UTF-8";
    break;
  case "Google":
  case "Google-specific":
    url = "http://www.google.co.jp/search?rls=ja-jp&q=" + value + "&ie=UTF-8&oe=UTF-8";
    //    url = "http://www.google.co.jp/custom?q=" + value + "&sa=%8C%9F%8D%F5&client=pub-0478687313408094&forid=1&channel=9804979666&ie=UTF-8&oe=UTF-8&cof=GALT%3A%23008000%3BGL%3A1%3BDIV%3A%23336699%3BVLC%3A663399%3BAH%3Acenter%3BBGC%3AFFFFFF%3BLBGC%3A336699%3BALC%3A0000FF%3BLC%3A0000FF%3BT%3A000000%3BGFNT%3A0000FF%3BGIMP%3A0000FF%3BFORID%3A1&hl=ja";
    break;
  case "Google-phrase":
    if (isYahooWidget) {
      url = 'http://www.google.com/search?hl=ja&rls=ja-jp&q=%22' + value + "%22&btnG=Google+%E6%A4%9C%E7%B4%A2&lr=";
    }
    else if (isMacOSXWidget || isWebpage) {
      url = 'http://www.google.com/search?hl=ja&rls=ja-jp&q="' + value + '"&btnG=Google+%E6%A4%9C%E7%B4%A2&lr="';
    }
    break;
  case "Google-towa":
    url = "http://www.google.com/search?rls=ja-jp&q=" + value + escape("とは") + "&ie=UTF-8&oe=UTF-8";
    break;
  case "Google-define":
    url = "http://www.google.com/search?rls=ja-jp&q=define:+" + value + "&ie=UTF-8&oe=UTF-8";
    break;
  case "Google-japanese":
    url = "http://www.google.co.jp/search?hl=ja&q=" + value + "&btnG=Google+"  + escape("検索") + "&lr=lang_ja";
    break;
  case "Google-image":
    url = 'http://images.google.com/images?q=' + value + '&hl=ja&btnG=' + escape("イメージ検索");
    break;
  case "Yahoo":
    url = "http://search.yahoo.co.jp/bin/search?p=" + value + "&fr=top&src=top&search.x=15&search.y=10";
    break;
/*
  case "Yahoogle":
    url = "http://www.yahoogle.jp/yahoogle-1-" + value + ".html";
    break;
*/
  case "yamaoka":
//    url = "http://studio1.idd.tamabi.ac.jp/corpus/script/check.cgi?word=" + value + "";

    if (isYahooWidget || isMacOSXWidget) {
      var escapedWord = yakugoEscape_utf8(value);
      escapedWord = escapedWord.toLowerCase();


      while (escapedWord.lastIndexOf("%") >=0) {
	escapedWord = escapedWord.replace("%", "");
      }
      var filename = yakugoSplitFilenameWithSlash(escapedWord);
      url = "http://www.dictjuggler.net/yakugo/" + filename;    
    }
    else if (isWebpage) {
      var escapedWord = yakugoEscape_utf8(value);
      escapedWord = escapedWord.toLowerCase();
      while (escapedWord.lastIndexOf("%") >=0) {
	escapedWord = escapedWord.replace("%", "");
      }
      var filename = yakugoSplitFilenameWithSlash(escapedWord);
      showURL(filename);
      return;
    }

    break;
  case "kanji":
    url = "http://dic.yahoo.co.jp/dsearch?enc=UTF-8&p="
      + value + "&stype=1&dtype=0";

// インタフェースが変わってしまったので、Yahooに変更 11/5/2008

//    url = "http://jiten.www.infoseek.co.jp/Kanji?qt=" + value + "&qtb=&qty=&qtk=0&pg=result_kj.html&col=KJ&sv=DC";
    break;

  case "ruigo":
    //url = "http://thesaurus.weblio.jp/content/" + value;

    if (isYahooWidget || isMacOSXWidget) {
      var escapedWord = escape_utf8(value);
      escapedWord = escapedWord.toLowerCase();
      while (escapedWord.lastIndexOf("%") >=0) {
	escapedWord = escapedWord.replace("%", "");
      }
      var filename = splitFilenameWithSlash(escapedWord);
      url = "http://www.dictjuggler.net/tamatebako/" + filename;
    }
    else if (isWebpage) {
      var escapedWord = escape_utf8(value);
      escapedWord = escapedWord.toLowerCase();
      while (escapedWord.lastIndexOf("%") >=0) {
	escapedWord = escapedWord.replace("%", "");
      }
      var filename = splitFilenameWithSlash(escapedWord);
      tamatebakoShowURL(filename);
      return;
    }

    break;

  case "Weblio":
    url = "http://www.weblio.jp/content/" + value;
    break;

  case "DictJuggler":
    url = "http://www.google.co.jp/search?num=100&hl=ja&client=safari&rls=ja-jp&as_qdr=all&q=" + value + "+site:www.dictjuggler.net&btnG=検索&lr="
    break;

  case "Wikipedia-j":
    url = "http://ja.wikipedia.org/wiki/" + value;
    break;
  case "Wikipedia-e":
    url = "http://en.wikipedia.org/wiki/Special:Search?search=" + value + "&go=Go";
    break;
  case "Answer-com":
    url = "http://www.answers.com/" + value + "";
    log("answer.com  URL=" + url);
    break;
  case "Livedoor-map":
    url = "http://map.livedoor.com/search/?type=&word=" + value + "";
    break;
  case "Google-maps":
    url = "http://maps.google.co.jp/maps?f=q&hl=ja&q=" + value + "";
    break;
  case "bidders":
    url = "http://www.bidders.co.jp/dap/sv/list1?categ_id=&keyword=" + value + "&Submit=%8C%9F%8D%F5&at=NO,PA,FL&at=FP&at=GB";
    break;
//  case "yahoo-auction":
//    case_utf8はだめ
//    url = "http://search.auctions.yahoo.co.jp/search/auc?p=" + escape_utf8 (value);
// %B9%FC%C6%A1
// http://search.auctions.yahoo.co.jp/jp/search/auc?p=" + value + "&auccat=0&alocale=0jp&acc=jp";
    break;
  case "aucfan":
    url = "http://aucfan.com/search2?q=" + value;
    break;

  case "cookpad":
    url = "http://cookpad.com/レシピ/" + value;
    break;

  case "rakuten-auction":
    url = "http://esearch.rakuten.co.jp/rms/sd/esearch/vc?sv=13&sitem=" + value + "&oid=000";
    break;
			
  case "japanese-japanese":
    url = "http://www.excite.co.jp/dictionary/japanese/?search=" + value + "&match=beginswith";
    break;
						
  case "english-japanese":
    url = "http://www.excite.co.jp/dictionary/english_japanese/?search=" + value + "&match=beginswith";
    break;
			
  case "japanese-english":
    url = "http://dic.yahoo.co.jp/dsearch?enc=UTF-8&p=" + value + "&stype=0&dtype=3";

    // "http://www.excite.co.jp/dictionary/japanese_english/?search=" + value + "&match=beginswith";
    break;

    //  case "japanese-english":
    //    url = "http://www.excite.co.jp/dictionary/japanese_english/?search=" + value + "&match=beginswith";
    //    break;
			
  case "japanese-chinese":
    url = "http://www.excite.co.jp/dictionary/japanese_chinese/?search=" + value + "&match=beginswith";
    break;
  case "chinese-japanese":
    url = "http://www.excite.co.jp/dictionary/chinese_japanese/?search=" + value + "&submit=+" + escape("検") + "+" + escape("索") + "&match=beginswith";
    break;
			
  case "english-english":
    url = "http://education.yahoo.com/reference/dictionary/search?p=" + value + "";
    break;
  case "english-thesaurus":
    url = "http://encarta.msn.com/thesaurus_/" + value + ".html";
    break;
  case "english-korean":
    url = "http://kr.dic.yahoo.com/search/eng/search.html?prop=eng&p=" + value + "&x=0&y=0&a=c";
    break;
  case "english-german":
    url = "http://www.dict.cc/?s=" + value + "";
    break;
  case "japanese-korean":
    url = "http://jpdic.paran.com/search.php?l=jp&s=0&t=%2Fparan_dic%2Fdic_simple_jp_all.php&q=" + value + "&m=a";

    //    url = "http://jpdic.empas.com/dicsearch/show.html/?z=F&q=" + value + "&x=25&y=13";
    break;
  case "Eijiro":
    url = "http://eow.alc.co.jp/" + value;

    //    url = "http://www2.alc.co.jp/ejr/index.php?word_in=" + value + "&word_in2=%82%A0%82%A2%82%A4%82%A6%82%A8&word_in3=PVawEWi72JXCKoa0Je";

    break;

  case "english-chinese":
    url = "http://www.mandarintools.com/cgi-bin/wordlook.pl?word=" + value + "&searchtype=chinese&where=whole";
    break;

  case "spanish-english":
    url = "http://www.wordreference.com/es/en/translation.asp?spen=" + value + "&v=b";
    break;

  case "english-spanish":
    url = "http://www.wordreference.com/es/translation.asp?tranword=" + value + "&v=b";
    break;
						
  case "italian-english":
    url = "http://www.wordreference.com/iten/" + value + "";
    break;

  case "english-italian":
    url = "http://www.wordreference.com/enit/" + value + "";
    break;
						
  case "french-english":
    url = "http://www.wordreference.com/fren/" + value + "";
    break;

  case "english-french":
    url = "http://www.wordreference.com/enfr/" + value + "";
    break;


  case "Amazon-Japan":
    url = "http://www.amazon.co.jp/gp/search?ie=UTF8&keywords=" + value + "&index=blended&linkCode=ur2";

//    url = "http://www.amazon.co.jp/gp/search?ie=UTF8&keywords=" + value + "&index=blended&linkCode=ur2&camp=247&creative=1211";


//    url = "http://www.amazon.co.jp/gp/search?ie=UTF8&keywords=" + value + "&tag=marlinarmscor-22&index=blended&linkCode=ur2&camp=247&creative=1211";

    //    log(url);

    break;
  case "Amazon-com":
    value = value.replace(/ /g, "%20");
    url = "http://www.amazon.com/gp/search?ie=UTF8&keywords=" + value +
      "&tag=marlinarmscor-20&index=blended&linkCode=ur2&camp=1789&creative=9325";

      // http://www.amazon.com/s/ref=br_ss_hs/002-0997786-9032825?platform=gurupa&url=index%3Dblended&keywords=" + value + "&Go.x=0&Go.y=0&Go=Go";

    //        log(url);
    break;

  case "e-words":
    if (isMacOSXWidget || isWebpage) {
      // log("will call escape_utf8");
      value = escape_utf8(value);
    }
    value = value.replace(/%/g, "");
    url = "http://e-words.jp/w/" + value + ".html";
    log(url);
    break;

  case "searchmash":
    url = "http://www.searchmash.com/search/" + value;
    log("searchmash:" + url);
    break;

  case "Google-code":
    url = "http://www.google.com/codesearch?hl=en&lr=&q=" + value + "&btnG=Search";
    break;

  case "stl":
    url = "http://www.google.co.jp/search?as_q=" + value + "&hl=ja&num=50&btnG=Google+%E6%A4%9C%E7%B4%A2&as_epq=&as_oq=&as_eq=&lr=&cr=&as_ft=i&as_filetype=&as_qdr=all&as_occt=any&as_dt=i&as_sitesearch=http%3A%2F%2Fwww.wakhok.ac.jp%2F%7Esumi%2Fstl%2F&as_rights=&btnI=I%27m+Feeling+Lucky&lr=";
    break;

  case "weather":
    if (isMacOSXWidget || isWebpage) {
      url = "http://search.weather.yahoo.co.jp/bin/search?pref=all&p=" +
	escape_utf8(value) + "&s=";
    }
    else {
      url = "http://search.weather.yahoo.co.jp/bin/search?pref=all&p=" +value + "&s=";
    }
    break;

  case "flickr":
    url = "http://www.flickr.com/search/?q=" + value;
    log("flickr:" + url);
    break;

  case "YouTube":
    url = "http://www.youtube.com/results?search_query=" + value + "&search=Search";
    log("YouTube:" + url);
    break;

  case "zip-japan":
    if (value.match("^([0-9][0-9][0-9][0-9][0-9])") ) {
      url = "http://www.post.japanpost.jp/cgi-zip/zipcode.php?zip=" + value + "&x=0&y=0";
    }
    else if (value.match("^([0-9][0-9]-[0-9][0-9][0-9])") ) {
      url = "http://www.post.japanpost.jp/cgi-zip/zipcode.php?zip=" + value + "&x=0&y=0";
    }
    else if (value.match("^([0-9][0-9][0-9])") ) {
      url = "http://www.post.japanpost.jp/cgi-zip/zipcode.php?zip=" + value + "&x=0&y=0";
    }
    else {
      url = "http://www.post.japanpost.jp/cgi-zip/zipcode.php?pref=&addr=" + value + "&x=0&y=0";
    }
    break;

  case "tohoho-html":
    url = "http://www.tohoho-web.com/html/" + value + ".htm";
    break;

  case "tohoho-css":
    url = "http://www.tohoho-web.com/css/reference.htm#" + value;
    break;

  case "tohoho-javascript":
    url = "http://www.google.co.jp/search?hl=ja&as_qdr=all&q=" + value + "++site%3Ahttp%3A%2F%2Fwww.tohoho-web.com%2Fjs&btnG=Google+%E6%A4%9C%E7%B4%A2&lr=&btnI=I%27m+Feeling+Lucky&lr=";
    //    url = "http://www.google.co.jp/search?as_q=" + value + "&hl=ja&btnG=Google+検索&as_epq=&as_oq=&as_eq=&lr=&as_ft=i&as_filetype=&as_qdr=all&as_occt=any&as_dt=i&as_sitesearch=http://www.tohoho-web.com/js&as_rights=&btnI=I%27m+Feeling+Lucky&lr=";
    // url = "http://www.google.co.jp/search?as_q=" + value + "&hl=ja&btnG=Google+検索&as_epq=&as_oq=&as_eq=&lr=&as_ft=i&as_filetype=&as_qdr=all&as_occt=any&as_dt=i&as_sitesearch=http%3A%2F%2Fwww.tohoho-web.com%2Fjs&as_rights=";
    break;

  case "php":
    url = "http://www.google.co.jp/search?hl=ja&as_qdr=all&q=" + value + "++site%3Ahttp%3A%2F%2Fwww.php.net%2Fmanual&btnG=Google+%E6%A4%9C%E7%B4%A2&lr=&btnI=I%27m+Feeling+Lucky&lr=";
    break;

  case "clusty":
    url = "http://clusty.com/search?input-form=clusty-simple&v%3Asources=webplus&query=" + value;
    break;


  case "rakuten":
    if (isMacOSXWidget || isWebpage) {
       log("will call escapeEUCJP");
      value = EscapeJIS7(value);
      //      value = EscapeEUCJP(value);
    }

    url = "http://pt.afl.rakuten.co.jp/c/03a344b2.7fb86978/?sv=2&p=0&sitem=" + value +
      "&submit=%BE%A6%C9%CA%B8%A1%BA%F7;"
    break;

  case "yahoo-shopping":
    url = "http://shopping.yahoo.co.jp/search?p=" + value + "&cspid=&first=1";
    break;
  case "open":
    url = value;
    break;
  case "help":
  default:
    url = "http://www.google.co.jp/search?rls=ja-jp&q=" + value + "&ie=UTF-8&oe=UTF-8";
    //   url = helpURL;
    break;
  }

  if (isMacOSXWidget) {
    document.getElementById('searchString').focus(true);

    document.getElementById('searchString').select();
  }

  if (dict=="open") {
    if (isMacOSXWidget  || isYahooWidgetForMac) {
      // log("the URL is: " + url);

      var option = " -a ";
      if (url.match("^([Ee][Xx][Cc][Ee][Ll]|[Mm][Ee])$") ) {
	url = "Microsoft Excel";
      }
      else if (url.match("^([Ww][Oo][Rr][Dd]|[Mm][Ww])$")) {
	url = "Microsoft Word";
      }
      else if (url.match("^([Pp][Oo][Ww][Ee][Rr][Pp][Oo][Ii][Nn][Tt]|[Mm][Pp])$")) {
	url = "Microsoft PowerPoint";
      }
      else if (url.match("^([Ii]llustrator|[Aa][Ii][Ll]?)$")) {
	option = " -b ";
	url = "com.adobe.illustrator";
      }
      else if (url.match("^([Pp]hotoshop|[Aa][Pp])$")) {
	option = " -b ";
	url = "com.adobe.Photoshop";
      }
      else if (url.match("^([Ii]n[Dd]esign|[Aa][Ii][Dd])$")) {
	option = " -b ";
	url = "com.adobe.Indesign";
      }
      else if (url.match("^([Aa]crobat *[Pp]ro|[Aa][Aa][Pp])$")) {
	option = " -b ";
	url = "com.adobe.Acrobat.Pro";
      }
      else if (url.match("^([Aa]crobat|[Aa]crobat *[R]eader|[Aa][Rr])$")) {
	option = " -b ";
	url = "com.adobe.Reader";
      }
      else if (url.match("^([Vv][Nn][Cc])$")) {
	url = "Chicken of the VNC";
      }
      else if (url.match("^([Qq][Tt])$")) {
	url = "QuickTime Player";
      }
      else if (url.match("^([Oo][Gg][Pp])$")) {
	url = "OmniGraffle Professional";
      }
      else if (url.match("^([Oo][Gg])$")) {
	url = "OmniGraffle";
      }
      else if (url.match("^([Oo][Oo])$")) {
	option = " -b ";
	url = "com.omnigroup.OmniOutliner";
      }
      else if (url.match("^([Jj][Ee][Dd][Ii][Tt][Xx])|([Jj][Xx])$")) {
	option = " -b ";
	url = "jp.co.artman21.JeditX";
      }
      else if (url.match("^([Jj][Ee][Dd][Ii][Tt])|([Jj]4)$")) {
	option = " -b ";
	url = "jp.co.matsumoto.Jedit4";
      }
      else if (url.match("^([Tt][Vv])$")) {
	url = "PixeStationTV(G4)";
      }
      else if (url.match("^([Pp][Aa][Rr][Aa])$")) {
	url = "Parallels\ Desktop";
      }
      else if (url.match("^([Dd][Jj])$")) {
	url = "DictJuggler";
      }
      else if (url.match("^([Dd][Uu])$")) {
      	url = "Disk\ Utility";
      }
      //      else if (url.match("[Fm][Mm]")) {
      //	option = " -b ";
      //url = "com.omnigroup.OmniOutliner";
      // }
      else if (url.match("^[Ff][Ff]$")) {
	url = "Firefox";
      }


      url = "\'" + url + "\'";
      //      document.getElementById('searchString').value = url;

      var status = run(option + " "  + url);

      if (status != undefined) {
	if (isMacOSXWidget) {
	  say("Ooops!");
	}
	else if (isYahooWidgetForMac) {
	  beep(); sleep(300); beep();
	}
      }
      else {
	// say(" OK");
	// widget.system("/usr/bin/say " + url + " wo kid oh she mash ee ta", null );
      }

      /*	
      if (isMacOSXWidget) {
	error = widget.system("/usr/bin/open " + option + " "  + url, null).errorString;
	// document.getElementById('searchString').value = error;
	if (error != undefined) {
	  say("Ooops!");
	}
	else {
	  widget.system("/usr/bin/say " + " OK", null );
	  // widget.system("/usr/bin/say " + url + " wo kid oh she mash ee ta", null );
	}
      }
      else { // Yahoo Widget for Mac
	status = runCommand("/usr/bin/open " + option + " "  + url);
	beep();	sleep(200); beep();
	say(url + " wo kid oh she mash ee ta" );
	// log(status);
      }
      */

      /*
      if (status == 0) {
	document.getElementById('searchString').value = "起動しました";
	var id = setTimeout(displayOriginalMessage(originalMessage), 500);
      }
      var originalMessage = document.getElementById('searchString').value;
      var param = "/bin/ls /Applications/" + url + '.app "  /Applications/'
		      + '*' + '/' + url + '.app"'
      var existsApplicationP =
	widget.system("/bin/ls /Applications/" + url + '.app "/Applications/'
		      + '*' + '/' + url + '.app"', null).status;
	//var appliPath = widget.system("/usr/bin/find /Applications/ -type d -name " + url + ".app", null).status;
      document.getElementById('searchString').value = "|" + existsApplicationP + "|";

      if (existsApplicationP != 0) {
	document.getElementById('searchString').value = "見つかりません";
	for (var i=0; i<100000; i++) {
	  ;
	}
	document.getElementById('searchString').value = originalMessage;
	document.getElementById('searchString').focus(true);
	document.getElementById('searchString').select();
	// var id = setTimeout(displayOriginalMessage(originalMessage), 2000);
      }
      else {
	document.getElementById('searchString').focus(true);
	document.getElementById('searchString').select();
	var status = widget.system("/usr/bin/open -a " + url, null);

	//	if (status == 0) {
	// document.getElementById('searchString').value = "起動しました";
	// var id = setTimeout(displayOriginalMessage(originalMessage), 500);
	}
      }
      */
  
    }
    else {
      log("will run command: " + url);
      runCommand("/usr/bin/open -a " + url);
    }
  }
  else {
    log("final URL=" + url);
    openURL(url);
  }

}

function openHandler() {
  ;
}

function displayOriginalMessage( originalMessage ) {
  document.getElementById('searchString').value = originalMessage;
  document.getElementById('searchString').focus(true);
  document.getElementById('searchString').select();
}

function dictJugglerSearchString (inputString) {

  // 時刻表示のため ここでやるのはちょっと汚いが
  // engineSelection のonDragDropでもやる必要があるので、そこにも同じことを書いてある
  GDidSomethingAfterOnTimer = true;


  var value = inputString;


  value = normalize(value);
  if (isMacOSXWidget || isWebpage) {
    var dict = document.getElementById('engineSelection').value;

    if (window.widget) {
      widget.setPreferenceForKey(dict, "engineSelection"); // trying
      widget.setPreferenceForKey(value, "searchWord");
    }

  }
  else { // Yahoo! Widget
    var dict = convertDictStringToDictIDString(preferences.searchEngine.value);
  }

  log("in dictJugglerSearchString, inputString=" + value + "; engine=" + dict);

  if (value.length <= 0) {
    if (isMacOSXWidget || isWebpage) {
      value = document.getElementById('searchString').value;
    }
    else {
      value = preferences.searchStringValue.value;
    }

    if (value.length <= 0) {
      return;
    }
    else {
      ;
      //log("value = " + value);
    }
  }
  else {
    if (isYahooWidget) {
      preferences.searchStringValue.value = value;
    }
  }



  log("searching string=" + value);

  var url;
  var httpRegexp1 = new RegExp("^ *http.*//");
  var httpRegexp2 = new RegExp("^ *www\.");
  //var calcRegexp = new RegExp("^[0-9]*$");
  var calcRegexp = new RegExp("^[- 　0-9.,，０-９*/+xXｘＸ^＊＋−×÷＾ー()（）]*$");

  if (false) {
    ;
  }

  /*  version 1.3から止める。辞書指定を優先
  else if (value.match("^[ 　	]*〒?([0-9０-９]{3}-[0-9０-９]{4})[ 　	]*$") &&
     !  preferences.searchEngine.value.match("天気") // う〜〜、汚い
      )
      {
    // log("郵便番号");
    dict = defaultZipMap;
    value = RegExp.$1;
    //郵便番号
   ; // 下の方でマッチするが、計算と思われないようにここで一旦マッチ。
  }
  */

  else if (value.match("^[　 ]*[?？][　 ]*$")) {
    openURL(helpURL);
    return;
  }
  else if (value.match(httpRegexp1)) {
    // log("matched: " + value);
    var strlen = value.length;
    while (strlen > 0 &&
	   (value.substr(strlen-1,1) == " " || value.substr(strlen-1,1) == " ")) {
      value = value.substr(0, strlen-1);
      strlen = value.length;
      // log(value);
    }
    openURL(value);
    return;
  }
  else if (value.match(httpRegexp2)) {
    value = "http://" + value;
    // log("matched: " + value);
    openURL(value);
    return;
  }

  /*  version 1.3から止めようと思ったが、便利なのでそのまま */
    else if (value.match(calcRegexp) &&
	     ! value.match("^[ 　]*[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][ 　]*$") &&
	     ! value.match("^[ 　]*[0-9]+[ 　]*$")
	     ) {
    dict = "Google";
    // log(dict + ": " + value);
    value = value.replace(/ー/g, "-");
    value = value.replace(/\+/g, "%2B");
      // log("value=" + value);
    url = "http://www.google.co.jp/search?hl=ja&q=" + value + "";
    //    url = "http://www.google.co.jp/search?hl=ja&q=" + value + "&btnG=Google+%E6%A4%9C%E7%B4%A2&lr=";
    openURL(url);
    return;
  }
  
  log("URL not matched; value=" + value);



  // <a nane="shortcutmatching"> </a> ********
  if (false) {
    ;
  }
  else if (value.match("^[Tt][Rr] +(.*)$") || value.match("^(.*) +[Tt][Rr]$") ) {
    value = RegExp.$1;
    dict = "transportation";
  }
  else if (value.match("^[Ii][Tt] (.*)$") || value.match("^(.*) [Ii][Tt]$") ) {
    value = RegExp.$1;
    dict = "e-words";
  }
  else if (value.match("^([Eee][Jj]|英日|英) (.*)$") ||
	   value.match("^()(.*) ([Ee][Jj]|英日|英)$") ) {
    value = RegExp.$2
    dict = defaultEJDict;
  }
  else if (value.match("^([Jj][Ee]|日英|日) (.*)$") ||
	   value.match("^()(.*) ([Jj][Ee]|日英|日)$") 	   
	   ) {
    value = RegExp.$2;
    dict = defaultJEDict;
  }
  else if (value.match("^([Jj]|[Jj][Jj]|国語|国) (.*)$") ||
	   value.match("^()(.*) ([Jj]|[Jj][Jj]|国語|国)$")
	   ) {
    value = RegExp.$2;
    // log(value);
    dict = defaultJJDict;
  }
  else if (value.match("^([Ee][Nn][Cc][Yy]?|百科|百) (.*)$") ||
	   value.match("^()(.*) ([Ee][Nn][Cc][Yy]?|百科|百)$")
	   ) {
    value = RegExp.$2;
    dict = defaultEncyclopedia;
  }
  else if (value.match("^([Ee]|[Ee][Ee]|英英) (.*)$") ||
	   value.match("^()(.*) ([Ee]|[Ee][Ee]|英英)$") 
	   ) {
    value = RegExp.$2;
    dict = defaultEEDict;
  }
  else if (value.match("^([Dd][Jj]) (.*)$") ||
	   value.match("^()(.*) ([Dd][Jj])$")
	   ) {
    value = RegExp.$2;
    dict = "DictJuggler";
  }
  else if (value.match("^([Ww]) (.*)$") ||
	   value.match("^()(.*) ([Ww])$")
	   ) {
    value = RegExp.$2;
    dict = "Weblio";
  }
  else if (value.match("^([Gg]) (.*)$") ||
	   value.match("^()(.*) ([Gg])$")
	   ) {
    value = RegExp.$2;
    dict = "Google";
  }
  else if (value.match("^([Y|y])  *(.*)$") ||
	   value.match("^()(.*)  *([Y|y])$") 
	   ) {
    value = RegExp.$2;
    dict = "Yahoo";
  }
  else if (value.match("^([T|t]) ([a-zA-Z ]*[-a-zA-Z])$") ||
	   value.match("^()([a-zA-Z ]*[-a-zA-Z]) ([T|t])$")
	   ) {
    value = RegExp.$2;
    dict = "english-thesaurus";
  }
  else if (value.match("^([Tt][Jj]{0,1}) (.*)$") ||
	   value.match("^()(.*) ([Tt][Jj]{0,1})$")
	   ) {
    value = RegExp.$2;
    dict = "ruigo";
  }
  else if (value.match("^([Aa][Jj]) (.*)$") || value.match("^()(.*) ([Aa][Jj])$")
	  ) {
    value = RegExp.$2;
    dict = "Amazon-Japan";
  }
  else if (value.match("^(AA) (.+)$") || value.match("^()(.+) (AA)$")
	  ) {
    value = RegExp.$2;
    dict = "AmazonAuthor";
  }
  else if (value.match("^(AT) (.+)$") || value.match("^()(.+) (AT)$")
	  ) {
    value = RegExp.$2;
    dict = "AmazonTitle";
  }
  else if (value.match("^(A) (.+)$") || value.match("^()(.+) (A)$")
	  ) {
    value = RegExp.$2;
    dict = "AmazonJuggler";
//    log(value);
  }
  else if (value.match("^([Aa][Uu]) (.*)$") ||  value.match("^()(.*) ([Aa][Uu])$") ||
	   value.match("^([Aa][Cc]) (.*)$") ||  value.match("^()(.*) ([Aa][Cc])$")
	   ) {
    value = RegExp.$2;
    dict = "Amazon-com";
  }
  else if (value.match("^(漢|漢字|KA|ka|Ka|kA) (.*)$") ||
	   value.match("^()(.*) (漢|漢字|KA|ka|Ka|kA)$")
	   ) {
    value = RegExp.$2;
    dict = "kanji";
  }
  else if (value.match("^(画|画像|[Pp]|GI|gi|Gi|gI) (.*)$") ||
	   value.match("^()(.*) (画|画像|[Pp]|GI|gi|Gi|gI)$")
	   ) {
    value = RegExp.$2;
    dict = "Google-image";
  }
  else if (value.match("^(地|地図|[Mm]|[Gg][Mm]|[Mm][Aa][Pp]) (.*)$") ||
	   value.match("^()(.*) (地|地図|[Mm]|[Gg][Mm]|[Mm][Aa][Pp])$")
	   ) {
    value = RegExp.$2;
    dict = "Google-maps";
  }

  else if (value.match("^([Yy][Dd]) (.*)$") ||
	   value.match("^()(.*) ([Yy][Dd])$")
	   ) {
    value = RegExp.$2;
    dict = "Yahoodicts";
  }

  else if (value.match("^([Yy][Aa]) +([^ ]+)$") ||
	   value.match("^()([^ ]+) +([Yy][Aa])$")
	   ) {
    value = RegExp.$2;
    dict = "yahoo-auction";
  }

  else if (value.match("^([Aa][Ff]) +(.*)$") ||
	   value.match("^()(.*) +([Aa][Ff])$")
	   ) {
    value = RegExp.$2;
    dict = "aucfan";
  }


  else if (value.match("^([Cc][Oo])  *(.*)$") ||
	   value.match("^()(.*)  *([Cc][Oo])$")
	   ) {
    value = RegExp.$2;
    dict = "cookpad";
  }


  else if (value.match("^([Gg][Ph]) +(.*)$") ||
	   value.match("^()(.*) +([Gg][Ph])$")
	   ) {
    value = RegExp.$2;
    dict = "Google-phrase";
  }

  else if (value.match("^([Gg][Tt]) (.*)$") ) {
    value = RegExp.$2;
    dict = "Google-towa";
  }
  else if (value.match("^([Gg][Dd]) (.*)$") ||
	   value.match("^()(.*) ([Gg][Dd])$")
	   ) {
    value = RegExp.$2;
    dict = "Google-define";
  }
  else if (value.match("^([Gg][Jj]) (.*)$") ||
	   value.match("^()(.*) ([Gg][Jj])$")
	   ) {
    value = RegExp.$2;
    dict = "Google-japanese";
  }
  else if (value.match("^([Ww][Ee]) (.*)$") ||
	   value.match("^()(.*) ([Ww][Ee])$")
	   ) {
    value = RegExp.$2;
    dict = "weather";
  }
  else if (value.match("^([Ii][Tt]) (.*)$") ||
	   value.match("^()(.*) ([Ii][Tt])$")
	   ) {
    value = RegExp.$2;
    dict = "e-words";
  }
  else if (value.match("^([Ff][Ll]) (.*)$") ||
	   value.match("^()(.*) ([Ff][Ll])$")
	   ) {
    value = RegExp.$2;
    dict = "flickr";
  }
  else if (value.match("^([Yy][Tt]) (.*)$") ||
	   value.match("^()(.*) ([Yy][Tt])$")
	   ) {
    value = RegExp.$2;
    dict = "YouTube";
  }
  else if (value.match("^([Yy][Mm]) (.*)$") ||
	   value.match("^()(.*) ([Yy][Mm])$") ||
	   value.match("^([Hh][Yy]) (.*)$") ||
	   value.match("^()(.*) ([Hh][Yy])$")
	   ) {
    value = RegExp.$2;
    dict = "yamaoka";
  }
  else if (value.match("^([Ww][Jj]) (.*)$") ||
	   value.match("^()(.*) ([Ww][Jj])$")
	   ) {
    value = RegExp.$2;
    dict = "Wikipedia-j";
  }
  else if (value.match("^([Ww][Ee]) (.*)$") ||
	   value.match("^()(.*) ([Ww][Ee])$")
	   ) {
    value = RegExp.$2;
    dict = "Wikipedia-e";
  }
  else if (value.match("^([Hh][Tt]|[Hh][Tt][Mm][Ll]) (.*)$") ||
	   value.match("^()(.*) ([Hh][Tt]|[Hh][Tt][Mm][Ll])$")
	   ) {
    value = RegExp.$2;
    dict = "tohoho-html";
  }
  else if (value.match("^([Cc][Ss][Ss]?) (.*)$") ||
	   value.match("^()(.*) ([Cc][Ss][Ss]?)$")
	   ) {
    value = RegExp.$2;
    dict = "tohoho-css";
  }

  else if (value.match("^([Jj][Ss]) (.*)$") ||
	   value.match("^()(.*) ([Jj][Ss])$")
	   ) {
    value = RegExp.$2;
    dict = "tohoho-javascript";
  }

  else if (value.match("^([Pp][Hh][Pp]) (.*)$") ||
	   value.match("^()(.*) ([Pp][Hh][Pp])$")
	   ) {
    value = RegExp.$2;
    dict = "php";
  }

  else if (value.match("^([Jj][Zzp]|[Jj][Pp]|〒|郵便) (.*)$") ||
	   value.match("^()(.*) ([Jj][Zzp]|[Jj][Pp]|〒|郵便)$")
	   ) {
    value = RegExp.$2;
    dict = "zip-japan";
  }


  else if (value.match("^([Ss][Tt][Ll]) (.*)$") ) {
    value = RegExp.$2;
    dict = "stl";
  }

  else if (value.match("^([Gg][Cc]) (.*)$") ) {
    value = RegExp.$2;
    dict = "Google-code";
  }

  else if (value.match("^([Aa][Nn]) (.*)$") ) {
    value = RegExp.$2;
    dict = "Answer-com";
  }

  else if (value.match("^([Ss][Mm]) (.*)$") ||
	   value.match("^()(.*) ([Ss][Mm])$")
	   ) {
    value = RegExp.$2;
    dict = "searchmash";
  }

  else if (value.match("^([Jj][Cc]) (.*)$") ||
	   value.match("^()(.*) ([Jj][Cc])$")
	   ) {
    value = RegExp.$2;
    dict = "japanese-chinese";
  }

  else if (value.match("^([Cc][Jj]) (.*)$") ||
	   value.match("^()(.*) ([Cc][Jj])$")
	   ) {
    value = RegExp.$2;
    dict = "chinese-japanese";
  }

  else if (value.match("^([Ee][Kk]) (.*)$") ||
	   value.match("^()(.*) ([Ee][Kk])$")
	   ) {
    value = RegExp.$2;
    dict = "english-korean";
  }

  else if (value.match("^([Ee][Cc]) (.*)$") ||
	   value.match("^()(.*) ([Ee][Cc])$")
	   ) {
    value = RegExp.$2;
    dict = "english-chinese";
  }

  else if (value.match("^([Ee][Gg]) (.*)$") ||
	   value.match("^()(.*) ([Ee][Gg])$")
	   ) {
    value = RegExp.$2;
    dict = "english-german";
  }

  else if (value.match("^([Ee][Ff]) (.*)$") ||
	   value.match("^()(.*) ([Ee][Ff])$")
	   ) {
    value = RegExp.$2;
    dict = "english-french";
  }

  else if (value.match("^([Ff][Ee]) (.*)$") ||
	   value.match("^()(.*) ([Ff][Ee])$")
	   ) {
    value = RegExp.$2;
    dict = "french-english";
  }

  else if (value.match("^([Ee][Ii]) (.*)$") ||
	   value.match("^()(.*) ([Ee][Ii])$")
	   ) {
    value = RegExp.$2;
    dict = "english-italian";
  }

  else if (value.match("^([Ii][Ee]) (.*)$") ||
	   value.match("^()(.*) ([Ii][Ee])$")
	   ) {
    value = RegExp.$2;
    dict = "italian-english";
  }

  else if (value.match("^([Ee][Ss]) (.*)$") ||
	   value.match("^()(.*) ([Ee][Ss])$")
	   ) {
    value = RegExp.$2;
    dict = "english-spanish";
  }

  else if (value.match("^([Ss][Ee]) (.*)$") ||
	   value.match("^()(.*) ([Ss][Ee])$")
	   ) {
    value = RegExp.$2;
    dict = "spanish-english";
  }

  else if (value.match("^([Xx][Ee][Jj]) (.*)$") ||
	   value.match("^()(.*) ([Xx][Ee][Jj])$")
	   ) {
    value = RegExp.$2;
    dict = "english-japanese";
  }

  else if (value.match("^([Xx][Jj][Ee]) (.*)$") ||
	   value.match("^()(.*) ([Xx][Jj][Ee])$")
	   ) {
    value = RegExp.$2;
    dict = "japanese-english";
  }

  else if (value.match("^([Cc][Ll]) (.*)$") ||
	   value.match("^()(.*) ([Cc][Ll])$")
	   ) {
    value = RegExp.$2;
    dict = "clusty";
  }

  else if (value.match("^([Oo][Pp]|[Oo][Pp][Ee][Nn]|[Rr][Uu][Nn]) (.*)$") ||
	   value.match("^()(.*) ([Oo][Pp]|[Oo][Pp][Ee][Nn]|[Rr][Uu][Nn])$")
	   ) {
    value = RegExp.$2;
    dict = "open";
  }

  /* for copying
  else if (value.match("^([][]) (.*)$") ) {
    value = RegExp.$2;
    dict = "";
  }
  */


  /*
  else if (value.match("^〒?([0-9０-９]{3}-[0-9０-９]{4})$")) {
    dict = defaultZipMap;
    value = RegExp.$1;
  }
  */

//*** debug1
 // log("dict=", dict);
  
//  log("value=" + value);

  if (dict == "Guessing") {
    if (value.match("^[ 　	]*〒?([0-9０-９]{3}-[0-9０-９]{4})[ 　	]*$") )  {
	// log("郵便番号");
	dict = defaultZipMap;
	value = RegExp.$1;
	//郵便番号
	; // 下の方でマッチするが、計算と思われないようにここで一旦マッチ。
      }
    else if (value.match(calcRegexp)) {
      dict = "Google";
      // log(dict + ": " + value);
      value = value.replace(/ー/g, "-");
      // log(value);
    }

    else if (value.match("^[- a-zA-Z0-9']+$")) {
      dict = defaultEJDict;
    }
    else if (value.match("^(.+)とは$")) {
      dict = "Google-towa";
      value = RegExp.$1;
    }
    else if (value.match("([^ ]{2,3}[都道府県].+[区市群町村].*)$")) {
      //    else if (value.match("([^ 　	]{2,3}[都道府県].+[区市群町村].*|([^ 　	]{1,10}+[区市群町村].+[1-9１-９一-九].*)$")) {
      dict = "Google-maps";
      value = RegExp.$1;
    }
    else if (value.match("(\"[^\"]+\")$")) {
      dict = "Google";
      value = RegExp.$1;
    }
    else {
      dict = "Google";
    }
  }

  // log("dict=" + dict + "; value=" + value);
  
  dictJugglerDoSearch(dict, value);

  // 11/8/2006 (Wed)  次回の検索時に同じかどうかを判断するため。
  // 同じ検索文字列の時に、メニューが選択されたらすぐに検索を開始する
  // ここでいいのかな？
  GPreviousSearchString = value;

}


function keydown (event, input) {
  //  document.getElementById("searchString").value = event.keyCode;
  //   dictJugglerSearchString (input.value);
  //  return;

  if (event.keyCode == 13 || event.keyCode == 33) {
    dictJugglerSearchString(document.getElementById("searchString").value);
  }
  // document.getElementById("searchString").value = event.keyCode;
}



// いまのところ 翻訳訳語辞典用は別なので注意
function escape_utf8(data) {
  if (data == '' || data == null) {
    return '';
  }

  data = data.toString();
  var buffer = '';
  for (var i=0; i<data.length; i++) {
    var c = data.charCodeAt(i);
    if (c <=0x80) {
      // print (" escape_utf8 data=" + data);
      buffer += data.substr(i,1);
      continue;
    }
    // log(c);
    var bs = new Array();
    if (c > 0x10000) { // ４バイトデータ
      bs[0] = 0xF0 | ((c & 0x1C0000) >>> 18);
      bs[1] = 0x80 | ((c & 0x3F000) >>> 12);
      bs[2] = 0x80 | ((c & 0xFC0) >>> 6);
      bs[3] = 0x80 | (c & 0x3F);
    }
    else if (c > 0x800) { // ４バイトデータ
      bs[0] = 0xE0 | ((c & 0xF000) >>> 12);
      bs[1] = 0x80 | ((c & 0xFC0) >>> 6);
      bs[2] = 0x80 | (c & 0x3F);
    }
    else if (c > 0x80) { // 2 バイトデータ
      bs[0] = 0xC0 | ((c & 0x7C0) >>> 6);
      bs[1] = 0x80 | (c & 0x3F);
    }
    else { // １バイトデータ
      // log(c);
      bs[0] = c;
    }
    if (bs[0]<0x80) {
      buffer += bs[0];
    }
    else {
      for (var j=0; j<bs.length; j++) {
	var b = bs[j];
	var hex = nibble_to_hex((b & 0xF0) >>> 4) + nibble_to_hex(b &0x0F);
	buffer += hex;
      }
    }
  }
  return buffer;
}


function yakugoEscape_utf8(data) {
  if (data == '' || data == null) {
    return '';
  }

  data = data.toString();
  var buffer = '';
  for (var i=0; i<data.length; i++) {
    var c = data.charCodeAt(i);
    var bs = new Array();
    if (c > 0x10000) {
      // 4 bytes
      bs[0] = 0xF0 | ((c & 0x1C0000) >>> 18);
      bs[1] = 0x80 | ((c & 0x3F000) >>> 12);
      bs[2] = 0x80 | ((c & 0xFC0) >>> 6);
      bs[3] = 0x80 | (c & 0x3F);
    }
    else if (c > 0x800) {
      // 3 bytes
      bs[0] = 0xE0 | ((c & 0xF000) >>> 12);
      bs[1] = 0x80 | ((c & 0xFC0) >>> 6);
      bs[2] = 0x80 | (c & 0x3F);
    }
    else if (c > 0x80) {
      // 2 bytes
      bs[0] = 0xC0 | ((c & 0x7C0) >>> 6);
      bs[1] = 0x80 | (c & 0x3F);
    }
    else {
      // 1 byte
      bs[0] = c;
    }
    for (var j=0; j<bs.length; j++) {
      var b = bs[j];
      var hex = nibble_to_hex((b & 0xF0) >>> 4)
	+ nibble_to_hex(b &0x0F);buffer += '%'+hex;
    }
  }
  return buffer;
}


function nibble_to_hex(nibble) {
  var chars = '0123456789ABCDEF';
  return chars.charAt(nibble);
}

// 全角英数字をすべて半角に。white spaceの連続を１文字のスペースに

function normalize(string) {
//  log("in normalize; original=" + string);
  string = string.replace(/^[\t 　]+/g, "");
  string = string.replace(/[\t 　]+$/g, "");
  string = string.replace(/[\t 　]+/g, " ");
  string = string.replace(/０/g, "0");
  string = string.replace(/１/g, "1");
  string = string.replace(/２/g, "2");
  string = string.replace(/３/g, "3");
  string = string.replace(/４/g, "4");
  string = string.replace(/５/g, "5");
  string = string.replace(/６/g, "6");
  string = string.replace(/７/g, "7");
  string = string.replace(/８/g, "8");
  string = string.replace(/９/g, "9");


  string = string.replace(/Ａ/g, "A");
  string = string.replace(/Ｂ/g, "B");
  string = string.replace(/Ｃ/g, "C");
  string = string.replace(/Ｄ/g, "D");
  string = string.replace(/Ｅ/g, "E");
  string = string.replace(/Ｆ/g, "F");
  string = string.replace(/Ｇ/g, "G");
  string = string.replace(/Ｈ/g, "H");
  string = string.replace(/Ｉ/g, "I");
  string = string.replace(/Ｊ/g, "J");
  string = string.replace(/Ｋ/g, "K");
  string = string.replace(/Ｌ/g, "L");
  string = string.replace(/Ｍ/g, "M");
  string = string.replace(/Ｎ/g, "N");
  string = string.replace(/Ｏ/g, "O");
  string = string.replace(/Ｐ/g, "P");
  string = string.replace(/Ｑ/g, "Q");
  string = string.replace(/Ｒ/g, "R");
  string = string.replace(/Ｓ/g, "S");
  string = string.replace(/Ｔ/g, "T");
  string = string.replace(/Ｕ/g, "U");
  string = string.replace(/Ｖ/g, "V");
  string = string.replace(/Ｗ/g, "W");
  string = string.replace(/Ｘ/g, "X");
  string = string.replace(/Ｙ/g, "Y");
  string = string.replace(/Ｚ/g, "Z");

  string = string.replace(/ａ/g, "a");
  string = string.replace(/ｂ/g, "b");
  string = string.replace(/ｃ/g, "c");
  string = string.replace(/ｄ/g, "d");
  string = string.replace(/ｅ/g, "e");
  string = string.replace(/ｆ/g, "f");
  string = string.replace(/ｇ/g, "g");
  string = string.replace(/ｈ/g, "h");
  string = string.replace(/ｉ/g, "i");
  string = string.replace(/ｊ/g, "j");
  string = string.replace(/ｋ/g, "k");
  string = string.replace(/ｌ/g, "l");
  string = string.replace(/ｍ/g, "m");
  string = string.replace(/ｎ/g, "n");
  string = string.replace(/ｏ/g, "o");
  string = string.replace(/ｐ/g, "p");
  string = string.replace(/ｑ/g, "q");
  string = string.replace(/ｒ/g, "r");
  string = string.replace(/ｓ/g, "s");
  string = string.replace(/ｔ/g, "t");
  string = string.replace(/ｕ/g, "u");
  string = string.replace(/ｖ/g, "v");
  string = string.replace(/ｗ/g, "w");
  string = string.replace(/ｘ/g, "x");
  string = string.replace(/ｙ/g, "y");
  string = string.replace(/ｚ/g, "z");

  // log("string=" + string);
  return string;
}

function say(string) {
  if (isMacOSXWidget) {
    widget.system("/usr/bin/say " + string, null );
  }
  else {
    speak(string);
  }
}

function run(string) {
  if (isMacOSXWidget) {
    var error = widget.system("/usr/bin/open " + string, null).errorString;
    return error;
  }
  else if (isYahooWidgetForMac) {
    var output = runCommand("/usr/bin/open " + string);
    if (output == "") {
      return undefined;
    }
    else {
      return output;
    }
  }
  else {
    ;
    return null;
  }
}

function getCurrentTimeString (date) {
  //  alert( date.getYear() );
  var year = date.getFullYear();
  var year2 = year - 2000;
  if (year2 == 0) {
    year2 = "00";
  }
  else if (year2 < 10)  {
    year2 = "0" + year2;
  }
  var jyear = year-1988;
  var month = date.getMonth() + 1;
  //log( "month=" + month);

  var mydate = date.getDate();
  var hour = date.getHours();
  if (12 < hour) {
    hour -= 12;
  }
    if (hour <= 9) {
      hour = "0" + hour;
    }
  var minute = date.getMinutes();
    if (minute <= 9) {
      minute = "0" + minute;
    }
  var dayOfTheWeek = getJapaneseDayOfTheWeek(date.getDay());
  var second = date.getSeconds();
  if (second <= 9) {
    second = "0" + second;
  }

  year2 = year - 2000;
  if (year2 < 10) {
    year2 = "0" + year2;
  }
  
  return hour + ":" + minute + "  " + month + "月" + mydate + "日 " + dayOfTheWeek + "曜 " + year + " H" + jyear;
  //  return hour + "時" + minute + "分 " + mydate + "日 " + dayOfTheWeek + "曜 " + year + " H" + jyear;
  //  return hour + ":" + minute + " " + mydate + "日 " + dayOfTheWeek + "曜 " + year + " H" + jyear;
  //  return hour + ":" + minute + " " + month + "/" + mydate + "（" + dayOfTheWeek + "） H" + jyear + " " + year;


  // return hour + ":" + minute + ":" + second + " " + month + "/" + mydate + " " + dayOfTheWeek + " " + year + " H" + jyear + "";

}

function getJapaneseDayOfTheWeek(num) {
  switch (num) {
  case 0:
    return "日";
    break;
  case 1:
    return "月";
    break;
  case 2:
    return "火";
    break;
  case 3:
    return "水";
    break;
  case 4:
    return "木";
    break;
  case 5:
    return "金";
    break;
  case 6:
    return "土";
    break;
  default:
    return "";
  }
}

function updateDateTimeDisplay() {
  // version 2.0  return;
  
  if (isYahooWidget) {
    if (preferences.dateTimePreference.value == "しない") {
      return;
    }
    date = new Date();
    searchString.data = getCurrentTimeString(date);
    GPreviousDateTimeString = searchString.data;
    //      searchString.select(0, -1)
    // dateTime.data = getCurrentTimeString(date);
    delete date;
  }
  else { // mac dashboard
    date = new Date();
    // document.getElementById("datetime").value = getCurrentTimeString(date);
    document.getElementById("datetime").innerHTML =
      '<div style="text-align: center; color: ' + GTimeColor + ';">' + getCurrentTimeString(date) +
      "</div>";
    delete date;
    timerID = setTimeout("updateDateTimeDisplay()", 1000*GTimeUpdateInterval);
    GTimerRunning = true;
  }
}


// この関数は、convert-filename.plのsplitFilenameWithSlash と同じように動作しな
// ければならないので注意。そうしないと、検索がマッチしなくなる
function splitFilenameWithSlash(originalEncodedName) {
  // Joliet規格ではファイル名が64以内なのでこれ以内にする 8/10/2006

  var dir1;
  var dir2;
  var stem;

  var asciiRegexp = new RegExp("^[0-7]");

  if (originalEncodedName.match(asciiRegexp)) {
    dir1 = "300";
    dir2 = "300";
    stem = originalEncodedName;
    if (originalEncodedName.length > 200) {
      stem = originalEncodedName.stem(0, 200);
    }
  }
  // new after 4/4/2007 (Wed)
  else if (originalEncodedName.length >= 12) {
    dir1 = originalEncodedName.substr(0,6);
    dir2 = originalEncodedName.substr(0,12);
    stem = originalEncodedName;
    if (originalEncodedName.length > 200) {
      stem = originalEncodedName.stem(0, 200);
    }
  }
  // １文字のもののために、このels ifも残す  old before 4/4/2007 (Wed)
  else if (originalEncodedName.length >= 6) {
    dir1 = originalEncodedName.substr(0,3);
    dir2 = originalEncodedName.substr(0,6);
    stem = originalEncodedName;
    if (originalEncodedName.length > 200) {
      stem = originalEncodedName.stem(0, 200);
    }
  }
  else {
    dir1 = originalEncodedName;
    dir2 = originalEncodedName;
    stem = originalEncodedName;
  }

  var returnValue = "data/" + dir1 + "/" + dir2 + ".html#" + stem;  
  return returnValue;


  /*
  if (originalEncodedName.length >= 112) { // 8/10/2006変更
    var dir1 = originalEncodedName.substr(0,55); 
    var dir2 = originalEncodedName.substr(55,55);
    var stem = originalEncodedName.substr(110, 56); // 実際これより長くはない
  }
  else if (originalEncodedName.length >= 64) {
    var dir1 = originalEncodedName.substr(0,30);
    var dir2 = originalEncodedName.substr(30,30);
    var stem = originalEncodedName.substr(60);
  }
  // 旧バージョン
  //  if (originalEncodedName.length >= 90) {
  //    var dir1 = originalEncodedName.substr(0,30);
  //    var dir2 = originalEncodedName.substr(30,30);
  //    var stem = originalEncodedName.substr(60,30);
  //}
  else if (originalEncodedName.length >= 8) {
    var dir1 = originalEncodedName.substr(0,3);
    var dir2 = originalEncodedName.substr(3,3);
    var stem = originalEncodedName.substr(6);
  }
  else if (originalEncodedName.length >= 5) {
    var dir1 = originalEncodedName.substr(0,3);
    var dir2 = "000";
    var stem = originalEncodedName.substr(3);
  }
  else {
    dir1 = "000";
    dir2 = "000";
    stem = originalEncodedName;
  }

  var returnValue = "data/" + dir1 + "/" + dir2 + ".html#" + stem;  
  //  var returnValue = "data/" + dir1 + "/" + dir2 + "/" + stem  + ".html";
  return returnValue;

  */
}


function yakugoSplitFilenameWithSlash(originalEncodedName) {
  // Joliet規格ではファイル名が64以内なのでこれ以内にする 8/10/2006
  if (originalEncodedName.length >= 112) { // 8/10/2006変更
    var dir1 = originalEncodedName.substr(0,55); 
    var dir2 = originalEncodedName.substr(55,55);
    var stem = originalEncodedName.substr(110, 56); // 実際これより長くはない
  }
  else if (originalEncodedName.length >= 64) {
    var dir1 = originalEncodedName.substr(0,30);
    var dir2 = originalEncodedName.substr(30,30);
    var stem = originalEncodedName.substr(60);
  }
  // 旧バージョン
  //  if (originalEncodedName.length >= 90) {
  //    var dir1 = originalEncodedName.substr(0,30);
  //    var dir2 = originalEncodedName.substr(30,30);
  //    var stem = originalEncodedName.substr(60,30);
  //}
  else if (originalEncodedName.length >= 8) {
    var dir1 = originalEncodedName.substr(0,3);
    var dir2 = originalEncodedName.substr(3,3);
    var stem = originalEncodedName.substr(6);
  }
  else if (originalEncodedName.length >= 5) {
    var dir1 = "000";
    var dir2 = originalEncodedName.substr(0,3);
    var stem = originalEncodedName.substr(3);
  }
  else {
    dir1 = "000";
    dir2 = "000";
    stem = originalEncodedName;
  }
  
  var returnValue = "data/" + dir1 + "/" + dir2 + "/" + stem  + ".html";
  //  log("encoded path=" + returnValue);
  return returnValue;
}

function log (value) {
//  var answer = confirm(value);
//  if (!answer) {
//    exit; // ワザとエラーを起こす
//  }
//  print(value);
}


function print (value) {
	return;
  var answer = true;
  if (true) { // 
    answer = confirm(value);  // for compatibility with Yahoo! Widget
  }
  if (!answer) {
    openURL("dummy");
  }
}


  function ecofinDoSearchString(word) {
    location.href="./ecofin/index.php?word=" + word;
  }

  function environDoSearchString(word) {
    location.href="./environ/environ.php?word=" + word;
  }

