	/*
	****************************************************
	Author : Lea Smart
	Source : www.totallysmartit.com
	Date : 7/3/2001
	DHTML Calendar
	Version 1.1
	
	You are free to use this code if you retain this header.
	You do not need to link to my site (be nice though!)
	******************************************************
	*/
	var timeoutDelay = 1000; // milliseconds, change this if you like, set to 0 for the calendar to never auto disappear
	
	// preload images
	var imgUp = new Image(8,12);
	imgUp.src = 'images/up.gif';
	var imgDown = new Image(8,12);
	imgDown.src = 'images/down.gif';
	var dateFormat = "mm/dd/yyyy";
	
	// used by timeout auto hide functions
	var timeoutId = false;
	
	// the now standard 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 require this written to the HEAD section
	
	if (browser.dom || browser.ie4){
	    document.writeln('<style>');
		document.writeln('#container {');
		document.writeln('position : absolute;');
		document.writeln('left : 100px;');
		document.writeln('top : 100px;');
		document.writeln('width : 112px;');;
		document.writeln('height : 165px;');
                document.writeln('clip:rect(0px 124px 165px 0px);');
		//document.writeln('overflow : hidden;');
		document.writeln('visibility : hidden;');
		document.writeln('z-index : 2;');
		document.writeln('background-color : #CCCCCC');
		document.writeln('}');
		document.writeln('</style>')
		document.write('<div id="container"');
		if (timeoutDelay) document.write(' onmouseout="calendarTimeout();" onmouseover="if (timeoutId) clearTimeout(timeoutId);"');
		document.write('></div>');
	}
	
	var g_Calendar;  // global to hold the calendar reference, set by constructor
	
	function calendarTimeout(){
	  if (browser.ie4 || browser.ie5){
	    if (window.event.srcElement && window.event.srcElement.name!='month') 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;
 
	  // some constants needed throughout the program
	  this.daysOfWeek = new Array("Su","Mo","Tu","We","Th","Fr","Sa");
	  this.months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	  this.daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	  
	  if (browser.ns4){
	    var tmpLayer = new Layer(124);
		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('container'):document.all.container;
	  }
	  this.containerLayer = tmpLayer;
	  /*if (browser.ns4) {
	    this.containerLayer.clip.height=127;
	    this.containerLayer.clip.width=126;
	  }*/

	}
	
 	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 {
	     // is it a leap year
		    if (this.sCalIsLeapYr(this.year)) {
			  return 29;
			}
		    else {
			  return 28;
			}
	   }
	}
	 
	Calendar.prototype.sCalIsLeapYr = function() {
	  if (this.year%4==0 && ((this.year%100!=0) || (this.year%400==0))) return true; else return false;
	}
	
	Calendar.prototype.buildString = function(){
	  var tmpStr = '<form onSubmit="this.year.blur();return false;"><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" 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="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="images/up.gif" width="8" height="12" border="0" alt=""></a></td></tr></table>';
	  }
	  tmpStr += '</td>';
	  /* observation : for some reason if the below event is changed to 'onChange' rather than 'onBlur' it totally crashes IE (4 and 5)!
	  */
	  tmpStr += '<td width="40%" align="right">';
	  
	  if (this.hasDropDown) { 
	    tmpStr += '<input class="year" type="text" size="';
	    // get round NS4 win32 lenght of year input problem
	    (browser.ns4 && browser.platform=='Win32')?tmpStr += 1:tmpStr += 4;
	  tmpStr += '" name="year" maxlength="4" onBlur="g_Calendar.inputChange();" value="' + this.year + '">';
	  } else {
	  tmpStr += '<table border="0" cellspacing="0" cellpadding="0"><tr><td><a href="javascript: g_Calendar.changeYear(-1);"><img name="calendar" src="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="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 = this.getFirstDOM(); // to prevent calling it in a loop
	  var iDaysInMonth = this.getDaysInMonth(); // to prevent calling it in a loop
	  
	  tmpStr += '<table width="100%" border="0" cellspacing="0" cellpadding="1" class="calBgColor">';
	  tmpStr += '<tr>';
	    for (var i=0;i<this.daysOfWeek.length;i++){
		  tmpStr += '<td align="center" class="calDaysColor">' + this.daysOfWeek[i] + '</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;
	  var rowEnter = false;
	  for (var j=1;j<=6;j++){
	     tmpStr += '<tr>';
             rowEnter=false;
	     for (var i=1;i<=7;i++){
                  // tmpStr += '<td width="16" align="center" '
		   if ( (7*(j-1) + i)>=iFirstDOM+1  && iCount <= iDaysInMonth){
                   rowEnter=true;
                   tmpStr += '<td width="16" align="center" '
		     if (iCount==this.day && this.year==this.oYear && this.month==this.oMonth) tmpStr += 'class="calHighlightColor"';
			 else tmpStr += 'class="cal"';
		     tmpStr += '>';
			 /* could create a date object here and compare that but probably more efficient to convert to a number
			   and compare number as numbers are primitives */
			 tmpCompare = parseInt('' + this.year + sCalPadZero(this.month) + sCalPadZero(iCount),10);
			 if (tmpCompare >= tmpFrom && tmpCompare <= tmpTo) {
			   tmpStr += '<a class="cal" href="javascript: g_Calendar.clickDay(' + iCount + ');">' + iCount + '</a>';
			 } else {
			   tmpStr += '<span class="disabled">' + iCount + '</span>';
			 }
			 iCount++;
                        }else if(j==6&&rowEnter==false){
                          break;
                        } else {
                          tmpStr += '<td width="16" align="center" >&nbsp;';
                        }        
                   /*else {
		     tmpStr += '>&nbsp;';
                   }*/
		   tmpStr += '</td>'
		 }
                 tmpStr += '</tr>';
	  }
          tmpStr += '<tr><td colspan="7"><hr/>';
          tmpStr += '<div style="text-align:center;">';
          tmpCompare = parseInt('' + this.year + sCalPadZero(this.month) + sCalPadZero(this.day),10);
	  if (tmpCompare >= tmpFrom && tmpCompare <= tmpTo) {
	   tmpStr += '<a class="cal u-cal" href="#" onClick="g_Calendar.selectToday();return false;">today</a>';
	  } else {
	    tmpStr += '<span class="disabled">today</span>';
          }
          tmpStr += '</div>';
          tmpStr += ' </td></tr></table></td></tr></table>';
          tmpStr += '</form>';
	  return tmpStr;
	}
	Calendar.prototype.selectToday = function(){
	  var theDate = new Date();
	  this.year =  theDate.getFullYear();
	  this.month = theDate.getMonth();
	  this.day = theDate.getDate();
	  this.clickDay(this.day)
        }
	
	Calendar.prototype.selectChange = function(){
	  this.month = browser.ns6?this.containerLayer.ownerDocument.forms[0].month.selectedIndex:this.containerLayer.document.forms[0].month.selectedIndex;
	  this.writeString(this.buildString());
	}
	
	Calendar.prototype.inputChange = function(){
	  var tmp = browser.ns6?this.containerLayer.ownerDocument.forms[0].year:this.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){
	   day = ''+day;
	   if(day.length == 1){
	     day = '0' + day;
	   }
	   var tmp = eval('document.' + this.target);
	   if (this.dateFormat=='dd-mmm-yyyy') tmp.value = day + this.dateDelim + this.months[this.month].substr(0,3) + this.dateDelim + this.year;
	   // Added new format by Navis
	   if (this.dateFormat=='dd-mon-yyyy') tmp.value = day + this.dateDelim + this.months[this.month].substr(0,3) + this.dateDelim + this.year;
	   if (this.dateFormat=='dd-mm-yyyy') tmp.value = day + this.dateDelim + (this.month+1) + this.dateDelim + this.year;
	   if (this.dateFormat=='mm-dd-yyyy') tmp.value = (this.month+1) + this.dateDelim + day + this.dateDelim + this.year;
	   if (this.dateFormat=='mon-dd-yyyy') tmp.value = this.months[this.month].substr(0,3) + this.dateDelim + day + this.dateDelim + this.year;
	   //Added  new format  yyyy-mm-dd 
	   if (this.dateFormat=='yyyy-mm-dd') tmp.value = this.year + this.dateDelim +(this.month+1)+ this.dateDelim +day;
	   if (this.dateFormat=='dd/mm/yyyy') tmp.value = day + this.dateDelim + (this.month+1) + this.dateDelim + this.year;
	   if (this.dateFormat=='mm/dd/yyyy') tmp.value = (this.month+1) + this.dateDelim + day + this.dateDelim + this.year;
	   
	    if (browser.ns4) this.containerLayer.hidden=true;
	    if (browser.dom || browser.ie4){
	      this.containerLayer.style.visibility='hidden'
	    }
	}
	
	Calendar.prototype.writeString = function(str){
	  if (browser.ns4){
	    this.containerLayer.document.open();
	    this.containerLayer.document.write(str);
	    this.containerLayer.document.close();
	  } 
	  if (browser.dom || browser.ie4){
	    this.containerLayer.innerHTML = str;
	  }
	}
	
	Calendar.prototype.show = function(event, target, bHasDropDown, dateFormat, dateFrom, dateTo){
	// calendar can restrict choices between 2 dates, if however no restrictions
	// are made, let them choose any date between 1900 and 3000
	try{
	if (dateFrom) this.dateFrom = dateFrom; else this.dateFrom = new Date(1900,0,1);
	this.dateFromDay = sCalPadZero(this.dateFrom.getDate());
	this.dateFromMonth = sCalPadZero(this.dateFrom.getMonth());
	this.dateFromYear = this.dateFrom.getFullYear();
	if (dateTo) this.dateTo = dateTo; else this.dateTo = new Date(3000,0,1);
	this.dateToDay = sCalPadZero(this.dateTo.getDate());
	this.dateToMonth = sCalPadZero(this.dateTo.getMonth());
	this.dateToYear = this.dateTo.getFullYear();
	this.hasDropDown = bHasDropDown;
	if (dateFormat) this.dateFormat = dateFormat; else this.dateFormat = dateFormatOverride;//'dd-mmm-yyyy';
	}
	catch(e){
	this.dateFrom = new Date(1900,0,1);
	this.dateTo = new Date(3000,0,1);
        }
	//Added  new format  yyyy-mm-dd 
	
	switch (this.dateFormat){
	  case 'dd-mmm-yyyy':
	  case 'dd-mon-yyyy':
       	  case 'dd-mm-yyyy':
       	  case 'mm-dd-yyyy':
          case 'mon-dd-yyyy':
	    	this.dateDelim = '-';
		break;
	  case 'yyyy-mm-dd':
	  	this.dateDelim = '-';
		break;	
	  case 'dd/mm/yyyy':
	  case 'mm/dd/yyyy':
	    	this.dateDelim = '/';
		break;
	  default:
	        this.dateFormat='dd-mmm-yyyy';
                this.dateDelim = '-';
	}
	
	  if (browser.ns4) {
	    if (!this.containerLayer.hidden) {
		  this.containerLayer.hidden=true;
		  return;
		}
	   }
	  if (browser.dom || browser.ie4){
	    if (this.containerLayer.style.visibility=='visible') {
		  this.containerLayer.style.visibility='hidden';
		  return;
		}  
	  }

	  if (browser.ie5 || browser.ie4){
	    var event = window.event;
	  }
	  if (browser.ns4){
	    this.containerLayer.x = event.x+10;
	    this.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.containerLayer.style.left = x+35;
		if (event.y>0)this.containerLayer.style.top = y;
	  }
	  if (browser.ns6){
	    this.containerLayer.style.left = event.pageX+10;
		this.containerLayer.style.top = event.pageY-5;
	  }
	  this.target = target;
	  var tmp = eval('document.' + this.target);
	  if (tmp && tmp.value && tmp.value.split(this.dateDelim).length==3){
	    var atmp = tmp.value.split(this.dateDelim)
		this.day = this.oDay = this.dateFormat.substr(0,2)=='dd'?parseInt(atmp[0],0):parseInt(atmp[1],0);
		if (this.dateFormat=='dd-mmm-yyyy' ||this.dateFormat=='dd-mon-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;
		    }
		  }
		} 
		else {
		  if (this.dateFormat.substr(0,2)=='dd') this.month = this.oMonth = parseInt(atmp[1]-1,0); 
		  else {
		  	if ( this.dateFormat=='mon-dd-yyyy' ) {
		  		for (var i=0;i<this.months.length;i++){
		    			if (atmp[0].toLowerCase()==this.months[i].substr(0,3).toLowerCase()){
		      				this.month = this.oMonth = i;
			  			break;
		    			}
		  		}	
		  	}
		  	else this.month = this.oMonth = parseInt(atmp[0]-1,0);
		  }
		}
		this.year= this.oYear = parseInt(atmp[2],10);
	  } else { // no date 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.containerLayer.hidden=false;
	   }
	  if (browser.dom || browser.ie4){
	      this.containerLayer.style.visibility='visible';
	  }
	}
	
	Calendar.prototype.hide = function(){
	  if (browser.ns4) this.containerLayer.hidden = true;
	  if (browser.dom || browser.ie4){
	    this.containerLayer.style.visibility='hidden';
	  }
	}
		
	function handleDocumentClick(evt){
	if(is.ie) 
		{
		evt = (evt) ? evt : ((window.event) ? window.event : "")
		srcEl = evt.srcElement
	  if (is.ie4 || is.ie5)
	  	{
			// extra test to see if user clicked inside the calendar but not on a valid date, we don't want it to disappear in this case
			var bTest = (evt.x > parseInt(g_Calendar.containerLayer.style.left,10) && evt.x <  (parseInt(g_Calendar.containerLayer.style.left,10)+125) && evt.y < (parseInt(g_Calendar.containerLayer.style.top,10)+125) && evt.y > parseInt(g_Calendar.containerLayer.style.top,10));
			if (evt.srcElement.name!='imgCalendar' && evt.srcElement.name!='month' && evt.srcElement.name!='year' && !bTest & typeof(evt.srcElement)!='object')
				{
				g_Calendar.hide(); 
				}
			}
		}
	else
		{
		srcEl = evt.target
		if (srcEl.name!='imgCalendar' && srcEl.name!='month'  && srcEl.name!='year' && srcEl.name!='calendar')
			{
			g_Calendar.hide(); 
			}
		}
	}

	function doCalendar(){
	   //var aCalendar = new Calendar(new Date(2001,1,13));
	   var aCalendar = new Calendar(new Date());
	}
	// utility function
	function sCalPadZero(num) {
	  return ((num <= 9) ? ("0" + num) : num);
	}
	/** adapted from http://tech.irt.org/articles/js052/index.htm */

	function sCalAddDays(startDate,addition) {
	    
		var accumulate    = new Array(0,31, 59, 90,120,151,181,212,243,273,304,334);
		var accumulateLY  = new Array(0,31, 60, 91,121,152,182,213,244,274,305,335);

	    var year = startDate.getFullYear();
		var month = startDate.getMonth();
		var day = startDate.getDate();
		
		if (sCalIsLeapYr(year)) var number = day + accumulateLY[month] + addition;
	    else                var number = day + accumulate[month]   + addition;
	    var days = sCalIsDaysInYear(year);
	   
	    while (number > days) {
	        number -= days;
	        days = sCalIsDaysInYear(++year);
	    }
	
	    while (number < 1) {
	        days = sCalIsDaysInYear(--year);
	        number += days;
	    }
	
	    month = 0;
	
	    if (sCalIsLeapYr(year)) {
	        while (number > accumulateLY[month]) { month++; }
	        day = number - accumulateLY[--month];
	    }
	    else {
	        while (number > accumulate[month]) { month++; }
	        day = number - accumulate[--month];
	    }
		return new Date(year,month,day);
	}
	
	function sCalIsDaysInYear(year) { if (sCalIsLeapYr(year)) return 366; else return 365; }
	function sCalIsLeapYr(year) { if (year%4==0 && ((year%100!=0) || (year%400==0))) return true; else return false; }
