var prodKey = "Dyd7kC4wxLDFz0rQ6W5T28DPgrM6SOBe";
var serviceURL = "https://decent-destiny-704.appspot.com/laxservices/";
//var serviceURL = "https://la-crosse-alerts.uc.r.appspot.com/laxservices/";
var urlLoc = window.location.protocol + "//" + window.location.host;
if ( urlLoc.indexOf("localhost") > -1 )
var serviceURL = "http://" + window.location.host + "/laxservices/";
jQuery(document).ready(function($){
$("#BuyNow").hide();
$("#formLogin").on( 'submit' , pseudoLogIn );
$("#years").on('change', buttonSwap );
$(document).on('click', '.box', function(){
var $this = $(this);
if ($this.is(':checked')) {
$(this).addClass("checkmark");
} else {
$(this).removeClass("checkmark");
}
update();
});
$("#years").val("3");
buttonSwap();
})
function update() {
deviceList = "";
devices = 0;
string = " Device";
$(".checkmark").each(function() {
deviceList += $( this ).val() + ";";
devices += 1;
});
if (devices > 1)
string += "s";
$("#priceSelect1y").val(devices + string);
$("#priceSelect3y").val(devices + string + " 3y");
yearsVal = $("#years").val();
$(".DevsToBuy").val(deviceList + yearsVal);
years = $("#years option[value='" + yearsVal + "']").text();
yearsVal2 = "";
if (yearsVal != 1) {
yearsVal2 = " " + yearsVal + "y";
}
yearsVal += "y";
textVal = $("#priceSelect" + yearsVal + " option[value='" + devices + string + yearsVal2 + "']").text();
console.log("textVal"+textVal);
console.log("yearsVal"+yearsVal);
console.log("devices"+devices);
console.log("string"+string);
console.log("yearsVal2"+yearsVal2);
$("#priceTag").html( "Purchase " + years + " per device for " + textVal);
if (devices >= 5) {
$(".box").not(".checkmark").each(function() {
$(this).attr("disabled", true);
$("#limit").show();
});
} else {
$(".box").removeAttr("disabled");
$("#limit").hide();
}
if (devices == 0) {
$("#sub").hide();
} else {
$("#sub").show();
}
}
function buttonSwap ( event ) {
yearsVal = $("#years").val();
$("#1yBuy").hide();
$("#3yBuy").hide();
$("#" + yearsVal + "yBuy").show();
update();
}
function pseudoLogIn ( event ) {
var userEmail = String($("#iLogEmail").val());
var dataString = "pkey=" + prodKey + "&action=getDevForUser";
passform = $("#formLogin");
if (userEmail.length > 0) {
rawdata = passform.serialize();
$.post( serviceURL + "user-api.php?" + dataString, rawdata)
.done(function( response ) {
if (response && response.sensors && response.sensors.length >= 1) {
boxes = "";
$('#welcome').text(
"Welcome back " +
response.first_name.charAt(0).toUpperCase() + response.first_name.substring(1) + " " +
response.last_name.charAt(0).toUpperCase() + response.last_name.substring(1) + "!");
$('#Devices').empty();
for(var i = 0; i < response.sensors.length; i++) {
devinfo = String(response.sensors[i].value);
boxes += ' ' + String(response.sensors[i].text) + '
'
$("#pseudoLogin-warning").text("Success").show();
var res = response.sensors[i].value;
$("#ref").val(res.substring(0, res.indexOf("-")));
}
$("#Devices").html(boxes);
$("#loginBlock").hide();
$("#codeEntry").show();
} else {
$("#codeEntry").hide();
$("#pseudoLogin-warning").text("No devices were found for the given account. ensure the credentials are correct").show();
}
})
.fail(function(){
alert('a server error occured, please try again in a few seconds.');
});
}
event.preventDefault();
}