//offersv4 var attempts = 0; var fbconnected = false; var sessiondata = options.sessiondata; var requiresPermissions = false; window.submitarr = []; window.submitcounter = 0; $(document).bind('nextdialog', function (e) { window.submitcounter++; $(document).trigger('rundialog'); }); $(document).bind('rundialog', function (e) { if (window.submitcounter >= window.submitarr.length) submit_form(); else run_dialog(); }); function run_dialog() { if (typeof window.submitarr[window.submitcounter] === 'function') { window.submitarr[window.submitcounter](); } } function submit_form() { try { FORM.is_submitting = true; $.ajax({type: "POST", async: false, url: "/conversion",data: "offer="+options.shard+"&action=submitattempts&"+sessiondata}); $('#actionbutton').prop('disabled', true); $('#input').unbind('submit'); $('#input').submit(); } catch(err) { $.ajax({type: "POST", async: false, url: "/conversion",data: "offer="+options.shard+"&action=submiterrors&"+sessiondata}); } } function submit_form_tweetbonus () { if ($('#tweet').val() != '') { submit_form_twitter(); } else if ($(':input[name="twittertweet"]').val() != '') { submit_form_twitter(); } else { $(document).trigger('nextdialog'); } } function submit_form_twitter() { if (document.cookie.indexOf('twittersession') == -1) { var that = this; that._oauthWindow = window.open('/' + options.shard + '/twitterlogin', 'ConnectToTwitter', 'location=0,status=0,width=800,height=400'); that._oauthInterval = window.setInterval(function(){ if (that._oauthWindow.closed) { window.clearInterval(that._oauthInterval); if (document.cookie.indexOf('twittersession') != -1) { $(document).trigger('nextdialog'); } } }, 1000); return false; } else { $(document).trigger('nextdialog'); } } function submit_form_pinterest () { var $pin_tracking = $("#pins-wrap").find(".pin_tracking"); if($pin_tracking.length) { // handle v4 pinterest stuff $pin_tracking.each(function() { $('#input').append($(this)); }); $(document).trigger('nextdialog'); // trigger it regardless of whether any were clicked, let the form validation do its job } else { // handle v3 pinterest stuff if (document.cookie.indexOf('pinterest_username') == -1) { var pinwin = window.open('/offers/username/'+options.shard+'/pinterest?redirect='+encodeURIComponent('/util/closewindow'), 'PinterestUsernameRequest', 'location=0,status=0,width=660,height=300'); var success = false; pinInterval = window.setInterval(function(){ if (typeof pinwin !== 'undefined' && pinwin.closed) { window.clearInterval(pinInterval); if ($('input[name="pinterest_username"]').val() != '') { $(document).trigger('nextdialog'); } } else if (typeof pinwin === 'undefined') { window.clearInterval(pinInterval); } }, 1000); } else { $(document).trigger('nextdialog'); } } } function twitter_tweet_data(event) { if (event) { $.ajax({ type: 'post', url: "/" + options.shard + "/api/twittershare", success: function(data){ return true; } }); } } function fb_connect() { if(navigator.userAgent.match('CriOS')) { $(document).trigger('nextdialog'); return true; } $.ajax({type: "POST", async: false, url: "/conversion",data: "offer="+options.shard+"&action=attempts&"+sessiondata}); try { FACEBOOK.login(options.facebook_oauth_scope).then( function () { $(document).trigger('nextdialog'); }, function () { // show why auth is needed $.ajax({type: "GET", async: false, url: "/conversion",data: "offer="+options.shard+"&action=authdenies&"+sessiondata}); } ); $.ajax({type: "GET", async: false, url: "/conversion",data: "offer="+options.shard+"&action=authdialogs&"+sessiondata}); } catch(err) { $.ajax({type: "GET", async: false, url: "/conversion",data: "offer="+options.shard+"&action=authdialogerrors&"+sessiondata}); $(document).trigger('nextdialog'); return false; } return false; } var fangatelikeclicked = false; function onFacebookLoad() { FB.Event.subscribe('edge.create', function(href, widget) { if (href == options.facebook_fanpage_url) fangatelikeclicked = true; $.ajax({ type: "POST", data: "href="+href, url: "/" + options.shard + "/api/liked" }); }); FB.Event.subscribe('auth.login', function(response) { // window.location.reload( true ); }); FB.getLoginStatus(function(response) { if (response.authResponse) { FACEBOOK.checkPermissions(options.facebook_oauth_scope).then( function () {}, function () { // if ((options.tab && options.requires_facebook_canvas) || (!options.tab && options.requires_facebook)) // window.submitarr.push(fb_connect); // temporarily disabled because FB.login is not opening modal inside tab, use top redirect instead $.ajax({type: "POST", async: false, url: "/conversion",data: "offer="+options.shard+"&action=needlikepermission&"+sessiondata}); } ); } else { FB.Event.unsubscribe('auth.login', function() {}); // if ((options.tab && options.requires_facebook_canvas) || (!options.tab && options.requires_facebook)) // window.submitarr.push(fb_connect); } }); // login event to check to see if page needs to be refreshed } function tryFormlessSubmit () { if (options.formless_submit) $(document).trigger('rundialog'); return false; } // OPTIONS $(document).ready(function() { // handle pinterest button and widget clicks $('#pins-wrap').ready(function() { var $wrap = $(this); $(function($) { $wrap.on('click', '[data-pin-log="button_pinit"]', function(e) { e.preventDefault(); $(this).closest('.pin').find('.pin_tracking').val('1'); return false; // tracks clicks on pinterest buttons }).on('click', '[data-pin-log="embed_pin_repin"]', function(e) { e.preventDefault(); $(this).closest('.pin').find('.pin_tracking').val('1'); return false; // tracks clicks on pinterest widgets }); }); }); if (options.uses_pinterest) { if (!$(':input[name="pinterest_username"]').length) $('#input').append(""); window.submitarr.push(submit_form_pinterest); } }); // only load twitter sdk when required if (options.has_twitterfollowbutton || options.has_tweetbutton) { window.twttr = (function (d,s,id) { var t, js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js=d.createElement(s); js.id=id; js.src="https://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs); return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } }); }(document, "script", "twitter-wjs")); } if (options.has_tweetbutton) { twttr.ready(function (twttr) { twttr.events.bind('tweet', twitter_tweet_data); }); } if (options.requires_twitter) { window.submitarr.push(submit_form_twitter); } else if (options.has_tweetbonus) { window.submitarr.push(submit_form_tweetbonus); } $(document).on('context.loaded', function () { // Change id of first form to input if we don't have an input id var $form = $('.page-components > li.form form'); if (!$form.length) { $form = $("
"); $('body').append($form); } if (!$("#input").length) $form.attr("id", "input"); // Add hidden form fields $('#input').append(''); $('#input').append(''); $('#input').append(''); $('#input').append(''); $('').attr({ type: 'hidden', name: options.session_name, value: options.session_data}).appendTo('#input'); if ($('canvas').length) { $('#input').append(''); } if (options.tab) { $('#input').append(''); } if (options.embed) { $('#input').append(''); } if (options.plugin) { $('#input').append(''); } // Set form action $("#input").attr("action", options.submit_url); $("#input").attr("ACTION", options.submit_url); //set form method if (!$('#input').attr("method")) { $('#input').attr("method", "post"); } //set multipart if has file field if (!$('#input').attr("enctype") && $('#input').children('input:file').length) { $('#input').attr("enctype", "multipart/form-data"); } // Rename element names submit (avoid jquery submit issue on certain browsers) $('input[name="submit"]').attr("name", "actionbutton"); //trigger form loaded $(document).trigger('form.loaded'); // override submit for dialogs $('#input').submit(function (event) { event.preventDefault(); // get canvas data if any if ($('canvas').length) { var data = document.getElementsByTagName('canvas')[0].toDataURL(); $('input[name="canvas"]').val(data); } $(document).trigger('rundialog'); }); // form validation $('#input input, #input textarea').blur(function(){ if ($(this).attr('type') == 'file') return false; if (!validate_input($(this))) { $(this).attr('aria-invalid', 'true'); $(this).addClass('input-error'); } }) .focus(function(){ $(this).removeClass('input-error'); $(this).removeAttr('aria-invalid'); }); // validate birthday fields as a group $('#input .type-birthday select').blur(function() { validate_birthday_input($(this)); }) .focus(function(){ $(this).removeClass('input-error'); $(this).removeAttr('aria-invalid'); }); function validate_input(input) { if (options.formless_submit) return true; var maximum_file_size = 250 * 1024 * 1024; //250mb if (input.attr('type') == 'file' && typeof input[0].files === 'object' && input[0].files.length) { for (var i = 0; i < input[0].files.length; i++) { if (typeof input[0].files[i] === 'object' && typeof input[0].files[i].size !== 'undefined' && input[0].files[i].size > maximum_file_size) { alert('Error: File too large. Max file size is 250MB.'); return false; } } } if (!input.attr('required') && input.prop('tagName') != 'SELECT' && input.attr('name') != 'video') return true; if ((input.attr('type') == 'email' || input.attr('name') == 'email') && !util.email_valid(input.val())) return false; else if (input.attr('type') == 'checkbox' && !input.is(':checked')) return false; else if (input.attr('type') == 'radio') return !!$('[name="'+input.attr('name')+'"]:checked').length; else if (input.prop('tagName') == 'SELECT' && input.attr('required') && input.val() == '') return false; else if (input.prop('name') == 'video') { if (input.prop('required') || input.val()) return util.validateUrl(input.val()); else return true; } else if (!input.val()) return false; return true; } function validate_birthday_input(input) { var $selects = input.closest('.type-birthday').find('select'); // grab the three select menus representing month, date, and year var invalid_count = 0; // let's count how many out of these three suckers are filled out var required = input.attr('required'); // possibly the birthday is required var valid_bool = false; // assume it's invalid var selectcount = 0; $selects.each(function() { if($(this).val()=='-1') invalid_count++; // it has a value selected selectcount++; }); // if it's required, then all must be filled out if(required && invalid_count===0) { $selects.removeClass('input-error').removeAttr('aria-invalid'); valid_bool = true; } // if it's not required, then either none or all need to be filled out else if(!required && (invalid_count===0 || invalid_count==selectcount)) { $selects.removeClass('input-error').removeAttr('aria-invalid'); valid_bool = true; } // otherwise there we are either missing a month, day, or year (or if required possibly all three) else { $selects.each(function() { if($(this).val()=='-1') { $(this).attr('aria-invalid', 'true'); $(this).addClass('input-error'); } }); } // finally return a boolean as to whether or not the set of birthday fields is valid_count return valid_bool; } function inputs_valid() { var all_valid = true; $('#input input, #input textarea').each(function(){ if (!validate_input($(this))) { all_valid = false; $(this).attr('aria-invalid', 'true'); $(this).addClass('input-error'); } }); $('#input .type-birthday').each(function() { // we only need to validate the first select within a birthday input group // because the function checks all three together // as well as applying input error class when validation fails if (!validate_birthday_input($(this).find('select:first'))) { all_valid = false; } }); $('#input #country_id').each(function() { if($(this).attr('required')) { var blocked_countries = $(this).data('block').split(','); if(blocked_countries.indexOf($(this).val()) !== -1) { all_valid = false; $('body').append(''+$(this).data('blockmsg')+'