<commit_before>/**
 * @author PerronJones
 */

var currentUser;


function setCurrentUser(userID){
	
	//check if user exist. if the user does not exist, then create the user. 
	//necessary fields needed for creation (firstName, lastName, email, phone, latitude, longitude)
	//If the user does exist, then set the current user.
	//Also, update the position.
	console.log("user.js(setCurrentUser): userID: " + userID);
	var user = readUser(userID);
	//var user = readUser(1232);
	
	if(user != null){
		console.log("user exists");
		console.log(user);
		
        //set session User ID
        $.ajax({
			url : "sess",
			data : {
				'UserID' : userID

			},
			context : document.body,
			async : false,
			type : 'POST',
			dataType : "json",
			success : function(data) {
				console.log("Data Success");
				console.log(data);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				console.log("Status: " + textStatus);
				console.log("Error: " + errorThrown);
			}
		});
        
		//update position
		currentUser = user;
		//obtainLocation(); //The position is updated automatically when this method is called.
		obtainLocationFromInitialization();
	}
	else{
		console.log("user does NOT exist");
		console.log(user);
	}
	
}


function refreshCurrentUser()
{
    console.log("inside refreshCurrentUser");
    // will return -1 if there is no user session
    var promise = $.ajax({
		url : "sess",
		context : document.body,
		async : false,
		type : 'GET',
		dataType : "json",
		success : function(data) {
            console.log("Data Success");
            console.log(data);
		},
		error : function(jqXHR, textStatus, errorThrown) {
            console.log("Status: " + textStatus);
            console.log("Error: " + errorThrown);
		}
	});
    
    // get the returned user ID
	var userID = null;
	
	promise.success(function(data) {
		console.log("attempt: " + data.success)
		userID = data;
	});
    
    // if user has session, set the current user, if not, go back to sign in page
    if(userID >= 0){
        // set the current user
        setCurrentUser(userID);
        if(window.location.href == "http://beta.helpkonnect.com"){ // window.location.href = "http://www.helpkonnect.com";
            console.log("Going to home");
            $.mobile.changePage("#home");
        }
    }
    else{
        // go back to home page sign in
        console.log(userID);
        //window.location.href = "http://www.helpkonnect.com";
        window.location.href = "http://beta.helpkonnect.com";
    }
}

function getCurrentUser()
{     
	return currentUser;
}

function obtainLocation() {
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(locationSuccess, locationError);
	} else {
		alert("Geolocation is not supported by this browser.");
	}
}

function locationSuccess(location) {
	console.log("location: " + location);
	
	currentUser.Latitude=location.coords.latitude;
	currentUser.Longitude=location.coords.longitude;
	
	updateUser(currentUser.UserID, null, currentUser.FirstName, currentUser.LastName, currentUser.Email, currentUser.Phone, location.coords.latitude, location.coords.longitude, currentUser.UsePayPal, currentUser.PayPalEmail, currentUser.EmailAlert, currentUser.SMSAlert);
	alert("Your location has been updated successfully!");
}

function locationError(error) {
	switch(error.code) {
		case error.PERMISSION_DENIED:
			alert("User denied the request for Geolocation.");
			break;
		case error.POSITION_UNAVAILABLE:
			alert("Location information is unavailable.");
			break;
		case error.TIMEOUT:
			alert("The request to get user location timed out.");
			break;
		case error.UNKNOWN_ERROR:
			alert("An unknown error occurred.");
			break;
	}
	
	alert("Your location update has failed! Please try again.");
}

function obtainLocationFromInitialization() {
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(locationSuccessFromInitialization, locationError);
	} else {
		alert("Geolocation is not supported by this browser.");
	}
}

function locationSuccessFromInitialization(location) {<commit_msg>Added comments to the user.js file<commit_after>/**
 * @author PerronJones
 */

var currentUser;

/*
 * sets the current user for use throughout the application
 */
function setCurrentUser(userID){
	
	//check if user exist. if the user does not exist, then create the user. 
	//necessary fields needed for creation (firstName, lastName, email, phone, latitude, longitude)
	//If the user does exist, then set the current user.
	//Also, update the position.
	console.log("user.js(setCurrentUser): userID: " + userID);
	var user = readUser(userID);
	//var user = readUser(1232);
	
	if(user != null){
		console.log("user exists");
		console.log(user);
		
        //set session User ID
        $.ajax({
			url : "sess",
			data : {
				'UserID' : userID

			},
			context : document.body,
			async : false,
			type : 'POST',
			dataType : "json",
			success : function(data) {
				console.log("Data Success");
				console.log(data);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				console.log("Status: " + textStatus);
				console.log("Error: " + errorThrown);
			}
		});
        
		//update position
		currentUser = user;
		//obtainLocation(); //The position is updated automatically when this method is called.
		obtainLocationFromInitialization();
	}
	else{
		console.log("user does NOT exist");
		console.log(user);
	}
	
}

/*
 * refreshes the current user if the page is reload
 */
function refreshCurrentUser()
{
    console.log("inside refreshCurrentUser");
    // will return -1 if there is no user session
    var promise = $.ajax({
		url : "sess",
		context : document.body,
		async : false,
		type : 'GET',
		dataType : "json",
		success : function(data) {
            console.log("Data Success");
            console.log(data);
		},
		error : function(jqXHR, textStatus, errorThrown) {
            console.log("Status: " + textStatus);
            console.log("Error: " + errorThrown);
		}
	});
    
    // get the returned user ID
	var userID = null;
	
	promise.success(function(data) {
		console.log("attempt: " + data.success)
		userID = data;
	});
    
    // if user has session, set the current user, if not, go back to sign in page
    if(userID >= 0){
        // set the current user
        setCurrentUser(userID);
        if(window.location.href == "http://beta.helpkonnect.com"){ // window.location.href = "http://www.helpkonnect.com";
            console.log("Going to home");
            $.mobile.changePage("#home");
        }
    }
    else{
        // go back to home page sign in
        console.log(userID);
        //window.location.href = "http://www.helpkonnect.com";
        window.location.href = "http://beta.helpkonnect.com";
    }
}

/*
 * returns the current user
 */
function getCurrentUser()
{     
	return currentUser;
}

/*
 * obtains the user's location using HTML5's geolocation
 */
function obtainLocation() {
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(locationSuccess, locationError);
	} else {
		alert("Geolocation is not supported by this browser.");
	}
}

/*
 * sets the current user's location upon the successful gathering of location
 */
function locationSuccess(location) {
	console.log("location: " + location);
	
	currentUser.Latitude=location.coords.latitude;
	currentUser.Longitude=location.coords.longitude;
	
	updateUser(currentUser.UserID, null, currentUser.FirstName, currentUser.LastName, currentUser.Email, currentUser.Phone, location.coords.latitude, location.coords.longitude, currentUser.UsePayPal, currentUser.PayPalEmail, currentUser.EmailAlert, currentUser.SMSAlert);
	alert("Your location has been updated successfully!");
}

/*
 * alerts upon failed gathering of location
 */
function locationError(error) {
	switch(error.code) {
		case error.PERMISSION_DENIED:
			alert("User denied the request for Geolocation.");
			break;
		case error.POSITION_UNAVAILABLE:
			alert("Location information is unavailable.");
			break;
		case error.TIMEOUT:
			alert("The request to get user location timed out.");
			break;
		case error.UNKNOWN_ERROR:
			alert("An unknown error occurred.");
			break;
	}
	
	alert("Your location update has failed! Please try again.");
}

/*
 * obtains the user's location when the home page is being initialized
 */
function obtainLocationFromInitialization() {
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(locationSuccessFromInitialization, locationError);
	} else {
		alert("Geolocation is not supported by this browser.");
	}
}

function locationSuccessFromInitialization(location) {
{'fraction_non_alphanumeric': 0.12621584066697544, 'fraction_numerical': 0.0015053265400648448, 'mean_word_length': 3.3423831070889896, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3009832', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2584, 'n_words': 686}