var country_blocks = new Array();
var currency_blocks = new Array();

// As rate data feed is set relative to USD, we need to introduce the base currency - TWD
var base_currency = "TWD";

function get_country_type_id(index) {
  return "converter-country-type"+index.toString();
}

function get_country_amount_id(index) {
  return "converter-country-amount"+index.toString()
}

function get_currency_type_id(index) {
  return "converter-type"+index.toString();
}

function get_currency_amount_id(index) {
  return "converter-amount"+index.toString()
}

function make_country_block(name, currency) {
  var id = country_blocks.length;
  
  country_blocks[id] = currency;
  var name1 = get_country_amount_id(id);
  var name2 = get_country_type_id(id);

  document.write("<div class=\"converter-country-block\">");
  document.write("<div class=\"converter-country-name\">"+name+"</div>");
  document.write("<div class=\"converter-country-value\">");
  document.write("<input type=\"text\" name=\""+name1+"\" id=\""+name1+"\" onkeyup=\"update_country("+id+")\" title=\"Type a value here\" size=\"10\" maxlength=\"50\"/>");
  document.write("<input type=\"hidden\" name=\""+name2+"\" id=\""+name2+"\" value=\""+currency+"\"/>");
  document.write("</div>");
  document.write("</div>");
}

function make_country_block2(name, currency) {
  var id = country_blocks.length;
  
  country_blocks[id] = currency;
  var name1 = get_country_amount_id(id);
  var name2 = get_country_type_id(id);

  document.write("<div class=\"converter-country-block\">");
  document.write("<div class=\"converter-country-value\">");
  document.write("<input type=\"text\" value=\""+name+"\" name=\""+name1+"\" id=\""+name1+"\" onkeyup=\"update_country("+id+")\" title=\"Type a value here\" size=\"10\" maxlength=\"50\"/>");
  document.write("<input type=\"hidden\" name=\""+name2+"\" id=\""+name2+"\" value=\""+currency+"\"/>");
  document.write("</div>");
  document.write("</div>");
}

function make_currency_block(num) {
  currency_blocks.push(num);

  document.write("<div class=\"converter-currency-block\">");
  document.write("<select onchange=\"update_tw(false)\" name=\"converter-type"+num+"\" id=\"converter-type"+num+"\" title=\"Select currency\">");
  document.write("<option value=\"Your Currency\" selected=\"selected\">Select...");
  document.write("<option value=\"AUD\">Australia");
  document.write("<option value=\"CAD\">Canada");
  document.write("<option value=\"EUR\">EURO");
  document.write("<option value=\"INR\">India");
  document.write("<option value=\"IEP\">Ireland");
  document.write("<option value=\"JMD\">Jamaica");
  document.write("<option value=\"NZD\">New Zealand");
  document.write("<option value=\"NGN\">Nigeria");
  document.write("<option value=\"PGK\">P.N.Guinea");
  document.write("<option value=\"PKR\">Pakistan");
  document.write("<option value=\"ZAR\">South Africa");
  document.write("<option value=\"UGX\">Uganda");
  document.write("<option value=\"GBP\">UK");
  document.write("<option value=\"USD\">USA");
  document.write("<option value=\"---\">---------");
  document.write("<option value=\"AFA\">Afghanistan");
  document.write("<option value=\"ALL\">Albania");
  document.write("<option value=\"DZD\">Algeria");
  document.write("<option value=\"ARS\">Argentina");
  document.write("<option value=\"AUD\">Australia");
  document.write("<option value=\"ATS\">Austria");
  document.write("<option value=\"BSD\">Bahamas");
  document.write("<option value=\"BBD\">Barbados");
  document.write("<option value=\"BEF\">Belgium");
  document.write("<option value=\"BMD\">Bermuda");
  document.write("<option value=\"BRL\">Brazil");
  document.write("<option value=\"BND\">Brunei");
  document.write("<option value=\"BGL\">Bulgaria");
  document.write("<option value=\"CAD\">Canada");
  document.write("<option value=\"CLP\">Chile");
  document.write("<option value=\"CNY\">China");
  document.write("<option value=\"COP\">Colombia");
  document.write("<option value=\"CRC\">Costa Rica");
  document.write("<option value=\"CYP\">Cyprus");
  document.write("<option value=\"CZK\">Czech Republic");
  document.write("<option value=\"DKK\">Denmark");
  document.write("<option value=\"EGP\">Egypt");
  document.write("<option value=\"EUR\">EURO");
  document.write("<option value=\"FJD\">Fiji");
document.write("<option value=\"FIM\">Finland");
document.write("<option value=\"FRF\">France");
document.write("<option value=\"DEM\">Germany");
document.write("<option value=\"GRD\">Greece");
document.write("<option value=\"HKD\">Hong Kong");
document.write("<option value=\"HUF\">Hungary");
document.write("<option value=\"ISK\">Iceland");
document.write("<option value=\"INR\">India");
document.write("<option value=\"IDR\">Indonesia");
document.write("<option value=\"IQD\">Iraq Dinars");
document.write("<option value=\"IEP\">Ireland");
document.write("<option value=\"ILS\">Israel");
document.write("<option value=\"ITL\">Italy");
document.write("<option value=\"JMD\">Jamaica");
document.write("<option value=\"JPY\">Japan Yen");
document.write("<option value=\"JOD\">Jordan");
document.write("<option value=\"KWD\">Kuwait");
document.write("<option value=\"LBP\">Lebanon");
document.write("<option value=\"LUF\">Luxembourg");
document.write("<option value=\"MOP\">Macau");
document.write("<option value=\"MYR\">Malaysia");
document.write("<option value=\"MTL\">Malta");
document.write("<option value=\"MXN\">Mexico");
document.write("<option value=\"MAD\">Morocco");
document.write("<option value=\"NLG\">Netherlands");
document.write("<option value=\"NZD\">New Zealand");
document.write("<option value=\"NGN\">Nigeria");
document.write("<option value=\"NOK\">Norway");
document.write("<option value=\"PKR\">Pakistan");
document.write("<option value=\"PGK\">P.N.Guinea");
document.write("<option value=\"PHP\">Philippines");
document.write("<option value=\"PLN\">Poland");
document.write("<option value=\"PTE\">Portugal");
document.write("<option value=\"ROL\">Romania");
document.write("<option value=\"RUR\">Russia");
document.write("<option value=\"SAR\">Saudi Arabia");
document.write("<option value=\"SGD\">Singapore");
document.write("<option value=\"SKK\">Slovakia");
document.write("<option value=\"ZAR\">South Africa");
document.write("<option value=\"KRW\">South Korea");
document.write("<option value=\"ESP\">Spain");
document.write("<option value=\"SDD\">Sudan");
document.write("<option value=\"SEK\">Sweden");
document.write("<option value=\"CHF\">Switzerland");
document.write("<option value=\"THB\">Thailand");
document.write("<option value=\"TTD\">Trinidad & Tobago");
document.write("<option value=\"TRL\">Turkey");
document.write("<option value=\"AED\">UAE");
document.write("<option value=\"UGX\">Uganda");
document.write("<option value=\"GBP\">UK");
document.write("<option value=\"USD\">USA");
document.write("<option value=\"VEB\">Venezuela");
document.write("<option value=\"VND\">Vietnam");
document.write("<option value=\"ZMK\">Zambia");
document.write("</option>");
document.write("</select>");
document.write("<br/>");
document.write("<input onkeyup=\"update_currency("+num+")\" size=\"12\" name=\"converter-amount"+num+"\" id=\"converter-amount"+num+"\" title=\"Enter the currency amount\" onfocus=\"javascript: selectAll(this);\"/>");
document.write("</div>");
}

function make_currency_block2(num) {
  currency_blocks.push(num);

  document.write("<div class=\"converter-currency-block2\">");
  document.write("<select onchange=\"update_tw(false)\" name=\"converter-type"+num+"\" id=\"converter-type"+num+"\" title=\"Select currency\">");
  document.write("<option value=\"currency\" selected=\"selected\">currency");
  document.write("<option value=\"AUD\">Australia");
  document.write("<option value=\"CAD\">Canada");
  document.write("<option value=\"EUR\">EURO");
  document.write("<option value=\"INR\">India");
  document.write("<option value=\"IEP\">Ireland");
  document.write("<option value=\"JMD\">Jamaica");
  document.write("<option value=\"NZD\">New Zealand");
  document.write("<option value=\"NGN\">Nigeria");
  document.write("<option value=\"PGK\">P.N.Guinea");
  document.write("<option value=\"PKR\">Pakistan");
  document.write("<option value=\"ZAR\">South Africa");
  document.write("<option value=\"UGX\">Uganda");
  document.write("<option value=\"GBP\">UK");
  document.write("<option value=\"USD\">USA");
  document.write("<option value=\"---\">---------");
  document.write("<option value=\"AFA\">Afghanistan");
  document.write("<option value=\"ALL\">Albania");
  document.write("<option value=\"DZD\">Algeria");
  document.write("<option value=\"ARS\">Argentina");
  document.write("<option value=\"AUD\">Australia");
  document.write("<option value=\"ATS\">Austria");
  document.write("<option value=\"BSD\">Bahamas");
  document.write("<option value=\"BBD\">Barbados");
  document.write("<option value=\"BEF\">Belgium");
  document.write("<option value=\"BMD\">Bermuda");
  document.write("<option value=\"BRL\">Brazil");
  document.write("<option value=\"BND\">Brunei");
  document.write("<option value=\"BGL\">Bulgaria");
  document.write("<option value=\"CAD\">Canada");
  document.write("<option value=\"CLP\">Chile");
  document.write("<option value=\"CNY\">China");
  document.write("<option value=\"COP\">Colombia");
  document.write("<option value=\"CRC\">Costa Rica");
  document.write("<option value=\"CYP\">Cyprus");
  document.write("<option value=\"CZK\">Czech Republic");
  document.write("<option value=\"DKK\">Denmark");
  document.write("<option value=\"EGP\">Egypt");
  document.write("<option value=\"EUR\">EURO");
  document.write("<option value=\"FJD\">Fiji");
document.write("<option value=\"FIM\">Finland");
document.write("<option value=\"FRF\">France");
document.write("<option value=\"DEM\">Germany");
document.write("<option value=\"GRD\">Greece");
document.write("<option value=\"HKD\">Hong Kong");
document.write("<option value=\"HUF\">Hungary");
document.write("<option value=\"ISK\">Iceland");
document.write("<option value=\"INR\">India");
document.write("<option value=\"IDR\">Indonesia");
document.write("<option value=\"IQD\">Iraq Dinars");
document.write("<option value=\"IEP\">Ireland");
document.write("<option value=\"ILS\">Israel");
document.write("<option value=\"ITL\">Italy");
document.write("<option value=\"JMD\">Jamaica");
document.write("<option value=\"JPY\">Japan Yen");
document.write("<option value=\"JOD\">Jordan");
document.write("<option value=\"KWD\">Kuwait");
document.write("<option value=\"LBP\">Lebanon");
document.write("<option value=\"LUF\">Luxembourg");
document.write("<option value=\"MOP\">Macau");
document.write("<option value=\"MYR\">Malaysia");
document.write("<option value=\"MTL\">Malta");
document.write("<option value=\"MXN\">Mexico");
document.write("<option value=\"MAD\">Morocco");
document.write("<option value=\"NLG\">Netherlands");
document.write("<option value=\"NZD\">New Zealand");
document.write("<option value=\"NGN\">Nigeria");
document.write("<option value=\"NOK\">Norway");
document.write("<option value=\"PKR\">Pakistan");
document.write("<option value=\"PGK\">P.N.Guinea");
document.write("<option value=\"PHP\">Philippines");
document.write("<option value=\"PLN\">Poland");
document.write("<option value=\"PTE\">Portugal");
document.write("<option value=\"ROL\">Romania");
document.write("<option value=\"RUR\">Russia");
document.write("<option value=\"SAR\">Saudi Arabia");
document.write("<option value=\"SGD\">Singapore");
document.write("<option value=\"SKK\">Slovakia");
document.write("<option value=\"ZAR\">South Africa");
document.write("<option value=\"KRW\">South Korea");
document.write("<option value=\"ESP\">Spain");
document.write("<option value=\"SDD\">Sudan");
document.write("<option value=\"SEK\">Sweden");
document.write("<option value=\"CHF\">Switzerland");
document.write("<option value=\"THB\">Thailand");
document.write("<option value=\"TTD\">Trinidad & Tobago");
document.write("<option value=\"TRL\">Turkey");
document.write("<option value=\"AED\">UAE");
document.write("<option value=\"UGX\">Uganda");
document.write("<option value=\"GBP\">UK");
document.write("<option value=\"USD\">USA");
document.write("<option value=\"VEB\">Venezuela");
document.write("<option value=\"VND\">Vietnam");
document.write("<option value=\"ZMK\">Zambia");
document.write("</option>");
document.write("</select>");
document.write("");
document.write("<input onkeyup=\"update_currency("+num+")\" size=\"12\" name=\"converter-amount"+num+"\" id=\"converter-amount"+num+"\" title=\"Enter the currency amount\" onfocus=\"javascript: selectAll(this);\"/>");
document.write("</div>");
}

function validate_amount(amount) {
  // Empty amount value is not valid, 
  // but should not be colored
  //
  if (amount.value == "") { 
    amount.className = 'goodvalue';
    return false; 
  };

  var matches = amount.value.match(/^\d([\d, `']*)(\.\d+)?$/);
  
  if (matches == null) {
    amount.className = 'badvalue';
    return false;
  };

  amount.className = 'goodvalue';
  return true;
} 

function validate_type(type) {
  if (!rate[type.value]) { 
//    type.className = 'badvalue';
    return false;
  };

//  type.className = 'goodvalue';
  return true;
}

// Note that all currency values are acutoconverted into taiwan
// currency!
//
function amount_to_num(currency, amount) {
  var cleared = amount.replace(/[ `',]/g,'');

  if (cleared == '') {
    cleared = "0";
  };

  return convert_currency_from(currency,parseFloat(cleared));
}

function num_to_amount(currency, num) {
  var currency_num = convert_currency_to(currency, num);

  var fraction = Math.floor(currency_num*100+0.50001) % 100;  
  // Add leading zero to fractional part, if required)
  if (fraction < 10) { fraction = "0" + fraction.toString(); };


  // Split integer value into groups of three digits separated by commas
  var integer = Math.floor(currency_num).toString();
  var integer_str = ""
  for (var i = integer.length-1; i >= 0; i-=3 ) {
    integer_str = "," + integer.substr(i-3 <  0  ? 0 : i-2, 
                                       i-3 >= 0  ? 3 : i+1) + integer_str;
  };
  integer_str = integer_str.substr(1); 

  // Check if there's no data in integer string
  if (integer_str == "") { integer_str = "0"; };

  return integer_str + "." + fraction;
}

function convert_currency_from(currency, value) {
  if (currency == "") { return value; };

  return Math.floor(value / rate[currency] * rate[base_currency] * 100 + 0.50001) / 100;
}

function convert_currency_to(currency, value) {
  if (currency == "") { return value; };

  return Math.floor(value * rate[currency] / rate[base_currency] * 100 + 0.50001) / 100;
}

function update(taiwan_amount, currency, amount) {
  // If currency is not set, just clear the amount value
  if (currency.value == "") { 
    amount.value = "";
    return; 
  };

  // if currency is not selected, do not change its color
  if (currency.value != "---" || amount.value != "") {
    if (validate_type(currency)) {
      amount.value = num_to_amount(currency.value, taiwan_amount);
      amount.className = 'goodvalue';
    };
  } else {
    currency.className = "goodvalue";
  };
}

function update_country(index) {
  var amount = document.getElementById(get_country_amount_id(index));
  var type   = document.getElementById(get_country_type_id(index));

  // Check if entered value can be converted to a number
  //
  if (!validate_amount(amount)) { return; };
  if (!validate_type(type)) { return; };

  // Convert entered amount to a numeric value (possibly removing the commas)
  // 
  var base_amount = amount_to_num(type.value,amount.value);

  // Update all other values
  //
  document.getElementById('converter-taiwan-amount').value = num_to_amount("",base_amount);
  document.getElementById('converter-taiwan-amount').className = 'goodvalue';

  // Update country values
  //
  for (var i=0; i<country_blocks.length; i++) {
    if (index != i) {
      update(base_amount,
             document.getElementById(get_country_type_id(i)),
             document.getElementById(get_country_amount_id(i)));
    };
  };

  // Update currency values
  //
  for (var i=0; i<currency_blocks.length; i++) {
    update(base_amount,
           document.getElementById(get_currency_type_id(currency_blocks[i])),
           document.getElementById(get_currency_amount_id(currency_blocks[i])));
  };
}

// @param emptyZero true  - treat empty value in taiwan currency field as zero
//                  false - treat empty value in taiwan currency field as invalid empty value
//
function update_tw(emptyZero) {
  var amount = document.getElementById('converter-taiwan-amount');

  // Check if entered value can be converted to a number
  //
  var base_amount;
  if (amount.value == "" && emptyZero) {
    // Empty value in "taiwan" field is treated as zero
    //
    base_amount = 0;
  } else {
    if (!validate_amount(amount)) { return; };

    // Convert entered amount to a numeric value (possibly removing the commas)
    // 
    base_amount = amount_to_num("",amount.value);
  };

  // Update all other values
  //
  for (var i=0; i<country_blocks.length; i++) {
    update(base_amount,
           document.getElementById(get_country_type_id(i)),
           document.getElementById(get_country_amount_id(i)));
  };

  // Update currency values
  //
  for (var i=0; i<currency_blocks.length; i++) {
    update(base_amount,
           document.getElementById(get_currency_type_id(currency_blocks[i])),
           document.getElementById(get_currency_amount_id(currency_blocks[i])));
  };
}

function update_currency(num) {
  var amount = document.getElementById(get_currency_amount_id(num));
  var type   = document.getElementById(get_currency_type_id(num));

  // Check if entered value can be converted to a number
  //
  if (!validate_amount(amount)) { return; };
  if (!validate_type(type)) { return; };

  // Convert entered amount to a numeric value (possibly removing the commas)
  // 
  var base_amount = amount_to_num(type.value,amount.value);

  // Update all other values
  //
  document.getElementById('converter-taiwan-amount').value = num_to_amount("",base_amount);
  document.getElementById('converter-taiwan-amount').className = 'goodvalue';

  // Update country values
  //
  for (var i=0; i<country_blocks.length; i++) {
    update(base_amount,
           document.getElementById(get_country_type_id(i)),
           document.getElementById(get_country_amount_id(i)));
  };

  // Update currency values
  //
  for (var i=0; i<currency_blocks.length; i++) {
    if (currency_blocks[i] != num) {
      update(base_amount,
             document.getElementById(get_currency_type_id(currency_blocks[i])),
             document.getElementById(get_currency_amount_id(currency_blocks[i])));
    };
  };
}
