DOMAssistant.DOMReady(loadFunctions);

var waiting=0; var calendarshown=0; var whichcalendar=""; var calendareventsdisable=0;
var cX = 0; var cY = 0; var rX = 0; var rY = 0;

function loadFunctions() {
if ($$("rentalcheckyes")) $$("rentalcheckyes").addEvent("click", showRental);
if ($$("rentalcheckno")) $$("rentalcheckno").addEvent("click", hideRental);
if ($$("guidecheckyes")) $$("guidecheckyes").addEvent("click", showGuide);
if ($$("guidecheckno")) $$("guidecheckno").addEvent("click", hideGuide);
if ($$("guidetype")) $$("guidetype").addEvent("change", checkGuide);
if ($$("groupinfo")) hideElement("groupinfo");
if (calendareventsdisable!=1) if ($$("calendar")) calendarAddEvents();
if ($$("calendar")) calendarAddNavEvents();
if ($$("linkaddress"))
   {
   $$("linkaddress").addEvent("click", toggleAddressSearch);
   if ($$("address").value=="") hideElement("addressbox");
   }
}

function setCookie(cookieName,cookieValue,nDays) {
var today=new Date();
var expire=new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime()+3600000*24*nDays);
document.cookie=cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString();
}

function toggleAddressSearch() {
if (isHidden("addressbox"))
   {
   $$("Xlocation1").checked=false;
   $$("Xlocation2").checked=false;
   $$("Xlocation3").checked=false;
   $$("Xlocation4").checked=false;
   showElement("addressbox");
   }
else
   {
   $$("address").value="";
   hideElement("addressbox");
   }
}

function showGroupInfo() {
if ($$("Xpersonnumber").selectedIndex<=6) hideElement("groupinfo");
if ($$("Xpersonnumber").selectedIndex>5)
   {
   showElement("groupinfo","inline");
   flashNewInfo("groupinfo");
   }
}

function changeCalendar(response) {
$$("calendar").replaceContent(response);
calendarAddNavEvents();
if (calendareventsdisable!=1)
   {
   calendarAddEvents();
   if(typeof calendarAddEventsCookies=="function") calendarAddEventsCookies();
   }
waiting=0;
return false;
}

function getNewMonth(hotelID,month,year,event) {
if (waiting==1) return false;
waiting=1;
$$("calendar").get("calendar.php?hotelID="+hotelID+"&calyear="+year+"&calmonth="+month, changeCalendar);
DOMAssistant.cancelBubble(event);
return false;
}

function changeFromDay() {
if ($$("fromday").selectedIndex>=$$("today").selectedIndex)
   {
   if ($$("frommonthyear").selectedIndex==$$("tomonthyear").selectedIndex) // same months
      {
      if ($$("today").selectedIndex==0)
         {
         $$("fromday").selectedIndex=30;
         $$("frommonthyear").selectedIndex=$$("tomonthyear").selectedIndex-1;
         }
      else if ($$("fromday").selectedIndex>=$$("today").selectedIndex)
         {
         $$("fromday").selectedIndex=$$("today").selectedIndex-1;
         }
      /*  buggy: more check needed: fromday set, frommonthyear set (months sync), setting today, changes fromday to today-1
      else
         {
         $$("fromday").selectedIndex=$$("today").selectedIndex-1;
         }
      */
      }
   else
      {
      if ($$("today").selectedIndex==0)
         {
         $$("tomonthyear").selectedIndex=$$("frommonthyear").selectedIndex+1;
         }
      }
   }
}

function changeToDay() {
if ($$("fromday").selectedIndex>=$$("today").selectedIndex && $$("frommonthyear").selectedIndex==$$("tomonthyear").selectedIndex)
   {
   // number of days 31
   if ($$("frommonthyear").selectedIndex==0 || $$("frommonthyear").selectedIndex==2 || $$("frommonthyear").selectedIndex==4 || $$("frommonthyear").selectedIndex==6 || $$("frommonthyear").selectedIndex==7 || $$("frommonthyear").selectedIndex==9 || $$("frommonthyear").selectedIndex==11)
      {
      if ($$("fromday").selectedIndex<=30) $$("today").selectedIndex=$$("fromday").selectedIndex+1;
      else
         {
         $$("today").selectedIndex=0;
         $$("tomonthyear").selectedIndex=$$("tomonthyear").selectedIndex+1;
         }
      }
   // number of days 30
   else if ($$("frommonthyear").selectedIndex==3 || $$("frommonthyear").selectedIndex==5 || $$("frommonthyear").selectedIndex==8 || $$("frommonthyear").selectedIndex==10)
      {
      if ($$("fromday").selectedIndex<=29) $$("today").selectedIndex=$$("fromday").selectedIndex+1;
      else
         {
         $$("today").selectedIndex=0;
         $$("tomonthyear").selectedIndex=$$("tomonthyear").selectedIndex+1;
         }
      }
   else if ($$("frommonthyear").selectedIndex==1) // february + leap year algo
      {
      isLeap=new Date($$("frommonthyear").value.substr(3,4),1,29).getDate(); // returns 29 for leap year
      if (isLeap==29) // leap year
         {
         if ($$("fromday").selectedIndex<=28) $$("today").selectedIndex=$$("fromday").selectedIndex+1;
         else
            {
            $$("today").selectedIndex=0;
            $$("tomonthyear").selectedIndex=$$("tomonthyear").selectedIndex+1;
            }
         }
      else // not a leap year
         {
         if ($$("fromday").selectedIndex<=27) $$("today").selectedIndex=$$("fromday").selectedIndex+1;
         else
            {
            $$("today").selectedIndex=0;
            $$("tomonthyear").selectedIndex=$$("tomonthyear").selectedIndex+1;
            }
         }
      }
   }
}

function changeToMonthYear() {
$$("tomonthyear").selectedIndex=$$("frommonthyear").selectedIndex;
}

function changeFromMonthYear() {
if (($$("frommonthyear").value.substr(0,2)>$$("tomonthyear").value.substr(0,2) && $$("frommonthyear").value.substr(3,4)==$$("tomonthyear").value.substr(3,4)) || $$("frommonthyear").value.substr(3,4)>$$("tomonthyear").value.substr(3,4))
   {
   $$("frommonthyear").value=$$("tomonthyear").value;
   }
}

function checkCalendarFrom(event) {
whichcalendar="from";
checkCalendar(event);
}

function checkCalendarTo(event) {
whichcalendar="to";
checkCalendar(event);
}

function checkCalendar(event) {
if (isHidden("calendar"))
   {
   assignPosition("calendar",event);
   showElement("calendar");
   calendarshown=1;
   }
else
   {
   hideElement("calendar");
   calendarshown=0;
   }
DOMAssistant.cancelBubble(event);
}

function calendarAddEvents() {
days=$$("calendar").elmsByTag("td");
days.each( function () { if ($(this).className && $(this).className!="nav" && $(this).className!="notbook" && $(this).className!="full") { $(this).addEvent("click", changeDay); $(this).style.cursor="pointer"; } } );
}

function calendarAddNavEvents(event) {
if ($("a[id^=prevmonth]"))
   {
   $("a[id^=prevmonth]").addEvent("click", function (event) { pararray=$(this).id.split("-"); getNewMonth(pararray[1],pararray[2],pararray[3],event); } );
   }
if ($("a[id^=nextmonth]"))
   {
   $("a[id^=nextmonth]").addEvent("click", function (event) { pararray=$(this).id.split("-");  getNewMonth(pararray[1],pararray[2],pararray[3],event); } );
   }
}

function changeDay() {
if ($$(whichcalendar+"day").options.length==31) $$(whichcalendar+"day").selectedIndex=parseInt(this.innerHTML-1);
else $$(whichcalendar+"day").selectedIndex=parseInt(this.innerHTML*1);
changeMonthYear(whichcalendar);
hideElement("calendar");
}

function changeMonthYear(whichcalendar) {
monthyear=$$("calendar").elmsByTag("span");
monthyear=monthyear[0].innerHTML;
for (i=0; i<$$(whichcalendar+"monthyear").length; i++)
   {
   if ($$(whichcalendar+"monthyear")[i].value==monthyear)
      {
      selected=$$(whichcalendar+"monthyear")[i].index;
      break;
      }
   }
$$(whichcalendar+"monthyear").selectedIndex=selected;
if (whichcalendar=="from")
   {
   changeToDay(); changeToMonthYear();
   }
if (whichcalendar=="to")
   {
   changeFromMonthYear();
   }
}

function showElement(element,typeX) {
if (typeX==null) typeX="block";
$$(element).style.display=typeX;
}

function hideElement(element) {
$$(element).style.display="none";
}

function hideCalendar() {
if (calendarshown==1) hideElement("calendar");
}

function showTransfer() {
showElement("transferfield");
flashNewInfo("transferfield");
}

function hideTransfer() {
hideElement("transferfield");
}

function showRental() {
showElement("rentalfield");
flashNewInfo("rentalfield");
}

function hideRental() {
hideElement("rentalfield");
}

function showGuide() {
showElement("guidefield");
flashNewInfo("guidefield");
}

function hideGuide() {
hideElement("guidefield");
}

function checkGuide() {
if ($$("guidetype").value>=4 && $$("guidetype").value<=6)
   {
   hideElement("guidelocationfield");
   }
else
   {
   showElement("guidelocationfield");
   flashNewInfo("guidelocationfield");
   }
}

function isHidden(element) {
if ($$(element).style.display=="none" || $$(element).style.display=="") return 1;
else return 0;
}

function flashNewInfo(element) {
setTimeout('$$("'+element+'").style.backgroundColor="#ffff99"',100);
setTimeout('$$("'+element+'").style.backgroundColor="#ffffaa"',200);
setTimeout('$$("'+element+'").style.backgroundColor="#ffffbb"',300);
setTimeout('$$("'+element+'").style.backgroundColor="#ffffcc"',400);
setTimeout('$$("'+element+'").style.backgroundColor="#ffffdd"',500);
setTimeout('$$("'+element+'").style.backgroundColor="#ffffee"',600);
setTimeout('$$("'+element+'").style.backgroundColor="#ffffff"',700);
}

function assignPosition(element,event) {
cX=event.clientX; cY=event.clientY;
if ($$(element).pageYOffset)
   {
   rX=$$(element).pageXOffset;
   rY=$$(element).pageYOffset;
   }
if (document.body)
   {
   rX=document.body.scrollLeft;
   rY=document.body.scrollTop;
   }
if (document.documentElement && document.documentElement.scrollTop)
   {
   rX=document.documentElement.scrollLeft;
   rY=document.documentElement.scrollTop;
   }
cX+=rX;
cY+=rY;
$$(element).style.left=cX+"px";
$$(element).style.top=cY+"px";
}