var $fh = require('fh-mbaas-api');
var express = require('express');
var bodyParser = require('body-parser');
var cors = require('cors');

function leadRoute() {
  var lead = new express.Router();
  lead.use(cors());
  lead.use(bodyParser());

  // POST REST endpoint - note we use 'body-parser' middleware above to parse the request body in this route.
  // This can also be added in application.js
  // See: https://github.com/senchalabs/connect#middleware for a list of Express 4 middleware
  lead.post('/', function(req, res) {
    lottery = false;
    
    // check question
    var options = {
      "act": "read",
      "type": "question",
      "guid": req.body.answer.split(":")[0]
    };

    $fh.db(options, function (err, data) {
      if (err) {
        console.error("Error " + err);
      } else {
        console.log("Question: " + JSON.stringify(data));
        console.log("Correct Answr: " + data.fields.correct);
        console.log("User Asnwer: " + req.body.answer.split(":")[1]);
        console.log(data.fields.correct.valueOf() == req.body.answer.split(":")[1].valueOf());
        
        lottery = (data.fields.correct.valueOf() == req.body.answer.split(":")[1].valueOf());
        console.log("Lottery 1: " + lottery);
      }
    });
    
    console.log("Lottery 2: " + lottery);

    options = {
      "act": "create",
      "type": "lead", // Entity/Collection name
      "fields": { // The structure of the entry/row data. A data is analogous to "Row" in MySql or "Documents" in MongoDB
        "name": req.body.name,
        "email": req.body.email,
        "company": req.body.company,
        "job": req.body.job,
        "lottery": lottery
      }
    };

    $fh.db(options, function (err, data) {
      if (err) {
        console.error("Error " + err);
        res.json({msg: err, status: 'error'});
      } else {
        res.json({msg: "Registered", status: 'success'});
      }
    });
  });

  return lead;
}

module.exports = leadRoute;





@@ -1260,673 +1260,8 @@
 %7D);%0A
-    %0A    console.log(%22Lottery 2: %22 + lottery);%0A%0A    options = %7B%0A      %22act%22: %22create%22,%0A      %22type%22: %22lead%22, // Entity/Collection name%0A      %22fields%22: %7B // The structure of the entry/row data. A data is analogous to %22Row%22 in MySql or %22Documents%22 in MongoDB%0A        %22name%22: req.body.name,%0A        %22email%22: req.body.email,%0A        %22company%22: req.body.company,%0A        %22job%22: req.body.job,%0A        %22lottery%22: lottery%0A      %7D%0A    %7D;%0A%0A    $fh.db(options, function (err, data) %7B%0A      if (err) %7B%0A        console.error(%22Error %22 + err);%0A        res.json(%7Bmsg: err, status: 'error'%7D);%0A      %7D else %7B%0A        res.json(%7Bmsg: %22Registered%22, status: 'success'%7D);%0A      %7D%0A    %7D);%0A
   %7D)

update file /lib/lead.js