// ==UserScript==
// @name           myc4 sum bid amount
// @namespace      http://www.myc4.com/
// @description    Will sum up the different percentages on the Bids page
// @include        http://www.myc4.com/Portal/WebForms/Opportunities/ViewOpportunity.aspx?*
// ==/UserScript==

var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

function letsJQuery() {
	$(document).find("#ctl00_ctl00_ctl00_Middle_Center_Main_uiBidsRadGrid").after("<div id='table_sum' style='text-align: right;>summation</div>");
	var sum = 0;
	$(document).find("#ctl00_ctl00_ctl00_Middle_Center_Main_uiBidsRadGrid_ctl01 td+td+td+td+td").each(function(i) {
		$(this).after("<td id='bids_above_"+i+"'></td>");
		$(document).find("#bids_above_"+i).html(roundNumber(sum,2));
		sum += parseFloat($(this).text().replace(" ", "").replace(",", ""));
	})
	$(document).find("#table_sum").html("<b>Total sum:</b> <span id='total_sum_val'>"+sum+" &euro;</span>");
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

// Check if jQuery's loaded
function GM_wait() {
	if(typeof unsafeWindow.jQuery == 'undefined') {
		window.setTimeout(GM_wait,100);
	} else {
		$ = unsafeWindow.jQuery; letsJQuery();
	}
}
GM_wait();
