function btnLoad(clsList){
if(clsList.contains('btnLoading') == true){
setTimeout(function(){ clsList.remove('btnLoading'); }, 250, clsList);
} else {
clsList.add('btnLoading');
}
}
function prepForSOM(params, callbackId, url, action, target, srcElemId){
//console.log('params' + ' : ' + params);
//console.log('callbackId' + ' : ' + callbackId);
//console.log('url' + ' : ' + url);
//console.log('action' + ' : ' + action);
//console.log('target' + ' : ' + target);
//console.log('srcElemId' + ' : ' + srcElemId);
if (url == '') { //url is optional
var url = 'inv/jarvisPFCortex.php';
}
if (typeof target === 'undefined') { //target is optional
var target = '';
}
if (typeof srcElemId === 'undefined') { //srcElemId is optional, but useful if you want something else to be the source element - like an element in a div inherits the onclick, but you want the container div to be the source element
if (typeof event !== 'undefined') {
if(typeof event.target.id !== 'undefined'){
if(event.target.id == ''){
alert('need to set an ID on the element that called the event');
var srcElem = '';
} else {
var srcElemId = event.target.id;
}
}
}
}
if (typeof action === 'undefined') {
action = '';
} else if(action == 'btnSubmit'){
var srcElem = document.getElementById(srcElemId);
if(srcElem.nodeName == 'BUTTON'){
changeCSS(srcElem, 'btnLoading', 'add');
} else {
alert('didn\'t pass a button ID');
}
}
//---------------------constructor----------------------
var obj = {};
obj.url = url; //defaults to inv/jarvisPFCortex.php.
obj.params = params; //alla värden
obj.srcId = srcElemId; //vad som skickade det
obj.targetId = target; //vart innehållet ska hamna
obj.callbackId = callbackId; //vad transaktionen ska heta
obj.action = action; //action name
obj.params = 'callbackId=' + callbackId + '&' + obj.params;
if(obj.action != ''){
obj.params = obj.params + '&action=' + obj.action;
}
sendToSOM(obj);
}
function sendToSOM(obj){
//console.log(obj);
if (window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
} else {
xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
}
xmlHttp.open('POST', obj.url, true);
xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200){
// console.log('url: ' + obj.url + ' state: ' + xmlHttp.readyState + ' status: ' + xmlHttp.status);
if(obj.action == 'feedback'){
obj.msg = xmlHttp.responseText;
jarvix.responseOBJ = obj;
jarvix.bgTasks.status = 'processing';
prepForJarvis('&name='+obj.callbackId, 'doorman', '', 'doorman');
} else if(obj.action == 'doorman'){
jarvix.bgTasks.status = 'finalizing';
} else {
loadSuccess(obj.callbackId, obj.action, xmlHttp.responseText, obj.srcId, obj.targetId);
}
} else if (xmlHttp.status == 403) {
onlyChangeClassIfTrue(document.getElementById('reLoginModal').classList, 'hiddenModal', true);
} else if (xmlHttp.status == 500) {
alert('something didn\'t go as planned');
}
}
}
xmlHttp.send(obj.params);
}
function loadSuccess(callbackId, action, msg, srcId, targetId){
//----------------------checking data--------------------
//----------------------limit to section specific--------------------
console.log('callbackId: '+ callbackId);
console.log('action: '+ action);
console.log('msg: '+ msg);
console.log('from: '+ srcId);
console.log('target: '+ targetId);
var targetElem = document.getElementById(targetId);
if(callbackId == 'signUp'){
if(msg != 'error'){
document.getElementById('customModal').innerHTML = msg;
}
}
if(callbackId == 'addselectOption'){
if(msg != 'error'){
document.getElementById(targetId).innerHTML = msg;
document.getElementById(srcId).value = '';
document.getElementById(srcId).style.display = 'none';
}
}
if(callbackId == 'udatStaffing'){
document.getElementById(srcId).classList.remove('addedOk');
}
if(callbackId == 'signUpCHouse'){
if(msg != 'error'){
document.getElementById('sigupCompanyStuff').innerHTML = msg;
}
}
if(callbackId == 'forgotMyEmail' || callbackId == 'signUpRegister'){
if(msg != 'failed'){
if(callbackId == 'forgotMyEmail'){
toggleCSS(document.getElementById('customModal'), 'hiddenModal');
}
prepForSOM(msg, 'Smurfar', 'https://wac.som.org.uk/kom_api_em.php');
}
}
if(callbackId == 'newPw'){
document.getElementById('uidDivven').innerHTML = 'Thank you for updating your password.
You can now login using the password you just entered.
Please make sure to store it safely and securely.
Thanks again!';
}
if(callbackId == 'newStaffing'){
if(msg != 'error'){
document.getElementById('dataTbl').innerHTML = msg;
}
}
if(callbackId == 'editCHouse'){
if(msg != 'error'){
document.getElementById('companyHouseData').innerHTML = msg;
}
}
if(callbackId == 'editProvider'){
// if(msg == 'ok'){
onlyChangeClassIfTrue(document.getElementById('editOrganisationSubmitBtn').classList, 'btnLoading', true);
// }
}
if(callbackId == 'login'){
if(msg == 'Login successful'){
onlyChangeClassIfTrue(document.getElementById('loginButton').classList, 'btnLoading', true);
onlyChangeClassIfTrue(document.getElementById('loginError').classList, 'hidden', false);
window.top.location.href = 'welcome.php';
} else {
onlyChangeClassIfTrue(document.getElementById('loginButton').classList, 'btnLoading', true);
onlyChangeClassIfTrue(document.getElementById('loginError').classList, 'hidden', true);
onlyChangeClassIfTrue(document.getElementById('loginError').classList, 'hide', true);
}
}
if(callbackId == 'deleteEmail' || callbackId == 'deleteWorkload' || callbackId == 'deleteUser' ){
if(msg != 'error'){
document.getElementById('dataTbl').innerHTML = msg;
}
}
if(callbackId == 'addEmail'){
if(msg != 'error'){
document.getElementById('dataTbl').innerHTML = msg;
document.getElementById('email_topic').value = '';
document.getElementById('email_message').value = '';
toggleCSS(document.getElementById('customModal'), 'hiddenModal');
}
}
if(callbackId == 'editEmail'){
if(msg != 'error'){
document.getElementById('dataTbl').innerHTML = msg;
document.getElementById('edit_email_topic').value = '';
document.getElementById('edit_email_message').value = '';
toggleCSS(document.getElementById('customModal'), 'hiddenModal');
}
}
if(callbackId == 'deleteStaffApplication' || callbackId == 'acceptStaffApplication'){
if(msg != 'error'){
document.getElementById('staffApplic').innerHTML = msg;
}
}
if(callbackId == 'prepAddEmail'){
if(msg != 'error'){
document.getElementById('customModal').innerHTML = msg;
}
}
if(callbackId == 'remindPw'){
if(msg != 'error'){
document.getElementById('customModal').innerHTML = msg;
}
}
if(callbackId == 'csv_upl_workload' || callbackId == 'csv_upl_staffing'){
if(msg != 'error'){
document.getElementById('dataTbl').innerHTML = msg;
document.getElementById('csvUploadUserfileToUpload').value = '';
}
}
if(callbackId == 'prepEditEmail' || callbackId == 'prepEditStaffing' || callbackId == 'prepEditAmmending'){
if(msg != 'error'){
document.getElementById('customModal').innerHTML = msg;
}
}
if(callbackId == 'prepAddUser' || callbackId == 'prepEditUser' || callbackId == 'prepEditsomUser' || callbackId == 'prepStaffing' || callbackId == 'prepWorkload' || callbackId == 'prepEditWorkload'){
if(msg != 'error'){
document.getElementById('customModal').innerHTML = msg;
}
}
if(callbackId == 'addsomStaffing' || callbackId == 'deleteStaffing' || callbackId == 'editsomStaffing' || callbackId == 'editsomAmmending'){
if(msg != 'error'){
document.getElementById('dataTbl').innerHTML = msg;
onlyChangeClassIfTrue(document.getElementById('customModal').classList, 'hiddenModal', false);
}
}
if(callbackId == 'addsomUser' || callbackId == 'deletesomUser' || callbackId == 'editsomUser'){
if(msg != 'error'){
document.getElementById('dataTbl').innerHTML = msg;
onlyChangeClassIfTrue(document.getElementById('customModal').classList, 'hiddenModal', false);
}
}
if(callbackId == 'addsomUser'){
prepForSOM('', 'addedUserEmail', 'inv/dbData.php');
//send email thingy
prepForSOM(msg, 'Smurfar', 'https://wac.som.org.uk/kom_api_em.php');
}
if(callbackId == 'addsomWorkload' || callbackId == 'editsomWorkload'){
if(msg != 'error'){
document.getElementById('dataTbl').innerHTML = msg;
onlyChangeClassIfTrue(document.getElementById('customModal').classList, 'hiddenModal', false);
}
}
if(callbackId == 'editsomUser' || callbackId == 'addsomUser'){
prepForSOM('', 'updateMenu', 'inv/dbData.php');
}
if(callbackId == 'updateMenu'){
document.getElementById('menuBar').innerHTML = msg;
}
}
function onlyChangeClassIfTrue(clsLst, className, bolTrueFalse){
if(bolTrueFalse == true){
if(clsLst.contains(className) == true){
setTimeout(function(){ clsLst.remove(className); }, 100, clsLst);
}
} else {
clsLst.add(className);
}
}
//-------------------------------------validate input--------------------------------------
function clearRequiredInput(elem, className){
if(elem.offsetParent != null){
if(elem.classList.contains(className) == true){
elem.classList.remove(className);
}
}
}
function validateRequiredInput(elem, className){
//console.log(elem.offsetParent);
if(elem.value == '' && elem.offsetParent != null){
if(elem.classList.contains(className) == false){
elem.classList.add(className);
}
}
}
function validateInputFields(elem, className){
if(elem.offsetParent != null){
if(elem.type == 'email'){
if(validateEmail(elem.value) == false){
elem.classList.add(className);
}
}
}
}
function somValidate(){
//clear input
var validateReqElems = document.getElementsByClassName('somRequired');
var validateInpElems = document.getElementsByClassName('somValidate');
for(var i = 0; i < validateReqElems.length; i++){
clearRequiredInput(validateReqElems[i], 'somInvalidInput');
}
for(var i = 0; i < validateInpElems.length; i++){
clearRequiredInput(validateInpElems[i], 'somInvalidInput');
}
//validate input
for(var i = 0; i < validateReqElems.length; i++){
validateRequiredInput(validateReqElems[i], 'somInvalidInput');
}
for(var i = 0; i < validateInpElems.length; i++){
validateInputFields(validateInpElems[i], 'somInvalidInput');
}
//return if it passed
var retBool = false;
var isValidationOk = document.getElementsByClassName('somInvalidInput');
if(isValidationOk.length == 0){
retBool = true;
} else {
console.log(isValidationOk);
}
return retBool;
}
function isURL(string) {
var res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
return (res !== null)
}
function validateEmail(email) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if( !emailReg.test( email ) ) {
return false;
} else {
return true;
}
}
function back_2nd_form(){
document.getElementById('sginpone').style.display = 'none';
document.getElementById('sginptwo').style.display = 'block';
document.getElementById('sigupPartOne').style.display = 'block';
document.getElementById('sigupPartTwo').style.display = 'none';
document.getElementById('signupTitleH3').innerHTML = 'Signup (2 of 3)';
}
function toggleCSS(elem, CSSclass){
if(elem.classList.contains(CSSclass) == true){
elem.classList.remove(CSSclass);
} else {
elem.classList.add(CSSclass);
}
}
function uploadCsv(upldFile, callb){
csvCallBack = callb;
var reader = new FileReader();
reader.readAsText(upldFile);
reader.onload = uploadCSVHandler;
}
function uploadCSVHandler(event) {
processCSVData(event.target.result, csvCallBack);
}
function processCSVData(upldFile){
upldFile = upldFile.split(/\r?\n|\r/g);
var sendStr = '';
var arr = new Array();
for(var i = 15; i < upldFile.length; i++){
if(upldFile[i] != ''){
var repString = '';
var strTracer = '';
for(var j = 0; j < upldFile[i].length; j++){
if(upldFile[i][j] == '"'){
if(strTracer == ''){
strTracer = 'start';
} else {
strTracer = '';
}
}
if(upldFile[i][j] == ',' && strTracer == 'start'){
repString = repString + '|';
} else {
repString = repString + upldFile[i][j];
}
}
for(var j = 0; j < 5; j++){ //removes up to 5 blank spaces near the new seperator
repString = repString.replace(' |', '|');
repString = repString.replace('| ', '|');
}
arr[arr.length] = repString.split(',');
}
}
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < arr[i].length; j++){
if(arr[i][j].substr(0,1) == '"'){
arr[i][j] = arr[i][j].substr(1,arr[i][j].length - 2);
}
sendStr = sendStr + '&i' + i + '_j' + j + '=' + arr[i][j];
}
}
prepForSOM('&i=' + arr.length + '&j=' + arr[0].length + sendStr, csvCallBack, 'inv/dbData.php');
}
function addToHidden(idCode){
var str = '';
var idCol = document.getElementsByClassName('chkBox'+idCode);
for(var i = 0; i < idCol.length; i++){
if(idCol[i].checked == true){
if(str != ''){
str = str + '|';
}
str = str + idCol[i].value;
}
}
document.getElementById(idCode).value = str;
}