/*------------------------------------------------------------
            Online-Buchungs- und Reservierungsscript
                   der Planet-Windsurfing AG
            Copyright (C) - 2006 Klaus Michael Tamm
            zuletzt bearbeitet: 14.11.2008 09:10:22
------------------------------------------------------------*/

function calendar_date(ctrl) {
  if (ctrl.name=='month_year_from') {
    var day=document.reservation.day_from.value;
    var m_from=document.reservation.month_year_from.value;
    var month=m_from.substr(0,m_from.indexOf('.'))-1;
    var year=m_from.substr(m_from.indexOf('.')+1);
  } else {
    var day=document.reservation.day_until.value;
    var m_until=document.reservation.month_year_until.value;
    var month=m_until.substr(0,m_until.indexOf('.'))-1;
    var year=m_until.substr(m_until.indexOf('.')+1);
  }
  return new Date(year,month,day);
}

function check_article(ctrl) {
  if ((ctrl.name=='shop_article')&&(ctrl.value!='')) {
    if (typeof ctrl.value!='string') {
        alert("Please put in only the article's label here.");
        ctrl.focus();
        return false;
    }
  }
  if ((ctrl.name=='article_price')&&(ctrl.value!='')) {
    ctrl.value = ctrl.value.replace(/,/, ".");
    if (isNaN(ctrl.value) == true) {
        alert("Please put in only the article's price here.");
        ctrl.focus();
        return false;
    } else ctrl.value = parseFloat(ctrl.value);
    if (document.reservation.shop_article.value=='') ctrl.value='';
  }
  return true;
}

function check_booking_span() {
  if ((document.reservation.day_from)&&(document.reservation.month_year_from)&&(document.reservation.day_until)&&(document.reservation.month_year_until)) {
    var sdate = calendar_date(document.reservation.month_year_from);
    var edate = calendar_date(document.reservation.month_year_until);
    days = Math.floor(eval((edate-sdate)/(86400000))) + 1;
    if (days<=0) {
      alert("Invalid booking-date!");
      document.reservation.day_until.focus();
      return false;
    }
    if (days<3) {
      alert("Bookings over less than 3 days\ncan only be done at the destinations itself.");
      document.reservation.day_until.focus();
      return false;
    }
    if (days>28) {
      alert("Bookings over 28 days (4 weeks) can only be done by e-mail:info@planetallsports.com\nor over our Service Hotline: +49 (0) 881 - 927710.");
      document.reservation.day_until.focus();
      return false;
    }
    if ((days!=7)&&(is_extra_order())) {
      var temp = document.reservation.extra;
      for (i=0;i<temp.length;i++) {
        if (temp[i].value.toLowerCase()=='4 out of 7 days') {
         	temp[i].checked=false;
        	break;
        }
      }
    }
    if ((days!=14)&&(is_extra_order())) {
      var temp = document.reservation.extra;
      for (i=0;i<temp.length;i++) {
        if (temp[i].value.toLowerCase()=='9 out of 14 days') {
        	temp[i].checked=false;
        	break;
        }
      }
    }
    if ((days!=21)&&(is_extra_order())) {
      var temp = document.reservation.extra;
      for (i=0;i<temp.length;i++) {
        if (temp[i].value.toLowerCase()=='14 out of 21 days') {
        	temp[i].checked=false;
        	break;
        }
      }
    }
    for (var i=0;i<seasons.length;i++) {
      if ((i>0)&&(seasons[i][1]<=seasons[i-1][2])) {
        if ((sdate>seasons[i-1][1])&&(sdate<seasons[i-1][2])&&(edate>=seasons[i][1])&&(edate<seasons[i][2])) {
          if (edate>=date_add(seasons[i][1], 14)) {
            alert('The booking period exceeds the change of the winter/summer season.\nThe offered material might change for the coming season.\nPlease contact our service team info@planetallsports.com.\nWe will help you to find the right equipment!');
            document.reservation.day_until.focus();
            return false;
          }
        }
      }
    }
    document.reservation.startdate.value=document.reservation.day_from.value+'.'+document.reservation.month_year_from.value;
    document.reservation.enddate.value=document.reservation.day_until.value+'.'+document.reservation.month_year_until.value;
    return true;
  } else {
    alert("Booking not possible!\nThis destination is closed at the moment.");
    return false;
  }
}

function check_quantity(obj) {
  if (obj.value.length>0) {
    if(isNaN(obj.value) == true) {
      alert(obj.value + " is not an integer!\nPlease put in an integer between 1 and 8.");
      obj.value='';
      obj.focus();
      return false;
    }
    obj.value=Math.floor(obj.value);
    if((obj.value < 1)||(obj.value > 8)) {
      alert("Please put in an integer between 1 and 8.");
      obj.value='';
      obj.focus();
      return false;
    }
  }
  return true;
}

function check_selections() {
  var article=is_article_order();
  var bike=is_bike_order();
  var extra=is_extra_order();
  var lessons=is_lesson_order();
  var other=is_other_order();
  if (document.reservation.rental) {
    if ((document.reservation.rental.value=='none')&&(article==false)&&(bike==false)&&(extra==false)&&(lessons==false)&&(other==false)) {
      alert("You haven't done any selection yet!");
      return false;
    }
    if ((document.reservation.rental.value!='none')&&(document.reservation.rental.value!='Surf Storage')&&(document.reservation.rental.value!='Kite Storage')&&(document.reservation.elements['other'].checked==false)&&(check_protection_kit==1)) {
      if (document.reservation.dest_type.value!="Surfpool") {
        var decision=confirm("Do you need a material insurance?");
        check_protection_kit=0;
        if(decision==true) {
          document.reservation.elements['other'].checked=true;
          return false;
        } else return false;
      } else {
        var decision=confirm("Do you need a surf toolkit?");
        check_protection_kit=0;
        if(decision==true) {
          document.reservation.elements['other'].checked=true;
          return false;
        } else return false;
      }
    } else return true;
  } else {
      if ((article==false)&&(bike==false)&&(lessons==false)) {
        alert("You haven't done any selection yet!");
        return false;
      } else return true;
  }
}

function date_add(date0,days) {
  var date1=new Date(date0.getFullYear(),date0.getMonth(),date0.getDate());
  var time=date1.getTime();
  time+=(days*24*60*60*1000);
  date1.setTime(time);
  return date1;
}

function delete_all() {
  document.location.replace('index.php?sid='+document.reservation.sid.value+'&lang=en&dest_type='+document.reservation.dest_type.value+'&station='+document.reservation.station.value+'&season='+document.reservation.season.value+'&person='+document.reservation.person.value+'&selected_items='+document.reservation.selected_items.value+'&task=edit&selection=delete');
  return true;
}

function init_all() {
  set_services();
  var start = document.reservation.startdate.value;
  var end = document.reservation.enddate.value;
  if (start.length>0) {
    var day = start.substr(0,start.indexOf('.'));
    var month_year = start.substr(start.indexOf('.')+1);
    var temp = document.reservation.month_year_from;
    for (i=0;i<temp.options.length;i++) {
      if (temp.options[i].value==month_year) {
      	temp.options[i].selected=true;
      	break;
      }
    }
    set_days(document.reservation.month_year_from);
    temp = document.reservation.day_from;
    for (i=0;i<temp.options.length;i++) {
      if (temp.options[i].value==day) {
      	temp.options[i].selected=true;
      	break;
      }
    }
    set_days(document.reservation.day_from);
  }
  if (end.length>0) {
    var day = end.substr(0,end.indexOf('.'));
    var month_year = end.substr(end.indexOf('.')+1);
    var temp = document.reservation.month_year_until;
    for (i=0;i<temp.options.length;i++) {
      if (temp.options[i].value==month_year) {
      	temp.options[i].selected=true;
      	break;
      }
    }
    set_days(document.reservation.month_year_until);
    temp = document.reservation.day_until;
    for (i=0;i<temp.options.length;i++) {
      if (temp.options[i].value==day) {
      	temp.options[i].selected=true;
      	break;
      }
    }
  }
}

function is_article_order() {
  if((document.reservation.elements['shop_article'])&&(document.reservation.elements['article_price'])) {
      if ((document.reservation.elements['shop_article'].value!='')&&(document.reservation.elements['article_price'].value!='')) {
          return true;
      } else return false;
  } else return false;
}

function is_bike_order() {
  if(document.reservation.elements['bike']) {
    if (document.reservation.elements['bike'].value!='none') {
      return true;
    } else return false;
  } else return false;
}

function is_extra_order() {
  for (var i=0;i<document.reservation.length;i++) {
    if(document.reservation.elements[i].name=='extra') {
      if (document.reservation.elements[i].checked==true) {
        return true;
        break;
      }
    }
  }
  return false;
}

function is_lesson_order() {
  for (var i=0;i<document.reservation.length;i++) {
    if(document.reservation.elements[i].name=='quantity[]') {
      if (document.reservation.elements[i].value.length>0) {
        return true;
        break;
      }
    }
  }
  return false;
}

function is_other_order() {
  for (var i=0;i<document.reservation.length;i++) {
    if(document.reservation.elements[i].name=='other') {
      if (document.reservation.elements[i].checked==true) {
        return true;
        break;
      }
    }
  }
  return false;
}

function last_bookable_date(sdate) {
  var ldate = seasons[0][2];
  for (var i=0;i<seasons.length;i++) {
    if ((i>0)&&(seasons[i][1]<=seasons[i-1][2])) ldate = seasons[i][2];
    else if (sdate<seasons[i][2]) ldate = seasons[i][2];
  }
  return ldate;
}

function set_booking_span(ctrl) {
  var span = ctrl.value.substr(ctrl.value.length-7,2);
  var startdate = calendar_date(document.reservation.month_year_from);
  var new_enddate = date_add(startdate,span-1);
  document.reservation.enddate.value=new_enddate.getDate()+'.'+new_enddate.getMonth()+'.'+new_enddate.getYear();
  init_all();
  check_booking_span();
}

function set_days(ctrl) {
  var startdate = calendar_date(document.reservation.month_year_from);
  var startdate0 = last_bookable_date(startdate);
  var may2008 = new Date(2008, 4, 1);
  if (startdate>=may2008) var md=2; else var md=4;
  if (startdate>startdate0-md) startdate=date_add(startdate0, -md);
  if (ctrl.name=='month_year_from') {
    var temp = document.reservation.day_from;
    for (var i=temp.options.length-1;i>=0;i--) temp.options[i]=null;
    var fdom = first_days_of_month[ctrl.selectedIndex];
    var ldom = last_days_of_month[ctrl.selectedIndex];
    var lbd = last_bookable_date(calendar_date(document.reservation.month_year_from));
    if ((ctrl.value==eval(lbd.getMonth()+1)+'.'+lbd.getFullYear())&&(lbd.getDate()==ldom)) ldom = ldom - md;
    var j = eval(ldom - fdom);
    var day=null;
    for (i=0;i<=j;i++) {
      day = parseInt(fdom)+i;
      temp.options[i]=new Option(day,day);
    }
    temp.options[0].selected=true;
    startdate = calendar_date(document.reservation.month_year_from);
  }
  if ((ctrl.name=='day_from')||(ctrl.name=='month_year_from')||(ctrl.name=='month_year_until')) {
    var enddate = date_add(startdate, 27);
    startdate = date_add(startdate, md);
    if ((ctrl.name=='day_from')||(ctrl.name=='month_year_from')) {
      var temp = document.reservation.month_year_until;
      var temp0 = document.reservation.month_year_from;
      for (var i=temp.options.length-1;i>=0;i--) temp.options[i]=null;
      var sdate = eval(startdate.getMonth()+1)+'.'+startdate.getFullYear();
      var edate = eval(enddate.getMonth()+1)+'.'+enddate.getFullYear();
      for (i=0;i<temp0.options.length;i++) {
        if (temp0.options[i].value==sdate) temp.options[temp.options.length]=new Option(temp0.options[i].text,temp0.options[i].value);
        if ((temp0.options[i].value==edate)&&(edate!=sdate)) temp.options[temp.options.length]=new Option(temp0.options[i].text,temp0.options[i].value);
      }
      temp.options[0].selected=true;
    }
    var temp = document.reservation.day_until;
    var temp0 = document.reservation.month_year_until;
    for (var i=temp.options.length-1;i>=0;i--) temp.options[i]=null;
    for (i=0;i<document.reservation.month_year_from.options.length;i++) {
      if (document.reservation.month_year_from.options[i].value==temp0.options[temp0.selectedIndex].value) {
        var sIndex=i;
      }
    }
    var month_year_until = document.reservation.month_year_until.value;
    var month = month_year_until.substr(0,month_year_until.indexOf('.'))-1;
    var year = month_year_until.substr(month_year_until.indexOf('.')+1);
    var fdom = first_days_of_month[sIndex];
    var ldom = last_days_of_month[sIndex];
    var j = eval(ldom - fdom);
    var day=null;
    var date=null;
    for (i=0;i<=j;i++) {
      day = parseInt(fdom)+i;
      date0 = new Date(year,month,day);
      if ((date0>=startdate)&&(date0<=enddate)) {
      	temp.options[temp.options.length]=new Option(day,day);
      }
    }
    if ((ctrl.name=='day_from')||(ctrl.name=='month_year_from')) set_season();
  }
  document.reservation.startdate.value=document.reservation.day_from.value+'.'+document.reservation.month_year_from.value;
  document.reservation.enddate.value=document.reservation.day_until.value+'.'+document.reservation.month_year_until.value;

}

function set_destination_type(index) {
  if (document.reservation.selected_items.value.length>1&&(document.reservation.dest_type.selectedIndex!=desttype)) {
    alert('Innerhalb einer Buchung kann die Art des Zieles nicht gewechselt werden!');
    document.reservation.dest_type.options[document.reservation.dest_type.selectedIndex].selected=false;
    document.reservation.dest_type.options[desttype].selected=true;
  } else {
     window.location.href='index.php?lang=en&dest_type='+document.reservation.dest_type.value;
     desttype = document.reservation.dest_type.selectedIndex;
  }
}

function set_season() {
  var from=document.reservation.day_from.value+'.'+document.reservation.month_year_from.value;
  var until=document.reservation.day_until.value+'.'+document.reservation.month_year_until.value;
  var month_year_from=document.reservation.month_year_from.value;
  var month=month_year_from.substr(0,month_year_from.indexOf('.'))-1;
  var year=month_year_from.substr(month_year_from.indexOf('.')+1);
  var day=document.reservation.day_from.value;
  var sel_day=new Date(year,month,day);
  var s=null;
  for (var i=0;i<seasons.length;i++) {
    if ((sel_day>=seasons[i][1])&&(sel_day<seasons[i][2])) { s=seasons[i][0]; }
  }
  if ((s!=null)&&(document.reservation.season.value!=s)) {
    document.reservation.startdate.value='';
    document.reservation.enddate.value='';
    document.reservation.season.value=s;
    window.location.href= 'index.php?sid='+document.reservation.sid.value+'&lang=en&dest_type='+document.reservation.dest_type.value+'&station='+document.reservation.station.value+'&season='+document.reservation.season.value+'&from='+from+'&until='+until+'&person='+person;
  }
}

function set_services() {
  if (document.reservation.rental) {
    var rental = document.reservation.rental.value;
    var storage = rental.search(/Storage/)!=-1;
    var kite = document.reservation.station.value.search(/Kite/)!=-1;
    for (var i=0;i<document.reservation.length;i++) {
      if (document.reservation.elements[i].name=='extra') {
        if ((rental=='none')||(rental=='Startpool')||(rental=='Startset')||(storage)) {
          document.reservation.elements[i].checked=false;
          document.reservation.elements[i].disabled=true;
        } else document.reservation.elements[i].disabled=false;
      }
      if (document.reservation.elements[i].name=='other') {
        if ((rental=='none')||(storage)) {
          document.reservation.elements[i].checked=false;
          document.reservation.elements[i].disabled=true;
        } else document.reservation.elements[i].disabled=false;
      }
      /*if((document.reservation.elements[i].name=='quantity[]')&&(document.reservation.elements[i+1].value.search(/excl. mat./i)!=-1)) {
        if (((rental=='none')&&(!storage))&&((!kite))&&(document.reservation.dest_type.value!='Surfpool')) {
          document.reservation.elements[i].disabled=true;
          document.reservation.elements[i].value='';
          document.reservation.elements[i+1].disabled=true;
        } else {
          document.reservation.elements[i].disabled=false;
          document.reservation.elements[i+1].disabled=false;
        }
      }
      if((document.reservation.elements[i].name=='quantity[]')&&(document.reservation.elements[i+1].value.search(/incl. mat./i)!=-1)) {
        if (((rental=='none')||(storage))||((kite)&&(rental!='Kitepool'))||(document.reservation.dest_type.value=='Surfpool')) {
          document.reservation.elements[i].disabled=false;
          document.reservation.elements[i+1].disabled=false;
        } else {
          document.reservation.elements[i].disabled=true;
          document.reservation.elements[i].value='';
          document.reservation.elements[i+1].disabled=true;
        }
      }*/
    }
    if (document.all) {
      if (document.all.excl) {
        for (var i=0;i<document.all.excl.length;i++) {
          if (document.all.excl[i].disabled==true) document.all.excl[i].style.backgroundColor = "#c0c0c0";
          else document.all.excl[i].style.backgroundColor = "#ffffff";
        }
      }
      if (document.all.incl) {
        for (i=0;i<document.all.incl.length;i++) {
          if (document.all.incl[i].disabled==true) document.all.incl[i].style.backgroundColor = "#c0c0c0";
          else document.all.incl[i].style.backgroundColor = "#ffffff";
        }
      }
    } else {
      for (var i=0;i<document.getElementsByName('quantity[]').length;i++) {
        if (document.getElementsByName('quantity[]')[i].disabled==true) document.getElementsByName('quantity[]')[i].style.backgroundColor="#c0c0c0";
        else document.getElementsByName('quantity[]')[i].style.backgroundColor="#ffffff";
      }
    }
  }
}

function set_station_values(station) {
  var found=station.value.search(/Kiting not offered/);
  if(found!=-1) {
    alert('Sorry, but "Kiting" is not available at this destination!');
    station.options[station.selectedIndex].selected=false;
    station.options[station.selectedIndex-1].selected=true;
    destination=station.selectedIndex;
  } else {
    //var newstation=station.value.replace(/ Kite/,"");
    var newstation=station.value;
    if (document.reservation.selected_items.value.length>1&&(newstation!=destname)) {
        alert("Whithin one booking you can't change the destination!");
        station.options[station.selectedIndex].selected=false;
        station.options[destination].selected=true;
    } else {
      destination=station.selectedIndex;
      window.location.href='index.php?station='+station.value+'&lang=en&dest_type='+document.reservation.dest_type.value+'&person='+person;
    }
  }
  if(station.value=='Lefkada') {
    if(confirm('If you want to book "Surfpool" as a package, please click "OK" to redirect to the homepage of our partner "Sun&Fun",\nif not, just click "CANCEL" to book "Surfpool" at this site.')) {
        nw = window.open('http://www.sunandfun.de');
        nw.focus();
    }
  }
}

function show_pricelists() {
  var properties="dependent=yes,toolbar=no,location=no,directories=no," +
  "status=no,menubar=no,scrollbars=yes,resizable=yes,width=800,height=480";
  //var list="pricelists.php?dest_type="+document.reservation.dest_type.value+"&station="+document.reservation.station.value+"&show="+document.reservation.season.value;
  var list="pricelist.html";
  var nwin=window.open (list,"Pricelist",properties);
  nwin.focus();
}

function show_descriptions() {
  var properties="dependent=yes,toolbar=no,location=no,directories=no," +
  "status=no,menubar=no,scrollbars=yes,resizable=yes,width=800,height=480";
  if (document.reservation.dest_type.value=="Surfpool")
    var descriptions="descriptions_sp_en.html";
  else
    var descriptions="descriptions_fm_en.html";
  var nwin=window.open (descriptions,"Descriptions",properties);
  nwin.focus();
}

