var yaziDocRoot = '/';
var yaziAjaxRoot = yaziDocRoot + 'ajax/';
var yaziImgRoot = yaziDocRoot + 'img/';

var sboxId = 'SearchBoxWide';
var sboxProcessing = false;
var sboxInit = true;

document.observe('dom:loaded', function(){
    if (sboxInit) { Sbox_Initialize(); }
});

function Sbox_Initialize()
{
  var travelType = Sbox_GetTravelType();
  if (travelType == 'RT') {
    Sbox_Initialize_Rt();
  } else if (travelType == 'OW') {
    Sbox_Initialize_Ow();
  } else if (travelType == 'RTEX') {
    Sbox_Initialize_Rtex();
  }
  if ($('airline')) {
    if ($F('airline') != '') {
        Sbox_OpenOption();
    }
  }
  //Sbox_OnChangePerson();
}
function Sbox_Initialize_Rt(){}
function Sbox_Initialize_Ow(){}
function Sbox_Initialize_Rtex()
{
  if ($('scheduleNum').value == 0) {
    $('addSchedule').show();
    $('delSchedule').hide();
  } else if ($('scheduleNum').value == 3) {
    $('y3').onchange();
    $('addSchedule').hide();
    $('delSchedule').show();
  } else {
    $('y' + $('scheduleNum').value).onchange();
    $('addSchedule').show();
    $('delSchedule').show();
  }
}
function Sbox_ClickInput(label){
  var e = null;
  try{
    e = document.getElementById(label.htmlFor);
  }
  catch(exception){}
  if(e == null){
    var node;
    for(var i=0; i<label.childNodes.length; i++){
      node = label.childNodes.item(i);
      if(node.nodeName == 'INPUT'){
        e = node;
        break;
      }
    }
  }
  if(e != null){
    switch(e.type){
      case 'checkbox':
        e.checked =! e.checked;
        e.onclick();
        return false;
      case 'radio':
        e.checked = true;
        e.onclick();
        return false;
      case 'text':
      case 'password':
      case 'textarea':
        e.focus();
        return false;
    }
  }
}
function Sbox_ValidateRt(elements)
{
  errorMsg = '';
  for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    switch (element.name) {
      case 'deps':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '出発地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        $('depe').value = $('arrs').value;
        break;
      case 'arrs':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '目的地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        $('arre').value = $('deps').value;
        break;
      case 'ys':
        var di = new Date();
        if (!Sbox_IsValidDate($('ys').value, $('ms').value, $('ds').value)) {
          $('ys').style.backgroundColor = '#ffc';
          $('ms').style.backgroundColor = '#ffc';
          $('ds').style.backgroundColor = '#ffc';
          errorMsg += '出発日を入力してください。'+"\n";
        } else if (Sbox_DiffDate($('ys').value, $('ms').value, $('ds').value, di.getFullYear(), di.getMonth()+1, di.getDate()) == 1) {
          $('ys').style.backgroundColor = '#ffc';
          $('ms').style.backgroundColor = '#ffc';
          $('ds').style.backgroundColor = '#ffc';
          errorMsg += '出発日は本日以降に設定してください。'+"\n";
        } else {
          $('ys').style.backgroundColor = '#fff';
          $('ms').style.backgroundColor = '#fff';
          $('ds').style.backgroundColor = '#fff';
        }
        break;
      case 'ye':
        if (!Sbox_IsValidDate($('ye').value, $('me').value, $('de').value)) {
          $('ye').style.backgroundColor = '#ffc';
          $('me').style.backgroundColor = '#ffc';
          $('de').style.backgroundColor = '#ffc';
          errorMsg += '現地出発日を入力してください。'+"\n";
        } else {
          var idx = 's';
          if ($('y3') != null || $('y3') != undefined) {
            idx = '3';
          } else if ($('y2') != null || $('y2') != undefined) {
            idx = '2';
          } else if ($('y1') != null || $('y1') != undefined) {
            idx = '1';
          } else if ($('ys') != null || $('ys') != undefined) {
            idx = 's';
          }
          if (Sbox_DiffDate($('ye').value, $('me').value, $('de').value, $('y'+idx).value, $('m'+idx).value, $('d'+idx).value) == 1) {
            $('ye').style.backgroundColor = '#ffc';
            $('me').style.backgroundColor = '#ffc';
            $('de').style.backgroundColor = '#ffc';
            errorMsg += '現地出発日は出発日以降に設定してください。'+"\n";
          } else {
            $('ye').style.backgroundColor = '#fff';
            $('me').style.backgroundColor = '#fff';
            $('de').style.backgroundColor = '#fff';
          }
        }
        break;
      case 'adultNum':
        var adultNum = parseInt($('adultNum').value);
        var childNum = parseInt($('childNum').value);
        var infantNum = parseInt($('infantNum').value);
        if (adultNum + 1 < (childNum + infantNum)) {
          $('adultNum').style.backgroundColor = '#ffc';
          $('childNum').style.backgroundColor = '#ffc';
          $('infantNum').style.backgroundColor = '#ffc';
          errorMsg += 'お子様と幼児の合計人数が大人の人数を超える場合は、別途お問合せ下さい。'+"\n";
        } else if ((adultNum + childNum + infantNum) > 9) {
          $('adultNum').style.backgroundColor = '#ffc';
          $('childNum').style.backgroundColor = '#ffc';
          $('infantNum').style.backgroundColor = '#ffc';
          errorMsg += '参加人数が10名様以上の場合は、別途お問合せ下さい。'+"\n";
        } else {
          $('adultNum').style.backgroundColor = '#fff';
          $('childNum').style.backgroundColor = '#fff';
          $('infantNum').style.backgroundColor = '#fff';
        }
        break;
    }
  }
  return errorMsg;
}
function Sbox_ValidateOw(elements)
{
  errorMsg = '';
  return errorMsg;
}
function Sbox_ValidateRtex(elements)
{
  errorMsg = '';
  for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    switch (element.name) {
      case 'deps':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '行きの出発地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        break;
      case 'dep1':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '移動区間1の出発地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        break;
      case 'dep2':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '移動区間2の出発地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        break;
      case 'dep3':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '移動区間3の出発地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        break;
      case 'depe':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '帰りの出発地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        break;
      case 'arrs':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '行きの目的地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        break;
      case 'arr1':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '移動区間1の目的地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        break;
      case 'arr2':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '移動区間2の目的地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        break;
      case 'arr3':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '移動区間3の目的地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        break;
      case 'arre':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '帰りの目的地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        break;
      case 'ys':
        var di = new Date();
        if (!Sbox_IsValidDate($('ys').value, $('ms').value, $('ds').value)) {
          $('ys').style.backgroundColor = '#ffc';
          $('ms').style.backgroundColor = '#ffc';
          $('ds').style.backgroundColor = '#ffc';
          errorMsg += '行きの現地出発日を入力してください。'+"\n";
        } else if (Sbox_DiffDate($('ys').value, $('ms').value, $('ds').value, di.getFullYear(), di.getMonth()+1, di.getDate()) == 1) {
          $('ys').style.backgroundColor = '#ffc';
          $('ms').style.backgroundColor = '#ffc';
          $('ds').style.backgroundColor = '#ffc';
          errorMsg += '行きの現地出発日は本日以降に設定してください。'+"\n";
        } else {
          $('ys').style.backgroundColor = '#fff';
          $('ms').style.backgroundColor = '#fff';
          $('ds').style.backgroundColor = '#fff';
        }
        break;
      case 'y1':
        if (!Sbox_IsValidDate($('y1').value, $('m1').value, $('d1').value)) {
          $('y1').style.backgroundColor = '#ffc';
          $('m1').style.backgroundColor = '#ffc';
          $('d1').style.backgroundColor = '#ffc';
          errorMsg += '移動区間1の現地出発日を入力してください。'+"\n";
        } else if (Sbox_DiffDate($('y1').value, $('m1').value, $('d1').value, $('ys').value, $('ms').value, $('ds').value) == 1) {
          $('y1').style.backgroundColor = '#ffc';
          $('m1').style.backgroundColor = '#ffc';
          $('d1').style.backgroundColor = '#ffc';
          errorMsg += '移動区間1の現地出発日は前区間の出発日以降に設定してください。'+"\n";
        } else {
          $('y1').style.backgroundColor = '#fff';
          $('m1').style.backgroundColor = '#fff';
          $('d1').style.backgroundColor = '#fff';
        }
        break;
      case 'y2':
        if (!Sbox_IsValidDate($('y2').value, $('m2').value, $('d2').value)) {
          $('y2').style.backgroundColor = '#ffc';
          $('m2').style.backgroundColor = '#ffc';
          $('d2').style.backgroundColor = '#ffc';
          errorMsg += '移動区間2の現地出発日を入力してください。'+"\n";
        } else if (Sbox_DiffDate($('y2').value, $('m2').value, $('d2').value, $('y1').value, $('m1').value, $('d1').value) == 1) {
          $('y2').style.backgroundColor = '#ffc';
          $('m2').style.backgroundColor = '#ffc';
          $('d2').style.backgroundColor = '#ffc';
          errorMsg += '移動区間2の現地出発日は前区間の出発日以降に設定してください。'+"\n";
        } else {
          $('y2').style.backgroundColor = '#fff';
          $('m2').style.backgroundColor = '#fff';
          $('d2').style.backgroundColor = '#fff';
        }
        break;
      case 'y3':
        if (!Sbox_IsValidDate($('y3').value, $('m3').value, $('d3').value)) {
          $('y3').style.backgroundColor = '#ffc';
          $('m3').style.backgroundColor = '#ffc';
          $('d3').style.backgroundColor = '#ffc';
          element.style.backgroundColor = '#ffc';
          errorMsg += '移動区間3の現地出発日を入力してください。'+"\n";
        } else if (Sbox_DiffDate($('y3').value, $('m3').value, $('d3').value, $('y2').value, $('m2').value, $('d2').value) == 1) {
          $('y3').style.backgroundColor = '#ffc';
          $('m3').style.backgroundColor = '#ffc';
          $('d3').style.backgroundColor = '#ffc';
          errorMsg += '移動区間3の現地出発日は前区間の出発日以降に設定してください。'+"\n";
        } else {
          $('y3').style.backgroundColor = '#fff';
          $('m3').style.backgroundColor = '#fff';
          $('d3').style.backgroundColor = '#fff';
        }
        break;
      case 'ye':
        if (!Sbox_IsValidDate($('ye').value, $('me').value, $('de').value)) {
          $('ye').style.backgroundColor = '#ffc';
          $('me').style.backgroundColor = '#ffc';
          $('de').style.backgroundColor = '#ffc';
          errorMsg += '帰りの現地出発日を入力してください。'+"\n";
        } else {
          var idx = 's';
          if ($('y3') != null || $('y3') != undefined) {
            idx = '3';
          } else if ($('y2') != null || $('y2') != undefined) {
            idx = '2';
          } else if ($('y1') != null || $('y1') != undefined) {
            idx = '1';
          } else if ($('ys') != null || $('ys') != undefined) {
            idx = 's';
          }
          if (Sbox_DiffDate($('ye').value, $('me').value, $('de').value, $('y'+idx).value, $('m'+idx).value, $('d'+idx).value) == 1) {
            $('ye').style.backgroundColor = '#ffc';
            $('me').style.backgroundColor = '#ffc';
            $('de').style.backgroundColor = '#ffc';
            errorMsg += '帰りの現地出発日は前区間の出発日以降に設定してください。'+"\n";
          } else {
            $('ye').style.backgroundColor = '#fff';
            $('me').style.backgroundColor = '#fff';
            $('de').style.backgroundColor = '#fff';
          }
        }
        break;
      case 'adultNum':
        var adultNum = parseInt($('adultNum').value);
        var childNum = parseInt($('childNum').value);
        var infantNum = parseInt($('infantNum').value);
        if (adultNum + 1 < (childNum + infantNum)) {
          $('adultNum').style.backgroundColor = '#ffc';
          $('childNum').style.backgroundColor = '#ffc';
          $('infantNum').style.backgroundColor = '#ffc';
          errorMsg += 'お子様と幼児の合計人数が大人の人数を超える場合は、別途お問合せ下さい。'+"\n";
        } else if ((adultNum + childNum + infantNum) > 9) {
          $('adultNum').style.backgroundColor = '#ffc';
          $('childNum').style.backgroundColor = '#ffc';
          $('infantNum').style.backgroundColor = '#ffc';
          errorMsg += '参加人数が10名様以上の場合は、別途お問合せ下さい。'+"\n";
        } else {
          $('adultNum').style.backgroundColor = '#fff';
          $('childNum').style.backgroundColor = '#fff';
          $('infantNum').style.backgroundColor = '#fff';
        }
        break;
    }
  }
  return errorMsg;
}
function Sbox_Validation()
{
  var travelType = Sbox_GetTravelType();
  var errorMsg = '';
  var allElements = Form.getElements(sboxId);
  switch (travelType) {
    case 'RT':
      errorMsg = Sbox_ValidateRt(allElements);
      break;
    case 'OW':
      errorMsg = Sbox_ValidateOw(allElements);
      break;
    case 'RTEX':
      errorMsg = Sbox_ValidateRtex(allElements);
      break;
  }
  if (errorMsg != '') {
    alert(errorMsg);
    return false;
  }
  var locationForm = document.createElement('form');
  locationForm.id = 'locationForm';
  locationForm.action = $(sboxId).action + '?' + $(sboxId).serialize();
  locationForm.method = 'get';
  $$('body')[0].appendChild(locationForm);
  locationForm.submit();
}
function Sbox_IsValidDate(y,m,d){
  var di = new Date(y,m-1,d);
  if(di.getFullYear() == y && di.getMonth() == m-1 && di.getDate() == d){
    return true;
  }
  return false;
}
function Sbox_DiffDate(y1,m1,d1,y2,m2,d2) {
  var ms1 = Date.parse(y1 + '/' + m1 + '/' + d1);
  var ms2 = Date.parse(y2 + '/' + m2 + '/' + d2);
  if (ms1 == ms2) {
    return 0;
  } else if (ms1 < ms2) {
    return 1;
  } else if (ms1 > ms2) {
    return 2;
  } else {
    return 3;
  }
}
function Sbox_GetTravelType()
{
  for (i = 0; i < $(sboxId).travelType.length; i++) {
    if ($(sboxId).travelType[i].checked) {
      travelType = $(sboxId).travelType[i].value;
      break;
    }
  }
  return travelType;
}
function Sbox_OpenAreaSearch(label)
{
  Sbox_PopupCenter(yaziDocRoot + 'service/areasearch/?label=' + label, 'areasearch', 680, 380);
}
function Sbox_OpenCalendar(y, m, d)
{
  Sbox_PopupCenter(yaziDocRoot + 'service/calendar/?yl=' + y + '&ml=' + m + '&dl=' + d + '&y=' + $(y).value + '&m=' + $(m).value + '&d=' + $(d).value, 'calendar', 500, 300);
}
function Sbox_PopupCenter(url, name, width, height) {
  var left = (screen.width - width) / 2;
  var top = (screen.height - height) / 2;
  var options = "left=" + left + ",top=" + top + 
    ",width=" + width + ",height=" + height + 'channelmode=no,directories=no,status=no,location=no,titlebar=no,resizable=no,scrollbars=no,toolbar=no';
  window.open(url, name, options, 0).focus();
}
function Sbox_OpenOption() {
  $('button_search1').hide();
  $('button_search2').show();
  $('search_inner_bottom').show();
}
function Sbox_CloseOption() {
  $('button_search2').hide();
  $('button_search1').show();
  $('search_inner_bottom').hide();
}
function Sbox_OnChangeScheduleNum(num)
{
  $('scheduleNum').value = num;
  Sbox_OnChangeTravelType();
  return true;
}
function Sbox_OnChangeTravelType(elm) {
  if (sboxProcessing == false) {
    sboxProcessing = true;
    var request = new Ajax.Request(
      yaziAjaxRoot + 'searchbox/', {
        asynchronous: false,
        method: 'post',
        parameters: $(sboxId).serialize(),
        onSuccess: function(request) { 
          var resultText = request.responseText;
          $('searchBoxResult').update(resultText);
          sboxProcessing = false;
        }, 
        onComplete: function(request) {sboxProcessing = false;}, 
        onFailure: function(request) {sboxProcessing = false;}, 
        onException: function (request) {sboxProcessing = false;} 
      }
    );
    var travelType = Sbox_GetTravelType();
    if (travelType == 'RT') {
      Sbox_Initialize_Rt();
    } else if (travelType == 'OW') {
      Sbox_Initialize_Ow();
    } else if (travelType == 'RTEX') {
      Sbox_Initialize_Rtex();
    }
  }
  return true;
}
function Sbox_OnFocusSuggest(elm) {
  new Ajax.Autocompleter(
    elm.name,
    'suggest_'+elm.name,
    yaziAjaxRoot + 'suggest/', {
      method: 'get',
      paramName: 'keyword',
      autoSelect: false,
      minChars: 1,
      frequency: 0.6,
      select:'inputValue',
      afterUpdateElement: Sbox_OnUpdateAutocomp
  });
  return true;
}
function Sbox_OnUpdateAutocomp(elm)
{
  if (elm.name == 'deps' || elm.name == 'dep1' || elm.name == 'dep2' || elm.name == 'dep3' || elm.name == 'depe') {
    Sbox_OnChangeDep(elm);
  } else if (elm.name == 'arrs' || elm.name == 'arr1' || elm.name == 'arr2' || elm.name == 'arr3' || elm.name == 'arre') {
    Sbox_OnChangeArr(elm);
  }
  return true;
}
function Sbox_OnChangeDep(elm) {
  if (elm.name == 'deps') {
    $('arre').value = $('deps').value;
  }
  return true;
}
function Sbox_OnChangeArr(elm) {
  if (elm.name == 'arrs') {
    if ($('dep1') == null || $('dep1') == undefined) {
      $('depe').value = $('arrs').value;
    } else {
      $('dep1').value = $('arrs').value;
    }
  } else if (elm.name == 'arr1') {
    if ($('dep2') == null || $('dep2') == undefined) {
      $('depe').value = $('arr1').value;
    } else {
      $('dep2').value = $('arr1').value;
    }
  } else if (elm.name == 'arr2') {
    if ($('dep3') == null || $('dep3') == undefined) {
      $('depe').value = $('arr2').value;
    } else {
      $('dep3').value = $('arr2').value;
    }
  } else if (elm.name == 'arr3') {
    $('depe').value = $('arr3').value;
  }
  return true;
}
function Sbox_OnChangeDate(elm) {
  return true;
}
//時間の設定がある場合はsearchTypeを固定
function Sbox_OnChangeTime(elm) {
  if ($('hs').value < 0 && $('he').value < 0) {
    for (i=0;i<elm.form.searchType.length;i++) {
      elm.form.searchType[i].disabled = false;
    }
  } else {
    for (i=0;i<elm.form.searchType.length;i++) {
      if (elm.form.searchType[i].value == 1) {
        elm.form.searchType[i].checked = true;
      } else {
        elm.form.searchType[i].disabled = true;
      }
    }
  }
  return true;
}
function Sbox_OnChangePerson(elm)
{
  var adultNum = parseInt($('adultNum').value);
  var childNum = parseInt($('childNum').value);
  var infantNum = parseInt($('infantNum').value);

  $('childNum').length = adultNum + 2;
  if (adultNum + $('childNum').length > 9) {
    $('childNum').length = 9 - adultNum + 1;
  }
  for (var i = 0; i < $('childNum').length; i++) {
    $('childNum').options[i].value = i;
    $('childNum').options[i].text = i;
  }

  $('infantNum').length = adultNum + 1;
  if (adultNum + $('infantNum').length > 9) {
    $('infantNum').length = 9 - adultNum + 1;
  }
  for (var i = 0; i < $('infantNum').length; i++) {
    $('infantNum').options[i].value = i;
    $('infantNum').options[i].text = i;
  }

  return true;
}
var squickId = 'SearchBoxQuick';
var inquiryRequests = new Array();
var inquiryUpdaters = new Array();
var inquiryCounters = new Array();

document.observe('dom:loaded', function(){Squick_Initialize();});

function Squick_Initialize()
{
  if ($(squickId) == undefined) {
    return;
  }
}
function Squick_Search()
{
  if (!Squick_Validate()) {
    return;
  }

  $('qk-tokenId').disable();

  var locationForm = document.createElement('form');
  locationForm.id = 'locationForm';
  locationForm.action = $(sboxId).action;
  locationForm.method = 'get';

  var elementsHash = $(squickId).serialize(true);
  for(var i in elementsHash){
    var key = '';
    var work = i.match(/\[.+?\]/);
    key = work[0].gsub(/[\[\]]/, '');
    var input = document.createElement('input');  
    input.type = 'hidden';  
    input.name = key;  
    input.value = elementsHash[i];  
    locationForm.appendChild(input);
  }
  $$('body')[0].appendChild(locationForm);
  locationForm.submit();
}
function Squick_UpdateIcon(icon, id)
{
  if ($(id) == undefined) {
    return;
  }
  var element = $(id);

  if (icon == 'continue') {
    element.update('<img src="' + yaziImgRoot + 'icon_searching.gif" alt="照会中" />');
  } else if (icon == 'ok1') {
    element.update('<img src="' + yaziImgRoot + 'icon_seat.gif" alt="空席有り" />');
  } else if (icon == 'ok2') {
    element.update('<img src="' + yaziImgRoot + 'icon_seat_sale.gif" alt="空席わずか" />');
  } else if (icon == 'none') {
    element.update('<img src="' + yaziImgRoot + 'icon_seat_none.gif" alt="空席無" />');
  } else if (icon == 'tmo') {
    element.update('<span>Timeout</span>');
  }
  return;
}
function Squick_Inquiry(items)
{
  if (items == undefined || items == null) {
      return;
  }
  var baseParams = '';
  var elementsHash = $(squickId).serialize(true);
  var tokenId = '';
  var obj = new Object();
  for(var i in elementsHash){
    var key = '';
    var work = i.match(/\[.+?\]/);
    key = work[0].gsub(/[\[\]]/, '');
    if (key == 'tokenId') {
      tokenId = elementsHash[i];
      continue;
    }
    obj[key] = elementsHash[i];
  }
  var hash = $H(obj);
  baseParams = hash.toQueryString();

  for(var i=0;i<items.length;i++){
    Squick_UpdateIcon('continue', 'pickup'+items[i]);
    Squick_UpdateIcon('continue', 'normal'+items[i]);

    var params = baseParams + '&tokenId=' + encodeURI(tokenId + items[i]) + '&itemId=' + items[i] + '&linesOfPlan=1';

    inquiryCounters[items[i]] = 0;

    inquiryRequests[items[i]] = new Ajax.Request(
      yaziAjaxRoot + 'plan/', {
        asynchronous: true,
        method: 'post',
        parameters: params,
        onSuccess: function(response) {
          var info = response.responseText.evalJSON();
          if (info.sessionRef == null) {
            Squick_UpdateIcon('none', 'pickup'+info.itemId);
            Squick_UpdateIcon('none', 'normal'+info.itemId);
            return;
          }
          inquiryUpdaters[info.itemId] = new Ajax.PeriodicalUpdater('dummy', yaziAjaxRoot + 'plan/status/', {
            method: 'post', frequency: 5, decay: 1.0,
            parameters: 'itemId=' + encodeURI(info.itemId) + '&sessionRef=' + encodeURI(info.sessionRef),
            onSuccess: function(response) {
              var result = response.responseText.evalJSON();
              if (result.status == 'S') {
                if (result.availSeatsAvg < 5) {
                  Squick_UpdateIcon('ok2', 'pickup'+info.itemId);
                  Squick_UpdateIcon('ok2', 'normal'+info.itemId);
                } else {
                  Squick_UpdateIcon('ok1', 'pickup'+info.itemId);
                  Squick_UpdateIcon('ok1', 'normal'+info.itemId);
                }
                inquiryUpdaters[result.itemId].stop();
                inquiryUpdaters[result.itemId] = null;
              } else if (result.status == 'E' || result.status == 'P') {
                Squick_UpdateIcon('none', 'pickup'+info.itemId);
                Squick_UpdateIcon('none', 'normal'+info.itemId);
                inquiryUpdaters[result.itemId].stop();
                inquiryUpdaters[result.itemId] = null;
              //} else if (result.status == 'C') {
              } else {
                if (inquiryCounters[result.itemId] > 5) {
                  Squick_UpdateIcon('none', 'pickup'+info.itemId);
                  Squick_UpdateIcon('none', 'normal'+info.itemId);
                  inquiryUpdaters[result.itemId].stop();
                  inquiryUpdaters[result.itemId] = null;
                  alert(result.itemId);
                } else {
                  // for IEx
                  var str = inquiryUpdaters[result.itemId].options.parameters;
                  var hash = str.parseQuery();
                  hash["ajax_request_id"] = Math.random();
                  hash = $H(hash);
                  inquiryUpdaters[result.itemId].options.parameters = hash.toQueryString();
                }
                inquiryCounters[result.itemId]++;
              }
            }
          });
          inquiryRequests[info.itemId] = null;
      },
      onComplete: function() {
        inquiryRequests[info.itemId] = null;
      }
    });
  }
}
function Squick_OnChangeDate(elm)
{
  return true;
}
function Squick_OnChangePerson(elm)
{
  var adultNum = parseInt($('qk-adultNum').value);
  var childNum = parseInt($('qk-childNum').value);
  var infantNum = parseInt($('qk-infantNum').value);
  $('qk-childNum').length = adultNum + 2;
  if (adultNum + $('qk-childNum').length > 9) {
    $('qk-childNum').length = 9 - adultNum + 1;
  }
  for (var i = 0; i < $('qk-childNum').length; i++) {
    $('qk-childNum').options[i].value = i;
    $('qk-childNum').options[i].text = i;
  }
  $('qk-infantNum').length = adultNum + 1;
  if (adultNum + $('qk-infantNum').length > 9) {
    $('qk-infantNum').length = 9 - adultNum + 1;
  }
  for (var i = 0; i < $('qk-infantNum').length; i++) {
    $('qk-infantNum').options[i].value = i;
    $('qk-infantNum').options[i].text = i;
  }
  return true;
}
function Squick_Validate()
{
  var elements = Form.getElements(squickId);
  errorMsg = '';
  for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    switch (element.name) {
      case 'qk-deps':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '出発地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        $('qk-depe').value = $('qk-arrs').value;
        break;
      case 'qk[arrs]':
        if (element.value == '' || element.value == null) {
          element.style.backgroundColor = '#ffc';
          errorMsg += '目的地を入力してください。'+"\n";
        } else {
          element.style.backgroundColor = '#fff';
        }
        $('qk-arre').value = $('qk-deps').value;
        break;
      case 'qk[ys]':
        var di = new Date();
        if (!Sbox_IsValidDate($('qk-ys').value, $('qk-ms').value, $('qk-ds').value)) {
          $('qk-ys').style.backgroundColor = '#ffc';
          $('qk-ms').style.backgroundColor = '#ffc';
          $('qk-ds').style.backgroundColor = '#ffc';
          errorMsg += '出発日を入力してください。'+"\n";
        } else if (Sbox_DiffDate($('qk-ys').value, $('qk-ms').value, $('qk-ds').value, di.getFullYear(), di.getMonth()+1, di.getDate()) == 1) {
          $('qk-ys').style.backgroundColor = '#ffc';
          $('qk-ms').style.backgroundColor = '#ffc';
          $('qk-ds').style.backgroundColor = '#ffc';
          errorMsg += '出発日は本日以降に設定してください。'+"\n";
        } else {
          $('qk-ys').style.backgroundColor = '#fff';
          $('qk-ms').style.backgroundColor = '#fff';
          $('qk-ds').style.backgroundColor = '#fff';
        }
        break;
      case 'qk[ye]':
        if (!Sbox_IsValidDate($('qk-ye').value, $('qk-me').value, $('qk-de').value)) {
          $('qk-ye').style.backgroundColor = '#ffc';
          $('qk-me').style.backgroundColor = '#ffc';
          $('qk-de').style.backgroundColor = '#ffc';
          errorMsg += '現地出発日を入力してください。'+"\n";
        } else {
          var idx = 's';
          if ($('qk-y3') != null || $('qk-y3') != undefined) {
            idx = '3';
          } else if ($('qk-y2') != null || $('qk-y2') != undefined) {
            idx = '2';
          } else if ($('qk-y1') != null || $('qk-y1') != undefined) {
            idx = '1';
          } else if ($('qk-ys') != null || $('qk-ys') != undefined) {
            idx = 's';
          }
          if (Sbox_DiffDate($('qk-ye').value, $('qk-me').value, $('qk-de').value, $('qk-y'+idx).value, $('qk-m'+idx).value, $('qk-d'+idx).value) == 1) {
            $('qk-ye').style.backgroundColor = '#ffc';
            $('qk-me').style.backgroundColor = '#ffc';
            $('qk-de').style.backgroundColor = '#ffc';
            errorMsg += '現地出発日は出発日以降に設定してください。'+"\n";
          } else {
            $('qk-ye').style.backgroundColor = '#fff';
            $('qk-me').style.backgroundColor = '#fff';
            $('qk-de').style.backgroundColor = '#fff';
          }
        }
        break;
      case 'qk[adultNum]':
        var adultNum = parseInt($('qk-adultNum').value);
        var childNum = parseInt($('qk-childNum').value);
        var infantNum = parseInt($('qk-infantNum').value);
        if (adultNum + 1 < (childNum + infantNum)) {
          $('qk-adultNum').style.backgroundColor = '#ffc';
          $('qk-childNum').style.backgroundColor = '#ffc';
          $('qk-infantNum').style.backgroundColor = '#ffc';
          errorMsg += 'お子様と幼児の合計人数が大人の人数を超える場合は、別途お問合せ下さい。'+"\n";
        } else if ((adultNum + childNum + infantNum) > 9) {
          $('qk-adultNum').style.backgroundColor = '#ffc';
          $('qk-childNum').style.backgroundColor = '#ffc';
          $('qk-infantNum').style.backgroundColor = '#ffc';
          errorMsg += '参加人数が10名様以上の場合は、別途お問合せ下さい。'+"\n";
        } else {
          $('qk-adultNum').style.backgroundColor = '#fff';
          $('qk-childNum').style.backgroundColor = '#fff';
          $('qk-infantNum').style.backgroundColor = '#fff';
        }
        break;
    }
  }
  if (errorMsg != '') {
    alert(errorMsg);
    return false;
  }
  return true;
}


