var activeRow = null;

function RenderProposalTable() {
	var str = "";
	str += "<table style='margin:10px 0px 0px' cellspacing='2' width='100%' border=0>";
	// BEGIN HEADER ROW
	str += "<tr>";
	str += "<th style='width:1%'><!-- ARROWS --></th>";
	str += "<th class='rfp_list' style='width:55%'>DESCRIPTION</th>";
	str += "<th class='rfp_list' style='width:65px'>BASE COST</th>";
	str += "<th class='rfp_list' style='width:65px'>OTHER FEES</th>";
	str += "<th class='rfp_list' style='width:65px'>TOTAL</th>";
	str += "</tr>";
	// END HEADER ROW
	var grandTotal=0;
	for (i=0;i<rows.length;i++) {
		// BEGIN DATA ROW
		str += "<tr>";
		total = (rows[i]['base_price']/1)+(rows[i]['other_costs']/1); grandTotal += total;
		str += "<td class='arrow' style='width:1%;vertical-align:middle;padding-right:2px' onClick='InsertResponseRow("+i+")'>";
		str += "<img src='/ico/downred.gif' width='7' height='7' border='0' alt='Insert line'>";
		str += "</td>";
		str += "<td><input type='text' class='prop' name='lines["+i+"][description]' value='"+rows[i]['description']+"' onBlur=\"rows["+i+"]['description']=this.value\"></td>";
		str += "<td><input type='text' class='propnum' name='lines["+i+"][base_price]' value='"+((rows[i]['base_price']/1)?rows[i]['base_price']:'')+"' onBlur=\"rows["+i+"]['base_price']=this.value\"></td>";
		str += "<td><input type='text' class='propnum' name='lines["+i+"][other_costs]' value='"+((rows[i]['other_costs']/1)?rows[i]['other_costs']:'')+"' onBlur=\"rows["+i+"]['other_costs']=this.value\"></td>";
		str += "<td><input type='text' tabindex='-1' class='proptot' READONLY value='"+formatCurrency(total)+"'></td>";
		str += "</tr>";
		// END DATA ROW
	}
	str += "<tr><td></td><td><a class='tiny' href='JavaScript:AddResponseRow()'>ADD ROW</a>&nbsp;&nbsp;&nbsp;<a class='tiny' href='JavaScript:DeleteBlankResponseRows()'>DELETE EMPTY ROWS</a>&nbsp;&nbsp;&nbsp;<a class='tiny' href='JavaScript:InitProposalTable()'>RESET</a></td><td></td><td class='label'>Grand total:</td><td><input type='text' tabindex='-1' class='proptot' style='font-weight:bold' READONLY value='"+formatCurrency(grandTotal)+"'></td></tr>";

	str += "<tr><td></td><td colspan='4'>Notes:<br><textarea name='notes' rows='5' cols='80' style='width:99%' onChange='responseNotes = this.value'>"+responseNotes+"</textarea></td></tr>";

	str += "<tr><td></td><td colspan='4' style='padding-top:10px'><input type='submit' name='submit' class='submit' value='Recalculate'>&nbsp;";
	str += "<input type='submit' name='submit' class='submit' value='Attach to Message'>&nbsp;";
	str += "<input type='submit' name='submit' class='submit' value='Cancel'></td></tr>";

	str += "</table>";

	document.getElementById("ProposalTable").innerHTML = str;
}

//-----------------------------------------------------------------------------------------

function InsertResponseRow(rowNum) {
	rows[rows.length] = new Array();
	for (i = (rows.length-2); i >= rowNum; i--) {
		rows[i+1]['description'] = rows[i]['description'];
		rows[i+1]['base_price']  = rows[i]['base_price'];
		rows[i+1]['other_costs'] = rows[i]['other_costs'];
	}
	rows[rowNum]['description']	= "";
	rows[rowNum]['base_price']	= "";
	rows[rowNum]['other_costs']	= "";
	RenderProposalTable();
}

//-----------------------------------------------------------------------------------------

function AddResponseRow() {
	var length = rows.length;
	rows[length] = new Array();
	rows[length]['description']	= "";
	rows[length]['base_price']	= "";
	rows[length]['other_costs']	= "";
	RenderProposalTable();
}

//-----------------------------------------------------------------------------------------

function DeleteBlankResponseRows() {
	var i = 0;
	while (i < rows.length) {
		if (rows[i] && isBlankRow(i)) {
			DeleteResponseRow(i);
		} else {
			i++;
		}
	}
	RenderProposalTable();
}

//-----------------------------------------------------------------------------------------

function isBlankRow(rowNum) {
	if (rows[rowNum]['description'] == "") {
		return true;
	} else {
		return false;
	}
}

//-----------------------------------------------------------------------------------------

function DeleteResponseRow(rowNum) {
	for (i = rowNum; i < (rows.length-1); i++) {
		rows[i]['description']	= rows[i+1]['description'];
		rows[i]['base_price']	= rows[i+1]['base_price'];
		rows[i]['other_costs']	= rows[i+1]['other_costs'];
	}
	rows.length--;
}

//-----------------------------------------------------------------------------------------

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) {num = "0";}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) {cents = "0"+cents;}
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
	}
//	if (num/1) {
		return (((sign)?'':'-')+num+'.'+cents);
//	} else {
//		return '';
//	}
}
