/*
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 */
 //url = http://www.nsftools.com/misc/SearchAndHighlight.htm
 var terms = new Array();
 var definitions = new Array();
 terms[0] = ' tenant ';
 definitions[0] = 'A person who is permitted by the Landlord to occupy a residential premises under a residential tenancy agreement.';
 terms[1] = ' eligibility ';
 definitions[1] = 'Fit or proper to be chosen.';
 terms[2] = ' Obstacles';
 definitions[2] = 'Something that stands in the way of, or holds up progress.';
 terms[3] = ' Advocacy';
 definitions[3] = 'The act of supporting.';
 terms[4] = ' Underwriting';
 definitions[4] = 'When a person signs their name to mortgage documents.';
 terms[5] = ' Credit rating';
 definitions[5] = 'An estimate of the amount of credit that can be extended to a company or person without undue risk.';
 terms[6] = ' Equity';
 definitions[6] = 'The difference between the cost of all assets and the remaining debt associated with those assets.';
 terms[7] = ' Down payment';
 definitions[7] = 'An initial amount paid at the time of purchase of a new home.';
 terms[8] = ' Lenders';
 definitions[8] = 'People or companies that give out money on the condition that it is returned and that interest is paid for its temporary use.';
 terms[9] = ' Landlord';
 definitions[9] = 'A person who owns and leases or rents out land, buildings, etc. or a property manager who acts for the owner to lease or rent land, buildings, or units in a building.';
 terms[10] = ' Rent Supplement';
 definitions[10] = 'An amount paid to a person every month in order to help them make their rent payments.';
 terms[11] = ' Subsidized Housing';
 definitions[11] = 'Homes where the government pays for a part of the rent or reduces the rent on a premise as determined by the tenant’s income.';
 terms[12] = ' Privately Owned';
 definitions[12] = 'Property not owned by the government.';
 terms[13] = ' Administers';
 definitions[13] = 'To manage.';
 terms[14] = ' Income ';
 definitions[14] = 'The amount of money or its equivalent received during a period of time in exchange for labor or services, from the sale of goods or property, or as profit from financial investments including support from government services.';
 terms[15] = ' Market Rent';
 definitions[15] = 'The amount of money that a given property would command, if it were open to leasing at the moment to the general public at FAIR MARKET VALUE.';
 terms[16] = ' Security Deposit';
 definitions[16] = 'An amount of money paid at the start of tenancy and held by the Landlord as security for the performance of an obligation or payment of a liability by the Tenant.';
 terms[17] = ' Residential Tenancy Agreement';
 definitions[17] = 'A written, oral, or implied agreement to rent residential premises.';
 terms[18] = ' Lease Agreement';
 definitions[18] = 'A contract renting land, buildings, etc., to another.';
 terms[19] = ' Regulations';
 definitions[19] = 'A principle, rule, or law designed to control or govern conduct, behavior, or process.';
 terms[20] = ' Premises';
 definitions[20] = 'A piece of land including its buildings and units contained in those buildings.';
 terms[21] = ' Canadian Citizen';
 definitions[21] = 'A person who is Canadian by birth or who has applied for Canadian citizenship through Citizenship and Immigration Canada and has received a citizenship certificate.';
 terms[22] = ' Independent Landed Immigrant';
 definitions[22] = 'A person who has been granted permanent residence in Canada, but is not a Canadian citizen, and sponsors themselves to reside in Canada.';
 terms[23] = ' Government Sponsored Landed Immigrant';
 definitions[23] = 'A person sponsored by the Government of Canada to reside in Canada.';
 terms[24] = ' Verification';
 definitions[24] = 'Evidence that establishes or confirms the accuracy or truth of something.';
 terms[25] = ' Priority';
 definitions[25] = 'The right to receive certain services before others.';
 terms[26] = ' Subsidy';
 definitions[26] = 'Money given by a government to a person or group to help support that person or group.';
 terms[27] = ' Suite ';
 definitions[27] = 'A series of connected rooms used as a living unit.';
 terms[28] = ' Discretion';
 definitions[28] = 'the power or right to decide or act according to one\'s own judgment.';
 terms[29] = ' Qualify';
 definitions[29] = 'Meeting all of the requirements.';
 terms[30] = ' Circumstances';
 definitions[30] = 'The condition or state of a person with respect to income and material welfare.';
 terms[31] = ' Established';
 definitions[31] = 'To set up; found.';
 terms[32] = ' Monies';
 definitions[32] = 'Money';
 terms[33] = ' Approximately';
 definitions[33] = 'Close to.';
 terms[34] = ' Legal Suite';
 definitions[34] = 'A suite with a valid development permit and/or building permit connected to it and meets all legislated requirements including applicable by-laws.';
 terms[35] = ' Rent Receipt';
 definitions[35] = 'A receipt from your landlord stating you’ve paid your rent.';
 terms[36] = ' Cancelled Rent Cheque';
 definitions[36] = 'A cheque issued that has been negotiated by the issuer’s bank.';
 terms[37] = ' Need ';
 definitions[37] = 'Requires help or assistance in some manner.';
 terms[38] = ' Geographic Boundaries';
 definitions[38] = 'A theoretical line that marks the limit of an area of land.';
 terms[39] = ' Discontinued';
 definitions[39] = 'To put an end to; stop; terminate.';
 terms[40] = ' Taxable Benefit';
 definitions[40] = 'Something that is given to you that should be reported when you complete your income taxes.';
 terms[41] = ' Criminal Records Check';
 definitions[41] = 'An information check run by the Police reporting any criminal offenses.';
 terms[42] = ' Addendum';
 definitions[42] = 'A thing to be added; an addition.';
 terms[43] = ' Supportive Housing';
 definitions[43] = 'Housing where the facility managers help residents who may need assistance in maintaining their quality of life in the community.';
 terms[44] = ' Facility';
 definitions[44] = 'A building or location where specific services are provided.';
 terms[45] = ' Wheelchair Access';
 definitions[45] = 'A house/apartment allows for a person in a wheelchair to enter/leave.';
 terms[46] = ' Utilities';
 definitions[46] = 'Natural Gas, Water, and Electricity supplied to a house from outside organizations.';
 terms[47] = ' maintenance';
 definitions[47] = 'Care or upkeep.';
 terms[48] = ' Site Managers';
 definitions[48] = 'A person or persons who manage CRHC residential properties.';
 terms[49] = ' Debris';
 definitions[49] = 'The scattered remains of something broken or destroyed;Carelessly discarded refuse; litter.';
 terms[50] = ' ducts';
 definitions[50] = 'any tube, canal, pipe, or conduit by which a fluid, air, or other substance is transported, conducted or conveyed.';
 terms[51] = ' inoperable';
 definitions[51] = 'Not functioning.';

function doHighlight(bodyText, searchTerm, position) 
{
  
    var highlightStartTag = "<span class='defineword' title='" + definitions[position] + "'>";
    var highlightEndTag = "</span>";
  
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
	//Find the start of the search term
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
	//If the search term is not found
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
			//skip any links
			if (lcBodyText.lastIndexOf("/a>", i) >= lcBodyText.lastIndexOf("<a ", i)) {
			//update text
			newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
			bodyText = bodyText.substr(i + searchTerm.length);
			lcBodyText = bodyText.toLowerCase();
			i = -1;
		  }
        }
      }
    }
  }
  
  return newText;
}


/*
 * Iterates through the list of terms, feeding them to the doHighlight function
 */
function highlightSearchTerms(contentName)
{
	var bodyText = document.getElementById(contentName).innerHTML;//document.body.innerHTML;
	for (var x = 0; x < terms.length; x++)
	{
		bodyText = doHighlight(bodyText, terms[x], x);  
	}
	document.getElementById(contentName).innerHTML = bodyText;
	return true;
}

