//ver.1.2
//-----------------------------------------------------------------------------
// 初期処理
//-----------------------------------------------------------------------------
function init()
{
  //
  // 情報取得
  //
  obj_in_status = document.getElementById("in_status");
					// 入力URLチェック状態
  obj_in_url    = document.getElementById("in_url");
					// 入力URL
  obj_in_check  = document.getElementById("in_check");
					// 入力URLチェックボタン
  obj_in_result = document.getElementById("in_result");
					// 入力URLチェック結果

  //
  // ブックマークレット経由の処理
  //
  bookmarklet_url = document.location.search.substring(1);
					// 指定URL取得
  if(bookmarklet_url)
  {
    obj_in_url.value = decodeURIComponent(bookmarklet_url);
					// 指定URLを入力URLにセット
    obj_in_check.disabled = false;	// チェックボタン有効化
  }

  //
  // 初期化
  //
  checking_flg = 0;			// チェック中フラグOFF

  img_sign_ok   = "/image/sign_ok.png";	// 結果画像(OK)
  img_sign_ng   = "/image/sign_ng.png";	// 結果画像(OK)
  img_sign_none = "/image/sign_none.png";
					// 結果画像(空白)
  img_loading   = "/image/loading.gif";	// ローディング

  obj_in_status.src = img_sign_none;	// 入力URLチェック状態OFF

  xmlHttp_out = new Array();		// 外部URLチェックオブジェクト

  //
  // HTTPステータスコード
  //
  http_status = new Array();

  http_status[100] = "Continue";
  http_status[101] = "Switching Protocols";
  http_status[102] = "Processing";
  http_status[200] = "OK";
  http_status[201] = "Created";
  http_status[202] = "Accepted";
  http_status[203] = "Non-Authoritative Information";
  http_status[204] = "No Content";
  http_status[205] = "Reset Content";
  http_status[206] = "Partial Content";
  http_status[207] = "Multi-Status";
  http_status[226] = "IM Used";
  http_status[300] = "Multiple Choices";
  http_status[301] = "Moved Permanently";
  http_status[302] = "Found";
  http_status[303] = "See Other";
  http_status[304] = "Not Modified";
  http_status[305] = "Use Proxy";
  http_status[306] = "(Unused)";
  http_status[307] = "Temporary Redirect";
  http_status[400] = "Bad Request";
  http_status[401] = "Unauthorized";
  http_status[402] = "Payment Required";
  http_status[403] = "Forbidden";
  http_status[404] = "Not Found";
  http_status[405] = "Method Not Allowed";
  http_status[406] = "Not Acceptable";
  http_status[407] = "Proxy Authentication Required";
  http_status[408] = "Request Timeout";
  http_status[409] = "Conflict";
  http_status[410] = "Gone";
  http_status[411] = "Length Required";
  http_status[412] = "Precondition Failed";
  http_status[413] = "Request Entity Too Large";
  http_status[414] = "Request-URI Too Long";
  http_status[415] = "Unsupported Media Type";
  http_status[416] = "Requested Range Not Satisfiable";
  http_status[417] = "Expectation Failed";
  http_status[418] = "I'm a teapot";
  http_status[422] = "Unprocessable Entity";
  http_status[423] = "Locked";
  http_status[424] = "Failed Dependency";
  http_status[425] = "(Unordered Collection)";
  http_status[426] = "Upgrade Required";
  http_status[500] = "Internal Server Error";
  http_status[501] = "Not Implemented";
  http_status[502] = "Bad Gateway";
  http_status[503] = "Service Unavailable";
  http_status[504] = "Gateway Timeout";
  http_status[505] = "HTTP Version Not Supported";
  http_status[506] = "Variant Also Negotiates";
  http_status[507] = "Insufficient Storage";
  http_status[510] = "Not Extended";

  http_status[900] = "no such host or invalid url";
					// ホスト名不正
  http_status[901] = "redirected to non-HTTP site, not supported.";
					// HTTP以外のサイトにリダイレクト
}

//-----------------------------------------------------------------------------
// 後処理
//-----------------------------------------------------------------------------
function last()
{
  checking_flg = 0;				// チェック中フラグOFF

  obj_in_check.disabled = false;		// チェックボタン有効化
}

//-----------------------------------------------------------------------------
// メッセージ作成
//-----------------------------------------------------------------------------
function makeMessage(status)
{
  var msg = http_status[status];		// メッセージ文取得

  if (status < 900)				// 900番未満はHTTPエラー
  {
    msg = msg + " (HTTP status = " + status + ")";
  }

  return(msg);
}

//-----------------------------------------------------------------------------
// 入力URL妥当性チェック
//-----------------------------------------------------------------------------
function inCheck()
{
  //
  // URLチェック
  //
  in_url = obj_in_url.value;
  in_check_match = in_url.match(/^http:\/\//);
					// URLチェック(以下がOK)
					// - http://〜
  //
  // チェックボタンの有効/無効化
  //
  if (in_check_match)			// URLチェックがOKなら
  {
    if (checking_flg != 1)		// チェック中でなければ
    {
      obj_in_check.disabled = false;	// チェックボタン有効化
    }
  }
  else
  {
    obj_in_check.disabled = true;	// チェックボタン無効化
  }
}

//-----------------------------------------------------------------------------
// 外部URLチェック
//-----------------------------------------------------------------------------
function hrefCheck()
{
  checking_flg = 1;				// チェック中フラグON

  href_cnt = 0;					// 外部URLカウンタ初期化

  obj_in_check.disabled = true;			// チェックボタン無効化

  obj_in_result.innerHTML = "";			// 入力URLチェック結果OFF

  obj_out_result = document.getElementById("result");
						// 結果ブロック取得
  obj_out_result.innerHTML = "";		// 結果ブロック初期化

  xmlHttp = new createXMLHttp();		// XMLHttpRequest生成

  if(xmlHttp)
  {
    obj_in_status.src = img_loading;		// ローディング画像

    var request = "chk.cgi?type=in&url=" + escape(in_url);
						// リクエスト生成

    xmlHttp.open("GET", request, true); 	// リクエスト送信

    xmlHttp.onreadystatechange = hrefCheck_proc;
						// リクエスト完了時処理定義
    xmlHttp.send(null);
  }
}

//
// 対象URLチェック
//
function hrefCheck_proc()
{
  if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
						// リクエスト正常終了
  {
    var href_all = xmlHttp.responseText;	// 処理結果取得

    //
    // 入力URLに外部URLなし
    //
    if (!href_all)
    {
      obj_in_status.src = img_sign_ok;

      obj_in_result.style.color = "#91ca86";

      obj_in_result.innerHTML = "no external link";

      last();

      return;
    }

    //
    // 入力URLエラー
    //
    if(href_all.match(/^error/))
    {
      var href_status = href_all.split("|")[1];
					// エラーNo取得

      var msg = makeMessage(href_status);
					// メッセージ作成

      obj_in_status.src = img_sign_ng;

      obj_in_result.style.color = "#ed6465";

      obj_in_result.innerHTML = msg;

      last();

      return;
    }

    //
    // 入力URLのはてなブックマーク数出力
    //
    var in_result_html = "<a href='http://b.hatena.ne.jp/entry/" + in_url + "'>"
                       + "<img src='http://b.hatena.ne.jp/entry/image/"
                       + in_url + "' alt='' /></a>";
    obj_in_result.innerHTML = in_result_html;

    //
    // 外部URLチェック
    //
    obj_in_status.src = img_sign_ok;

    href_list = href_all.split("|");	// 各URLに分割
    href_num = href_list.length;	// URL数保持

    for (var i = 0; i < href_num; i++)	// URL毎に処理
    {
      var href_url = href_list[i];	// URL

      var href_html = "<a href='" + href_url + "'>" + href_url + "</a>"
                    + "<a href='http://b.hatena.ne.jp/entry/" + href_url + "'>"
                    + "<img src='http://b.hatena.ne.jp/entry/image/"
                    + href_url + "' alt='' /></a>";
					// 外部URLのはてなブックマーク数生成

      //
      // URL行生成
      //
      var el_href = document.createElement("h3");
      el_href.innerHTML = href_html;
      obj_out_result.appendChild(el_href);

      //
      // エラーメッセージ行生成(初期時は空)
      //
      var el_msg = document.createElement("h4"); 
      el_msg.innerHTML = "message area";
      el_msg.style.display = "none";
      obj_out_result.appendChild(el_msg);
    }

    //
    // 要素集合保持
    //
    list_href = document.getElementsByTagName("h3");
					// URL行
    list_msg  = document.getElementsByTagName("h4");
					// エラーメッセージ行
    //
    // URL実在チェック
    //
    for (j = 0; j < href_num; j++)	// URL毎に処理
    {
      setTimeout("hrefCheck_proc2('" + j + "')", 1);
    }
  }
}

//
// 外部URLチェック
//
function hrefCheck_proc2(j)
{
  var href_url = href_list[j];		// URL

  var req_href = "chk.cgi?type=href&url=" + escape(href_url) + "&num=" + j;
					// リクエスト生成

  xmlHttp_out[j] = new createXMLHttp();	// XMLHttpRequest生成

  xmlHttp_out[j].open("GET", req_href, true);
					// リクエスト送信

  xmlHttp_out[j].onreadystatechange = function()
  {
    if (xmlHttp_out[j].readyState == 4 && xmlHttp_out[j].status == 200)
    {
      var data = xmlHttp_out[j].responseText;
					// 処理結果取得

      setTimeout("dispData('" + data + "')", 10);
					// 処理結果出力
    }
  }

  xmlHttp_out[j].send(null);
}

//
// 外部URLチェック結果出力
//
function dispData(data)
{
  var href_no     = data.split("|")[0];	// URL番号取得 
  var href_url    = data.split("|")[1];	// URL取得 
  var href_status = data.split("|")[2];	// ステータス取得

  if(href_status == 200)		// URL利用可能
  {
    bgimage = img_sign_ok;		// 背景画像(OK)指定
  }
  else					// URL利用不可
  {
    bgimage = img_sign_ng;		// 背景画像(NG)指定

    msg = makeMessage(href_status);	// メッセージ作成

    list_msg[href_no].innerHTML = msg;
    list_msg[href_no].style.display = "block";
  }

  list_href[href_no].style.backgroundImage = "url(" + bgimage + ")";

  href_cnt++;				// URLチェックカウンタインクリメント

  if (href_cnt >= href_num)		// 全ての外部URLチェックが終わったら
  {
    last();				// 終了処理
  }
}

//-----------------------------------------------------------------------------
// XMLHttpRequestオブジェクト定義(クロスブラウザ対応)
//-----------------------------------------------------------------------------
function createXMLHttp()
{
  var xmlHttp;

  if (window.XMLHttpRequest)			// Safari, Firefox, Opera...
  {
    xmlHttp = new XMLHttpRequest();
  }
  else						// IE4,5,6
  {
    if (window.ActiveXObject)
    {
      try
      {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(e)
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  }

  return(xmlHttp);
}

