i var clientId = "hachette_wd"; var domain = "http://womansday.sharedbook.com/serve/smartbtn/" + clientId + "/"; function SharedBookSmartButton() { // *********** Private members *********** var cookie_name = clientId + "_sbUserId"; // Timer ID var tid = null; // Count how many times setInterval(.) was called to allow max retries var interval_counter = 0; var doc_url = document.location.href; var pattern = "/(Recipes/\.*)$"; var recipe = doc_url.match(pattern)[1]; var printableUrl = "http://" + document.location.host + "/layout/set/print/" + recipe; var userIdFromCookie = null; var remove_function = "javascript:sbSmartBtnObj.sharedbookRemoveArticle()"; var add_function = "javascript:sbSmartBtnObj.sharedbookAddArticle()"; var empty_function = function(){}; var button_initial_off = domain + "images/sharedbook_state_img_off.gif"; var button_initial_on = domain + "images/sharedbook_state_img_on.gif"; // To identify pressing "enter" in the email popup. var enter_key_code = "13"; // ************************************************************************** // Privileged functions (public with access to private methods) // ************************************************************************** /** * Add article to the user's clipboard. */ this.sharedbookAddArticle = function () { // Make sure we have user ID. if(userIdFromCookie == null) { userIdFromCookie = getCookie(cookie_name); var userIdFromPrompt = ""; if(userIdFromCookie == null) { // We don't have user ID, lets get it from the user. this.openEmailPopup(onEmailEnteredForAddURL); return; } } // else // { // We do have user ID, lets add the article to the clipboard. handleAddURLwithEmail(); // } /** * Called from the email popup div, when submiting the form. */ function onEmailEnteredForAddURL () { closeEmailPopup(); if (!validateEmailAddress()) return; handleAddURLwithEmail(); }; /** * We have user ID, add the article to the clipboard. */ function handleAddURLwithEmail() { // Returns "add_article_success == "True" " var add_article_url = encodeURI(domain + "add_url_to_book.djs?user_id=" + userIdFromCookie + "&article_url=" + doc_url + "&scraping_url=" + printableUrl); var add_article_script = document.createElement('script'); add_article_script.src = add_article_url; document.getElementsByTagName('head')[0].appendChild(add_article_script); // Change the main (left) button state to "in progress". var defaultStateObj = document.getElementById('default-state'); displayObj(defaultStateObj, false) var waitStateObj = document.getElementById('waiting-state'); displayObj(waitStateObj, true) interval_counter = 0; tid = setInterval(handleBtnState, 1000); function handleBtnState() { // Waits for "add_url_to_book.djs" to finish. if(typeof add_article_success != 'undefined' && add_article_success != null) { clearInterval(tid); tid = null; interval_counter = 0; if(add_article_success == "True") { // Reset to allow subsequent requests that will use the same var. add_article_success = null; changeBtnAfterAddArticle(); } else { handleFailedAddURL(); } } else if (interval_counter++ > 120) { clearInterval(tid); tid = null; interval_counter = 0; handleFailedAddURL(); } } function handleFailedAddURL() { // Reset to allow subsequent requests that will use the same var. add_article_success = null; changeBtnAfterRemoveArticle(); //revalidateDropdownFunc(); alert("Failed to add content to the book."); } } }; /** * Remove an article from the user's clipboard. */ this.sharedbookRemoveArticle = function () { if(userIdFromCookie == null) { // This shouldn't happen normally, but just in case, reset the button to its initial state. // Possible scenario: user added an article, then deleted the cookies, then clicked "remove". changeBtnAfterRemoveArticle(); } // Remove the article from the clipboard, no need to wait for result since even if the article wasn't // found in the clipboard we consider it as "removed". var remove_article_url = encodeURI(domain + "remove_url_from_book.djs?user_id=" + userIdFromCookie + "&article_url=" + doc_url); var remove_article_script = document.createElement('script'); remove_article_script.src = remove_article_url; document.getElementsByTagName('head')[0].appendChild(remove_article_script); changeBtnAfterRemoveArticle(); }; /** * Finalize the user's selections. */ this.sharedbookFinalizeBook = function () { // Make sure we have user ID. if(userIdFromCookie == null) { userIdFromCookie = getCookie(cookie_name); var userIdFromPrompt = ""; if(userIdFromCookie == null) { // No user ID, close the menu and open the email popup. this.openEmailPopup(onEmailEnteredForFinalize); return; } } else { // We have user ID, let's go and finalize. handleFinalizeBookWithEmail(); } // Called from the email popup div, on submit email. function onEmailEnteredForFinalize() { closeEmailPopup(); if (!validateEmailAddress()) return; // We have user ID, let's go and finalize. handleFinalizeBookWithEmail(); } /** * Finalize book: * 1. Verifiy there is at least one article in the user's clipboard. * 2. Go to bridge page. */ function handleFinalizeBookWithEmail() { // Verify there is at least one article in the user's clipboard. // This code generats "var articles_exists = 'True'" (or False). var finalize_article_url = encodeURI(domain + "verify_articles_in_book.djs?user_id=" + userIdFromCookie); var finalize_article_script = document.createElement('script'); finalize_article_script.src = finalize_article_url; document.getElementsByTagName('head')[0].appendChild(finalize_article_script); interval_counter = 0; tid = setInterval(handleFinalizeBook, 1000); function handleFinalizeBook() { // Check if we got a response from "verify_articles_in_book.djs". if(typeof articles_exists != 'undefined' && articles_exists != null) { clearInterval(tid); tid = null; interval_counter = 0; if(articles_exists == "True") { // Reset to allow subsequent requests that will use the same var. articles_exists = null; document.location.href = encodeURI(domain + "bridge.djs?user_id=" + userIdFromCookie+"&article_url="+doc_url); } else { articles_exists = null; alert("Please add at least one page"); } } else if (interval_counter++ > 120) { clearInterval(tid); tid = null; interval_counter = 0; articles_exists = null; alert("Failed to finalize book, please try again.\nIf the problem remains please contact our Customer Care at www.sharedbook.com"); } } } }; /** * Open the email popup div, invalidate the button so user couldn't click or hover it * while the popup is opened. */ this.openEmailPopup = function (func) { document.getElementById("sb_email_addr").disabled = false; document.getElementById("sb_email_content").style.display = "inline"; document.getElementById("sb_email_content").style.visibility = "visible"; document.getElementById("sb_email_popup_submit").onclick = func; } /** * Close the email popup div, revalidate the button. */ function closeEmailPopup () { document.getElementById("sb_email_content").style.visibility = "hidden" document.getElementById("sb_email_addr").disabled = true; } this.closeEmailPopup = closeEmailPopup; this.expandDiv = function (obj) { var el = document.getElementById(obj); if(el.style.display != "none") { el.style.display = 'none'; } else { el.style.display = ''; } } /** * Event handler for key down - invokes submit email when "enter" key was pressed. */ this.onkeydownHandler = function (evt, win) { evt = win.event ? win.event : evt; if (evt.keyCode == enter_key_code) { stopEvent(evt); document.getElementById("sb_email_popup_submit").onclick(); } } this.setVisible = function (obj) { obj = document.getElementById(obj); obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible'; }; // ************************************************************************** // Private functions // ************************************************************************** function displayObj( obj, isDisplay ) { if( isDisplay ) { obj.style.display = 'inline'; obj.style.visible = 'visible'; } else { obj.style.display = 'none'; obj.style.visible = 'hidden'; } } function elementSetAtt(elm, name, value) { elm.setAttribute(name, value); } function createElement(name, value) { var elm = document.createElement("input"); elementSetAtt(elm, "type", "hidden"); elementSetAtt(elm, "name", name); elementSetAtt(elm, "value", value); return elm; } /** * Disable all functionality of the entire button (left and dropdown). */ function invalidateEntireBtnFunc() { // var drop_down_link = document.getElementById("sharedbook_dropdown_link"); // drop_down_link.onclick = empty_function; // drop_down_link.onmouseover = empty_function; // drop_down_link.onmouseout = empty_function; // // document.getElementById("sharedbook_main_link").href = "javascript: void(0)"; // invalidateMainBtnMouseEvents(); } /** * Set UI and functionality of the button after article was removed. */ function changeBtnAfterRemoveArticle() { var addedRecipeStateObj = document.getElementById('addedrecipe-state'); displayObj(addedRecipeStateObj, false) var defaultStateObj = document.getElementById('default-state'); displayObj(defaultStateObj, true) } /** * Set UI and functionality of the button after article was added. */ function changeBtnAfterAddArticle() { var waitStateObj = document.getElementById('waiting-state'); displayObj(waitStateObj, false) var addedRecipeStateObj = document.getElementById('addedrecipe-state'); displayObj(addedRecipeStateObj, true) } /** * Enable the functioanlity of the main (left) button. */ function revalidateMainBtn(func) { // document.getElementById("sharedbook_state_img").src = button_off; // document.getElementById("sharedbook_main_link").href = func; // revalidateMainBtnMouseEvents(); } /** * Enable mouse events on the main (left) button. */ function revalidateMainBtnMouseEvents() { // document.getElementById("sharedbook_main_link").onmouseover = sbSmartBtnObj.setMainBtnStateOn; // document.getElementById("sharedbook_main_link").onmouseout = sbSmartBtnObj.setMainBtnStateOff; } /** * Disable mouse events on the main (left) button. */ function invalidateMainBtnMouseEvents() { // document.getElementById("sharedbook_main_link").onmouseover = empty_function; // document.getElementById("sharedbook_main_link").onmouseout = empty_function; } function setCookie(value) { // Find the domain: // "blog.sharedbook.com".match(/(\S*\.)*(\S+[\.]\S+)/) would result: // ["blog.sharedbook.com", "blog.", "sharedbook.com"] var fullDomain = document.domain; var cookie_domain = null; try { cookie_domain = fullDomain.match(/(\S*\.)*(\S+[\.]\S+)/)[2]; } catch(e) { cookie_domain = "sharedbook.com" } var cookieCommand = encodeURI( cookie_name + "=" + value + "; path=/; domain=" + cookie_domain + "; expires=Fri, 31 Dec 2099 23:59:59 GMT" ); document.cookie = cookieCommand; } function getCookie() { var dc = document.cookie; var cname = cookie_name + "="; if(dc.length > 0) { begin = dc.indexOf(cname); if(begin != -1) { begin += cname.length; end = dc.indexOf(";", begin); if(end == -1) end = dc.length; return unescape(dc.substring(begin, end)); } } return null; } function validateEmailAddress() { // TODO: VALIDATE EMAIL ADDRESS var userIdFromPrompt = document.getElementById("sb_email_addr").value; if(userIdFromPrompt == '') { return false; } setCookie(userIdFromPrompt); userIdFromCookie = userIdFromPrompt; return true; } function stopEvent (evt) { //IE if(typeof(window.event) != "undefined") { evt.cancelBubble = true; evt.returnValue = false; } //FF else { evt.preventDefault(); evt.stopPropagation(); } } img1 = new Image(); img1.src = domain + 'images/addrecipe_on.gif'; img2 = new Image(); img2.src = domain + 'images/viewbtn_on.gif'; img3 = new Image(); img3.src = domain + 'images/learnmore_on.gif'; img4 = new Image(); img4.src = domain + 'images/remove_on.gif'; this.chgBtn = function( which, state ) { state == "on" ? st = 'on' : st = 'off'; document.getElementById(which).src = domain + 'images/' + which + '_' + st + '.gif'; } this.openLearnMore = function() { this.setVisible('learn-more'); } } // Important: client name turns to directory name, keep it lower case ! var sbSmartBtnObj = new SharedBookSmartButton(); var dws = "" dws +=''; dws +=''; dws +=''; dws +='
'; dws +='
'; dws +='
'; dws +='
'; dws +=' '; dws +='
'; dws +=' '; dws +=' '; dws +=' '; dws +=' '; dws +='
'; dws +='
'; dws +='

Create your own
Recipe Book

'; dws +='
'; dws +='
'; dws +=''; if (!window.smartBtnGM) { document.write(dws); } else { smartBtnGM.buttonContainerElement.innerHTML += dws }