/****************************\
* Anders W. Gjære - 2004     *
*     woid@runbox.com        *
\****************************/

function ShowNewWindow(url, height, width) {
	var _arguments = arguments || "";
	var _height = height || "300";
	var _width = width || "300";
	var leftmid = (screen.width - _width) / 2;
	var topmid = (screen.height - _height)/ 2;
	return window.open(url, "_blank", "height=" + _height + "px,width=" + _width + "px,top=" + topmid + "px, left=" + leftmid + "px,resizable=1,titlebar=0,scrollbars=1,toolbar=0,status=0,menubar=0,directories=0", true);
}

function rFormatInt(o, def) {
	o.value = rFormatIntVal(o.value, def);
}

function rFormatIntVal(v, def) {
	var _def = def || "";
	if (v == "") { return _def; }
	var negative = (v.indexOf("-") > -1);
	v = v.replace(/[^0-9]/g, "");
	if (negative) { v = "-" + v; }
	if (!isNaN(v) && v != "") {
		return v;
	}
	else {
		return _def;
	}
}



function CallWebservice(name, params, async) {
	var _async = true;
	if (typeof(async) != "undefined") {
		_async = async;
	}
	
	var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");	
	xmlhttp.open("POST", ApplicationPath + "WS/ws.asmx/" + name, _async);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
	xmlhttp.send(params);
	return xmlhttp;
}


function EditTextMax(srcText) {
	var w = window.showModalDialog(ApplicationPath + "Components/TextEdit.aspx" + "?r=" + Math.random(), srcText, "center:1;dialogHeight:590px;dialogWidth:620px;scroll:1;resizable:1;");
	if (w != null) {
		srcText.value = w;
	}
}

function CloseModal(state) {
	window.returnValue = state;
	window.close();
}



function RiskRowSumCalc(obj) {
	var id = obj.id;
	var parts = id.split("_");
	var base = id.substring(0, id.length - parts[parts.length-1].length);
	var pagebase = base.substring(0, base.length - parts[parts.length-2].length-1);
	if (id.indexOf("after") != -1) {
		RiskSumAfter(base);
	}
	else {
		RiskSum(pagebase);
	}
}

function RiskSum(pagebase) {
	var i = 0;
	var tc = 0;
	var tp = 0;
	while(true) {
		i++;
		var rowid = "ctl" + i;
		var c = RiskConsequenceRowSum(pagebase, rowid);
		if (c == null) { i--; break; }
		var p = RiskProbabilityRowSum(pagebase, rowid);
		tc += c;
		tp += p;
		var s = c * p;
		RiskUpdateRowSum(pagebase, rowid, s);
	}
	var parts = pagebase.split("_");
	var page = parts[0];
	var AvgConsequenceTd = document.all(page + "_AvgConsequenceTd");
	var AvgProbabilityTd = document.all(page + "_AvgProbabilityTd");
	var TotalSumTd = document.all(page + "_TotalSumTd");
	tc = tc / i;
	tp = tp / i;
	AvgConsequenceTd.innerHTML = kFormatDecimalNumber(tc, 0, 2);
	AvgProbabilityTd.innerHTML = kFormatDecimalNumber(tp, 0, 2);
	TotalSumTd.innerHTML = kFormatDecimalNumber(tc * tp, 0, 2);
	TotalSumTd.bgColor = RiskColor(tc * tp);
}

function RiskUpdateRowSum(base, rowid, rowsum) {
	var rowsumobj = document.all(base + rowid + "_" + "rowsum");
	rowsumobj.innerHTML = kFormatDecimalNumber(rowsum, 0, 2);
	rowsumobj.bgColor = RiskColor(rowsum);
}

function RiskConsequenceRowSum(base, rowid, elemid) {
	var eid = elemid || "consequencelist";
	if (rowid != "") { rowid += "_"; }
	var rowbase = base + rowid + eid;
	var o = document.all(rowbase + "1");
	if (typeof(o) == "undefined" || o == null) { return null; }
	if (o.checked) { return 1; }
	else if (document.all(rowbase + "2").checked) { return 2; }
	else if (document.all(rowbase + "3").checked) { return 3; }
	else if (document.all(rowbase + "4").checked) { return 4; }
	else if (document.all(rowbase + "5").checked) { return 5; }
	return null;
}

function RiskProbabilityRowSum(base, rowid, elemid) {
	var eid = elemid || "probability";
	if (rowid != "") { rowid += "_"; }
	var rowbase = base + rowid + eid;
	var o = document.all(rowbase + "1");
	if (typeof(o) == "undefined" || o == null) { return null; }
	if (o.checked) { return 1; }
	else if (document.all(rowbase + "2").checked) { return 2; }
	else if (document.all(rowbase + "3").checked) { return 3; }
	else if (document.all(rowbase + "4").checked) { return 4; }
	else if (document.all(rowbase + "5").checked) { return 5; }
	return null;
}


function RiskSumAfter(base) {
	var c = RiskConsequenceRowSum(base, "", "consequenceafter");
	var p = RiskProbabilityRowSum(base, "", "probabilityafter");
	var rowsumobj = document.all(base + "rowsumafter");
	rowsumobj.innerHTML = kFormatDecimalNumber(p * c, 0, 2);
	rowsumobj.bgColor = RiskColor(p * c);
}


function RiskColor(sum) {
	sum = Number(sum);
	if (sum < 9) {
		return "#99ff00";
	}
	else if (sum < 14) {
		return "#ffff33";
	}
	else {
		return "#ff3333";
	}
}



