var xMouse;
var yMouse;

String.prototype.leftTrim = function () 
{
	return (this.replace(/^\s+/,""));
};

String.prototype.rightTrim = function () 
{
	return (this.replace(/\s+$/,""));
};

String.prototype.trim = function () 
{
	return (this.replace(/\s+$/,"").replace(/^\s+/,""));
};


String.prototype.superTrim = function ()
{
	return(this.replace(/\s+/g," ").replace(/\s+$/,"").replace(/^\s+/,""));
};


String.prototype.removeWhiteSpaces = function ()
{
	return (this.replace(/\s+/g,""));
};


function number_format(number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}

	function showRate(curr)
	{
		if (!rate)
		{
			return null;
		}
		if(!curr)
		{
			curr='EUR';
		}
		
		var val = document.getElementById('calc_amount').value;
		if (val == "")
		{
			return false;
		}
		
		val = val.replace(",",".");	
		val = parseFloat(val);
			
		if (val >= 0.01)
		{
			var dollars = number_format(Math.round(val*rate[curr]['value']*100)/100,2,",");
			var euros   = number_format(Math.round(val/rate[curr]['value']*100)/100,2,",");
			var nval = number_format (val, 2, ",");
				
			

			document.getElementById('calc_fromeuro').innerHTML = nval + " " + rate[curr]['sign'];
			document.getElementById('calc_todollar').innerHTML = dollars + " $"
			document.getElementById('calc_fromdollar').innerHTML = nval + " $";
			document.getElementById('calc_toeuro').innerHTML = euros + " " + rate[curr]['sign'];
		}
	} 
	
	function getElementsByClassName(oElm, strTagName, oClassNames){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var arrRegExpClassNames = new Array();
    if(typeof oClassNames == "object"){
        for(var i=0; i<oClassNames.length; i++){
            arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
        }
    }
    else{
        arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
    }
    var oElement;
    var bMatchesAll;
    for(var j=0; j<arrElements.length; j++){
        oElement = arrElements[j];
        bMatchesAll = true;
        for(var k=0; k<arrRegExpClassNames.length; k++){
            if(!arrRegExpClassNames[k].test(oElement.className)){
                bMatchesAll = false;
                break;                      
            }
        }
        if(bMatchesAll){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
	}
	
	function getMouseCoords(e) 
	{		
		var posX = 0, posY = 0;
		if (!e) 
		{ 
			e = window.event; 
		} 
		if (!e) 
		{ 
			return [ 0, 0 ]; 
		}
				
		if( typeof(e.pageX ) == 'number') 
		{
      posX = e.pageX; posY = e.pageY;
		}
		else 
		{
			if( typeof( e.clientX ) == 'number' )
			{
				posX = e.clientX; 
				posY = e.clientY;
				
				if (document.body && !(window.opera || window.debug || navigator.vendor == 'KDE')) 
				{
					if (typeof( document.body.scrollTop) == 'number')
					{
						posX += document.body.scrollLeft; 
						posY += document.body.scrollTop;
					}
				}
				if (document.documentElement && !( window.opera || window.debug || navigator.vendor == 'KDE') )
				{
					if (typeof(document.documentElement.scrollTop) == 'number')
					{
						posX += document.documentElement.scrollLeft; 
						posY += document.documentElement.scrollTop;
					}
				}
			}
		}
		xMouse = posX;
		yMouse = posY;
	}	
	
	function createCookie(name,value,miliseconds)
	{
		if (miliseconds)
		{
			var date = new Date();
			date.setTime(date.getTime()+miliseconds);
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) 
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++)
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	function eraseCookie(name)
	{
		createCookie(name,"",-1);
	}