/***
 * financeBoard.js  (require prototype.js)
 *
 *      IRI Ubiteq, Co. 2007.06.05
 ***/

var target = 'financeBoard.rbx';
var list = new Array();
var searchList = new Array();
var update = new Array();
var updateTime = '00:00';
var async = false;
var light = 3;
var mainTimer;

function init() {
  var default_list = new Array('6662', '(株)IRIユビテック', '', '', '');
  list.push(default_list);
  var default_list = new Array('4689', 'ヤフー(株)', '', '', '');
  list.push(default_list);
  displayBoardLayout();
//  async = true;
  requestValue();
  mainTimer = setTimeout('asyncRequest()', 60 * 1000);
}

function term() {
  clearTimeout(mainTimer);
}

function asyncRequest() {
  async = true;
  light = 3;
  requestValue();
  mainTimer = setTimeout('asyncRequest()', 60 * 1000);

}

function getToday() {
  var date = new Date();
  var year = date.getYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  if (year < 2000) {
    year += 1900;
  }
  if (month < 10) {
    month = '0' + month;
  }
  if (day < 10) {
    day = '0' + day;
  }
  return year + '/' + month + '/' + day;
}

function displayBoardLayout() {
  var oTable = document.createElement('table');
//  oTable.setAttribute('border', '1');
  oTable.setAttribute('width', '100%');
  var oTbody = document.createElement('tbody');
  oTbody.setAttribute('id', 'list');
  var oTr = document.createElement('tr');
  var oTd = document.createElement('td');
  oTd.colSpan = 2;
  var oTitle = document.createElement('font');
  oTitle.style.color = 'white';
  oTitle.style.fontFamily = '"ＭＳ Ｐゴシック", Osaka, "ヒラギノ角ゴ Pro W3"';
  oTitle.style.fontSize = '16px';
  oTitle.innerHTML = getToday() + ' <font id="time">--:--</font>';
  oTd.appendChild(oTitle);
  oTr.appendChild(oTd);
  var oTd = document.createElement('td');
  oTd.colSpan = 2;
  oTd.setAttribute('align', 'right');
  var oLink = document.createElement('a');
  oLink.setAttribute('href', '#');
  oLink.setAttribute('onclick', new Function('config();'));
  oLink.setAttribute('onClick', 'config();');
  var oFont = document.createElement('font');
  oFont.style.color = 'aqua';
  oFont.style.backgroundColor = 'gray';
  oFont.style.fontFamily = '"ＭＳ Ｐゴシック", Osaka, "ヒラギノ角ゴ Pro W3"';
  oFont.style.fontSize = '16px';
  oFont.innerHTML = '追加';
  oLink.appendChild(oFont);
  oTd.appendChild(oLink);
  oTr.appendChild(oTd);

  oTbody.appendChild(oTr);

  for (var i = 0; i < list.length; i++) {
    var oTr = document.createElement('tr');
    for (var j = 0; j < list[i].length; j++) {
      if (j != 2) {
        var oTd = document.createElement('td');
        var oFont = document.createElement('font');
        if (j > 2) {
          oTd.setAttribute('align', 'right');
          oTd.setAttribute('nowrap', 'nowrap');
          if (j == 4) {
            oTd.width = '45px';
          }
          var idName = i + '-' + j;
          oFont.setAttribute('id', idName);
          oFont.style.visibility = 'visible';
          oFont.style.fontWeight = 'bold';
        }
        oFont.style.color = 'white';
        oFont.style.fontFamily = '"ＭＳ Ｐゴシック", Osaka, "ヒラギノ角ゴ Pro W3"';
        oFont.style.fontSize = '12px';
        oFont.innerHTML = list[i][j];
//        if (j == 4) {
//          var check = list[i][j];
//          if (check.match(/^\+\d/)) {
//            oFont.style.color = 'red';
//          } else if (check.match(/^-\d/)) {
//            oFont.style.color = 'blue';
//          }
//        }
        oTd.appendChild(oFont);
        oTr.appendChild(oTd);
      }
    }
    oTbody.appendChild(oTr);
  }
  oTable.appendChild(oTbody);
  $('board').appendChild(oTable);
}

function addLastEntry() {
  var index = list.length - 1;
  var oTr = document.createElement('tr');
  for (var j = 0; j < list[index].length; j++) {
    if (j != 2) {
      var oTd = document.createElement('td');
      var oFont = document.createElement('font');
      if (j > 2) {
        oTd.setAttribute('align', 'right');
        oTd.setAttribute('nowrap', 'nowrap');
        var idName = index + '-' + j;
        oFont.setAttribute('id', idName);
        oFont.style.visibility = 'visible';
        oFont.style.fontWeight = 'bold';
      }
      oFont.style.color = 'white';
      oFont.style.fontFamily = '"ＭＳ Ｐゴシック", Osaka, "ヒラギノ角ゴ Pro W3"';
      oFont.style.fontSize = '12px';
      oFont.innerHTML = list[index][j];
      oTd.appendChild(oFont);
      oTr.appendChild(oTd);
    }
  }
  $('list').appendChild(oTr);
}

function displayBoard() {
  $('time').innerHTML = updateTime;
  for (var i = 0; i < update.length; i++) {
    var value = update[i] + '-3';
    var diff = update[i] + '-4';
    $(value).innerHTML = list[update[i]][3];
    $(diff).innerHTML = list[update[i]][4];
    var check = list[update[i]][4];
    if (check.match(/^\+\d/)) {
      $(diff).style.color = 'red';
    } else if (check.match(/^-\d/)) {
      $(diff).style.color = 'lime';
    } else {
      $(diff).style.color = 'white';
    }
  }
  lightOn();
}

function lightOn() {
  for (var i = 0; i < update.length; i++) {
    var value = update[i] + '-3';
    var diff = update[i] + '-4';
    $(value).style.visibility = 'visible';
    $(diff).style.visibility = 'visible';
  }
  if (async && (light > 0)) {
    timerID = setTimeout('lightOff()', 500);
    light--;
  }
}

function lightOff() {
  for (var i = 0; i < update.length; i++) {
    var value = update[i] + '-3';
    var diff = update[i] + '-4';
    $(value).style.visibility = 'hidden';
    $(diff).style.visibility = 'hidden';
  }
  timerID = setTimeout('lightOn()', 500);
}

function config() {
  var oDiv = document.createElement('div');
  oDiv.style.backgroundColor = 'silver';
  oDiv.style.position = 'absolute';
  oDiv.style.left = '5px';
  oDiv.style.top = '5px';
  oDiv.style.width = '290px';
  oDiv.style.height = '190px';
  oDiv.style.overflowY = 'scroll';
  oDiv.setAttribute('id', 'result');
  var oTable = document.createElement('table');
  oTable.setAttribute('width', '270px');
  var oTbody = document.createElement('tbody');
  var oTr = document.createElement('tr');

  var oTd = document.createElement('td');
  oTd.setAttribute('align', 'right');
  var oFont = document.createElement('font');
  oFont.style.color = 'black';
  oFont.style.fontFamily = '"ＭＳ Ｐゴシック", Osaka, "ヒラギノ角ゴ Pro W3"';
  oFont.style.fontSize = '14px';
  oFont.innerHTML = '銘柄ｺｰﾄﾞ・企業名';
  oTd.appendChild(oFont);
  oTr.appendChild(oTd);

  var oTd = document.createElement('td');
  oTd.setAttribute('align', 'center');
  var oInput = document.createElement('input');
  oInput.setAttribute('type', 'text');
  oInput.setAttribute('size', '9');
  oInput.setAttribute('id', 'search-word');
  oInput.style.fontFamily = '"ＭＳ Ｐゴシック", Osaka, "ヒラギノ角ゴ Pro W3"';
  oInput.style.fontSize = '14px';
  oTd.appendChild(oInput);
  oTr.appendChild(oTd);

  var oTd = document.createElement('td');
  oTd.setAttribute('align', 'center');
  var oInput = document.createElement('input');
  oInput.setAttribute('type', 'button');
  oInput.style.fontFamily = '"ＭＳ Ｐゴシック", Osaka, "ヒラギノ角ゴ Pro W3"';
  oInput.style.fontSize = '14px';
  oInput.value = '検索';
  oInput.setAttribute('onclick', new Function('requestSearch();'));
  oInput.setAttribute('onClick', 'requestSearch();');
  oTd.appendChild(oInput);
  oTr.appendChild(oTd);

  var oTd = document.createElement('td');
  oTd.setAttribute('align', 'right');
  var oLink = document.createElement('a');
  oLink.setAttribute('href', '#');
  oLink.setAttribute('onclick', new Function('closeConfig();'));
  oLink.setAttribute('onClick', 'closeConfig();');
  var oFont = document.createElement('font');
  oFont.style.color = 'aqua';
  oFont.style.backgroundColor = 'gray';
  oFont.style.fontFamily = '"ＭＳ Ｐゴシック", Osaka, "ヒラギノ角ゴ Pro W3"';
  oFont.style.fontSize = '14px';
  oFont.innerHTML = '×';
  oLink.appendChild(oFont);
  oTd.appendChild(oLink);
  oTr.appendChild(oTd);

  oTbody.appendChild(oTr);
  oTable.appendChild(oTbody);
  oDiv.appendChild(oTable);
  $('board').appendChild(oDiv);
}

function displaySearch() {
  if ($('result-list')) {
    $('result').removeChild($('result-list'));
  }

  var oTable = document.createElement('table');
  oTable.setAttribute('width', '270px');
  oTable.setAttribute('id', 'result-list');
  var oTbody = document.createElement('tbody');

  for (var i = 0; i < searchList.length; i++) {
    var oTr = document.createElement('tr');
    var oTd = document.createElement('td');
    var oFont = document.createElement('font');
    oFont.style.color = 'black';
    oFont.style.fontFamily = '"ＭＳ Ｐゴシック", Osaka, "ヒラギノ角ゴ Pro W3"';
    oFont.style.fontSize = '12px';
    oFont.innerHTML = searchList[i][0];
    oTd.appendChild(oFont);
    oTr.appendChild(oTd);

    var oTd = document.createElement('td');
    var oFont = document.createElement('font');
    oFont.style.color = 'black';
    oFont.style.fontFamily = '"ＭＳ Ｐゴシック", Osaka, "ヒラギノ角ゴ Pro W3"';
    oFont.style.fontSize = '12px';
    oFont.innerHTML = searchList[i][1];
    oTd.appendChild(oFont);
    oTr.appendChild(oTd);

    var oTd = document.createElement('td');
    oTd.setAttribute('align', 'right');
    var oInput = document.createElement('input');
    oInput.setAttribute('type', 'button');
    oInput.style.fontFamily = '"ＭＳ Ｐゴシック", Osaka, "ヒラギノ角ゴ Pro W3"';
    oInput.style.fontSize = '12px';
    oInput.value = '登録';
    oInput.setAttribute('id', i);
    oInput.setAttribute('onclick', new Function('add(this);'));
    oInput.setAttribute('onClick', 'add(this);');
    oTd.appendChild(oInput);
    oTr.appendChild(oTd);
    oTbody.appendChild(oTr);
  }
  oTable.appendChild(oTbody);
  $('result').appendChild(oTable);
}

function add(element) {
  var entry = new Array(searchList[element.id][0], searchList[element.id][1], '', '', '');
  list.push(entry);
  $('board').removeChild($('result'));
  addLastEntry();
//  async = true;
  requestValue();
}

function closeConfig() {
  $('board').removeChild($('result'));
}

function requestValue() {
  var time = new Date();
  var param = 'time=' + time.getTime() + '&word=&code=';
//  var param = 'word=&code=';
  for (var i = 0; i < list.length; i++) {
    param += list[i][0];
    if (i + 1 != list.length) {
      param += ';';
    }
  }
  request(param);
}

function requestSearch() {
//  var time = new Date();
//  var param = 'time=' + time.getTime() + '&code=&word=';
  var param = 'code=&word=';
  var word = $('search-word').value;
  param += word;
  request(param);
}

function request(param) {
  var ajaxConnect = new Ajax.Request( target,
                               { method: 'get',
                                 parameters: param,
                                 onSuccess: success,
                                 onFailure: failure,
                                 onComplete: complete });
}

function loading(response) {
}

function complete(response) {
}

function success(response) {
  var err = $('error');
  if (err) {
    $('result').removeChild(err);
  }
  var res = response.responseText
  /* get value */
  if (res.match(/^[0-9]?[0-9](:|\/)[0-9]?[0-9]#/)) {
    update = [];
    var datas = res.split(';');
    for (var i = 0; i < datas.length; i++) {
      var info_a = datas[i].split('#');
      /* check time */
      if (list[i][2] != info_a[0]) {
        if (info_a[0].match(/[\d]?[\d]:[\d][\d]/)) {
          var info_a_time = info_a[0].split(':');
          var update_time = updateTime.split(':');
          if ((update_time[0] < info_a_time[0]) ||
          ((update_time[0] == info_a_time[0]) && (update_time[1] < info_a_time[1]))) {
            updateTime = info_a[0];
          }
        }
        /* check value */
        if (list[i][3] != info_a[1]) {
          update.push(i);
        }
        for (var j = 0; j < info_a.length; j++) {
          list[i][j + 2] = info_a[j];
        }
      }
    }
    displayBoard();

  /* search code */
  } else if (res.match(/^[0-9][0-9][0-9][0-9]#/)){
    searchList = [];
    var datas = res.split(';');
    for (var i=0; i<datas.length; i++) {
      var info_a = datas[i].split('#');
      searchList.push(info_a);
    }
    displaySearch();

  } else {
    failure(response);
  }
}

function failure(response) {
  var div = $('result');
  if (div) {
    var oFont = document.createElement('font');
    oFont.style.color = 'red';
    oFont.style.fontFamily = '"ＭＳ Ｐゴシック", Osaka, "ヒラギノ角ゴ Pro W3"';
    oFont.style.fontSize = '14px';
    oFont.setAttribute('id', 'error');
    oFont.innerHTML = '一致する銘柄は見つかりませんでした';
    div.appendChild(oFont);
  }
}