/*********************************************
Input Calendar v1.2

Developer: Shawn Bailly (shawn.bailly@cox.net)
Purpose: Rapid full deployment of simple yet versitile calendar control.
Supports: All browsers.
Features: Millennium switch compliance.
          Time selection.
          DHTML Dropdowns for rapid Month, Year, Decade, Century, Millennium selection.


Version 1.2
   Modified function to only require inclusion of this JS, css and control="calendar" on calendar input.
************************************************/

var __AF_CALENDAR_DATE = new Date();
var __AF_CALENDAR_ISDOWN = false;
var __AF_CALENDAR_EL_MAIN = null;
var __AF_CALENDAR_EL_HEADER = null;
var __AF_CALENDAR_EL_DAYS = null;
var __AF_CALENDAR_EL = null;
var __AF_CALENDAR_SRC = null;
var __AF_CALENDAR_OSRC = null;
var __AF_CALENDAR_CONTEXT = false;
var __AF_CALENDAR_DELAY = false;
var __AF_CALENDAR_MBLEFT = (navigator.userAgent.indexOf("MSIE") > -1 ? 1 : 0);
var __AF_CALENDAR_MBRIGHT = (navigator.userAgent.indexOf("Opera") > -1 ? 4 : 2);
var __AF_CALENDAR_TIMER = null;
var __AF_CALENDAR_REPEAT = false;
var __AF_CALENDAR_COUNTER = 0;
var __AF_CALENDAR_IFRAME = null;
var __AF_CALENDAR_IFRAME_SUB = null;

var __AF_CALENDAR_PATCH_1 = function(str){
   /*===================================
   Function: Patch 1.1
   Input: Text string formatted as nn/nn/nn or nn/nn/nnnn.
   Output: nn/nn/nnnn.
   Purpose: Any two digit number less than 50 will be read as part of the current century.
            Any two digit number greater than or equal to 50 will be of the previous century.
   Examples: If the current year is 2005.
             01/01/09 = 01/01/2009
             01/01/30 = 01/01/2030
             01/01/98 = 01/01/1998
             01/01/50 = 01/01/1950
   ===================================*/
   var result = str;
   var re = /^(\d{2})(\D)(\d{2})(\D)(\d{2})([\s|$])/i;
   var arr = re.exec(str);
   if (arr != null){
      var century = new Number(new Date().getFullYear().toString().substr(0,2)*100);
      var decade = new Number(arr[5]);
      var year = 0;
      if (decade < 50)year = century+decade;
      else year = (century-100)+decade;
      result = str.replace(re,arr[1]+arr[2]+arr[3]+arr[4]+year+arr[6]);
   };
   return result;
};

function __AF_CALENDAR_HIDEBG(el,forsub){
   forsub = (forsub == null ? false : forsub);
   if (window.navigator.userAgent.indexOf("MSIE") > -1){
      var ver = window.navigator.userAgent.indexOf("MSIE");
      ver = parseFloat(window.navigator.userAgent.substr(ver+5,3));
      var getPos = function(el){
         var pStr = "";
         var top = 0;
         var left = 0;
         while (eval("el"+pStr) != null){
            if (eval("el"+pStr+".nodeType") == 1)top += eval("el"+pStr+".offsetTop");
            if (eval("el"+pStr+".nodeType") == 1)left += eval("el"+pStr+".offsetLeft");
            pStr += ".offsetParent";
         };
         return {x:left,y:top};
      };
      if (ver > 5.5){
         if (forsub)__AF_CALENDAR_IFRAME_SUB = document.createElement("iframe");
         else __AF_CALENDAR_IFRAME = document.createElement("iframe");
         var dim = getPos(el);
         var dTop = dim.y+(forsub ? 1 : 0);
         var dLeft = dim.x+(forsub ? 1 : 0);
         var dRight = dLeft+el.offsetWidth;
         var dBottom = dTop+el.offsetHeight+(forsub ? 1 : 0);
         with ((forsub ? __AF_CALENDAR_IFRAME_SUB : __AF_CALENDAR_IFRAME)){
            name = (forsub ? "__AF_CALENDAR_IFRAME_SUB" : "__AF_CALENDAR_IFRAME");
            style.position = "absolute";
            style.zIndex = 3;
            style.top = dTop+"px";
            style.left = dLeft+"px";
            style.width = el.offsetWidth+"px";
            style.height = el.offsetHeight+(forsub ? 1 : 0)+"px";
            style.visibility = "visible";
         };
         document.body.appendChild((forsub ? __AF_CALENDAR_IFRAME_SUB : __AF_CALENDAR_IFRAME));
      }
      else{
         var selects = document.getElementsByTagName("select");
         for (var i=0; i<selects.length; i++){
            var sTop = getPos(selects[i]).y;
            var sLeft = getPos(selects[i]).x;
            var sRight = getPos(selects[i]).x+selects[i].offsetWidth;
            var sBottom = getPos(selects[i]).y+selects[i].offsetHeight;
            var yTouch = (dTop >= sTop && dTop <= sBottom) || (dBottom >= sTop && dBottom <= sBottom) || (dTop <= sTop && dBottom >= sBottom);
            var xTouch = (dLeft >= sLeft && dLeft <= sRight) || (dRight >= sLeft && dRight <= sRight) || (dLeft <= sLeft && dRight >= sRight);
            if (yTouch && xTouch){
               selects[i].style.visibility = "hidden";
               this.hiddenSelects[this.hiddenSelects.length] = selects[i];
            };
         };
         
      };
   };
};

function __AF_CALENDAR_RESIZEBG(el,forsub){
   forsub = (forsub == null ? false : forsub);
   if (window.navigator.userAgent.indexOf("MSIE") > -1){
      var ver = window.navigator.userAgent.indexOf("MSIE");
      ver = parseFloat(window.navigator.userAgent.substr(ver+5,3));
      if (ver < 6.5 && __AF_CALENDAR_IFRAME != null){
         with ((forsub ? __AF_CALENDAR_IFRAME_SUB : __AF_CALENDAR_IFRAME)){
            style.top = el.style.top;
            style.left = el.style.left;
            style.width = el.offsetWidth;
            style.height = el.offsetHeight;
         };
      };
   };
};

function __AF_CALENDAR_CLEARBG(forsub){
   forsub = (forsub == null ? false : forsub);
   var iframes = document.getElementsByTagName("iframe");
   for (var i=0; i<iframes.length; i++)if ((iframes[i].getAttribute("name") == "__AF_CALENDAR_IFRAME_SUB" && forsub) || (iframes[i].getAttribute("name") == "__AF_CALENDAR_IFRAME" && !forsub))document.body.removeChild(iframes[i]);
   if (forsub)__AF_CALENDAR_IFRAME_SUB = null;
   else __AF_CALENDAR_IFRAME = null;
};

function __AF_CALENDAR_SHOW(evt){
   var el = null;
   evt = (evt == null) ? window.event : evt;
   __AF_CALENDAR_EL = evt.srcElement;
   el = evt.srcElement;
   if (!isNaN(Date.parse(el.value))){
      el.value = __AF_CALENDAR_PATCH_1(el.value);
      __AF_CALENDAR_SETDATE(new Date(el.value));
   }
   else __AF_CALENDAR_SETDATE(new Date());
   var absDim = function(el){
      var pStr = "";
      var top = 0;
      var left = 0;
      while (eval("el"+pStr) != null){
         if (eval("el"+pStr+".nodeType") == 1)top += eval("el"+pStr+".offsetTop");
         if (eval("el"+pStr+".nodeType") == 1)left += eval("el"+pStr+".offsetLeft");
         pStr += ".offsetParent";
      };
      return {x:left,y:top};
   };
   var x = absDim(el).x;
   var y = absDim(el).y;
   if (y+el.offsetHeight+__AF_CALENDAR_EL_MAIN.offsetHeight > document.body.clientHeight+document.body.scrollTop)y -= __AF_CALENDAR_EL_MAIN.offsetHeight;
   else y += el.offsetHeight;
   if (x+el.offsetWidth+__AF_CALENDAR_EL_MAIN.offsetWidth > document.body.clientWidth+document.body.scrollLeft)x = (document.body.scrollLeft+document.body.clientWidth)-__AF_CALENDAR_EL_MAIN.offsetWidth;
   __AF_CALENDAR_EL_MAIN.style.top = y;
   __AF_CALENDAR_EL_MAIN.style.left = x;
   __AF_CALENDAR_HIDEBG(__AF_CALENDAR_EL_MAIN);
   __AF_CALENDAR_EL_MAIN.style.visibility = "visible";
   if (window.navigator.userAgent.indexOf("MSIE") > -1){
      document.body.attachEvent("onclick",__AF_CALENDAR_HIDE);
   }
   else{
      document.body.addEventListener("click",__AF_CALENDAR_HIDE,false);
   };
};

function __AF_CALENDAR_HIDE(evt){
   evt = (evt == null) ? window.event : evt;
   if (evt != null)evt.cancelBubble = true;
   __AF_CALENDAR_EL.value = (__AF_CALENDAR_EL.getAttribute("mask") != null && __AF_CALENDAR_EL.getAttribute("mask") != "") ? __AF_CALENDAR_DATE.dateFormat(__AF_CALENDAR_EL.getAttribute("mask")) : __AF_CALENDAR_DATE.toLocaleString();
   __AF_CALENDAR_EL = null;
   __AF_CALENDAR_EL_MAIN.style.visibility = "hidden";
   __AF_CALENDAR_CLEARBG();
   document.getElementById("__AF_CALENDAR_MENU").style.visibility = "hidden";
   __AF_CALENDAR_CLEARBG(true);
   if (window.navigator.userAgent.indexOf("MSIE") > -1)document.body.detachEvent("onclick",__AF_CALENDAR_HIDE);
   else document.body.removeEventListener("click",__AF_CALENDAR_HIDE,false);
};

function __AF_CALENDAR_SHOWMENU(html){
   var menu = document.getElementById("__AF_CALENDAR_MENU");
   var absDim = function(el){
      var pStr = "";
      var top = 0;
      var left = 0;
      while (eval("el"+pStr) != null){
         if (eval("el"+pStr+".nodeType") == 1)top += eval("el"+pStr+".offsetTop");
         if (eval("el"+pStr+".nodeType") == 1)left += eval("el"+pStr+".offsetLeft");
         pStr += ".offsetParent";
      };
      return {x:left,y:top};
   };
   menu.innerHTML = html;
   if (window.navigator.userAgent.indexOf("MSIE") > -1){
      var x = absDim(__AF_CALENDAR_SRC).x-absDim(__AF_CALENDAR_EL_MAIN).x;
      var y = absDim(__AF_CALENDAR_SRC).y-absDim(__AF_CALENDAR_EL_MAIN).y;
      if (y+absDim(__AF_CALENDAR_EL_MAIN).y+__AF_CALENDAR_SRC.offsetHeight+menu.offsetHeight > document.body.clientHeight+document.body.scrollTop)y -= menu.offsetHeight;
      else y += __AF_CALENDAR_SRC.offsetHeight;
      if (x+absDim(__AF_CALENDAR_EL_MAIN).x+__AF_CALENDAR_SRC.offsetWidth+menu.offsetWidth > document.body.clientWidth+document.body.scrollLeft)x = (document.body.scrollLeft+document.body.clientWidth)-menu.offsetWidth;
   }
   else{
      var x = absDim(__AF_CALENDAR_SRC).x;
      var y = absDim(__AF_CALENDAR_SRC).y;
      if (y+__AF_CALENDAR_SRC.offsetHeight+menu.offsetHeight > document.body.clientHeight+document.body.scrollTop)y -= menu.offsetHeight;
      else y += __AF_CALENDAR_SRC.offsetHeight;
      if (x+__AF_CALENDAR_SRC.offsetWidth+menu.offsetWidth > document.body.clientWidth+document.body.scrollLeft)x = (document.body.scrollLeft+document.body.clientWidth)-menu.offsetWidth;
   };
   menu.style.left = x;
   menu.style.top = y;
   __AF_CALENDAR_HIDEBG(menu,true);
   menu.style.visibility = "visible";
};

function __AF_CALENDAR_HIDEMENU(){
   var menu = document.getElementById("__AF_CALENDAR_MENU");
   menu.innerHTML;
   menu.style.visibility = "hidden";
   __AF_CALENDAR_CLEARBG(true);
};

function __AF_CALENDAR_PREVMENU(){
   var html = "";
   html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_GOTO(-1)\">Previous Month</div>";
   html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_GOTO(-12)\">Previous Year</div>";
   html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_GOTO(-120)\">Previous Decade</div>";
   html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_GOTO(-1200)\">Previous Century</div>";
   html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_GOTO(-12000)\">Previous Millennium</div>";
   __AF_CALENDAR_SHOWMENU(html);
};

function __AF_CALENDAR_NEXTMENU(){
   var html = "";
   html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_GOTO(1)\">Next Month</div>";
   html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_GOTO(12)\">Next Year</div>";
   html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_GOTO(120)\">Next Decade</div>";
   html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_GOTO(1200)\">Next Century</div>";
   html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_GOTO(12000)\">Next Millennium</div>";
   __AF_CALENDAR_SHOWMENU(html);
};

function __AF_CALENDAR_MONTHMENU(){
   var html = "";
   for (var i=0; i<11; i++)html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_SETDATE(new Date(__AF_CALENDAR_DATE.getFullYear(),"+i+",1,__AF_CALENDAR_DATE.getHours(),__AF_CALENDAR_DATE.getMinutes(),__AF_CALENDAR_DATE.getSeconds(),__AF_CALENDAR_DATE.getMilliseconds()))\">"+__AF_CALENDAR_DATE.getMonthName(i)+"</div>";
   __AF_CALENDAR_SHOWMENU(html);
};

function __AF_CALENDAR_MENUSCROLL(direction){
   document.getElementById("__AF_CALENDAR_TEMP").scrollTop += direction;
};

function __AF_CALENDAR_YEARMENU(){
   var html = "";
   if (window.navigator.userAgent.toLowerCase().indexOf("opera") > -1){
      for (var i=__AF_CALENDAR_DATE.getFullYear()-10; i<=__AF_CALENDAR_DATE.getFullYear()+10; i++)html += "<div class=\"item"+(i == __AF_CALENDAR_DATE.getFullYear() ? "_sel" : "")+"\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_SETDATE(new Date("+i+",__AF_CALENDAR_DATE.getMonth(),1,__AF_CALENDAR_DATE.getHours(),__AF_CALENDAR_DATE.getMinutes(),__AF_CALENDAR_DATE.getSeconds(),__AF_CALENDAR_DATE.getMilliseconds()))\">"+i+"</div>";
      __AF_CALENDAR_SHOWMENU(html);
   }
   else{
      html += "<div class=\"button\" onmouseover=\"__AF_CALENDAR_OVER(this); __AF_CALENDAR_TIMER = setInterval('__AF_CALENDAR_MENUSCROLL(-10)',10);\" onmouseout=\"__AF_CALENDAR_OUT(this); clearInterval(__AF_CALENDAR_TIMER); __AF_CALENDAR_TIMER = null;\" style=\"text-align:center\">&#x25b2;</div>";
      html += "<div id=\"__AF_CALENDAR_TEMP\" style=\"height:180px; overflow:hidden;\">";
      for (var i=__AF_CALENDAR_DATE.getFullYear()-100; i<=__AF_CALENDAR_DATE.getFullYear()+100; i++)html += "<div class=\"item"+(i == __AF_CALENDAR_DATE.getFullYear() ? "_sel" : "")+"\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_SETDATE(new Date("+i+",__AF_CALENDAR_DATE.getMonth(),1,__AF_CALENDAR_DATE.getHours(),__AF_CALENDAR_DATE.getMinutes(),__AF_CALENDAR_DATE.getSeconds(),__AF_CALENDAR_DATE.getMilliseconds()))\">"+i+"</div>";
      html += "</div>";
      html += "<div class=\"button\" onmouseover=\"__AF_CALENDAR_OVER(this); __AF_CALENDAR_TIMER = setInterval('__AF_CALENDAR_MENUSCROLL(10)',10);\" onmouseout=\"__AF_CALENDAR_OUT(this); clearInterval(__AF_CALENDAR_TIMER); __AF_CALENDAR_TIMER = null;\" style=\"text-align:center\">&#x25bc;</div>";
      __AF_CALENDAR_SHOWMENU(html);
      document.getElementById("__AF_CALENDAR_TEMP").scrollTop = document.getElementById("__AF_CALENDAR_TEMP").scrollHeight/2-180/2;
   };
};

function __AF_CALENDAR_HOURMENU(){
   var attr = __AF_CALENDAR_EL.getAttribute("mask");
   var html = "";
   var am = (__AF_CALENDAR_DATE.getHours() < 12 ? true : false);
   if (attr == null || attr.indexOf("H") > -1)for (var i=0; i<24; i++)html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_SETDATE(new Date(__AF_CALENDAR_DATE.getFullYear(),__AF_CALENDAR_DATE.getMonth(),__AF_CALENDAR_DATE.getDate(),"+i+",__AF_CALENDAR_DATE.getMinutes(),__AF_CALENDAR_DATE.getSeconds(),__AF_CALENDAR_DATE.getMilliseconds()))\">"+(i <= 9 ? "0"+i : i)+"</div>";
   else for (var i=0; i<12; i++)html += "<div class=\"item\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_SETDATE(new Date(__AF_CALENDAR_DATE.getFullYear(),__AF_CALENDAR_DATE.getMonth(),__AF_CALENDAR_DATE.getDate(),"+(am ? (i == 11 ? 0 : i+1) : (i == 11 ? 12 : i+13))+",__AF_CALENDAR_DATE.getMinutes(),__AF_CALENDAR_DATE.getSeconds(),__AF_CALENDAR_DATE.getMilliseconds()))\">"+(i+1)+"</div>";
   __AF_CALENDAR_SHOWMENU(html);
};

function __AF_CALENDAR_MINUTEMENU(){
   var html = "";
   if (window.navigator.userAgent.toLowerCase().indexOf("opera") > -1){
      for (var i=__AF_CALENDAR_DATE.getMinutes()-10; i<=__AF_CALENDAR_DATE.getMinutes()+10; i++)html += "<div class=\"item"+(i == __AF_CALENDAR_DATE.getMinutes() ? "_sel" : "")+"\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_SETDATE(new Date(__AF_CALENDAR_DATE.getFullYear(),__AF_CALENDAR_DATE.getMonth(),__AF_CALENDAR_DATE.getDate(),__AF_CALENDAR_DATE.getHours(),"+i+",__AF_CALENDAR_DATE.getSeconds(),__AF_CALENDAR_DATE.getMilliseconds()))\">"+(i <= 9 ? "0"+i : i)+"</div>";
      __AF_CALENDAR_SHOWMENU(html);
   }
   else{
      html += "<div class=\"button\" onmouseover=\"__AF_CALENDAR_OVER(this); __AF_CALENDAR_TIMER = setInterval('__AF_CALENDAR_MENUSCROLL(-10)',10);\" onmouseout=\"__AF_CALENDAR_OUT(this); clearInterval(__AF_CALENDAR_TIMER); __AF_CALENDAR_TIMER = null;\" style=\"text-align:center\">&#x25b2;</div>";
      html += "<div id=\"__AF_CALENDAR_TEMP\" style=\"height:180px; overflow:hidden;\">";
      for (var i=0; i<=59; i++)html += "<div class=\"item"+(i == __AF_CALENDAR_DATE.getMinutes() ? "_sel" : "")+"\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" click=\"__AF_CALENDAR_SETDATE(new Date(__AF_CALENDAR_DATE.getFullYear(),__AF_CALENDAR_DATE.getMonth(),__AF_CALENDAR_DATE.getDate(),__AF_CALENDAR_DATE.getHours(),"+i+",__AF_CALENDAR_DATE.getSeconds(),__AF_CALENDAR_DATE.getMilliseconds()))\">"+(i <= 9 ? "0"+i : i)+"</div>";
      html += "</div>";
      html += "<div class=\"button\" onmouseover=\"__AF_CALENDAR_OVER(this); __AF_CALENDAR_TIMER = setInterval('__AF_CALENDAR_MENUSCROLL(10)',10);\" onmouseout=\"__AF_CALENDAR_OUT(this); clearInterval(__AF_CALENDAR_TIMER); __AF_CALENDAR_TIMER = null;\" style=\"text-align:center\">&#x25bc;</div>";
      __AF_CALENDAR_SHOWMENU(html);
      document.getElementById("__AF_CALENDAR_TEMP").scrollTop = document.getElementById("__AF_CALENDAR_TEMP").scrollHeight/2-180/2;
   };
};

function __AF_CALENDAR_GOTO(direction){
   __AF_CALENDAR_DATE = new Date(__AF_CALENDAR_DATE.getFullYear(),__AF_CALENDAR_DATE.getMonth()+direction,Math.min(__AF_CALENDAR_DATE.getDaysInMonth(new Date(__AF_CALENDAR_DATE.getFullYear(),__AF_CALENDAR_DATE.getMonth()+direction,1)),__AF_CALENDAR_DATE.getDate()),__AF_CALENDAR_DATE.getHours(),__AF_CALENDAR_DATE.getMinutes(),__AF_CALENDAR_DATE.getSeconds(),__AF_CALENDAR_DATE.getMilliseconds());
   __AF_CALENDAR_EL.value = (__AF_CALENDAR_EL.getAttribute("mask") != null && __AF_CALENDAR_EL.getAttribute("mask") != "") ? __AF_CALENDAR_DATE.dateFormat(__AF_CALENDAR_EL.getAttribute("mask")) : __AF_CALENDAR_DATE.toLocaleString();
   __AF_CALENDAR_FILL();
};

function __AF_CALENDAR_ADDTIME(direction){
   __AF_CALENDAR_DATE.addMinute(direction);
   __AF_CALENDAR_EL.value = (__AF_CALENDAR_EL.getAttribute("mask") != null && __AF_CALENDAR_EL.getAttribute("mask") != "") ? __AF_CALENDAR_DATE.dateFormat(__AF_CALENDAR_EL.getAttribute("mask")) : __AF_CALENDAR_DATE.toLocaleString();
   __AF_CALENDAR_FILL();
};

function __AF_CALENDAR_SETDATE(date,close){
   var close = (close == null) ? false : close;
   __AF_CALENDAR_DATE = (date == null || Date.parse(date) == "Invalid Date") ? new Date() : new Date(date);
   __AF_CALENDAR_FILL();
   __AF_CALENDAR_EL.value = (__AF_CALENDAR_EL.getAttribute("mask") != null && __AF_CALENDAR_EL.getAttribute("mask") != "") ? __AF_CALENDAR_DATE.dateFormat(__AF_CALENDAR_EL.getAttribute("mask")) : __AF_CALENDAR_DATE.toLocaleString();
   if (close)__AF_CALENDAR_HIDE();
};

function __AF_CALENDAR_FILL(segment){
   var fdow = __AF_CALENDAR_DATE.getFirstDay();
   var el = null;
   var dow = 0;
   segment = (segment == null ? [] : segment.split(","));
   if (segment.find("date") > -1 || segment.length == 0){
      __AF_CALENDAR_EL_HEADER.childNodes[0].innerHTML = __AF_CALENDAR_DATE.getMonthName();
      __AF_CALENDAR_EL_HEADER.childNodes[1].nodeValue = " "+__AF_CALENDAR_DATE.getDate()+", ";
      __AF_CALENDAR_EL_HEADER.childNodes[2].innerHTML = __AF_CALENDAR_DATE.getFullYear();
   };
   if (segment.find("days") > -1 || segment.length == 0){
      for (var i=0; i<__AF_CALENDAR_EL_DAYS.length; i++){
         if (i >= fdow && i < __AF_CALENDAR_DATE.getDaysInMonth()+fdow){
            __AF_CALENDAR_EL_DAYS[i].childNodes[0].nodeValue = i-fdow+1;
            if (dow == 0 || dow == 6)__AF_CALENDAR_EL_DAYS[i].className = "we";
            else __AF_CALENDAR_EL_DAYS[i].className = "wd";
            if (new Date(__AF_CALENDAR_DATE.getFullYear(),__AF_CALENDAR_DATE.getMonth(),i-fdow+1).isEqual(__AF_CALENDAR_DATE))__AF_CALENDAR_EL_DAYS[i].className = "sel";
            __AF_CALENDAR_EL_DAYS[i].setAttribute("click","__AF_CALENDAR_SETDATE(new Date(__AF_CALENDAR_DATE.getFullYear(),__AF_CALENDAR_DATE.getMonth(),"+(i-fdow+1)+",__AF_CALENDAR_DATE.getHours(),__AF_CALENDAR_DATE.getMinutes(),__AF_CALENDAR_DATE.getSeconds(),__AF_CALENDAR_DATE.getMilliseconds()),__AF_CALENDAR_EL.getAttribute(\"single\") == \"true\")");
         }
         else{
            if (i < fdow){
               __AF_CALENDAR_EL_DAYS[i].childNodes[0].nodeValue = __AF_CALENDAR_DATE.getDaysInMonth(__AF_CALENDAR_DATE.getMonth()-1)-(fdow-i)+1;
               if (dow == 0 || dow == 6)__AF_CALENDAR_EL_DAYS[i].className = "weo";
               else __AF_CALENDAR_EL_DAYS[i].className = "wdo";
               __AF_CALENDAR_EL_DAYS[i].setAttribute("click","__AF_CALENDAR_SETDATE(new Date(__AF_CALENDAR_DATE.getFullYear(),__AF_CALENDAR_DATE.getMonth()-1,"+(__AF_CALENDAR_DATE.getDaysInMonth(__AF_CALENDAR_DATE.getMonth()-1)-(fdow-i)+1)+",__AF_CALENDAR_DATE.getHours(),__AF_CALENDAR_DATE.getMinutes(),__AF_CALENDAR_DATE.getSeconds(),__AF_CALENDAR_DATE.getMilliseconds()))");
            }
            else{
               if (i < 35 && fdow+__AF_CALENDAR_DATE.getDaysInMonth() < 35 || fdow+__AF_CALENDAR_DATE.getDaysInMonth() > 35){
                  __AF_CALENDAR_EL_DAYS[i].childNodes[0].nodeValue = i-fdow-__AF_CALENDAR_DATE.getDaysInMonth()+1;
                  if (dow == 0 || dow == 6)__AF_CALENDAR_EL_DAYS[i].className = "weo";
                  else __AF_CALENDAR_EL_DAYS[i].className = "wdo";
                  __AF_CALENDAR_EL_DAYS[i].setAttribute("click","__AF_CALENDAR_SETDATE(new Date(__AF_CALENDAR_DATE.getFullYear(),__AF_CALENDAR_DATE.getMonth()+1,"+(i-fdow-__AF_CALENDAR_DATE.getDaysInMonth()+1)+",__AF_CALENDAR_DATE.getHours(),__AF_CALENDAR_DATE.getMinutes(),__AF_CALENDAR_DATE.getSeconds(),__AF_CALENDAR_DATE.getMilliseconds()))");
               }
               else{
                  __AF_CALENDAR_EL_DAYS[i].childNodes[0].nodeValue = "";
                  if (dow == 0 || dow == 6)__AF_CALENDAR_EL_DAYS[i].className = "weo";
                  else __AF_CALENDAR_EL_DAYS[i].className = "wdo";
                  __AF_CALENDAR_EL_DAYS[i].removeAttribute("click");
               };
            };
         };
         (dow < 6) ? dow++ : dow = 0;
      };
   };
   if (segment.find("time") > -1 || segment.length == 0){
      if (__AF_CALENDAR_EL != null){
         if (__AF_CALENDAR_EL.getAttribute("mask") == null || __AF_CALENDAR_EL.getAttribute("mask") == "" || __AF_CALENDAR_EL.getAttribute("mask").toLowerCase().indexOf("h") > -1 || __AF_CALENDAR_EL.getAttribute("mask").toLowerCase().indexOf("n") > -1 || __AF_CALENDAR_EL.getAttribute("mask").toLowerCase().indexOf("s") > -1){
            var time = document.getElementById("__AF_CALENDAR_TIME");
            var row = time.parentNode;
            row.style.display = "";
            var html = "";
            html += (__AF_CALENDAR_EL.getAttribute("mask") != null && __AF_CALENDAR_EL.getAttribute("mask").toLowerCase().indexOf("h") > -1 ? "<span click=\"__AF_CALENDAR_ADDTIME(60)\" delay=\"__AF_CALENDAR_HOURMENU()\" context=\"__AF_CALENDAR_ADDTIME(-60)\">"+__AF_CALENDAR_DATE.dateFormat(__AF_CALENDAR_EL.getAttribute("mask").substring(__AF_CALENDAR_EL.getAttribute("mask").toLowerCase().indexOf("h"),__AF_CALENDAR_EL.getAttribute("mask").toLowerCase().lastIndexOf("h")+1))+"</span>" : "<span click=\"__AF_CALENDAR_ADDTIME(60)\" context=\"__AF_CALENDAR_ADDTIME(-60)\">"+__AF_CALENDAR_DATE.getHours()+"</span>");
            html += ":";
            html += (__AF_CALENDAR_EL.getAttribute("mask") != null && __AF_CALENDAR_EL.getAttribute("mask").toLowerCase().indexOf("n") > -1 ? "<span click=\"__AF_CALENDAR_ADDTIME(1)\" delay=\"__AF_CALENDAR_MINUTEMENU()\" context=\"__AF_CALENDAR_ADDTIME(-1)\">"+__AF_CALENDAR_DATE.dateFormat(__AF_CALENDAR_EL.getAttribute("mask").substring(__AF_CALENDAR_EL.getAttribute("mask").toLowerCase().indexOf("n"),__AF_CALENDAR_EL.getAttribute("mask").toLowerCase().lastIndexOf("n")+1))+"</span>" : "<span click=\"__AF_CALENDAR_ADDTIME(1)\" context=\"__AF_CALENDAR_ADDTIME(-1)\">"+__AF_CALENDAR_DATE.dateFormat("nn")+"</span>");
            html += " ";
            html += (__AF_CALENDAR_EL.getAttribute("mask") != null && __AF_CALENDAR_EL.getAttribute("mask").toLowerCase().indexOf("t") > -1 ? "<span click=\"__AF_CALENDAR_ADDTIME(720)\" context=\"__AF_CALENDAR_ADDTIME(-720)\">"+__AF_CALENDAR_DATE.dateFormat(__AF_CALENDAR_EL.getAttribute("mask").substring(__AF_CALENDAR_EL.getAttribute("mask").toLowerCase().indexOf("t"),__AF_CALENDAR_EL.getAttribute("mask").toLowerCase().lastIndexOf("t")+1))+"</span>" : "");
            time.innerHTML = html;
         }
         else{
            var time = document.getElementById("__AF_CALENDAR_TIME");
            var row = time.parentNode;
            row.style.display = "none";
         };
      };
   };
   __AF_CALENDAR_RESIZEBG(__AF_CALENDAR_EL_MAIN);
};

function __AF_CALENDAR_OVER(el){
   switch (el.className){
      case "wd" : el.className = "wd_over"; break;
      case "we" : el.className = "we_over"; break;
      case "wdo" : el.className = "wdo_over"; break;
      case "weo" : el.className = "weo_over"; break;
      case "sel" : el.className = "sel_over"; break;
      case "item" : el.className = "item_over"; break;
      case "item_sel" : el.className = "item_sel_over"; break;
      case "button" : el.className = "button_over"; break;
      case "tbtn" : el.className = "tbtn_over"; break;
   };
};

function __AF_CALENDAR_OUT(el){
   switch (el.className){
      case "wd_over" : el.className = "wd"; break;
      case "we_over" : el.className = "we"; break;
      case "wdo_over" : el.className = "wdo"; break;
      case "weo_over" : el.className = "weo"; break;
      case "sel_over" : el.className = "sel"; break;
      case "item_over" : el.className = "item"; break;
      case "item_sel_over" : el.className = "item_sel"; break;
      case "button_over" : el.className = "button"; break;
      case "tbtn_over" : el.className = "tbtn"; break;
   };
};

function __AF_CALENDAR_DELAYMENU(){
   clearTimeout(__AF_CALENDAR_TIMER);
   __AF_CALENDAR_TIMER = null;
   if (__AF_CALENDAR_SRC == __AF_CALENDAR_OSRC){
      eval(__AF_CALENDAR_OSRC.getAttribute("delay"));
      __AF_CALENDAR_DELAY = true;
   };
};

function __AF_CALENDAR_INITREPEAT(){
   clearTimeout(__AF_CALENDAR_TIMER);
   __AF_CALENDAR_TIMER = null;
   if (__AF_CALENDAR_SRC == __AF_CALENDAR_OSRC){
      __AF_CALENDAR_TIMER = setInterval(__AF_CALENDAR_OSRC.getAttribute("repeat"),75);
      __AF_CALENDAR_REPEAT = true;
   };
};

function __AF_CALENDAR_MOUSEDOWN(evt){
   evt = (evt == null) ? window.event : evt;
   __AF_CALENDAR_ISDOWN = true;
   evt.returnValue = false;
   evt.cancelBubble = true;
   __AF_CALENDAR_SRC = evt.srcElement;
   __AF_CALENDAR_OSRC = evt.srcElement;
   __AF_CALENDAR_DELAY = false;
   __AF_CALENDAR_REPEAT = false;
   if (!__AF_CALENDAR_FINDPARENT(evt.srcElement,"__AF_CALENDAR_MENU"))__AF_CALENDAR_HIDEMENU();
   switch (evt.button){
      case __AF_CALENDAR_MBLEFT:
         if (evt.srcElement.getAttribute("delay") != null && evt.srcElement.getAttribute("delay") != "")__AF_CALENDAR_TIMER = setTimeout(__AF_CALENDAR_DELAYMENU,500);
         if (evt.srcElement.getAttribute("repeat") != null && evt.srcElement.getAttribute("repeat") != "")__AF_CALENDAR_TIMER = setTimeout(__AF_CALENDAR_INITREPEAT,500);
         break;
      case __AF_CALENDAR_MBRIGHT:
         if (evt.srcElement.getAttribute("context") != null){
            eval(evt.srcElement.getAttribute("context"));
            __AF_CALENDAR_CONTEXT = true;
         }
         else{
            evt.cancelBubble = true;
            evt.returnValue = false;
         };
         break;
   };
   
};

function __AF_CALENDAR_RTIME(direction){
   var mul = 1;
   direction = (direction == null ? 1 : direction);
   __AF_CALENDAR_COUNTER++;
   if (__AF_CALENDAR_COUNTER > 10)mul = 2;
   if (__AF_CALENDAR_COUNTER > 20)mul = 3;
   if (__AF_CALENDAR_COUNTER > 30)mul = 4;
   if (__AF_CALENDAR_COUNTER > 40)mul = 5;
   if (__AF_CALENDAR_COUNTER > 50)mul = 6;
   if (__AF_CALENDAR_COUNTER > 60)mul = 10;
   __AF_CALENDAR_DATE.addMinute(1*mul*direction);
   __AF_CALENDAR_FILL("time");
};

function __AF_CALENDAR_MOUSEUP(evt){
   evt = (evt == null) ? window.event : evt;
   __AF_CALENDAR_ISDOWN = false;
   switch (evt.button){
      case __AF_CALENDAR_MBLEFT:
         if (!__AF_CALENDAR_REPEAT && ((!__AF_CALENDAR_CONTEXT && __AF_CALENDAR_OSRC == evt.srcElement) || ( (__AF_CALENDAR_DELAY || __AF_CALENDAR_CONTEXT) && __AF_CALENDAR_FINDPARENT(evt.srcElement,"__AF_CALENDAR_MENU"))) && evt.srcElement.getAttribute("click") != null)eval(evt.srcElement.getAttribute("click"));
         __AF_CALENDAR_HIDEMENU();
         __AF_CALENDAR_CONTEXT = false;
         if (__AF_CALENDAR_REPEAT){
            clearInterval(__AF_CALENDAR_TIMER);
            __AF_CALENDAR_TIMER = null;
            __AF_CALENDAR_COUNTER = 0;
         };
         break;
   };
   clearTimeout(__AF_CALENDAR_TIMER);
   __AF_CALENDAR_TIMER = null;
   __AF_CALENDAR_SRC = null;
   __AF_CALENDAR_OSRC = null;
};

function __AF_CALENDAR_MOUSEMOVE(evt){
   evt = (evt == null) ? window.event : evt;
   if (__AF_CALENDAR_ISDOWN)__AF_CALENDAR_SRC = evt.srcElement;
   if (__AF_CALENDAR_CONTEXT)__AF_CALENDAR_OSRC = evt.srcElement;
   evt.returnValue = false;
   evt.cancelBubble = true;
};

function __AF_CALENDAR_FINDPARENT(el,parent){
   var pStr = "";
   var cont = true;
   var e = null;
   while (cont){
      e = eval("el"+pStr);
      if (e != null){
         if (e.nodeType == 1 && e.getAttribute("id") == parent)cont = false;
      }
      else{
         cont = false;
      };
      pStr += ".offsetParent";
   };
   return (e != null ? true : false);
};

function __AF_CALENDAR_INIT(){
   var html = "";
   html += "<table unselectable=\"on\" id=\"__AF_CALENDAR\" cellpadding=0 cellspacing=0 border=0 class=\"calendar\" style=\"position:absolute; visibility:hidden; z-index:4\"><tr><td valign=\"top\" align=\"left\">";
   html += "<div unselectable=\"on\" id=\"__AF_CALENDAR_MENU\" class=\"menu\" style=\"top:0px; left:0px; position:absolute; z-index:5; visibility:hidden\"></div>";
   html += "<table cellpadding=0 cellspacing=0 border=0>";
   html += "<thead id=\"__AF_CALENDAR_HEADER\"><tr><th title=\"Click for previous month.\nRight click or hold button for context menu.\" context=\"__AF_CALENDAR_PREVMENU()\" delay=\"__AF_CALENDAR_PREVMENU()\" click=\"__AF_CALENDAR_GOTO(-1)\" class=\"tbtn\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&lt;</th><th colspan=5><span context=\"__AF_CALENDAR_MONTHMENU()\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" delay=\"__AF_CALENDAR_MONTHMENU()\" class=\"tbtn\">"+__AF_CALENDAR_DATE.getMonthName()+"</span> "+__AF_CALENDAR_DATE.getDate()+", <span onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" class=\"tbtn\" context=\"__AF_CALENDAR_YEARMENU()\" delay=\"__AF_CALENDAR_YEARMENU()\">"+__AF_CALENDAR_DATE.getFullYear()+"</span></th><th class=\"tbtn\" onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\" context=\"__AF_CALENDAR_NEXTMENU()\" delay=\"__AF_CALENDAR_NEXTMENU()\" click=\"__AF_CALENDAR_GOTO(1)\">&gt;</th></tr><tr valign=\"top\" align=\"center\"><td>"+__AF_CALENDAR_DATE.getDayName(0,3)+"</td><td>"+__AF_CALENDAR_DATE.getDayName(1,3)+"</td><td>"+__AF_CALENDAR_DATE.getDayName(2,3)+"</td><td>"+__AF_CALENDAR_DATE.getDayName(3,3)+"</td><td>"+__AF_CALENDAR_DATE.getDayName(4,3)+"</td><td>"+__AF_CALENDAR_DATE.getDayName(5,3)+"</td><td>"+__AF_CALENDAR_DATE.getDayName(6,3)+"</td></tr></thead>";
   html += "<tbody id=\"__AF_CALENDAR_BODY\"><tr><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td></tr>";
   html += "<tr><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td></tr>";
   html += "<tr><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td></tr>";
   html += "<tr><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td></tr>";
   html += "<tr><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td></tr>";
   html += "<tr><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td><td onmouseover=\"__AF_CALENDAR_OVER(this)\" onmouseout=\"__AF_CALENDAR_OUT(this)\">&nbsp;</td></tr>";
   html += "<tr><td repeat=\"__AF_CALENDAR_RTIME(-1)\">&lt;</td><td colspan=5 id=\"__AF_CALENDAR_TIME\"></td><td repeat=\"__AF_CALENDAR_RTIME(1)\">&gt;</td></tr>";
   html += "</tbody>";
   html += "<tfoot>";
   html += "<tr valign=\"top\"><td align=\"center\" colspan=7 onclick=\"__AF_CALENDAR_SETDATE()\">Today: "+__AF_CALENDAR_DATE.getMonthName()+" "+__AF_CALENDAR_DATE.getDate()+", "+__AF_CALENDAR_DATE.getFullYear()+"</td></tr></tfoot></table></td></tr></table>";
   document.writeln(html);
   __AF_CALENDAR_EL_MAIN = document.getElementById("__AF_CALENDAR");
   __AF_CALENDAR_EL_HEADER = document.getElementById("__AF_CALENDAR_HEADER").rows[0].cells[1];
   __AF_CALENDAR_EL_DAYS = new Array();
   bRows = document.getElementById("__AF_CALENDAR_BODY").rows;
   for (var i=0; i<bRows.length-1; i++){
      bCells = bRows[i].cells;
      for (var j=0; j<bCells.length; j++)__AF_CALENDAR_EL_DAYS[__AF_CALENDAR_EL_DAYS.length] = bCells[j];
   };
   __AF_CALENDAR_EL_MAIN.onmouseup = __AF_CALENDAR_MOUSEUP;
   __AF_CALENDAR_EL_MAIN.onmousedown = __AF_CALENDAR_MOUSEDOWN;
   __AF_CALENDAR_EL_MAIN.onmousemove = __AF_CALENDAR_MOUSEMOVE;
   __AF_CALENDAR_EL_MAIN.onclick = function(evt){
      evt = (evt == null) ? window.event : evt;
      evt.cancelBubble = true;
      evt.returnValue = false;
   };
   __AF_CALENDAR_EL_MAIN.oncontextmenu = function(evt){
      evt = (evt == null) ? window.event : evt;
      evt.cancelBubble = true;
      evt.returnValue = false;
   };
   __AF_CALENDAR_FILL();
};

function __AF_CALENDAR_ASSIGN(){
   var inputs = document.getElementsByTagName("input");
   var handler = "";
   for (var i=0; i<inputs.length; i++){
      if (inputs[i].getAttribute("control") == "calendar"){
         if (inputs[i].getAttributeNode("handler") != null)handler = inputs[i].getAttributeNode("handler").nodeValue;
         else handler = "dblclick";
         if (navigator.userAgent.indexOf("MSIE") > -1)inputs[i].attachEvent("on"+handler,__AF_CALENDAR_SHOW);
         else inputs[i].addEventListener(handler,__AF_CALENDAR_SHOW,false);
      };
   };
};

if (navigator.userAgent.indexOf("MSIE") > -1)window.attachEvent("onload",__AF_CALENDAR_ASSIGN);
else window.addEventListener("load",__AF_CALENDAR_ASSIGN,false);

/* Array object expansions */
Array.prototype.find = function(str){
   var pos = -1;
   for (var i=0; i<this.length; i++)if(this[i].toLowerCase() == str.toLowerCase())pos = i;
   return pos;
};

/* Date object expansions */
Date.prototype.getFirstDay = function(month){
   month = (month == null) ? this.getMonth() : month;
   var date = new Date(this.getFullYear(),month,1);
   return date.getDay();
};
Date.prototype.getDayName = function(dow,len){
   var days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
   dow = (dow == null) ? this.getDay() : dow;
   return (len == null) ? days[dow] : days[dow].substr(0,len);
};
Date.prototype.getMonthName = function(month,len){
   var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
   month = (month == null) ? this.getMonth() : month;
   return (len == null) ? months[month] : months[month].substr(0,len);
};
Date.prototype.getDaysInMonth = function(date){
   date = (date == null || Date.parse(date) == "Invalid Date") ? this : new Date(date);
   var dim = new Array(31,(date.getFullYear() % 4 == 0 ? 29 : 28),31,30,31,30,31,31,30,31,30,31);
   return dim[date.getMonth()];
};
Date.prototype.isEqual = function(date,precision){
   precision = (precision == null) ? "d" : precision;
   if (date.constructor == Date){
      switch (precision){
         case "y" : return date.getFullYear() == this.getFullYear();
         case "m" : return date.getFullYear() == this.getFullYear() && date.getMonth() == this.getMonth();
         case "d" : return date.getFullYear() == this.getFullYear() && date.getMonth() == this.getMonth() && date.getDate() == this.getDate();
         case "h" : return date.getFullYear() == this.getFullYear() && date.getMonth() == this.getMonth() && date.getDate() == this.getDate() && date.getHours() == this.getHours();
         case "mm" : return date.getFullYear() == this.getFullYear() && date.getMonth() == this.getMonth() && date.getDate() == this.getDate() && date.getHours() == this.getHours() && date.getMinutes() == this.getMinutes();
         case "s" : return date.getFullYear() == this.getFullYear() && date.getMonth() == this.getMonth() && date.getDate() == this.getDate() && date.getHours() == this.getHours() && date.getMinutes() == this.getMinutes() && date.getSeconds() == this.getSeconds();
         case "ms" : return date.getFullYear() == this.getFullYear() && date.getMonth() == this.getMonth() && date.getDate() == this.getDate() && date.getHours() == this.getHours() && date.getMinutes() == this.getMinutes() && date.getSeconds() == this.getSeconds() && date.getMilliseconds() == this.getMilliseconds();
         default : return date.getFullYear() == this.getFullYear() && date.getMonth() == this.getMonth() && date.getDate() == this.getDate();
      };
   }
   else return false;
};
Date.prototype.addMinute = function(num){
   var minute = this.getMinutes();
   var hour = this.getHours();
   minute = minute+num;
   hour = Math.floor(minute/60)+hour;
   if (hour > 23)hour = hour-24*Math.floor(hour/24);
   if (hour < 0)hour = hour+24*Math.abs(Math.floor(hour/24));
   if (minute > 59)minute = minute-60*Math.floor(minute/60);
   if (minute < 0)minute = minute+60*Math.abs(Math.floor(minute/60));
   this.setHours(hour);
   this.setMinutes(minute);
};
Date.prototype.addMonth = function(num){
   this.setMonth(this.getMonth()+num);
   return this;
};
Date.prototype.dateFormat = function(date,mask){
   if (isNaN(Date.parse(date))){
      mask = date;
      date = this;
   };
   var masks = new Array();
   var protect = new Array();
   var fail = false;
   var nextSegment = "";
   var time24to12 = new Function("num","return (num > 12 ? num-12 : (num == 0 ? 12 : num))");
   var ampm = new Function("num","return (num >= 12 ? \"p\" : \"a\")");
   var reReplace = function (re,string,replaceStr,start){
      start = (start == null) ? 0 : start;
      var postString = string.substr(start);
      postString = postString.replace(re,replaceStr);
      return string.substr(0,start)+postString;
   };
   mask = (mask == null) ? "" : mask;
   if (!isNaN(Date.parse(date))){
      if (mask.length > 0){
         if (date.constructor != Date)date = new Date(date);
         masks[masks.length] = new function(){this.mask = date.getDayName(); this.re = /d{4}/};
         masks[masks.length] = new function(){this.mask = date.getDayName(date.getDay(),3); this.re = /d{3}/};
         masks[masks.length] = new function(){this.mask = (date.getDate() <= 9 ? "0"+date.getDate() : date.getDate()); this.re = /d{2}/};
         masks[masks.length] = new function(){this.mask = date.getDate(); this.re = /d{1}/};
         masks[masks.length] = new function(){this.mask = date.getMonthName(); this.re = /m{4}/};
         masks[masks.length] = new function(){this.mask = date.getMonthName(date.getMonth(),3); this.re = /m{3}/};
         masks[masks.length] = new function(){this.mask = (date.getMonth() < 9 ? "0"+(date.getMonth()+1) : (date.getMonth()+1)); this.re = /m{2}/};
         masks[masks.length] = new function(){this.mask = date.getMonth()+1; this.re = /m{1}/};
         masks[masks.length] = new function(){this.mask = date.getFullYear(); this.re = /y{4}/};
         masks[masks.length] = new function(){this.mask = date.getYear(); this.re = /y{2}/};
         masks[masks.length] = new function(){this.mask = (date.getHours() <= 9 ? "0"+date.getHours() : date.getHours()); this.re = /H{2}/};
         masks[masks.length] = new function(){this.mask = date.getHours(); this.re = /H{1}/};
         masks[masks.length] = new function(){this.mask = (time24to12(date.getHours()) <= 9 ? "0"+time24to12(date.getHours()) : time24to12(date.getHours())); this.re = /h{2}/};
         masks[masks.length] = new function(){this.mask = time24to12(date.getHours()); this.re = /h{1}/};
         masks[masks.length] = new function(){this.mask = (date.getMinutes() <= 9 ? "0"+date.getMinutes() : date.getMinutes()); this.re = /n{2}/};
         masks[masks.length] = new function(){this.mask = date.getMinutes(); this.re = /n{1}/};
         masks[masks.length] = new function(){this.mask = (date.getSeconds() <= 9 ? "0"+date.getSeconds() : date.getSeconds()); this.re = /s{2}/};
         masks[masks.length] = new function(){this.mask = date.getSeconds(); this.re = /s{1}/};
         masks[masks.length] = new function(){this.mask = ampm(date.getHours()).toUpperCase()+"M"; this.re = /T{2}/};
         masks[masks.length] = new function(){this.mask = ampm(date.getHours()).toUpperCase(); this.re = /T{1}/};
         masks[masks.length] = new function(){this.mask = ampm(date.getHours())+"m"; this.re = /t{2}/};
         masks[masks.length] = new function(){this.mask = ampm(date.getHours()); this.re = /t{1}/};
         masks[masks.length] = new function(){this.mask = date.getMilliseconds(); this.re = /l{1}/};
         for (var i=0; i<masks.length; i++){
            var pos = mask.search(masks[i].re);
            while (pos > -1){
               fail = false;
               for (var j=0; j<protect.length; j++)if (pos >= protect[j][0] && pos <= (protect[j][0]+protect[j][1]-1))fail = true;
               if (!fail){
                  var prevMask = mask.length;
                  mask = reReplace(masks[i].re,mask,masks[i].mask,pos);
                  if (prevMask != mask.length)for (var j=0; j<protect.length; j++)if (protect[j][0] > pos)protect[j][0] += (mask.length-prevMask);
                  protect[protect.length] = new Array(pos,masks[i].mask.toString().length);
               };
               nextSegment = mask.substr(pos+1);
               pos = nextSegment.search(masks[i].re);
               if (pos > -1)pos = pos+(mask.length-nextSegment.length);
            };
         };
         return mask;
      }
      else return true
   }
   else return false;
};
/* Event object expansions */
if (window.navigator.userAgent.indexOf("Firefox") > -1){
   if (window.navigator.userAgent.indexOf("Firefox/1.0.3") > -1){//Firefox 1.0.3 bug fix
      Element.prototype.__proto__.__defineGetter__("text",function(){
         return this.firstChild.nodeValue
      });
      Event.prototype.__proto__.__defineGetter__("srcElement",function(){
         var node = this.target;
         while (node.nodeType != 1)node = node.parentNode;
         return node;
      });
      Event.prototype.__proto__.__defineSetter__("cancelBubble",function(b){
         if (b)this.stopPropagation();
      });
      Event.prototype.__proto__.__defineSetter__("returnValue",function(b){
         if (!b)this.preventDefault();
      });
   }
   else{
      Event.prototype.__defineGetter__("srcElement",function(){
         var node = this.target;
         while (node.nodeType != 1)node = node.parentNode;
         return node;
      });
      Event.prototype.__defineSetter__("cancelBubble",function(b){
         if (b)this.stopPropagation();
      });
      Event.prototype.__defineSetter__("returnValue",function(b){
         if (!b)this.preventDefault();
      });
   };
};
__AF_CALENDAR_INIT();
