/*
 *
 * (C) Copyright Neat Group, Inc. 1999-2005 - All Rights Reserved
 *
 * The original version of this source code and documentation
 * is copyrighted and owned by Neat Group, Inc.
 *
 * Using, copying, modifying and distributing this software
 * is strictly prohibited without a prior written agreement
 * of Neat Group, Inc.
 *
 */
var sdt_fld_y = null
var sdt_fld_m = null
var sdt_fld_d = null
var sdt_onchange = null
var sdt_win = null
var apos = "'"

function gen_month_tb(imgdir,y,m,fy,fm,fromday,ty,tm,today)
{
 prevm = m-1
 prevy = y
 nextm = m+1
 nexty = y
 if (prevm < 1)
 {
  prevm = 12
  prevy--
 }
 if (nextm > 12)
 {
  nextm = 1
  nexty++
 }
 var doc = sdt_win.document;
 doc.open()
 doc.writeln('<!doctype html public "-//w3c//dtd html 4.0 transitional//en">')
 doc.writeln('<head><title>Choose Date</title>')
 doc.writeln('<style type="text/css">')
 doc.writeln('<!--')
 doc.writeln('.db A:link {color:blue; text-decoration:none; }')
 doc.writeln('.db A:visited {color:blue; text-decoration:none; }')
 doc.writeln('.db A:active {color:blue; text-decoration:none; }')
 doc.writeln('.db A:hover {color:red; text-decoration:underline; }')
 doc.writeln('.db {font-size: 9pt; font-family:sans-serif,helvetica; }')
 doc.writeln('.cl A:visited {color:blue; text-decoration:underline; }')
 doc.writeln('.cl A:active {color:blue; text-decoration:underline; }')
 doc.writeln('.cl A:hover {color:red; text-decoration:underline; }')
 doc.writeln('.cl {font-size: 8pt; font-family:sans-serif,helvetica; }')
 doc.writeln('-->')
 doc.writeln('</style>')
 doc.writeln('</head>')
 doc.write('<body bgcolor="#ffffff"><center>')
 if (y>fy || (y==fy && m>fm))
 {
  doc.write('<a href="#" onclick="return opener.sdt_sw(\''+imgdir+'\','+prevy+','+prevm+','+fy+','+fm+','+fromday+','+ty+','+tm+','+today+');">')
  doc.write('<img src="'+imgdir+'dt_up.gif" border=0 width=50 height=18 alt="'+sdt_tip_prev+'" title="'+sdt_tip_prev+'" />')
  doc.write('</a>');
 }
 else
  doc.write('<img src="'+imgdir+'dt_vinact.gif" border=0 width=50 height=18>')
 gen_month(doc,imgdir,y,m,fy,fm,fromday,ty,tm,today)
 if (y<ty || (y==ty && m<tm))
 {
  doc.write("<br>");
  gen_month(doc,imgdir,nexty,nextm,fy,fm,fromday,ty,tm,today)
  if (nexty<ty || (nexty==ty && nextm<tm))
  {
   nnextm=nextm+1
   nnexty=nexty
   if (nnextm > 12)
   {
    nnextm = 1
    nnexty++
   }
   doc.write('<a href="#" OnClick="return opener.sdt_sw(\''+imgdir+'\','+nexty+','+nextm+','+fy+','+fm+','+fromday+','+ty+','+tm+','+today+');">')
   doc.write('<img src="'+imgdir+'dt_down.gif" border=0 width=50 height=18 alt="'+sdt_tip_next+'" title="'+sdt_tip_next+'" />')
   doc.write('</a>')
  }
  else
   doc.write('<img src="'+imgdir+'dt_vinact.gif" border=0 width=50 height=18></td>')
 }
 doc.write('<br />&nbsp;<br /><form><select name="selmon" class="cl"')
 doc.write(' onchange="opener.sdt_sw(\''+imgdir+'\',selmon[selmon.selectedIndex].value.substring(0,4)*1,selmon[selmon.selectedIndex].value.substring(4)*1,'+fy+','+fm+','+fromday+','+ty+','+tm+','+today+')"')
 doc.write('>')
 cy=fy
 cm=fm
 while (cy<ty || (cy==ty && cm<=tm))
 {
  sel = (cy==y && cm==m) ? " selected " : ""
  doc.write('<option'+sel+' value="'+cy+cm+'">' + sdt_month_name[cm-1] + ' ' + cy)
  if (++cm > 12)
  {
   cm = 1
   cy++
  }
 }
 doc.write('</select></form>')

 doc.write('<table width="100%"><tr bgcolor="#ffffff"><td align="center" class="cl"><a href="#" onclick="opener.sdt_clswin(); return false;">'+sdt_close+'</a></td></tr></table>')
 doc.write('</center></body>')
 doc.close()
}
function gen_month(doc,imgdir,y,m,fy,fm,fromday,ty,tm,today)
{
 currDate=new Date()
 curry=currDate.getFullYear()
 currm=currDate.getMonth()+1
 currd=currDate.getDate()
 doc.write('<table width="140" bgcolor="#ffffff" cellpadding=1 cellspacing=1>')
 doc.write('<tr bgcolor="#a4a4a4"><td align=center colspan=7 class=db nowrap><font color="#ffffff"><b>' + sdt_month_name[m-1] + ' ' + y + '</b></font></td></tr>')
 // Week Days title
 doc.writeln('<tr>')
 for (i=0; i<7; i++)
  doc.write('<td align=right class=db><b>'+sdt_day_name[i]+'</b></td>')
 doc.writeln('</tr>')
 // Fill month
 dt1 = new Date(y,m-1,1)
 fd = dt1.getDay()
 ny=y
 nm = m+1
 if (nm > 12)
 {
   nm = 1
   ny++
 }
 dte = new Date(ny, nm-1, 1)
 dye = dte.getDay()
 doc.write('<tr align="right">')
 for (c=0; c<fd; c++)
  doc.write('<td width="20" class=db>&nbsp;</td>')
 var rows = 0
 for (i=1; i<29 || c!=dye; i++)
 {
  if (i==currd && m==currm && y==curry)
  {
   tmks='<td><table border=1 bordercolor="red" cellpadding=0 cellspacing=0><tr align="right">'
   tmke='</td></tr></table>'
  }
  else tmks=tmke="";
  if ((m==fm&&y==fy&& i<fromday)||
   (m==tm&&y==ty&&i>today))
   doc.write(tmks+'<td class=db><font color="#B6C4CF">'+i+'</a></font></td>'+tmke)
  else
   doc.write(tmks+'<td class=db><a href="#" OnClick="return opener.sdt_set('+y+','+m+','+i+')">'+i+'</a></td>'+tmke)

  c=(++c)%7
  if (c==0)
  {
   doc.write('</tr><tr align="right">')
   rows++
  }
 }

 while (rows < 6) // Fill last row(s)
 {
  for (; c<7; c++)
   sdt_win.document.write('<TD class=db>&nbsp;</TD>')
  c=0
  rows++
  if (rows<6)
   sdt_win.document.write('</tr><tr align="right">')
 }
 doc.writeln('</tr>')
 doc.writeln('</table>')
}
function sdt(event,imgdir,dbox,mbox,ybox,fy,fm,fd,ty,tm,td,onchange)
{
 if (dbox == sdt_fld_d && sdt_win != null && !sdt_win.closed)
 {
  sdt_clswin() // second click = close
  return
 }
 if (event != null && sdt_win != null && !sdt_win.closed)
  sdt_clswin()
 if (dbox != null)
 {
  sdt_fld_d = dbox
  sdt_fld_m = mbox
  sdt_fld_y = ybox
  sdt_onchange = onchange
 }
 if (sdt_win == null || sdt_win.closed)
 {
  var loc = ''
  if (event.clientX)
   loc = ',top='+event.clientY+',left='+event.clientX
  else
   if (event.pageX)
    loc = ',top='+event.pageY+',left='+event.pageX
  sdt_win=window.open('','sdt','toolbar=no,scrollbars=no,width=160,height=460,resizable=yes,status=no,menubar=no'+loc,true);
 }
 if (ybox.options)
  year = ybox.options[ybox.selectedIndex].value * 1
 else
  year = ybox.value
 month = mbox.options[mbox.selectedIndex].value * 1
 if (year == 0 || month == 0)
 {
  var cd = new Date()
  if (cd.getFullYear() >= fy && cd.getFullYear() <= ty && cd.getMonth() >= fm-1 && cd.getMonth() <= tm-1)
  {
   year = cd.getFullYear()
   month = cd.getMonth() + 1
  }
  else
  {
   year = fy
   month = fm
  }
 }
 gen_month_tb(imgdir,year,month,fy,fm,fd,ty,tm,td)
 sdt_win.focus()
}

function sdt_clswin()
{
	sdt_win.close()
	sdt_win = null
	sdt_fld_y = null
	sdt_fld_m = null
	sdt_fld_d = null
}

function sdt_set(y,m,d)
{
	if (sdt_fld_y.options)
		sdt_set_opt(sdt_fld_y, y)
	else
		sdt_fld_y.value=y
	sdt_set_opt(sdt_fld_m, m)
	sdt_set_opt(sdt_fld_d, d)
	if (sdt_onchange != null)
		eval(sdt_onchange)
	sdt_win.close()
	return false
}
function sdt_set_opt(fld, val)
{
	for (i=0; i<fld.options.length; i++)
	{
		if (fld.options[i].value * 1 == val)
		{
			fld.selectedIndex = i
			return
		}
	}
}

function sdt_sw(imgdir,year,month,fy,fm,fd,ty,tm,td)
{
	gen_month_tb(imgdir,year,month,fy,fm,fd,ty,tm,td)
	sdt_win.focus()
	return false
}

function sdt_validate(dbox,mbox,ybox,fd,fm,fy,td,tm,ty,fldtype,onchange)
{
	load_y_m_d(dbox,mbox,ybox)
 	// Field set to null
 	if ( (fldtype=='D' && d == 0) || (fldtype=='M' && m == 0) || (fldtype=='Y' && y == 0) )
 	{
		if (ybox.options)
			ybox.selectedIndex = 0
		else
			ybox.value='0'
		mbox.selectedIndex = 0
		if (dbox != null)
			dbox.selectedIndex = 0
	}
	else
	{
		cd = new Date()
		if (d == 0) d = cd.getDate()
		if (m == 0) m = cd.getMonth()+1
		if (y == 0) y = cd.getFullYear()
		if (y < fy) y = fy
		if (y > ty) y = ty
		dfd = new Date(fy, fm-1, fd)
		dtd = new Date(ty, tm-1, td)
		dsd = new Date(y, m-1, d)

		while (d > 27 && dsd.getMonth()!=m-1)
			dsd = new Date(y, m-1, --d) // Month has less days
		if (dsd < dfd || dsd > dtd)
		{
			var altdates = get_alternatives(dsd,dfd,dtd,fldtype,dbox!=null)
			if (altdates.length > 0)
				dsd=altdates[0]
		}

		if (dsd < dfd)
			dsd = dfd
		if (dsd > dtd)
			dsd = dtd
		if (ybox.options)
			sdt_set_opt(ybox, dsd.getFullYear())
		else
			ybox.value = dsd.getFullYear()
		sdt_set_opt(mbox, dsd.getMonth()+1)
		if (dbox != null)
			sdt_set_opt(dbox, dsd.getDate())
		if (onchange != null)
		{
			eval(onchange)
			// were values modifed (blocked) ?
			load_y_m_d(dbox,mbox,ybox)
			if (y != dsd.getFullYear() || m != (dsd.getMonth()+1) || d != dsd.getDate())
			{
				var altdates = get_alternatives(dsd,dfd,dtd,fldtype,dbox!=null)
				var i
				for (i=0; i<altdates.length; i++)
				{
					sd=altdates[i]
					if (ybox.options)
						sdt_set_opt(ybox, dsd.getFullYear())
					else
						ybox.value = dsd.getFullYear()
					sdt_set_opt(mbox, dsd.getMonth()+1)
					if (dbox != null)
						sdt_set_opt(dbox, dsd.getDate())
					eval(onchange)
					load_y_m_d(dbox,mbox,ybox)
					if (y == dsd.getFullYear() && m == (sd.getMonth()+1) && d == dsd.getDate())
						break;
				}
			}
		}
	}
}

function load_y_m_d(dbox,mbox,ybox)
{
	if (ybox.options)
		y = ybox.options[ybox.selectedIndex].value * 1
	else // Invisible year
		y = ybox.value*1
	m = mbox.options[mbox.selectedIndex].value * 1
	if (dbox != null)
	d = dbox.options[dbox.selectedIndex].value * 1
		else
	d = 1
}

// Get alternativs when field was modified
function get_alternatives(dt,fd,td,fld,hasday)
{
	var res = new Array()
	var d = dt.getDate()
	var m = dt.getMonth()
	var y = dt.getFullYear()
	if (fld == 'M') // month changed
	{
		var mxd = hasday ? 31 : 1;
		for (nd=1; nd<=mxd; nd++)
		{
			for (ny=fd.getFullYear(); ny<=td.getFullYear(); ny++)
			{
				var ndt = new Date(ny,m,nd)
				if (ndt.getMonth() == m && ndt >= fd && ndt <= td)
					res[res.length] = ndt
			}
		}
	}
	else
	if (fld == 'D') // day changed
	{
		for (nm=0; nm<12; nm++)
		{
			for (ny=fd.getFullYear(); ny<=td.getFullYear(); ny++)
			{
				var ndt = new Date(ny,nm,d)
				if (ndt.getDate() == d && ndt >= fd && ndt <= td)
					res[res.length] = ndt
			}
		}
	}

	// sort by proximity
	var i,j;
	for (i=0; i<res.length; i++)
	{
		var di = Math.abs(res[i].getDate()-d)+Math.abs(res[i].getMonth()-m)+Math.abs(res[i].getFullYear()-y)
		for (j=i+1; j<res.length; j++)
		{
			var dj = Math.abs(res[j].getDate()-d)+Math.abs(res[j].getMonth()-m)+Math.abs(res[j].getFullYear()-y)
			if (di > dj)
			{
				var t = res[i]
				res[i] = res[j]
				res[j] = t
				di = dj
			}
		}
	}
	return res
}

// Adjust a peer related date field (if necessary)
function sdt_adjust_related(dbox,mbox,ybox, rdbox, rmbox, rybox, rfd,rfm,rfy,rtd,rtm,rty, mindiffd, maxdiffd)
{
 var form = getForm(dbox)
 var dbox = form[dbox]
 var mbox = form[mbox]
 var ybox = form[ybox]
 var rdbox = form[rdbox]
 var rmbox = form[rmbox]
 var rybox = form[rybox]

 if (ybox.options)
  y = ybox.options[ybox.selectedIndex].value * 1
 else // Invisible year
  y = ybox.value*1
 m = mbox.options[mbox.selectedIndex].value * 1
 if (dbox != null)
  d = dbox.options[dbox.selectedIndex].value * 1
 else
  d = 1
 if (rybox.options)
  ry = rybox.options[rybox.selectedIndex].value * 1
 else
  ry = rybox.value*1
 rm = rmbox.options[rmbox.selectedIndex].value * 1
 if (rdbox != null)
  rd = rdbox.options[rdbox.selectedIndex].value * 1
 else
  rd = 1
 if (y==0 || m==0 || d==0 || ry==0 || rm==0 || rd==0)
  return; // null is not adjusted/adjusting
 rfdt = new Date(rfy, rfm-1, rfd)
 rfd_rel  = new Date(y, m-1, d+mindiffd)
 if (rfd_rel > rfdt)
  rfdt = rfd_rel  // higher start from base and relation
 rtdt = new Date(rty, rtm-1, rtd)
 rtd_rel  = new Date(y, m-1, d+maxdiffd)
 if (rtd_rel < rtdt)
  rtdt = rtd_rel  // higher start from base and relation
 if (maxdiffd < 0) // negative (last to first)
 {
  t = rfdt
  rfdt = rtdt
  rtdt = t
 }
 var sd = new Date(ry, rm-1, rd) // current related selection
 changed = false
 if (sd < rfdt)
 {
  changed = true
  sd = rfdt
 }
 else
 if (sd > rtdt)
 {
  changed = true
  sd = rtdt
 }
 if (changed)
 {
  if (rybox.options)
   sdt_set_opt(rybox, sd.getFullYear())
  else
   rybox.value = sd.getFullYear()
  sdt_set_opt(rmbox, sd.getMonth()+1)
  if (rdbox != null)
   sdt_set_opt(rdbox, sd.getDate())
 }
}

function sdt_getdate(dbox,mbox,ybox)
{
	if (ybox.options)
		y = ybox.options[ybox.selectedIndex].value * 1
	else // Invisible year
		y = ybox.value*1
	m = mbox.options[mbox.selectedIndex].value * 1
	if (dbox != null)
		d = dbox.options[dbox.selectedIndex].value * 1
	else
		d = 1
	if (y == 0)
		return null
	return new Date(y, m-1, d)
}

function sdt_copy(fromctl, toctl)
{
	var fyc = document.getElementById("year"+fromctl)
	var fmc = document.getElementById("month"+fromctl)
	var fdc = document.getElementById("day"+fromctl)
	var tyc = document.getElementById("year"+toctl)
	var tmc = document.getElementById("month"+toctl)
	var tdc = document.getElementById("day"+toctl)

	var fd = document.getElementById("date"+fromctl)
	var td = document.getElementById("date"+toctl)

	if (fdc)
		sdt_set_opt(tdc, fdc.options[fdc.selectedIndex].value * 1)
	if (fmc)
		sdt_set_opt(tmc, fmc.options[fmc.selectedIndex].value * 1)
	if (fyc)
	{
		if (fyc.options)
			sdt_set_opt(tyc, fyc.options[fyc.selectedIndex].value * 1)
		else
		{
			tyc.value = fyc.value
		}
	}
	if (fd)
		td.value = fd.value
}

/////////////////////////////////////////////////////////////////////////////////////////
// Auto popup date functions
/////////////////////////////////////////////////////////////////////////////////////////

var cal_focusField = null
var cal_closeField = null
var cal_mayCloseWindow = true

function cal_open(fld,y,m,fy,fm,fd,ty,tm,td,imgdir,related,onchange)
{
	var cal = document.getElementById("cal_win")
	if (cal.style.display == "block" && cal_focusField == fld)
	{
	}
	else
	{
		var relatedValues = new Array()
		for (var i=0; i<related.length; i++)
			relatedValues[i] = related[i].value
		cal_focusField = fld
		sdt_onchange = onchange
		window.frames["cal_win"].cal_gen_month(y,m,fy,fm,fd,ty,tm,td,fld.value,imgdir,true,relatedValues)
		var pos = cal_getPosition(fld)
		cal.style.left = pos.left+"px"
		cal.style.top  = pos.top+fld.offsetHeight+"px"
		cal.style.display = "block"

		var maxY
		if (document.all)
		{
			maxY = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)
				 + (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight)
		}
		else
		{
			maxY = window.scrollY + window.innerHeight
		}
		if (pos.top+fld.offsetHeight+cal.clientHeight > maxY)
		{
			var y = pos.top-cal.clientHeight
			if (y < 0)
				y = 0
			cal.style.top  = y+"px"
		}
		pos.top  = y
		cal.style.zIndex = 100;
	}
	cal_mayCloseWindow = true
	if (cal_closeField == fld)
		cal_closeField = null
}

function cal_close(fld)
{
	cal_closeField = fld
	setTimeout('cal_close_impl()', 300)
}

function cal_close_impl()
{
	if (cal_mayCloseWindow && cal_focusField == cal_closeField)
	{
		cal_focusField = null
		var calwin = document.getElementById("cal_win")
		calwin.style.display = "none"
	}
	else
	{
		if (cal_focusField)
			cal_focusField.focus()
	}

	cal_mayCloseWindow = true
}


function cal_getPosition(fld)
{
	var pos = new Object()

	pos.left = 0
	pos.top  = 0
	var item = fld
	while (item)
	{
		if (item.style.position == 'absolute')
			break

		pos.left += item.offsetLeft
		pos.top  += item.offsetTop
		item= item.offsetParent
	}

	var x   = pos.left
	var y   = pos.top+fld.offsetHeight

	return pos
}

function cal_set(d,m,y)
{
	if (cal_focusField)
	{
		cal_setf(cal_focusField,d,m,y)
		if (sdt_onchange != null)
			eval(sdt_onchange)
	}
}

function cal_setf(fld,d,m,y)
{
	var val = ''
	var sy = '/'
	if (y<2010)
		sy = sy + '0'
	sy = sy + (y-2000)
	if (sdt_format == 'M/d/yy')
		val = m+'/'+d+sy
	else
	if (sdt_format == 'd/M/yy')
		val = d+'/'+m+sy
	if (val != '')
		fld.value = val
}

function cal_getFocusField()
{
	return cal_focusField
}

function cal_setMayClose(mayClose)
{
	cal_mayCloseWindow = mayClose
}

function cal_parse(val)
{
	var d = -1
	var m = -1
	var y = -1
	var sep = '/'
	if (val.indexOf(sep) >= 0)
	{
		var c1 = val.substring(0, val.indexOf(sep))
		var c2c3 = val.substring(val.indexOf(sep)+1)
		if (c2c3.indexOf(sep) >= 0)
		{
			var c2 = c2c3.substring(0, c2c3.indexOf(sep))
			var c3 = c2c3.substring(c2c3.indexOf(sep)+1)
			if (sdt_format == 'M/d/yy')
			{
				d = parseInt(c2)
				m = parseInt(c1)
				y = parseInt(c3,10)
			}
			else
			if (sdt_format == 'd/M/yy')
			{
				d = parseInt(c1)
				m = parseInt(c2)
				y = parseInt(c3,10)
			}
			if (y >=0 && y < 100)
				y += 2000
		}
	}
	return new Array(y, m, d)
}


function cal_adjust_related(fld, rfld, rfd, rfm, rfy, rtd, rtm, rty, mindiffd, maxdiffd)
{
	var form = getForm(fld)

	myvals = cal_parse(form[fld].value)
	rfvals = cal_parse(form[rfld].value)
	y = myvals[0]
	m = myvals[1]
	d = myvals[2]
	ry = rfvals[0]
	rm = rfvals[1]
	rd = rfvals[2]

	if (y==-1 || m==-1 || d==-1 || ry==-1 || rm==-1 || rd==-1)
		  return; // invalid value, ignore (do not change)

	rfdt = new Date(rfy, rfm-1, rfd)
	rfd_rel  = new Date(y, m-1, d+mindiffd)
	if (rfd_rel > rfdt)
		rfdt = rfd_rel  // higher start from base and relation
	rtdt = new Date(rty, rtm-1, rtd)
	rtd_rel  = new Date(y, m-1, d+maxdiffd)
	if (rtd_rel < rtdt)
		rtdt = rtd_rel  // higher start from base and relation
	if (maxdiffd < 0) // negative (last to first)
	{
		t = rfdt
		rfdt = rtdt
		rtdt = t
	}
	var sd = new Date(ry, rm-1, rd) // current related selection
	changed = false
	if (sd < rfdt)
	{
		changed = true
		sd = rfdt
	}
	else
	if (sd > rtdt)
	{
		changed = true
		sd = rtdt
	}
	if (changed)
	{
		cal_setf(form[rfld],sd.getDate(),sd.getMonth()+1,sd.getFullYear())
	}
}

