/*************************************************************************
  Name      : \DivFormWAPI.js
  Purpose   : Severeral handy form management functions

  Version   : 20000413
  History   : 20000413 JH  Added a way to let controll characters through in the
                           checkNum function.
              19991007 JH  Creation of file

  Usage     : Description in DivForm Help.htm
  functions : function getSelectedItemsArray(selectObj)
              function getSelectedItems(selectObj)
              function indexOfOptionValue(selectObj, Value)
              function setSelectedItems(selectObj, Value)
              function AddIgnoredChangedFormElement(FormName, ElName)
              function InitFormChanges()
              
              function isCorrectDateStr(aDate, aFormat) : boolean;
              function isCorrectTimeStr(aTime, aFormat, aType) : boolean;
              function isCorrectDateTimeStr(aDateTime, aDateFormat, aTimeFormat, aType) : boolean;
              function isBeforeNow(aDateTime, aDateFormat, aTimeFormat, aType) : boolean;
              function StringToDate(StrDate, Format, toLocalTime) : Date;
              function DateToString(DateStr, Format) : String;
              function dateAndTimeToStr(aDate, aTime, aDateFormat, aTimeFormat, aType) : String;
              function dateTimeToStr(aDateTime, aDateFormat, aTimeFormat, aType) : String;
              function strToDateTime(aDateTime, aDateFormat, aTimeFormat, aType) : Date;
              function timeToStr(aTime, aFormat, aType) : String;
              function stringToTime(aTime, aFormat, aType, aBreak) : Date;
              function strToTime(aStrTime, aFormat, aType) : Date;
              function xmlDateTimeToStr(aDate)
              function xmlStrToDateTime(aDateStr)

*/

//Global settings / Consts
  var OrgFormValues      = new Array()
  var IgnoreChangedFormElements = new Array()

  var ErrMsgInvalidDate    = "Invalid date. Expecting valid date by format %s"
  var ErrMsgInvalidTime    = "Invalid time. Expecting valid time by format %s"
  var ErrMsgDateToHigh     = "Expecting date before %s"
  var ErrMsgDateToLow      = "Expecting date after %s"
  var ErrMsgTimeToHigh     = "Expecting time less than %s"
  var ErrMsgTimeToLow      = "Expecting time greater than %s"
  var ErrMsgNumberToHigh   = "Expecting number lower than %s"
  var ErrMsgNumberToLow    = "Expecting number higher than %s"
  var ErrMsgRequiredEmpty  = "Required field is empty"
  var ErrMsgFormFieldsErr  = "Some elements on the form are not correct:\n"

  var is24Clock            = 0;
  var is12ClockAM          = 1;
  var is12ClockPM          = 2;
  var is12Clock            = 3;
  var isUnknownClock       = 4;
  var EmptyDate            = new Date(0);
  var invalidDateTimeFormat= new Date(-9);
  var invalidDateFormat    = new Date(-8);
  var invalidTimeFormat    = new Date(-7);
  var invalidDate          = new Date(-1);
  var invalidTime          = new Date(-2);
  var invalidDateTime      = new Date(-3);
  var dateFormat           = "dd/mm/yyyy"
  var dateSeparator        = "/"
  var timeFormat           = "HH:nn:ss"
  var timeSeparator        = ":"
  var numberDecimalSymbol  = '.'
  var DigitGroupingSymbol  = ''
  var NrOfDigitsInGroup    = 30
  var CurrencySymbol       = '$'
  var CurrencyPlacement    = 'C1.1'
  var CurrencyDigits       = 2
  var MonthNames           = new Array("January","February","March","April","May",
                                   "June","July","August","September",
                                   "October","November","December");
  var MonthNamesShort      = new Array("Jan","Feb","Mar","Apr","May",
                                   "Jun","Jul","Aug","Sep",
                                   "Oct","Nov","Dec");
  var DayNames             = new Array("Sunday","Monday","Tuesday","Wednesday",
                                   "Thursday","Friday","Saturday");
  var DayNamesShort        = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");




////////////////////////////////////////////////////////////////////////////////
/////////////           select-(multiple/one) functions        /////////////////
////////////////////////////////////////////////////////////////////////////////

  function getSelectedItemsArray(selectObj) {
    var selected = new Array();
    var index = 0;
    for (var intLoop = 0; intLoop < selectObj.length; intLoop++) {
      if ((selectObj[intLoop].selected) ||
           (selectObj[intLoop].checked)) {
        index                 = selected.length;
        selected[index]       = new Object;
        selected[index].value = selectObj[intLoop].value;
        selected[index].index = intLoop;
      } // if
    } // for
    return selected;
  } // function getSelectedItemsArray()


  function getSelectedItems(selectObj) {
    var sel = getSelectedItemsArray(selectObj);
    var strSel = "";
    for (var item in sel) {
      if (strSel != "") {
         strSel = strSel + ','
      } // if
      strSel += sel[item].value;
    } // for
    return strSel;
  } // function getSelectedItems()


  function indexOfOptionValue(selectObj, Value) {
    var Result = -1
    var I      =  0
    while ((Result == -1) && (I < selectObj.length)) {
      if (selectObj[I].value == Value) {
        Result = I
      } // if
      I++
    } // while
  } // function indexOfOptionValue()


  function setSelectedItems(selectObj, Value) {
    Value = ','+Value+','
    var I = 0
    while (I < selectObj.length) {
      selectObj[I].selected = (Value.indexOf(','+selectObj[I].value+',') >= 0)
      I++
    } // while
  } // function setSelectedItems()





////////////////////////////////////////////////////////////////////////////////
//////////////           Form changes made functions          //////////////////
////////////////////////////////////////////////////////////////////////////////

  function AddIgnoredChangedFormElement(FormName, ElName) {
    IgnoreChangedFormElements[IgnoreChangedFormElements.length] = FormName
    IgnoreChangedFormElements[IgnoreChangedFormElements.length] = ElName
  } // function AddIgnoredChangedFormElement()

  function IsIgnoredChangedForm(FormName) {
    return IsIgnoredChangedFormElement(FormName, '*')
  } // function IsIgnoredChangedForm()

  function IsIgnoredChangedFormElement(FormName, ElName) {
    var Result = false
    var I      = 0
    while ((!Result) && (I < IgnoreChangedFormElements.length)) {
      if ((IgnoreChangedFormElements[I] == FormName) && (IgnoreChangedFormElements[I+1] == ElName)) {
        Result = true
      }  // if
      I += 2
    } // while
    return Result
  } // function IsIgnoredChangedFormElement()


  function InitFormChanges() {
    OrgFormValues = new Array()
    var I = 0
    while (I < document.forms.length) {
      var FormName = document.forms[I].name
      if (!IsIgnoredChangedForm(FormName)) {
        var J        = 0
        while (J < document.forms[I].length) {  // Nr of elements on the form
          var ElName = document.forms[I].elements[J].name
          var ElType = document.forms[I].elements[J].type

          if (!IsIgnoredChangedFormElement(FormName, ElName)) {
            var MustSave = false;
            switch (ElType) {
              case "button"          :
                var OrgValue = document.forms[I].elements[J].value
                break
              case "checkbox"        :
                var OrgValue = document.forms[I].elements[J].checked
                MustSave = true
                break
              case "file"            :
                var OrgValue = document.forms[I].elements[J].value
                MustSave = true
                break
              case "hidden"          :
                var OrgValue = document.forms[I].elements[J].value
                MustSave = true
                break
              case "image"           :
                alert('Input type image has no value')
                break
              case "password"        :
                var OrgValue = document.forms[I].elements[J].value
                MustSave = true
                break
              case "radio"           :
                var OrgValue = document.forms[I].elements[J].checked
                MustSave = true
                break
              case "reset"           :
                var OrgValue = document.forms[I].elements[J].value
                break
              case "select-multiple" :
                var OrgValue = getSelectedItems(document.forms[I].elements[J])
                MustSave = true
                break
              case "select-one"      :
                var OrgValue = document.forms[I].elements[J].value
                MustSave = true
                break
              case "submit"          :
                var OrgValue = document.forms[I].elements[J].value
                break
              case "text"            :
                var OrgValue = document.forms[I].elements[J].value
                MustSave = true
                break
              case "textarea"        :
                var OrgValue = document.forms[I].elements[J].value
                MustSave = true
                break
              default                :
                if (((typeof ElName)=="undefined") || ((typeof ElType)=="undefined")) {
//                  alert(document.forms[I].elements[J].tagName)
                }
                else  {
                  alert('Unknown input type. Input nr.'+I+'  name: '+ElName+' (type: '+ ElType+')');
                }//endif
                break
            } // switch
            if (MustSave == true) {
              OrgFormValues[OrgFormValues.length] = FormName
              OrgFormValues[OrgFormValues.length] = ElName
              OrgFormValues[OrgFormValues.length] = ElType
              OrgFormValues[OrgFormValues.length] = OrgValue
            }//endif MustSave
          }//endif not IgnoredChangeFormElement
          J++

        } // while
      } // if not IgnoredChangeForm
      I++
    } // while
  }  // function InitFormChanges()


  function CompareInputChanges() {
    var Changes = new Array()
    var I = 0
    while (I < OrgFormValues.length) {
      var FormName = OrgFormValues[I+0]
      var ElName   = OrgFormValues[I+1]
      var ElType   = OrgFormValues[I+2]
      var OrgValue = OrgFormValues[I+3]
      if ((ElType == "checkbox") ||(ElType == "radio")) {
        if (document.forms[FormName].elements[ElName].checked != OrgValue) {
          Changes[Changes.length] = FormName
          Changes[Changes.length] = ElName
          Changes[Changes.length] = ElType
        }
      } else {
        if ((ElType == "file") ||(ElType == "hidden") ||(ElType == "password") ||(ElType == "text") ||(ElType == "textarea") ||(ElType == "select-one")) {
          if (document.forms[FormName].elements[ElName].value != OrgValue) {
            Changes[Changes.length] = FormName
            Changes[Changes.length] = ElName
            Changes[Changes.length] = ElType
          }
        } else {
          if ((ElType == "select-multiple")) {
            if (getSelectedItems(document.forms[FormName].elements[ElName]) != OrgValue) {
              Changes[Changes.length] = FormName
              Changes[Changes.length] = ElName
              Changes[Changes.length] = ElType
            }
          } else {
          }
        }
      }
      I += 4
    }
    return Changes
  }

  function IsFormChanged() {
    if (OrgFormValues.length <= 0) { return true }  // If no original 
    var Result = false
    var ChangedElements = CompareInputChanges()
    if (ChangedElements.length > 0) {
      Result = true
    } // if
    return Result
  } // function IsFormChanged()




////////////////////////////////////////////////////////////////////////////////
/////////////       form check/data validation functions       /////////////////
////////////////////////////////////////////////////////////////////////////////
  function isStrInt(value) {
    if ((typeof value) != 'string')
      return ( ((typeof value) == 'number') );
    else if (value=='') 
      return (false);
    else if (value.length==1) 
      return (isAllValidChars(value, '0123456789'));
    else
      return (isAllValidChars(value.substr(1,1), '-+0123456789') && isAllValidChars(value.substr(2), '0123456789'));
  }//endfunction isStrInt()
  

  function isDateCorrect(dateType, timeType, dateFormat, timeFormat, varName, allowEmpty) {
    var result = true;
    
    var clockType = isUnknownClock;
    if (getObject(varName+'_AmPm') != null) {
      clockType = is12Clock;
      if (getRadioValue(varName+'_AmPm') == 'PM')
        clockType = is12ClockPM;
      else if (getRadioValue(varName+'_AmPm') == 'AM')
        clockType = is12ClockAM;
    } else { 
		clockType = is24Clock;
    }

    if ((dateType == 0) && (timeType == 0)) {
      dtValue  = getValue(varName+'_DateTime');
      if (isCorrectDateTimeStr(dtValue, dateFormat, timeFormat, clockType)) {
        var newDate    = strToDateTime(dtValue, dateFormat, timeFormat, clockType);
        setValue(varName, xmlDateTimeToStr(newDate));
      } else if (dtValue == "") {
         result = allowEmpty
      } else
        result = false;
    } else {
      var dateValue = '';
      var timeValue = '';
    
      if (dateType == 2) {
        dateValue = getValue(varName+'_Date');
      } else if ((dateType == 3) || (dateType == 4)) {
        var year;
        if (getObject(varName+'_Year') != null) 
          year = getValue(varName+'_Year');
        else
          year = ''+(new Date()).getFullYear();
        var month = '';
        if (getObject(varName+'_Month') != null)
          month = getValue(varName+'_Month');
        else
          month = '1';
        var day   = '';
        if (getObject(varName+'_Day') != null) 
          day   = getValue(varName+'_Day');
        else
          day   = '1';
        if ((year != '') && (month != '') && (day != '')) {
          if ((isStrInt(year)) && (isStrInt(month)) && (isStrInt(day))) { 
            var newDate = new Date( StringToInt(year), StringToInt(month)-1, StringToInt(day) );
            if ((newDate.getDate()== day) && (newDate.getMonth()==(month-1)) && ((newDate.getFullYear()==year) || (newDate.getYear()==year))) {
              dateValue = xmlDateTimeToStr(newDate);
            } else {
              result = false;
            }//endif
          } else
            result = false;
        } else {
          if ((year+month+day) != '') 
            result = false;
          else
            result = allowEmpty;
        }//endif
      } else if (dateType > 0) {
        dtValue  = getValue(varName+'_Date');
        if (isCorrectDateStr(dtValue, dateFormat)) {
          var newDate    = StringToDate(dtValue, dateFormat);
          dateValue = xmlDateTimeToStr(newDate);
        } else if (dtValue == "") {
            result = allowEmpty
        } else
            result = false;
      }//endif
      
      if (timeType == 2) {
        timeValue = getValue(varName+'_Time');
        if (timeValue == 'T00:00:00' ) {
          result = false;
        }
      } else if ((timeType == 3) || (timeType == 4)) {
        var hour = getValue(varName+'_Hour');
        var minute = getValue(varName+'_Minute');
        var second = '';
        if (getObject(varName+'_Second') != null) 
          second = getValue(varName+'_Second');
        else
          second = '0';

        if ((hour != '') && (minute != '') && (second != '')) {
          if ((isStrInt(hour)) && (isStrInt(minute)) && (isStrInt(second))) { 
            if ((clockType == is12ClockPM) && (hour < 12) && (hour != 0)) {
              hour = ''+((hour*1) + 12);
            } else if  ((clockType == is12ClockAM) && (hour == 12)) {
              hour = 0;
            }//endif
            var newTime = new Date(1970, 1, 1,  StringToInt(hour), StringToInt(minute), StringToInt(second) );
            if ((newTime.getHours()== hour) && (newTime.getMinutes()==minute) && ((newTime.getSeconds()==second))) {
              timeValue	= xmlDateTimeToStr(newTime);
            } else
              result = false;
          } else
            result = false;
        } else {
          if ((hour+minute) != '') 
            result = false;
          else
            result = allowEmpty;
        }//endif
      } else if (timeType > 0) {
        dtValue  = getValue(varName+'_Time');
        if (isCorrectTimeStr(dtValue, timeFormat, clockType)) {
          var newTime    = strToTime(dtValue, timeFormat, clockType);
          timeValue = xmlDateTimeToStr(newTime);
        } else if (dtValue == "") {
          result = allowEmpty;
        } else
          result = false;
      }//endif
      
      if (dateValue.indexOf('T') > 0) 
        dateValue = dateValue.substr(0, dateValue.indexOf('T'));
      
      if (timeValue.indexOf('T') > 0) 
        timeValue = timeValue.substr(timeValue.indexOf('T'));
        
      setValue(varName, dateValue+timeValue);
    }//endif
//alert(varName+':='+getValue(varName)+'   (Result: '+result+')');
    return (result);
  }//endfunction isDateCorrect()
  
  
  function isDateEmpty(dateType, timeType, varName) {
    var result = false;
    
    if (getObject(varName+'_AmPm') != null) {
      if ((getRadioValue(varName+'_AmPm') != 'PM') && (getRadioValue(varName+'_AmPm') == 'AM')) {
        return ( true );
      }//endif
    }//endif 
        

    if ((dateType == 0) && (timeType == 0)) {
      dtValue  = getValue(varName+'_DateTime');
      return (dtValue == '');
    } else {
      var dateValue = '';
      var timeValue = '';
    
      if (dateType == 2) {
        dateValue = getValue(varName+'_Date');
        if (dateValue == '')
          return (true);
      } else if ((dateType == 3) || (dateType == 4)) {
        var year;
        if (getObject(varName+'_Year') != null) 
          year = getValue(varName+'_Year');
        else
          year = ''+(new Date()).getFullYear();
        var month = '';
        if (getObject(varName+'_Month') != null)
          month = getValue(varName+'_Month');
        else
          month = '1';
        var day   = '';
        if (getObject(varName+'_Day') != null) 
          day   = getValue(varName+'_Day');
        else
          day   = '1';
        if ((year == '') && (month == '') && (day == '')) {
          return (true);
        }//endif
      } else if (dateType > 0) {
        dtValue  = getValue(varName+'_Date');
        if (dtValue == '') 
          return ( true );
      }//endif
      
      if (timeType == 2) {
        timeValue = getValue(varName+'_Time');
        if (timeValue == '')
          return ( true );
      } else if ((timeType == 3) || (timeType == 4)) {
        var hour = getValue(varName+'_Hour');
        var minute = getValue(varName+'_Minute');
        var second = '';
        if (getObject(varName+'_Second') != null) 
          second = getValue(varName+'_Second');
        else
          second = '';

        if ((hour == '') && (minute == '') && (second == '')) 
          return ( true );
      } else if (timeType > 0) {
        dtValue  = getValue(varName+'_Time');
        if (dtValue == '')
          return ( true );
      }//endif
    }//endif
    return ( false );
  }//endfunction isDateEmpty()

  

  function SetDateFormat(NewFormat) {
    dateFormat = NewFormat

    var ExtraDateChars = ""
    var I              = 0
    while (I < NewFormat.length) {
      if (("ymdYMD".indexOf(NewFormat.substr(I, 1)) == -1) && (ExtraDateChars.indexOf(NewFormat.substr(I, 1)) == -1)) {
        ExtraDateChars += NewFormat.substr(I, 1)
      } // endif
      I++
    } // endwhile
    dateSeparator = ExtraDateChars
  } // end function SetDateFormat()


  function SetTimeFormat(NewFormat) {
    timeFormat = NewFormat
    var ExtraTimeChars = ""
    var I              = 0
    while (I < NewFormat.length) {
      if (("hHnNsS".indexOf(NewFormat.substr(I, 1)) == -1) && (ExtraTimeChars.indexOf(NewFormat.substr(I, 1)) == -1)) {
        ExtraTimeChars += NewFormat.substr(I, 1)
      } // endif
      I++
    } // endwhile
    timeSeparator = ExtraTimeChars
  } // end function SetTimeFormat()
  

  function SetNumberFormat(NewFormat) {
    if (NewFormat.indexOf('thousandsep=') >= 0) {
      DigitGroupingSymbol = NewFormat.substr(NewFormat.indexOf('thousandsep=')+12, 1)
    }//endif
    if (NewFormat.indexOf('thousandgrp=') >= 0) {
      var Hulp = NewFormat.substr(NewFormat.indexOf('thousandgrp=')+12)
      var P    = 0
      while ((P<Hulp.length) && ("0123456789".indexOf(Hulp.substr(P,1)) >= 0)) {
        P++
      }//enddo
      Hulp = Hulp.substr(0, P)
      NrOfDigitsInGroup = 1 * Hulp
    }//endif
    if (NewFormat.indexOf('decsep=') >= 0) {
      numberDecimalSymbol = NewFormat.substr(NewFormat.indexOf('decsep=')+7, 1)
    }//endif

  }//endfunction SetNumberFormat()


  function InitFormChecks() {
    if (document.forms[0].DateFormat) {
      SetDateFormat(document.forms[0].DateFormat.value)
    } // endif

    if (document.forms[0].TimeFormat) {
      SetTimeFormat(document.forms[0].TimeFormat.value)
    } // endif

    if (document.forms[0].NumberFormat) {
      SetNumberFormat(document.forms[0].NumberFormat.value)
    } // endif

    var I = 0
    while (I < document.forms.length) {
      var FormName = document.forms[I].name
      if (!IsIgnoredChangedForm(FormName)) {
        var J        = 0
        while (J < document.forms[I].length) {  // Nr of elements on the form
          var ElName = document.forms[I].elements[J].name
          var ElType = document.forms[I].elements[J].type

          if (!IsIgnoredChangedFormElement(FormName, ElName)) {
            if (null!=document.forms[I].elements[J].getAttribute("required")) {
              // Add requered style id
            }

            var Format = ''
            if (null!=document.forms[I].elements[J].getAttribute("dataformat")) {
              Format = document.forms[I].elements[J].dataformat
            }//endif

            if (null!=document.forms[I].elements[J].getAttribute("datatype")) {
              var DataType = document.forms[I].elements[J].getAttribute("datatype");
              if ((ElType=="text") || (ElType=="textarea") || (ElType=="hidden") || (ElType=="password")) {
                if (document.forms[I].elements[J].Initialized != true) {
                  if       (DataType=="date") {
                    var Name = 'tempdate'
                    if ((document.forms[I].elements[J].name) && (document.forms[I].elements[J].name != '')) {
                      Name = document.forms[I].elements[J].name
                    }//endif name exists
                    if ((document.forms[I].elements[J].id) && (document.forms[I].elements[J].id != '')) {
                      Name = document.forms[I].elements[J].id
                    }//endif id exists

                    document.forms[I].elements[J].outerHTML = document.forms[I].elements[J].outerHTML +
                        '<input name="'+Name+'Button" type="button" value="..." '+
                        'class="' + document.forms[I].elements[J].className + '" ' +
                        'style="height:22px" ' +
                        'onClick="FindDate(event, document.forms['+I+'].'+Name+')">'

                    var FuncName = ''
                    if (document.forms[I].elements[J].onchange != null) {
                      FuncName = 'OnChange'+document.forms[I].elements[J].name
                      eval('var '+FuncName+' = document.forms[I].elements[J].onchange')
                    }//endif
                    eval('document.forms[I].elements[J].onchange   = function Temp(e) { checkFormat(e); '+ (FuncName!=""?FuncName+'()':"") +'  }')
//                    document.forms[I].elements[J].onchange   = function Temp() { checkFormat(); }

                    document.forms[I].elements[J].onkeypress = function Temp(e) { checkNum(e, dateSeparator); }
                  }else if (DataType=="string") {
                  }else if (DataType=="spinner") {
                    var ThousandSep = ''
                    if (Format.indexOf('thousandsep=') >= 0) {
                      ThousandSep = Format.substr(Format.indexOf('thousandsep=')+12, 1)
                    }else{
                      if (Format.indexOf('thousandgrp=') >= 0) {
                        ThousandSep = DigitGroupingSymbol
                      }//endif
                    }//endif


                    var FuncName = ''
                    if (document.forms[I].elements[J].onchange != null) {
                      FuncName = 'OnChange'+document.forms[I].elements[J].name
                      eval('var '+FuncName+' = document.forms[I].elements[J].onchange')
                    }//endif

                    var SpinObjName = document.forms[I].elements[J].name
                    document.forms[I].elements[J].outerHTML =
                        '<table cellspacing="0" cellpadding="0" border="0"><tr><td>'+
                        document.forms[I].elements[J].outerHTML +
                        '</td><td>'+
                        '<table cellspacing="0" cellpadding="0" border="0"><tr>'+
                        '<td onclick="SpinnerInc('+"'"+SpinObjName +"'"+')">'+
                        '<img style="height: 11px; vertical-align: bottom" src="/Images/SpinnerUp.gif">'+
                        '</td></tr><tr>'+
                        '<td onclick="SpinnerDec('+"'"+ SpinObjName+"'"+')">'+
                        '<img style="height: 11px; vertical-align: top" src="/Images/SpinnerDown.gif">'+
                        '</td></tr></table>'+
                        '</td></tr></table>';


                    document.forms[I].elements[J].style.textAlign = "right"
                    eval('document.forms[I].elements[J].onchange   = function Temp(e) { checkFormat(e); '+ (FuncName!=""?FuncName+'()':"") +'  }')
                    document.forms[I].elements[J].onkeypress = function Temp(e) { checkNum(e, '-'+ThousandSep) }
                  }else if (DataType=="time") {
                    var TimeSep = timeSeparator
                    if (Format.indexOf('timesep=') >= 0) {
                      TimeSep = Format.substr(Format.indexOf('timesep=')+8, 1)
                    }//endif
                    document.forms[I].elements[J].style.textAlign = "right"

                    var FuncName = ''
                    if (document.forms[I].elements[J].onchange != null) {
                      FuncName = 'OnChange'+document.forms[I].elements[J].name
                      eval('var '+FuncName+' = document.forms[I].elements[J].onchange')
                    }//endif
//                    document.forms[I].elements[J].onchange   = function Temp() { checkFormat(); }
                    eval('document.forms[I].elements[J].onchange   = function Temp(e) { checkFormat(e); '+ (FuncName!=""?FuncName+'()':"") +'  }')

                    document.forms[I].elements[J].onkeypress = function Temp(e) { checkNum(e, TimeSep) }
                  }else if (DataType=="integer") {
                    var ThousandSep = ''
                    if (Format.indexOf('thousandsep=') >= 0) {
                      ThousandSep = Format.substr(Format.indexOf('thousandsep=')+12, 1)
                    }else{
                      if (Format.indexOf('thousandgrp=') >= 0) {
                        ThousandSep += DigitGroupingSymbol
                      }//endif
                    }//endif
                    document.forms[I].elements[J].style.textAlign = "right"


                    var FuncName = ''
                    if (document.forms[I].elements[J].onchange != null) {
                      FuncName = 'OnChange'+document.forms[I].elements[J].name
                      eval('var '+FuncName+' = document.forms[I].elements[J].onchange')
                    }//endif
//                    document.forms[I].elements[J].onchange   = function Temp() { checkFormat(); }
                    eval('document.forms[I].elements[J].onchange   = function Temp(e) { checkFormat(e); '+ (FuncName!=""?FuncName+'()':"") +'  }')

                    document.forms[I].elements[J].onkeypress = function Temp(e){ checkNum(e, '-'+ThousandSep) }
                  }else if (DataType=="real") {
                    if (Format.indexOf('decsep=') >= 0) {
                      var Sep = Format.substr(Format.indexOf('decsep=')+7, 1)
                    }else{
                      var Sep = numberDecimalSymbol
                    }//endif
                    if (Format.indexOf('thousandsep=') >= 0) {
                      Sep += Format.substr(Format.indexOf('thousandsep=')+12, 1)
                    }else{
                      if (Format.indexOf('thousandgrp=') >= 0) {
                        Sep += DigitGroupingSymbol
                      }//endif
                    }//endif
                    document.forms[I].elements[J].style.paddingRight = '5px'
                    document.forms[I].elements[J].style.textAlign = "right"
  //                    document.forms[I].elements[J].style = 'IntInput'

                    var FuncName = ''
                    if (document.forms[I].elements[J].onchange != null) {
                      FuncName = 'OnChange'+document.forms[I].elements[J].name
                      eval('var '+FuncName+' = document.forms[I].elements[J].onchange')
                    }//endif
                    eval('document.forms[I].elements[J].onchange   = function Temp(e) { checkFormat(e); '+ (FuncName!=""?FuncName+'()':"") +'  }')
//                    document.forms[I].elements[J].onchange   = function Temp() { checkFormat(); }

                    document.forms[I].elements[J].onkeypress = function Temp(e) { checkNum(e, '-'+Sep) }
                  }else if (DataType=="zipcode") {
                  }else{
                  }//endif
                  document.forms[I].elements[J].Initialized = true
                }//endif
              } else {
                if (ElType=="checkbox") {
                } else {
                  if (ElType=="file") {
                  } else {
                    if (ElType=="select-multiple") {
                    } else {
                      if ((ElType=="button") || (ElType=="image") || (ElType=="radio") || (ElType=="select-one") || (ElType=="reset") || (ElType=="submit")) {
                      } else {
                      } // if other type
                    } // end if "select-multiple"
                  } // end if "file"
                } // end if "checkbox"
              } // end if "text" etc.
            } // if has datatype tag
          } // if not IgnoredChangeFormElement
          J++

        } // while
      } // if not IgnoredChangeForm
      I++
    } // while
  }  // function InitFormChanges()


  function FormatStr(Msg, VarArr) {
  var I    = 0
  var P = Msg.indexOf('%')
  while (P >= 0) {
    if (Msg.substr(P+1, 1) != '%') {
      var Last = Msg.substr(P+2)
      Msg = Msg.substr(0, P)
      Msg += VarArr[I]
      Msg += Last
    I++
    }else{
      Msg = Msg.substr(0, P)+ Msg.substr(P+1)
    }//endif
    P = Msg.indexOf('%', P+1)
  }//endwhile
  return Msg
}//function FormatStr()


  function DoCheckElmErr(ErrMsg, ValArr, ShowErrors, Elm) {
    ResultMsg = FormatStr(ErrMsg, ValArr);
    if (ShowErrors) {
      alert(ResultMsg)
    }//endif

    var Hulp = ''
    if (Elm) {
      if ((Elm.title) && (Elm.title!='')) {
        Hulp = Elm.title + ', '
      }else{
        if ((Elm.name) && (Elm.name!='')) {
          Hulp = Elm.name + ', '
        }else{
          if ((Elm.id) && (Elm.id!='')) {
            Hulp = Elm.id + ', '
          }else{
          }//endif
        }//endif
      }//endif
    }//endif

    FormErrorList[FormErrorList.length] =  '\n' + Hulp + ResultMsg
  }

  var FormErrorList = new Array()
  function checkElement(Elm, ShowErrors) {
    if (!(Elm)) { return true }
    if ((! Elm.value) || (Elm.value=='')) {
      if (ShowErrors) {
        return true
      }else{
        if (null!=Elm.getAttribute("required")) {
          DoCheckElmErr(ErrMsgRequiredEmpty, new Array(), ShowErrors, Elm)
          return false
        }else{
          return true
        }//endif
      }//endif
    }//endif
    if (Elm.dataformat) {var Format = Elm.dataformat} else {var Format = ''}
    if (Elm.dataError)  {var ErrMsg = Elm.dataError}  else {var ErrMsg = ''}
    if (Elm.datatype) {
      switch (Elm.datatype) {
        case "date"    :
          if (Format == '') {Format = dateFormat}
          if (ErrMsg == '') (ErrMsg = ErrMsgInvalidDate)
          var TheDate = StringToDate(Elm.value, Format)
          if (TheDate == EmptyDate) {
            ResultMsg = DoCheckElmErr(ErrMsg, new Array(Format), ShowErrors, Elm)
            Elm.focus()
            return false
          } else {
            Elm.value = DateToString(TheDate, Format)
            if ((null!=Elm.getAttribute("maxvalue")) && (TheDate > StringToDate(Elm.maxvalue, Format))) { DoCheckElmErr(ErrMsgDateToHigh, new Array(Elm.maxvalue), ShowErrors, Elm); return false }
            if ((null!=Elm.getAttribute("minvalue")) && (TheDate < StringToDate(Elm.minvalue, Format))) { DoCheckElmErr(ErrMsgDateToLow,  new Array(Elm.minvalue), ShowErrors, Elm); return false }
            return true
          }
          break
        case "string" :
          return true
          break
        case "spinner" :
          if (ErrMsg == '') (ErrMsg = 'Error invalid number')
          var Value = 1 * StringToInt(Elm.value)
          if (isNaN(Value)) {
            DoCheckElmErr(ErrMsg, new Array(), ShowErrors, Elm)
            return false
          }
          Elm.value = IntToString(Value)
          if ((null!=Elm.getAttribute("maxvalue")) && (Value > StringToInt(Elm.maxvalue, Format))) { DoCheckElmErr(ErrMsgNumberToHigh, new Array(Elm.maxvalue), ShowErrors, Elm); return false }
          if ((null!=Elm.getAttribute("minvalue")) && (Value < StringToInt(Elm.minvalue, Format))) { DoCheckElmErr(ErrMsgNumberToLow,  new Array(Elm.minvalue), ShowErrors, Elm); return false }
          return true
          break
        case "time" :
          if (ErrMsg == '') (ErrMsg = 'Error invalid time')
          var Value = 1 * TimeStringToInt(Elm.value, Format)
          if (isNaN(Value)) {
            DoCheckElmErr(ErrMsg, new Array(), ShowErrors, Elm)
            return false
          }
          Elm.value = IntToTimeString(0 + Value, Format)
          if ((null!=Elm.getAttribute("maxvalue")) && (Value > TimeStringToInt(Elm.maxvalue, Format))) { DoCheckElmErr(ErrMsgTimeToHigh, new Array(Elm.maxvalue), ShowErrors, Elm); return false }
          if ((null!=Elm.getAttribute("minvalue")) && (Value < TimeStringToInt(Elm.minvalue, Format))) { DoCheckElmErr(ErrMsgTimeToLow,  new Array(Elm.minvalue), ShowErrors, Elm); return false }
          return true
          break
        case "integer" :
          if (ErrMsg == '') (ErrMsg = 'Error invalid number')
          var Value = 1 * StringToInt(Elm.value)
          if (isNaN(Value)) {
            DoCheckElmErr(ErrMsg, new Array(), ShowErrors, Elm)
            return false
          }
          Elm.value = IntToString(Value)
          if ((null!=Elm.getAttribute("maxvalue")) && (Value > StringToInt(Elm.maxvalue, Format))) { DoCheckElmErr(ErrMsgNumberToHigh, new Array(Elm.maxvalue), ShowErrors, Elm); return false }
          if ((null!=Elm.getAttribute("minvalue")) && (Value < StringToInt(Elm.minvalue, Format))) { DoCheckElmErr(ErrMsgNumberToLow,  new Array(Elm.minvalue), ShowErrors, Elm); return false }
          return true
          break
        case "real" :
          if (ErrMsg == '') {ErrMsg = 'Error invalid number'}
          var Value = StringToFloat(Elm.value, Format)
          if (isNaN(Value)) {
            DoCheckElmErr(ErrMsg, new Array(), ShowErrors, Elm)
            return false
          }
          Elm.value = FloatToString(Value, Format)
          if ((null!=Elm.getAttribute("maxvalue")) && (Value > StringToFloat(Elm.maxvalue, Format))) { DoCheckElmErr(ErrMsgNumberToHigh, new Array(Elm.maxvalue), ShowErrors, Elm); return false }
          if ((null!=Elm.getAttribute("minvalue")) && (Value < StringToFloat(Elm.minvalue, Format))) { DoCheckElmErr(ErrMsgNumberToLow,  new Array(Elm.minvalue), ShowErrors, Elm); return false }
          return true
          break
        case "zipcode" :
          break
        default        :
          break
      }//endcase
    }//endif Exists(datatype)
    return true
  }//endfunction checkElement()


  function checkFormat(e) {
    if (typeof e == "undefined" || !e) e = event; 
		var Elm = e.srcElement;
    return checkElement(Elm, true);
  }//endfunction checkFormat()


  function checkForm() {
    FormErrorList = new Array() // Empty all errors
    var Result = true
    var I      = 0
    while (I < document.forms.length) {
      var J    = 0
      while (J < document.forms[I].length) {  // Nr of elements on the form
          if (! (checkElement(document.forms[I].elements[J], false))) {
            Result = false
          }//endif
        J++
      }//enddo
      I++
    }//enddo
    if (! Result) {
      alert(ErrMsgFormFieldsErr + FormErrorList)
    }//endif
    return Result
  }//endfunction checkForm()



  function checkNum(e, Extra) {
    if (typeof e == "undefined" || !e) e = event;
    if (e.keyCode > 30) {  // Check only non-control characters
      if ((e.keyCode < 48) || (e.keyCode > 57)) {
        if (!((Extra) && (Extra.indexOf(String.fromCharCode(e.keyCode))>= 0))) {
          if        ((e.keyCode == 44) && (Extra.indexOf(',') < 0) && (Extra.indexOf('.') >= 0)) {
            e.keyCode = 46
          } else if ((e.keyCode == 46) && (Extra.indexOf('.') < 0) && (Extra.indexOf(',') >= 0)) {
            e.keyCode = 44
          } else {
            e.returnValue = false;
          }//endif
        }//endif
      }//endif
    }//endif
  }//endfunction checkNum()




  function CalcWindowParams(Evt, Height, Width, TranslateY, TranslateX) {
    var Xpos = 10
    var Ypos = 10
    if (! TranslateY) { TranslateY = 0 }
    if (! TranslateX) { TranslateX = 0 }

    if (Evt) {
      screenPosX = Evt.screenX - Evt.clientX
      screenPosY = Evt.screenY - Evt.clientY

      var ElmX = Evt.clientX - Evt.offsetX
      var ElmY = Evt.clientY - Evt.offsetY
      Xpos = screenPosX + ElmX + TranslateX
      Ypos = screenPosY + ElmY + TranslateY
      if ((Ypos+Height+75) > screen.height) {
        Ypos = screen.height - Height - 75
      }
      if ((Xpos+Width) > screen.width) {
        Xpos = screen.width - Width - 5
      }
    }else{
      alert('No event found')
    }

    var Params = "channelmode=0,dependent=1,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0,titlebar=0"
        Params = Params + ",height="+Height+",width="+Width
        Params = Params + ",screenX="+Xpos+",left="+Xpos
        Params = Params + ",screenY="+Ypos+",top="+Ypos
    return Params
  }

  var CurrentInputObj    = null;
  function FindDate(Evt, Elm) {
    CurrentInputObj = Elm // eval('document.all.'+Elm)
    var Params = CalcWindowParams(Evt, 215, 245, 22, -245)
    var PriorityMenu = open("/Datepicker.htm", "DateSubWin", Params);
  }//function FindDate()


  function SpinnerInc(ObjName) {
    var Elm = getObject(ObjName);
    var NrOfQ = Elm.value * 1;
    if (Elm.step != null) {
      var Step  = Elm.step  * 1;
    }else{
      var Step  = 1;
    }//endif

    if (null!=Elm.getAttribute("maxvalue")) {
      var MaxVal = Elm.maxvalue * 1;
    }else{
      var MaxVal = Number.MAX_VALUE;
    }//endif

    Elm.value = NrOfQ + Step;
    if (Elm.value > MaxVal) {
      Elm.value = MaxVal;
    }//endif
    if (Elm.onchange) 
      Elm.onchange();
  }//endfunction SpinnerInc()


  function SpinnerDec(ObjName) {
    var Elm = getObject(ObjName);
    var NrOfQ = Elm.value * 1;
    if (Elm.step != null) {
      var Step  = Elm.step  * 1;
    }else{
      var Step  = 1;
    }//endif
    if (null!=Elm.getAttribute("minvalue")) {
      var MinVal = Elm.minvalue  * 1;
    }else{
      var MinVal = 0
    }//endif

    Elm.value = NrOfQ - Step;
    if (Elm.value < MinVal) {
      Elm.value = MinVal;
    }//endif
    if (Elm.onchange)
      Elm.onchange();
  }//endfunction SpinnerDec()









////////////////////////////////////////////////////////////////////////////////
/////////////             data conversion functions            /////////////////
////////////////////////////////////////////////////////////////////////////////




  function xmlDateTimeToStr(aDate) {
	var temptext = ""+aDate.getFullYear();
    if (aDate.getMonth() < 9)
      temptext += "0";
    temptext += (aDate.getMonth()+1);
	if (aDate.getDate() < 10)
      temptext += "0";
    temptext += aDate.getDate();
    temptext += 'T';
    if (aDate.getHours() < 10)
      temptext += "0";
    temptext += aDate.getHours();
    temptext += ":";
    if (aDate.getMinutes() < 10)
      temptext += "0";
    temptext += aDate.getMinutes();
    temptext += ":";
    if (aDate.getSeconds() < 10)
    temptext += "0";
    temptext += aDate.getSeconds();

    //the milliseconds part
    temptext += ".";
    var msecs = aDate.getMilliseconds();  //getTime() - (Math.floor(aDate.getTime() / 1000) * 1000);
    if (msecs < 100)
      temptext += "0";
    if (msecs < 10)
      temptext += "0";
    temptext += msecs;
    return (temptext);
  }//end function xmlDateTimeToStr(aDate)


  
  function xmlStrToDateTime(aDateStr) {
    var result = new Date(0);
    var yy = 0;
    var mm = 0;
    var dd = 0;
    var hh = 0;
    var mi = 0;
    var ss = 0;
    aDateStr = aDateStr.toUpperCase();
    if (aDateStr.indexOf('T') == 0) {
      hh = (aDateStr.substr( 1, 2) * 1);
      mi = (aDateStr.substr( 4, 2) * 1);
      ss = (aDateStr.substr( 7, 2) * 1);
    } else if (aDateStr.indexOf('T') > 0) {
      yy = (aDateStr.substr( 0, 4) * 1);
      mm = (aDateStr.substr( 4, 2) * 1);
      dd = (aDateStr.substr( 6, 2) * 1);
      hh = (aDateStr.substr( 9, 2) * 1);
      mi = (aDateStr.substr(12, 2) * 1);
      ss = (aDateStr.substr(15, 2) * 1);
    } else {
      yy = (aDateStr.substr( 0, 4) * 1);
      mm = (aDateStr.substr( 4, 2) * 1);
      dd = (aDateStr.substr( 6, 2) * 1);
    }//endif
    if ((aDateStr.indexOf('GMT') >= 0) || (aDateStr.indexOf('UTC') >= 0)) {
      result.setUTCFullYear(yy);
      result.setUTCMonth(mm-1);
      result.setUTCDate(dd);
      result.setUTCHours(hh);
      result.setUTCMinutes(mi);
      result.setUTCSeconds(ss);
    } else {
      result = new Date(yy, mm-1, dd, hh, mi, ss);
    }//endif
    return (result);
  }//end function xmlStrToDateTime(aDateStr)


  function isCorrectDateStr(aDate, aFormat) {
    var newDate    = StringToDate(aDate, aFormat);
    return (! ( (newDate.getTime() < 0) && (newDate.getTime() > -10)))
  }//end


  function isCorrectTimeStr(aTime, aFormat, aType) {
    var newTime    = stringToTime(aTime, aFormat, aType);
    return (! ( (newTime.getTime() < 0) && (newTime.getTime() > -10)))
  }//endfunction isCorrectTimeStr


  function isCorrectDateTimeStr(aDateTime, aDateFormat, aTimeFormat, aType) {
    var newDate    = strToDateTime(aDateTime, aDateFormat, aTimeFormat, aType);
    return (! ( (newDate.getTime() < 0) && (newDate.getTime() > -10)))
  }//endfunction isCorrectDateTimeStr()
  
  function isBeforeNow(aDateTime, aDateFormat, aTimeFormat, aType) {
    var workDate; 
    if (typeof aDateTime == "string") {
      workDate = strToDateTime(aDateTime, aDateFormat, aTimeFormat, aType);
    } else {
      workDate = aDateTime;
    }//endif
    return ( workDate.getTime() < ((new Date()).getTime()));
  }//endfunction isBeforeNow()


  function StringToDate(StrDate, Format, toLocalTime) {
    if (Format == null) { Format = dateFormat }
    if (Format == '')   { Format = dateFormat }
    if (toLocalTime == null) { toLocalTime = false; }

    var dateSep = dateSeparator
    var dateFormatArr = Format.split(dateSep)

    if (dateFormatArr.length != 3) {
      var Found = false
      var I = 0
      while (I < Format.length) {
        if ((!Found) && ("ymdYMD".indexOf(Format.substr(I, 1)) == -1)) {
          dateSep = Format.substr(I, 1)
          Found   = true
        }
        I++
      }
      if (Found) {
        dateFormatArr = Format.split(dateSep)
        if (dateFormatArr.length != 3) {
          return  invalidDateFormat;
        }
      } else {
        return  invalidDateFormat;
      }
    }
    var DateArr       = StrDate.split(dateSep)
    if (DateArr.length != 3) {
      return ( invalidDate );
    }//endif
    var I = 0
    var isYYYY = false;
    var isMM   = false;
    var isDD   = false;
    while (I < dateFormatArr.length) {
      if (dateFormatArr[I].substr(0,1).toLowerCase() == 'y') {
        if (isYYYY) { alert('Illegal date format "'+Format+'" contains yyyy twice'); return (invalidDateFormat) }
        isYYYY = true;
        var YYYY = 1 * DateArr[I];
      } else {
        if (dateFormatArr[I].substr(0,1).toLowerCase() == 'm') {
          if (isMM) { alert('Illegal date format "'+Format+'" contains mm twice'); return (invalidDateFormat) }
          isMM = true;
          var MM = 0;
              MM = 1 * DateArr[I];
        } else {
          if (dateFormatArr[I].substr(0,1).toLowerCase() == 'd') {
            if (isDD) { alert('Illegal date format "'+Format+'" contains dd twice'); return (invalidDateFormat) }
            isDD = true;
            var DD = 0; 
              DD = 1 * DateArr[I];
          } else {
            alert('Illegal date format "'+Format+'" contains a not y m or d char')
            return  (invalidDateFormat);
          }
        }
      }
      I++
    }
    if ((isYYYY) && (isMM) && (isDD)) {
      if (YYYY < 100) {
        if (YYYY <= 50) {
          YYYY += 2000
        } else {
          YYYY += 1900
        }
      } // if
      var d = new Date(YYYY, MM-1, DD, 0, 0, 0);
      
      if ((d.getDate()==DD) && (d.getMonth()==(MM-1)) && ((d.getFullYear()==YYYY) || (d.getYear()==YYYY))) {
        if (toLocalTime) {
          d.setTime(d.getTime() - (d.getTimezoneOffset()*60*1000));
        }//endif
        return d
      } else {
        return (invalidDate);
      }//endif
    } else {
      alert('Illegal date format "'+Format+'" missing a "y", "m" or "d"')
      return invalidDateFormat;
    }//endif
  }



  function DateToString(DateStr, Format) {
    if (Format == null) { Format = dateFormat }
    if (Format == '')   { Format = dateFormat }

    var dateSep = dateSeparator
    var dateFormatArr = Format.split(dateSep)
    if (dateFormatArr.length != 3) {
      var Found = false
      var I = 0
      while (I < Format.length) {
        if ((!Found) && ("ymdYMD".indexOf(Format.substr(I, 1)) == -1)) {
          dateSep = Format.substr(I, 1)
          Found   = true
        }//endif
        I++
      }//enddo
      if (Found) {
        dateFormatArr = Format.split(dateSep)
        if (dateFormatArr.length != 3) {
          return ''
        }//endif
      } else {
        return ''
      }//endif
    }//endif
    var Result = ''
    var I = 0
    while (I < dateFormatArr.length) {
      if (I > 0) { Result += dateSep }
      if (dateFormatArr[I].substr(0,1).toLowerCase() == 'y') {
        if (dateFormatArr[I] == 'yyyy') {
          if (DateStr.getFullYear() < 1000) {
            Result += '0'+DateStr.getFullYear()
          } else {
            Result += DateStr.getFullYear()
          }//endif full year less then thousand
        } else {
          if (dateFormatArr[I] == 'yy') {
            if (DateStr.getYear() < 10) {
              Result += '0'+DateStr.getYear()
            } else {
              Result += DateStr.getYear()
            }//endif year less then ten
          } else {
            alert('Illegal date format "'+Format+'" Year must be "yy" or "yyyy"')
            return ''
          }//endif two digit year
        }//endif four digit year
      } else {
        if (dateFormatArr[I].substr(0,1).toLowerCase() == 'm') {
          var MM = DateStr.getMonth() + 1
          if (dateFormatArr[I] == 'mm') {
            if (MM < 10) {
              Result += '0' + MM
            } else {
              Result += MM
            }//endif month less then 10
          } else {
            if (dateFormatArr[I] == 'm') {
              Result += MM
            } else {
              alert('Illegal date format "'+Format+'" Month must be "m" or "mm"')
              return ''
            }//endif month without zero
          }//endif month with zero
        } else {
          if (dateFormatArr[I].substr(0,1).toLowerCase() == 'd') {
            if (dateFormatArr[I] == 'dd') {
              if (DateStr.getDate() < 10) {
                Result += '0' + DateStr.getDate()
              } else {
                Result += DateStr.getDate()
              }//endif day less then ten
            } else {
              if (dateFormatArr[I] == 'd') {
                Result += DateStr.getDate()
              } else {
                alert('Illegal date format "'+Format+'" Month must be "d" or "dd"')
                return ''
              }//endif day without zero
            }//endif day with zero
          } else {
            alert('Illegal date format "'+Format+'" contains a not y m or d char')
           return ''
          }//endif day
        }//endif month
      }//endif year
      I++
    }//enddo
    return Result
  }//endfunction DateToString()
  
  
  
  function dateAndTimeToStr(aDate, aTime, aDateFormat, aTimeFormat, aType) {
    var newDate = StringToDate(aDate, aDateFormat, true);
    var newTime = stringToTime(aTime, aTimeFormat, aType);
    if ((newDate.getTime() < 0) && (newDate.getTime() > -10)) 
      return (invalidTime);
    if ((newTime.getTime() < 0) && (newTime.getTime() > -10))
      return (invalidDate);
    return (new Date(newDate.getTime() + newTime.getTime()) );
  }//endfunction dateAndTimeToStr
  
  
  
  function dateTimeToStr(aDateTime, aDateFormat, aTimeFormat, aType) {
    return (DateToString(aDateTime, aDateFormat) + ' ' + timeToStr(aDateTime, aTimeFormat, aType) );
  }//endfunction dateAndTimeToStr
  
  
  function isDST(checkDate) {
    var date1 = new Date(checkDate.getFullYear(), 0, 1);  // Januari first
    var date2 = new Date(checkDate.getFullYear(), 6, 1);  // July first
    var offset1 = date1.getTimezoneOffset();
    var offset2 = date2.getTimezoneOffset();
    var checkOffset = checkDate.getTimezoneOffset();
    if (offset1 == offset2) {
      return (false); 
    } else if (offset1 > offset2) {    // Northern hemisphere DST
      return (offset2 == checkOffset);
    } else {                           // Southern hemisphere DST
      return (offset1 == checkOffset);
    }//endif
  }
  
  
  function strToDateTime(aDateTime, aDateFormat, aTimeFormat, aType) {
    var dtArr = aDateTime.split(' ');
    if (dtArr.length >= 2) {
      var newDate = StringToDate(dtArr[0], aDateFormat, false);
      var newTime = stringToTime(dtArr[1], aTimeFormat, aType);
      if ((typeof newDate != "object") || ((newDate.getTime() < 0) && (newDate.getTime() > -10))) 
        return (invalidDate);
      if ((typeof newTime != "object") || ((newTime.getTime() < 0) && (newTime.getTime() > -10)))
        return (invalidTime);
      
      var newDateTime = new Date(newTime.getTime());
      newDateTime.setFullYear(newDate.getFullYear());
      newDateTime.setMonth(newDate.getMonth());
      newDateTime.setDate(newDate.getDate())

      return (newDateTime);
    } else if (dtArr.length = 1) {
      if ( (aDateTime.indexOf('/') >= 0) || (aDateTime.indexOf('-') >= 0) || (aDateTime.indexOf('.') >= 0) || (aDateTime.indexOf(':') < 0) || (aDateTime.length > 8) ){
        var newDate = (StringToDate(dtArr[0], aDateFormat, true));
        var newTime = stringToTime("00:00:00", aTimeFormat, aType);
        newTime.setTime( newTime.getTime() - ( (newDate.getTimezoneOffset() - newTime.getTimezoneOffset()) *60000) );
        if ((typeof newDate != "object") || ((newDate.getTime() < 0) && (newDate.getTime() > -10))) 
  	  return (invalidDate);

        return (new Date(newDate.getTime() + newTime.getTime()) );
      } else {
        return (stringToTime(dtArr[1], aTimeFormat, aType));
      }//endif
    }//endif
  }//endfunction strToDateTime
  
  
  function timeToStr(aTime, aFormat, aType) {
    if (aFormat == null) { aFormat = timeFormat }
    if (aFormat == '')   { aFormat = timeFormat }
    if ((aType   == null) || (aType == isUnknownClock)) { 
      if (aFormat.indexOf('H') >= 0)
        aType   = is24Clock; 
      else
        aType   = is12Clock;
      }
    
    var timeSep = timeSeparator
    var timeFormatArr = aFormat.split(timeSep)
    if (timeFormatArr.length < 2) {
      var Found = false
      var I = 0
      while (I < aFormat.length) {
        if ((!Found) && ("hHnNsS".indexOf(aFormat.substr(I, 1)) == -1)) {
          timeSep = aFormat.substr(I, 2)
          Found   = true
        }//endif
        I++
      }//enddo
      if (Found) {
        timeFormatArr = aFormat.split(timeSep)
        if (timeFormatArr.length < 2) {
          return ''
        }//endif
      } else {
        return ''
      }//endif
    }//endif
    var Result    = ''
    var endString = '';
    var I = 0
    while (I < timeFormatArr.length) {
      if (I > 0) { Result += timeSep }
      if (timeFormatArr[I].substr(0,1).toLowerCase() == 'h') {
        var TempHours = aTime.getHours();
        if ((aType == is12Clock) || (aType == is12ClockAM) || (aType == is12ClockPM)) {
          if (TempHours >= 12) {
            endString = ' pm';
            if (TempHours > 12) 
              TempHours = TempHours - 12;
          } else {
            endString = ' am';
            if (TempHours == 0) 
              TempHours = 12;
          }//endif
        }//endif
        if ((timeFormatArr[I].toLowerCase() == 'hh') && (TempHours < 10)) {
          Result += '0'+ TempHours;
        } else {
          Result += ''+TempHours;
        }//endif hours
      } else if (timeFormatArr[I].substr(0,1).toLowerCase() == 'n') {
        var TempMin = aTime.getMinutes();
        if ((timeFormatArr[I].toLowerCase() == 'nn') && (TempMin < 10)) {
          Result += '0'+ TempMin;
        } else {
          Result += ''+TempMin;
        }//endif minutes
      } else if (timeFormatArr[I].substr(0,1).toLowerCase() == 's') {
        var TempSec = aTime.getSeconds();
        if ((timeFormatArr[I].toLowerCase() == 'ss') && (TempSec < 10)) {
          Result += '0'+ TempSec;
        } else {
          Result += ''+TempSec;
        }//endif minutes
      } else {
        alert('Illegal time format "'+aFormat+'" contains a not h n or s char')
        return ''
      }//endif
      I++
    }//enddo
    Result += endString;
    return Result
  }

  function getPosOfName(aStr, aList, aSep) {
    if (aSep == null) aSep = ',';
    var aSearch = aList.split(aSep);
    var result  = -1;
    var i	= 0;
    while (i < aSearch.length) {
      result = aStr.indexOf( aSearch[i] );
      if (result >= 0) 
        return ( result );
      i++;
    }//endwhile
    return ( result );
  }//endfunction getPosOfName

  function stringToTime(aTime, aFormat, aType, aBreak) {
    if (aFormat == null) { aFormat = timeFormat }
    if (aFormat == '')   { aFormat = timeFormat }
    if (aType == null) aType = isUnknownClock;
    if ((aType == is12Clock) || (aType == isUnknownClock)) {
      var work = aTime.toLowerCase();
      var ofsA = getPosOfName(work, 'am,a.m.,vm,v.m.', ',');
      var ofsP = getPosOfName(work, 'pm,p.m.,nm,n.m.', ',');
      if ( ofsA > 0 ) {
        aType = is12ClockAM;
        aTime = aTime.substr(0, ofsA);
      } else if ( ofsP > 0 ) {
        aType = is12ClockPM;
        aTime = aTime.substr(0, ofsP);
      } else if ( aFormat.indexOf('H') >= 0 ) {
        aType = is24Clock;
      } else {
        work = work.substr(0, 2);
        if (! ('0123456789'.indexOf(work.substr(0, 1)>=0)))
          return ( invalidTime );
        if ((work.length == 2) && (! ('0123456789'.indexOf(work.substr(1, 1)>=0)))) 
          work = work.substr(0,1);
        var num = 1 * work;
//        if (aBreak == null) aBreak = 9;
        if (aType == is12Clock) {
          if (aBreak != null) {
            if (num < aBreak) {
              aType = is12ClockPM;
            } else {
              aType = is12ClockAM;
            }//endif
          } else {
            return (invalidDateFormat);
          }//endif
        } else {
          if ((num == 0) || (num > 12)) {
            aType = is24Clock;
          } else {
            if (num < aBreak) {
              aType = is12ClockPM;
            } else {
              aType = is12ClockAM;
            }//endif
          }//endif
        }//endif
      }//endif
    }//endif

    if ((aType == is24Clock) || (aType == is12ClockAM) || (aType == is12ClockPM)) {
      return ( strToTime(aTime, aFormat, aType) );
    } else {
      alert('Could not determine date format');
      return (invalidDateFormat);
    }//endif
  }//endfunction stringToTime


  function strToTime(aStrTime, aFormat, aType) {
    if (aFormat == null) { aFormat = timeFormat }
    if (aFormat == '')   { aFormat = timeFormat }
    if ((aType   == null) || (aType == isUnknownClock)) { if (aFormat.indexOf('H') >= 0) aType   = is24Clock; else aType   = is12Clock;}
    
    var StrTime = aStrTime;
    if ((StrTime == null) || (StrTime == ""))
      StrTime = "0"
      
    var ofsA = getPosOfName(aStrTime, 'AM,am,a.m.,vm,v.m.', ',');
    var ofsP = getPosOfName(aStrTime, 'PM,pm,p.m.,nm,n.m.', ',');
    if ( ofsA > 0 ) {
      aType = is12ClockAM;
      StrTime = StrTime.substr(0, ofsA);
    } else if ( ofsP > 0 ) {
      aType = is12ClockPM;
      StrTime = StrTime.substr(0, ofsP);
    }//endif
    
    var timeSep = timeSeparator
    var timeFormatArr = aFormat.split(timeSep)
    if (timeFormatArr.length < 2) {
      var Found = false
      var I = 0
      while (I < aFormat.length) {
        if ((!Found) && ("hHmMsS".indexOf(aFormat.substr(I, 1)) == -1)) {
          timeSep = aFormat.substr(I, 1)
          Found   = true
        }
        I++
      }
      if (Found) {
        timeFormatArr = aFormat.split(timeSep)
        if (timeFormatArr.length < 2) {
          return (invalidTimeFormat);
        }
      } else {
        return (invalidTimeFormat);
      }
    }
    var TimeArr       = StrTime.split(timeSep)
    var I = 0
    var isHH = false
    var isNN = false
    var isSS = false
    var HH = 0;
    var NN = 0;
    var SS = 0;
    while (I < timeFormatArr.length) {
      if (timeFormatArr[I].substr(0,1).toLowerCase() == 'h') {
        if (isHH) { alert('Illegal time format "'+Format+'" contains hh twice'); return (invalidTimeFormat) }
        isHH = true
        if (I <TimeArr.length)
            HH = 1 * TimeArr[I]
      } else if (timeFormatArr[I].substr(0,1) == 'n') {
        if (isNN) { alert('Illegal time format "'+Format+'" contains nn twice'); return (invalidTimeFormat) }
        isNN = true
        if (I <TimeArr.length)
          NN = 1 * TimeArr[I]
      } else if (timeFormatArr[I].substr(0,1) == 's') {
        if (isSS) { alert('Illegal time format "'+Format+'" contains ss twice'); return (invalidTimeFormat) }
        isSS = true
        if (I < TimeArr.length)
          SS = 1 * TimeArr[I];
      } else {
          alert('Illegal time format "'+aFormat+'" contains another character then h n or s')
          return (invalidTimeFormat);
      }//endif
      I++
    }
    var result = invalidTime;
    if ((!isHH) && (!isNN) && (!isSS)) {
      return (invalidTimeFormat);
    }//endif
    if (aType == is24Clock)     {
      result = new Date(1970,0,1, HH, NN, SS);
    } else if ((aType == is12ClockAM) || (aType == is12ClockPM)) {
      if ((aType == is12ClockPM) && (HH < 12)) {
        HH = HH +12;
      } else if ((aType == is12ClockPM) && (HH==12)) {
        HH = 0;
      }//endif
      result = new Date(1970,0,1, HH, NN, SS);
    } else {
      return ( result );
    }//endif
    if ((HH == result.getHours()) && (NN == result.getMinutes()) && (SS == result.getSeconds()))
      return result;
    else
      return (invalidTime);
  }


  function TimeStringToInt(aStrTime, Format) {
    if (Format == null) { Format = timeFormat }
    if (Format == '')   { Format = timeFormat }
    
    var StrTime = aStrTime;
    if ((StrTime == null) || (StrTime == ""))
      StrTime = "0"
      
    var timeSep = timeSeparator
    var timeFormatArr = Format.split(timeSep)
    if (timeFormatArr.length < 2) {
      var Found = false
      var I = 0
      while (I < Format.length) {
        if ((!Found) && ("hHmMsS".indexOf(Format.substr(I, 1)) == -1)) {
          timeSep = Format.substr(I, 1)
          Found   = true
        }
        I++
      }
      if (Found) {
        timeFormatArr = Format.split(timeSep)
        if (timeFormatArr.length < 2) {
          return 0
        }
      } else {
        return 0
      }
    }
    var TimeArr       = StrTime.split(timeSep)
//    if (TimeArr.length < 2) {
//      return 0
//    }//endif
    var I = 0
    var isHH = false
    var isNN = false
    var isSS = false
    while (I < timeFormatArr.length) {
      if ((timeFormatArr[I].substr(0,1)).toUpperCase() == 'H') {
        if (isHH) { alert('Illegal time format "'+Format+'" contains hh twice'); return 0 }
        isHH = true
        var HH = 0
        //check for example h:nn => only minutes are typed
        if ((TimeArr.length >= 2) || (timeFormatArr.length < 2)) {
          HH = 1 * TimeArr[I] //normal situation
        }
      } else {
        if (timeFormatArr[I].substr(0,1) == 'n') {
          if (isNN) { alert('Illegal time format "'+Format+'" contains nn twice'); return 0 }
          isNN = true
          var NN = 0
          //check for example n:ss => only seconds are typed
          if ((TimeArr.length >= 2) || (timeFormatArr.length < 2)) {
            NN = 1 * TimeArr[I] //normal situation
          }
          else {
            if (isHH) {
              //the typed number are the number of minutes, not hours
              //those minutes are in the TimeArr[0]
              NN = 1 * TimeArr[0]
            }
          }
        } else {
          if (timeFormatArr[I].substr(0,1) == 's') {
            if (isSS) { alert('Illegal time format "'+Format+'" contains ss twice'); return 0 }
            isSS = true
            var SS = 0
              SS = 1 * TimeArr[I]
          } else {
            alert('Illegal time format "'+Format+'" contains a not h n or s char')
            return 0
          }
        }
      }
      I++
    }
    var d = 0;
    if (isHH) {
      d += 3600 * HH
    }
    if (isNN) {
      d += 60 * NN
    }
    if (isSS) {
      d += SS
    }
    if ((!isHH) && (!isNN) && (!isSS)) {
      alert('Illegal time format "'+Format+'" does not contain any "h", "n" or "s"')
    } 
    return d;
  } //function TimeStringToInt



  function IntToTimeString(TimeInt, Format) {
    if (Format == null) { Format = timeFormat }
    if (Format == '')   { Format = timeFormat }
    var timeSep = timeSeparator
    var timeFormatArr = Format.split(timeSep)
    if (timeFormatArr.length < 2) {
      var Found = false
      var I = 0
      while (I < Format.length) {
        if ((!Found) && ("hHnNsS".indexOf(Format.substr(I, 1)) == -1)) {
          timeSep = Format.substr(I, 2)
          Found   = true
        }//endif
        I++
      }//enddo
      if (Found) {
        timeeFormatArr = Format.split(timeSep)
        if (timeFormatArr.length < 2) {
          return ''
        }//endif
      } else {
        return ''
      }//endif
    }//endif
    var Result = ''
    var I = 0
    while (I < timeFormatArr.length) {
      if (I > 0) { Result += timeSep }
      if ((timeFormatArr[I].substr(0,1)).toUpperCase() == 'H') {
        var TempHours = parseInt(TimeInt / 3600)
        if (timeFormatArr[I] == 'hh') {
          if (TempHours < 10) {
            Result += '0'+ (TimeInt > 0 ? TempHours : 0)
          } else {
            Result += (TimeInt > 0 ? TempHours : 0)
          }//endif less than 10 hours
        } else {
          if (timeFormatArr[I].toUpperCase() == 'H') {
              Result += (TimeInt > 0 ? TempHours : 0)
          }
        }//endif hours
      } else {
        if (timeFormatArr[I].substr(0,1) == 'n') {
          if (timeFormatArr[I] == 'nn') {
            if ((TimeInt % 3600) < 10 * 60) {
              Result += '0'+ Math.floor(((TimeInt % 3600) > 0 ? (TimeInt % 3600) / 60 : 0));
            } else {
              Result += Math.floor(((TimeInt % 3600) > 0 ? (TimeInt % 3600) / 60 : 0));
            }//endif less than 10 minutes
          } else {
            if (timeFormatArr[I] == 'n') {
                Result += Math.floor(((TimeInt % 3600) > 0 ? (TimeInt % 3600) / 60 : 0));
            }
          }//endif minutes
        } else {
          if (timeFormatArr[I].substr(0,1) == 's') {
            if (timeFormatArr[I] == 'ss') {
              if ((TimeInt % 60) < 10) {
                Result += '0'+ (TimeInt % 60)
              } else {
                Result += (TimeInt % 60)
              }//endif less than 10 hours
            } else {
              if (timeFormatArr[I] == 's') {
                  Result += (TimeInt % 60)
              }
            }//endif seconds
          } else {
            alert('Illegal time format "'+Format+'" contains a not h n or s char')
           return ''
          }//endif seconds
        }//endif hours
      }//endif minutes
      I++
    }//enddo
    return Result
  }//endfunction IntToTimeString()


  function StringToInt(Int, Format) {
    var I    = 0
    var Hulp = ''
    while (I < Int.length) {
      if ("-0123456789".indexOf( Int.substr(I, 1) ) != -1) {
        Hulp += Int.substr(I,1)
      }//endif
      I++
    }//enddo
    var Result = 1 * Hulp
    return Result
  }//endfunction StringToInt()


  function IntToString(Int, Format) {
    var ThousandSep = DigitGroupingSymbol
    var ThousandGrp = NrOfDigitsInGroup
    var UseThousand = false
    var Result      = ""+Int
    if (Format == null) { Format = '' }  // if not exists interpret as empty

    if (Format.indexOf('thousandsep=') >= 0) {
      UseThousand = true
      ThousandSep = Format.substr(Format.indexOf('thousandsep=')+12, 1)
    }//endif
    if (Format.indexOf('thousandgrp=') >= 0) {
      UseThousand = true
      var Hulp = Format.substr(Format.indexOf('thousandgrp=')+12)
      var P    = 0
      while ((P<Hulp.length) && ("0123456789".indexOf(Hulp.substr(P,1)) >= 0)) {
        P++
      }//enddo
      Hulp = Hulp.substr(0, P)
      ThousandGrp = 1 * Hulp
    }//endif

    if ((ThousandSep=='') || (ThousandGrp<=0)) {
      UseThousand = false
    }//endif

    if (UseThousand) {
      var I = Result.length
      I -= ThousandGrp
      while (I > 0) {
        var Hulp = Result.substr(I)
        Result = Result.substr(0, I) + ThousandSep + Hulp
        I -= ThousandGrp
      }//enddo
    }//endif

    return Result
  }//endfunction IntToString()



  function StringToFloat(Str, Format, NeverThousand) {
    if (typeof Str == "number")
      return Str;
    var DecSep = numberDecimalSymbol
    var deStr = "";
    deStr = "" + Str;
    if ((deStr == null) || (deStr == ""))
      deStr = "0";
    var hetFormat = "";
    hetFormat = Format;
    if ((hetFormat == null) || (hetFormat == ""))
      hetFormat = "decsep=" + numberDecimalSymbol + 
                  " thousandsep=" + DigitGroupingSymbol +
                  " thousandgrp=" + NrOfDigitsInGroup +
                  " dec=" + CurrencyDigits;
    if (hetFormat.indexOf('decsep=') >= 0) {
      DecSep = hetFormat.substr(hetFormat.indexOf('decsep=')+7, 1)
    }//endif

    var P = deStr.indexOf(DecSep);
    var Nr  = 0;
    if (P >= 0) {
      Nr  = StringToInt(deStr.substr(0,P), hetFormat)
    } else {
      Sep = '.';
      P = deStr.indexOf(Sep);
      if (P < 0) {
        Sep = ',';
        P = deStr.indexOf(Sep);
      }//endif
      if (P >= 0) {
        if (deStr.split(Sep).length > 2) {  // if more then one separator probably not a floating point
          P = -1;
        } else {
          if ((! NeverThousand) && (P == (deStr.length - 4)))
            P = -1;  //if is exactly 3 digits after sep. (12,345) is probably not a floating point
        }//endif        
      }//endif
      
      if (P >= 0)
        Nr  = StringToInt(deStr.substr(0,P), hetFormat)
      else
        Nr  = StringToInt(deStr, hetFormat);
    }//endif Decimal symbol found

    var Decimal = 0
    if (P >= 0) {
      var Intje  = deStr.substr(P+1)
      var I    = 0
      var Hulp = ''
      while (I < Intje.length) {
        if ("0123456789".indexOf( Intje.substr(I, 1) ) != -1) {
          Hulp += Intje.substr(I,1)
        }//endif
        I++
      }//enddo
      Decimal = Hulp
//      Decimal = StringToInt(deStr.substr(P+1), '')
    } // if (contains decimals)

    if (! ((isNaN(Nr)) || (isNaN(Decimal))) ) {
      var WholeReal = Nr + '.' + Decimal
      var Value = 1 * WholeReal
      return Value
    } else {
      return Number.NaN
    }//endif valid number
  }//endfunction StringToFloat()


  function FloatToString(Fl, Format) {
    var hetFormat = Format;
    if ((Format == null) || (Format == ""))
      hetFormat = "decsep=" + numberDecimalSymbol + 
//                  " thousandsep=" + DigitGroupingSymbol +
//                  " thousandgrp=" + NrOfDigitsInGroup +
                  " dec=" + CurrencyDigits;

    if (isNaN(Fl)) { Fl = 0 }
    var FlStr = "" + Fl

    var DecSep = numberDecimalSymbol
    if (hetFormat.indexOf('decsep=') >= 0) {
      DecSep = hetFormat.substr(hetFormat.indexOf('decsep=')+7, 1)
    }//endif

    var NrOfDigits = -1
    if (hetFormat.indexOf('dec=') >= 0) {
      var Hulp = hetFormat.substr(hetFormat.indexOf('dec=')+4)
      var P    = 0
      while ((P<Hulp.length) && ("0123456789".indexOf(Hulp.substr(P,1)) >= 0)) {
        P++
      }//enddo
      Hulp = Hulp.substr(0, P)
      NrOfDigits = 1 * Hulp
    }//endif

    var P = -1;
    if (NrOfDigits >=0) {
      FlStr = "" + Fl
      P   = FlStr.indexOf('.')
      if (P == -1)
        FlStr = FlStr + '.'
      FlStr = FlStr + '000000000'
      
      //move the decimal seperator NrOfDigits times to the right
      P   = FlStr.indexOf('.')
      FlStr = FlStr.substr(0, P) +
               FlStr.substr(P+1, NrOfDigits) + 
                '.' + 
                 FlStr.substr(P+1+NrOfDigits)
      Fl = parseFloat(FlStr);
      // old method, problem: for example 4.725 * 100 becomes 472.4999999999        
      // Fl = Fl * Dummy
      Fl = Math.round(Fl)
      var Dummy = Math.pow(10, NrOfDigits)
      Fl = Fl / Dummy
    }//endif
    FlStr = "" + Fl
    P   = FlStr.indexOf('.')
    if (P != -1) {
      var Nr  = FlStr.substr(0, P)
      var Dec = FlStr.substr(P+1)
      if (NrOfDigits >= 0) {
        if (Dec.length > NrOfDigits) {
alert('FOUT  WAARSCHUW NEBU  '+Nr + ' , "'+ Dec + '" Rounding to ' +NrOfDigits)
          var Dummy = Math.pow(10, NrOfDigits)
          var DecNr = (1*('0.'+Dec.substr(NrOfDigits))) * Dummy
//          DecNr = Math.round(DecNr)
          Dec   = '' + (Math.round(DecNr))

//          var DecNr = 1*('0.'+Dec.substr(NrOfDigits))
//            DecNr = Math.round(DecNr)
//          Dec   = Dec.substr(0, NrOfDigits)
//          DecNr = (1*Dec) + DecNr
//          Dec = ''+DecNr
//          while (Dec.length < NrOfDigits) {
//            Dec = '0' + Dec
//          } //enddo
        } else {
          while (Dec.length < NrOfDigits) {
            Dec += '0'
          } //enddo
        } // endif
      } // endif Must measure digits
    } else {
      var Nr  = FlStr
      var Dec = ''
      if (NrOfDigits >= 0) {
        while (Dec.length < NrOfDigits) {
          Dec += '0'
        } // end while
      } // endif
    }
    var Result = IntToString(Nr, hetFormat)
    if (Dec != '') { Result += DecSep + Dec }
    return Result
  } // function FloatToString()



