Wed Aug 23 2017
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
// STEP 1:: Verify that it's a valid email id


/**************************************************
BRITEVERIFY EMAIL VERIFICATION
***************************************************/
function get_token(){
	window.briteverifyToken = false;

	var form_token = '4047df1c-5078-4e96-b335-f61aa2b92891'; // public key
	var url = 'https://forms-api-v1.briteverify.com/api/submissions/view.json?callback=getBriteverifyToken&form_token='+form_token+'&_='+Date.now();

	// append script to body to get cross-domain jsonp data
	var script = document.createElement('script');
	script.src = url;
	document.body.appendChild(script);

	window.getBriteverifyToken = function(data){
		// console.log(data);
		// console.log('token: '+data.token);

		window.briteverifyToken = data.token;

		delete window.getBriteverifyToken;
		document.body.removeChild(script);
	}
}
// if the demo hasn't already been taken, get an email verification token
if(!RSUI.util.getCookie('demotaken')) {
	get_token();
}

function verify_real_email(email,success,failure){
	if(!success){success = function(){};}
	if(!failure){failure = function(){};}
	if(window.briteverifyToken){

		var token = window.briteverifyToken;
		var url = 'https://forms-api-v1.briteverify.com/api/submissions/verify.json?callback=briteverify&form_token=4047df1c-5078-4e96-b335-f61aa2b92891&token='+token+'&email='+email+'&_='+Date.now();

		var script = document.createElement('script');
		script.src = url;
		document.body.appendChild(script);

		// if the script url doesn't work (maybe they changed the api)
		script.onerror = function(e){
			// console.log('onerror');
			// console.log(e);

			// ignore verification and move on
			success();
		};
		// script.onload = function(){this.remove();}

		window.briteverify = function(data){
			try{
				// console.log(data);
				// console.log('validity status: '+data.status);

				// if email is valid
				if(data.status!=='invalid'){
					success();
				}
				// if email is invalid
				else{
					// error handling
					failure();
				}

				document.body.removeChild(script);
			}
			catch(e){
				// if error, ignore verification and move on
				// console.log('jsonp callback error');

				success();
			}
			delete window.briteverify;
		}
	}
	else{
		// if no token, ignore verification and move on
		// console.log('no token');

		success();
	}
}

// STEP 2:: Submit the valid email id's to CIS
/**************************************************
FORM SUBMIT
***************************************************/
$form.submit(function(e) {
	e.preventDefault()

	var $inputEmail = $('input[type=text]')
		, $languageError = $('#language-error')
		, error = false
		, email = $('input[type=text]').val()
		, selectedLanguage = $dropdown.find('.selected').data('code') || 'en-es'
		;

	// This should never happen since to see email field, you have to select a language.
	// but add it for good measure anyway.
	if (!$dropdownLi.find('.selected')) {
		error = true
		$languageError.fadeIn()
	} else {
		$languageError.fadeOut()
	}

	// show or hide email error messages
	function show_email_error(spelling){
		error = true;
		$inputEmail.addClass('input-error');
		if(spelling){
			document.getElementById('spelling_error_message').style.display = 'block';
		}
		else{
			document.getElementById('email_error_message').style.display = 'block';
		}
	}
	function hide_email_error(){
		$inputEmail.removeClass('input-error');
		document.getElementById('email_error_message').style.display = 'none';
		document.getElementById('spelling_error_message').style.display = 'none';
	}


	// Only validate email if user doesn't have the demotaken cookie
	if (!RSUI.util.getCookie('demotaken')) {

		// check if the email format is valid
		if (!validateEmail(email)) {
			show_email_error();
		} else {
			hide_email_error();

			// make sure there's no language dropdown error (should never happen, but just to be safe)
			if(!error){

				// briteverify validation to make sure email is real
				verify_real_email(email,
					function(){
						submitEmailToEC(email, selectedLanguage);
					},
					function(){
						show_email_error('spelling');
					});
			}
		}


	}

	// if no errors, and demo already taken, submit (the demo will open)
	if (!error && RSUI.util.getCookie('demotaken')) {
		submitEmailToEC(email, selectedLanguage);
	}
})


/* ========================== Send Email To EC ========================*/
function submitEmailToEC(email, lang) {
  if (RSUI.util.getCookie('demotaken') || RSUI.util.getCookie('rsdemo')) {
    openDemo(lang);
    return;
  } else {
    var map = {
      'en-fr' : 'fra',
      'en-de' : 'deu',
      'en-it' : 'ita',
      'en-en' : 'eng',
      'en-es' : 'esp'
    }

    var defer = jQuery.Deferred()

    $(document).trigger('demo_email', {
      email: email,
      is_offer: false
    });

    var baseURL = (window.location.href.search(/(\.stg\.)|(local)/i)>-1 ?
          'http://www.stg' :
          'http://www')
          + '.rosettastone.com/?redirect2mobile=no&p_p_id=rosettaajaxsubmit_WAR_rosettaajaxsubmitportlet&p_p_lifecycle=2&data=';

    var data = {
        email : email,
        demo_lang : map[lang] || lang,
        cis_name : 'mobile demo',
        website: 'US_WEBSITE',
        form_type : 'demo',
        demo_type : 'PE',
        form_url : 'hub-rs',
        cid: grabUrlParam('cid') || '',
        newsletter_type : "DTC_mobile-demo"
    }

    var request = jQuery.ajax({
        contentType: "application/json; charset=utf-8",
        url: baseURL + encodeURIComponent(JSON.stringify(data)),
        type: "GET",
        data:data
    });

    request.done(function(msg) {
        if (/"cisFlag":"true"/.test(msg)) {
            // Doty's demo pixel
            $('body').append('<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/1041440066/?label=WzGCCNzOy1gQwrrM8AM&amp;guid=ON&amp;script=0"/>')
            // record the email in a cookie that expires in 30 days
            RSUI.util.setCookie("curEmailIdsc",email)
            RSUI.util.setCookie('demotaken', "1", 1, 30 * 1000 * 60 * 60 * 24);
            //document.cookie = 'curEmailIdsc='+email+'; max-age='+60*60*24*30+'; path=/;';
            defer.resolve('success')
    		openDemo(lang);
        }else{
          $('input[type=text]').addClass('input-error').attr('placeholder', 'Sorry, try again!')
        }

    })
    return defer.promise()
  }
}