function init_birthday(){
  var manager = new CookieManager();
  var date = new Date(manager.getCookie("mote_birthday"));
  if (isNaN(date) || date.getTime() == 0){
    date = new Date();
  }
  document.frm.birthday_year.value=date.getFullYear();
  document.frm.birthday_month.value=date.getMonth()+1;
  document.frm.birthday_day.value=date.getDate();
}

function save()
{
  var manager = new CookieManager();
  var date = new Date(document.frm.birthday_year.value + '/' +
                      document.frm.birthday_month.value + '/' +
                      document.frm.birthday_day.value);
  manager.setCookie("mote_birthday", date.toDateString());
  manager.setCookie("mote_blood", document.frm.blood.value);
  manager.setCookie("mote_color", escape(document.frm.color.value));
  manager.setCookie("mote_food", escape(document.frm.food.value));
}

function open_result(){
  save();
  init_birthday();
  window.open("result.html", "result", "scrollbars=1,resizable=1");
}

function select_date(name)
{
  var now = new Date();
  str = '<select name="'+name+'_year">';
  for (i = now.getFullYear() + 10; i >= 1900 ; i--){
    str += '<option value='+i+'>'+i+'</option>';
  }
  str += '</select>年';
  str += '<select name="'+name+'_month">';
  for (i = 1; i <= 12; i++){
    str += '<option value='+i+'>'+i+'</option>';
  }
  str += '</select>月';
  str += '<select name="'+name+'_day">';
  for (i = 1; i <= 31; i++){
    str += '<option value='+i+'>'+i+'</option>';
  }
  str += '</select>日';
  return str;
}

function select_color()
{
  var foods = [
    ['赤', 'red'], ['青', 'blue'], ['緑', 'green'],
    ['紫', 'magenta'], ['水色', 'cyan'], ['黄色', 'gold'],
    ['白', 'gray'], ['黒', 'black']
    ];
  str = '<select name="color">';
  foods.each(function(f) {
    str += '<option style="color:'+f[1]+'" value="'+f[0]+'">'+f[0]+'</option>';
  });
  str += '</select>';
  return str;
}

function select_food()
{
  var foods = [
    'ラーメン', 'チャーハン', 'すし', 'さしみ',
    'うどん', 'そば', '焼きそば', '焼き肉', 'すき焼き', 'しゃぶしゃぶ',
    'ステーキ', 'ハンバーグ', 'カレーライス', 'オムライス',
    'ピザ', 'スパゲッティ', 'ドリア'
    ];
  str = '<select name="food">';
  foods.each(function(f) {
    str += '<option value="'+f+'">'+f+'</option>';
  });
  str += '</select>';
  return str;
}

document.write('<form name="frm" class="calc-input">');
document.write('<div class="calc-header">モテ期計算</div>');
document.write('<dl>');
document.write('<dt>誕生日:</dt>');
document.write('<dd>'+select_date('birthday')+'</dd>');

document.write('<dt>血液型:</dt>');
document.write('<dd>');
document.write('<select name="blood">');
document.write('<option value="A">A</option>');
document.write('<option value="B">B</option>');
document.write('<option value="O">O</option>');
document.write('<option value="AB">AB</option>');
document.write('</select>');
document.write('</dd>');
document.write('<dt>好きな色:</dt>');
document.write('<dd>'+select_color()+'</dd>');
document.write('</dd>');
document.write('<dt>好きな食べ物:</dt>');
document.write('<dd>'+select_food()+'</dd>');
document.write('</dd>');
document.write('</dl>');
document.write('<div class="calc-button">');
document.write('<input type="button" value="計算" size="12" onclick="open_result();" />');
document.write('</div>');
document.write('</form>');
init_birthday();

