$.urlParam = function(name){ var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href); if (results == null){ return null; } else{ return results[1] || 0; } } function parseInputs(frm) { var inputs = $(frm).serializeArray(); var inpt = new Object; $.each(inputs, function() { inpt[this.name] = this.value; }); return inpt; } function isValidEmail(emailAddress) { var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i); return pattern.test(emailAddress); }; function loginFacebook() { FB.login(function(response) { if (response.authResponse) { FB.api('/me', function(response) { window.location.href='/login/facebook'; }); } },{scope: 'email,publish_stream'}); } function signupFacebook() { FB.login(function(response) { if (response.authResponse) { FB.api('/me', function(response) { window.location.href='/login/facebook?url=profile'; }); } },{scope: 'email,publish_stream'}); } function fbAfterInit(callback) { if(!window.fbApiInit) { setTimeout(function() {fbAfterInit(callback);}, 50); } else { if(callback) { callback(); } } } function connectFB() { FB.login(function(response) { if (response.authResponse) { FB.api('/me', function(response) { $('#fbImage').css('opacity', '1'); $('#fbConnected').show(); }); } },{scope: 'email,publish_stream'}); } function showModal(modal, hide) { $('#coverAll').css('background', '#666'); $('#coverAll').height($(document).height()); $('#coverAll').show(); if (hide != false) { //$('#coverAll').click(function() { hideModal(modal); }); $(document).keyup(function(e) { if (e.keyCode == 27) { hideModal(modal); } }); } $('#' + modal).css('top', $(window).scrollTop() + 50); $('#' + modal).css('left', '50%'); $('#' + modal).css('margin-left', ($('#' + modal).width() / 2) * -1); $('#' + modal).fadeIn('slow'); } function hideModal(modal) { $('#' + modal).hide(); $('#coolPigPopCoupon').remove(); $('#coolPigPopPrint').remove(); $('#coverAll').height($(window).height()); $('#coverAll').hide(); } function getLocation() { navigator.geolocation.getCurrentPosition(foundLocation); } function foundLocation(position) { window.location.href='/profile/setLocation?lat=' + position.coords.latitude + '&lon=' + position.coords.longitude; } function showAccount() { $('#accountSection').show(); $('#emailSection').hide(); $('#profileSection').hide(); $('.button').val("Continue"); $('.accountButton a').show(); $("#account").addClass('current').siblings().removeClass('current'); } function showProfile() { $('#accountSection').hide(); $('#emailSection').hide(); $('#profileSection').show(); $('.button').val("Continue"); $('.accountButton a').show(); $("#emailPref").addClass('current').siblings().removeClass('current'); } function showEmail() { $('#accountSection').hide(); $('#emailSection').show(); $('#profileSection').hide(); $('.button').val("I'm Done"); $('.accountButton a').hide(); $("#emailSub").addClass('current').siblings().removeClass('current'); } function chooseLevel(id, name, level) { var newLevel = -1; if (level == 'negative') { newLevel = 0; $('#' + id + '_none').css('opacity', '1'); $('#' + id + '_preferred').css('opacity', '.25'); $('#' + id + '_interest').css('opacity', '.25'); if ($('#negativeSave').val().search(name) == -1) { $('#negativeSave').val($('#negativeSave').val() + '|' + name); } $('#interestSave').val($('#interestSave').val().replace(name, '')); $('#preferredSave').val($('#preferredSave').val().replace(name, '')); } else if (level == 'preferred') { newLevel = 2; $('#' + id + '_none').css('opacity', '.25'); $('#' + id + '_preferred').css('opacity', '1'); $('#' + id + '_interest').css('opacity', '.25'); if ($('#preferredSave').val().search(name) == -1) { $('#preferredSave').val($('#preferredSave').val() + '|' + name); } $('#interestSave').val($('#interestSave').val().replace(name, '')); $('#negativeSave').val($('#negativeSave').val().replace(name, '')); } else if (level == 'interest') { newLevel = 1; $('#' + id + '_none').css('opacity', '.25'); $('#' + id + '_preferred').css('opacity', '.25'); $('#' + id + '_interest').css('opacity', '1'); if ($('#interestSave').val().search(name) == -1) { $('#interestSave').val($('#interestSave').val() + '|' + name); } $('#preferredSave').val($('#preferredSave').val().replace(name, '')); $('#negativeSave').val($('#negativeSave').val().replace(name, '')); } if (newLevel >= 0) { $.ajax({ url: "/util/update_interest", method: 'POST', data: "id=" + id + "&level=" + newLevel, dataType: "json", success: function (data) { } }); } } function updateProfile(field, value) { if (field == 'dob') { value = $('#year').val() + '-' + $('#month').val() + '-' + $('#day').val(); } $.ajax({ url: "/util/update_profile", method: 'POST', data: "field=" + field + "&value=" + escape(value), dataType: "json", success: function (data) { } }); } function signUp() { var email = $('#signupEmail').val(); var zip = $('#signupZip').val(); var pw = $('#signupPassword').val(); var cpw = $('#signupConfirm').val(); $('#emailError').html(''); $('#passwordError').html(''); $('#zipError').html(''); $('#loadingMore').show(); $('#loadingMore').css({'margin-left':'-4px', 'position':'relative','top': '181px', 'left':'263px', 'z-index': '9999'}); $.ajax({ url: "/util/signup", method: 'POST', data: "email=" + email + "&zip=" + zip + "&pw=" + pw + "&cpw=" + cpw, dataType: "json", success: function (data) { $('#loadingMore').hide(); if (data.status == 'failure') { for (error in data.errors) { if (data.errors[error] == 'account exists') { $('#emailError').html('Account already exists'); } else if (data.errors[error] == 'invalid password') { $('#passwordError').html('Invalid Password'); } else if (data.errors[error] == 'invalid email') { $('#emailError').html('Invalid Email'); } else if (data.errors[error] == 'invalid zip') { $('#zipError').html('Invalid Zip'); } } } else { $('#tmpLocation').val(zip); window.location.href = '/profile'; _gaq.push(['_trackEvent', 'Sign up - Main Interstitial', 'Signed Up', $('#cid').val()]); } } }); } function signUpFB() { FB.login(function(response) { if (response.authResponse) { FB.api('/me', function(response) { loginFB(); }); } },{scope: 'email,publish_stream'}); } function loginFB() { $.ajax({ url: "/util/signupfb", method: 'POST', dataType: "json", success: function (data) { if (data) { $('#tmpLocation').val(data.zip); $('#tmpFirstName').val(data.first_name); $('#tmpLastName').val(data.last_name); hideModal('signupModal'); window.location.href = '/profile'; } else { } } }); } function checkFB(cb) { FB.getLoginStatus(function(response){ if (response.status != "connected") { showModal("signupModal", false); } else { loginFB(); } }); } $('#tmpLocation').autocomplete({ source: function(request, response) { $.ajax({ url: "/util/cities", data: "city=" + request.term, dataType: "json", success: function (data) { response( $.map( data, function( city ) { return { label: city.city, value: city.city } }) ); } }); }, minLength: 3, height:300 }); function getSignUpFrm() { $('#coverAll').css('background', '#666'); $('#coverAll').height($(document).height()); $('#coverAll').show(); $.ajax('/profile/show_signup', { success: function(data) { $('#modal').html(data); showModal('modal',true); } }); } function popUnder(site) { _gaq.push(['_trackEvent', 'Online Coupon Codes', 'Viewed Coupon Code', $('#cid').val()]); //$.popunder([[site], {menubar:1,location:1,resizable:1,scrollbars:1,status:1,titlebar:1}]); $.popunder([[site]]); } function couponEm(data) { $('#printFrame').hide(); $('#loadingMore').hide(); $('#coolPigPopCoupon').html(data); _gaq.push(['_trackEvent', 'Coupon Printer - Unsupported Device ' + $('#userStatus').val(), 'Sent Email', $('#cid').val()]); } $(document).ready(function() { $(document).on('click', '.modalClose', function() { hideModal('modal'); return false; }); $(document).on('click', '#showStores', function() { $('.hidden').show(250); $('#hideStores').show(250); $('#showStores').hide(250); return false; }); $(document).on('click', '#hideStores', function() { $('.hidden').hide(250); $('#hideStores').hide(250); $('#showStores').show(250); return false; }); $(document).on('click','#checkimg', function(event) { var checked = $('#check').val(); if(checked == 1) { $('#checkimg').attr('src','http://i.coolsavings.com/media/noncheckcoupons.png'); $('#check').val(0); } else { $('#checkimg').attr('src','http://i.coolsavings.com/media/checkcoupons.png'); $('#check').val(1); } }); $('a.close2').click(function() { $('#coolFade , .coolPopBlock').fadeOut(function() { $('#coverAll').hide(); $('#coolzip').val(''); $('#coolemail').val(''); $.cookie('CoolSavingsCoupon', 'zi', {expires: 1, path: '/', domain: '.coolsavings.com'}); }); return false; }); $('#signup_lnk').click(function() { getSignUpFrm(); _gaq.push(['_trackEvent', 'Sign up - Main Interstitial', 'Viewed Sign up Form', $('#cid').val()]); return false; }); /*$('#coverAll').click(function() { if ($('#pigaboo').length) { $.cookie('CoolSavingsCoupon', 'zi', {expires: 1, path: '/', domain: '.coolsavings.com'}); $('#pigaboo').remove(); } hideModal('modal'); $('#coolPigPop').hide(); });*/ if ($('#pwreset_email').length) { $('#pwreset_email').focus(); } $('#signup_btn').click(function() { signUp(); } ); $(document).on('submit', '#reg_fm',function(event) { $('#loadingMore').show(); $('#loadingMore').css({'z-index':'999999', 'top':'401px', 'left': '51%'}); $('.errorTxt').html(''); var inpt = parseInputs(this); var err = false; var errMsg = ""; if (inpt.email.trim() == "") { errMsg = "Email cannot be blank"; } else if (!isValidEmail(inpt.email.trim())) { errMsg = "Invalid Email format."; } else if (inpt.zip.trim() == "") { errMsg = "Zip cannot be blank."; } else if (inpt.zip.indexOf(" ") != -1) { errMsg = "Spaces are not allowed."; } $.ajax({ url: "/util/check_inter", async: false, method: 'POST', data: "email=" + inpt.email.trim() + "&zip=" + inpt.zip.trim() , dataType: "json", success: function (data) { if (data.status == 'failure') { if (data.error == 'account exists') { errMsg = 'Account already exists'; } else if (data.error == 'invalid password') { errMsg = 'Invalid Password'; } else if (data.error == 'invalid email') { errMsg = 'Invalid Email'; } else if (data.error == 'invalid zip') { errMsg = 'Invalid Zip'; } } } }); if (errMsg) { $('#loadingMore').hide(); $('.errorTxt').html(errMsg); return false; } _gaq.push(['_trackEvent', 'Sign up - ' + $('#interType').val() + ' Interstitial', 'Signed Up', $('#cid').val()]); return false; }); $(document).on('submit', '#coupon_user_fm',function(event) { var inpt = parseInputs(this); $.ajax({ url: "/util/coupon_inter", async: false, method: 'POST', data: "email=" + inpt.email.trim() + "&check=0" + "&urlkey=" + inpt.urlkey.trim(), dataType: "text", success: function (data) { couponEm(data); } }); }); $(document).on('submit', '#coupon_fm',function(event) { $('#loadingMore').show(); $('#loadingMore').css({'z-index':'999999', 'top':'401px', 'left': '51%'}); $('.errorTxt').html(''); var inpt = parseInputs(this); var err = false; var errMsg = ""; if (inpt.email.trim() == "") { errMsg = "Email cannot be blank"; } else if (!isValidEmail(inpt.email.trim())) { errMsg = "Invalid Email format."; } else if (inpt.zip.trim() == "") { errMsg = "Zip cannot be blank."; } else if (inpt.zip.indexOf(" ") != -1) { errMsg = "Spaces are not allowed."; } $.ajax({ url: "/util/check_inter", async: false, method: 'POST', data: "email=" + inpt.email.trim() + "&zip=" + inpt.zip.trim() , dataType: "json", success: function (data) { if (data.status == 'failure') { if (data.error == 'account exists') { errMsg = 'Account already exists'; } else if (data.error == 'invalid password') { errMsg = 'Invalid Password'; } else if (data.error == 'invalid email') { errMsg = 'Invalid Email'; } else if (data.error == 'invalid zip') { errMsg = 'Invalid Zip'; } } } }); if (errMsg) { $('#loadingMore').hide(); $('.errorTxt').html(errMsg); return false; } else { $.ajax({ url: "/util/coupon_inter", async: false, method: 'POST', data: "email=" + inpt.email.trim() + "&zip=" + inpt.zip.trim() + "&check=" + inpt.check.trim() + "&urlkey=" + inpt.urlkey.trim(), dataType: "text", success: function (data) { couponEm(data); _gaq.push(['_trackEvent', 'Sign up - Unsupported Print Interstitial', 'Signed Up', $('#cid').val()]); } }); } return true; }); });