/* $Id: start.js 5262 2006-10-20 11:07:03Z jwilkins $ */


/****
 ****	Display bid list
 ****/
var bidListData = new Array();

function displayBidList() {
	var tableRows = '';
	for (var i = 0 ; i < bidListData.length ; i++) {
		isOddRow = ((i+1)%2 ? 'Odd' : '');
		tableRows = tableRows
		+	'<tr class="bidListItem'+isOddRow+'">'
		+	'<td>'
		+	'<input type="hidden" name="cusip[]" value="'+bidListData[i].cusip+'" />'
		+	bidListData[i].cusip
		+	'</td>'
		+	'<td>'
		+	'<input type="hidden" name="qty[]" value="'+bidListData[i].qty+'" />'
		+	bidListData[i].qty
		+	'</td>'
		+	'<td>'
		+	'<input type="hidden" name="symbol[]" value="'+bidListData[i].symbol+'" />'
		+	bidListData[i].symbol
		+	'</td>'
		+	'<td>'
		+	'<input type="hidden" name="cpn[]" value="'+bidListData[i].cpn+'" />'
		+	bidListData[i].cpn + (bidListData[i].cpn ? ' %' : '')
		+	'</td>'
		+	'<td>'
		+	'<input type="hidden" name="maturity[]" value="'+bidListData[i].maturity+'" />'
		+	bidListData[i].maturity
		+	'</td>'
		+	'<td>'
		+	'<input type="hidden" name="settlement[]" value="'+bidListData[i].settlement+'" />'
		+	bidListData[i].settlement
		+	'</td>'
		+	'<td class="bidListButton" rowspan="2">'
		+	'<input type="button" value="Remove" onClick="deleteBid('+i+');" />'
		+	'</td>'
		+	'</tr>'
		+	'<tr class="bidListItem'+isOddRow+'">'
		+	'<td colspan="6" class="bidComment">'
		+	'<input type="hidden" name="comment[]" value="'+bidListData[i].comment+'" />'
		+	bidListData[i].comment
		+	'</td>'
		+	'</tr>'
		;
	}

	var table = ''
	+	'<table class="bidList">'
	+	'<tr>'
	+	'<th class="bidCusip">CUSIP / ISIN</th>'
	+	'<th class="bidQty">Quantity</th>'
	+	'<th class="bidSymbol">SYMBOL / Description</th>'
	+	'<th class="bidCpn">Coupon</th>'
	+	'<th class="bidMaturity">Maturity</th>'
	+	'<th class="bidSettlement">Settlement</th>'
	+	'<th style="display: none;"></th>'
	+	'</tr>'
	+	'<tr>'
	+	'<th colspan="6" class="bidComment">Comments</th>'
	+	'<th style="display: none;"></th>'
	+	'</tr>'
	+	(bidListData.length ? tableRows : '<tr class="bidListItemOdd"><td colspan="6"><em>no bids yet</em></td></tr>')
	+	'</table>';

	var obj;
	if ( obj = document.getElementById('bidList') ) {
		obj.innerHTML = table;
	}

	document.buildList.addCusipButton.value = (bidListData.length ? 'Add Another Item To List' : 'Add Item To List');

}

function deleteBid(index){
	var oldLength = bidListData.length;

	// out of range
	if (index < 0 || index >= oldLength) {
		return;
	}

	bidListData.splice(index, 1);

	displayBidList();
}

displayBidList();

/****
 **** Validate add new item form
 ****/

function isValidCusip(cusip) {
	if (cusip == '') return true;
	var regex = new RegExp('^([0-9A-Za-z]{9}|[0-9A-Za-z]{12})$');
	return regex.test(cusip);
}
function isValidWholeNumber(number) {
	if (number == '') return true;
	var regex = new RegExp('^[0-9]+$');
	return regex.test(number);
}
function isValidInterestRate(rate) {
	if (rate == '') return true;
	var regex = new RegExp('^[0-9]+(\.[0-9]{1,3}|)$');
	return regex.test(rate);
}
function isValidDate(date) {
	if (date == '') return true;
	var regex = new RegExp("^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$");
	return regex.test(date);
}
function isValidSettlementDate(date) {
	if (date == 'regular') return true;
	if (date == '') return false;
	return isValidDate(date);
}

function validate(show_required){
	show_required = (typeof show_required == 'boolean') ? true : false;

	var noErrors = true;
	var obj;

	// validate CUSIP
	obj = $('validateCusipError');
	if ( isValidCusip(document.buildList.cusip.value) ) {
		if (obj) obj.style.display = 'none';
	} else {
		if (obj) obj.style.display = '';	/* should be list-item, but IE doesn't understand */
		noErrors = false;
	}

	// validate Quantity
	obj = $('validateQuantityError');
	if ( isValidWholeNumber( document.buildList.qty.value )
		 && !(show_required && document.buildList.qty.value == '') )
	{
		if (obj) obj.style.display = 'none';
	} else {
		if (obj) obj.style.display = '';
		noErrors = false;
	}

	// validate Coupon
	obj = $('validateCouponError');
	if ( isValidInterestRate( document.buildList.cpn.value ) ) {
		if (obj) obj.style.display = 'none';
	} else {
		if (obj) obj.style.display = '';
		noErrors = false;
	}

	// validate Maturity
	year = document.buildList.maturity_year.options[document.buildList.maturity_year.selectedIndex].value;
	month = document.buildList.maturity_month.options[document.buildList.maturity_month.selectedIndex].value;
	day = document.buildList.maturity_day.options[document.buildList.maturity_day.selectedIndex].value;
	if ( year == '' || month == '' || day == '' )
	{
		document.buildList.maturity.value = '';
	} else {
		document.buildList.maturity.value = month + '/' + day + '/' + year;
	}
	obj = $('validateMaturityError');
	if ( isValidDate( document.buildList.maturity.value ) ) {
		if (obj) obj.style.display = 'none';
	} else {
		if (obj) obj.style.display = '';
		noErrors = false;
	}

	// validate Settlement Date
	if ( document.buildList.settlement_type.value == 'regular' ) {
		document.buildList.settlement.value = 'regular';
	} else {
		year = document.buildList.settlement_year.options[document.buildList.settlement_year.selectedIndex].value;
		month = document.buildList.settlement_month.options[document.buildList.settlement_month.selectedIndex].value;
		day = document.buildList.settlement_day.options[document.buildList.settlement_day.selectedIndex].value;
		if ( year == '' || month == '' || day == '' ) {
			document.buildList.settlement.value = '';
		} else {
			document.buildList.settlement.value = month + '/' + day + '/' + year;
		}
	}
	obj = $('validateSettlementError');
	if ( isValidSettlementDate( document.buildList.settlement.value ) ) {
		if (obj) obj.style.display = 'none';
	} else {
		if (obj) obj.style.display = '';
		noErrors = false;
	}

	// check required
	if (show_required) {
		obj = $('validateRequiredError');
		if ( document.buildList.cusip.value != ''
			|| ( document.buildList.symbol.value != ''
				&& document.buildList.maturity.value != '' )
		   )
		{
			if (obj) obj.style.display = 'none';
		} else {
			noErrors = false;
			if (obj) obj.style.display = '';
		}
	}

	// display error messages
	obj = $('validateFormError');
	if ( obj ) {
		obj.style.display = noErrors ? 'none' : 'block';
	}

	return noErrors;
}

function validateList() {
	var hasItem = (bidListData.length == 0) ? false : true;
	var obj = $('validateBidListError');
	if (obj) obj.style.display = hasItem ? 'none' : 'block';
	return hasItem;
}

/****
 **** event handlers
 ****/

function toggleSettlementDate() {
	var obj = $('settlement_date');
	if (obj) {
		if ( document.buildList.settlement_type.selectedIndex == 0 ) {
			obj.style.display = 'none';
		} else {
			obj.style.display = 'block';
		}
	}
}

function htmlspecialchars(text) {
	return text.replace(
		/[&"'<>]/g,
		function(source) {
			switch (source) {
				case '&':
					return '&amp;';
				case '"':
					return '&quot;';
				case "'":
					return '&#39;';
				case '<':
					return '&lt;';
				case '>':
					return '&gt;';
				default:
					return source;
			}
		}
	);
}

function addToList() {
	// validate form
	noErrors = validate(true);

	// add bid to list
	if (noErrors) {

		newBid = new Object();
		newBid.cusip		= htmlspecialchars( document.buildList.cusip.value );
		newBid.qty			= htmlspecialchars( document.buildList.qty.value );
		newBid.symbol		= htmlspecialchars( document.buildList.symbol.value );
		newBid.cpn			= htmlspecialchars( document.buildList.cpn.value );
		newBid.maturity		= htmlspecialchars( document.buildList.maturity.value );
		newBid.settlement	= htmlspecialchars( document.buildList.settlement.value );
		newBid.comment		= htmlspecialchars( document.buildList.comment.value );

		bidListData[bidListData.length] = newBid;

		displayBidList();
		validateList();

		// prepare the 'add to list' form for more entries
		document.buildList.cusip.value = '';
		document.buildList.qty.value = '';
		document.buildList.symbol.value = '';
		document.buildList.cpn.value = '';
		document.buildList.maturity.value = '';
		document.buildList.maturity_year.selectedIndex = 0;
		document.buildList.maturity_month.selectedIndex = 0;
		document.buildList.maturity_day.selectedIndex = 0;
		document.buildList.settlement.value = '';
		document.buildList.settlement_type.selectedIndex = 0;
		document.buildList.settlement_year.selectedIndex = 0;
		document.buildList.settlement_month.selectedIndex = 0;
		document.buildList.settlement_day.selectedIndex = 0;
		document.buildList.comment.value = '';
		toggleSettlementDate();
	}
}

var allowSubmit = false;

function clickSubmit() {
	allowSubmit = validateList();
	if ( allowSubmit ) {
		document.buildList.submit();
	}
}

function submitHandler() {
	return allowSubmit;
}

/****
 **** Add handlers
 ****/

var obj;
var objectList = ['cusip', 'qty', 'symbol', 'cpn', 'maturity_year', 'settlement_year', 'comment'];
for (var i=0; i < objectList.length; i++) {
	if (obj = $(objectList[i])) {
		obj.onchange = validate;
	}
}
if (obj = $('settlement_type')) {
	obj.onchange = toggleSettlementDate;
}
if (obj = $('addCusipButton')) {
	obj.onclick = addToList;
}
if (obj = $('submitButton')) {
	obj.onclick = clickSubmit;
}
document.buildList.onsubmit = submitHandler;

