document.onkeydown = checkKey;
document.onload = fetchAllKeys();

var allData = [];
var pos;
function checkKey(e) {
	if (pos >= allData.length) {
		return
	}
	e = e || window.event;

	if (e.keyCode == '37') {

		getLeft(allData[pos]["key"], function(val) {
			var newVal = val + 1;
			if(newVal == 2) {
				console.log("sending reject email!");
				sendRejectEmail("gore.shantanu@gmail.com");
			}
			else {
				console.log("newVal:" + newVal);
			}
		});

       $("#resume").fadeOut(500, function() {
			addLeft(allData[pos]["key"]);
       		setNextImage();
       });

       $("#cross").fadeIn(500, function() {
       		$("#cross").fadeOut(500, null);
       });
			 var email = allData[pos]["value"]["email"]
       sendRejectEmail(email);

	}
	else if (e.keyCode == '39') {
       // document.getElementById("resume").src = "shantanu.jpg";
       // fade();
       $("#resume").fadeOut(500, function() {
			addRight(allData[pos]["key"]);
       		setNextImage()
       });

       $("#check").fadeIn(500, function() {
       		$("#check").fadeOut(500, null);
       });
			 var email = allData[pos]["value"]["email"]
       sendAcceptEmail(email);
   }

}

function fetchAllKeys() {
	var ref = firebase.database().ref();
	ref.orderByChild("timestamp").once('value').then(function(snapshot) {
		snapshot.forEach(function(childSnap) {
				allData.push({"key": childSnap.key, "value": childSnap.val()})
		})
	});
	pos = 0;
}


function setNextImage() {
	if (pos >= allData.length - 1) {
		document.getElementById("resume").src = "done.jpg";
		++pos;
	}
	else {
		document.getElementById("resume").src = allData[++pos]["value"]["imageName"];
	}
	$("#resume").one("load", function() {
		$("#resume").fadeIn(500, null);
	})
}

function sendAcceptEmail(toEmail) {
	var url = "https://api.sendgrid.com/v3/mail/send";

	data = {
	  "personalizations": [
	    {
	      "to": [
	        {
	          "email": toEmail
	        }
	      ],
	      "subject": "Hello, World!"
	    }
	  ],
	  "from": {
	    "email": "sg937@cornell.edu"
	  },
	  "content": [
	    {
	      "type": "text/plain",
	      "value": "Congratulations! We would like to schedule an interview with you!"
	    }
	  ]
	};

	$.ajax({
	  url:url,
	  method:"POST",
	  data:JSON.stringify(data),
	  contentType:"application/json; charset=utf-8",
		crossDomain: true,
	  dataType:"json",
	  beforeSend: function(xhr) {
	  	// xhr.setRequestHeader("Content-Type", "application/json");
	  	xhr.setRequestHeader("Authorization", "BEARER SG.NPvEqNZsQJiF3wfNt1XZ3w.wHZ83YN0iZUVUdHq22wdJOYmN6NjE6M6N-sjUn0ytt0");
	  },
	  success: function(result){
	    alert("done!");
	  }
	})
}

function sendRejectEmail(toEmail) {
	var url = "https://api.sendgrid.com/v3/mail/send";

	data = {
	  "personalizations": [
	    {
	      "to": [
	        {
	          "email": toEmail
	        }
	      ],
	      "subject": "Hello, World!"
	    }
	  ],
	  "from": {
	    "email": "sg937@cornell.edu"
	  },
	  "content": [
	    {
	      "type": "text/plain",
	      "value": "Sorry, we received many highly qualified applicants this year, and could not find a position for you."
	    }
	  ]
	};

	$.ajax({
	  url:url,
	  type:"POST",
	  data:JSON.stringify(data),
		crossDomain: true,
	  contentType:"application/json; charset=utf-8",
	  dataType:"json",
	  beforeSend: function(xhr) {
	  	// xhr.setRequestHeader("Content-Type", "application/json");
	  	xhr.setRequestHeader("Authorization", "BEARER SG.NPvEqNZsQJiF3wfNt1XZ3w.wHZ83YN0iZUVUdHq22wdJOYmN6NjE6M6N-sjUn0ytt0");
	  },
	  success: function(){
	    alert("done!");
	  }
	})
}

@@ -666,83 +666,60 @@
 %7D);%0A
-%09%09%09 var email = allData%5Bpos%5D%5B%22value%22%5D%5B%22email%22%5D%0A       sendRejectEmail(email
+%0A       // sendRejectEmail(%22gore.shantanu@gmail.com%22
 );%0A%0A
@@ -1052,83 +1052,57 @@
 %7D);%0A
-%09%09%09 var email = allData%5Bpos%5D%5B%22value%22%5D%5B%22email%22%5D%0A       sendAcceptEmail(email
+%0A       sendAcceptEmail(%22gore.shantanu@gmail.com%22
 );%0A 
@@ -1281,24 +1281,65 @@
 hildSnap) %7B%0A
+%09%09%09v = childSnap.val();%0A%09%09%09if(v.display)%0A
 %09%09%09%09allData.
@@ -1401,14 +1401,16 @@
 %09%09%7D)
+;
 %0A%09%7D);%0A
+%0A
 %09pos
@@ -2204,14 +2204,12 @@
 %0A%09  
-method
+type
 :%22PO
@@ -2293,37 +2293,16 @@
 utf-8%22,%0A
-%09%09crossDomain: true,%0A
 %09  dataT
@@ -2466,74 +2466,74 @@
  SG.
-NPvEqNZsQJiF3wfNt1XZ3w.wHZ83YN0iZUVUdHq22wdJOYmN6NjE6M6N-sjUn0ytt0
+tGvhI1-_QC2rtVHY5rayTA.5pjZKGArZeTuvffW2lrG16Y-BLLSyhT5wfxw9WTsXSc
 %22);%0A
@@ -2563,14 +2563,8 @@
 ion(
-result
 )%7B%0A%09
@@ -3162,29 +3162,8 @@
 a),%0A
-%09%09crossDomain: true,%0A
 %09  c
@@ -3381,74 +3381,74 @@
  SG.
-NPvEqNZsQJiF3wfNt1XZ3w.wHZ83YN0iZUVUdHq22wdJOYmN6NjE6M6N-sjUn0ytt0
+tGvhI1-_QC2rtVHY5rayTA.5pjZKGArZeTuvffW2lrG16Y-BLLSyhT5wfxw9WTsXSc
 %22);%0A
@@ -3488,29 +3488,28 @@
   alert(%22done!%22);%0A%09  %7D%0A%09%7D)%0A%7D
-%0A

send email after 2 rejects logic