var BuchungsFenster = null;

function GetAttribute(pElement, pAttributeName, pDefaultValue)
{
  var vResult = pElement.getAttribute(pAttributeName, 0);
  if (vResult == null)
     vResult = pDefaultValue;
  return vResult;
} // GetAttribute()

function GetDateValue(pTextBoxID)
{
   var vTextBoxFld = document.getElementById(pTextBoxID);
   var vDateOrder = new Array();
   vDateOrder[0] = 'dd';
   vDateOrder[1] = 'MM';
   vDateOrder[2] = 'yyyy';
   if ((vTextBoxFld.value.length > 2) && (vTextBoxFld.value.indexOf('.') == -1))
     AutoFillInDateSeparators(vTextBoxFld, vDateOrder);
   var vDate = null;
   var vToday = new Date();
   var vYear = 0;
   var vMonth = 0;
   var vDay = 0;
   var vOK = true;
   var vTextBoxParts = vTextBoxFld.value.split('.');
   if (vTextBoxParts.length == 1)   // only entered the date
   {
      vYear = vToday.getFullYear();
      vMonth = vToday.getMonth() + 1;
   }
   else if (vTextBoxParts.length == 2) // only entered month and date
   {
      vYear = vToday.getFullYear();
   }
   var vTextBoxCount = 0;
   for (var vI = 0; vOK && (vI < vDateOrder.length); vI++)
   {
      switch (vDateOrder[vI].charAt(0))
      {
         case 'd':
         case 'D':
            vDay = ParseDateElement(vTextBoxParts[vTextBoxCount]);
            if (!isNaN(vDay) && (vDay > 0))
               vTextBoxCount++;
            else
               vOK = false;
            break;

         case 'm':
         case 'M':
            if (vMonth == 0)
            {
               if (vTextBoxParts[vTextBoxCount] == '')
                  vMonth = vToday.getMonth() + 1;
               else
                  vMonth = ParseDateElement(vTextBoxParts[vTextBoxCount]);
               if (isNaN(vMonth))
               {
                  vMonth = vToday.getMonth() + 1;
                  vTextBoxCount++;
               }
               else if ((vMonth <= 12) && (vMonth > 0))
                  vTextBoxCount++;
               else
                  vOK = false;
            }
            break;

         case 'y':
         case 'Y':
            if (vYear == 0)
            {
               if (vTextBoxParts[vTextBoxCount] == '')
                  vYear = vToday.getFullYear();
               else
                  vYear = ParseDateElement(vTextBoxParts[vTextBoxCount]);
               if (isNaN(vYear))
               {
                  vYear = vToday.getFullYear();
                  vTextBoxCount++;
               }
               else if (vYear <= 9999)
               {
                  if (vYear < 100)
                  {
                    var vCenturyBreak = GetAttribute(vTextBoxFld, 'Custom_CenturyBreak', 50);
                    if ((vCenturyBreak == 0) || (vYear < vCenturyBreak))
                       vYear = vYear + 2000;
                    else
                       vYear = vYear + 1900;
                  }
                  vTextBoxCount++;
               }
               else
                  vOK = false;
            }
            break;

      }  // switch
   }  // for vI


   var vForeColor = GetAttribute(vTextBoxFld, 'Custom_ForeColor','');
   if (vOK)
   {
      vDate = new Date(vYear, vMonth - 1, vDay);
      if (vForeColor != '')
        vTextBoxFld.style.color='';
   }
   else
      if (vForeColor != '')
        vTextBoxFld.style.color=vForeColor;
   return vDate;
}  // GetDateValue()

function ParseDateElement(pValue)
{
   if (pValue == '0')
      return 0;
   else
      return parseInt(pValue.replace(/^0*/, ''));
}  // ParseDateElement()

function SetDateValue(pTextBoxID, pDate, pMarkDirtyB)
{
   var vTextBoxFld = document.getElementById(pTextBoxID);
   var vDateOrder = new Array();
   vDateOrder[0] = 'dd';
   vDateOrder[1] = 'MM';
   vDateOrder[2] = 'yyyy';
   var vDateSeparator = '.';
   var vResultString = '';
   for (var vI = 0; vI < 3; vI++)
   {
      if (vResultString != '')
         vResultString = vResultString + vDateSeparator;
      switch (vDateOrder[vI].charAt(0))
      {
         case 'd':
         case 'D':
            if ((vDateOrder[vI].length == 1) || (pDate.getDate() >= 10))
               vResultString = vResultString + pDate.getDate().toString();
            else  // 1 digit but needs two
               vResultString = vResultString + '0' + pDate.getDate().toString();
            break;

         case 'm':
         case 'M':
            var vMonth = pDate.getMonth() + 1;
            if ((vDateOrder[vI].length == 1) || (vMonth >= 10))
               vResultString = vResultString + vMonth.toString();
            else  // 1 digit but needs two
               vResultString = vResultString + '0' + vMonth.toString();
            break;

         case 'y':
         case 'Y':
            var vYear = pDate.getFullYear();
            if (vDateOrder[vI].length < 4)
               vYear = vYear % 100;
            if (vYear >= 10)
               vResultString = vResultString + vYear.toString();
            else
               vResultString = vResultString + '0' + vYear.toString();
            break;

      }  // switch
   }  // for vI
   vTextBoxFld.value = vResultString;
   if (pMarkDirtyB)
      vTextBoxFld.setAttribute('Custom_Dirty', true, 0);
}  // SetDateValue()

function ShowDateFieldError(pTextBoxFldID)
{
  var vCode = 'ShowDateFieldErrorBody(\'' + pTextBoxFldID + '\')';
  setTimeout(vCode, 10);
}  // ShowDateFieldError()

function ShowDateFieldErrorBody(pTextBoxFldID)
{
  var vFld = document.getElementById(pTextBoxFldID);
  var vFocusOnErrorB = GetAttribute(vFld, 'Custom_FocusOnError', false);
  if (vFocusOnErrorB == true)
  {
     vFld.focus();
     vFld.select();
  }
  var vInvalidDateMsg = GetAttribute(vFld, 'Custom_InvalidDateMsg', false);
  if (vInvalidDateMsg != '')
     window.alert(vInvalidDateMsg);
}  // ShowDateFieldErrorBody()

function GetDaysInMonth(pMonth, pYear)
{  // pMonth is a month number between 1 and 12. pYear is 4 digits
   if (pMonth == 2)  // 28 or 29 (leap year)
   {
      var vDays = 28;
      if (pYear % 4 == 0)
      {
         vDays = 29;
         if ((pYear % 100 == 0) && (pYear % 400 != 0))
            vDays = 28;
      }
      return vDays;
   }
   else if ((pMonth == 4) || (pMonth == 6) || (pMonth == 9) || (pMonth == 11))
      return 30;
   else
      return 31;
}  // GetDaysInMonth()

function AutoFillInDateSeparators(pTextBoxFld, pDateOrder)
{
   vElementOffset = new Array();
   for (vElement = 0; vElement < 2; vElement++)
   {
      switch (pDateOrder[vElement].charAt(0))
      {
          case 'D':
          case 'd':
             vElementOffset[vElement] = 2; // always assign two digit dates
             break;
          case 'M':
          case 'm':
            // months are 2 digits if even string length, 1 if odd
             if (pTextBoxFld.value.length % 2 == 0)  // even number of characters
                vElementOffset[vElement] = 2;
             else
                vElementOffset[vElement] = 1;
             break;
          case 'Y':
          case 'y':
             // years are 0 digits if length is 3-4; 2 if 5-6; 4 if 7-8
             if (pTextBoxFld.value.length >= 5)
                if (pTextBoxFld.value.length >= 7)
                   vElementOffset[vElement] = 4;
                else
                   vElementOffset[vElement] = 2;
             break;
       }  // switch
     }  // for vElement
    // apply the results. If a vElementOffset is 0, skip it
    vDateSeparator = '.'
    vResultText = pTextBoxFld.value.substring(0, vElementOffset[0]) + vDateSeparator + pTextBoxFld.value.substring(vElementOffset[0], vElementOffset[0] + vElementOffset[1]);
    if (pTextBoxFld.value.length > 4)
       vResultText = vResultText + vDateSeparator + pTextBoxFld.value.substring(vElementOffset[0] + vElementOffset[1], pTextBoxFld.value.length + 1);
    pTextBoxFld.value = vResultText;
   }  // AutoFillInDateSeparators()

function ExEncodeURI(pTextToEncode)
{
   var vDateSeparator = '.';
   var vDateSeparatorEncode = '.';
   var pDateParts = pTextToEncode.split(vDateSeparator);
   return pDateParts[0] + vDateSeparatorEncode + pDateParts[1] + vDateSeparatorEncode + pDateParts[2];
} // ExEncodeURI

function AssignToday(pTextBoxID, pStartRangeBoxID, pEndRangeBoxID)
{
   vToday = new Date(); // today's date
   SetDateValue(pTextBoxID, vToday, true);
   document.getElementById(pTextBoxID).select();
   ApplyDateRangeRules(vToday, pStartRangeBoxID, pEndRangeBoxID);
}  // AssignToday()
function AssignNextDate(pTextBoxID, pStartRangeBoxID, pEndRangeBoxID)
{
   var vDate = null;
   // if blank, assign today first
   if (document.getElementById(pTextBoxID).value == '')
      vDate = new Date();
   else
      vDate = GetDateValue(pTextBoxID);

   if (vDate != null)
   {  // increment one date. May wrap around on the month or year
      var vDaysInMonth = GetDaysInMonth(vDate.getMonth() + 1, vDate.getFullYear());
      if (vDate.getDate() < vDaysInMonth)
         vDate.setDate(vDate.getDate() + 1);
      else  // roll over month
      {
         vDate.setDate(1); // day 1 of the next month
         // NOTE: getMonth and setMonth use January = 0
         if (vDate.getMonth() < 11)
            vDate.setMonth(vDate.getMonth() + 1);
         else  // next year
         {
            vDate.setMonth(0);
            vDate.setFullYear(vDate.getFullYear() + 1);
         }  // if (vDate.getMonth())
      }  // if (vDate.getDate())
      SetDateValue(pTextBoxID, vDate, true);
      document.getElementById(pTextBoxID).select();
      ApplyDateRangeRules(vDate, pStartRangeBoxID, pEndRangeBoxID);
   }  // if (vDate != null)

   else
      ShowDateFieldError(pTextBoxID);
}  // AssignNextDate()

function AssignPrevDate(pTextBoxID, pStartRangeBoxID, pEndRangeBoxID)
{
   // if blank, assign today first
   if (document.getElementById(pTextBoxID).value == '')
      vDate = new Date();
   else
      vDate = GetDateValue(pTextBoxID);

   if (vDate != null)
   {  // decrement one date. May wrap around on the month or year
      if (vDate.getDate() > 1)
         vDate.setDate(vDate.getDate() - 1);
      else  // roll over month
      {
         if (vDate.getMonth() > 0)
            vDate.setMonth(vDate.getMonth() - 1);
         else  // roll over year
         {
            vDate.setMonth(11);  // December
            vDate.setFullYear(vDate.getFullYear() - 1);
         }
         // last date of the month
         var vDaysInMonth = GetDaysInMonth(vDate.getMonth() + 1, vDate.getFullYear());
         vDate.setDate(vDaysInMonth);
      }  // if (vDate.getDate())
      SetDateValue(pTextBoxID, vDate, true);
      document.getElementById(pTextBoxID).select();
      ApplyDateRangeRules(vDate, pStartRangeBoxID, pEndRangeBoxID);
   }  // if (vDate != null)

   else
      ShowDateFieldError(pTextBoxID);
}  // AssignPrevDate()

function DateTextBox_KeyPress(pTextBoxID, pButtonID, pStartRangeBoxID, pEndRangeBoxID, pEvent)
{
   var vShowIt = false;
   var vDateSeparatorCode = '46';
   var vKeyCode = (window.event) ? pEvent.keyCode : pEvent.which;
   if ((vKeyCode >= 97) && (vKeyCode <= 122))  // make uppercase
      vKeyCode = vKeyCode - 32;
   if (!document.getElementById && document.layers)
   {
      if ((vKeyCode == vDateSeparatorCode) || ((vKeyCode >= 48) && (vKeyCode <= 57)) || (vKeyCode < 30))
        vShowIt = true;
      return vShowIt;
   }
   var vKeyCodeStr = String.fromCharCode(vKeyCode);
   var vTextBoxFld = document.getElementById(pTextBoxID);
   if ((vKeyCode == vDateSeparatorCode) || ((vKeyCode >= 48) && (vKeyCode <= 57)) || (vKeyCode < 30))
      vShowIt = true;

   if (!vShowIt)
      pEvent.cancelBubble = true;
   return vShowIt;
}  // DateTextBox_KeyPress()

function DateTextBox_OnBlur(pTextBoxID, pUpdateStatus)
{
  if (pUpdateStatus)
     window.status = '';
  var vTextBoxFld = document.getElementById(pTextBoxID);
    if (vTextBoxFld.fireEvent != null) // IE 5.5 and higher
       vTextBoxFld.fireEvent('onChange');
    else if (document.createEvent != null) // Netscape
    {
       var vEvt = document.createEvent('HTMLEvents')
       vEvt.initEvent('change', true, false)
       vTextBoxFld.dispatchEvent(vEvt)
    }
    else    // anything else
    {
       var vOnChange = vTextBoxFld.getAttribute('onchange');
       var vOnChangeString = vOnChange.toString();
       var vIndex = vOnChangeString.indexOf('DateTextBox');
       vOnChangeString = vOnChangeString.substring(vIndex, vOnChangeString.length - 1);
       vIndex = vOnChangeString.indexOf(')');
       vOnChangeString = vOnChangeString.substring(0, vIndex + 1);
       eval(vOnChangeString + ';');
    }
} // DateTextBox_OnBlur()

function DateTextBox_OnChange(pTextBoxID, pStartRangeBoxID, pEndRangeBoxID)
{
  var vTextBoxFld = document.getElementById(pTextBoxID);
  if (vTextBoxFld.value == '') // blanks are valid and properly formed
     return;
  var vDate = GetDateValue(pTextBoxID);
  if (vDate != null)  // if null, its an error. Leave it for validation
  {
    SetDateValue(pTextBoxID, vDate, false);
    ApplyDateRangeRules(vDate, pStartRangeBoxID, pEndRangeBoxID);
  }
  else if (GetAttribute(vTextBoxFld, 'Custom_ShowErrorOnChange', false) == true)
     ShowDateFieldError(pTextBoxID);
} // DateTextBox_OnChange()

function ApplyDateRangeRules(pSourceDate, pStartRangeBoxID, pEndRangeBoxID)
{
  var vToday=new Date();
  if (pStartRangeBoxID != '')   // this control must be >= to pStartRangeBoxID
  {
     var vOtherDate = GetDateValue(pStartRangeBoxID);
     if ((vOtherDate != null) && (pSourceDate.valueOf() < vOtherDate.valueOf())) // if null, its an error. Leave it for validation
        SetDateValue(pStartRangeBoxID, pSourceDate, false);
  }
  if (pEndRangeBoxID != '')  // this control must be <= to pStartRangeBoxID
  {
     var vOtherDate = GetDateValue(pEndRangeBoxID);
     if ((vOtherDate != null) && (pSourceDate.valueOf() > vOtherDate.valueOf())) // if null, its an error. Leave it for validation
        SetDateValue(pEndRangeBoxID, pSourceDate, false);
  }
  if(pSourceDate<vToday){
     var vTomorrow=new Date(vToday.getTime()+1000*60*60*24);
     SetDateValue(pStartRangeBoxID, vToday, false);
     SetDateValue(pEndRangeBoxID, vTomorrow, false);
  }
} // ApplyDateRangeRules()


function DateTextBox_OnChange(pTextBoxID, pStartRangeBoxID, pEndRangeBoxID)
{
var vTextBoxFld = document.getElementById(pTextBoxID);
if (vTextBoxFld.value == '') // blanks are valid and properly formed
  return;
var vDate = GetDateValue(pTextBoxID);
if (vDate != null)  // if null, its an error. Leave it for validation
{
  SetDateValue(pTextBoxID, vDate, false);
  ApplyDateRangeRules(vDate, pStartRangeBoxID, pEndRangeBoxID);


  //webres
  if (pTextBoxID.indexOf("StartField")>-1){
  var vDaysInMonth = GetDaysInMonth(vDate.getMonth() + 1, vDate.getFullYear());
  if (vDate.getDate() < vDaysInMonth)
    vDate.setDate(vDate.getDate() + 1);
  else  // roll over month
  {
    vDate.setDate(1); // day 1 of the next month
    // NOTE: getMonth and setMonth use January = 0
    if (vDate.getMonth() < 11)
      vDate.setMonth(vDate.getMonth() + 1);
    else  // next year
    {
      vDate.setMonth(0);
      vDate.setFullYear(vDate.getFullYear() + 1);
    }  // if (vDate.getMonth())
  }  // if (vDate.getDate())
  SetDateValue(pEndRangeBoxID, vDate, true);
  }
  //webres, End
}
else {
  ShowDateFieldError(pTextBoxID);
}
} // DateTextBox_OnChange()

  function getElementsByClassName(class_name)
      {
        var all_obj,ret_obj=new Array(),j=0,teststr;
    if(document.all)all_obj=document.all;
        else if(document.getElementsByTagName && !document.all)
          all_obj=document.getElementsByTagName("div");

        for(i=0;i<all_obj.length;i++)
        {
          if(all_obj[i].className.indexOf(class_name)!=-1)
          {
            teststr=","+all_obj[i].className.split(" ").join(",")+",";
            if(teststr.indexOf(","+class_name+",")!=-1)
            {
              ret_obj[j]=all_obj[i];
              j++;
            }
          }
        }
        return ret_obj;
      }

  function layout(){
  }

            function quick(StartfieldID,DuefieldID, personenID,zimmerID,LANG){

              layout();var webresid='4994';
/*              var ContentNode=document.getElementById('centercenter');
              while(child=ContentNode.firstChild){
                ContentNode.removeChild(child);
              };
              var RightNode=document.getElementById('centerright');
              while(child=RightNode.firstChild){
                RightNode.removeChild(child);
              };
              var iframe = document.createElement("IFRAME");*/
              var vDate=GetDateValue(StartfieldID);
              var bDate=GetDateValue(DuefieldID);
              if(!vDate || !bDate){
                alert("Bitte überprüfen Sie Ihre Datumsangaben");
                return;
              }
              var persons=document.getElementById(personenID);
              var rooms=document.getElementById(zimmerID);
              var TempMonthS=vDate.getMonth()+1;
              if(TempMonthS.toString().length==1)TempMonthS="0"+TempMonthS;
              var TempMonthD=bDate.getMonth()+1;
              if(TempMonthD.toString().length==1)TempMonthD="0"+TempMonthD;
              var vDay=(vDate.getDate().toString().length==1)?"0"+vDate.getDate():vDate.getDate();
              var bDay=(bDate.getDate().toString().length==1)?"0"+bDate.getDate():bDate.getDate();
              var formsrc="http://www.hoteldaten.com/koop-apis/wdba/dorequest.php?webresid="+webresid+"&LANG=" + LANG + "&arrival=" + vDay + "." + TempMonthS + "." + vDate.getFullYear() + "&departure=" + bDay + "." + TempMonthD + "." + bDate.getFullYear() + "&persons=" + persons.options[persons.selectedIndex].value + "&rooms=" + rooms.options[rooms.selectedIndex].value
/*              ContentNode.appendChild(iframe);
              iframe.setAttribute("src",formsrc);
              iframe.setAttribute("border","0");iframe.setAttribute("frameborder","0");iframe.setAttribute("height","512");
              iframe.setAttribute("width","607");
              iframe.setAttribute("name","webres Quick Booker");
              iframe.setAttribute("id","webresiframe");*/
              if ( BuchungsFenster == null ) {
                BuchungsFenster = window.open(
                                     formsrc, "Buchungsanfrage", "toolbar=no, width=750, height=550, directories=no, status=yes,scrollbars=yes, resize=yes, menubar=no"
                                     );
              } else {
                if ( BuchungsFenster.closed ) {
                BuchungsFenster = window.open(
                                     formsrc, "Buchungsanfrage", "toolbar=no, width=750, height=550, directories=no, status=yes, scrollbars=yes, resize=yes, menubar=no"
                                     );
                } else {
                                     BuchungsFenster.location="http://www.hoteldaten.com/koop-apis/wdba/dorequest.php?webresid="+webresid+"&LANG=" + LANG + "&arrival=" + vDay + "." + TempMonthS + "." + vDate.getFullYear() + "&departure=" + bDay + "." + TempMonthD + "." + bDate.getFullYear() + "&persons=" + persons.options[persons.selectedIndex].value + "&rooms=" + rooms.options[rooms.selectedIndex].value
                                     BuchungsFenster.focus();
                }
              }
}

function setToday(start,end){
  now=new Date();
  if(GetDateValue(start))return;
  s=document.getElementById(start);
  day=now.getDate();
  if(day<10)day='0'+day;
  month=now.getMonth()+1;
  if(month<10)month='0'+month;
  year=now.getFullYear();
  s.value=day+'.'+month+'.'+year;
  DateTextBox_OnBlur(start,false)
}