/*
   * (non-Javadoc)
   *
   * @see android.view.View.OnClickListener#onClick(android.view.View)
   */
  public void onClick(final View v) {

    if (v.getId() == R.id.btnJobStartDate) {
      showDialog(ID_START_DATEPICKER);

    } else if (v.getId() == R.id.btnJobStartTime) {
      showDialog(ID_START_TIMEPICKER);

    } else if (v.getId() == R.id.btnJobEndDate) {
      showDialog(ID_END_DATEPICKER);

    } else if (v.getId() == R.id.btnJobEndTime) {
      showDialog(ID_END_TIMEPICKER);

    } else if (v.getId() == R.id.btnJobDiscard) {

      AlertDialog alertDialog = new AlertDialog.Builder(this).create();
      alertDialog.setTitle("Discard, Are you sure?");
      alertDialog.setButton(
          "Yes",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              Intent home = new Intent(v.getContext(), HomeActivity.class);
              startActivityForResult(home, CommonConstants.HOME_REQ_ID);
              return;
            }
          });
      alertDialog.setButton2(
          "Cancel",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              return;
            }
          });
      alertDialog.show();

    } else if (v.getId() == R.id.btnJobReset) {
      AlertDialog alertDialog = new AlertDialog.Builder(this).create();
      alertDialog.setTitle("Reset fields, Are you sure?");
      alertDialog.setButton(
          "Yes",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              editTxtLatitude.setText("");
              editTxtLongitude.setText("");
              editTxtLocRange.setText("");
              editTxtNodes.setText("");
              editTxtFreq.setText("");
              editTxtTimePeriod.setText("");
              editTxtDesc.setText("");
              return;
            }
          });
      alertDialog.setButton2(
          "Cancel",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              return;
            }
          });
      alertDialog.show();

    } else if (v.getId() == R.id.btnJobSave) {
      Calendar startDatetime = null;
      Calendar endDatetime = null;
      boolean validStartEndTime = true;

      if (chkBxStarttime.isChecked()) {
        startDatetime = Calendar.getInstance();
        startDatetime.set(startYear, startMonth, startDay, startHour, startMinute);
      }
      if (chkBxEndtime.isChecked()) {
        endDatetime = Calendar.getInstance();
        endDatetime.set(endYear, endMonth, endDay, endHour, endMinute);
        validStartEndTime = Calendar.getInstance().before(endDatetime);
      }
      if (validStartEndTime && startDatetime != null && endDatetime != null) {
        validStartEndTime = startDatetime.before(endDatetime);
      }
      if (validStartEndTime) {
        if (spinSensorType.getSelectedItem().toString().length() == 0
            || editTxtLatitude.getText().length() == 0
            || editTxtLongitude.getText().length() == 0
            || editTxtLocRange.getText().length() == 0
            || editTxtNodes.getText().length() == 0
            || editTxtFreq.getText().length() == 0
            || editTxtTimePeriod.getText().length() == 0) {
          Toast.makeText(
                  AddJobActivity.this,
                  "Input Validation failed! All fields should be not null and the end date should be after the start date.",
                  Toast.LENGTH_LONG)
              .show();
        } else {
          boolean issuccess =
              serviceInvoker.addJob(
                  1,
                  Float.parseFloat(editTxtLatitude.getText().toString()),
                  Float.parseFloat(editTxtLongitude.getText().toString()),
                  Float.parseFloat(editTxtLocRange.getText().toString()),
                  (startDatetime != null) ? startDatetime.getTimeInMillis() : 0,
                  (endDatetime != null) ? endDatetime.getTimeInMillis() : 0,
                  Integer.parseInt(editTxtFreq.getText().toString()),
                  Integer.parseInt(editTxtTimePeriod.getText().toString()),
                  Integer.parseInt(editTxtNodes.getText().toString()),
                  imei,
                  editTxtDesc.getText().toString(),
                  username);
          if (issuccess) {
            // when add job completes go back to the previous step or viewjob.
            Toast.makeText(AddJobActivity.this, "Job Added Successfully.", Toast.LENGTH_LONG)
                .show();
            Intent home = new Intent(v.getContext(), HomeActivity.class);
            startActivityForResult(home, CommonConstants.HOME_REQ_ID);

          } else {
            Toast.makeText(AddJobActivity.this, "Could not add job! Try again.", Toast.LENGTH_LONG)
                .show();
          }
        }
      } else {
        Toast.makeText(AddJobActivity.this, "Invalid StartTime or ExpireTime!", Toast.LENGTH_LONG)
            .show();
      }
    }
  }