//run automatically when page loaded (happens last)
window.onload = handle_OnLoad
function handle_OnLoad() {
	//highlight any rows with users sail number in (got from cookie)
	var iSailNo = get_cookie_value('SailNo')
	if (iSailNo != '') highlight_matching_rows(iSailNo, 'yellow')
}

//make compatible with UTILITIES1.JS
function address(name, domain, text) {
	document.write(address2(name, domain, text))
}


//stop spammers stealing addresses
function address2(name, domain, text) {
  try {
	if (domain == '') domain = 'merlinrocket.co.uk'
	if (text == '') text = name + '&#64;' + domain
	return('<a title="Send email" href="mai' + 'lto' + ':' + name + '&#64;' + domain + '">' + text + '</a>')
  }
  catch(e) {
	return('')
  }
}


//get value out of cookie string
function get_cookie_value(sParameter) {
	var sURL = document.cookie
	sParameter = sParameter + '='
	var iStartPos = sURL.indexOf(sParameter)
	if (iStartPos != -1) {
		iStartPos += sParameter.length
		var iEndPos = sURL.indexOf(';', iStartPos)
		if (iEndPos == -1) iEndPos = sURL.length
		return sURL.substr(iStartPos, iEndPos - iStartPos)
	}
	return ''
}


//loop every table on the page, looking for text in a cell to match, and highlight row if found
function highlight_matching_rows(sMatch, sColour) {
	var oTables = document.getElementsByTagName('TABLE')
	var iTableTotal = oTables.length
	var iTableCount
	var oRows
	var iRowTotal
	var iRowCount
	var oCells
	var iCellTotal
	var iCellCount
	var sCellText
	for (iTableCount = 0; iTableCount < iTableTotal; iTableCount++) {
		oRows = oTables[iTableCount].rows
		iRowTotal = oRows.length
		for (iRowCount = 0; iRowCount < iRowTotal; iRowCount++) {
			oCells = oRows[iRowCount].cells
			iCellTotal = oCells.length
			for (iCellCount = 0; iCellCount < iCellTotal; iCellCount++) {
				sCellText = oCells[iCellCount].innerText.replace(/\s/g, '')
				if (sCellText == sMatch) {
					oRows[iRowCount].style.backgroundColor = sColour
					return
				}
			}
		}
	}
}
