var monthName = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
var month;
var year;
var selectedDate;
var fromDate;
var toDate;
var targetElement;
var dockElement;
function getCoordsInTopWindow(o){
var x = 0;
var y = 0;
var win;
if(o.ownerDocument.defaultView)
{
win = o.ownerDocument.defaultView;
}
else
{
win = o.ownerDocument.parentWindow;
}
if(win.frameElement){
var parentFrameCoords = getCoordsInTopWindow(win.frameElement);
x += parentFrameCoords.x;
y += parentFrameCoords.y;
}
while(o){
x += o.offsetLeft;
y += o.offsetTop;
o=o.offsetParent;
}
var r = new Object();
r.x = x;
r.y = y;
return r;
}
function getCoords(o){
var x = 0;
var y = 0;
while(o.tagName!="BODY"){
x += o.offsetLeft;
y += o.offsetTop;
o=o.offsetParent;
}
var r = new Object();
r.x = x;
r.y = y;
return r;
}
function calendarClose(e)
{
if(!e) {e = top.frames.calendar.event;}
if(!e) return;
if(e.toElement) return;
top.document.getElementById("calendarDiv").style.visibility = "hidden";
}
function calendarPick(pdockElement, ptargetElement, pfromDate, ptoDate)
{
if(ptargetElement.disabled) return;
var pselectedDate = stringToDate(ptargetElement.value);
if(pselectedDate)
{
if(dateToString(pselectedDate)!=ptargetElement.value)
{
pselectedDate = false;
}
}
dockElement = pdockElement;
targetElement = ptargetElement;
var coords = getCoordsInTopWindow(dockElement);
coords.x += dockElement.offsetWidth+2;
var div;
div = top.document.getElementById("calendarDiv");
if(!div)
{
alert("Calendar not loaded yet!");
return;
}
else
{
div.style.left = coords.x+"px";
div.style.top = coords.y+"px";
div.style.visibility = "visible";
}
if(pselectedDate)
{
month = pselectedDate.getMonth();
year = pselectedDate.getUTCFullYear();
}
else if(pfromDate)
{
month = pfromDate.getMonth();
year = pfromDate.getUTCFullYear();
}
else
{
var now = new Date();
month = now.getMonth();
year = now.getUTCFullYear();
}
selectedDate = pselectedDate;
fromDate = pfromDate;
toDate = ptoDate;
calendarRewrite();
//top.frames.calendar.onblur=calendarClose;
top.document.getElementById("calendar").onblur=calendarClose;
if('onfocusout' in top.frames.calendar.document ){
top.frames.calendar.document.onfocusout=null;
top.frames.calendar.document.body.focus();
top.frames.calendar.document.onfocusout=calendarClose;
}
else{
top.frames.calendar.focus();
top.frames.calendar.onblur=calendarClose;
}
//setTimeout("top.frames.calendar.focus()", 50);
}
function calendarMonthDec(){
month--;
if(month<0)
{
month=11;
year--;
}
calendarRewrite();
return false;
}
function calendarMonthInc()
{
month++;
if(month>11)
{
month=0;
year++;
}
calendarRewrite();
return false;
}
function calendarMonthSet()
{
month = this.month;
calendarRewrite();
return false;
}
function calendarYearSet()
{
year = this.year;
calendarRewrite();
return false;
}
function calendarMonthSelectShow(e)
{
top.frames.calendar.document.getElementById("monthSelect").style.display="block";
top.frames.calendar.document.getElementById("yearSelect").style.display="none";
if(month>=3)
{
window.setTimeout('top.frames.calendar.document.getElementById("monthSelect").scrollTop = top.frames.calendar.document.getElementById("monthSelect").getElementsByTagName("a")[month-3].offsetTop', 10);
}
if(!e) {e = top.frames.calendar.event;}
if(e.stopPropagating)
{
e.stopPropagating();
}
else
{
e.cancelBubble=true;
}
}
function calendarYearSelectShow(e)
{
top.frames.calendar.document.getElementById("monthSelect").style.display="none";
top.frames.calendar.document.getElementById("yearSelect").style.display="block";
if(!e) {e = top.frames.calendar.event;}
if(e.stopPropagating)
{
e.stopPropagating();
}
else
{
e.cancelBubble=true;
}
}
function dateToString(d){
return d.getDate()+"."+(d.getMonth()+1)+"."+d.getUTCFullYear();
}
function stringToDate(s)
{
try
{
var a = s.split(".");
if(!parseInt(a[2]) && !parseInt(a[1]) && !parseInt(a[0])) {return false};
return new Date(parseInt(a[2]), parseInt(a[1])-1, parseInt(a[0]));
}
catch(ex)
{
return false;
}
}
function calendarDateSelect()
{
targetElement.value = dateToString(this.date) + '.';
targetElement.focus();
return false;
}
function calendarRewrite()
{
var doc = top.frames.calendar.document;
var html;
html = "
";
html += "";
html += "| < | "+monthName[month]+" | > | "+year+" | ";
html += "
";
html += "
";
html += "";
html += "";
html += "| Mo | ";
html += "Tu | ";
html += "We | ";
html += "Th | ";
html += "Fr | ";
html += "Sa | ";
html += "Su | ";
html += "
";
html += "";
html += "| | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += "
";
html += "";
html += "| | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += "
";
html += "";
html += "| | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += "
";
html += "";
html += "| | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += "
";
html += "";
html += "| | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += "
";
html += "";
html += "| | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += " | ";
html += "
";
html += "
";
html += "";
html += "
";
html += "";
html += "
";
doc.body.innerHTML = html;
doc.getElementById('monthDec').onclick=calendarMonthDec;
doc.getElementById('monthInc').onclick=calendarMonthInc;
var monthNameCoords = getCoords(doc.getElementById("monthName"));
var monthSelect = doc.getElementById("monthSelect");
monthSelect.style.left = monthNameCoords.x;
monthSelect.style.top = monthNameCoords.y + doc.getElementById("monthName").offsetHeight - 1;
monthSelect.style.width = doc.getElementById("monthName").offsetWidth;
monthSelect.onblur = function(){ this.display="none" }
for(i=0;i<12;i++)
{
var monthLink = doc.createElement("a");
monthLink.innerHTML = " "+monthName[i];
monthLink.href = "javascript:;";
monthLink.month = i;
monthLink.onclick = calendarMonthSet;
monthSelect.appendChild(monthLink);
if(i==month)
{
monthLink.className = "selected";
}
}
doc.getElementById("monthName").onclick = calendarMonthSelectShow;
var yearNameCoords = getCoords(doc.getElementById("yearName"));
var yearSelect = doc.getElementById("yearSelect");
yearSelect.style.left = yearNameCoords.x;
yearSelect.style.top = yearNameCoords.y + doc.getElementById("yearName").offsetHeight - 1;
yearSelect.style.width = doc.getElementById("yearName").offsetWidth;
var now = new Date();
for(i=now.getUTCFullYear();i=fromDate || !fromDate) && (d<=toDate || !toDate))
{
var link = doc.createElement("a");
link.href="javascript:;";
link.onclick=calendarDateSelect;
link.date=d;
link.innerHTML=d.getDate();
cell.appendChild(link);
}
else
{
cell.innerHTML = d.getDate();
}
if(selectedDate && d.getTime()==selectedDate.getTime())
{
cell.className+="selected";
}
d = new Date(d.getTime()+dayMili);
if(firstX==-1) {firstX = x;}
}
}
function calendarInit(){
var div;
div = top.document.getElementById("calendarDiv");
if(!div){
div = top.document.createElement("div");
div.style.visibility = "hidden";
div.style.position = "absolute";
div.style.left = "0px";
div.style.top = "0px";
div.id = "calendarDiv";
top.document.body.appendChild(div);
div.innerHTML = "";
/*
doc = top.frames.calendar.document;
doc.open();
doc.write("");
doc.write("");
doc.write("");
doc.write("");
doc.write("");
doc.write("");
doc.write("");
doc.close();
*/
//top.frames.calendar.onblur=calendarClose;
//top.document.getElementById("calendar").onblur=calendarClose;
}
}
window.onload=calendarInit;