
//function get_value(result_holder,e_message) {
function get_value(amount,months,product_id) {
		
		var pass = amount+'/'+months+'/'+product_id;
		var fullurl = webroot + 'loans/simulate/'+pass;
 
		return $.ajax( {
			url: fullurl,
			async: false,
			/*success: {}*/
			//error: function(XMLHttpRequest, textStatus, errorThrown) { loader.fadeOut(400); e_message.html('<p>Se produjo un error: ' + textStatus + "</p> " + "<pre>" + errorThrown + "</pre>").show(); }
		}).responseText;
}

function update_value(obj) {

	//alert(obj.attr('class'));
	var e_message = obj.find('#error_msg');
	var loader= obj.find('.loader');
	//var loader = $('.loader').each();
	//var loader = $('.loader');
	var result_holder = obj.find('#fee');
	//alert(result_holder.html());
	var wrapper = obj.find('#wrapper');
	//var input_value = obj.find('');
	
	var amount_field = obj.find('.SimulatorAmount');
	var months_field = obj.find('.SimulatorMonths');

	e_message.html('').hide();
	wrapper.hide(0);
	

	//var amount = parseFloat(amount_field.val());
	var amount = amount_field.val().replace(',','.');
	var months = parseInt(months_field.val());
	var product_id = obj.find('#ProductId').val();
	
	var min_amount = parseInt(obj.find('#ProductMinAmount').val());
	var max_amount = parseInt(obj.find('#ProductMaxAmount').val());
	var min_months = parseInt(obj.find('#ProductMinMonths').val());
	var max_months = parseInt(obj.find('#ProductMaxMonths').val());
	
	if(amount <= max_amount && amount >= min_amount && months <= max_months && months >= min_months) {
		loader.css('display','inline').fadeIn(0);
		result_holder.fadeOut('fast');
		//loader.fadeIn(60);
		var data = get_value(amount,months,product_id); // get_value(amount, months, product_id);
		
		//var response = data; // anda solo en chromium
		//var response = jQuery.parseJSON(data); // no anda en chromium
		//var response = JSON.parse(data); // no anda en ninguno de los dos
		var response = (new Function("return " + data))(); // con esto anda en chromium y FF
		
		if(response.error_code==0)
		{
			response.fee = response.fee.replace('.',','); // lo muestro con coma
			//return response.fee;
			if(response.fee!=result_holder.html())
			{
				obj.find('#SimulatorValue').attr('value',response.fee);

				loader.fadeOut(300);
				result_holder.html(response.fee).fadeIn(50);
				//result_holder.html(response.fee);
			} else { loader.fadeOut(0); wrapper.show(); } 
		} else { 
			result_holder.html('').hide();
			wrapper.fadeOut(100);
			loader.fadeOut(0);
			//return response.error_msg;
			e_message.html(response.error_msg).fadeIn(100);
		}
		loader.fadeOut(60);		
		
	} else {
		e_message.html('El monto debe estar entre $' + min_amount + ' y $' + max_amount).fadeIn(100);
		wrapper.hide(0);
		loader.fadeOut(0);
	}
	loader.fadeOut(0);
	wrapper.fadeIn(20); 
	result_holder.fadeIn(10);
}

$(document).ready(function() {


	$(document).find('.loader').fadeOut(0);

	$('.SimulatorAmount, .SimulatorMonths').change(function() {
		var block = $(this).closest('.product');
		
		update_value(block);
		//alert('hola');
	});

	// solo un preventdefault
	$('.SimulatorAmount, .SimulatorMonths').keydown(function(e) {
		if (!e.which && ((e.charCode || e.charCode === 0) ? e.charCode: e.keyCode)) {e.which = e.charCode || e.keyCode;}
		if(e.which==13) { e.preventDefault(); }
	});

	$('.SimulatorAmount, .SimulatorMonths').keyup(function(e) {
		
		var block = $(this).closest('.product');
		
		if (!e.which && ((e.charCode || e.charCode === 0) ? e.charCode: e.keyCode)) {
			e.which = e.charCode || e.keyCode;
		}
		if ( e.which == 13 ) {
			e.preventDefault();
			update_value(block);
		} else { 
			if($(this).hasClass('SimulatorMonths')) // pero los meses con cualquier tecla
			{
				//alert(e.keyCode + ' ' + e.charCode + ' ' + e.which);
				if(e.which >= 33 && e.which <= 40) // begin, end, pgup, pgdn, left, up, right, down
					update_value(block);
			}
		}
		
	});
    
});
