function isUnsignedInteger(s) {
  return (s.toString().search(/^[0-9]+$/) == 0);
}

function fn_check_integer_value(elm) {
	value = elm.val();
	value = parseInt(value);
 	value = Math.round(value);

	if (isUnsignedInteger(value)) {
		elm.val(value)
		return value;

	} else {
		elm.val('')
		return '';
	}
}

function fn_check_float2_value(elm) {
	value = elm.val();
	value = value.replace(',', '.');
 
	int_value = Math.round(value*100);

	if (isUnsignedInteger(int_value)) {
		elm.val(value)
		return value;

	} else {
		elm.val('')
		return '';
	}
}

function fn_set_fields_classes(product_id) {
	product_units = $('#product_units_' + product_id).val();
	fn_calculate_product_units(product_id);
	
	if (product_units == 'R') {
		$('#length_val_' + product_id).attr("readonly", "");
		$('#length_val_' + product_id).removeClass("locked_field");
		$('#area_val_' + product_id).attr("readonly", "readonly");
		$('#area_val_' + product_id).addClass("locked_field");

	} else {
		$('#length_val_' + product_id).attr("readonly", "readonly");
		$('#length_val_' + product_id).addClass("locked_field");
		$('#area_val_' + product_id).attr("readonly", "");
		$('#area_val_' + product_id).removeClass("locked_field");	
	}
}

function fn_calculate_product_units(product_id) {
	
	product_units = $('#product_units_' + product_id).val();

	var width = fn_check_float2_value($('#width_val_' + product_id));
	var area = fn_check_float2_value($('#area_val_' + product_id));

	if (product_units == 'R') {
		var length = fn_check_float2_value($('#length_val_' + product_id));

		area = (length*width/100).toFixed(4);
		$('#area_val_' + product_id).val(area);
	} else {

		if (width > 0) {
			$('#length_val_' + product_id).val((area / width * 100).toFixed(3));

		} else {
			$('#length_val_' + product_id).val(0);
		}
	}

	$('#amount_' + product_id).val(Math.round(area * 1000 * 1000));

if (meters_in_roll > 0) {
		var length = $('#length_val_' + product_id).val();
		var div = length/meters_in_roll - (length/meters_in_roll) % 1;

		$('#entire_rolls_' + product_id)[0].innerHTML = (div + ' + ' + length % meters_in_roll);
	}

}
