﻿/* commons.js create by nishiyama 2009/05/07 */
/* switch (hide|show) OBJ */
function switchHideOBJ(id) {
  var disp = jQuery("#" + id).css("display");
  if (disp == "none") {
    jQuery("#" + id).css("display","block");
  } else {
    jQuery("#" + id).css("display","none");
  }
}
/**
* 指定されたクラスの表示／非表示を切り替える
* @param className 切り替え対象のクラス名
*/
function switchHideOBJClass(className) {
  var disp = jQuery("." + className).css("display");
  if (disp == "none") {
    jQuery("." + className).css("display","block");
  } else {
    jQuery("." + className).css("display","none");
  }
}
/** 
* 指定されたクラスの表示／非表示を切り替える(Table(tr)表示切替用)
* @param className 切り替え対象のクラス名
*/
function switchHideOBJClassTable(className) {
  var disp = jQuery("." + className).css("display");
  if (disp == "none") {
    jQuery("." + className).css("display", "");
  } else {
    jQuery("." + className).css("display", "none");
  }
}
/**
* 親の子IDがhidechildで始まる物ですべてのdisplayで
* targetidの表示・非表示をする
* @param preid 親のID
* @param targetid  switch先のID
*/
function switchHideOBJToTarget(preid, targetid) {
  if (jQuery("#" + preid).children("div[class*='JSHIDE']:visible").length > 0) {
    jQuery("#" + targetid).css("display","block");
  } else {
    jQuery("#" + targetid).css("display","none");
  }
}
/* お気に入り登録ポップアップ */
function favorite_popup(el) {
  var style = " width=500" +
  ",height=400" +
  ",resizable=yes" +
  ",toolbar=no" +
  ",scrollbars=yes" +
  ",directories=no" +
  ",status=no" +
  ",location=yes" +
  ",menubar=no";
  popup(el, style);
}
/* プラン詳細ポップアップ */
function plandetail_popup(el) {
  var style = " width=800" +
  ",height=650" +
  ",resizable=yes" +
  ",toolbar=no" +
  ",scrollbars=yes" +
  ",directories=no" +
  ",status=no" +
  ",location=yes" +
  ",menubar=no";
  popup(el, style);
}
/* 食事詳細ポップアップ */
function meal_popup(el) {
  var style = " width=700" +
  ",height=600" +
  ",resizable=yes" +
  ",toolbar=no" +
  ",scrollbars=yes" +
  ",directories=no" +
  ",status=no" +
  ",location=yes" +
  ",menubar=no";
  popup(el, style);
}
/* map popup */
function ymap_popup(el) {
  var style = " width=650" +
  ",height=800" +
  ",resizable=yes" +
  ",toolbar=no" +
  ",scrollbars=yes" +
  ",directories=no" +
  ",status=no" +
  ",location=yes" +
  ",menubar=no";
  popup(el, style);
}
/* popup */
function popup(el, style) {
  var href = jQuery(el).children().attr("href");
  var name = jQuery(el).children().attr("target");
  jQuery(el).children().attr("href", "javascript:void(0);");
  var w1 = window.open(href, name, style);
  w1.focus();
  self.status = "";
}
/* 
* 親配下のinputの値を合計してtargetに合計値を入れる
* @param preid 親divのID
* @param targetid 表示用(divかspan)に合計値を入れる
*/
function child_sum(preid, targetid) {
  var s = 0;
  for (i = 0; i < jQuery("#"+ preid).children(":input").length; i++ ) {
    s = s + parseInt(jQuery("#"+ preid).children(":input").eq(i).val(), 10);
  }
  jQuery("#"+ targetid).text(s);
}
/* 
* 宿泊日数算出
* @param targetid 表示用(divかspan)に合計値を入れる
* @param fromyearclass 開始年
* @param frommonthclass 開始月
* @param fromdayclass 開始日
* @param toyearclass 終了年
* @param tomonthclass 終了月
* @param todayclass 終了日
* @param chmod モード 値があればfromとして動作
*/
function lodging_sum(targetid, fromyearclass, frommonthclass, fromdayclass, toyearclass, tomonthclass, todayclass, chmod) {
 var fy = jQuery("."+ fromyearclass + " option:selected").val();
 var fm = jQuery("."+ frommonthclass + " option:selected").val();
 var fd = jQuery("."+ fromdayclass + " option:selected").val();
 var ty = jQuery("."+ toyearclass + " option:selected").val();
 var tm = jQuery("."+ tomonthclass + " option:selected").val();
 var td = jQuery("."+ todayclass + " option:selected").val();
 if (chmod.length != 0) {
  var nd = parseInt(fd, 10) + 1;
  if (nd < 10) { nd = "0" + nd; }
  var ndate = new jQuery.exDate(fy + "/" + fm + "/" + nd);
  var fDate = jQuery.exDate(fy + "/" + fm + "/" + fd);
  var tDate = jQuery.exDate(ty + "/" + tm + "/" + td);
  var ll = tDate.getTime() - fDate.getTime();
  ll = ll / 1000 / 60 / 60 / 24;
  if (ll > 0) {
   jQuery("#"+ targetid).text(ll);
  } else {
   jQuery("."+ toyearclass).val(ndate.toChar("yyyy"));
   jQuery("."+ tomonthclass).val(ndate.toChar("mm"));
   jQuery("."+ todayclass).val(ndate.toChar("dd"));
   jQuery("#"+ targetid).text(1);
  }
 } else {
  fDate = jQuery.exDate(fy + "/" + fm + "/" + fd);
  tDate = jQuery.exDate(ty + "/" + tm + "/" + td);
  ll = tDate.getTime() - fDate.getTime();
  ll = ll / 1000 / 60 / 60 / 24;
  if (ll > 0) {
   jQuery("#"+ targetid).text(ll);
  } else {
   jQuery("#"+ targetid).text(0);
  }
 }
}
/* 
* 宿泊日数算出
* @param targetid 表示用(divかspan)に合計値を入れる
* @param fromyearclass 開始年
* @param frommonthclass 開始月
* @param fromdayclass 開始日
* @param toyearclass 終了年
* @param tomonthclass 終了月
* @param todayclass 終了日
* @param chmod モード 値があればfromとして動作
*/
function lodging_sum_setclass(targetclass, fromyearclass, frommonthclass, fromdayclass, toyearclass, tomonthclass, todayclass, chmod) {
  var fy = jQuery("." + fromyearclass + " option:selected").val();
  var fm = jQuery("." + frommonthclass + " option:selected").val();
  var fd = jQuery("." + fromdayclass + " option:selected").val();
  var ty = jQuery("." + toyearclass + " option:selected").val();
  var tm = jQuery("." + tomonthclass + " option:selected").val();
  var td = jQuery("." + todayclass + " option:selected").val();
  // fromを変更した場合
  if (chmod.length != 0) {
    var nd = parseInt(fd, 10) + 1;
    if (nd < 10) { nd = "0" + nd; }
    var ndate = new jQuery.exDate(fy + "/" + fm + "/" + nd);
      jQuery("." + toyearclass).val(ndate.toChar("yyyy"));
      jQuery("." + tomonthclass).val(ndate.toChar("mm"));
      jQuery("." + todayclass).val(ndate.toChar("dd"));
      jQuery("." + targetclass).text(1);
  } else {
    fDate = jQuery.exDate(fy + "/" + fm + "/" + fd);
    tDate = jQuery.exDate(ty + "/" + tm + "/" + td);
    ll = tDate.getTime() - fDate.getTime();
    ll = ll / 1000 / 60 / 60 / 24;
    if (ll > 0) {
      jQuery("." + targetclass).text(ll);
    } else {
      jQuery("." + targetclass).text(0);
    }
  }
}
/* 
* 指定されたコンポーネントの日付が、日付として正しいかどうかを判定し、
* ありえない日付の場合の指定年月の最終日をセットしなおす。
* @param fromyearclass 開始年
* @param frommonthclass 開始月
* @param fromdayclass 開始日
* @param toyearclass 終了年
* @param tomonthclass 終了月
* @param todayclass 終了日
*/
function fixDateValue(fromyearclass, frommonthclass, fromdayclass, toyearclass, tomonthclass, todayclass, chmod) {
  var fy = jQuery("." + fromyearclass + " option:selected").val();
  var fm = jQuery("." + frommonthclass + " option:selected").val();
  var fd = jQuery("." + fromdayclass + " option:selected").val();
  var ty = jQuery("." + toyearclass + " option:selected").val();
  var tm = jQuery("." + tomonthclass + " option:selected").val();
  var td = jQuery("." + todayclass + " option:selected").val();
  if (chmod == 'from'){
    if (isDate(fy,fm,fd) != true){
      /* 指定年月＋１ヶ月の１日から１日を引き、指定年月の最終日を求める */
      var wDate = new jQuery.exDate(fy + "/" + fm + "/" + "01");
      var wYear = parseFloat(fy);
      var wCurDate = new Date(wYear, wDate.getMonth() + 1, wDate.getDate() - 1);
      var dateFormat = new DateFormat("yyyy/MM/dd"); 
      var strDate = dateFormat.format(wCurDate); 
      wDate = new jQuery.exDate(strDate);
      jQuery("." + fromyearclass).val(wDate.toChar("yyyy"));
      jQuery("." + frommonthclass).val(wDate.toChar("mm"));
      jQuery("." + fromdayclass).val(wDate.toChar("dd"));
    }
  } else {
    if (isDate(ty,tm,td) != true){
      /* 指定年月＋１ヶ月の１日から１日を引き、指定年月の最終日を求める */
      var wDate = new jQuery.exDate(ty + "/" + tm + "/" + "01");
      var wYear = parseFloat(ty);
      var wCurDate = new Date(wYear, wDate.getMonth() + 1, wDate.getDate() - 1);
      var dateFormat = new DateFormat("yyyy/MM/dd"); 
      var strDate = dateFormat.format(wCurDate); 
      wDate = new jQuery.exDate(strDate);
      jQuery("." + toyearclass).val(wDate.toChar("yyyy"));
      jQuery("." + tomonthclass).val(wDate.toChar("mm"));
      jQuery("." + todayclass).val(wDate.toChar("dd"));
    }
  }
}
/* 
* 指定されたコンポーネントの日付が、日付として正しいかどうかを判定し、
* ありえない日付の場合の指定年月の最終日をセットしなおす。
* @param yearclass 年
* @param monthclass 月
* @param dayclass 日
*/
function fixDateValueForSingle(yearclass, monthclass, dayclass) {
  var y = jQuery("." + yearclass + " option:selected").val();
  var m = jQuery("." + monthclass + " option:selected").val();
  var d = jQuery("." + dayclass + " option:selected").val();
  if (isDate(y, m, d) != true) {
    /* 指定年月＋１ヶ月の１日から１日を引き、指定年月の最終日を求める */
    var wDate = new jQuery.exDate(y + "/" + m + "/" + "01");
    var wYear = parseFloat(y);
    var wCurDate = new Date(wYear, wDate.getMonth() + 1, wDate.getDate() - 1);
    var dateFormat = new DateFormat("yyyy/MM/dd");
    var strDate = dateFormat.format(wCurDate);
    wDate = new jQuery.exDate(strDate);
    jQuery("." + yearclass).val(wDate.toChar("yyyy"));
    jQuery("." + monthclass).val(wDate.toChar("mm"));
    jQuery("." + dayclass).val(wDate.toChar("dd"));
  }
}
/* 指定された年月日が日付として正しいかを判定する */
function isDate(iyear, imonth, iday){
  var years = parseFloat(iyear);
  var months = parseFloat(imonth);
  var days = parseFloat(iday);
  var flag = true;
  years = parseInt(years, 10);
  months = parseInt(months, 10) - 1;
  days = parseInt(days, 10);
  if (years < 1900) {
    window.alert("年の値が小さすぎます");
    return false;
  }
  var dates = new Date(years,months,days);
  if (dates.getYear() < 1900) {
    if (years != dates.getYear() + 1900) { flag = false; }
  } else {
    if (years != dates.getYear()) { flag = false; }
  }
  if (months != dates.getMonth()) { flag = false; }
  if (days != dates.getDate()) { flag = false; }
  return flag;
}
/* 日付フォーマット取得関数 
 var fmt = new DateFormat("yyyy/MM/dd HH:mm:ss SSS");
 var str = fmt.format(new Date()); // "2007/05/10 12:21:19 002"
 var date = fmt.parse("2007/05/10 12:21:19 002"); // return Date object
*/
var DateFormat = function(pattern) {
  this._init(pattern);
};
DateFormat.prototype = {
  _init: function(pattern) {
  this.pattern = pattern;
  this._patterns = [];
  for (var i = 0; i < pattern.length; i++) {
  var ch = pattern.charAt(i);
  if (this._patterns.length == 0) {
    this._patterns[0] = ch;
  } else {
    var index = this._patterns.length - 1;
    if (this._patterns[index].charAt(0) == "'") {
    if (this._patterns[index].length == 1 
       || this._patterns[index].charAt(this._patterns[index].length - 1) != "'") {
      this._patterns[index] += ch;
    } else {
      this._patterns[index + 1] = ch;
    }
    } else if (this._patterns[index].charAt(0) == ch) {
    this._patterns[index] += ch;
    } else {
    this._patterns[index + 1] = ch;
    }
  }
  }
  },
  format: function(date) {
  var result = [];
  for (var i = 0; i < this._patterns.length; i++) {
  result[i] = this._formatWord(date, this._patterns[i]);
  }
  return result.join('');
  },
  _formatWord: function(date, pattern) {
  var formatter = this._formatter[pattern.charAt(0)];
  if (formatter) {
  return formatter.apply(this, [date, pattern]);
  } else {
  return pattern;
  }
  },
  _formatter: {
  "y": function(date, pattern) {
  // Year
  var year = String(date.getFullYear());
  if (pattern.length <= 2) {
    year = year.substring(2, 4);
  } else {
    year = this._zeroPadding(year, pattern.length);
  }
  return year;
  },
  "M": function(date, pattern) {
  // Month in year
  return this._zeroPadding(String(date.getMonth() + 1), pattern.length);
  },
  "d": function(date, pattern) {
  // Day in month
  return this._zeroPadding(String(date.getDate()), pattern.length);
  },
  "H": function(date, pattern) {
  // Hour in day (0-23)
  return this._zeroPadding(String(date.getHours()), pattern.length);
  },
  "m": function(date, pattern) {
  // Minute in hour
  return this._zeroPadding(String(date.getMinutes()), pattern.length);
  },
  "s": function(date, pattern) {
  // Second in minute
  return this._zeroPadding(String(date.getSeconds()), pattern.length);
  },
  "S": function(date, pattern) {
  // Millisecond
  return this._zeroPadding(String(date.getMilliseconds()), pattern.length);
  },
  "'": function(date, pattern) {
  // escape
  if (pattern == "''") {
    return "'";
  } else {
    return pattern.replace(/'/g, '');
  }
  }
  },
  _zeroPadding: function(str, length) {
  if (str.length >= length) {
  return str;
  }
  return new Array(length - str.length + 1).join("0") + str;
  },
  /// Parser ///
  parse: function(text) {
  if (typeof text != 'string' || text == '') return null;
  var result = {year: 1970, month: 1, day: 1, hour: 0, min: 0, sec: 0, msec: 0};
  for (var i = 0; i < this._patterns.length; i++) {
   if (text == '') return null; // parse error!!
   text = this._parseWord(text, this._patterns[i], result);
   if (text === null) return null; // parse error!!
  }
  if (text != '') return null; // parse error!!
  return new Date(result.year, result.month - 1, result.day, result.hour, result.min, result.sec, result.msec);
  },
  _parseWord: function(text, pattern, result) {
  var parser = this._parser[pattern.charAt(0)];
  if (parser) {
  return parser.apply(this, [text, pattern, result]);
  } else {
  if (text.indexOf(pattern) != 0) {
    return null;
  } else {
    return text.substring(pattern.length);
  }
  }
  },
  _parser: {
  "y": function(text, pattern, result) {
  // Year
  var year;
  if (pattern.length <= 2) {
    year = text.substring(0, 2);
    year = year < 70 ? '20' + year : '19' + year;
    text = text.substring(2);
  } else {
    var length = (pattern.length == 3) ? 4 : pattern.length;
    year = text.substring(0, length);
    text = text.substring(length);
  }
  if (!this._isNumber(year)) return null; // error
  result.year = parseInt(year, 10);
  return text;
  },
  "M": function(text, pattern, result) {
  // Month in year
  var month;
  if (pattern.length == 1 && text.length > 1
    && text.substring(0, 2).match(/1[0-2]/) != null) {
    month = text.substring(0, 2);
    text  = text.substring(2);
  } else {
    month = text.substring(0, pattern.length);
    text  = text.substring(pattern.length);
  }
  if (!this._isNumber(month)) return null; // error
  result.month = parseInt(month, 10);
  return text;
  },
  "d": function(text, pattern, result) {
  // Day in month
  var day;
  if (pattern.length == 1 && text.length > 1 
    && text.substring(0, 2).match(/1[0-9]|2[0-9]|3[0-1]/) != null) {
    day  = text.substring(0, 2);
    text = text.substring(2);
  } else {
    day  = text.substring(0, pattern.length);
    text = text.substring(pattern.length);
  }
  if (!this._isNumber(day)) return null; // error
  result.day = parseInt(day, 10);
  return text;
  },
  "H": function(text, pattern, result) {
  // Hour in day (0-23)
  var hour;
  if (pattern.length == 1 && text.length > 1
    && text.substring(0, 2).match(/1[0-9]|2[0-3]/) != null) {
    hour = text.substring(0, 2);
    text = text.substring(2);
  } else {
    hour = text.substring(0, pattern.length);
    text = text.substring(pattern.length);
  }
  if (!this._isNumber(hour)) return null; // error
  result.hour = parseInt(hour, 10);
  return text;
  },
  "m": function(text, pattern, result) {
  // Minute in hour
  var min;
  if (pattern.length == 1 && text.length > 1
    && text.substring(0, 2).match(/[1-5][0-9]/) != null) {
    min  = text.substring(0, 2);
    text = text.substring(2);
  } else {
    min  = text.substring(0, pattern.length);
    text = text.substring(pattern.length);
  }
  if (!this._isNumber(min)) return null; // error
  result.min = parseInt(min, 10);
  return text;
  },
  "s": function(text, pattern, result) {
  // Second in minute
  var sec;
  if (pattern.length == 1 && text.length > 1
    && text.substring(0, 2).match(/[1-5][0-9]/) != null) {
    sec  = text.substring(0, 2);
    text = text.substring(2);
  } else {
    sec  = text.substring(0, pattern.length);
    text = text.substring(pattern.length);
  }
  if (!this._isNumber(sec)) return null; // error
  result.sec = parseInt(sec, 10);
  return text;
  },
  "S": function(text, pattern, result) {
  // Millimsecond
  var msec;
  if (pattern.length == 1 || pattern.length == 2) {
    if (text.length > 2 && text.substring(0, 3).match(/[1-9][0-9][0-9]/) != null) {
    msec = text.substring(0, 3);
    text = text.substring(3);
    } else if (text.length > 1 && text.substring(0, 2).match(/[1-9][0-9]/) != null) {
    msec = text.substring(0, 2);
    text = text.substring(2);
    } else {
    msec = text.substring(0, pattern.length);
    text = text.substring(pattern.length);
    }
  } else {
    msec = text.substring(0, pattern.length);
    text = text.substring(pattern.length);
  }
  if (!this._isNumber(msec)) return null; // error
  result.msec = parseInt(msec, 10);
  return text;
  },
  "'": function(text, pattern, result) {
  // escape
  if (pattern == "''") {
    pattern = "'";
  } else {
    pattern = pattern.replace(/'/g, '');
  }
  if (text.indexOf(pattern) != 0) {
    return null; // error
  } else {
    return text.substring(pattern.length);
  }
  }
  },
  _isNumber: function(str) {
  return /^[0-9]*$/.test(str);
  }
}


/* サムネイル表示を行う。 */
var fiSpeed  = 1000;        // フェードイン   時間
var fuSpeed  = 500;         // フェードアウト 時間
function SwitchImageLikeTheThumbnail( id_big_image_block , id_clicked_small_image )
{
    var bigSrc = jQuery("#" + id_big_image_block).attr("src");
    var smallSrc = jQuery("#" + id_clicked_small_image).attr("src");
    if ( bigSrc == smallSrc )
    {
        return;
    }
    smallSrc = jQuery("#" + id_clicked_small_image).attr("src");
    jQuery("#" + id_big_image_block).fadeOut
    (
        fuSpeed,
        function()
        {
            jQuery("#" + id_big_image_block).attr("src", smallSrc);
            if (navigator.userAgent.indexOf("MSIE",0) == -1)
            {
                jQuery("#" + id_big_image_block).load(function()
                {
                    jQuery("#" + id_big_image_block).fadeIn(fiSpeed);                
                });
            }
            else
            {
                jQuery("#" + id_big_image_block).fadeIn(fiSpeed);
            }
        }
    );
}

/**
* テキストボックスからValue値を消去する
* @param e テキストボックスオブジェクト
*/
function clearDefault(e) {
    var oText = e;
    if (!oText) return;
    if (!oText.defText) oText.defText = oText.value;
    if (oText.value == oText.defText) oText.value = '';
}
/**
* テキストボックスからValue値を消去する
* @param e テキストボックスオブジェクト
*/
function showDefault(e) {
    var oText = e;
    if (!oText) return;
    if (oText.value == '' && oText.defText) oText.value = oText.defText;
}

/* ASP.netのフォームをSubmitする処理 */
function submitAspnetForm(url){
	window.document.aspnetForm.method = "POST";
	window.document.aspnetForm.action = url;
	window.document.aspnetForm.submit();
	return false;
}

