//
// execms.js
//
// @author Yutaka Chiba <y.chiba@i-exec.jp>
// $Id:$
//
var ExeCMS = new Object;

ExeCMS.emSelectedMenu = function (id) {
    var menu = $('menu_' + id);
    if (menu == undefined) {
    return;
    }
    return;
    menu.style.color = '#000';
    menu.style.fontWeight = 'normal';
    menu.style.backgroundColor = '#ffffff';
}

ExeCMS.emSelectedRow = function (id) {
    var target = $(id);
    if (target == undefined) {
    return;
    }
    var trlist = target.getElementsByTagName('tr');
    for (var i = 0; i < trlist.length; ++i) {
        trlist[i].onmouseover = function() {
            this.className = 'lc_on';
        }
        trlist[i].onmouseout = function() {
            this.className = 'lc_off';
        }
    }
}

ExeCMS.emSelectedField = function () {

  var fields = document.getElementsByTagName('input');
  for (var i = 0; i < fields.length; ++i) {
      if (fields[i].type == 'text' ||
          fields[i].type == 'password') {
                      fields[i].onfocus = function (evt) {this.style.backgroundColor = '#eec'};
                      fields[i].onblur = function (evt) {this.style.backgroundColor = '#fff'};
                      }
  }

  var fields = document.getElementsByTagName('textarea');
  for (var i = 0; i < fields.length; ++i) {
                      fields[i].onfocus = function (evt) {this.style.backgroundColor = '#eec'};
                      fields[i].onblur = function (evt) {this.style.backgroundColor = '#fff'};
  }
}


ExeCMS.confirmAndDelete = function(f, msg) {

    var checked_flg = 0;
    for (var i = 0; i < f.length; ++i) {
        if (f[i].type == 'checkbox' &&
            f[i].checked) {
            checked_flg = 1;
            break;
        }
    }

    if (checked_flg != 1) {
        alert('削除したいデータにチェックを入れてください。');
        return;
    }

    if (!msg) {
        msg = '削除してもよろしいですか？\nこの操作は取り消せません。'
    }

    if (window.confirm(msg)) {
        f.submit();
    }
}


ExeCMS.showCalendar = function (diff, path, sy, sm, sd) {

    var today = new Date;
    var y = today.getFullYear();
    var m = today.getMonth() + 1 + diff;
    var d = today.getDate();

    var leap_year = 0;
    if ((y % 4 == 0 && y % 100 != 0) ||
        (y % 400 == 0)) {
        leap_year = 1;
    }
    var lom = new Array(31, 28 + leap_year, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    var days = 0;
    for (var i = 0; i < m - 1; ++i) {
        days += lom[i];
    }
    var week = Math.floor((y * 365.2425 + days) % 7);
    var j = 0;

    var cal = '<table class="cal"><tr><th colspan="7">' + y + '/' + m + "</th></tr>\n";

    cal += "<tr>\n";
    for (i = 0; i < week; ++i, ++j) {
        cal += "<td>&nbsp;</td>\n";
    }
    for (i = 1; i <= lom[m - 1]; ++i) {
        cal += '<td';
        if (sy == y &&
            sm == m &&
            sd == i) {
            cal += ' class="selected"';
        } else if (diff == 0 &&
            i == d) {
            cal += ' class="today"';
        }
        if (diff == 0 &&
            i <= d) {
            cal += '><a href="' + path  + '/y/' + y + '/m/' + m + '/d/' + i + '/">' + i + '</a></td>';
        } else if (diff < 0) {
            cal += '><a href="' + path  + '/y/' + y + '/m/' + m + '/d/' + i + '/">' + i + '</a></td>';
        } else {
            cal += '>' + i + '</td>';
        }

        ++j;
        if (j > 6) {
            cal += "</tr>\n<tr>\n";
            j = 0;
        }
    }

    for (i = j; i <= 6; ++i) {
        cal += "<td>&nbsp;</td>\n";
    }
    cal += '</tr></table>';
    document.write(cal);
}


ExeCMS.searchPostalCode = function (url, p1, p2, pref, ad) {
    var p = p1;
    if (p2 != '') {
        p += p2;
    }
    url += '/' + p;

    new Ajax.Request(url,
                     {
                         method: 'get',
                         onComplete: showResponse
                     });

    function showResponse(originalRequest)
    {
        var o = originalRequest.responseText.split(':');
        if (o.length != 3) {
            alert('該当する住所が見つかりません。');
            return false;
        }
        pref.selectedIndex = o[1];
        ad.value = o[2];
    }
}


ExeCMS.checkPasswordSafety = function (f) {

  var val = $(f).value;

  var pat = 0;
  var len = val.length;
  if (len > 10) {
    len = 10;
  }

  if (val.match(/[a-zA-Z]/)) pat += 15;
  if (val.match(/\d/))    pat += 15;
  if (val.match(/\W/))    pat += 20;
  if (val.match(/[0-9]/) &&
      val.match(/[a-z]/) &&
      val.match(/[A-Z]/)) {
      pat += 30;
  }
  if (val.match(/(?:.)$1{2,}/)) {
    pat -= 10;
  }

  var star = new Array(
               10000,
               30000000,
               1000000000,
               300000000000,
               5000000000000,
               500000000000000,
               300000000000000000,
               5000000000000000000
            );

  var res = parseInt(Math.pow(pat, len));
  var view = '';

  for (var i = 0; i < 5; ++i) {
    view += (star[i] <= res) ? '★' : '☆';
  }

  $('res_' + f).innerHTML = view;

}

ExeCMS.selectOption = function (parentValue, value, text) {

  this.parentValue = parentValue;

  this.setOption = function() {
    this.value = value;
    this.text = text;
  };

  return this;
}

ExeCMS.linkedSelectBox = function(id, selected) {

  function getObject() {

    var obj = $(id);
    if(!obj.options && ((typeof obj.length) == "number")) {
      if(obj.length > 0) {
        obj = obj[0];
      } else {
        obj = null;
      }
    }
    return obj;
  }

  var options = [];

  this.registOption = function(option) {
    options[options.length] = option;
  };

  var child = null;

  this.setChild = function(childObj) {
    child = childObj;
  };

  this.make = function(parentValue) {
    var obj = getObject();

    if(obj) {
      obj.options.length = 0;

      var opt = (parentValue != null) ? [] : options;
      if(parentValue != null) {
        for(var i = 0; i < options.length; i++) {
          if( (options[i].parentValue == null) || (options[i].parentValue == parentValue) ) {
            opt[opt.length] = options[i];
          }
        }
      }

      obj.options.length = opt.length;
      for(var i = 0; i < opt.length; i++) {
        opt[i].setOption.call(obj.options[i]);
      }

      for (var i = 0; i < obj.options.length; ++i) {
         if (obj.options[i].value == selected) {
           obj.options[i].selected = true;
         }
      }

      if(child) {
        child.make(obj.value);
      }
    }
  };

  return this;
}

ExeCMS.checkDateStr = function(id) {
  var obj = $(id);
  var v = obj.value;
  if (v != '' &&
      !v.match(/^2\d{3}[0-1]\d[0-3]\d$/)) {
      alert('日付はYYYYMMDD形式で入力してください。');
      obj.value = '';
      obj.focus();
  }
}

ExeCMS.openwin = function(link, w, h) {
  window.open(link, '_blank', 'width=' + w + ',height=' + h + ',menubar=no,toolbar=no');
}
