    
//////////////////////////// define gps data point object
function gpsPt (sn, tstr, lat, lng, spd, dir, sts) {
  this.sn = sn;
  yea = tstr.substr (0, 4);
  mon = tstr.substr (5, 2);
  day = tstr.substr (8, 2);
  hou = tstr.substr (11, 2);
  min = tstr.substr (14, 2);
  sec = tstr.substr (17, 2);
  t = new Date ();
  t.setUTCFullYear(yea, mon-1, day);
  t.setUTCHours(hou);
  t.setUTCMinutes(min);
  t.setUTCSeconds(sec);
  this.time = t;
  this.latitude = lat;
  this.longitude = lng;
  this.speed = spd;
  this.direction = dir;
  this.status = sts;
}

    
//////////////////////////// define object for vehicle summary 
function Vehicle (sn, id) {
  this.sn = sn;                    // ivu serial number
  this.id = id;                    // vehile id
  this.fleet = 0;
  this.from = 0;                   // first start time, ms
  this.to = 0;                     // last stop time, ms
  this.msec = 0;                   // total travel time, ms
  this.mile = 0;                   // total mileage, km
  this.speed = 0;                  // highest speed of the day, kph
  this.spdings = 0;                // speeding times
  this.spdingt = 0;                // speeding time period
  this.spdingm = 0;                // speeding mileage
  this.aryPoint = new Array();     // array for gps points
  this.arySpding = new Array();    // array for speeding records
  this.aryStop = new Array();      // array for startstop records
}
  
    
//////////////////////////// define object for speeding record
function Spding (sn, fr, to, mi, sp) {
  this.sn = sn;                    // ivu serial number
  this.from = fr;                  // speeding start time, ms
  this.to = to;                    // speeding end time, ms
  this.msec = to - fr;             // speeding time interval, ms
  this.mile = mi;                  // speeding mileage, km
  this.speed = sp;                 // highest speeding time, kph
  this.at;                         // speeding start location
}
  
    
//////////////////////////// define object for start/stop period  
function Stop (sn, fr, to, mi, sp, lat, lng) {
  this.sn = sn;                    // ivu serial number
  this.from = fr;                  // from time, ms
  this.to = to;                    // to time, ms
  this.msec = to - fr;             // time interval of this period, ms
  this.mile = mi;                  // travel mileage of this period, km
  this.speed = sp;                 // highest speed of this period, kph
  this.latitude = lat;                        // start location of this period
  this.longitude = lng;
}
  
    
//////////////////////////// ????????
function getDate (dtstr) {
  var i = dtstr.search (":");  
  if (i > 1) {
    str1 = dtstr.substring (0, i - 3);
    str2 = dtstr.substr (i + 7); 
    str1 = str1.concat (str2);
    return str1;
  }
  else {
    return dtstr;
  }
}
    
    
//////////////////////////// translate relative delta time of ms to hh:mm:ss
function dms2hms (t) {
  d = new Date;
  d.setTime(t);
  h = d.getUTCHours();
  if (h < 10) h = "0" + h;
  m = d.getUTCMinutes();
  if (m < 10) m = "0" + m;
  s = d.getUTCSeconds();
  if (s < 10) s = "0" + s;
  return h + ":" + m + ":" + s;
}
  
  
//////////////////////////// translate absolute time of ms to hh:mm:ss
function ms2hms (t) {
  if (t > 0) {
    d = new Date;
    d.setTime(t);
    h = d.getHours();
    if (h < 10) h = "0" + h;
    m = d.getMinutes();
    if (m < 10) m = "0" + m;
    s = d.getSeconds();
    if (s < 10) s = "0" + s;
    return h + ":" + m + ":" + s;
  }
  else return "-";
}

    
//////////////////////////// translate absolute time to yyyy-mm-dd
function tm2ymd (t) {
  y = t.getFullYear();
  m = t.getMonth() + 1;
  if (m < 10) m = "0" + m;
  d = t.getDate();
  if (d < 10) d = "0" + d;
  return y + "-" + m + "-" + d;
}


//////////////////////////// translate absolute time to hh-mm-ss
function tm2hms (t) {
  h = t.getHours();
  if (h < 10) h = "0" + h;
  m = t.getMinutes();
  if (m < 10) m = "0" + m;
  s = t.getSeconds();
  if (s < 10) s = "0" + s;
  return h + ":" + m + ":" + s;
}


//////////////////////////// float type transfer to 1 decimal
function ft1d (f) {
  return Math.round(f * 10) / 10 ;
}

    
//////////////////////////// get date of today in yyyy-mm-dd format
function getToday () {
  var t = new Date();
  return tm2ymd (t);
}


//////////////////////////// get time zone in unit hour 
function getTzone () {
  var tTmp = new Date();
  var tmz = tTmp.getTimezoneOffset();
  tmz = -tmz/60;
  return tmz;
}


//////////////////////////// get parameters in url link text 
function HTTP_GET_VARS (paras) {
  var url = location.href; 
  var paraString = url.substring (url.indexOf ("?")+1, url.length).split ("&"); 
  var paraObj = {} 
  for (i=0; j=paraString[i]; i++) { 
    paraObj [j.substring (0, j.indexOf("=")).toLowerCase()] = j.substring (j.indexOf("=")+1, j.length); 
  } 
  var returnValue = paraObj [paras.toLowerCase()]; 
  if (typeof(returnValue) == "undefined") { 
    return ""; 
  } 
  else { 
    return returnValue; 
  } 
}


//////////////////////////// calculate the report data for a vehicle 
function vehcalc (theVeh) {

//document.write ("Calculation Start.");
  var msNew = 0;        // milisecond from 1970-01-01 of current point
  var latNew = 0.0;     // latitude of current point, in float type
  var lgtNew = 0.0;     // longitude of current point, in float type
  var spdNew = 0;       // speed of current point, in integer type
  var msOld = 0;        // milisecond from 1970-01-01 of last point
  var latOld = 0.0;     // latitude of last point, in float type
  var lgtOld = 0.0;     // longitude of last point, in float type
  var spdOld = 0;       // speed of last point, in integer type

  var mileAll = 0;      // total mileage, in unit meter, in integer type
  var mileStop = 0;     // mileage of each moving period
  var mileNew = 0;      // mileage from last point

  var spdAll = 0;       // highest speed of the day
  var spdStop = 0;      // max speed of each moving period

  var msAll = 0;        // milisecond total travelling
  var msPause = 0;      // time of find first 0 speed point
  var msSpdingFr = 0;   // Speeding from time
  var msSpdingTo = 0;   // Speeding to time

  var nStop = 0;        // Start Stop counter
  var nSpding = 0;      // Speeding times
     
  var isMoving = 0;     // 0 - stop, 1 - pause, 2 - moving

  len = theVeh.aryPoint.length;
//document.write ("len=" + len + "<br>");  
  if (len > 0) {
    for (j in theVeh.aryPoint) {

      msNew = theVeh.aryPoint[j].time.getTime();
//document.write ("msNew=" + msNew + ", ");
      latNew = theVeh.aryPoint[j].latitude;
      lgtNew = theVeh.aryPoint[j].longitude;
      spdNew = theVeh.aryPoint[j].speed;
      mileNew = 0;
//document.write ("spdNew=" + spdNew + ", ");

      if (isMoving >= 1) {                                ////////////////////////////////// moving
        mileNew = (latNew - latOld) * (latNew - latOld) + (lgtNew - lgtOld) * (lgtNew - lgtOld);
        mileNew = Math.sqrt (mileNew) * 111.32;
        mileStop += mileNew;                                         // mileage of a moving section
//document.write ("mileNew=" + mileNew + ", mileStop=" + mileStop + ", ");

        if (spdStop < spdNew) spdStop = spdNew; 
        if (spdAll < spdNew) spdAll = spdNew;
 
        if (isMoving == 1) {                               // pause
          if (msNew - msPause > ppStm * 60000) {           // pause to stop
            isMoving = 0;
            theVeh.aryStop[nStop] = new Stop (theVeh.sn, msPause, msNew, 0, 0, latNew, lgtNew);
            if (nStop >= 1) {
              theVeh.aryStop[nStop-1].to = msPause;
              theVeh.aryStop[nStop-1].msec = msPause - theVeh.aryStop[nStop-1].from;
              theVeh.aryStop[nStop-1].mile = mileStop;
              theVeh.aryStop[nStop-1].speed = spdStop;
              msAll += theVeh.aryStop[nStop-1].msec;
              mileAll += theVeh.aryStop[nStop-1].mile;    // total mileage of the vehicle
            }
            nStop ++;
          }                                               // pause to re-moving
          else if (spdNew >= ppSpm) {
            isMoving = 2;                                 // re-move from pause
          }
        }
        else if (spdNew < ppSpm) {                        // from moving to pause
          isMoving = 1;
          msPause = msNew;
        }
      }

      if ((isMoving == 0) && (spdNew >= ppSpm)) {      //////////////////////////////// start
        isMoving = 2;
        theVeh.aryStop[nStop] = new Stop (theVeh.sn, msNew, msNew, 0, spdNew);
        if (nStop >= 1) {
          theVeh.aryStop[nStop-1].to = msNew;
          theVeh.aryStop[nStop-1].msec = msNew - theVeh.aryStop[nStop-1].from;
        }
        mileStop = 0;
        spdStop = spdNew;
        nStop ++;
//document.write ("spdStop=" + spdStop + ", nStop=" + nStop + ", ");      
      }
        
      if (spdNew > ppSpl) {                           /////////////////////////////// speeding
        if (spdOld <= ppSpl) {                                                                // start speeding
          theVeh.arySpding[nSpding] = new Spding (theVeh.sn, msNew, msNew, 0, spdNew);
        }
        else {                                                                                // continue speeding
          theVeh.arySpding[nSpding].to = msNew;
          theVeh.arySpding[nSpding].msec = msNew - theVeh.arySpding[nSpding].from;
          theVeh.arySpding[nSpding].mile += mileNew;
          if (theVeh.arySpding[nSpding].speed < spdNew) theVeh.arySpding[nSpding].speed = spdNew;
        }
      }
 
      if ((spdOld > ppSpl) && (spdNew <= ppSpl)) {    /////////////////////////// end speeding
        nSpding ++;
      }
          
      msOld = msNew;
      latOld = latNew;
      lgtOld = lgtNew;
      spdOld = spdNew;
//document.write ("isMoving=" + isMoving + "<br>");    
    
    }                                     ////////////////// end of loop j - each point

    if ((theVeh.aryPoint[j].speed > 0) || (isMoving > 0)) {         //// no stop point this day
      theVeh.aryStop[nStop-1].to = msNew + 1000;
      theVeh.aryStop[nStop-1].msec = msNew - theVeh.aryStop[nStop-1].from + 1000;
      theVeh.aryStop[nStop-1].mile = mileStop;
      theVeh.aryStop[nStop-1].speed = spdStop;
    }

    started = 0;
    for (j = 0; j < nStop; j ++) {
      if (theVeh.aryStop[j].speed > 0) {
        if (started == 0) {
          theVeh.from = theVeh.aryStop[j].from;
          started = 1;
        }
        theVeh.to = theVeh.aryStop[j].to;
        theVeh.msec += theVeh.aryStop[j].msec;
        theVeh.mile += theVeh.aryStop[j].mile;
        if (theVeh.speed < theVeh.aryStop[j].speed) theVeh.speed = theVeh.aryStop[j].speed;
      }
    }
    theVeh.spdings = nSpding;
    for (j = 0; j < nSpding; j ++) {
      theVeh.spdingt += theVeh.arySpding[j].msec;
      theVeh.spdingm += theVeh.arySpding[j].mile;
    }
  }
//document.write ("Calculation End. ");
  return theVeh;
}


//////////////////////////// print all information of the fleet 
function debugAll (aryVeh) {

  document.write ("<br>**************************** Debug Window ***************************<br>");
  
  document.write ("ppSpl=" + ppSpl + "<br>");
  document.write ("ppSpm=" + ppSpm + "<br>");
  document.write ("ppStm=" + ppStm + "<br>");
  document.write ("ppFid=" + ppFid + "<br>");
  document.write ("ppPsw=" + ppPsw + "<br>");
  document.write ("ppDay=" + ppDay + "<br>");
  document.write ("ppTmz=" + ppTmz + "<br>");
  document.write ("ppSys=" + ppSys + "<br>");
  document.write ("ppPub=" + ppPub + "<br>");
  document.write ("ppPri=" + ppPri + "<br>");
  document.write ("ppPid=" + ppPid + "<br>");
  document.write ("ppPpw=" + ppPpw + "<br><br>");
  
  document.write ("*********** aryVeh.length=" + aryVeh.length + " ***********<br><br>");
  for (i in aryVeh) {
    document.write ("*********** Vehile # " + i + " ***********<br>");
    document.write ("sn=" + aryVeh[i].sn + ", ");
    document.write ("id=" + aryVeh[i].id + ", ");
    document.write ("from=" + ms2hms(aryVeh[i].from) + ", ");
    document.write ("to=" + ms2hms(aryVeh[i].to) + ", ");
    document.write ("msec=" + dms2hms(aryVeh[i].msec) + ", ");
    document.write ("mile=" + ft1d(aryVeh[i].mile) + "km, ");
    document.write ("speed=" + aryVeh[i].speed + ", ");
    document.write ("spdings=" + aryVeh[i].spdings + ", ");
    document.write ("spdingt=" + aryVeh[i].spdingt + ", ");
    document.write ("spdingm=" + aryVeh[i].spdingm + "<br>");
    
    document.write ("aryVeh[" + i + "].aryPoint.length=" + aryVeh[i].aryPoint.length + " >>>>>>>>>>>>><br>");
    for (j in aryVeh[i].aryPoint) {
      document.write ("sn=" + aryVeh[i].aryPoint[j].sn + ", ");
      document.write ("time=" + tm2hms(aryVeh[i].aryPoint[j].time) + ", ");
      document.write ("latitude=" + aryVeh[i].aryPoint[j].latitude + ", ");
      document.write ("longitude=" + aryVeh[i].aryPoint[j].longitude + ", ");
      document.write ("speed=" + aryVeh[i].aryPoint[j].speed + ", ");
      document.write ("direction=" + aryVeh[i].aryPoint[j].direction + ", ");
      document.write ("status=" + aryVeh[i].aryPoint[j].status + "<br>");
    }
    
    document.write ("aryVeh[" + i + "].arySpding.length=" + aryVeh[i].arySpding.length + " >>>>>>>>>>>>><br>");
    for (j in aryVeh[i].arySpding) {
      document.write ("sn=" + aryVeh[i].arySpding[j].sn + ", ");
      document.write ("from=" + ms2hms(aryVeh[i].arySpding[j].from) + ", ");
      document.write ("to=" + ms2hms(aryVeh[i].arySpding[j].to) + ", ");
      document.write ("msec=" + dms2hms(aryVeh[i].arySpding[j].msec) + ", ");
      document.write ("mile=" + aryVeh[i].arySpding[j].mile + ", ");
      document.write ("speed=" + aryVeh[i].arySpding[j].speed + ", ");
      document.write ("at=" + aryVeh[i].arySpding[j].at + "<br>");
    }
    
    document.write ("aryVeh[" + i + "].aryStop.length=" + aryVeh[i].aryStop.length + " >>>>>>>>>>>>><br>");
    for (j in aryVeh[i].aryStop) {
      document.write ("sn=" + aryVeh[i].aryStop[j].sn + ", ");
      document.write ("from=" + ms2hms(aryVeh[i].aryStop[j].from) + ", ");
      document.write ("to=" + ms2hms(aryVeh[i].aryStop[j].to) + ", ");
      document.write ("msec=" + dms2hms(aryVeh[i].aryStop[j].msec) + ", ");
      document.write ("mile=" + ft1d(aryVeh[i].aryStop[j].mile) + ", ");
      document.write ("speed=" + aryVeh[i].aryStop[j].speed + ", ");
      document.write ("at=" + aryVeh[i].aryStop[j].at + "<br>");
    }
    
    document.write ("*********** End of vehicle # " + i + " ***********<br><br>");
  }
  
  document.write ("**************************** End of Debug Window ***************************<br><br>");
  return;
}

