// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/**
 * 検索条件入力チェック
 */
function check_conditions() {
    if (check_institution_selected() == false)
        return false;
    
    if (check_date() == false)
        return false;
        
    if (check_time() == false)
        return false;
    
    return true;
}

/**
 * 施設選択チェック
 */
function check_institution_selected() {
    var frm = document.forms[0];
    
    if (frm.elements["institution_id[start]"].selectedIndex <= 0) {
        alert("出発地の施設・停留所を選択してください。");
       return false;
    }
    
    if (frm.elements["institution_id[arrival]"].selectedIndex <= 0) {
        alert("目的地の施設・停留所を選択してください。");
       return false;
    }
    
    return true;
}

/**
 * 日付正当チェック
 */
function check_date()
{
  var frm = document.forms[0];
  var year = frm.elements["date[year]"].value;
  var month = frm.elements["date[month]"].value;
  var day = frm.elements["date[day]"].value;
  
  if (day < 1 || day > get_month_day(year, month)) {
    alert("正確な日付を入力してください。");
    return false;
  }
  
  return true;
}


/**
 * 月別日数取得関数
 * 
 * 指定年月の日数を返す。
 * 
 * @param int year 年
 * @param int month 月
 * @return int 日数
 */
function get_month_day(year, month)
{
  days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  if (month == 2 && check_uruu(year)) return 29;
  return days[month - 1];
}


/**
 * うるう年チェック関数
 * 
 * 指定年がうるう年かどうかチェックし、結果を返す。
 * 
 * @param int year 年
 * @return bool true:うるう年
 */
function check_uruu(year)
{
  return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}

/**
 * 時刻入力チェック
 */
function check_time()
{
    var frm = document.forms[0];
    var min = frm.elements["time[min]"].value;

    if (min == "") {
        alert("時刻(分)を入力してください。");
        return false;
    }
    
    if (isNaN(parseInt(min))) {
        alert("時刻(分)は整数値で入力してください。");
        return false;
    }
    
    if (min < 0 || min > 59) {
        alert("正確な時刻(分)を入力してください。");
        return false;
    }
    return true;
}

/**
 * エリア変更(施設一覧)
 */
function changeArea(area_id)
{
  new Ajax.Updater('institutions', '../sightseeing/institutions?area_id=' + area_id);
}

/**
 * 施設情報→検索画面
 */
function jumpSearchRoute(institution_id)
{
  window.opener.location.href = "../sightseeing/jump_search_route?institution_id=" + institution_id;
  window.close();
}

/**
 * お問合せフォーム入力チェック
 */
function check_inquiry()
{
  var frm = document.forms[0];
  var name = frm.elements["name"].value;
  var mail = frm.elements["mail"].value;
  var body = frm.elements["body"].value;
  
  if (name == ""){
    alert("お名前を入力してください。");
    return false;
  }
  
  if (mail == "") {
    alert("メールアドレスを入力してください。");
    return false;
  } else if(mail.match("[0-9a-zA-Z\.\-\_]+@[0-9a-zA-Z\.\-\_]+\.[0-9a-zA-Z\.\-\_]+") == null){
    alert("正しいメールアドレスを入力してください。");
    return false;
  }
  
  if (body == "") {
    alert("お問合せ内容を入力してください。");
    return false;
  }
  
  return true;
}


