/*
* register.js
*/
/**
* scrive in html se non passo la classe si intende rosso, se no "green"
* @param {type} htmlText
* @param {type} classeTipo
* @returns {undefined}
*/
function my_alert(htmlText, classeTipo) {
$("#my_alertMessage").removeClass("green")
$("#my_alertMessage").html(htmlText)
if (classeTipo != undefined) {
$("#my_alertMessage").addClass(classeTipo)
}
$("#my_alertMessage").show()
}
/**
* esistono #captcha-n1, #captcha-n2 e #captcha-ope #captcha-result e #captcha-label
* numeri interi tra 1 e 9 e + - *
* @returns {undefined}
*/
function captchaPopulate() {
var n1 = parseInt(Math.random() * 9) + 1
var n2 = parseInt(Math.random() * 9) + 1
var ope = parseInt(Math.random() * 3)
var operazioni = ["+", "*", "-"]
$("#captcha-n1").text(n1)
$("#captcha-n2").text(n2)
$("#captcha-ope").text(operazioni[ope])
}
function captchaCheck() {
var n1 = $("#captcha-n1").text()
var n2 = $("#captcha-n2").text()
var ope = $("#captcha-ope").text()
var result = String($.trim($("#captcha-result").val()))
var resultOk = String(eval(n1 + ope + n2))
console.log(result, resultOk, result === resultOk)
if (result !== resultOk) {
my_alert("captcha control fails!
Enter a right answer, like " + resultOk)
$("#captcha-result").css("border-color", "red");
$("#captcha-label").removeClass("captcha-pass")
return false;
} else {
$("#captcha-label").addClass("captcha-pass")
$("#captcha-result").css("border-color", "initial");
return true;
}
}
$(document).ready(function () {
$("#name").focus()
var appInfo = new Object();
var gwInfo = new Object();
appInfo = getAppInfo();
gwInfo = getGatewayInfo();
$("#content").show();
$("#btnRegister").show();
$("#result").text("");
//console.log(appInfo);
//console.log(gwInfo);
var register = function () {
var userData = new Object();
var dest;
var mitt = dest = "000000000000";
var error = false;
var name = "";
var surname = "";
var address = "";
var city = "";
var prov = "";
var cap = "";
var state = "";
var email = "";
var email2 = ""; // Not used
var phone1 = "";
var phone2 = ""; // Not used
userData.name = $("#name").val();
userData.surname = $("#surname").val();
userData.address = $("#address").val();
userData.city = $("#city").val();
userData.prov = $("#prov").val();
userData.cap = $("#cap").val();
userData.state = $("#state").val();
userData.email = $("#email").val();
userData.email2 = $("#email").val();
userData.phone1 = $("#phone1").val();
userData.phone2 = $("#phone1").val();
userData.password = $("#password").val();
userData.repassword = $("#rePassword").val();
userData.macAddress = $("#macAddress1").val() + "-" + $("#macAddress2").val() + "-" + $("#macAddress3").val();
userData.macAddress1 = $("#macAddress1").val();
userData.macAddress2 = $("#macAddress2").val();
userData.macAddress3 = $("#macAddress3").val();
userData.productId = $("#productId").val();
console.log(showProp(userData))
if (userData.name == "") {
error = true;
showError("name");
return false
}
;
if (userData.surname == "") {
error = true;
showError("surname");
return false
}
;
if (userData.email == "") {
error = true;
showError("email");
return false
}
if (!isValidEmail(userData.email)) {
error = true;
showError("email");
return false;
}
if (userData.password == "") {
error = true;
showError("password");
return false
}
if (userData.repassword == "") {
error = true;
showError("rePassword");
return false
}
if (userData.password != userData.repassword) {
error = true;
showError("password");
return false
}
if ($.trim(userData.productId).length < 6) { //lunghezza productId alfanumerico 6 caratteri
error = true;
showError("PID");
return false
}
if (!checkMacAddress(userData.macAddress1, userData.macAddress2, userData.macAddress3, userData.productId)) {
error = true;
showError("macAddress");
return false
}
if (!captchaCheck()) {
return false;
}
//console.log("fine controlli")
if (!error) { //viene chiamato il comando 300 ma del newManageReq che da non abilitati
var postData = JSON.stringify(createReqArray(mitt, dest, 1300, 1, userData));
var urlManageReq = "../common/manageReq.php" //punta a quello esterno protetto da pw
xhr = $.ajax({
url: urlManageReq,
data: postData,
type: "POST",
dataType: "JSON"
});
console.log("invio a " + urlManageReq, postData)
xhr.done(function (data) {
console.log("ritorno", data)
$("#content").hide();
$("#btnRegister").hide();
if (data[1].st == 0) {
my_alert("Exception");
} else if (data[1].st == 1) {
my_alert("User registered successfully! Wait for the approval of the technical support.", "green");
} else if (data[1].st == 2) {
my_alert("Missing param error");
} else if (data[1].st == 3) {
my_alert("No data found");
} else if (data[1].st == 4) {
my_alert("JSON syntax error");
} else if (data[1].st == 5) {
my_alert("Command not permitted");
} else if (data[1].st == 6) {
my_alert("User already registered!");
} else {
my_alert("Unknow exception")
console.log(data)
}
});
xhr.fail(function (jqXHR, textStatus) {
my_alert("error fail
" + textStatus)
console.log(jqXHR)
});
xhr.always(function (data) {
console.log("always:", data)
});
}
return error;
};
$(".form-control").change(function () {
$(this).css("border-color", "initial");
$("#my_alertMessage").hide();
})
var showError = function (divId) {
console.log("showError")
//$("#" + divId).append(" *");
$("#" + divId).css("border-color", "red").focus();
my_alert("Error
" + divId + ": the value of this field is null or invalid!")
};
$("#btnSubmit").click(function (event) {
$(".spanError").remove();
register();
});
captchaPopulate()
});
$(function () {
$("input[autocomplete=off]").css("color", )
setTimeout(function () {
console.log("svuoto campi..")
$("input[autocomplete=off]").val("")
}, 500)
setTimeout(function () {
console.log("svuoto campi..")
$("input[autocomplete=off]").val("")
}, 1000)
})