function format(amount) {

	var i = parseFloat(amount);
	if( isNaN(i) ) { 
		i = 0.00; 
	}
	
	var minus = '';
	if( i < 0 ) { 
		minus = '-'; 
	}
	
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if( s.indexOf('.') < 0 ) { 
		s += '.00'; 
	}	
	if( s.indexOf('.') == (s.length - 2) ) { 
		s += '0'; 
	}
	s = minus + s;
	
	return s;
}

function dolares() {

//	document.getElementById('price').style.background='url(currency-wait.gif)';		
	var priceVar=document.getElementById('price');
	if(priceVar){
		priceVar.innerHTML = '&nbsp;&nbsp;<b>...<br />&nbsp;...</b>';
	}
	
	new Ajax.Request('../php/currency.php?c2c=USDUSD', {
		method:'get',		
		onSuccess: function( transport ) {
//			document.getElementById('price').style.background='url(currency-bck.jpg)';
			document.getElementById('price').innerHTML = '&#36;<br>'+format(costo);
		},

		onFailure: function(){ 
//			document.getElementById('price').style.background='url(currency-bck.jpg)';
			document.getElementById('price').innerHTML = '&nbsp;&nbsp;&nbsp;&#36;<br>'+format(costo);
			alert('Ha ocurrido un error al acceder\na Yahoo! Finance');
		}
	});
}

function euros() {

//	document.getElementById('price').style.background='url(currency-wait.gif)';		
	document.getElementById('price').innerHTML = '&nbsp;&nbsp;<b>...<br />&nbsp;...</b>';

	new Ajax.Request('../php/currency.php?c2c=USDEUR', {
		method:'get',		
		onSuccess: function( transport ) {
			change = parseFloat(transport.responseText);	
			amount = change*costo;
//			document.getElementById('price').style.background='url(currency-bck.jpg)';
			document.getElementById('price').innerHTML = '&#128;<br>'+format(amount);
		},

		onFailure: function(){ 
//			document.getElementById('price').style.background='url(currency-bck.jpg)';
			document.getElementById('price').innerHTML = '&nbsp;&nbsp;&nbsp;&#36;<br>'+format(costo);
			alert('Ha ocurrido un error al acceder\na Yahoo! Finance');
		}
	});
}

function soles() {

//	document.getElementById('price').style.background='url(currency-wait.gif)';		
	document.getElementById('price').innerHTML = '&nbsp;&nbsp;<b>...<br />&nbsp;...</b>';

	new Ajax.Request('../php/currency.php?c2c=USDPEN', {
		method:'get',		
		onSuccess: function( transport ) {
			change = parseFloat(transport.responseText);	
			amount = Number(change)*Number(costo);
//			document.getElementById('price').style.background='url(currency-bck.jpg)';
			document.getElementById('price').innerHTML = '&nbsp;S/.<br>'+format(amount);
		},

		onFailure: function(){ 
//			document.getElementById('price').style.background='url(currency-bck.jpg)';
			document.getElementById('price').innerHTML = '&nbsp;&nbsp;&nbsp;&#36;<br>'+format(costo);
			alert('Ha ocurrido un error al acceder\na Yahoo! Finance');
		}
	});
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

var solesPrice = 0.0;
var eurosPrice = 0.0;
function errorYahoo(str) {
	return 'Error al intentar obtener la cotización del '+str+'\n, en Yahoo! Finance';
}
function getCurrencyPrices() {
	new Ajax.Request( '../php/currency.php?c2c=USDPEN', {
		method:'get',		
		onSuccess: function( transport ) {
			solesPrice = parseFloat(transport.responseText);
		},

		onFailure: function(){ 
			alert(errorYahoo('dólar/sol'));
		}
	});

	new Ajax.Request('../php/currency.php?c2c=USDEUR', {
		method:'get',		
		onSuccess: function( transport ) {
			eurosPrice = parseFloat(transport.responseText);	
		},

		onFailure: function(){ 
			alert(errorYahoo('dólar/euro'));
		}
	});	
}

function getPrice(dollarPrice) {
	costSoles = Number(solesPrice)*Number(dollarPrice);
	costEuros = Number(eurosPrice)*Number(dollarPrice);
	contexto = '<div style=\'background-image:url(../imgs/fondo-price.png); background-repeat:no-repeat; width:83px; height:43px; padding-top:7px; color:#FFF; text-align:left; padding-left:12px;\'>&#128; '+format(costEuros)+'<br>S/. '+format(costSoles)+'</div>';
	document.getElementById('precio_in').innerHTML = contexto;
}

addLoadEvent(dolares);
addLoadEvent(getCurrencyPrices);