var StoreManager = function(section){ var storeArea = $("#store-area"), loadingArea = storeArea.find(".loading-store"), loadingAreaStatus = loadingArea.find(".loading-status"), waitForStoreResponseCount = 0, section = section, formArea = storeArea.find(".form-area"); var initialize = function(){ loadingArea.find("img").show(); loadingAreaStatus.text("Contacting Ruling Store."); loadingArea.find("p").eq(1).html("Please wait..."); setTimeout(step1,1000); } var step1 = function(){ $.ajax({ "type" : "get", "dataType" : "json", "url" : "/admin/store/check_credentials" }).done(function(status){ if(status.success == true){ loadingAreaStatus.text("Checking permissions."); setTimeout(step2,1000); }else if(status.success == false){ setTimeout(loadRegisterForm,1000); } }).fail(showError); } var step2 = function(){ $.ajax({ "type" : "get", "dataType" : "json", "url" : "/admin/store/check_permissions" }).done(function(data){ if(data.success){ loadingAreaStatus.text("Fetching store data."); if(section == "templates"){ setTimeout(loadTemplateStore,1000); }else if(section == "apps"){ setTimeout(loadAppStore,1000); } }else if(!data.success){ loadingArea.find("img").hide(); loadingAreaStatus.text(data.message); if(data.error == "SITE_NOT_CONFIRMED"){ var resentBtn = $("Resend Email"); resentBtn.on("click",function(){ resendEmail(); return false; }) loadingArea.find("p").eq(1).html(resentBtn); }else if(data.error == "SITE_PERMISSION_DENIED"){ loadingArea.find("p").eq(1).html("Please contact www.rulingcom.com."); }else{ loadingArea.find("p").eq(1).html("Please contact www.rulingcom.com."); } } }).fail(showError); } var loadTemplateStore = function(){ $.ajax({ "type" : "get", "dataType" : "html", "url" : "/admin/store/template_store" }).done(function(html){ storeArea.html(html); }).fail(showError); } var loadAppStore = function(){ $.ajax({ "type" : "get", "dataType" : "html", "url" : "/admin/store/app_store" }).done(function(html){ storeArea.html(html); }).fail(showError); } var loadTemplateStore = function(){ $.ajax({ "type" : "get", "dataType" : "html", "url" : "/admin/store/template_store" }).done(function(html){ storeArea.html(html); }).fail(showError); } var loadRegisterForm = function(){ loadingAreaStatus.text("Loading registeration form.") $.ajax({ "type" : "get", "dataType" : "html", "url" : "/admin/store/register_form" }).done(function(html){ loadingArea.hide(); formArea.html(html); var form = formArea.find("form"); new FormValidator(form); form.on("submit",function(){ var email = form.find("#inputEmail").val(); if(email){ loadingAreaStatus.text("Registering with Orbit Store.") formArea.hide(); loadingArea.show(); $.ajax({ "type" : "post", "url" : form.attr("action"), "dataType" : "json", "data" : {"email" : email} }).done(function(data){ if(data.success){ loadingAreaStatus.text("Waiting for Store."); setTimeout(waitForStoreResponse,500); }else{ loadingArea.find("img").hide(); loadingAreaStatus.text(data.message); loadingArea.find("p").eq(1).html("Please contact www.rulingcom.com."); } }).fail(showError) } return false; }) }) } var waitForStoreResponse = function(){ $.ajax({ "type" : "get", "dataType" : "json", "url" : "/admin/store/check_credentials" }).done(function(status){ if(status.success == true){ resendEmail(); }else if(status.success == false){ waitForStoreResponseCount++; if(waitForStoreResponseCount > 5){ loadingArea.find("img").hide(); loadingAreaStatus.text("Orbit Store couldn't contact your server."); loadingArea.find("p").eq(1).html("Please try again later or contact www.rulingcom.com."); }else{ setTimeout(waitForStoreResponse,500); } } }).fail(showError); } var resendEmail = function(){ loadingArea.find("img").show(); loadingAreaStatus.text("Sending verification email."); loadingArea.find("p").eq(1).html("Please wait..."); $.ajax({ "url" : "/admin/store/send_email", "dataType" : "json", "type" : "get" }).done(function(data){ if(data.success){ loadingArea.find("img").hide(); loadingAreaStatus.text("Email sent. Please confirm and click on link below or refresh the page."); var checkAgainBtn = $("Check Again"); checkAgainBtn.on("click",function(){ initialize(); return false; }) loadingArea.find("p").eq(1).html(checkAgainBtn); }else{ loadingArea.find("img").hide(); loadingAreaStatus.text("Email couldn't be sent. You can try again or contact www.rulingcom.com"); loadingArea.find("p").eq(1).html("Please contact www.rulingcom.com."); } }).fail(showError); } var showError = function(){ loadingArea.find("img").hide(); loadingAreaStatus.text("There was some unknown error."); loadingArea.find("p").eq(1).html("Please try again later or contact www.rulingcom.com."); } initialize(); } // step 1 -> check connection to store server // step 2 -> check for site confirmation and access permission