var field;
var start;
var mx;
var opt;
var wid;
var uselastday=0;
var x=0;
var y=0;
var wid=370;
var hid=250;
var totcols = 16;
var pickerDiv;
var iFrameDiv;
var backcolor="#f8f8b0"
var iFrameDivID = "datepickeriframe";
var sx;
var sy;
var focus;

function popCal(op,fld,maxm,fokus){
  var parent = document.form1.elements[fld];
//    if (e != '') {
 //       ex = e.screenX;						//absolute screen
  //      ey = e.screenY;						//absolute screen
//		alert (ex+' '+ey)									
 //   }
  focus = fokus;
  x = parent.offsetLeft;  // + parent.offsetWidth;
  y = parent.offsetTop;

  var fwid = parent.offsetWidth;
  var fhid = parent.offsetHeight;
  
  parent.style.backgroundColor = backcolor;
  while (parent.offsetParent) {
    parent = parent.offsetParent;
    x += parent.offsetLeft;
    y += parent.offsetTop ;
  }
	month="";
	year="";
	day="";
    field = fld
	if (op< 0) {uselastday=1;op=0-op;}else{uselastday=0}
	opt = op
	start = maxm - (maxm % 10)
	mx = maxm
	if (op==2){wid=290;totcols=11}
    if (op==3){wid=240; hid=205;totcols=10}
	getCenter()
	

//alert('sx: '+sx+' sy: '+sy+' frameWidth: '+frameWidth+' frameHeight: '+frameHeight+' x: '+x+' y: '+y+' wid: '+wid+' hid: '+hid+' fwid: '+fwid+' fhid: '+fhid)
   if (y < sy){y+=fhid}else{y-=hid-10}
   if (x > sx){x+=fwid-wid}
   if (y<10) {y=10}
   if (x<10) {x=10}
// alert(x + " " + y)  
	//var xt = screen.availWidth - wid - 5
	//var yt = screen.availHeight - hid - 5
//	var xt = document.documentElement.clientWidth - wid - 5
//	var yt = document.documentElement.clientHeight - hid - 5
//	alert (document.body.clientWidth + ' ' +document.body.clientHeight)
//	alret (document.body.scrollX + ' ' + document.body.scrollY) 
//	if (ex > xt) {x-=(ex-xt)}
//	if (ey > yt) {y-=(ey-yt)}
//x=ex;
//y=ey;
//x=0
//y=0
//	alert(document.documentElement.clientWidth+" "+	document.documentElement.clientHeight)
//	alert('ex: ' + ex + ' xt: '+xt+' x: ' + x + ' ey: ' + ey + ' yt: '+yt+' y: '+y )
	buildCal();
	}
	
var month;
var year;
var day;
var monthnum;

function doChk() {
   var med = document.getElementById('msg')
   if (!year) {
   	  med.textContent ='Please select year';
   	  med.innerText ='Please select year';
	  med.style.backgroundColor = "#ADD8E6";
	  }
   if (!month) {
   	  med.textContent ='Please select month';
   	  med.innerText ='Please select month';
	  med.style.backgroundColor = "#ADD8E6";
	  }	
   if (((month) && (year)) || (opt==3)) {
   	  if ((!day)&& ( opt!=3)) {if (uselastday==1){day = 32 - new Date(year, monthnum, 32).getDate()}else{day=1}};
   	  if (opt==3) { t=""}
	  if (opt==2) {t=month+" "}
	  if (opt==1) {t=month + " " + day + ","}; 
   	  if (year <= mx) {t +=" 20"+year}else{t+=" 19"+year};
	     //var fe = document.getElementByName(field)	
   	  	 //fe.value = t;
      	 //fe.style.backgroundColor = "white";
		 document.form1.elements[field].value = t;
		 closeIt();
   }	 
}
function closeIt(){
	document.form1.elements[field].style.backgroundColor = "white";
	pickerDiv.style.visibility = "hidden";
	pickerDiv.style.display = "none";
    adjustiFrame();
//	setTimeout(function(){document.form1.elements(focus).focus();},10);
	document.form1.elements[focus].focus();
}

function dclick(str) {
   var ed = document.getElementById("d"+str);
   if (day) {
   	  var led = document.getElementById("d"+day);
	  led.style.backgroundColor = "white";
	  }
   day = str;
   ed.style.backgroundColor = "yellow";
   doChk();
   }

function mclick(str,m) {
   monthnum = m;
   var ed = document.getElementById(str);
   if (month) {
   	  var led = document.getElementById(month);
	  led.style.backgroundColor = "white";
	  }
   month = str;
   ed.style.backgroundColor = "yellow";
   doChk();
   }
   
function yclick(str) {
   var ed = document.getElementById(str);
   if (year) {
   	  var led = document.getElementById(year);
	  led.style.backgroundColor = "white";
	  }
   year = str;
   ed.style.backgroundColor = "yellow";
   doChk();
   }

   
function buildCal(){

  // the datepicker table will be drawn inside of a <div> with an ID defined by the
  // global datePickerDivID variable. If such a div doesn't yet exist on the HTML
  // document we're working with, add one.
  if (!document.getElementById("popcal"+opt)) {
    var nothere=1;
    // don't use innerHTML to update the body, because it can cause global variables
    // that are currently pointing to objects on the page to have bad references
    //document.body.innerHTML += "<div id='popcal" + opt "' class='dpDiv'></div>";

    var newNode = document.createElement("div");
    newNode.setAttribute("id", "popcal"+opt);
//    newNode.setAttribute("class", "links");
    newNode.setAttribute("style", "visibility: hidden;background-color: #ffffff; opacity:.9; font-size:8pt;");
    document.body.appendChild(newNode);
  }
 
  // move the datepicker div to the proper x,y coordinate and toggle the visiblity
  pickerDiv = document.getElementById("popcal"+opt);
  pickerDiv.style.position = "absolute";
  pickerDiv.style.left = x;
  pickerDiv.style.top = y; 
  
//  pickerDiv.style.visibility = "visible";
//  pickerDiv.style.display = "block";
  pickerDiv.style.visibility = (pickerDiv.style.visibility == "visible" ? "hidden" : "visible");
  pickerDiv.style.display = (pickerDiv.style.display == "block" ? "none" : "block");
  pickerDiv.style.zIndex = 10000;
  
  // draw the datepicker table

w = "<table width="+wid+" cellpadding=4 align=top style='border: 1px solid red; background-color: white; font-size:8pt;'>"
w +="<tr style='font-size:8pt;'>"
if (opt < 3){w+="<td width=20 depreciated><b>Month</b></td>"}
if (opt == 1){ w+="<td align=center colspan=4><b>Day</b></td><td width=2></td>"}
//w+= "<td colspan=2>19xx</td><td colspan=2></td><td colspan=2><b>Year</b></td>"
w+= "<td colspan=2></td><td colspan=2></td><td colspan=2><b>Year</b></td>"
w+="<td colspan=2></td><td colspan=2 align=right><input type=button style=\"size: 18;\" value=\"X\" onclick=\"closeIt();\"></td>"
//t = start/10
//if ((t<4)&&(start!=0)) {
//   t = 4-t
//   w+="<td colspan=" + t + "></td><td colspan=2>20xx</td>"
//   }
w+="</tr><tr>"
if (opt<3){
w +="<td rowspan=2 style='border-right:solid 1px;'>"
w +="<a href='#' class='popCal' id=\"Jan\" onclick=\"mclick('Jan',0);\">Jan</a><br>"
w +="<a href='#' class='popCal' id=\"Feb\" onclick=\"mclick('Feb',1);\">Feb</a><br>"
w +="<a href='#' class='popCal' id=\"Mar\" onclick=\"mclick('Mar',2);\">Mar</a><br>"
w +="<a href='#' class='popCal' id=\"Apr\" onclick=\"mclick('Apr',3);\">Apr</a><br>"
w +="<a href='#' class='popCal' id=\"May\" onclick=\"mclick('May',4);\">May</a><br>"
w +="<a href='#' class='popCal' id=\"Jun\" onclick=\"mclick('Jun',5);\">Jun</a><br>"
w +="<a href='#' class='popCal' id=\"Jul\" onclick=\"mclick('Jul',6);\">Jul</a><br>"
w +="<a href='#' class='popCal' id=\"Aug\" onclick=\"mclick('Aug',7);\">Aug</a><br>"
w +="<a href='#' class='popCal' id=\"Sep\" onclick=\"mclick('Sep',8);\">Sep</a><br>"
w +="<a href='#' class='popCal' id=\"Oct\" onclick=\"mclick('Oct',9);\">Oct</a><br>"
w +="<a href='#' class='popCal' id=\"Nov\" onclick=\"mclick('Nov',10);\">Nov</a><br>"
w +="<a href='#' class='popCal' id=\"Dec\" onclick=\"mclick('Dec',11);\">Dec</a><br>"
}

if (opt<2){
var t
for (x=0;x<4;x++){
	w+="<td style='width: 9' valign=top>"
	for (y=0;y<10;y++){
	   	t = x*10 + y
		if (t==0) {
		   w+="<br>"
		}else{
		   w+="<a href='#' class='popCal' id=\"d" + t + "\" onclick=\"dclick(" + t +");\">" + t + "</a><br>"
		}
		if (t == 31) {y=9}
	}
	w +="</td>"
}

w +="<td rowspan=1 width=2 style=\"border-right:solid 1px;\">&nbsp;</td>"
}

for (y=0;y<10;y++) {
	w += "<td rowspan=1 valign=top style='width: 9' >"
	for (x=0;x<10;x++) {
//	    tt = y*10 + x + start
		tt = x*10 + y + start
		dt = tt + 1900
		t = tt % 100
		if (t<10){t = "0"+t}
		if (y!=0) {dt=t}
		
		w+="<a href='#' class='popCal' id=\"" + t + "\" title=\""+(1900+tt)+"\" onclick=\"yclick('" + t + "');\">" + dt + "</a><br>"
	}
	w+="</td>"
}	
w+="</tr><tr><td colspan="+(totcols-1)+" align=center>&nbsp;<span id=\"msg\"></span></td>"
w+="</tr></tr></table>"
pickerDiv.innerHTML = w;
adjustiFrame();
}

function adjustiFrame(pickerDiv, iFrameDiv)
{
  // we know that Opera doesn't like something about this, so if we
  // think we're using Opera, don't even try
   var t =navigator.userAgent.toLowerCase();
  if ((t.indexOf("opera") != -1) || (t.indexOf("firefox") != -1) ){return}

  // put a try/catch block around the whole thing, just in case
  try {
    if (!document.getElementById(iFrameDivID)) {
      // don't use innerHTML to update the body, because it can cause global variables
      // that are currently pointing to objects on the page to have bad references
      //document.body.innerHTML += "<iframe id='" + iFrameDivID + "' src='javascript:false;' scrolling='no' frameborder='0'>";
      var newNode = document.createElement("iFrame");
      newNode.setAttribute("id", iFrameDivID);
      newNode.setAttribute("src", "javascript:false;");
      newNode.setAttribute("scrolling", "no");
      newNode.setAttribute ("frameborder", "0");
      document.body.appendChild(newNode);
    }
    if (!pickerDiv)
      pickerDiv = document.getElementById("popcal"+opt);
    if (!iFrameDiv)
      iFrameDiv = document.getElementById(iFrameDivID);
    
    try {
      iFrameDiv.style.position = "absolute";
      iFrameDiv.style.width = pickerDiv.offsetWidth;
      iFrameDiv.style.height = pickerDiv.offsetHeight ;
      iFrameDiv.style.top = pickerDiv.style.top;
      iFrameDiv.style.left = pickerDiv.style.left;
      iFrameDiv.style.zIndex = pickerDiv.style.zIndex - 1;
      iFrameDiv.style.visibility = pickerDiv.style.visibility ;
      iFrameDiv.style.display = pickerDiv.style.display;
    } catch(e) {
    }
 
  } catch (ee) {
  }
 
}

function getCenter() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape 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 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }

if (self.innerWidth)
{
	frameWidth = self.innerWidth;
	frameHeight = self.innerHeight;
}
else if (document.documentElement && document.documentElement.clientWidth)
{
	frameWidth = document.documentElement.clientWidth;
	frameHeight = document.documentElement.clientHeight;
}
else if (document.body)
{
	frameWidth = document.body.clientWidth;
	frameHeight = document.body.clientHeight;
}
//alert(scrOfX+' '+scrOfY)
sx=frameWidth/2+scrOfX
sy=frameHeight/2+scrOfY

}
