var yaziDocRoot = '/';
var yaziAjaxRoot = yaziDocRoot + 'ajax/';
var yaziImgRoot = yaziDocRoot + 'img/';
var errorColor = "#ffc";
var normalColor = "#fff";

function sbox_lp_search_suggest(input, choice) {
  new Ajax.Autocompleter(
	  input,
	  choice,
	  yaziAjaxRoot + 'suggest/',
	  {method: 'get', paramName: 'keyword',autoSelect: false,minChars: 1,frequency: 1.0,select:'inputValue'}
  );
  return true;
}
function sbox_lp_search_popareasearch(label)
{
  sbox_lp_search_popupcenter(yaziDocRoot + 'service/areasearch/?label=' + label, 'areasearch', 680, 380);
}
function sbox_lp_search_popcalendar(y, m, d)
{
  sbox_lp_search_popupcenter(yaziDocRoot + 'service/calendar/?yl=' + y + '&ml=' + m + '&dl=' + d + '&y=' + $(y).value + '&m=' + $(m).value + '&d=' + $(d).value, 'calendar', 500, 300);
}
function sbox_lp_search_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_lp_search_validate(elements) {
  errorMsg = '';  
  for (var i = 0; i < elements.length; i++) {
	var element = elements[i];
	switch (element.name) {
	  case 'deps':
		break;
	  case 'arrs':
		if (element.value == '' || element.value == null) {
		  element.style.backgroundColor = errorColor;
		  errorMsg += '目的地を入力してください。'+"\n";
		} else {
		  element.style.backgroundColor = normalColor;
		}
		break;
	  case 'ys':
		var di = new Date();
		if (!sbox_lp_search_isvaliddate($('ys').value, $('ms').value, $('ds').value)) {
		  $('ys').style.backgroundColor = $('ms').style.backgroundColor = $('ds').style.backgroundColor = errorColor;
		  errorMsg += '選択された出発日はお取り扱いできません。'+"\n";
		} else if (sbox_lp_search_diffdate($('ys').value, $('ms').value, $('ds').value, di.getFullYear(), di.getMonth()+1, di.getDate()) == 1) {
		  $('ys').style.backgroundColor = $('ms').style.backgroundColor = $('ds').style.backgroundColor = errorColor;
		  errorMsg += '出発日は本日以降に設定してください。'+"\n";
		} else {
		  $('ys').style.backgroundColor = $('ms').style.backgroundColor = $('ds').style.backgroundColor = normalColor;
		}
		break;
	  case 'ye':
		if (!sbox_lp_search_isvaliddate($('ye').value, $('me').value, $('de').value)) {
		  $('ye').style.backgroundColor = $('me').style.backgroundColor = $('de').style.backgroundColor = errorColor;
		  errorMsg += '出発日の選択に誤りがあります。'+"\n";
		} else if (sbox_lp_search_diffdate($('ye').value, $('me').value, $('de').value, $('ys').value, $('ms').value, $('ds').value) == 1) {
		  $('ye').style.backgroundColor = $('me').style.backgroundColor = $('de').style.backgroundColor = errorColor;
		  errorMsg += '現地出発日は出発日以降に設定してください。'+"\n";
		} else {
		  $('ye').style.backgroundColor = $('me').style.backgroundColor = $('de').style.backgroundColor = normalColor;
		}
		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;
	}
  }
  if (errorMsg != '') {
	alert(errorMsg);
	return false;
  } else {
	return true;
  }
}
function sbox_lp_search_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_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;
}
function sbox_lp_search_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;
  }
}

