$(document).ready(function(){ var currentClientTime = getDateTimeNow(); var cookieName = 'currenttime'; var cookieOptions = {expires: null, path: '/'}; $.cookie(cookieName, currentClientTime, cookieOptions); var initName = 'inittime'; var initTimeValue; if($.cookie(initName)){ initTimeValue = $.cookie(initName); } else { $.cookie(initName, currentClientTime, cookieOptions); initTimeValue = currentClientTime; // just got here? } var visitDuration = currentClientTime - initTimeValue; // OKAY, WE ONLY WANT TO OPEN THIS THING ONCE FOR EACH CUSTOMER // SO, IF THEY X OUT OF IT OR THEY SUBSCRIBE, THEN DONT SHOW IT AGAIN. if( $.cookie('has_xout_askbsw') ){ // they have clicked the x... } else if( $.cookie('has_opened_askbsw_before') ){ // they have already had it display... } else if( $.cookie('has_subscribed_to_askbsw') ) { // THEY ALREADY SUBSCRIBED } else if( visitDuration < 10000 ) { var tm = 10000 - visitDuration; //alert('vd: ' + visitDuration + ', got here: ' + initTimeValue); init_newsletterpopup(tm); } else if( visitDuration >= 10000 ) { open_askbsw(); } //if submit button is clicked $('#submit').click(function () { //Get the data from all the fields var name = $('input[name=askbsw_name]'); var email = $('input[name=askbsw_email]'); //Simple validation to make sure user entered something //If error found, add hightlight class to the text field if (name.val()=='') { name.addClass('hightlight'); hideForm(); showFormError('Oops, name and email are required.'); return 0; } else name.removeClass('hightlight'); if (email.val()=='') { email.addClass('hightlight'); hideForm(); showFormError('Oops, name and email are required.'); return 0; } else email.removeClass('hightlight'); //organize the data properly var data = 'name=' + name.val() + '&email=' + email.val(); //show the loading sign && remove any messages & form hideFormSuccess(); hideFormError(); hideForm(); showFormLoading(); //start the ajax $.ajax({ //this is the php file that processes the data and send mail url: "/assets/ajax/process_askbsw.php", //GET method is used type: "GET", //pass the data data: data, //Do not cache the page cache: false, //success success: function (html) { //if process.php returned 1/true (send mail success) if (html=='good') { //hide the form hideFormLoading(); //show the success message var spmsg = 'Thank you. A confirmation email has been sent to ' + email.val(); //alert(spmsg); showFormSuccess(spmsg); var cookieName = 'has_subscribed_to_askbsw'; var cookieOptions = {expires: 365, path: '/'}; var cookieValue = getDateTimeNow(); $.cookie(cookieName, cookieValue, cookieOptions); close_newsletterpopuptimer(6000,1); // close the form automatically //if process.php returned 0/false (send mail failed) } else { hideFormLoading(); showFormError(html); } } }); //cancel the submit button default behaviours return false; }); }); function tryItAgain(){ hideFormError(); hideFormSuccess(); hideFormLoading(); showForm(); } function showFormLoading(){ //$("#askbsw_form_loading").css('display','block'); $('#askbsw_form_loading').fadeIn('slow'); } function hideFormLoading(){ $('#askbsw_form_loading').fadeOut('fast'); } function showForm(){ //$("#askbsw_form").css('display','block'); $('#askbsw_form').fadeIn('slow'); } function hideForm(){ //$("#askbsw_form").css('display','none'); $('#askbsw_form').fadeOut('fast'); } function showFormSuccess(msg){ $('#askbsw_form_success').empty(); $('#askbsw_form_success').append(msg); $('#askbsw_form_success').fadeIn('slow'); } function hideFormSuccess(){ $('#askbsw_form_success').fadeOut('fast'); } function showFormError(msg){ $('#askbsw_form_error_msg').empty(); $('#askbsw_form_error_msg').append(msg); $('#askbsw_form_error').fadeIn('slow'); } function hideFormError(){ $('#askbsw_form_error').fadeOut('fast'); } function xout_askbsw(){ var cookieName = 'has_xout_askbsw'; var cookieOptions = {expires: null, path: '/'}; var cookieValue = getDateTimeNow(); $.cookie(cookieName, cookieValue, cookieOptions); close_askbsw(); } function close_askbsw(){ $("#askbsw_wrapper").removeClass("askbsw_open"); $("#askbsw_wrapper").addClass("askbsw_closed"); } function open_askbsw(){ var cookieName = 'has_opened_askbsw'; var cookieOptions = {expires: null, path: '/'}; // session cookie var cookieValue = getDateTimeNow(); $.cookie(cookieName, cookieValue, cookieOptions); cookieName = 'has_opened_askbsw_before'; cookieOptions = {expires: 30, path: '/'}; // 30 day cookie cookieValue = getDateTimeNow(); $.cookie(cookieName, cookieValue, cookieOptions); $("#askbsw_wrapper").removeClass("askbsw_closed"); $("#askbsw_wrapper").addClass("askbsw_open"); } function init_newsletterpopup(tm){ run_newsletterpopuptimer(tm); } function close_newsletterpopuptimer(tm,perm){ if( perm ){ setTimeout("xout_askbsw()",tm); // DO IT IN tm SEC } else { setTimeout("close_askbsw()",tm); // DO IT IN tm SEC } } function run_newsletterpopuptimer(tm){ setTimeout("open_askbsw()",tm); // DO IT IN tm SEC } function getDateTimeNow(){ var now = new Date(); return now.getTime(); } function submitAskBSW(){ }