// All routines Copyright © Coraider Services 1997
function MonthToInt(mon)
// Routine Copyright © Coraider Services 1997
// MonthToInt V1.0-00 (DateSub.js)
{
  var mm, months;

  months="|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|";
  mm=months.indexOf("|"+mon.toLowerCase()+"|");
  with (Math) if (mm>0) mm=floor((mm/4));
  return mm;
}

function IntToMonth(mm)
// Routine Copyright © Coraider Services 1997
// IntToMonth V1.0-00 (DateSub.js)
{
  var mon, months;

  months="JanFebMarAprMayJunJulAugSepOctNovDec";
  return months.substring(mm*3,mm*3+3);
}

function DaysInMonth(mm, yy)
// Routine Copyright © Coraider Services 1997
// DaysInMonth V1.0-00 (DateSub.js)
{
  var days, dd;

  days="31,28,31,30,31,30,31,31,30,31,30,31"
  dd=days.substring(mm*3,mm*3+2);
  if (mm==1) with (Math) if (floor(yy/4)*4==yy) dd++;
  return dd;
}

function GetElement(str, seplst, no)
// Routine Copyright © Coraider Services 1997
// GetElement V1.0-00 (DateSub.js)
{
  var pos, i, tmp, st, elm;
  var buf;

  elm=0;
  st=0;
  pos=0;
  while (true) {
    pos=str.length+1;
    for(i=0;i<seplst.length;i++) {
	  buf=seplst.substring(i,i+1);
  	  tmp=str.indexOf(buf, st);
	  if (tmp!=-1 && tmp<pos) pos=tmp;
    }
    if (pos>str.length || elm==no) break;
    st=pos+1;
    elm++;
  }
  if (pos>str.length && elm!=no) 
    return "";
  else
    return str.substring(st, pos);
}

function ParseDateTime(txt, fmt)
{
// Routine Copyright © Coraider Services 1999
// ParseDateTime V1.0-000 (DateSub.js)
  var dt, tm, i, st;
  
  st=txt.indexOf(":");
  dt="";
  tm="";
  if (st!=-1) {
    for(i=st;i>=0;i--) {
      if (txt.charAt(i)==" ") {
        dt=txt.substring(0,i);
        tm=txt.substring(i+1,txt.length);
        i=-2;
      }
    }
    if (i==-1) tm=txt;
  } else {
    dt=txt;
  }
  if (dt!="") dt=ParseDate(dt, fmt);
  if (tm!="" && !CheckTime(tm))   return "";
  if (tm=="") {
    if (fmt.toLowerCase()=="early") tm="00:00";
    if (fmt.toLowerCase()=="late") tm="23:59";
  }
  return dt + " " + tm;
}

function ParseDate(txt, fmt1)
{
// Routine Copyright © Coraider Services 1997
// ParseDate V1.1-00 (DateSub.js)
  var dd, mm, yy, fmt, dt, days, lstdd, seps;

  seps=" /-."
  dt=new Date;
 
  fmt=fmt1.toLowerCase();
  yy=GetElement(txt,seps,2);
  if (yy=="") {
    dd="";
    yy=GetElement(txt,seps,1);
    if (yy=="") {
	    mm="";
	    yy=GetElement(txt,seps,0);
	  } else {
	    mm=GetElement(txt,seps,0);
	  }
  } else {
	  dd=GetElement(txt,seps,0);
	  mm=GetElement(txt,seps,1);
  }
  if (yy=="") {
  	if (fmt="early") yy=1900;
	  else if (fmt="late") yy="XXX";
	  else yy=dt.getYear();
  }
  if (isNaN(parseInt(yy,10))) {
	  alert("Illegal date "+txt+", bad year");
	  return "";
  }
  yy=parseInt(yy,10)
  if (yy<100) {
    if (yy<26)
      yy+=2000;
    else
      yy+=1900;
  }
  if (mm=="") {
	if (fmt=="late") mm=11;
	else if (fmt=="early") mm=0;
	else mm=dt.getMonth();
  } else {
     if (isNaN(parseInt(mm,10)))
       mm=MonthToInt(mm);
	   else mm--;
  }
  if (mm<0 || mm>11) {
	  alert("Illegal date "+txt+", bad month"+mm);
	  return "";
  }
  lstdd=DaysInMonth(mm, yy);
  if (dd=="") {
	  if (fmt=="late") dd=lstdd;
	  else if (fmt=="early") dd=1
	  else dd=dt.getDate();
  } else {
	  if (isNaN(parseInt(dd,10))) {
	    alert("Illegal date "+txt+", bad day");
	    return "";
	  }
    dd=parseInt(dd,10);
	  if ((dd<1) || (dd >lstdd)) {
 	    alert("Illegal date "+txt);
	    return "";
	  }
  }
  return dd+"-"+IntToMonth(mm)+"-"+yy;
}

function EarlyDate(txt)
// Routine Copyright © Coraider Services 1997
// EarlyDate V1.0-00 (DateSub.js)
{
  return ParseDate(txt ,"early");
}

function LateDate(txt)
// Routine Copyright © Coraider Services 1997
// LateDate V1.0-00 (DateSub.js)
{
  return ParseDate(txt, "late");
}

function CheckTime(txt)
// Routine Copyright © Coraider Services 1997
// CheckTime V1.0-00 (DateSub.js)
{
  var no;

  no=GetElement(txt, ":", 0);
  no=parseInt(no, 10);
  if (isNaN(no)) return false;
  if (no<0 || no>23) return false;
  no=GetElement(txt, ":", 1);
  no=parseInt(no, 10);
  if (isNaN(no)) return false;
  if (no<0 || no>59) return false;
  return true;
}
function ConvertHours(txt) 
{
// Routine Copyright © Coraider Services 1999
// ConvertHours V1.0-000 (DateSub.js)
  var hr, mn;
  
  hr=GetElement(txt,":.", 0);
  hr=parseInt(hr, 10);
  if (isNaN(hr)) return "";
  mn=GetElement(txt,":.", 1);
  if (mn=="") return hr + ":00";
  mn=parseInt(mn,10);
  if (isNaN(mn)) return "";
  if (txt.indexOf(":")!=-1) {
    if (mn<0 || mn>59) return "";
  } else {
    if (mn<0) return "";
    mn=parseFloat("." + mn)
    mn=Math.round(60*mn);
  }
  if (mn<10) return hr + ":0" + mn;
  return hr + ":" + mn; 
}

function ValidTime(fld)
// Routine Copyright © Coraider Services 1997
// ValidTime V1.0-00 (DateSub.js)
{
  if (!PatternCheck(fld.value ,"99:99") ) {
    alert(ConvertName(fld.name)+" not of form '99:99'");
    return false;
  }
  if (!CheckTime(fld.value)) {
    alert(ConvertName(fld.name)+" invalid");
    return false;
  }
  return true;
}

function DateField(fld)
// Routine Copyright © Coraider Services 1997
// DateField V1.0-00 (DateSub.js)
{
  var buf;
  
  if (fld.value=="") return;
  buf=ParseDate(fld.value,"Early");
  if (buf!="") fld.value=buf;
}