﻿//-------------------------------------------------------------------------------------------------
var validateFirstNameErrorText;
var thankYou;
var countdownAlertText;
var useYourImagination;
var scriptExampleAlertText;
//.................................................................................................
function setLanguage(language) {
  switch (language) {
    case 'en':
      validateFirstNameErrorText = 'Required information is missing.';
      thankYou = 'Thank you';
      countdownAlertText = 'This is an example of a script that runs without your permission. \n\n' +
'This window opened automatically when the countdown reached 0. \n\n' +
'Your computer just ran this script without you doing anything! \n\n' +
'Don\'t worry; this script does not contain malicious code. \n\n' +
'But imagine what could happen if this script had malicious code \n' +
'and instead of your first name (as in the first example) \n' +
'you had written your credit card number.';
      useYourImagination = 'Use your imagination';
      scriptExampleAlertText = 'This is an example of a script in a ButtonOrLinkPlaceholder. \n\n' +
'You just gave this script permission to run in your computer! \n\n' +
'Don\'t worry; this script does not contain malicious code.';
      break;
      
    case 'es':
      validateFirstNameErrorText = 'Faltan datos obligatorios.';
      thankYou = 'Gracias';
      countdownAlertText = 'Este es un ejemplo de un script que corre sin tu permiso. \n\n' +
'Esta ventana se abrió automáticamente cuando la cuenta regresiva llegó a 0. \n\n' +
'¡Tu computadora acaba de correr este script sin que tú hagas nada! \n\n' +
'No te preocupes; este script no contiene código malicioso. \n\n' +
'Pero imagina lo que podría pasar si este script tuviera código malicioso \n' +
'y en lugar de tu nombre (como en el primer ejemplo) \n' +
'hubieras escrito el número de tu tarjeta de crédito.';
      useYourImagination = 'Usa tu imaginación';
      scriptExampleAlertText = 'Este es un ejemplo de un script en un ButtonOrLinkPlaceholder. \n\n' +
'¡Acabas de dar permiso a este script para correr en tu computadora! \n\n' +
'No te preocupes; este script no contiene código malicioso.';
      break;
  }
}
//.................................................................................................
function getFirstName() {
  var firstNameTextBox = window.document.getElementById('FirstNameTextBox');
  var firstName = firstNameTextBox.value;
  firstName = firstName.replace(/(^\s*)|(\s*$)/g, '');
  return firstName;
}
//.................................................................................................
function validateFirstName() {
  var firstName = getFirstName();
  var firstNameSpan = window.document.getElementById('FirstNameSpan');
  
  if (firstName === '') { firstNameSpan.innerHTML = '<span class="Error">' + validateFirstNameErrorText + '</span>'; }
  else { firstNameSpan.innerHTML = thankYou + ' ' + firstName + '.'; }
}
//.................................................................................................
var secondCount = 120;
//.................................................................................................
function countdown() {
  var countdownSpan = window.document.getElementById('CountdownSpan');
  countdownSpan.innerHTML = secondCount;
  
  if (secondCount === 0) {
    var firstName = getFirstName();
    if (firstName !== '') { countdownAlertText = countdownAlertText + ' \n\n ' + useYourImagination + ' ' + firstName + '.'; }
    
    window.alert(countdownAlertText);
  }
  
  secondCount = secondCount - 1;
  window.setTimeout(countdown, 1000);
}
//.................................................................................................
function scriptExample(ButtonOrLink) {
  window.alert(scriptExampleAlertText.replace('ButtonOrLinkPlaceholder', ButtonOrLink));
  return false;
}
//-------------------------------------------------------------------------------------------------