//*****************************************************************************************************
//*** Author:     Ruediger von Creytz     ruediger.creytz@globalbit.net                             ***
//*** Copyright:  globalBIT, LLC          http://www.globalbit.net                                  ***
//*** Version:    20080709                                                                          ***
//*****************************************************************************************************


//*****************************************************************************************************
//*** Implement following lines into the head of your template: ***************************************
//*****************************************************************************************************
/*

<script src="JS/calhelp.js" type="text/javascript"></script>

*/
//*****************************************************************************************************
//*** Implement following lines into the very end of your template's body: ****************************
//*****************************************************************************************************
/*

<script type="text/javascript">
  <!--
    inputToCalhelp('ACTUAL_INPUT_ID');
  //-->
</script>

*/
//*****************************************************************************************************
//*****************************************************************************************************

var calhelpArrowLeft = "arrow_left_16x16.gif";
var calhelpArrowRight = "arrow_right_16x16.gif";
var calhelpArrowSize = 16;
var calhelpBgColor = "#ffffff";
var calhelpBgColorMonth = "#f0f0f0";
var calhelpBgColorWeek = "#e6e6e6";
var calhelpBorderColor = "#999999";
var calhelpButtonBgColor = "#e6e6e6";
var calhelpButtonImage = "calendar_16x16.gif";
var calhelpCellWidth = 18;
var calhelpColor = "#000000";
var calhelpColorToday = "#ff0000";
var calhelpColorTodayFocused = "#ff0000";
var calhelpCursor = '';
var calhelpDate = new Date();
var calhelpDayNames = new Array("MO", "DI", "MI", "DO", "FR", "SA", "SO");
var calhelpDays = new Array();
var calhelpDivField = null;
var calhelpFontSize = 12;
var calhelpFocusedBgColor = "#0a246a";
var calhelpFocusedColor = "#ffffff";
var calhelpFocusedID = null;
var calhelpFocusedNum = null;
var calhelpFontFamily = "sans-serif";
var calhelpFontSize = 12;
var calhelpHeight = 18;
var calhelpMonthNames = new Array("Januar", "Februar", "M&auml;rz", "April", "Mai", "Juni",
                      "Juli", "August", "September", "Oktober", "November", "Dezember");
var calhelpMouseover = false;
var calhelpScrollTimer = false;
var calhelpTimer = false;
var calhelpToday = new Date();
var calhelpes = new Array();

var calhelpMonth = calhelpDate.getMonth();
var calhelpYear = calhelpDate.getFullYear();

if(window.event) {
  calhelpCursor = 'hand';
}
else {
  calhelpCursor = 'pointer';
}

//*****************************************************************************************************

function calhelpInit(id) {
  if(id == null || id == "")
    return;
  document.getElementById(id+'-month-year').innerHTML = calhelpMonthNames[calhelpMonth]
  + " " + calhelpYear;
  var theDate = new Date(calhelpYear, calhelpMonth, 1);
  var theDateSelected = getFocusedDateCalhelp(id);
  if(theDateSelected == null)
    theDateSelected = getCalhelpSelectedValue(id);
  var theNumSelected = null;
  var theWeekField = 0;
  while(theDate.getDay() != 1)
    calhelpSubtractDay(theDate);
  for(var i=0; i<42; i++) {
    var enabled = false;
    var today = false;
    if(theDate.getDay() == 1) {
      document.getElementById(id+'-week'+theWeekField).innerHTML = calhelpWeek(theDate);
      theWeekField++;
    }
    if(theDateSelected != null
    && theDateSelected.getDate() == theDate.getDate()
    && theDateSelected.getMonth() == theDate.getMonth()
    && theDateSelected.getFullYear() == theDate.getFullYear()) {
      theNumSelected = i;
    }
    var dayElem = document.getElementById(id+'-day'+i);
    if(theDate.getMonth() == calhelpMonth) {
      dayElem.innerHTML = theDate.getDate();
      dayElem.style.cursor = calhelpCursor;
      if(calhelpToday.getDate() == theDate.getDate()
      && calhelpToday.getMonth() == theDate.getMonth()
      && calhelpToday.getFullYear() == theDate.getFullYear()) {
        dayElem.style.color = calhelpColorToday;
        dayElem.style.fontWeight = "bold";
        today = true;
      }
      else {
        dayElem.style.color = "#000000";
        dayElem.style.fontWeight = "normal";
      }
      enabled = true;
    }
    else {
      dayElem.innerHTML = theDate.getDate();
      dayElem.style.color = "#aaaaaa";
      dayElem.style.cursor = "default";
      dayElem.style.fontWeight = "normal";
      enabled = false;
    }
    calhelpDays[i] = new Array(enabled, theDate.getFullYear(), theDate.getMonth(), theDate.getDate(), today);
    calhelpAddDay(theDate);
  }
  setFocusedCalhelp(id, theNumSelected);
}

//*****************************************************************************************************

function calhelpMonthAfter(id) {
  if(id == null || id == "")
    return false;
  unsetFocusedCalhelp(id);
  if(calhelpMonth == 11) {
    calhelpMonth = 0;
    calhelpYear++;
  }
  else
    calhelpMonth++;
  calhelpInit(id);
  return false;
}

//*****************************************************************************************************

function calhelpMonthBefore(id) {
  if(id == null || id == "")
    return false;
  unsetFocusedCalhelp(id);
  if(calhelpMonth == 0) {
    calhelpMonth = 11;
    calhelpYear--;
  }
  else
    calhelpMonth--;
  calhelpInit(id);
  return false;
}

//*****************************************************************************************************

function calhelpWeek(theDate) {
  var myDate = new Date(theDate.getFullYear(), 0, 1);
  var theWeek = 0;
  if(myDate.getDay() < 4) {
    while(myDate.getDay() != 1)
      calhelpSubtractDay(myDate);
  }
  else {
    while(myDate.getDay() != 1)
      calhelpAddDay(myDate);
  }
  while(myDate.getTime() <= theDate.getTime()) {
    calhelpAddWeek(myDate);
    theWeek++;
  }
  calhelpSubtractWeek(myDate);
  if(myDate.getMonth() == 11 && myDate.getDate() >= 29)
    theWeek = 1;
  return theWeek;
}

//*****************************************************************************************************

function calhelpAddDay(theDate) {
  var theDay = theDate.getDate();
  var theMonth = theDate.getMonth();
  var theYear = theDate.getFullYear();
  theDate.setTime(theDate.getTime() + 86400000);
  if(theMonth == theDate.getMonth()
  && theYear == theDate.getFullYear()
  && (theDay+1) != theDate.getDate())
    theDate.setDate(theDay+1);
}

//*****************************************************************************************************

function calhelpAddWeek(theDate) {
  for(var i=0; i<7; i++)
    calhelpAddDay(theDate);
}

//*****************************************************************************************************

function calhelpSubtractDay(theDate) {
  var theDay = theDate.getDate();
  var theMonth = theDate.getMonth();
  var theYear = theDate.getFullYear();
  theDate.setTime(theDate.getTime() - 86400000);
  if(theMonth == theDate.getMonth()
  && theYear == theDate.getFullYear()
  && (theDay-1) != theDate.getDate())
    theDate.setDate(theDay-1);
}

//*****************************************************************************************************

function calhelpSubtractWeek(theDate) {
  for(var i=0; i<7; i++)
    calhelpSubtractDay(theDate);
}

//*****************************************************************************************************

/**
 * Avoids doing the browser's default actions.
 * @param event The event to avoid.
 * @return FALSE.
 */
function getFalseCalhelp(event) {
  if(event.preventDefault)
    event.preventDefault();
  if(event.returnValue)
    event.returnValue = false;
  return false;
}

//*****************************************************************************************************

/**
 * Returns the focused calhelp entry.
 * @param id ID of the calhelp to make sure the program is working on the correct one.
 * @return Number of the focused calhelp entry or NULL.
 */
function getFocusedCalhelp(id) {
  if(id == null || id == "")
    return null;
  if(calhelpFocusedID == id)
    return calhelpFocusedNum;
  else
    return null;
}

//*****************************************************************************************************

/**
 * Returns the focused calhelp entry.
 * @param id ID of the calhelp to make sure the program is working on the correct one.
 * @return Number of the focused calhelp entry or NULL.
 */
function getFocusedDateCalhelp(id) {
  var theNum = getFocusedCalhelp(id);
  if(theNum == null)
    return null;
  return new Date(calhelpDays[theNum][1], calhelpDays[theNum][2], calhelpDays[theNum][3]);
}

//*****************************************************************************************************

/**
 * Returns the selected value.
 * @param id ID of the calhelp.
 * @return The selected value.
 */
function getCalhelpSelectedValue(id) {
  if(id == null || id == "")
    return null;
  var theValue = document.getElementById(id).value.split('.');
  var theDate;
  if(theValue.length == 3) {
    try {
      theDate = new Date(theValue[2], (theValue[1]-1), theValue[0]);
      theDate.setTime(theDate.getTime());
      if(theDate.getDate() == (theValue[0]*1)
      && theDate.getMonth() == ((theValue[1]*1)-1)
      && theDate.getFullYear() == (theValue[2]*1))
        return theDate;
    }
    catch(e) {
      //do nothing
    }
  }
  return null;
}

//*****************************************************************************************************

/**
 * Hides the calhelp entries.
 * @param id ID of the calhelp.
 */
function hideCalhelp(id) {
  if(id == null || id == "")
    return;
  var theDiv = document.getElementById(id+'-div');
  theDiv.style.display = "none";
  theDiv.style.visibility = "hidden";
  unsetFocusedCalhelp(id);
}

//*****************************************************************************************************

/**
 * For the event ONBLUR.
 * @param id ID of the calhelp.
 * @return TRUE if possible otherwise FALSE.
 */
function onblurCalhelp(id) {
  if(id == null || id == "")
    return false;
  calhelpTimer = window.setTimeout("onblurCalhelpDelayed('" + id + "', false)", 5);
  return true;
}

//*****************************************************************************************************

/**
 * To do delayed by event ONBLUR.
 * @param id ID of the calhelp.
 * @param returnKey FALSE if mouse must be over an entry for setting the entry as selected.
 */
function onblurCalhelpDelayed(id, returnKey) {
  if(id == null || id == "")
    return;
  if(getFocusedCalhelp(id) == null || (!returnKey && !calhelpMouseover)) {
    if(calhelpDivField == null || isNaN(calhelpDivField))
      hideCalhelp(id);
  }
  else {
    setCalhelp(id, getFocusedCalhelp(id));
  }
  calhelpMouseover = false;
}

//*****************************************************************************************************

/**
 * For the event ONCHANGE of the text input field.
 * @param id ID of the calhelp.
 */
function onchangeCalhelp(id) {
  if(id == null || id == "")
    return;
  var theDate = getCalhelpSelectedValue(id)
  if(theDate == null)
    document.getElementById(id).value = '';
  else
    document.getElementById(id).value = theDate.getDate() + '.' + (theDate.getMonth()+1)
    + '.' + theDate.getFullYear();
}

//*****************************************************************************************************

/**
 * For the event ONCHANGE of the text input field.
 * @param id ID of the calhelp.
 */
function onclickCalhelp(id, num, divField) {
  if(id == null || id == "")
    return false;
  if(num != null && !isNaN(num) && calhelpDays.length > num && calhelpDays[num][0] == true)
    setCalhelp(id, num);
  switch(divField) {
    case 0:
      calhelpMonthBefore(id);
      return false;
    case 2:
      calhelpMonthAfter(id);
      return false;
  }
  document.getElementById(id+'-button').focus();
  return false;
}

//*****************************************************************************************************

/**
 * For the event ONKEYDOWN of the text input field.
 * @param event The browser event.
 * @param id ID of the calhelp.
 * @return FALSE.
 */
function onkeydownCalhelp(event, id) {
//  var calhelp = getCalhelp(id);
  if(id == null || id == "")
    return false;
  calhelpTimer = false;
  var theNum = null;
  var theKeyCode;
  if(window.captureEvents)
    theKeyCode = event.which;
  else
    theKeyCode = event.keyCode;
  switch(theKeyCode) {
    case 13:   //return
      if(!calhelpScrollTimer) {
        if(document.addEventListener) {
          document.addEventListener("keydown", getFalseCalhelp, false);
          document.addEventListener("keypress", getFalseCalhelp, false);
          document.addEventListener("keyup", getFalseCalhelp, false);
        }
        if(document.attachEvent)
          document.attachEvent("onkeydown", getFalseCalhelp);
        calhelpScrollTimer = window.setTimeout("scrollTopCalhelp('"
        + window.document.documentElement.scrollTop + "')", 10);
        if(getFocusedCalhelp(id) == null)
          toggleCalhelp(id);
        else
          onblurCalhelpDelayed(id, true);
      }
      break;
    case 16:   //shift
      break;
    case 27:   //esc
      hideCalhelp(id);
      break;
    case 32:   //space
      break;
    case 37:   //arrow left
      if(!calhelpScrollTimer) {
        if(document.addEventListener) {
          document.addEventListener("keydown", getFalseCalhelp, false);
          document.addEventListener("keypress", getFalseCalhelp, false);
          document.addEventListener("keyup", getFalseCalhelp, false);
        }
        if(document.attachEvent)
          document.attachEvent("onkeydown", getFalseCalhelp);
        calhelpScrollTimer = window.setTimeout("scrollTopCalhelp('"
        + window.document.documentElement.scrollTop + "')", 10);
        if(document.getElementById(id+'-div').style.visibility != 'visible')
          toggleCalhelp(id);
        else if(getFocusedCalhelp(id) == null) {
          for(var i=calhelpDays.length-1; i>=0; i--) {
            if(calhelpDays[i][0]) {
              calhelpFocusedNum = i - 1;
              break;
            }
          }
          setFocusedCalhelp(id, calhelpFocusedNum+1);
        }
        else {
          var theDate = getFocusedDateCalhelp(id);
          var theMonth = theDate.getMonth();
          calhelpSubtractDay(theDate);
          if(theMonth != theDate.getMonth()) {
            calhelpMonthBefore(id);
            for(var i=calhelpDays.length-1; i>=0; i--) {
              if(calhelpDays[i][0]) {
                calhelpFocusedNum = i + 1;
                break;
              }
            }
          }
          setFocusedCalhelp(id, calhelpFocusedNum-1);
        }
      }
      break;
    case 38:   //arrow up
      if(!calhelpScrollTimer) {
        if(document.addEventListener) {
          document.addEventListener("keydown", getFalseCalhelp, false);
          document.addEventListener("keypress", getFalseCalhelp, false);
          document.addEventListener("keyup", getFalseCalhelp, false);
        }
        if(document.attachEvent)
          document.attachEvent("onkeydown", getFalseCalhelp);
        calhelpScrollTimer = window.setTimeout("scrollTopCalhelp('"
        + window.document.documentElement.scrollTop + "')", 10);
        if(document.getElementById(id+'-div').style.visibility != 'visible')
          toggleCalhelp(id);
        else {
          if(getFocusedCalhelp(id) == null) {
            for(var i=calhelpDays.length-1; i>=0; i--) {
              if(calhelpDays[i][0]) {
                calhelpFocusedNum = i + 7;
                break;
              }
            }
          }
          else {
            var theDate = getFocusedDateCalhelp(id);
            var theMonth = theDate.getMonth();
            calhelpSubtractWeek(theDate);
            var theDay = theDate.getDate();
            if(theMonth != theDate.getMonth()) {
              calhelpMonthBefore(id);
              for(var i=calhelpDays.length-1; i>=0; i--) {
                if(calhelpDays[i][0] && calhelpDays[i][3] == theDay) {
                  calhelpFocusedNum = i + 7;
                  break;
                }
              }
            }
          }
          setFocusedCalhelp(id, calhelpFocusedNum-7);
        }
      }
      break;
    case 39:   //arrow right
      if(!calhelpScrollTimer) {
        if(document.addEventListener) {
          document.addEventListener("keydown", getFalseCalhelp, false);
          document.addEventListener("keypress", getFalseCalhelp, false);
          document.addEventListener("keyup", getFalseCalhelp, false);
        }
        if(document.attachEvent)
          document.attachEvent("onkeydown", getFalseCalhelp);
        calhelpScrollTimer = window.setTimeout("scrollTopCalhelp('"
        + window.document.documentElement.scrollTop + "')", 10);
        if(document.getElementById(id+'-div').style.visibility != 'visible')
          toggleCalhelp(id);
        else {
          if(getFocusedCalhelp(id) == null) {
            for(var i=0; i<calhelpDays.length; i++) {
              if(calhelpDays[i][0]) {
                calhelpFocusedNum = i - 1;
                break;
              }
            }
          }
          else {
            var theDate = getFocusedDateCalhelp(id);
            var theMonth = theDate.getMonth();
            calhelpAddDay(theDate);
            if(theMonth != theDate.getMonth()) {
              calhelpMonthAfter(id);
              for(var i=0; i<calhelpDays.length; i++) {
                if(calhelpDays[i][0]) {
                  calhelpFocusedNum = i - 1;
                  break;
                }
              }
            }
          }
          setFocusedCalhelp(id, calhelpFocusedNum+1);
        }
      }
      break;
    case 40:   //arrow down
      if(!calhelpScrollTimer) {
        if(document.addEventListener) {
          document.addEventListener("keydown", getFalseCalhelp, false);
          document.addEventListener("keypress", getFalseCalhelp, false);
          document.addEventListener("keyup", getFalseCalhelp, false);
        }
        if(document.attachEvent)
          document.attachEvent("onkeydown", getFalseCalhelp);
        calhelpScrollTimer = window.setTimeout("scrollTopCalhelp('"
        + window.document.documentElement.scrollTop + "')", 10);
        if(document.getElementById(id+'-div').style.visibility != 'visible')
          toggleCalhelp(id);
        else {
          if(getFocusedCalhelp(id) == null) {
            for(var i=0; i<calhelpDays.length; i++) {
              if(calhelpDays[i][0]) {
                calhelpFocusedNum = i - 7;
                break;
              }
            }
          }
          else {
            var theDate = getFocusedDateCalhelp(id);
            var theMonth = theDate.getMonth();
            calhelpAddWeek(theDate);
            var theDay = theDate.getDate();
            if(theMonth != theDate.getMonth()) {
              calhelpMonthAfter(id);
              for(var i=0; i<calhelpDays.length; i++) {
                if(calhelpDays[i][0] && calhelpDays[i][3] == theDay) {
                  calhelpFocusedNum = i - 7;
                  break;
                }
              }
            }
          }
          setFocusedCalhelp(id, calhelpFocusedNum+7);
        }
      }
      break;
    case 65:   //a
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "a");
    case 66:   //b
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "b");
    case 67:   //c
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "c");
    case 68:   //d
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "d");
    case 69:   //e
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "e");
    case 70:   //f
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "f");
    case 71:   //g
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "g");
    case 72:   //h
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "h");
    case 73:   //i
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "i");
    case 74:   //j
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "j");
    case 75:   //k
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "k");
    case 76:   //l
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "l");
    case 77:   //m
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "m");
    case 78:   //n
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "n");
    case 79:   //o
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "o");
    case 80:   //p
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "p");
    case 81:   //q
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "q");
    case 82:   //r
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "r");
    case 83:   //s
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "s");
    case 84:   //t
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "t");
    case 85:   //u
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "u");
    case 86:   //v
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "v");
    case 87:   //w
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "w");
    case 88:   //x
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "x");
    case 89:   //y
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "y");
    case 90:   //z
      if(theNum == null)
        theNum = getCalhelpNumByFirstValueChar(id, "z");
  }
  if(theNum != null)
    onmouseoverCalhelp(id, theNum);
  return false;
}

//*****************************************************************************************************

/**
 * For the event ONMOUSEOUT.
 * @param id ID of the calhelp.
 * @param num Number of the focused entry.
 * @return TRUE.
 */
function onmouseoutCalhelp(id, num, divField) {
  if(id == null || id == "")
    return true;
  if(num != null && calhelpDays.length > num && calhelpDays[num][0])
    calhelpMouseover = false;
  if(divField != null && !isNaN(divField))
    calhelpDivField = null;
  return true;
}

//*****************************************************************************************************

/**
 * For the event ONMOUSEOVER.
 * @param id ID of the calhelp.
 * @param num Number of the focused entry.
 * @return TRUE.
 */
function onmouseoverCalhelp(id, num, divField) {
  calhelpTimer = false;
  if(id == null || id == "")
    return true;
  if(num != null && calhelpDays.length > num && calhelpDays[num][0]) {
    setFocusedCalhelp(id, num);
    calhelpMouseover = true;
  }
  if(divField != null && !isNaN(divField))
    calhelpDivField = divField;
  return true;
}

//*****************************************************************************************************

/**
 * Opens the entries for the calhelp under text input field and button.
 * @param id ID of the calhelp.
 */
function openCalhelpDown(id) {
  var theBox = document.getElementById(id+'-box');
  var theDiv = document.getElementById(id+'-div');
  var theTab = document.getElementById(id+'-tab');
  theDiv.style.display = "block";
  if(theTab.style.borderTopWidth == "1px") {
    theTab.style.borderTopWidth = "0px";
    theTab.style.borderBottomWidth = "1px";
    if(window.event) {
      theDiv.style.top = theDiv.offsetTop + theBox.offsetHeight + theDiv.offsetHeight;
    }
    else
      theDiv.style.top = (theDiv.offsetTop + theBox.offsetHeight + theDiv.offsetHeight) + 'px';
  }
  theDiv.style.visibility = 'visible';
}

//*****************************************************************************************************

/**
 * Opens the entries for the calhelp over text input field and button.
 * @param id ID of the calhelp.
 */
function openCalhelpUp(id) {
  var theBox = document.getElementById(id+'-box');
  var theDiv = document.getElementById(id+'-div');
  var theTab = document.getElementById(id+'-tab');
  theDiv.style.display = "block";
  if(theTab.style.borderBottomWidth != "0px") {
    theTab.style.borderTopWidth = "1px";
    theTab.style.borderBottomWidth = "0px";
    if(window.event)
      theDiv.style.top = theDiv.offsetTop - theBox.offsetHeight - theDiv.offsetHeight;
    else
      theDiv.style.top = (theDiv.offsetTop - theBox.offsetHeight - theDiv.offsetHeight) + 'px';
  }
  theDiv.style.visibility = 'visible';
}

//*****************************************************************************************************

/**
 * Avoids the default scrolling of the browser.
 * @param scroll
 */
function scrollTopCalhelp(scroll) {
  window.document.documentElement.scrollTop = scroll;
  if(document.removeEventListener) {
    document.removeEventListener("keydown", getFalseCalhelp, false);
    document.removeEventListener("keypress", getFalseCalhelp, false);
    document.removeEventListener("keyup", getFalseCalhelp, false);
  }
  if(document.detachEvent)
    document.detachEvent("onkeydown", getFalseCalhelp);
  calhelpScrollTimer = false;
}

//*****************************************************************************************************

/**
 * Builds the HTML-code for a new calhelp and replaces a select box with it.
 * @param id ID of select box and new calhelp.
 * @param width Width of the select box in px as number.
 */
function inputToCalhelp(id, width) {
  if(id == null || id == "")
    return;
  var oldInput = document.getElementById(id);
  var theParent = oldInput.parentNode;
  width = width + 4;
  var htmlString = ''
  + '<table border="0" cellpadding="0" cellspacing="0"'
  +   ' style="border-spacing:0px;height:'+calhelpHeight+'px;padding:0px">'
  +   '<tr>'
  +     '<td style="border-spacing:0px;height:'+calhelpHeight+'px;overflow:visible;padding:0px">'
  +       '<table border="0"'
  +         ' cellpadding="0"'
  +         ' cellspacing="1"'
  +         ' id="'+id+'-box"'
  +         ' style="background-color:'+calhelpBorderColor+';border-spacing:1px;margin:0px;padding:0px">'
  +         '<tr>'
  +           '<td style="background-color:'+calhelpBgColor+';border-spacing:0px;'
  +             'font-family:'+calhelpFontFamily+';font-size:'+calhelpFontSize+'px;'
  +             'height:'+(calhelpHeight-2)+'px;padding:0px;vertical-align:middle">'
  +             '<div style="overflow:hidden;height:'+(calhelpHeight-2)+'px;padding:0px">'
  +               '<input id="' + id + '"'
  +                 ' name="' + id + '"'
  +                 ' onchange="onchangeCalhelp(' + "'" + id + "'" +')"'
  +                 ' style="background-color:'+calhelpBgColor+';border:0px;color:'+calhelpColor+';'
  +                   'font-family:'+calhelpFontFamily+';font-size:'+calhelpFontSize+'px;'
  +                   'height:'+(calhelpHeight-2)+'px;width:'+(width-calhelpHeight-3)+'px"'
  +                 ' type="text"'
  +                 ' value="'+document.getElementById(id).value+'"/>'
  +             '</div>'
  +           '</td>'
  +           '<td style="background-color:'+calhelpButtonBgColor+';border-spacing:0px;'
  +             'font-family:'+calhelpFontFamily+';font-size:'+calhelpFontSize+'px;'
  +             'height:'+(calhelpHeight-2)+'px;padding:0px">'
  +             '<div style="overflow:hidden;height:'+(calhelpHeight-2)+'px;padding:0px;'
  +               'width:'+(calhelpHeight-2)+'px">'
  +               '<input id="' + id + '-button"'
  +                 ' onblur="onblurCalhelp('+"'"+id+"'"+')"'
  +                 ' onclick="toggleCalhelp('+"'"+id+"'"+')"'
  +                 ' onkeydown="onkeydownCalhelp(event, ' + "'" + id + "'" +')"'
  +                 ' style="background-color:'+calhelpButtonBgColor+';'
  +                   'background-image:url('+calhelpButtonImage+');border:0px;border-spacing:0px;'
  +                   'cursor:'+calhelpCursor+';'
  +                   'height:'+(calhelpHeight-2)+'px;padding:0px;width:'+(calhelpHeight-2)+'px"'
  +                 ' type="button"/>'
  +             '</div>'
  +           '</td>'
  +         '</tr>'
  +       '</table>'
  +       '<div id="'+id+'-div"'
  +         ' style="border-spacing:0px;display:none;padding:0px;position:absolute;'
  +           'visibility:hidden;z-index:90"'
  +         ' onkeydown="onkeydownCalhelp(event, ' + "'" + id + "'" +')">'
  +         '<table border="1" cellpadding="0" cellspacing="0" id="' + id + '-tab"'
  +           ' style="background-color:'+calhelpBorderColor+';'
  +             'border:1px solid '+calhelpBorderColor+';border-spacing:0px;border-top-width:0px;'
  +             'padding:0px">'
  +           '<tr>'
  +             '<td colspan="8"'
  +               ' style="background-color:'+calhelpBgColorMonth+';'
  +                 'border:0px;border-bottom:1px solid '+calhelpBorderColor+';'
  +                 'font-family:'+calhelpFontFamily+';font-size:'+calhelpFontSize+'px;'
  +                 'padding:0px">'
  +               '<table border="0" cellpadding="0" cellspacing="0" width="100%">'
  +                 '<tr>'
  +                   '<td style="background-color:'+calhelpBgColorMonth+';border:0px;'
  +                       'font-family:'+calhelpFontFamily+';font-size:'+calhelpFontSize+'px;'
  +                       'padding:0px;width:'+calhelpArrowSize+'px">'
  +                     '<div style="overflow:hidden;height:'+calhelpArrowSize+'px;padding:0px;'
  +                       'width:'+calhelpArrowSize+'px">'
  +                       '<input'
  +                         ' onblur="onblurCalhelp('+"'"+id+"'"+')"'
  +                         ' onclick="onclickCalhelp('+"'"+id+"'"+', null, 0)"'
  +                         ' onmouseout="onmouseoutCalhelp(' + "'" + id + "'" + ', null, 0)"'
  +                         ' onmouseover="onmouseoverCalhelp(' + "'" + id + "'" + ', null, 0)"'
  +                         ' style="background-color:'+calhelpBgColorMonth+';'
  +                           'background-image:url('+calhelpArrowLeft+');border:0px;border-spacing:0px;'
  +                           'cursor:'+calhelpCursor+';'
  +                           'height:'+(calhelpHeight-2)+'px;padding:0px;width:'+(calhelpHeight-2)+'px"'
  +                         ' type="button"/>'
  +                     '</div>'
  +                   '</td>'
  +                   '<td align="center" id="' + id + '-month-year"'
  +                     ' onclick="onclickCalhelp('+"'"+id+"'"+', null, 1)"'
  +                     ' onmouseout="onmouseoutCalhelp(' + "'" + id + "'" + ', null, 1)"'
  +                     ' onmouseover="onmouseoverCalhelp(' + "'" + id + "'" + ', null, 1)"'
  +                     ' style="background-color:'+calhelpBgColorMonth+';border:0px;'
  +                       'cursor:default;'
  +                       'font-family:'+calhelpFontFamily+';font-size:'+calhelpFontSize+'px;'
  +                       'padding:2px">&#160;</td>'
  +                   '<td style="background-color:'+calhelpBgColorMonth+';border:0px;'
  +                       'font-family:'+calhelpFontFamily+';font-size:'+calhelpFontSize+'px;'
  +                       'padding:0px;width:'+calhelpArrowSize+'px">'
  +                     '<div style="overflow:hidden;height:'+calhelpArrowSize+'px;padding:0px;'
  +                       'width:'+calhelpArrowSize+'px">'
  +                       '<input'
  +                         ' onblur="onblurCalhelp('+"'"+id+"'"+')"'
  +                         ' onclick="onclickCalhelp('+"'"+id+"'"+', null, 2)"'
  +                         ' onmouseout="onmouseoutCalhelp(' + "'" + id + "'" + ', null, 2)"'
  +                         ' onmouseover="onmouseoverCalhelp(' + "'" + id + "'" + ', null, 2)"'
  +                         ' style="background-color:'+calhelpBgColorMonth+';'
  +                           'background-image:url('+calhelpArrowRight+');border:0px;border-spacing:0px;'
  +                           'cursor:'+calhelpCursor+';'
  +                           'height:'+(calhelpHeight-2)+'px;padding:0px;width:'+(calhelpHeight-2)+'px"'
  +                         ' type="button"/>'
  +                     '</div>'
  +                   '</td>'
  +                 '</tr>'
  +               '</table>'
  +             '</td>'
  +           '</tr>'
  +           '<tr>'
  +             '<td align="center"'
  +               ' onclick="onclickCalhelp('+"'"+id+"'"+', null, 3)"'
  +               ' onmouseout="onmouseoutCalhelp(' + "'" + id + "'" + ', null, 3)"'
  +               ' onmouseover="onmouseoverCalhelp(' + "'" + id + "'" + ', null, 3)"'
  +               ' style="background-color:'+calhelpBgColorWeek+';border:0px;'
  +                 'font-family:'+calhelpFontFamily+';font-size:'+calhelpFontSize+'px;'
  +                 'padding:2px;width:'+calhelpCellWidth+'px">&#160;</td>';
  for(var i=0; i<calhelpDayNames.length; i++) {
    htmlString += ''
    + '<td align="center"'
    +   ' onclick="onclickCalhelp('+"'"+id+"'"+', null, ' + (i+4) + ')"'
    +   ' onmouseout="onmouseoutCalhelp(' + "'" + id + "'" + ', null, ' + (i+4) + ')"'
    +   ' onmouseover="onmouseoverCalhelp(' + "'" + id + "'" + ', null, ' + (i+4) + ')"'
    +   ' style="background-color:'+calhelpBgColorWeek+';border:0px;'
    +     'cursor:default;'
    +     'font-family:'+calhelpFontFamily+';font-size:'+calhelpFontSize+'px;padding:2px;'
    +     'width:'+calhelpCellWidth+'px"'
    +   '>'+calhelpDayNames[i]+'</td>';
  }
  htmlString += '</tr>';
  var d = 0;
  for(var y=0; y<6; y++) {
    htmlString += '<tr>';
    for(var x=0; x<8; x++) {
      htmlString += ''
      + '<td align="center"';
      if(x == 0) {
        htmlString += ' id="' + id + '-week' + y + '"'
        + ' onclick="onclickCalhelp('+"'"+id+"'"+', null, ' + (y+11) + ')"'
        + ' onmouseout="onmouseoutCalhelp(' + "'" + id + "'" + ', null, ' + (y+11) + ')"'
        + ' onmouseover="onmouseoverCalhelp(' + "'" + id + "'" + ', null, ' + (y+11) + ')"'
        + ' style="background-color:'+calhelpBgColorWeek+';border:0px;'
        +   'cursor:default;'
        +   'font-family:'+calhelpFontFamily+';font-size:'+calhelpFontSize+'px;'
        +   'padding:2px"';
      }
      else {
        htmlString += ' id="' + id + '-day' + d + '"'
        +   ' onclick="onclickCalhelp('+"'"+id+"'"+', ' + d + ', null)"'
        + ' onmouseout="onmouseoutCalhelp(' + "'" + id + "'" + ', ' + d + ', ' + (d+20) + ')"'
        + ' onmouseover="onmouseoverCalhelp(' + "'" + id + "'" + ', ' + d + ', ' + (d+20) + ')"'
        + ' style="background-color:'+calhelpBgColor+';border:0px;'
        +   'cursor:'+calhelpCursor+';'
        +   'font-family:'+calhelpFontFamily+';font-size:'+calhelpFontSize+'px;'
        +   'padding:2px"';
        d++;
      }
      htmlString += '>&#160;</td>';
    }
    htmlString += '</tr>';
  }
  htmlString += ''
  +         '</table>'
  +       '</div>'
  +     '</td>'
  +   '</tr>'
  + '</table>';
  theParent.innerHTML = htmlString;
  calhelpInit(id);
}

//*****************************************************************************************************

/**
 * Sets the selected calhelp entry.
 * @param id ID of the calhelp.
 * @param num Number of the selected calhelp entry.
 */
function setCalhelp(id, num) {
  if(id == null || id == "" || id != calhelpFocusedID || !calhelpDays[num][0])
    return;
  document.getElementById(id).value = calhelpDays[num][3] + '.' + (calhelpDays[num][2]+1)
  + '.' + calhelpDays[num][1];
  hideCalhelp(id);
}

//*****************************************************************************************************

/**
 * Sets the focused calhelp entry.
 * @param id ID of the calhelp.
 * @param num Number of the focused calhelp entry.
 */
function setFocusedCalhelp(id, num) {
  if(id == null || id == ""
  || num == null || calhelpDays.length <= num || !calhelpDays[num][0])
    return false;
  unsetFocusedCalhelp(id);
  if(getFocusedCalhelp(id) != null)
    onmouseoutCalhelp(id, getFocusedCalhelp(id));
  var theDay = document.getElementById(id+'-day'+num);
  theDay.style.backgroundColor = calhelpFocusedBgColor;
  if(calhelpDays[num][4]) {
    theDay.style.color = calhelpColorTodayFocused;
  }
  else {
    theDay.style.color = calhelpFocusedColor;
  }
  calhelpFocusedID = id;
  calhelpFocusedNum = num;
  return true;
}

//*****************************************************************************************************

/**
 * Opens or closes the calhelp entries.
 * @param id ID of the calhelp.
 */
function toggleCalhelp(id) {
  if(id == null || id == "")
    return;
  var theBox = document.getElementById(id+'-box');
  var theDiv = document.getElementById(id+'-div');
  var theTab = document.getElementById(id+'-tab');
  if(theDiv.style.visibility!='visible') {
    calhelpDate = getCalhelpSelectedValue(id);
    if(calhelpDate == null)
      calhelpDate = new Date();
    calhelpMonth = calhelpDate.getMonth();
    calhelpYear = calhelpDate.getFullYear();
    calhelpInit(id);

    theDiv.style.display = 'block';
    var theOffsetTop = theDiv.offsetTop - window.document.documentElement.scrollTop;
    var theWindowHeight;
    if(theTab.style.borderTopWidth == '1px')
      theOffsetTop += theBox.offsetHeight;
    else
      theOffsetTop -= theBox.offsetHeight
    if(document.documentElement)
      theWindowHeight = document.documentElement.clientHeight;
    else
      theWindowHeight = window.innerHeight;
    if((theOffsetTop * 2) <= theWindowHeight)
      openCalhelpDown(id);
    else
      openCalhelpUp(id);
//    onmouseoverCalhelp(id, getCalhelpSelectedNum(id));
  }
  else
    hideCalhelp(id);
}

//*****************************************************************************************************

/**
 * Takes away the focus from the calhelp.
 * @param id ID of the calhelp to make sure the program is working on the correct one.
 */
function unsetFocusedCalhelp(id) {
  if(id == null || id == "")
    return;
  if(calhelpFocusedID == id) {
    for(var i=0; i<calhelpDays.length; i++) {
      if(calhelpDays[i][0]) {
        var theDay = document.getElementById(id+'-day'+i);
        theDay.style.backgroundColor = calhelpBgColor;
        if(calhelpDays[i][4]) {
          theDay.style.color = calhelpColorToday;
        }
        else {
          theDay.style.color = calhelpColor;
        }
      }
    }
    calhelpFocusedID = null;
    calhelpFocusedNum = null;
  }
  else
    hideCalhelp(calhelpFocusedID);
}

//*****************************************************************************************************

