﻿// JScript File
var saw_CountryId="", saw_NumCopiesId="", saw_EditionId="", saw_supportId="", saw_MediaId="", saw_priceId="", saw_vatId="", saw_totalId="", saw_currencyId="";

function formatCurrency(num,prefix) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + prefix + num + '.' + cents);
}

function fnUpdateCost()
{
    try
    {
        if (saw_CountryId == "")
        {
          document.getElementById(saw_priceId).innerText = "";
          document.getElementById(saw_vatId).innerText = "";
          return;
        }

        // get country...
        var countryCode = "", currency = "USD";
        countryCode = document.getElementById(saw_CountryId).options[document.getElementById(saw_CountryId).selectedIndex].value;
        
        if (countryCode == "")
        {
          document.getElementById(saw_priceId).innerText = "[Please select Country above]";
          document.getElementById(saw_vatId).innerText = "";
          return;
        }
    }
    catch (e)
    {
      return;
    }
        
    try
    {
        document.getElementById(saw_priceId).style.fontSize="18px";
    }
    catch (e)
    {
    }
    
    // get num copies
    var numCopies = 1;
    var currency, currencySymbol;
    
    try
    {
      numCopies = 1 * Number(document.getElementById(saw_NumCopiesId).value);
    }
    catch (e)
    {
      try
      { 
        document.getElementById(saw_NumCopiesId).value = 1;
      }
      catch (e2)
      {
      }
    }
    
    // work out currency
    if (countryCode == "AT" || countryCode == "BE" || countryCode == "BE" || countryCode == "CY" || countryCode == "FI" || countryCode == "FR" || countryCode == "DE"
            || countryCode == "GR" || countryCode == "IE" || countryCode == "IT" || countryCode == "LU" || countryCode == "MT" || countryCode == "NL"
            || countryCode == "PO" || countryCode == "SI" || countryCode == "ES" || countryCode == "YT" || countryCode == "MC" || countryCode == "SM" || countryCode == "AD" || countryCode == "CH" || countryCode == "PT"
            || countryCode == "BA" || countryCode == "BG" || countryCode == "MK" || countryCode == "CZ" || countryCode == "HU")
            currency = "EUR"; 
    else if (countryCode == "GB")
            currency = "GBP";
    else
            currency = "USD";
            
    var enterprise = 0; 
    var media = document.getElementById(saw_MediaId).selectedIndex; 
    var support = document.getElementById(saw_supportId).selectedIndex;
    var total = 0.0;
    var vat = 0.0;
    
    // now work out cost
    if (currency == "USD")
    {
      currencySymbol = "$";
      if (enterprise > 0)
      {
        total = 499 * numCopies;
      }
      else
      {
        total = 499 * numCopies;
      }
      
      if (support > 0 && media > 0)
      {
        total = total * 1.8;
      }
      else if (support > 0)
      {
        total = total * 1.4;
      }
      else if (media > 0)
      {
        total = total * 1.4;
      }
    }
    else if (currency == "GBP")
    {
      currencySymbol = "£";
      if (enterprise > 0)
      {
        total = 295 * numCopies;
      }
      else
      {
        total = 295 * numCopies;
      }
      
      if (support > 0 && media > 0)
      {
        total = total * 1.8;
      }
      else if (support > 0)
      {
        total = total * 1.4;
      }
      else if (media > 0)
      {
        total = total * 1.4;
      }
      
      vat = total * 0.175;
    }
    else
    {
      currencySymbol = "EURO ";
    
      if (enterprise > 0)
      {
        total = 375 * numCopies;
      }
      else
      {
        total = 375 * numCopies;
      }
      
      if (support > 0 && media > 0)
      {
        total = total * 1.8;
      }
      else if (support > 0)
      {
        total = total * 1.4;
      }
      else if (media > 0)
      {
        total = total * 1.4;
      }

      if (countryCode == "AT" || countryCode == "BE" || countryCode == "BE" || countryCode == "CY" || countryCode == "FI" || countryCode == "FR" || countryCode == "DE"
            || countryCode == "GR" || countryCode == "IE" || countryCode == "IT" || countryCode == "LU" || countryCode == "MT" || countryCode == "NL"
            || countryCode == "SI" || countryCode == "ES" || countryCode == "YT" || countryCode == "MC" || countryCode == "SM" || countryCode == "AD" || countryCode == "PT")
          vat = total * 0.175;
    }
    
    // store currency
    document.getElementById(saw_totalId).value=total + vat;
    document.getElementById(saw_currencyId).value=currency;
    
    // now populate total
    document.getElementById(saw_priceId).innerText = formatCurrency(total, currencySymbol);

    if (vat == 0.0)
    {
      if (currency == "USD")
        document.getElementById(saw_vatId).innerText = "(U.S.)";
      else
        document.getElementById(saw_vatId).innerText = "";
    }
    else
    {
      document.getElementById(saw_vatId).innerText = " (plus VAT at 17.5% : " + formatCurrency(vat, currencySymbol) + ")";
    }    
}

