//----------------------------------------------------------------------
function checkcondate(dateobject) {
  ok = checkdate(dateobject)

  if (ok) {
    condate=dateobject.value;
    if ((dayofweek(condate)==0) || (dayofweek(condate)==6)) {
      alert('The connection date must be a week day\nPlease enter a valid date.');
      dateobject.value='';
      dateobject.focus();
      return false;
    }

    firstdate=adddaystodate(1,'now');
    if (dayofweek(firstdate)==0) {
      firstdate=adddaystodate(1,firstdate); //Sunday
    }
    if (dayofweek(firstdate)==6) {
      firstdate=adddaystodate(2,firstdate); //Saturday
    }
    if (comparedate(condate,firstdate)<0) {
      alert('The connection date needs to be on or after '+firstdate+'\nPlease enter a valid date.');
      dateobject.value='';
      dateobject.focus();
      return false;
    }
    else {
      return true;
    }  
  }
}

//Original:  Mike Welagen (welagenm@hotmail.com)
//This script and many more are available free online at 
//The JavaScript Source!! http://javascript.internet.com 

function checkdate(objName) {
  var datefield = objName;
  if (chkdate(objName) == false) {
    datefield.select();
    alert("Please enter a valid date.");
    datefield.focus();
    return false;
  }
  else {
    return true;
  }
}

var lastdatestr;
var lasttimestr;

function chkdate(objName) {
  var datefield = objName;
  ok=chkdatetimestr(datefield.value);
  if (ok) {
    datefield.value=lastdatestr;
  }
  return ok;
}

function chkdatetime(objName) {
  var datefield = objName;
  ok=chkdatetimestr(datefield.value);
  if (ok) {
    if (lasttimestr.length < 1) {
      datefield.value=lastdatestr;
    }
    else {  
      datefield.value=lastdatestr+" "+lasttimestr;
    }  
  }
  return ok;
}

function chkdatetimestr(strDateTime) {
//var strDatestyle = "US"; //United States date style
var strDatestyle = "EU";  //European date style
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
lastdatestr = strDateTime.split(" ")[0];
lasttimestr = strDateTime.split(" ")[1];

ampm = strDateTime.split(" ")[2]
if ((ampm!=undefined) && (ampm!="")) {
  lasttimestr=lasttimestr+" "+ampm
}  

if (lasttimestr==undefined) {lasttimestr=""}

strDate=lastdatestr;
strTime=lasttimestr;

if (strDate.length < 1) {
  return true;
}

for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
  if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
    strDateArray = strDate.split(strSeparatorArray[intElementNr]);
    if (strDateArray.length != 3) {
      err = 1;
      return false;
    }
    else {
      strDay = strDateArray[0];
      strMonth = strDateArray[1];
      strYear = strDateArray[2];
    }
    booFound = true;
    }
}

if (booFound == false) {
  if (strDate.length>5) {
    strDay = strDate.substr(0, 2);
    strMonth = strDate.substr(2, 2);
    strYear = strDate.substr(4);
  }
  else {
    err=99;
    return false;
  }  
}

if (strYear.length == 2) {
  intYear = parseInt(strYear, 10);
  if (!isNaN(intYear)) {
    if (intYear>49) {
      strYear = '19' + strYear;
    }
    else
    {
      strYear = '20' + LZero(2,intYear);
    }
  }
}
// US style
if (strDatestyle == "US") {
  strTemp = strDay;
  strDay = strMonth;
  strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
  err = 2;
  return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
  for (i = 0;i<12;i++) {
    if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
      intMonth = i+1;
      strMonth = strMonthArray[i];
      i = 12;
    }
  }

  if (isNaN(intMonth)) {
    err = 3;
    return false;
  }
}

intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
  err = 4;
  return false;
}

if (intMonth>12 || intMonth<1) {
  err = 5;
  return false;
}

if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
  err = 6;
  return false;
}

if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
  err = 7;
  return false;
}
if (intMonth == 2) {
  if (intday < 1) {
    err = 8;
    return false;
  }
  if (LeapYear(intYear) == true) {
    if (intday > 29) {
      err = 9;
      return false;
    }
  }
  else {
    if (intday > 28) {
      err = 10;
      return false;
    }
  } 
}   

//Changed for SQL SmallDate
//if ((intYear<1900) || (intYear>2100)) {
if ((intYear<1900) || (intYear>2078)) {
  err = 11;
  return false;
}

  if (strDatestyle == "US") {
    lastdatestr = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
  }
  else {
    lastdatestr = LZero(2,intday) + "/" + LZero(2,intMonth) + "/" + LZero(4,strYear);
  }
  
  if (strTime.length>0) {
    strTime = strTime.toUpperCase();
    pm = strTime.indexOf("PM")>0;
    strTime=strTime.replace("PM","");
    
    strhour = strTime.split(":")[0];
    strmin = strTime.split(":")[1];
    if (strhour==undefined) {strhour=strTime}
    if (strmin==undefined) {strmin=""}

    if (strmin.length<1) {
      strmin='0';
    }  

    inthour = parseInt(strhour,10);
    if (isNaN(inthour)) {
      return false;
    }

    if (inthour>11) {
      pm=true;
      if (inthour>12) {
        inthour=inthour-12;
      }  
    }
      
    intmin = parseInt(strmin,10);
    if (isNaN(intmin)) {
      return false;
    }
    
    lasttimestr = LZero(2,inthour) + ":" + LZero(2,intmin); 
    if (pm) {
      lasttimestr=lasttimestr+" PM"
    }  
    else {
      lasttimestr=lasttimestr+" AM"
    }  
  }  
  
return true;
}

function LZero(len, data) {
 s=String(data);
  slen=s.length;
  while (slen<len) {
    s="0"+s;
    slen=s.length;
  }
  return s; 
}

function LeapYear(intYear) {
 if (intYear % 100 == 0) {
    if (intYear % 400 == 0) { return true; }
  }
  else {
    if ((intYear % 4) == 0) { return true; }
  }
  return false;
}

function doDateCheck(from, to) {
 //this routine doesn't work with dd/mm/yyyy
 if (Date.parse(from.value) <= Date.parse(to.value)) {
   alert("The dates are valid.");
 }
 else {
   if (from.value == "" || to.value == "") {
     alert("Both dates must be entered.");
   }
   else {
     alert("To date must occur after the from date.");
   }  
  }
}

function adddaystodate(adddays,indate) {
  if (indate.length<1) {
    return ''
  }
  else {  
    savelast = lastdatestr;
    var aDate = new Date();

    if (indate=='now') {
      lastdatestr=LZero(2,aDate.getDate()) + "/" + LZero(2,aDate.getMonth()+1) + "/" + LZero(4,aDate.getFullYear());
    }
    else {
      chkdatetimestr(indate);
    }
    var dayfield = lastdatestr.split("/")[0]
    var monthfield = lastdatestr.split("/")[1]
    var yearfield = lastdatestr.split("/")[2]
    aDate.setDate(dayfield);
    aDate.setMonth(monthfield-1);
    aDate.setYear(yearfield);
    aDate.setTime(aDate.getTime() + (adddays*24*60*60*1000))  
    lastdatestr = savelast;
    return LZero(2,aDate.getDate()) + "/" + LZero(2,aDate.getMonth()+1) + "/" + LZero(4,aDate.getFullYear());
  }  
}

function dayofweek(indate) {
  if (indate.length<1) {
    return -1
  }
  else {  
    savelast = lastdatestr;
    var aDate = new Date();

    if (indate=='now') {
      lastdatestr=LZero(2,aDate.getDate()) + "/" + LZero(2,aDate.getMonth()+1) + "/" + LZero(4,aDate.getFullYear());
    }
    else {
      chkdatetimestr(indate);
    }
    var dayfield = lastdatestr.split("/")[0]
    var monthfield = lastdatestr.split("/")[1]
    var yearfield = lastdatestr.split("/")[2]
    aDate.setDate(dayfield);
    aDate.setMonth(monthfield-1);
    aDate.setYear(yearfield);
    lastdatestr = savelast;
    return aDate.getDay();
  }  
}

function comparedate(date1,date2) {
  if (date1.length<1) {
    return 'date1 is invalid'
  }
  if (date2.length<1) {
    return 'date2 is invalid'
  }
  savelast = lastdatestr;
  var aDate1 = new Date();
  var aDate2 = new Date();

  if (date1=='now') {
    lastdatestr=LZero(2,aDate.getDate()) + "/" + LZero(2,aDate.getMonth()+1) + "/" + LZero(4,aDate.getFullYear());
  }
  else {
    chkdatetimestr(date1);
  }
  
  var dayfield = lastdatestr.split("/")[0]
  var monthfield = lastdatestr.split("/")[1]
  var yearfield = lastdatestr.split("/")[2]
  aDate1.setDate(dayfield);
  aDate1.setMonth(monthfield-1);
  aDate1.setYear(yearfield);

  if (date2=='now') {
    lastdatestr=LZero(2,aDate.getDate()) + "/" + LZero(2,aDate.getMonth()+1) + "/" + LZero(4,aDate.getFullYear());
  }
  else {
    chkdatetimestr(date2);
  }

  var dayfield = lastdatestr.split("/")[0]
  var monthfield = lastdatestr.split("/")[1]
  var yearfield = lastdatestr.split("/")[2]
  aDate2.setDate(dayfield);
  aDate2.setMonth(monthfield-1);
  aDate2.setYear(yearfield);

  lastdatestr = savelast;

  if (aDate1.getTime()==aDate2.getTime()) {
    return 0
  }  
  if (aDate1.getTime()>aDate2.getTime()) {
    return 1
  }  
  if (aDate1.getTime()<aDate2.getTime()) {
    return -1
  }  
}   

function Now()
{
var currentTime = new Date();
var month = LZero(2, currentTime.getMonth() + 1)
var day = LZero(2, currentTime.getDate());
var year = currentTime.getFullYear();
return (day + "/" + month + "/" + year);
}

