Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 93 additions & 146 deletions js/searching.js
Original file line number Diff line number Diff line change
@@ -1,195 +1,142 @@
var search=function() {

var dbRef = new Firebase('https://friendlychat-c4e05.firebaseio.com/');
var spamRef = dbRef.child('spammers');


var text = 'undefined';
text = document.getElementById("text").value;

if(!validateKeyword(text));
var search = function() {

//document.getElementById("result").innerHTML = text;
var text = document.getElementById("text").value;

if (!validateKeyword(text)) {
return false;
}

var qr = text.toUpperCase();
var locCheck = false;
var query = qr.split(" ");
console.log(query);
// var ilocName = document.getElementById("loc").value;
// var locName = ilocName.toUpperCase();
// var ikeywords = document.getElementById("keys").value;
// var keywords = ikeywords.toUpperCase();

//console.log(locName);
console.log(qr);
if((qr==="STOP") || (text === "") || (text === null))
{
document.getElementById("result").innerHTML="<div style=\"color:#D91E18\">Name is empty</div>";
document.getElementById("result2").innerHTML = "Use spammer's name, business, location, phone number or anything related";
return;
}
document.getElementById("result").innerHTML= "You Searched for: " + text;
var spam = [];
var count;
var spamNum = 0;
document.getElementById("newstyle").innerHTML = "";
var found= false;
var objects= [];

if(qr === "STOP" || text === "" || text === null) {
document.getElementById("result").innerHTML="<div style=\"color:#D91E18\">Name is empty</div>";
document.getElementById("result2").innerHTML = "Use spammer's name, business, location, phone number or anything related";
return false;
}

document.getElementById("result").innerHTML = "You Searched for: " + text;
var spam = [],
objects = [],
spamNum = 0,
count;

document.getElementById("newstyle").innerHTML = "";

spamRef.on("child_added", function(snap) {
objects.push(snap.val());
count=0;
count = 0;

snap.forEach(function(childSnapshot) {
var key = childSnapshot.key(); //Key is the child name, like "name", "email" etc.
var childData = childSnapshot.val(); //Value inside chil
if(typeof childData ==='string' )
{
var key = childSnapshot.key(); // Key is the child name, like "name", "email" etc.
var childData = childSnapshot.val(); // Value inside chil
if (typeof childData === "string") {
//console.log(childData);
for(var i=0;i<query.length;i++){
if(query[i] ===childData.toUpperCase() )
{
for (var i = 0; i < query.length; i++) {
if (query[i] === childData.toUpperCase()) {
count++;
console.log(count);
}
}
}




});

spam[spamNum] = count;
spamNum++;
});
// console.log(spam);
var index;
console.log(spam.length);
});

// console.log(spam);
console.log(spam.length);

//----------Checking if all Spammer searched score is ZERO----------
var check = false;
for(var k=0; k<spam.length; k++){
if(spam[k]!==0){
check = true;
}
}
if(check === false){
console.log("CANT FIND SPAMMER!");
CantfoundSpammer();
if (spam.filter(function(x) { return x; }).pop() == undefined) { // https://stackoverflow.com/questions/20744960/javascript-how-can-i-fetch-a-non-zero-value-from-an-array
console.log("CANT FIND SPAMMER!");
CantfoundSpammer();
return false;
}
//-------------------------------------------------------------------


//----------Finding the highest score among the searched-------------
else
{
var max = 0;
for(var k=0; k<spam.length; k++)
{
if(spam[k]>max)
{
max = spam[k];
index = k;
}
}
console.log("MAX = " + max);
//-----------------------------------------------------------------

//--------Finding if the max value is redundant--------------------
var countMax=0;
for(var k=0; k<spam.length; k++)
{
if(spam[k]===max)
{
countMax++;
}
}
if(countMax>1){
foundMultipleTimes();
}
if(countMax===1){
foundSpammer();
displayInfo(objects[index]);
var max = 0, index;
for (var k = 0; k < spam.length; k++) {
if (spam[k] > max) {
max = spam[k];
index = k;
}
//-----------------------------------------------------------------


}
console.log("MAX = " + max);

//--------Finding if the max value is redundant--------------------
var countMax = spam.reduce(function(n, val) { // https://stackoverflow.com/questions/17313268/efficiently-find-the-number-of-occurrences-a-given-value-has-in-an-array
return n + (val === max);
}, 0);

if (countMax > 1) {
foundMultipleTimes();
} else { // now it's definitely 1
foundSpammer();
displayInfo(objects[index]);
}
} /** Main Search function ends here **/

//-------------------------------------------------------------------


}



var CantfoundSpammer = function(){
function CantfoundSpammer() {
document.getElementById("result").innerHTML="<div style=\"color:#D91E18\">Can't find</div>";
document.getElementById("result2").innerHTML = "Try any other keyword to make your search wider";
document.getElementById("newstyle").innerHTML = " .box2 {width: 700px;height: 150px;}";
document.getElementById("newstyle").innerHTML = "body{background-color: #E74C3C !important;} .title-container .title-down{ color: #E4F1FE !important;} .title-container .title{ color: #E4F1FE !important;} a.btn {display: none;}";
document.getElementById("newstyle").innerHTML = " .box2 {width: 700px;height: 150px;} body{background-color: #E74C3C !important;} .title-container .title-down{ color: #E4F1FE !important;} .title-container .title{ color: #E4F1FE !important;} a.btn {display: none;}";
}


var foundSpammer = function(){
document.getElementById("result").innerHTML = "Found";
document.getElementById("newstyle").innerHTML = "body{background-color: #2ECC71 !important;} #result-heading,#result2{display:none;} .box2 { top: 55%;width: 150px;height: 150px;} #result{color:#92F22A;font-family: raleway;font-weight:900 !important;} .title-container .title-down{ color: #E4F1FE !important;} .title-container .title{ color: #E4F1FE !important;} a.btn {display: block;}";
function foundSpammer() {
document.getElementById("result").innerHTML = "Found";
document.getElementById("newstyle").innerHTML = "body{background-color: #2ECC71 !important;} #result-heading,#result2{display:none;} .box2 { top: 55%;width: 150px;height: 150px;} #result{color:#92F22A;font-family: raleway;font-weight:900 !important;} .title-container .title-down{ color: #E4F1FE !important;} .title-container .title{ color: #E4F1FE !important;} a.btn {display: block;}";

}


var foundMultipleTimes = function(){
document.getElementById("result").innerHTML="<div style=\"color:#446CB3\">There are many spammers with this keyword</div>";
function foundMultipleTimes() {
document.getElementById("result").innerHTML="<div style=\"color:#446CB3\">There are many spammers with this keyword</div>";
document.getElementById("result2").innerHTML = "Narrow down your search by adding last name or social ID or any keyword";
document.getElementById("newstyle").innerHTML = "body{background-color: #6BB9F0 !important;} .title-container .title-down{ color: #fff !important;} .title-container .title{ color: #fff !important;} a.btn {display: none;}";
}


var nameKeys = {
"first_name": "First Name",
"last_name": "Last Name",
"address": "Address",
"phone": "Phone Number",
"email": "Email Address",
"socialID": "Social ID",
"tobs": "Type of Business"
};
var nameKey = function(key){
if(key === 'first_name'){
return "First Name";
}
else if(key === 'last_name'){
return "Last Name";
}
else if(key === 'address'){
return "Address";
}
else if(key === 'phone'){
return "Phone Number";
}
else if(key === 'email'){
return "Email Address";
}
else if(key === 'socialID'){
return "Social ID";
}
else if(key === 'tobs'){
return "Type of Business";
}
return nameKeys[key];
}

var validateKeyword = function(search){
for(var i=0; i<search.length; i++)
{
if(search[i] === '+' || search[i] === '-' || search[i] === '/' || search[i] === '*' || search[i] === '.' || search[i] === '\'' || search[i] === '$' || search[i] === '#' || search[i] === '"' || search[i] === '_' || search[i] === '=' || search[i] === '%')
{
document.getElementById("result").innerHTML = "Invalid Characters";
return false;
}
else{
return true;
}
}
var invalidChars = ['+', '-', '/', '*', '.', '\'', '$', '#', '"', '_', '=', '%'];
function validateKeyword(keyword) {
for (var i = 0; i < keyword.length; i++) {
invalidChars.forEach(function (item, index) {
if (keyword[i] === item) {
document.getElementById("result").innerHTML = "Invalid Characters";
return false;
}
});
}
return true;
}

function displayInfo(obj) {
var details = document.getElementById("details");

var displayInfo = function(obj){
console.log(obj);
document.getElementById("popupHeading").innerHTML = obj.first_name + " " + obj.last_name;
document.getElementById("details").innerHTML = "<b>Email: </b>" + obj.email;
document.getElementById("details").innerHTML += "<br><b>Phone: </b>" + obj.phone;
document.getElementById("details").innerHTML += "<br><b>City: </b>" + obj.city;
document.getElementById("details").innerHTML += "<br><b>Type of Business: </b>" + obj.tobs;
document.getElementById("details").innerHTML += "<br><b>Social ID: </b>" + obj.socialID;
document.getElementById("details").innerHTML += "<br><b>Description: </b><br>" + "<div style=\"padding-left:1em;\">" + obj.description;
}

details.innerHTML = "<b>Email: </b>" + obj.email
+ "<br><b>Phone: </b>" + obj.phone
+ "<br><b>City: </b>" + obj.city
+ "<br><b>Type of Business: </b>" + obj.tobs
+ "<br><b>Social ID: </b>" + obj.socialID
+ "<br><b>Description: </b><br>" + "<div style=\"padding-left:1em;\">" + obj.description;
}