﻿/*search.js
 * nur für search
*/

function checkit ( trend, MaxDur, compDate) {
  var idx = document.SEARCHPARMS.ARRIVAL_1.value;
  if (!isNull ( document.SEARCHPARMS.ARRIVAL_1, sArrival ))
    return false;
  if (!isDate ( document.SEARCHPARMS.ARRIVAL_1, sArrival ))
    return false;
  if (!compareWithToday ( document.SEARCHPARMS.ARRIVAL_1, sArrival, compDate ))
    return false;
  day = idx.substring(0,2);
  month = idx.substring(3,5);
  year = idx.substring(6,10);
  if (document.SEARCHPARMS.DURATION.value.charAt(0)==" ") {
    alert(sNoBlanks);
    document.SEARCHPARMS.DURATION.focus();
    return false;
  }
  if (!isNull( document.SEARCHPARMS.DURATION, sDuration))
    return false;
  if (!isNumber( document.SEARCHPARMS.DURATION, sDuration))
    return false;
  if (!isNumber( document.SEARCHPARMS.ROOMS, sValidNrRoom))
    return false;
  if (document.SEARCHPARMS.DURATION.value=="0") {
    alert(sDuration + ": " + sMustNotBe0);
    return false; }
  x = new Date(year, month - 1, day);
  for (var i=2; i<=document.forms[0].DURATION.value; i++) {
    x.setDate(x.getDate()+1);
  }
  strmonth = x.getMonth() + 1;
  stryear = x.getFullYear();
  if ((parseInt(stryear,10)==parseInt(year,10) && parseInt(strmonth,10)-1 > parseInt(month,10)) ||
      (parseInt(stryear,10) > parseInt(year,10) && (13-parseInt(month,10))+parseInt(strmonth,10) > 2)) {
    alert(sDurrInSamMth);
    document.forms[0].DURATION.value="";
    document.forms[0].DURATION.focus();
    return false;
  }

  if (MaxDur != 100000) {
    var idd = document.SEARCHPARMS.DURATION.value;
    x = new Date(year, month-1, day, 0, 0, 0, 0);
    for (i=2; i<=idd; i++) {
      x.setDate(x.getDate()+1);
    }
    //nun entspricht x dem letzten gewünschten Tag
    todayDay = compDate.substring(0, 2);
    todayMonth = compDate.substring(3, 5);
    todayYear = compDate.substring(6, 10);
    today = new Date(todayYear, todayMonth - 1, todayDay);

    for (i=2; i<=MaxDur; i++) {
      today.setDate(today.getDate()+1);
    }
    day = today.getDate(); 
    month = today.getMonth(); 
    year  = today.getFullYear(); 
    today = new Date(year,month,day, 0, 0, 0);
    //nun entspricht today dem letzten erlaubten Tag
    //if (MaxDur != 100000) {
    diff = x.getTime() - today.getTime();
    if (diff > 0) {
      diff = Math.floor(diff / (1000 * 60 * 60 * 24) );
      if (diff > 0) {
	if (idd-diff>0) {
          document.forms[0].DURATION.value=idd-diff;
          alert(sMaxLMA1 + Math.floor(MaxDur/7) + sMaxLMA2 + sSMaxLMA3);
          document.forms[0].DURATION.focus();
          return false;
	}
	else if (idd-diff<=0) {
	  for (i=-idd; i<-1; i++) {
            today.setDate(today.getDate()-1);
          }
          var tag = '0';
          var monat = '0';
          if (today.getDate()>9)
            tag = today.getDate();
          else
            tag = tag + today.getDate();
          if (today.getMonth()+1>9)
            monat = today.getMonth()+1;
          else
            monat = monat + (today.getMonth()+1);
          document.forms[0].ARRIVAL_1.value=tag+"."+monat+"."+today.getFullYear();
          alert(sMaxLMA1 + Math.floor(MaxDur/7) + sMaxLMA2 + sSMaxLMA3);
          document.forms[0].ARRIVAL_1.focus();
          return false;
        }
      }
    }
  }

  if (!isNull( document.SEARCHPARMS.ROOMS, sRooms ))
    return false;
  idx = document.SEARCHPARMS.ROOMS.value;
  if ((trend == 1 && idx == 0) || idx < 0) {
    alert(sValidNrRoom);
    return false;
  }
  if (idx.charAt(0)==' ') {
    alert(sNoBlanks);
    document.SEARCHPARMS.ROOMS.value="0";
    return false;
  }
  idx = document.SEARCHPARMS.REGION.options[document.SEARCHPARMS.REGION.selectedIndex].value;
  if (idx=="dummy") {
    alert(sChooseRegion + document.SEARCHPARMS.REGION[document.SEARCHPARMS.REGION.selectedIndex].text);
    return false;
  }
  document.forms[0].w_width.value=screen.width;
  document.forms[0].w_height.value=screen.height;
  return true;
}
//End of checkit

function isSelectedh () {
  for (var i=0; i<1000; i++) {
    if (document.forms[0].HOTEL.options [i]!="") {
      if (document.forms[0].HOTEL.options [i].selected==true)
      {
        document.forms[0].LOCATIONI.options [0].selected=true;
        document.forms[0].REGION.options [0].selected=true;
        return true;
      }
    }
  }
  return true;
}
//End of isSelectedh

function isSelectedl (trend) {
  for (var i=0; i<500; i++) {
    if (document.forms[0].LOCATIONI.options [i]!="") {
      if (document.forms[0].LOCATIONI.options [i].selected==true) 
      { 
	if (trend==1) document.forms[0].HOTEL.options [0].selected=true;
        document.forms[0].REGION.options [0].selected=true;
        return true;
      }
    }
  }
  return true;
}
//End of isSelectedl

function isSelectedr (trend) {
  for (var i=0; i<100; i++) {
    if (document.forms[0].REGION.options [i]!="") {
      if (document.forms[0].REGION.options [i].selected==true)
      { 
	if (trend==1) document.forms[0].HOTEL.options [0].selected=true;
        document.forms[0].LOCATIONI.options [0].selected=true;
        return true;
      }
    }
  }
  return true;
}
//End of isSelectedr

