// ----------------------------------------------------
// くりっくかうんた (ajax + perl)
// clickCounter.js
// Ver 1.20
// 03.01.2006
//
// author: てんてす@赤いほう (http://www.akaiho.com)
// 利用、改変自由
// 転載は勘弁してクダサイ
// ----------------------------------------------------

// +++ 設定項目 +++

var maxWidth = 40;    // 棒グラフの横幅の最大値。
// クリック数が最大値以上の場合、棒グラフは最大値を超えない


var isScaled = true;  // スケーリング（縮小表示）しないなら false, するならtrue
var scaleNum = 30;     // 何分の１にスケールするか(上でfalseだったら無視)
                      // 例えば 2 と指定したら、 200クリックの場合 200/2 で100ドットの棒グラフに。

var cgiFile = './clicks.cgi';             // clicks.cgiのファイル名（変えてなければそのまま）
var barGraphFile = "./bargraph0y.png";  // 棒グラフの表示に使う画像

// +++ 項目終了 +++

// あとはコード読みたい人ダケドウゾ
// ---------------------------------------------------------------------

// -- generating a request to read a file fName

var useimg = true;

function loadTextFile(fName)
{
  httpObj = createXMLHttpRequest(displayData);

  if (httpObj)
    {
      httpObj.open("GET",fName,true);
      httpObj.send(null);
    }
}

// -- generating a request, and send it to the server
function countclick(thehref){
  httpObj = createXMLHttpRequest(displayData);
  if(httpObj){
    var query;
    query = "name="+decodeURIComponent(thehref);
//    query = "name="+decodeURIComponent(document.getElementById(theid).value);
//    var query = "name=" + document.ajaxForm.test.value;
    httpObj.open('POST', cgiFile);
    httpObj.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
    httpObj.send(query);
    document.getElementById("resultData").innerText = "Sent\n";
  }
}


// -- asynchronously recieved from server
function displayData(){
  if ((httpObj.readyState == 4) && (httpObj.status == 200)){
//    document.getElementById("resultData").innerText = "Succeed\n" + httpObj.responseText;
    if(document.getElementById("resultData") != null){
      document.getElementById("resultData").innerText = "";
    }
    parseData(httpObj.responseText);
  } else {
    if(document.getElementById("resultData") != null){
      document.getElementById("resultData").innerText = "Loading...";
    }
  }
}


// -- parsing the chunk of data, and output the result
function parseData(receivedText){
  var width;
  var Lines = receivedText.split("\n");
  for( var i=0; i<Lines.length; i++){
    var keys = Lines[i].split("<>");
    if(document.getElementById(keys[0]) != null){
      document.getElementById(keys[0]).innerHTML = "" + keys[1] + "clicks";
    }
    if(document.getElementById(keys[0] + "_bar") != null){
      if(isScaled){
        width = Math.round( keys[1] / scaleNum );
      }
      else { width = keys[1]; }

      if(width > maxWidth){ width = maxWidth; }

      if(useimg){
        document.getElementById(keys[0] + "_bar").style.width = "" + width + "px";
//        document.getElementById(keys[0] + "_bar").innerHTML =
//          "<img src=\"" + barGraphFile + "\" height=\"10\" width=\"" + width + "\" />";
      }
      else {
        document.getElementById(keys[0] + "_bar").style.width  = "" + width + "px";
        document.getElementById(keys[0] + "_bar").innerHTML = "&nbsp\;";
//        document.getElementById(keys[0] + "_bar").style.backgroundPosition = "0% 50%";
        document.getElementById(keys[0] + "_bar").style.backgroundColor = "#CC3333";
        
      }
    }
  }
}


