function decodeNumber(n) {
  return Math.round(Math.acos(n) * 100000 * 100) / 100;
}

function encodeNumber(n) {
  return Math.cos(n / 100000);
}

function makeItemNumber(lang, easy, medium, difficult, evil, countperpage) {
  // Check the language
  lang = lang.toUpperCase();
  langs = new Array('EN','FR', 'DE');
  found = false;
  for (i=0; i<langs.length; i++)
    if (lang == langs[i]) {
      found = true;
      break;
    }
  if (!found)
    lang = 'EN';
}

function calculatePrice(easy, medium, difficult, evil) {
  switch (easy + medium + difficult + evil) {
    case 20: return 1.99;
    case 50: return 3.99;
    case 100: return 6.99;
    default: return '---';
  }
}

function langToStr(lang) {
  switch (lang.toUpperCase()) {
    case 'EN': return 'English';
    case 'FR': return 'Français';
    case 'DE': return 'Deutsch';
    case 'IT': return 'Italiano';
    case 'SP': return 'Español';
    case 'RU': return 'Russki';
    default: return 'English';
  }
}

function buyNowToStr(lang) {
  switch (lang.toUpperCase()) {
    case 'EN': return 'Buy Now';
    case 'FR': return 'Commande';
    case 'DE': return 'Bestellung';
    default: return 'Buy Now';
  }
}

function addppForm(lang, easy, medium, difficult, evil, pp) {
  itemnumber = lang + '-' + easy + '-' + medium + '-' + difficult + '-' + evil + '-' + pp;
  document.write(itemnumber) + '<br>';
  document.write(langToStr(lang)) + '<br>';
  document.write('$' + calculatePrice(easy, medium, difficult, evil));
  document.write('<form action="https://www.paypal.com/cgi-bin/webscr" method="post">');
  document.write('<input type="hidden" name="cmd" value="_xclick">');
  document.write('<input type="hidden" name="business" value="info@sudoku129.com">');
  document.write('<input type="hidden" name="undefined_quantity" value="1">');
  document.write('<input type="hidden" name="item_name" value="Sudoku129 PDF Booklet">');
  document.write('<input type="hidden" name="item_number" value="' + itemnumber + '">');
  document.write('<input type="hidden" name="amount" value="' + calculatePrice(easy, medium, difficult, evil) + '">');
  document.write('<input type="hidden" name="no_shipping" value="1">');
  document.write('<input type="hidden" name="return" value="http://sudoku129.com/purchased.php">');
  document.write('<input type="hidden" name="cancel_return" value="http://sudoku129.com/canceled.php">');
  document.write('<input type="hidden" name="no_note" value="1">');
  document.write('<input type="hidden" name="currency_code" value="USD">');
  document.write('<input type="hidden" name="bn" value="PP-BuyNowBF">');
  document.write('<input type="image" src="./images/btBuyNow' + lang + '.gif" border="0" name="submit" alt="' + buyNowToStr(lang) +'">');
  document.write('</form>');
}

// Total
function getTotal() {
  return +document.getElementById('sudonumber').value;
}

function setTotal(n) {
  document.getElementById('sudonumber').value = n;
}

// Levels
function getEasyCount() {
  return +document.getElementById('easycount').value;
}

function getMediumCount() {
  return +document.getElementById('mediumcount').value;
}

function getDifficultCount() {
  return +document.getElementById('difficultcount').value;
}

function getEvilCount() {
  return +document.getElementById('evilcount').value;
}

function setEasyCount(n) {
  document.getElementById('easycount').value = n;
}

function setMediumCount(n) {
  document.getElementById('mediumcount').value = n;
}

function setDifficultCount(n) {
  document.getElementById('difficultcount').value = n;
}

function setEvilCount(n) {
  document.getElementById('evilcount').value = n;
}

function getPP(lang) {
  if (document.getElementById('rb1pp').checked) {
    if (lang == 'FR')
      document.getElementById('recappp').innerHTML = '1 grille par page';
    else
    if (lang == 'DE')
      document.getElementById('recappp').innerHTML = '1 Rätsel pro Seite';
    else
      document.getElementById('recappp').innerHTML = '1 puzzle per page';
    return 1;
  }
  else if (document.getElementById('rb2pp').checked) {
    if (lang == 'FR')
      document.getElementById('recappp').innerHTML = '2 grilles par page';
    else
    if (lang == 'DE')
      document.getElementById('recappp').innerHTML = '2 Rätsel pro Seite';
    else
      document.getElementById('recappp').innerHTML = '2 puzzles per page';
    return 2;
  }
  else if (document.getElementById('rb4pp').checked) {
    if (lang == 'FR')
      document.getElementById('recappp').innerHTML = '4 grilles par page';
    else
    if (lang == 'DE')
      document.getElementById('recappp').innerHTML = '4 Rätsel pro Seite';
    else
      document.getElementById('recappp').innerHTML = '4 puzzles per page';
    return 4;
  }
}

function getItemNumber(lang) {
  return lang + '-' + getEasyCount() + '-' + getMediumCount() + '-' + getDifficultCount() + '-' + getEvilCount() + '-' + getPP();
}

function getPrice() {
  total = getTotal();
  switch (total) {
  case 10:
    price = 1.99; break;
  case 20:
    price = 2.99; break;
  case 30:
    price = 3.69; break;
  case 40:
    price = 4.29; break;
  case 50:
    price = 4.79; break;
  case 60:
    price = 5.19; break;
  case 70:
    price = 5.59; break;
  case 80:
    price = 5.99; break;
  case 90:
    price = 6.39; break;
  case 100:
    price = 6.69; break;
  default:
    price = 9;
  }
  document.getElementById("amount").value = price;
  return price;
}

function addSudokuPPForm(lang) {
  document.write('<form action="https://www.paypal.com/cgi-bin/webscr" method="post">');
  document.write('<input type="hidden" name="cmd" value="_xclick">');
  document.write('<input type="hidden" name="business" value="info@sudoku129.com">');
  //document.write('<input type="hidden" name="undefined_quantity" value="1">');
  document.write('<input type="hidden" name="item_name" value="Sudoku129 PDF Booklet">');
  document.write('<input type="hidden" name="item_number" value="--" id="itemnumber">');
  document.write('<input type="hidden" name="amount" value="9" id="amount">');
  document.write('<input type="hidden" name="no_shipping" value="1">');
  document.write('<input type="hidden" name="return" value="http://sudoku129.com/puzzles/purchased.php">');
  document.write('<input type="hidden" name="cancel_return" value="http://sudoku129.com/puzzles/purchased.php">');
  document.write('<input type="hidden" name="no_note" value="1">');
  document.write('<input type="hidden" name="currency_code" value="USD">');
  document.write('<input type="hidden" name="bn" value="PP-BuyNowBF">');
  document.write('<input type="image" src="../images/btBuyNow' + lang + '.gif" border="0" name="submit" alt="' + buyNowToStr(lang) +'">');
  document.write('</form>');
}

function checkBookletFormValid() {
  document.getElementById('formemail').value = document.getElementById('recapemail').value;
	//alert(document.getElementById('formemail').value);
	return (emailCheck(document.getElementById('formemail').value));
}

function addSudokuBookletForm(rootpath, lang) {
  document.write('<form action="' + rootpath + '/inc/sendpdfmail.php" method="post" onSubmit="return checkBookletFormValid()">');
  document.write('<input type="hidden" name="easy" id="formeasy" value="5">');
  document.write('<input type="hidden" name="medium" id="formmedium" value="5">');
  document.write('<input type="hidden" name="difficult" id="formdifficult" value="5">');
  document.write('<input type="hidden" name="evil" id="formevil" value="5">');
  document.write('<input type="hidden" name="pp" id="formpp" value="2">');
  document.write('<input type="hidden" name="lang" value="' + lang + '">');
  document.write('<input ttype="hidden" name="recipient" id="formemail" value="">');
  //document.write('<input type="image" src="../images/btSendBooklet' + lang + '.gif" border="0" name="submit" alt="' + buyNowToStr(lang) +'" onbeforeactivate="document.getElementById(\'formemail\').value = document.getElementById(\'recapemail\').value; alert(\'ok\');">');
  document.write('<input type="submit" src="../images/btSendBooklet' + lang + '.gif" border="0" name="submit" value="submit" alt="' + buyNowToStr(lang) +'">');
  document.write('</form>');
}
