function clearDate(obj) {
  obj.value = "";
  return false;
}
function Calender(name) {
	this.name = name;
	this.cell_height = 20;
	this.cell_width = 24;
	this.mArray = new Array("Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep", "Oct", "Nov","Dec");
	this.mnArray = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
	this.year_start=1900;
        this.year_down=100;
	this.year_up=1;
	this.brcolor = '#000000';
	this.bgcolor = '#EEEEEE';
	this.cellcolor = '#EEEEEE';
	this.cellhover = '#FFFFFF';

	//this.instance = document.calender_layer;
this.getDay = function () {
	var tsy = this.calForm.yearSel;
	var tsm = this.calForm.monthSel;

	var selectIndex = tsm.selectedIndex
	timeA = new Date(tsy.options[tsy.selectedIndex].value, tsm.options[tsm.selectedIndex].value,1);
	timeC = new Date(tsy.options[tsy.selectedIndex].value, tsm.options[tsm.selectedIndex].value-1,1);
	timeDifference = timeA - 86400000;
	timeB = new Date(timeDifference);
	var daysInMonth = timeB.getDate();
	//timeB.setDate(30);
	startD = timeC.getDay();
	//alert(startD);

	var s2 = '';
	var d = 0;
	for(i=0; i<6; i++) {
		var y = this.cell_height*i;
		for(j=0; j<7; j++) {
			var x = this.cell_width*j;
			if(i*7 +j >=startD) d++
			if(d >0 && d <=daysInMonth) {
				s2 += this.getCell(i,j,x,y,d,true)
			} else { s2 += this.getCell(i,j,x,y,d,false) }

		}
	}
	this.write(s2);
} //--end getDay()

this.init = function() {
	var s = '';
	s = this.get();
	document.write(s);
	document.close();

	var now = new Date();
	var y = now.getYear();
	if(NS4a || NS6a) y +=1900;
	var m = now.getMonth();
	//alert(this.calForm)
	this.getNames();

	var ylist = this.calForm.yearSel;
	var mlist = this.calForm.monthSel;

	for(i=0, j=this.year_start; j<=y+this.year_up; i++, j++) { ylist.options[i] = new Option(j);
		ylist.options[i].value = j;
		if(j == y) ylist.options[i].selected = true;
	}
	for(i=0; i<12; i++) {
		mlist.options[i] = new Option(this.mArray[i]);
		mlist.options[i].value = i+1;
		if(i == m) mlist.options[i].selected = true;
	}
	//this.getDay()
} //--end init()

this.monChange = function (b) {
	if(b) {
		if(this.calForm.monthSel.selectedIndex < this.mArray.length-1 ) {
			this.calForm.monthSel.selectedIndex ++;
		} else if(this.calForm.yearSel.selectedIndex < this.year_down+this.year_up-1 ) {
			this.calForm.monthSel.selectedIndex = 0;
			this.calForm.yearSel.selectedIndex ++;
		}
	} else {
		if(this.calForm.monthSel.selectedIndex > 0 ) {
			this.calForm.monthSel.selectedIndex --;
		} else if(this.calForm.yearSel.selectedIndex > 0 ) {
			this.calForm.monthSel.selectedIndex = this.mArray.length-1;
			this.calForm.yearSel.selectedIndex --;
		}
	}
	this.getDay();
} //--end monChange()

this.setDate = function(day) {
	var sd = day.toString();
	sd = sd.length==1?'0'+sd:sd
	var dateStr = sd+'-'+this.mnArray[this.calForm.monthSel.selectedIndex]+'-'+this.calForm.yearSel[this.calForm.yearSel.selectedIndex].value;
	this.caller.value = dateStr;
	this.hide();
	return(false);
}

this.getDate = function(caller) {

	this.caller = caller;
	var v = caller.value;

	if(v) {
	var m = Number(v.substr(3,2))-1;
	var y = Number(v.substr(6,4))-Number(this.calForm.yearSel[0].value);
	this.calForm.monthSel.selectedIndex = m;
	this.calForm.yearSel.selectedIndex = y;
	}
	this.show();
	return false;
}
	this.compare = Calendar_compare;

	if(IE4a) {
		this.show = Calendar_show_IE;
		this.hide = Calendar_hide_IE;
		this.getCell = Calendar_getCell_IE;
		this.get = Calendar_get_IE;
		this.getNames = Calendar_getNames_IE;
		this.write = Calendar_write_IE;
	}
	else if(NS4a) {
		this.getNames = Calendar_getNames_NS;
		this.getCell = Calendar_getCell_NS;
		this.get = Calendar_get_NS;
		this.write = Calendar_write_NS;
		this.show = Calendar_show_NS;
		this.hide = Calendar_hide_NS;
	}
	else if(NS6a) {
		this.getNames = Calendar_getNames_NS6;
		this.getCell = Calendar_getCell_IE;
		this.get = Calendar_get_IE;
		this.write = Calendar_write_IE;
		this.show = Calendar_show_IE;
		this.hide = Calendar_hide_IE;
	}
	//this.init();
}



// FUNCTION
// IE 4++
function Calendar_show_IE(){
    	obj = this.caller;
          var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop+obj.offsetHeight;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	this.cal_layer.style.top = curtop+"px";
	this.cal_layer.style.left = curleft+"px";
	this.cal_layer.style.visibility = this.date_layer.style.visibility='visible'
	this.getDay();
}//end show
function Calendar_hide_IE() {
	this.cal_layer.style.visibility = 'hidden';
	this.date_layer.innerHTML = '';
	this.date_layer.style.visibility = 'hidden';
}// end hide;

function Calendar_getCell_IE(i,j,x,y,d,b) {
	if(b) {
		//alert(d);
		return ( '<div class="layer_cell" id="c'+i+j+'" style="width:20px; height:15px; left: '+x+'px; top: '+y+'px" onclick="'+this.name+'.setDate('+d+')" onmouseout="this.style.backgroundColor=bgcolor" onmouseover="this.style.cursor=\'hand\'; bgcolor=this.style.backgroundColor; this.style.backgroundColor=\''+this.cellhover+'\'">'+d+'</div>\n' )
	}
	else {
		return ( '<div class="layer_cell" id="c'+i+j+'" style="width:20px; height:15px; left: '+x+'px; top: '+y+'px"></div>\n' )
	}
} //getCell

function Calendar_get_IE() {
	return ( '<div id="'+this.name+'_layer" style="left:100px; top:100px; width:'+(this.cell_width*7+4)+'px; height:'+(this.cell_height*9)+'px" class="layer_cal">'+
	'<form name="'+this.name+'Form" method="post" action="">'+
  '<table width="'+(this.cell_width*7+2)+'" border="0" cellspacing="1" cellpadding="0" align="center">'+
   '<tr>'+
     '<td colspan="7" background="layoutImg/top_bg.gif" align="right"><a href="javascript:'+this.name+'.hide()"><img src="layoutImg/close.gif" border="0"></a></td>'+
   '</td></tr><tr>'+
    '  <td align="center"><a href="javascript:'+this.name+'.monChange(false)" class="layer_top"><img src="layoutImg/left_shift.gif" border="0"></a></td>'+
     ' <td colspan="5" align="center"> <select name="yearSel" class="layer_top" onchange="'+this.name+'.getDay()">'+
     '  </select>&nbsp;<select name="monthSel" class="layer_top" onchange="'+this.name+'.getDay()">'+
        '</select></td>'+
     '<td align="center"><a href="javascript:'+this.name+'.monChange(true)"><img src="layoutImg/right_shift.gif" border="0"></a></td>'+
    '</tr>'+
	'<tr height="13"><td class="layer_head" width="15%">sun</td><td class="layer_head" width="14%">mon</td>'+
	'<td class="layer_head" width="14%">tue</td><td class="layer_head" width="14%">wed</td>'+
	'<td class="layer_head" width="14%">thu</td><td class="layer_head" width="14%">fri</td>'+
	'<td class="layer_head" width="15%">sat</td></tr>'+
  '</table>'+
'</form>'+
'<div id="'+this.name+'_date_layer" style="left:2px; top:57px; width:'+(this.cell_width*7)+'px; height:'+(this.cell_height*6)+'px" class="layer_date" >a'+
'</div>'+
'</div>' )
} //Calendar_get_IE

// NS 4++
function Calendar_show_NS(){
	this.cal_layer.visibility = this.date_layer.visibility='show'
	this.getDay();
}//end show
function Calendar_hide_NS() {
	this.cal_layer.visibility = 'hide';
	this.date_layer.document.write('');
	this.date_layer.document.close();
	this.date_layer.document = 'hide';
}// end hide

function Calendar_getCell_NS(i,j,x,y,d,b) {
	if(b) { return ( '<layer id="c'+i+j+'" class="layer_celNS" width="20" height="15" left="'+x+'" top="'+y+'" z-index="1" bgcolor="'+this.cellcolor+'" onmouseout="this.bgColor=\''+this.cellcolor+'\'" onmouseover="this.bgColor=\''+this.cellhover+'\';"><a href="javascript:void(0)" onclick="return('+this.name+'.setDate('+d+'))">&nbsp;'+d+'&nbsp;</a></layer>\n' )
	}
	else { return ( '<layer id="c'+i+j+'" width="20" height="15" left="'+x+'" top="'+y+'"></layer>\n' )
	}
}// getCell

function Calendar_get_NS() {
	return ( '<layer id="'+this.name+'_layer" z-index="1" left="100" top="100" width="'+(this.cell_width*7)+'" height="'+(this.cell_height*9)+'" class="layer_cal">'+
	'<form name="'+this.name+'Form" method="post" action="">'+
  '<table width="'+(this.cell_width*7)+'" border="0" cellspacing="1" cellpadding="0">'+
   '<tr>'+
     '<td colspan="7" background="layoutImg/top_bg.gif" align="right"><a href="javascript:'+this.name+'.hide()"><img src="layoutImg/close.gif" border="0"></a></td>'+
   '</td></tr><tr>'+
    '<td colspan="7" class="layer_top" align="center"><a href="javascript:'+this.name+'.monChange(false)" class="layer_top"><img src="layoutImg/left_shift.gif" border="0">'+
       '</a><select name="yearSel" onchange="'+this.name+'.getDay()">'+
        '</select><select name="monthSel" onchange="'+this.name+'.getDay()">'+
        '</select><a href="javascript:'+this.name+'.monChange(true)"><img src="layoutImg/right_shift.gif" border="0"></a></td>'+
    '</tr>'+
	'<tr><td class="layer_head" width="15%">sun</td><td class="layer_head" width="14%">mon</td>'+
	'<td class="layer_head" width="14%">tue</td><td class="layer_head" width="14%">wed</td>'+
	'<td class="layer_head" width="14%">thu</td><td class="layer_head" width="14%">fri</td>'+
	'<td class="layer_head" width="15%">sat</td></tr>'+
  '</table>'+
'</form>'+
'<layer id="'+this.name+'_date_layer" z-index="1" left="2" top="60" width="'+(this.cell_width*7)+'" height="'+(this.cell_height*6)+'" class="layer_date">'+
'&nbsp;</layer>'+
'</layer>' )
} //Calendar_get_NS

function Calendar_getNames_IE() {
	this.calForm = eval(this.name+'Form');
	this.date_layer = eval(this.name+'_date_layer');
	this.cal_layer = eval(this.name+'_layer');
}
function Calendar_getNames_NS() {
	this.cal_layer = eval('document.layers.'+this.name+'_layer.document.layers.'+this.name+'_layer');
	this.date_layer = eval("this.cal_layer.document."+this.name+'_date_layer');
	this.calForm = eval("this.cal_layer.document."+this.name+'Form');
	//alert(this.date_layer.name)
}
function Calendar_getNames_NS6() {
	this.cal_layer = document.getElementById(this.name+'_layer');
	this.date_layer = document.getElementById(this.name+'_date_layer');
	this.calForm = this.cal_layer.firstChild;
	//alert(this.date_layer)
}

function Calendar_write_IE(s) {
	this.date_layer.innerHTML = s;
}
function Calendar_write_NS(s) {
	this.date_layer.document.write(s);
	this.date_layer.document.close();
}

function Calendar_compare(date, diff) {
	var v = this.caller.value;
	var d = Number(v.substr(0,2));
	var m = Number(v.substr(3,2))-1;
	var y = Number(v.substr(6,4));
	//alert(v+','+y+','+m+','+d)
	var s = m+' '+d+', '+y;
	var thisdate = new Date(y,m,d);
	//alert(thisdate)
	var t1 = thisdate.getTime();
	var t2 = date.getTime();
	return(t1-t2+diff) ;
}

