
var timeoutDelay=2000;//milliseconds, change this if you like, set to 0 for the calendar to never auto disappear
var g_startDay=0//0=sunday, 1=monday
var year1, year2
var tdate = new Date();
//year dropdown will contain 95 yr before and 5 year after the current year
year1 = parseInt(tdate.getFullYear())-95;
year2 = parseInt(tdate.getFullYear())+5;
//IE 5.0 check
var bname = navigator.appName;
var bver = navigator.appVersion;
bver = parseFloat(bver.substring(bver.indexOf('MSIE ') + 5 ));
//preload imgs
var imgUp=new Image(8,12);
imgUp.src='/MSIB21/FrontEnd/skins/default/images/up.gif';
var imgDown=new Image(8,12);
imgDown.src='/MSIB21/FrontEnd/skins/default/images/down.gif';
//used by timeout auto hide func
var timeoutId=false;
//the now std browser sniffer class
function Browser(){
this.dom=document.getElementById?1:0;
this.ie4=(document.all && !this.dom)?1:0;
this.ns4=(document.layers && !this.dom)?1:0;
this.ns6=(this.dom && !document.all)?1:0;
this.ie5=(this.dom && document.all)?1:0;
this.ok=this.dom || this.ie4 || this.ns4;
this.platform=navigator.platform;
}var browser=new Browser();
//dom browsers req this written to the HEAD section
if(browser.dom || browser.ie4){
document.writeln('<style>');
document.writeln('#cal_container {');
document.writeln('position : absolute;');
document.writeln('left : 100px;');
document.writeln('top : 100px;');
document.writeln('width : 200px;');;
browser.platform=='Win32'?height=180:height=170;
document.writeln('height : ' + height +'px;');
document.writeln('clip:rect(0px 200px ' + height + 'px 0px);');
document.writeln('visibility : hidden;');
document.writeln('z-index : 1000;');
document.writeln('background-color : #ffffff');
document.writeln('}');
document.writeln('</style>')
document.write('<div  id="cal_container" style="left:0px; z-index:100;">');
if(timeoutDelay) document.write(' onmouseout="calendarTimeout();" onmouseover="if(timeoutId) clearTimeout(timeoutId);"');
document.write('></div>');
document.writeln('<style>');
document.writeln('#DivShim {');
document.writeln('position : absolute;');
document.writeln('left : 0px;');
document.writeln('top : 0px;');
document.writeln('width : 200px;');
if(bname == "Microsoft Internet Explorer" && bver <= "5.01"){
browser.platform=='Win32'?height=0:height=0;
}
else{
browser.platform=='Win32'?height=164:height=164;
}
document.writeln('height : ' + height +'300px;');
document.writeln('clip:rect(0px 200px ' + height + 'px 0px);');
document.writeln('display:none;');
document.writeln('background-color : transparent');
document.writeln('}');
document.writeln('</style>')
if(location.protocol=="https:")
{document.write('<iframe src="/MSIB21/FrontEnd/skins/default/images/spacer.gif" id="DivShim" style="left:100px;" frameborder="0">');
document.write('</iframe>');
}else
{document.write('<iframe id="DivShim" style="left:100px;height:170px" frameborder="0">');
document.write('</iframe>');
}}
var DivRef=document.getElementById('cal_container');
var IfrRef=document.getElementById('DivShim');

var g_Calendar;
new Calendar(new Date());
//global to hold the calendar ref,set by constructor
function calendarTimeout(){
if(browser.ie4 || browser.ie5){
if(window.event.srcElement && window.event.srcElement.name!='month' && window.event.srcElement.name!='year')
timeoutId=setTimeout('g_Calendar.hide();',timeoutDelay);
}if(browser.ns6 || browser.ns4){
timeoutId=setTimeout('g_Calendar.hide();',timeoutDelay);
}}//constructor for calendar class
function Calendar(){
g_Calendar=this;
var oyears=new Array();
//some constants needed throughout the program
for(var i=year1;i<=year2;i++)
oyears[oyears.length]=i;
this.years=oyears;
this.daysInMonth=new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if(browser.ns4){
var tmpLayer=new Layer(127);
if(timeoutDelay){
tmpLayer.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
tmpLayer.onmouseover=function(event) { if(timeoutId) clearTimeout(timeoutId); };
tmpLayer.onmouseout=function(event) { timeoutId=setTimeout('g_Calendar.hide()',timeoutDelay);};
}tmpLayer.x=100;
tmpLayer.y=100;
tmpLayer.bgColor="#ffffff";
}if(browser.dom || browser.ie4){
var tmpLayer=browser.dom?document.getElementById('cal_container'):document.all.cal_container;
}this.cal_ContainerLayer=tmpLayer;
if(browser.ns4 && browser.platform=='Win32'){
this.cal_ContainerLayer.clip.height=134;
this.cal_ContainerLayer.clip.width=150;
}}
Calendar.prototype.getFirstDOM=function(){
var thedate=new Date();
thedate.setDate(1);
thedate.setMonth(this.month);
thedate.setFullYear(this.year);
return thedate.getDay();
}
Calendar.prototype.getDaysInMonth=function(){
if(this.month!=1){
return this.daysInMonth[this.month]
}else{
//leap year chk
if(Date.isLeapYear(this.year)){
return 29;
}else{
return 28;
}}}
Calendar.prototype.buildString=function(){
var tmpStr='<table width="100%" border="0"  cellspacing="0" cellpadding="2" class="calCloseColor"><TR><TD class="calCloseColor">Calendar</TD><TD align=right class="calCloseColor"><a href="#"><span onClick="return g_Calendar.CloseWindow();" class="calCloseColor" >X</span></a></TD></TR></Table>';
tmpStr+='<table width="100%" border="0" cellspacing="0" cellpadding="2"  class="calBorderColor"><tr><td valign="top" ><table width="100%"  border="0" cellspacing="0" cellpadding="1" class="calBgColor">';
tmpStr+='<tr>';
tmpStr+='<td width="60%" class="cal" align="left">';
if(this.hasDropDown){
tmpStr+='<select class="month" name="month" id="month" onchange="g_Calendar.selectChange();">';
for(var i=0;i<this.months.length;i++){
tmpStr+='<option value="' + i + '"'
if(i==this.month) tmpStr += ' selected';
tmpStr+='>' + this.months[i] + '</option>';
}tmpStr+='</select>';
}else{
tmpStr+='<table  border="0" cellspacing="0" cellpadding="0"><tr><td><a href="javascript: g_Calendar.changeMonth(-1);"><img name="calendar" src="/MSIB21/FrontEnd/skins/default/images/down.gif" width="8" height="12" border="0" alt=""></a></td><td class="cal" width="100%" align="center">' + this.months[this.month] + '</td><td class="cal"><a href="javascript: g_Calendar.changeMonth(+1);"><img name="calendar" src="/MSIB21/FrontEnd/skins/default/images/up.gif" width="8" height="12" border="0" alt=""></a></td></tr></table>';
}tmpStr+='</td>';
tmpStr+='<td width="40%" align="right" class="cal">';
if(this.hasDropDown){
//get round NS4 win32 lenght of year input problem
tmpStr+='<select class="year" name="year" id="year" onchange="g_Calendar.selectYearChange();">';
for(var i=0;i<this.years.length;i++){
tmpStr+='<option value="' + this.years[i] + '"'
if(this.years[i]==this.year) tmpStr+=' selected';
tmpStr+='>' + this.years[i] + '</option>';
}tmpStr+='</select>';
}else{
tmpStr+='<table border="0"  cellspacing="0" cellpadding="0"><tr><td class="cal"><a href="javascript: g_Calendar.changeYear(-1);"><img name="calendar" src="/MSIB21/FrontEnd/skins/default/images/down.gif" width="8" height="12" border="0" alt=""></a></td><td class="cal" width="100%" align="center">' + this.year + '</td><td class="cal"><a href="javascript: g_Calendar.changeYear(+1);"><img name="calendar" src="/MSIB21/FrontEnd/skins/default/images/up.gif" width="8" height="12" border="0" alt=""></a></td></tr></table>'
}tmpStr+='</td>';
tmpStr+='</tr>';
tmpStr+='</table>';
var iCount=1;
var iFirstDOM=(7+this.getFirstDOM()-g_startDay)%7;//prevent calling in loop
var iDaysInMonth=this.getDaysInMonth();//prevent calling in loop
tmpStr+='<table  width="100%" border="0" cellspacing="0" cellpadding="1" class="calBgColor">';
tmpStr+='<tr>';
for(var i=0;i<7;i++){
tmpStr+='<td align="center" height="20" class="calDaysColor">' + this.daysOfWeek[(g_startDay+i)%7] + '</td>';
}tmpStr+='</tr>';
var tmpFrom=parseInt('' + this.dateFromYear + this.dateFromMonth + this.dateFromDay,10);
var tmpTo=parseInt('' + this.dateToYear + this.dateToMonth + this.dateToDay,10);
var tmpCompare;
for(var j=1;j<=6;j++){
tmpStr+='<tr>';
for(var i=1;i<=7;i++){
tmpStr+='<td height="17" align="center" '
if((7*(j-1) + i)>=iFirstDOM+1  && iCount <= iDaysInMonth){
if(iCount==this.day && this.year==this.oYear && this.month==this.oMonth) tmpStr += 'class="calHighlightColor"';
else{
if(i==7-g_startDay || i==((7-g_startDay)%7)+1) tmpStr += 'class="calWeekend"';
else tmpStr += 'class="cal"';
}tmpStr+='>';
//could create a dt obj here and compare that but probably more efficient to convert to a number and compare number as numbers are primitives
tmpCompare=parseInt('' + this.year + padZero(this.month) + padZero(iCount),10);
if(tmpCompare>=tmpFrom && tmpCompare<=tmpTo){

tmpStr+='<a class="cal" href="javascript: g_Calendar.clickDay(' + iCount + ');">' + iCount + '</a>';
}else{
//alert("in disabled");
tmpStr+='<a class="cal" href="javascript: g_Calendar.clickDay(' + iCount + ');">' + iCount + '</a>';
//tmpStr+='<span class="disabled">' + iCount + '</span>';This was the orginal line but changed by mayur for safari purpose.
}iCount++;
}else{
if(i==7-g_startDay || i==((7-g_startDay)%7)+1) tmpStr += 'class="calWeekend"'; else tmpStr +='class="cal"';
tmpStr+='>&nbsp;';
}tmpStr+='</td>'
}tmpStr+='</tr>'
}tmpStr+='</table>';
tmpStr+='</td>';
tmpStr+='</tr>';
tmpStr+='</table>';
IfrRef.style.width=DivRef.offsetWidth;
IfrRef.style.height=DivRef.offsetHeight;
IfrRef.style.top=DivRef.style.top;
IfrRef.style.left=DivRef.style.left;
IfrRef.style.zIndex=DivRef.style.zIndex - 1;
return tmpStr;
}
Calendar.prototype.selectChange=function(){
this.month=document.getElementById('month').selectedIndex;
this.writeString(this.buildString());
}    
Calendar.prototype.selectYearChange=function(){
this.year=year1 + document.getElementById('year').selectedIndex;
this.writeString(this.buildString());
}    
Calendar.prototype.inputChange=function(){
var tmp=browser.ns6?this.cal_ContainerLayer.ownerDocument.forms[0].year:this.cal_ContainerLayer.document.forms[0].year;
if (tmp.value >=1900 || tmp.value <=2100){
this.year=tmp.value;
this.writeString(this.buildString());
}else{
tmp.value=this.year;
}}
Calendar.prototype.changeYear=function(incr){
(incr==1)?this.year++:this.year--;
this.writeString(this.buildString());
}
Calendar.prototype.changeMonth=function(incr){
if (this.month==11 && incr==1){
this.month=0;
this.year++;
}else{
if (this.month==0 && incr==-1){
this.month=11;
this.year--;
}else{
(incr==1)?this.month++:this.month--;
}}
this.writeString(this.buildString());
}    
Calendar.prototype.clickDay=function(day){
e=document.getElementById(this.target);
var tempday=new String(day);
if(tempday.length==1)
tempday="0" + tempday;
var tempmonth=new String(parseInt(this.month)+1);
if(tempmonth.length==1)
tempmonth="0" + tempmonth;
var value=tempday + '-' + tempmonth + '-' + this.year;
document.getElementById(hiddenTextBox).value=value;
document.getElementById(this.target).focus();
e.value=this.formatDateAsString(day,this.month,this.year);
if (browser.ns4) this.cal_ContainerLayer.hidden=true;
if (browser.dom || browser.ie4){
DivRef.style.display="none";
IfrRef.style.display="none";
this.cal_ContainerLayer.style.visibility='hidden';
imgname='';
}

}
Calendar.prototype.formatDateAsString=function(day, month, year){
var delim=eval('/\\' + this.dateDelim + '/g');
switch (this.dateFormat.replace(delim,"")){
case 'ddmmmyyyy': return padZero(day) + this.dateDelim + this.months[month].substr(0,3) + this.dateDelim + year;
case 'ddmmyyyy': return padZero(day) + this.dateDelim + padZero(month+1) + this.dateDelim + year;
case 'mmddyyyy': return padZero((month+1)) + this.dateDelim + padZero(day) + this.dateDelim + year;
case 'yyyymmdd': return year + this.dateDelim + padZero(month+1) + this.dateDelim + padZero(day);
case 'dd-mmm-yyyy': return padZero(day) + this.dateDelim + this.months[month].substr(0,3) + this.dateDelim + year;
}}
Calendar.prototype.writeString=function(str){
if (browser.ns4){
this.cal_ContainerLayer.document.open();
this.cal_ContainerLayer.document.write(str);
this.cal_ContainerLayer.document.close();
}if (browser.dom || browser.ie4){
this.cal_ContainerLayer.innerHTML=str;
}}
var hiddenTextBox;
Calendar.prototype.show=function(event, target, hiddenTarget, bHasDropDown, dateFormat, monthNames, weekDaysName, dateFrom, dateTo){
if(window.disabledCalendarControls)
{ 
  if (target == disabledCalendarControls)
  {  
    return;
  }
}

hiddenTextBox=hiddenTarget;
//calendar can restrict choices betw 2 dts,however no restrictions are made, let them choose any dt betw 1900 & 3000
this.months=monthNames.split(",");
this.daysOfWeek=weekDaysName.split(",");
this.dateFrom=dateFrom || new Date(1900,0,1);
this.dateFromDay=padZero(this.dateFrom.getDate());
this.dateFromMonth=padZero(this.dateFrom.getMonth());
this.dateFromYear=this.dateFrom.getFullYear();
this.dateTo=dateTo || new Date(3000,0,1);
this.dateToDay=padZero(this.dateTo.getDate());
this.dateToMonth=padZero(this.dateTo.getMonth());
this.dateToYear=this.dateTo.getFullYear();
this.hasDropDown=bHasDropDown;
this.dateFormat=dateFormat || 'dd-mmm-yyyy';
switch(this.dateFormat){
case 'dd-mmm-yyyy':
case 'dd-mm-yyyy':
case 'yyyy-mm-dd':
this.dateDelim=' ';
break;
case 'dd/mm/yyyy':
case 'mm/dd/yyyy':
case 'dd/mmm/yyyy':
this.dateDelim='/';
break;}

if(browser.ns4){
if(!this.cal_ContainerLayer.hidden){
this.cal_ContainerLayer.hidden=true;
return;
}}
if(browser.dom || browser.ie4){
if(this.cal_ContainerLayer.style.visibility=='visible'){
this.cal_ContainerLayer.style.visibility='hidden';
DivRef.style.display="none";
IfrRef.style.display="none";
imgname='';
return;
}else{
DivRef.style.display="block";
IfrRef.style.display="block";
DivRef.style.left = event.clientX + 'px';
DivRef.style.top = event.clientY + 'px';
//alert(window.pageXOffset)
}}
if(browser.ie5 || browser.ie4){
var event=window.event;
}if(browser.ns4){
this.cal_ContainerLayer.x=event.x+10;
this.cal_ContainerLayer.y=event.y-5;
}if(browser.ie5 || browser.ie4){
var obj=event.srcElement;
x=0;
while (obj.offsetParent != null){
x+=obj.offsetLeft;
obj=obj.offsetParent;
}x+=obj.offsetLeft;
y=0;
var obj=event.srcElement;
while(obj.offsetParent != null){
y+=obj.offsetTop;
obj=obj.offsetParent;
}y+=obj.offsetTop;
this.cal_ContainerLayer.style.left=x+35;
if(event.y>0)this.cal_ContainerLayer.style.top=y;
}if(browser.ns6){

this.cal_ContainerLayer.style.left=(event.pageX+10)+"px";
this.cal_ContainerLayer.style.top=(event.pageY)+"px";

}
this.target=target;
e=document.getElementById(this.target);
var tmp=e;
if(tmp && tmp.value && tmp.value.split(this.dateDelim).length==3 && tmp.value.indexOf('d')==-1){
var atmp=tmp.value.split(this.dateDelim)
switch(this.dateFormat){
case 'dd-mmm-yyyy':
case 'dd/mmm/yyyy':
for(var i=0;i<this.months.length;i++){
if(atmp[1].toLowerCase()==this.months[i].substr(0,3).toLowerCase()){
this.month=this.oMonth=i;
break;}}
this.day=parseInt(atmp[0],10);
this.year=this.oYear=parseInt(atmp[2],10);
break;
case 'dd/mm/yyyy':
case 'dd-mm-yyyy':
this.month=this.oMonth=parseInt(atmp[1]-1,10); 
this.day=parseInt(atmp[0],10);
this.year=this.oYear=parseInt(atmp[2],10);
break;
case 'mm/dd/yyyy':
case 'mm-dd-yyyy':
this.month=this.oMonth=parseInt(atmp[0]-1,10);
this.day=parseInt(atmp[1],10);
this.year=this.oYear=parseInt(atmp[2],10);
break;
case 'yyyy-mm-dd':
this.month=this.oMonth=parseInt(atmp[1]-1,10);
this.day=parseInt(atmp[2],10);
this.year=this.oYear=parseInt(atmp[0],10);
break;
}}else{//no dt set,default to today
var theDate=new Date();
this.year=this.oYear=theDate.getFullYear();
this.month=this.oMonth=theDate.getMonth();
this.day=this.oDay=theDate.getDate();
}this.writeString(this.buildString());
//and then show it!
if(browser.ns4){
this.cal_ContainerLayer.hidden=false;
}if(browser.dom || browser.ie4){
this.cal_ContainerLayer.style.visibility='visible';
}
}
Calendar.prototype.hide=function(){
if(browser.ns4) this.cal_ContainerLayer.hidden=true;
if(browser.dom || browser.ie4){
}}
var imgname;
function handleDocumentClick(e){
if(document.getElementById('cal_container')=="undefined")
{if(browser.ie4 || browser.ie5) e=window.event;
if(browser.ns6){
var bTest=(e.pageX > parseInt(g_Calendar.cal_ContainerLayer.style.left,10) && e.pageX <  (parseInt(g_Calendar.cal_ContainerLayer.style.left,10)+125) && e.pageY < (parseInt(g_Calendar.cal_ContainerLayer.style.top,10)+125) && e.pageY > parseInt(g_Calendar.cal_ContainerLayer.style.top,10));
if(e.target.name!='imgCalendar' && e.target.name!='month'  && e.target.name!='year' && e.target.name!='calendar' && !bTest){
g_Calendar.hide();
}}
if(browser.ie4 || browser.ie5){
//extra tst to see if user clicked inside the calendar but not on a valid date, we dont want it to disappear in this case
var bTest=(e.x > parseInt(g_Calendar.cal_ContainerLayer.style.left,10) && e.x <  (parseInt(g_Calendar.cal_ContainerLayer.style.left,10)+125) && e.y < (parseInt(g_Calendar.cal_ContainerLayer.style.top,10)+125) && e.y > parseInt(g_Calendar.cal_ContainerLayer.style.top,10));
if(e.srcElement.name!='imgCalendar' && e.srcElement.name!='month' && e.srcElement.name!='year' && !bTest & typeof(e.srcElement)!='object'){
g_Calendar.hide();
}}
if(browser.ns4) g_Calendar.hide();
}else{
//finding how far the window has scrolled
var scrOfX=0, scrOfY=0;
if(typeof(window.pageYOffset)=='number'){
//NS compliant
scrOfY=window.pageYOffset;
scrOfX=window.pageXOffset;
}else if(document.body && (document.body.scrollLeft || document.body.scrollTop)){
//DOM compliant
scrOfY=document.body.scrollTop;
scrOfX=document.body.scrollLeft;
}else if(document.documentElement &&
(document.documentElement.scrollLeft || document.documentElement.scrollTop)){
//IE6 stds compliant mode
scrOfY=document.documentElement.scrollTop;
scrOfX=document.documentElement.scrollLeft;
}if (typeof g_Calendar=='undefined'){
return;
}e=window.event;
var calX1=g_Calendar.cal_ContainerLayer.style.left;
calX1=eval(calX1.substring(0,calX1.length-2)) - scrOfX;
var calY1=g_Calendar.cal_ContainerLayer.style.top;
calY1=eval(calY1.substring(0,calY1.length-2)) - scrOfY ;
var calX2=eval(calX1) + 200;
var calY2=eval(calY1) + 180;
if(e.x>=calX1 && e.x<=calX2 && e.y>=calY1 && e.y<=calY2){
}else{
if(imgname!='imgCalendar' && e.srcElement.name!='imgCalendar')
{g_Calendar.CloseWindow();}}
}}
//utility func
function padZero(num){
return ((num <= 9) ? ("0" + num) : num);
}
//Finally licked extending  native dt obj
Date.isLeapYear=function(year){ if (year%4==0 && ((year%100!=0) || (year%400==0))) return true; else return false; }
Date.daysInYear=function(year){ if (Date.isLeapYear(year)) return 366; else return 365;}
var DAY=1000*60*60*24;
Date.prototype.addDays=function(num){
return new Date((num*DAY)+this.valueOf());
}
//events capturing, careful you dont override this by setting something in the onload event of the body tag
function fncal(){
new Calendar(new Date());
if(browser.ns4){
if(typeof document.NSfix=='undefined'){
document.NSfix=new Object();
document.NSfix.initWidth=window.innerWidth;
document.NSfix.initHeight=window.innerHeight;
}}}

/*window.onload=function()
{
debugger;
new Calendar(new Date());
if(browser.ns4){
if(typeof document.NSfix=='undefined'){
document.NSfix=new Object();
document.NSfix.initWidth=window.innerWidth;
document.NSfix.initHeight=window.innerHeight;
}}}*/


if (browser.ns4) window.onresize=function(){
if (document.NSfix.initWidth!=window.innerWidth || document.NSfix.initHeight!=window.innerHeight) window.location.reload(false);
}//ns4 resize bug workaround
window.document.onclick=handleDocumentClick;
window.onerror=function(msg,url,line){
}
Calendar.prototype.CloseWindow=function(){
DivRef.style.display="none";
IfrRef.style.display="none";
this.cal_ContainerLayer.style.visibility='hidden';
imgname='';
return false;
}
var hitId;
/* Commented By Paresh*/
document.onkeydown=function ()
{hitId=1;
	if(window.event)
	{ 
		if(event.keyCode == 13)
		{imgname='imgCalendar';
		}
	}
}
