/* Euro Convert - (C) Pim van Mun - Developed by: Pim van Mun - mun at wxs dot nl, PHP feed Patrick OBeirne Sysmod.com */

var ConvertButtonText, eur, v_eur_value, lastchange, language, updated;

ConvertButtonText = 'Convert';

eur = 1.0;
v_eur_value = eur ;
lastchange = 'v_eur';
language = 1;

// Errormessages in all languages.
error  = 'The given input is incorrect, it has to be a valid value. For example: 7.75';
error0 = 'U heeft een niet geldige invoer ingegeven, het dient een getal te zijn. Bijvoorbeeld: 7.75\nLet op: Geen komma\'s gebruiken.';
error1 = 'The given input is incorrect, it has to be a valid value. For example: 7.75';
error2 = 'Die Eingabe ist falsch, es muß ein gültiger Wert sein. Z.B.: 7.75';
error3 = 'L\'entrée donnée est incorrecte, il doit ętre une valeur valide. Par exemple: 7.75';
error4 = 'La entrada de información dada es incorrecta, él tiene que ser un valor válido. Por ejemplo: 7.75';
error5 = 'Il dato input č errato, esso deve essere un valore valido. Per esempio: 7.75';

function newval(formfieldname)
// Checks if the given name is the field which has changed, true if so, otherwise false;
// Pre : formfieldname
// Post: true: formfieldname is last changed field, otherwise false.
{
   if (formfieldname == lastchange) 
   {  
      return true;
   }
   else return false;
}

function calculate(curvalue)
// Calculates the value entered by the user to a value in the given currency (curvalue);
// Pre : Formfield contains new value.
// Post: New value calculated for the global formfield value in the given currency (curvalue);
{
   var formvalue = parseFloat(eval('document.euroform.' + lastchange + '.value'));
   
   return (Math.round(((formvalue / (eval(lastchange.substring(2,99))) * curvalue)) * 100)/100);
}

function InputOk()
// Check if the given value in the formfield is a valid value with which we can compute.
// Pre : Value given and button convert clicked.
// Post: If correct input return true, if false return errormessage and false.
{
   var tempInput = parseFloat(eval('document.euroform.' + lastchange + '.value'));

   if (!(tempInput))
   {
      alert(eval('error' + language));
      return false;
   }
   else
   {
      eval('document.euroform.' + lastchange + '.value = ' + tempInput);
      return true;
   }
}

function convert()
// Converts the given value to all the other currencies including the Euro.
// Pre : User has pressed enter or has pressed the convert button.
// Post: Values in the fields contain the converted values, if false input then alert.
{ 
   if (!(InputOk())) return;
   
   with (document.euroform)
   {
      if (!(newval('v_eur'))) v_eur.value = calculate(eur);
      if (!(newval('v_iep'))) v_iep.value = calculate(iep);
      if (!(newval('v_itl'))) v_itl.value = calculate(itl);
      if (!(newval('v_grd'))) v_grd.value = calculate(grd);
      if (!(newval('v_pte'))) v_pte.value = calculate(pte);
      if (!(newval('v_esp'))) v_esp.value = calculate(esp);
      if (!(newval('v_bef'))) v_bef.value = calculate(bef);
      if (!(newval('v_luf'))) v_luf.value = calculate(luf);
      if (!(newval('v_ats'))) v_ats.value = calculate(ats);
      if (!(newval('v_frf'))) v_frf.value = calculate(frf);
      if (!(newval('v_fim'))) v_fim.value = calculate(fim);
      if (!(newval('v_nlg'))) v_nlg.value = calculate(nlg);
      if (!(newval('v_dem'))) v_dem.value = calculate(dem);
      if (!(newval('v_cyp'))) v_cyp.value = calculate(cyp);
      if (!(newval('v_mlt'))) v_mlt.value = calculate(mlt);
      if (!(newval('v_sit'))) v_sit.value = calculate(sit);
      if (!(newval('v_skk'))) v_sit.value = calculate(skk);
   }
}


/* Initializes formfields to value of 1 euro
   can only do when form loaded and variables defined
   Pre : Page and variables all loaded
   Post: All values displayed in the formfields
*/
function GetVal()
{
   lastchange = 'v_eur';
   with (document.euroform)
   {
      v_eur.value = 1.0;
      v_eur.value = eur;
      v_iep.value = iep;
      v_itl.value = itl;
      v_grd.value = grd;
      v_pte.value = pte;
      v_esp.value = esp;
      v_bef.value = bef;
      v_luf.value = luf;
      v_ats.value = ats;
      v_frf.value = frf;
      v_fim.value = fim;
      v_nlg.value = nlg;
      v_dem.value = dem;
      v_cyp.value = cyp;
      v_mlt.value = mlt;
      v_sit.value = sit;
      v_skk.value = skk;
   }
}


function v_changed(currency)
// Sets the global variable with the name of the currency field.
// Pre : User has changed a formfield.
// Post: Global var contains name of the changed formfield.
{
   lastchange = '' + currency + '';
}
