
/*  $Id: coolDates.js,v 1.1 2005/07/28 08:09:59 ckoehler Exp $ */

//international strings
var textNoValidDateFormat =
    "Bitte ein gültiges Datum eintragen";

var textNoValidTimeFormat =
    "Bitte eine gültige Zeit eingeben";

var textDateNow =
    "JETZT";

var textDateToday =
    "HEUTE";

function convert_date(field1)
{
	var fLength = field1.value.length; // Length of supplied field in characters.
	var divider_values = new Array ('-','.','/',' ',':','_',','); // Array to hold permitted date seperators.  Add in '\' value
	var array_elements = 7; // Number of elements in the array - divider_values.
	var day1 = new String(null); // day value holder
	var month1 = new String(null); // month value holder
	var year1 = new String(null); // year value holder
	var divider1 = null; // divider holder
	var outdate1 = null; // formatted date to send back to calling field holder
	var counter1 = 0; // counter for divider looping
	var divider_holder = new Array ('0','0','0'); // array to hold positions of dividers in dates
	var s = String(field1.value); // supplied date value variable
	
	//If field is empty do nothing
	if ( fLength == 0 ) {
	   return true;
}

// Deal with today or now
if ( field1.value.toUpperCase() == textDateNow || field1.value.toUpperCase() == textDateToday ) {

        var newDate1 = new Date();

                if (navigator.appName == "Netscape") {
                var myYear1 = newDate1.getYear() + 1900;
                }
                else {
                        var myYear1 =newDate1.getYear();
                }

        var myMonth1 = newDate1.getMonth()+1;
        var myDay1 = newDate1.getDate();
        field1.value = myDay1 + "/" + myMonth1 + "/" + myYear1;
        fLength = field1.value.length;//re-evaluate string length.
        s = String(field1.value)//re-evaluate the string value.
}

//Check the date is the required length
if ( fLength != 0 && (fLength < 6 || fLength > 11) ) {
        invalid_date(field1);
        return false;
        }

// Find position and type of divider in the date
for ( var i=0; i<3; i++ ) {
        for ( var x=0; x<array_elements; x++ ) {
                if ( s.indexOf(divider_values[x], counter1) != -1 ) {
                        divider1 = divider_values[x];
                        divider_holder[i] = s.indexOf(divider_values[x], counter1);
                        counter1 = divider_holder[i] + 1;
                        break;
                }
        }
 }

// if element 2 is not 0 then more than 2 dividers have been found so date is invalid.
if ( divider_holder[2] != 0 ) {
   invalid_date(field1);
        return false;
}

// See if no dividers are present in the date string.
if ( divider_holder[0] == 0 && divider_holder[1] == 0 ) {

                //continue processing
                if ( fLength == 6 ) {//ddmmyy
                day1 = field1.value.substring(0,2);
                month1 = field1.value.substring(2,4);
                        year1 = field1.value.substring(4,6);
                        if ( (year1 = validate_year(year1)) == false ) {
                        invalid_date(field1);
                                return false;
                                }
                        }

                else if ( fLength == 7 ) {//ddmmmy
                day1 = field1.value.substring(0,2);
                        month1 = field1.value.substring(2,5);
                        year1 = field1.value.substring(5,7);
                        if ( (month1 = convert_month(month1)) == false ) {
                        invalid_date(field1);
                                return false;
                                }
                        if ( (year1 = validate_year(year1)) == false ) {
                        invalid_date(field1);
                                return false;
                                }
                        }
                else if ( fLength == 8 ) {//ddmmyyyy
                day1 = field1.value.substring(0,2);
                        month1 = field1.value.substring(2,4);
                        year1 = field1.value.substring(4,8);
                        }
                else if ( fLength == 9 ) {//ddmmmyyyy
                day1 = field1.value.substring(0,2);
                        month1 = field1.value.substring(2,5);
                        year1 = field1.value.substring(5,9);
                        if ( (month1 = convert_month(month1)) == false ) {
                        invalid_date(field1);
                                return false;
                                }
                        }

                if ( (outdate1 = validate_date(day1,month1,year1)) == false ) {
                alert(textNoValidDateFormat + ": " + field1.value);
                        field1.focus();
                        field1.select();
                        return false;
                        }

                field1.value = outdate1;
                return true;// All OK
                }

// 2 dividers are present so continue to process
if ( divider_holder[0] != 0 && divider_holder[1] != 0 ) {
        day1 = field1.value.substring(0, divider_holder[0]);
        month1 = field1.value.substring(divider_holder[0] + 1, divider_holder[1]);
        //alert(month1);
        year1 = field1.value.substring(divider_holder[1] + 1, field1.value.length);
        }

if ( isNaN(day1) && isNaN(year1) ) { // Check day and year are numeric
        invalid_date(field1);
        return false;
   }

if ( day1.length == 1 ) { //Make d day dd
   day1 = '0' + day1;
}

if ( month1.length == 1 ) {//Make m month mm
        month1 = '0' + month1;
}

if ( year1.length == 2 ) {//Make yy year yyyy
   if ( (year1 = validate_year(year1)) == false ) {
        invalid_date(field1);
                return false;
                }
}

if ( month1.length == 3 || month1.length == 4 ) {//Make mmm month mm
   if ( (month1 = convert_month(month1)) == false) {
        alert("month1" + month1);
        invalid_date(field1);
        return false;
   }
}

// Date components are OK
if ( (day1.length == 2 || month1.length == 2 || year1.length == 4) == false) {
   invalid_date(field1);
   return false;
}

//Validate the date
if ( (outdate1 = validate_date(day1, month1, year1)) == false ) {
   alert(textNoValidDateFormat + ": " + field1.value);
        field1.focus();
        field1.select();
        return false;
}

// Redisplay the date in dd/mm/yyyy format
field1.value = outdate1;
return true;//All is well

}
/******************************************************************
   convert_month()

   Function to convert mmm month to mm month

   Called by convert_date()

   Author: Simon Kneafsey
   Date Created: 4/9/00
   Email: simonkneafsey@hotmail.com
   WebSite: www.simonkneafsey.co.uk

   Notes:P lease feel free to use/edit this script.  If you do please keep my comments and details
   intact and notify me via a quick Email to the address above.  Enjoy!
*******************************************************************/
function convert_month(monthIn) {

var month_values = new Array ("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");

monthIn = monthIn.toUpperCase();

if ( monthIn.length == 3 ) {
        for ( var i=0; i<12; i++ )
                {
        if ( monthIn == month_values[i] )
                {
                        monthIn = i + 1;
                        if ( i != 10 && i != 11 && i != 12 )
                                {
                        monthIn = '0' + monthIn;
                                }
                        return monthIn;
                        }
                }
        }

else if ( monthIn.length == 4 && monthIn == 'SEPT') {
   monthIn = '09';
   return monthIn;
        }

else {
        return false;
        }
}
/******************************************************************
   invalid_date()

   If an entered date is deemed to be invalid, invali
   d_date() is called to display a warning message to
   the user.  Also returns focus to the date  in que
   stion and selects the date for edit.

   Called by convert_date()

*******************************************************************/
function invalid_date(inField)
{
    alert(textNoValidDateFormat);
    inField.focus();
    inField.select();
    return true
}
/******************************************************************
   validate_date()

   Validates date output from convert_date().  Checks
   day is valid for month, leap years, month !> 12,.

*******************************************************************/
function validate_date(day2, month2, year2)
{
var DayArray = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var MonthArray = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
var inpDate = day2 + month2 + year2;
var filter=/^[0-9]{2}[0-9]{2}[0-9]{4}$/;

//Check ddmmyyyy date supplied
if (! filter.test(inpDate))
  {
  return false;
  }
/* Check Valid Month */
filter=/01|02|03|04|05|06|07|08|09|10|11|12/ ;
if (! filter.test(month2))
  {
  return false;
  }
/* Check For Leap Year */
var N = Number(year2);
if ( ( N%4==0 && N%100 !=0 ) || ( N%400==0 ) )
        {
   DayArray[1]=29;
        }
/* Check for valid days for month */
for(var ctr=0; ctr<=11; ctr++)
        {
   if (MonthArray[ctr]==month2)
        {
      if (day2<= DayArray[ctr] && day2 >0 )
        {
        //changed '/' into fullstops. for german format!
        inpDate = day2 + '.' + month2 + '.' + year2;
        return inpDate;
        }
      else
        {
        return false;
        }
        }
   }
}
/******************************************************************
   validate_year()

   converts yy years to yyyy
   Uses a hinge date of 10
        < 10 = 20yy
        => 10 = 19yy.

   Called by convert_date() before validate_date().

*******************************************************************/
function validate_year(inYear)
{
if ( inYear < 10 )
        {
   inYear = "20" + inYear;
   return inYear;
        }
else if ( inYear >= 10 )
        {
   inYear = "20" + inYear;
   return inYear;
        }
else
        {
        return false;
        }
}


/******************************************************************
   istime - validate time format (hh:mm:ss)
   note: requires function alltrim() in file alltrim.cfm
   it also requires nochars() in file isdate.cfm

   checks time format
*******************************************************************/

/* nochars: check for the existence of a non int character within a string */
function nochars(strval) {
      var retval=true
	   for (var i=0; i<strval.length;i++) {
		 if (strval.substring(i,i+1) < "0" || strval.substring(i,i+1) > "9") {
		       retval=false
		       break
		 }
	   }
	   return retval
}

/* Trim leading and trailing blanks from an input field */
function alltrim(obj){
   var trimmedstring="",startpos=0, endpos=obj.value.length - 1
   /* find start position of string */	
   while (startpos<=obj.value.length && obj.value.substring(startpos, startpos+1)==" ") {
 	   startpos++
   }
   /* null string */
   if (endpos == -1) {
     endpos=0
   }
   /* find end position of string */
   while (endpos >= 0 && obj.value.substring(endpos,endpos+1)==" ") {
	   endpos--
   }
   /* replace value with trimmed string */
   obj.value=obj.value.substring(startpos,endpos+1)
   return obj
}

/* istime - validate time format (hh:mm:ss) or (hh:mm)*/
function istime(obj,retval) {
 if (retval) {
  retval=false
  var hours, minutes, seconds
  obj=alltrim(obj)
  if (obj.value.length==5) { /* seconds are optional */
	   obj.value+=":00"
  }
  
  // if no time value is given, return empty string and true!
  if (obj.value.length==0)
  {
       retval=true
  }
  
  
  if (obj.value!=""){
	   /* check formatting of string */
	   if (obj.value.length != 8 || obj.value.substring(2,3)!=":" || obj.value.substring(5,6)!=":") {
	   }
	   else {
		  /* Hours must be between 0 and 24 */
		  hours=obj.value.substring(0,2)
		  if (nochars(hours) && parseInt(hours) >= 0 && parseInt(hours) < 24) {
			hours=parseInt(hours)
			minutes=obj.value.substring(3,5)
			if (nochars(minutes)) {
			      minutes=parseInt(minutes)
			      if (minutes >=0 && minutes <=59) {
				    seconds=obj.value.substring(6,8)
				    /* seconds between 0 and 59 */
				    if (nochars(seconds) && parseInt(seconds) >= 0 && parseInt(seconds)<=59) {
					  retval=true
				    }
			      }
			
		        }
		 }
	   }
	   if (!retval) {
        alert(textNoValidTimeFormat);
       obj.focus();
       obj.select();
	   return false;
       }
    return true;
  }
 }
 return retval;
}

/******************************************************************/

/* startVsEndDate - check if the startDate greater than the endDate
 * return true the enddate is greater as the startdate - is correct
 * return false the enddate is not greater as the startdate - is not correct
 */
function startVsEndDate (startDate, endDate)
{
    arrayStart = startDate.split(".");
    arrayEnd   = endDate.split(".");

    // 0 = day, 1 = month, 2 = year
    if( (Number(arrayStart[2])) <= (Number(arrayEnd[2])) )
    {
        if((Number(arrayStart[2])) == (Number(arrayEnd[2])))
        {
            if((Number(arrayStart[1])) <= (Number(arrayEnd[1])))
            {
                if((Number(arrayStart[1])) == (Number(arrayEnd[1])))
                {
                    if((Number(arrayStart[0])) <= (Number(arrayEnd[0])))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                   return true;
                }
            }
            else
            {
               return false;
            }
        }
        else
        {
           return true;
        }
    }
    else
    {
       return false;
    }
}


