var lastInput;
		
		function strip(n,s) 
		{
			var pos=0;
			var len=n.length;
			var res='';
			while (pos<len) 
			{
				if (s.indexOf(n.charAt(pos))!=-1) 
					res+=n.charAt(pos);
				
				pos++;
			}
			return(res);
		}
		
		function fixfloat(n) 
		{
			var pos;
			var inp;
			var flp;
			var res;
			
			pos=n.indexOf(',');
					
			if (pos==-1)
				pos=n.indexOf('.');		
			
			// comma in string
			if (pos!=-1) 
			{
				inp=strip(n.substring(0,pos),'0123456789');
				flp=strip(n.substring(pos+1),'0123456789');		
						
				if (inp && flp) //num, commanum
					return eval(inp)+flp/Math.pow(10,flp.length);		
				else 
				{
					if (!inp && flp) //commanum
						return flp/Math.pow(10,flp.length); 			
					else 
					{ 
						if (!inp && !flp) //not num, not commanum
							return '';				
						else if (inp && !flp) //num
							return eval(inp);				
					}
				}
			}
			return(strip(n,'0123456789'));
		}
		
		//Gengis object
		function MakeItem(co,fa) 
		{
		   this.code=co;     
		   this.factor=parseFloat(fa);
		   return this;
		}
		
		function initCalc( code )
		{
			if ( code != '')
				calcCurrency(code,1);	
		}
		
		function calcCurrency(mynt, calc)
		{
			lastInput = mynt;	
			if (calc == 1)
				calcCurrency2();
		}
		
		
			function calcCurrency2()
			{ 
				var mynt = lastInput;
				
				var userInput = fixfloat(document.getElementById(mynt).value);
				var equivalent = arrCurrency[mynt].factor;	
		 
				if (userInput == '' || userInput == 0 || isNaN(userInput) )
				{
					ClearForm();
					return false;
				}	
				 
				var v;
				// for (var mynt in arrCurrency)
				// {
					//if (i!=origin)
					if( 'PLN' != lastInput )
					{
						document.getElementById('PLN').value = FormatMoney(equivalent/arrCurrency['PLN'].factor*userInput);
					}
					if( 'ISK' != lastInput )
					{
						document.getElementById('ISK').value = FormatMoney(equivalent/arrCurrency['ISK'].factor*userInput);
					}
					if( 'NOK' != lastInput )
					{
						document.getElementById('NOK').value = FormatMoney(equivalent/arrCurrency['NOK'].factor*userInput);
					}
					if( 'DKK' != lastInput )
					{
						document.getElementById('DKK').value = FormatMoney(equivalent/arrCurrency['DKK'].factor*userInput);
					}
					if( 'SEK' != lastInput )
					{
						document.getElementById('SEK').value = FormatMoney(equivalent/arrCurrency['SEK'].factor*userInput);
					}
					if( 'EUR' != lastInput )
					{
						document.getElementById('EUR').value = FormatMoney(equivalent/arrCurrency['EUR'].factor*userInput);
					}
					
				// }
				 
				return true;
			}
		
			
			function ClearForm()
			{
				// for( var mynt in arrCurrency)
					document.getElementById('PLN').value = "";
					document.getElementById('ISK').value = "";
					document.getElementById('SEK').value = "";
					document.getElementById('NOK').value = "";
					document.getElementById('EUR').value = "";
					document.getElementById('DKK').value = "";
		
				return true;
			}
		
		
		function formatThousand(szIn)
		{
		    var sz = szIn;
		    var szNew = '';
		    while (sz.length > 3)
		        {
		        if (szNew != '') szNew = '.' + szNew;
		        szNew = sz.slice((sz.length-3),sz.length) + szNew;
		        sz = sz.slice(0,(sz.length-3));
		        }
		    if (szNew != '') szNew = '.' + szNew;
		    szNew = sz + szNew;
		    return(szNew);
		}
		
		function FormatMoney(amt) 
		{
		    var tmpn=0;
		    var tmps="";
		    tmpn=Math.round(amt*100);
		    
		    //Add comma and 2 numbers to the end
		    if (tmpn < 0 || tmpn > 99999999999999) 
				return "*********";
		    else if (tmpn < 10) 
				tmps="00"+tmpn;
		    else if (tmpn < 100) 
				tmps="0"+tmpn;
		    else tmps=""+tmpn;
		    
		    var before = formatThousand(tmps.substring(0,(tmps.length-2)));        
		    return before+","+ tmps.substring((tmps.length-2),tmps.length);
		}
