selectedCurrency = "USD";
selectedCountry = "USA";
formArray = new Array();
flag = 0;
var customItemCount = 1;
var finalCustomItemCountArray = [1];
var alreadySubscribedFlag = 0;
var productPlanCurrency = null;
var displayProductPlan = false;
var subscriptionFrequency = 1;
var subscriptionStartDate;
var basicpackageCustomer = false;
var trialAccount = false;
var accountStatusId = "Payee";
currencyArray = {
"EUR" : "€",
"AUD" : "A$ ",
"USD" : "$",
"GBP" : "£",
"INR" : "₹"
};
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
// the typeahead jQuery plugin expects suggestions to a
// JavaScript object, refer to typeahead docs for more info
matches.push({ value: str });
}
});
cb(matches);
};
};
//*These functions are init functions of various pages *//
function initPricing(){
loadAllProductPlanData();
}
function initCompare(){
loadAllProductPlanData();
}
function initConfirm(){
loadAllCountryPrice();
}
function initBuy(currency, plan, isAlreadySubscribed){
productPlanCurrency = currency;
alreadySubscribedFlag = isAlreadySubscribed;
addRemoveInboundDiv();
loadSMSPackData(currency);
loadIncomingPriceData(currency);
loadProductPlanData(currency, plan ,isAlreadySubscribed);
if(accountStatusId == 'expired' || accountStatusId == 'trial')
{
displayCustomMessages();
}
}
function initSMSCost(){
loadTopCountryPrice();
getCurrencyConversionFactors();
loadTopIncomingPrice();
loadAllCountryPriceForSMSCostPage();
loadAllIncomingCountryPrice();
}
function initTopup(){
getCurrencyConversionFactors();
loadAllCountryPrice(); // for outgoing price and auto complete dropdown
loadAllIncomingCountryPrice();
loadSMSPackData("USD", 1);
}
/*This function is called on pricing and compare page on change of currency */
function updateProductPlanPrice(currency){
response = productPlanData[currency];
selectedCurrency = currency;
if(selectedCurrency == "INR"){
var plan1Price = response[0].price * 12;
var plan2Price = response[1].price * 12;
var plan3Price = response[2].price * 12;
}else{
var plan1Price = response[0].price;
var plan2Price = response[1].price;
var plan3Price = response[2].price;
}
$(".currencyButtons").removeClass("btn-primary");
$(".currencyButtons").addClass("btn-default");
$("#"+currency).addClass("btn-primary");
$(".currencyButtons").addClass("outlined");
$("#"+currency).removeClass("outlined");
var currencySign = currencyArray[currency];
$("#plan_1_currency").html(currencySign);
$("#plan_1_price").html(numerToPrice(plan1Price));
$("#plan_2_currency").html(currencySign);
$("#plan_2_price").html(numerToPrice(plan2Price));
$("#plan_3_currency").html(currencySign);
$("#plan_3_price").html(numerToPrice(plan3Price));
if(selectedCurrency == "INR"){
//document.getElementById('selectCurrencyINRDiv').style.display="block";
$('.currencydiv').show();
$('.selectCurrencyOTHERDiv').hide();
}else{
$('.currencydiv').hide();
$('.selectCurrencyOTHERDiv').show();
}
}
/*This function is called on SMS Cost page on change of currency */
function convertPricingSMS(currency){
selectedCurrency = currency;
var convFactor = factorArray[currency];
generateHtml("newTopCountryPriceDiv",topCountryList , convFactor,currency);
generateHtml("allCountryPriceDiv",countryList , convFactor,currency);
generateIncomingHtml("topIncomingCountryPriceDiv",topIncomingPriceData,1, convFactor , currency);
generateIncomingHtml("allIncomingCountryPriceDiv",incomingPriceData,0, convFactor , currency);
calculateCost( convFactor , currency);
}
/*This function is called on Topup page on change of currency */
function convertPricingTopup(currency){
selectedCurrency = currency;
document.getElementById('selectCountry').disabled = false;
if(currency == "INR"){
$('#selectCountry').val("India");
showCountryPrice("India" ,"INR");
var dropdown = $('select[name="selectCountry"]');
dropdown.prop('disabled', true); //disable IMEI select
}
generateSMSPackHtmlForTopupPage(smsPackPriceData, currency);
showCountryPrice(selectedCountry ,currency);
}
/*This function is called on Inbound page on change of currency*/
function convertPricingInbound(currency){
selectedCurrency = currency;
var inboundCountry = $( "#inboundCountry option:selected" ).text();
var params = {
"currency" : currency,
"countryName" : inboundCountry
};
$.post("/pricingajaxapi/getInboundCountryPricing", params, function(data){
var responseData = eval('(' + data + ')');
var price = responseData['price'];
var amount = price+" "+currencyArray[currency]+" "+"Per Month";
$('#priceInbound').html(amount);
$('#csrfField').attr('name', csrfInputName);
$("#csrfField").val(csrfTokenValue);
$("#inboundCurrency").val(currency);
loadIncomingPriceData(currency);
var pricePerNumber = incomingCountryPriceArray[inboundCountry];
var dataArr = pricePerNumber.split( '-' );
$("#inboundNumberProductItemId").val(dataArr[1]);
});
$(".currencyButtons").addClass("outlined");
$("#"+currency).removeClass("outlined");
$(".currencyButtons").removeClass("btn-primary");
$(".currencyButtons").addClass("btn-default");
$("#"+currency).addClass("btn-primary");
//generateSMSPackHtmlForTopupPage(smsPackPriceData, currency);
}
/* This function creates country Array, country price array and country flag array for incoming and price per sms cost */
function createCountryPriceArray(){
countryArray = new Array();
countryPriceArray = new Array();
countryFlagArray = new Array();
countryLocalPriceArray = new Array();
for(var i=0; i< countryList.length; i++){
var countryName = countryList[i].countryName;
var price = countryList[i].price;
var flagName = countryList[i].flagName;
var localprice = countryList[i].localprice;
countryArray.push(countryName);
countryLocalPriceArray[countryName] = localprice;
countryPriceArray[countryName] = price;
countryFlagArray[countryName] = flagName;
}
}
/* This function loads all countries to auto complete country dropdown. */
function loadAutoCompleteCountryList(){
var states = countryArray;
$('#scrollable-dropdown-menu .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
displayKey: 'value',
source: substringMatcher(states),
updater:function (item) {
}
});
}
/*This function adds and removes inbound number country-price row on buy page on click of add country and delete last item buttons. */
function addRemoveInboundDiv()
{
$("#addIncoming").click(function ()
{
var countryName = $('select[name=incomingInboundCountry]').val();
var addIncomingNumber = false;
if(accountStatusId == "Payee" || !accountStatusId)
{
addIncomingNumber = true;
}
var len = formArray.length;
for (var i =0; i< len; i++)
{
if(formArray[i].productId == 2 || (formArray[i].productId == 4 && formArray[i].productItemId == 5))
{
addIncomingNumber = true;
break;
}
}
if(addIncomingNumber == true)
{
if (countryName != "Select Country Name" || !countryName.trim())
{
currencySign = currencyArray[selectedCurrency];
var pricePerNumber = incomingCountryPriceArray[countryName];
var dataArr = pricePerNumber.split( '-' );
pricePerNumber = dataArr[0];
var id = dataArr[1];
var incomingNumberId = id;
// if(id > totalIncomingProducts)
// {
// incomingNumberId = parseInt(id % totalIncomingProducts);
// }
if(($('.incoming-group').length+1) > 10)
{
alert("Only 10 Numbers allowed");
return false;
}
var itemName = 'Inbound Number - '+ countryName;
var in_id = ($('.incoming-group').length + 1).toString();
//alert("incomingNumberId:" +incomingNumberId);
addItemToSummary(3,incomingNumberId , selectedCurrency ,pricePerNumber , itemName);
}
else
{
alert("Please Select Inbound Country");
return false;
}
}
else
{
accountStatusId = 'trial';
displayCustomMessages();
}
});
removeIncomingNumberCountryDiv();
}
function displayCustomMessages()
{
var message;
if(accountStatusId == 'expired')
{
message ="Your account is expired. Please buy Subscription Plan to activate account.";
}
else if(accountStatusId == 'trial')
{
message ="If you need an Incoming Number for testing, please contact : sales@screen-magic.com";
}
if($("#displayCustomMessages").length > 0)
{
$( "#displayCustomMessages" ).remove();
}
html = '
'+
''+
''+message+' '+
'
';
$("#trialExpiry").after(html);
}
function removeSummaryItem(id, productId, productItemId) {
if(formArray.length > 0)
{
for (var i = 0; i < formArray.length; i++)
{
if(i == id)
{
formArray.splice(i, 1);
break;
}
}
}
loadItemsToSummary(selectedCurrency);
loadCheckoutForm(selectedCurrency);
}
function removeIncomingNumberCountryDiv(){
$("#removeIncoming").click(function () {
if ($('.incoming-group').length == 0) {
return false;
}
var countryName = $(".incoming-group .country:last").html();
countryNameArr = countryName.split( '-' );
countryName = $.trim(countryNameArr[1]);
var pricePerNumber = incomingCountryPriceArray[countryName];
var dataArr = pricePerNumber.split( '-' );
pricePerNumber = dataArr[0];
var id = dataArr[1];
var incomingNumberId = id;
if(id > totalIncomingProducts){
incomingNumberId = parseInt(id % totalIncomingProducts);
}
$(".incoming-group:last").remove();
removeItemFromSummary(3,incomingNumberId ,selectedCurrency);
});
}
function incomingPricing(incomingPricingData ,currency){
var pricingData = incomingPricingData[currency];
for(var i=0; i< pricingData.length; i++){
var countryName = pricingData[i].countryName;
var id = pricingData[i].id;
var price = pricingData[i].price;
incomingCountryPriceArray[countryName] = price+"-"+id;
}
}
function loadAutoCompleteCountryListForIncoming(incomingPricingData ,currency){
incomingCountryArray = new Array();
incomingCountryPriceArray = new Array();
var pricingData = incomingPricingData[currency];
for(var i=0; i< pricingData.length; i++){
var countryName = pricingData[i].countryName;
var id = pricingData[i].id;
var price = pricingData[i].price;
incomingCountryArray.push(countryName);
}
incomingPricing(incomingPricingData ,currency);
var states = incomingCountryArray;
$('#scrollable-dropdown-menu .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
displayKey: 'value',
source: substringMatcher(states)
});
}
topupPricePerSMS = 0.0;
function showCountryPrice(countryName ,currency){
selectedCountry = countryName;
$("#typeahead").val(selectedCountry);
currencySign = currencyArray[selectedCurrency];
var convFactor = factorArray[selectedCurrency];
if(typeof(convFactor)==='undefined') convFactor = 1;
var pricePerSMS = countryPriceArray[countryName];
if(countryName == "India" && currency == "INR"){
var pricePerSMS = countryLocalPriceArray[countryName];
}
if(convFactor != 1){
pricePerSMS = (pricePerSMS * convFactor).toFixed(4);
}
topupPricePerSMS = pricePerSMS;
var flagName = countryFlagArray[countryName];
var incomingPrice = 0.0;
var countryList = incomingPriceData[selectedCurrency];
for(var i=0; i< countryList.length; i++){
if(countryList[i].countryName == countryName){
incomingPrice = countryList[i].price;
incomingPrice = incomingPrice +" "+ currencySign + " Per Month ";
if(currency == "INR"){
incomingPrice = countryList[i].price * 12;
incomingPrice = incomingPrice +" "+ currencySign + " Per Year ";
}
break;
}else{
incomingPrice = ' Contact Sales';//"
Contact Sales
";
}
}
$("#priceInbound").html(incomingPrice);
$("#pricePerSMS").html(pricePerSMS +" "+ currencySign);
var html = ''+countryName+'';
$("#countryFlagDiv").html(html);
generateSMSPackHtmlForTopupPage(smsPackPriceData, selectedCurrency);
}
function showInboundCountryPrice(countryName ,currency){
selectedCountry = countryName;
$("#typeahead").val(selectedCountry);
currencySign = currencyArray[selectedCurrency];
var flagName = countryFlagArray[countryName];
var html = ''+countryName+'';
$("#countryFlagDiv").html(html);
convertPricingInbound(currency);
}
function calculateCost( convFactor , currency){
if(typeof(selectedCurrency)==='undefined') selectedCurrency = "EUR";
var convFactor = factorArray[selectedCurrency];
if(typeof(convFactor)==='undefined') convFactor = 1;
var calculatedPrice = 0;
var countryName = $('#typeahead').prop("value");
var pricePerSMS = countryPriceArray[countryName];
if(currency == "INR"){
var pricePerSMS = countryLocalPriceArray[countryName];
}
if(convFactor != 1){
pricePerSMS = (pricePerSMS * convFactor).toFixed(4);
}
var number0fSMS = $('#number0fSMS').prop("value");
currencySign = currencyArray[selectedCurrency];
calculatedPrice = pricePerSMS * number0fSMS;
calculatedPrice = calculatedPrice.toFixed(2);
if(calculatedPrice == "NaN" || pricePerSMS == "NaN"){
calculatedPrice = 0;
pricePerSMS = (0.0000).toFixed(4);
}
$("#calculatedAmount").html(" = "+calculatedPrice +" "+ currencySign);
$("#pricePerSMS").html(" ( "+pricePerSMS +" "+ currencySign + " per sms )");
}
/* This function gets pricing of top countries from database and store it to global variable for further use. */
function loadTopCountryPrice(){
$.get("/pricingajaxapi/getTopCountryPricing", null, function(data) {
var responseData = eval('(' + data + ')');
topCountryList = responseData["topCountryList"];
generateHtml("newTopCountryPriceDiv",topCountryList,factorArray["USD"],"USD");
});
}
function getCurrencyConversionFactors(){
factorArray ={};
$.ajax({
type: "GET",
async: false,
url: "/pricingajaxapi/getCurrencyConversionFactors",
data: null,
success: function(data) {
var responseData = eval('(' + data + ')');
conversionFactors = responseData["conversionFactors"];
for(var i=0; i< conversionFactors.length; i++){
var toCurrency = conversionFactors[i].toCurrency;
var convFactor = conversionFactors[i].convFactor;
factorArray[toCurrency] = convFactor;
}
}
});
}
/* This function gets incoming number pricing of all countries from database and store it to global variable for further use.
buy page */
function loadIncomingPriceData(currency){
$.ajax({
type: "GET",
async: false,
url: "/pricingajaxapi/getAllIncomingCountryPricing",
data: null,
success: function(data) {
incomingPricingData = eval('(' + data + ')');
loadAutoCompleteCountryListForIncoming(incomingPricingData ,currency);
}
});
}
/* This function gets pricing of all countries from database and store it to global variable for further use.
* also it loads the country list to the auto complete drop down for topup page */
function loadAllCountryPrice(){
$.ajax({
type: "GET",
async: false,
url: "/pricingajaxapi/getAllCountryPricing",
data: null,
success: function(data) {
var responseData = eval('(' + data + ')');
countryList = responseData["countryList"];
createCountryPriceArray();
loadAutoCompleteCountryList();
//generateHtml("allCountryPriceDiv",countryList,factorArray["USD"],"USD");
}
});
}
function loadAllCountryPriceForSMSCostPage(){
$.ajax({
type: "GET",
async: false,
url: "/pricingajaxapi/getAllCountryPricing",
data: null,
success: function(data) {
var responseData = eval('(' + data + ')');
countryList = responseData["countryList"];
//loadAutoCompleteCountryList();
createCountryPriceArray();
generateHtml("allCountryPriceDiv",countryList,factorArray["USD"],"USD");
}
});
}
/* This function gets incoming number pricing of all countries from database and store it to global variable for further use.
sms cost page */
function loadAllIncomingCountryPrice(){
$.ajax({
type: "GET",
async: false,
url: "/pricingajaxapi/getAllIncomingCountryPricing",
data: null,
success: function(data) {
incomingPriceData = eval('(' + data + ')');
generateIncomingHtml("allIncomingCountryPriceDiv",incomingPriceData,0, factorArray["USD"],"USD");
}
});
}
/* This function gets incoming number pricing of top countries from database and store it to global variable for further use. */
function loadTopIncomingPrice(){
$.get("/pricingajaxapi/getTopIncomingNumberPricing", null, function(data) {
topIncomingPriceData = eval('(' + data + ')');
generateIncomingHtml("topIncomingCountryPriceDiv",topIncomingPriceData ,1, factorArray["USD"],"USD");
});
}
function generateHtml(divId,countryList , convFactor , currency){
if(typeof(convFactor)==='undefined') convFactor = 1;
if(typeof(currency)==='undefined') currency = "EUR";
var priceDivHTML = "";
var html ="";
var currencySign = currencyArray[currency];
for(var i=0; i< countryList.length; i++){
var countryName = countryList[i].countryName;
var flagName = countryList[i].flagName;
if((countryList[i].countryId == '80') && ( currency == "INR")){
var price = countryList[i].localprice;
}else{
var price = countryList[i].price;
}
if(convFactor != 1){
price = (price * convFactor).toFixed(4);
}
priceDivHTML = "