///// !!! GLOBAL FUNCTIONS !!! /////



// CHECKS THAT STRINGS ARE JSON
function isJSON(item) {

// 	item = (typeof item !== "string")
// 		? JSON.stringify(item)
// 		: item;

    try {
        item = JSON.parse(item);
    } catch (e) {
        return false;
    }

    return (typeof item === "object" && item !== null);

}



// MODAL SWITCHING FUNCTION (home.php & viewExperience.php)
function nextModal(name) {
	$('[data-toggle="tooltip"]').tooltip('hide');
	setTimeout(() => $(name).modal('show'), 100);
}



// Disable 'enter' key from submitting forms
// $('form').keypress(function(event){ if (event.which == '13') { event.preventDefault(); } } );
// $('[form]:not(button[form])').keypress(function(event){ if (event.which == '13') { event.preventDefault(); } } );



// NUMBER INPUT SANITIZATION (home.php & viewExperience.php)
$('input[type="number"]').on('change keyup', () => {

	// Remove invalid characters
	var sanitized = $(this).val().replace(/[^0-9]/g, '');
	
	// Update value
	$(this).val(sanitized);
	
});



// DATE INPUT DATEPICKER (home.php & viewExperience.php)
// Taken from: http://www.javascriptkit.com/javatutors/createelementcheck2.shtml
// If browser doesn't support input type="date", load files for jQuery UI Date Picker
let testDateField = document.createElement("input");

testDateField.setAttribute("type", "date");

if(testDateField.type != "date") {
	
	// First load the CSS ("base" theme) for the calendar
	document.write('<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />\n');
	
	// Then load the calendar functionality
    document.write('<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>\n');
   	
   	// Initialize date picker widget (on document.ready)
   	// Help: https://jqueryui.com/datepicker/#min-max
   	window.onload = () => {
   		if($('input[type="date"]').length !== 0) {
	   		jQuery(($) => $('input[type="date"]').datepicker({ minDate: 0, maxDate: "+1Y"}));
   		}
	}
	
}



// FOOTER COPYRIGHT YEAR
let date = new Date().getFullYear();
document.getElementById('copyrightDate').innerHTML = date;














// !!! FROM HERE ON DOWN, THERE'S ONLY OLD CODE !!! //



// Declaration and initialization of the "naming counter" 
var selectionCounter = 2;

// WHEN WANTING TO ADD MULTIPLE QUESTIONNAIRES TO AN EXPERIENCE (newExperience.php and editExperience.php)
function AddQuestionnaire() {

    // We add two linebreaks so that there is space between each dropdown list
    var br = document.createElement('br');
    document.getElementById('addQuestionnaires').appendChild(br);
    br = document.createElement('br');
    document.getElementById('addQuestionnaires').appendChild(br);
        
    // We create a copy of the dropdown list we already have, so multiple questions can be added at the same time
    var select = document.getElementById('questionnaire');
    var clone = select.cloneNode(true);
        
    // The clones will have the same name as the original but with a number that will distinguish them
    var name = select.getAttribute('name') + selectionCounter++;
    clone.id = name;
    clone.setAttribute('name', name);
    document.getElementById('addQuestionnaires').appendChild(clone);
    //document.getElementById("demo").innerHTML = selectionCounter;
}

// Reinitialization of the "naming counter"
selectionCounter = 2;

// WHEN WANTING TO ADD MULTIPLE QUESTIONS TO A QUESTIONNAIRE (newQuestionnaire.php and editQuestionnaire.php)
function AddQuestion() { 
    
    // We add a linebreak so that there is space between each dropdown list
    var br = document.createElement("br");
    document.getElementById("addQuestions").appendChild(br)
    var br2 = document.createElement("br");
    document.getElementById("addQuestions").appendChild(br2)

    // We create a copy of the dropdown list we already have, so multiple questions can be added at the same time.
    var select = document.getElementById("question");
    var clone = select.cloneNode(true);

    // The clones will have the same name as the original but with a number that will distinguish them
    var name = select.getAttribute("name") + selectionCounter++;
    clone.id = name;
    clone.setAttribute("name", name)
    document.getElementById("addQuestions").appendChild(clone)
    //document.getElementById("demo").innerHTML = selectionCounter;  	
}

