<!-- (c) Julian Shulman 1999

euro        = new euro_object();
value       = null;
decChar     = null;
curCurr     = null;
headingColA = null;
headingColB = null;

function euro_input() {

   with (document) {
      if (typeof(all) == "undefined") {
         writeln('<P Class="note"> Sorry, but the converter requires a Document Object Model enabled browser.</P>');
      } else {
         writeln('<FORM Method="Post" Action="none" onSubmit="checkAmount(null); return false;">');
         writeln('<DIV Class="EuroInput">');
         writeln('<LABEL For="Amount">Input an amount:</LABEL> <INPUT');
         write  (' Type="Text" Name="Amount" Value="" Size="12" Id="Amount"');
         writeln(' onFocus="clearAmount()" onChange="checkAmount(null)">');
         writeln(' and click on the country or currency to convert');
         writeln('</DIV>');
         writeln('</FORM>');
      }
   }
}

function euro_output() {

   var cols = 4;
   with (document) {
      if (typeof(all) != "undefined") {
         writeln('<DIV Class="EuroOutput">');
         writeln('<TABLE CellSpacing=15 Class="grid">')   
         for (var i=0, j=0; i<=euro.totalCountries(); i++) { 
            if (euro.isEuroland(i)) {
               if (j%cols == 0) writeln('<TR>');
               writeln('<TD Class="grid">');
               country_output(i);
               writeln('</TD>');
               if (++j%cols == 0) writeln('</TR>');
            }
         }
         writeln('</TABLE>');
         writeln('</DIV>');
         headingColA = all.selectAEUR.style.color;
         headingColB = all.selectBEUR.style.color;
      }
   }
}

function country_output(curCode) {

   var symbol = euro.currencySymbol(curCode);

   with (document) {       
      writeln('<TABLE Frame="Border" Rules="None" Class="display">'); 
      writeln('<TR><TD Class="country">');
      write  ('<DIV Class="selectA" Id="selectA' + symbol + '"');
      write  (' onMouseDown="checkAmount(euro.' + symbol + ')"');
      write  (' onMouseOut="lowlight(euro.' + symbol + ')"');
      write  (' onMouseOver="highlight(euro.' + symbol + ')">');
      writeln(euro.country(curCode) + '</DIV>');
      writeln('</TD></TR><TR><TD Class="display">');
      write  ('<IMG Class="flag" Src="images/flags/' + symbol.toLowerCase() + '.gif" ');
      write  ('onMouseDown="displayRate(' + curCode + ')" onMouseUp="hideRate(' + curCode + ')" ')
      if (curCode != euro.EUR) {
         writeln('Alt="1 euro = ' + euro.rate(curCode) + ' ' + euro.nationality(curCode) + 
                 ' ' + euro.currency(curCode) + '">');
      } else {
         writeln('Alt="The euro">');
      }
      writeln('</TD></TR><TR><TD Class="display" Id="output' + symbol + '">');
      writeln('&nbsp;');
      writeln('</TD></TR><TR><TD Class="currency">');
      write  ('<DIV Class="selectB" Id="selectB' + symbol + '"');
      write  (' onMouseDown="checkAmount(euro.' + symbol + ')"');
      write  (' onMouseOut="lowlight(euro.' + symbol + ')"');
      write  (' onMouseOver="highlight(euro.' + symbol + ')">');
      writeln(euro.currency(curCode) + '</DIV>');
      writeln('</TD></TR>');
      writeln('</TABLE>');
   }
}

function checkAmount(curCode) {

   if (curCode == null) {
      curCode = curCurr;
   }

   if (curCode != null) {
      with (document.forms[0]) {
         value = Amount.value;
         if (value.length == 0) {
            alert('Please input an amount to convert');
            Amount.focus();
         } else if (!isNumber()) {
            alert('Invalid amount entered');
            Amount.focus();
         } else {
            curCurr = curCode;
            defaultStatus = "converting " + value + ' ' + euro.nationality(curCode) + ' ' + 
                             euro.currency(curCode);  
            highlight(curCurr);
            euro.convert(value, curCode);
            fillAmounts();
         }
      }
   }
}

function highlight(curCode) {
   
   with (document.all) {
      for (var i=0; i<=euro.totalCountries(); i++) { 
         if (euro.isEuroland(i)) {
            headingA = eval('selectA' + euro.currencySymbol(i));
            headingB = eval('selectB' + euro.currencySymbol(i));
            if (i == curCurr) {
               headingA.style.color = "#0000FF";
               headingB.style.color = headingA.style.color;
            } else if (i == curCode) {
               headingA.style.color = "#FF0000";
               headingB.style.color = headingA.style.color;
            } else {
               headingA.style.color = headingColA;
               headingB.style.color = headingColB;
            }
         } 
      }
   }  
}

function lowlight(curCode) {
   
   with (document.all) {
      headingA = eval('selectA' + euro.currencySymbol(curCode));
      headingB = eval('selectB' + euro.currencySymbol(curCode));
      if (curCode != curCurr) {
         headingA.style.color = headingColA;
         headingB.style.color = headingColB;
      }
   } 
   defaultStatus = "" 
}

function clearAmount() {
   with (document.forms[0]) {
      Amount.value = "";
   }
}

function fillAmounts() {

   with (document.all) {
      for (var i=0; i<=euro.totalCountries(); i++) { 
         if (euro.isEuroland(i)) {
            currOut = eval('output' + euro.currencySymbol(i));
            if (euro.valueIn(i) != null) {
               var valueIn = euro.valueIn(i);
               if (decChar != null) { 
                  decPos = valueIn.indexOf('.');
                  if (decPos >=0) valueIn = valueIn.substring(0, decPos) + decChar + valueIn.substring(decPos + 1);
               }   
                 currOut.innerHTML = valueIn;
            } else {
                 currOut.innerHTML = "&nbsp;";
            }
         }
      }
   }  
}

function displayRate(curCode) {
   if (euro.isEuroland(curCode) && (curCode != euro.EUR) ) {
	defaultStatus = "1 euro = " + euro.rate(curCode) + " " + euro.nationality(curCode) 
                    + " " + euro.currency(curCode);
   }
}

function hideRate() {
   defaultStatus = ""
}

function isNumber() {

   decChar = null;
   for (i=0; i<value.length; i++) {
      var valueChar = value.substring(i, i+1)
      if (valueChar < '0' || valueChar > '9') {
         if (decChar == null) {
            if (valueChar == '.' || valueChar == ',') {
               decChar = valueChar;
               value = value.substring(0, i) + '.' + value.substring(i+1);
            } else {
               return false;
            }
         } else {
            return false;
         }
      }
   }
   return true;
}
	
// -->