/* 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_usd'))) v_usd.value = calculate(usd);
      if (!(newval('v_jpy'))) v_jpy.value = calculate(jpy);
      if (!(newval('v_dkk'))) v_dkk.value = calculate(dkk);
      if (!(newval('v_gbp'))) v_gbp.value = calculate(gbp);
      if (!(newval('v_sek'))) v_sek.value = calculate(sek);
      if (!(newval('v_chf'))) v_chf.value = calculate(chf);
      if (!(newval('v_hrk'))) v_hrk.value = calculate(hrk);
      if (!(newval('v_nok'))) v_nok.value = calculate(nok);
      if (!(newval('v_bgn'))) v_bgn.value = calculate(bgn);
      if (!(newval('v_czk'))) v_czk.value = calculate(czk);
      if (!(newval('v_eek'))) v_eek.value = calculate(eek);
      if (!(newval('v_huf'))) v_huf.value = calculate(huf);
      if (!(newval('v_ltl'))) v_ltl.value = calculate(ltl);
      if (!(newval('v_lvl'))) v_lvl.value = calculate(lvl);
      if (!(newval('v_pln'))) v_pln.value = calculate(pln);
   }    
}


/* 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;
      	if(typeof(usd)=="undefined"){ 
      		v_usd.value = 0;	
    		usd = 0;
	} else {
		v_usd.value = usd;
	}
	if(typeof(jpy)=="undefined"){
		v_jpy.value = 0;
		jpy = 0;
	} else {
		v_jpy.value = jpy;
	}
	if(typeof(dkk)=="undefined"){ 
		v_dkk.value = 0;
		dkk = 0;
	}else {	
		v_dkk.value = dkk;
	}
	if(typeof(gbp)=="undefined"){ 
		v_gbp.value = 0;
		gbp = 0;
	} else {
		v_gbp.value = gbp;
	}
	if(typeof(sek)=="undefined"){
		v_sek.value = 0;
		sek = 0;
	} else {
		v_sek.value = sek;
	}
	if(typeof(chf)=="undefined"){
		v_chf.value = 0;
		chf = 0;
	} else {
		v_chf.value = chf;
	}
	if(typeof(hrk)=="undefined"){
		v_hrk.value = 0;
		hrk = 0;
	} else {
		v_hrk.value = hrk;
	}
	if(typeof(nok)=="undefined"){
		v_nok.value = 0;
		nok = 0;
	} else {
		v_nok.value = nok;
	}
	if(typeof(bgn)=="undefined"){
		v_bgn.value = 0;
		bgn = 0;
	}else {
		v_bgn.value = bgn;
	}	
	if(typeof(czk)=="undefined"){ 
		v_czk.value = 0;
		czk = 0;
	} else {
		v_czk.value = czk;	
	}
	if(typeof(eek)=="undefined"){ 
		v_eek.value = 0;
		eek = 0;
	} else {
		v_eek.value = eek;
	}
	if(typeof(huf)=="undefined"){ 
		v_huf.value = 0;
		huf = 0;
	} else {
		v_huf.value = huf;	
	}
	if(typeof(ltl)=="undefined"){ 
		v_ltl.value = 0;
		ltl = 0;
	} else {
		v_ltl.value = ltl;
	}
	if(typeof(lvl)=="undefined"){ 
		v_lvl.value = 0;
		lvl = 0;
	} else {
		v_lvl.value = lvl;	
	}
	if(typeof(pln)=="undefined"){ 
		v_pln.value = 0;
		pln = 0;
	} else {
		v_pln.value = pln;	
	}
   } 
}


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 + '';
}
