 /*
	This function is responsible to create links out of key pharses.
	It checks the existence of key phrases as well as contextual phrases
	inside the element, id of which is given in variable "idOfElementToModify".

	This function also takes care of case sensitive or cas insensetive search based on the
	value of variable "keyPhrasesCaseSensistive"

	This function also ignores the tags given by user in the "ignoreTags" array.
	To do this, it replaces the tags to ignore with certain pattern in the content of the
	desired element and then it process the content to place the links. Once the link placements
	is over, it replaces the patterns with original ignored tags. To accomplish this task it
	stores the temporarily igonred tags into "tagStorage" array.
*/
function findLink() {
	if(!document.getElementById(idOfElementToModify) || document.getElementById(idOfElementToModify)==null ) {
		return false;
	}
	var tempDivText = document.getElementById(idOfElementToModify).innerHTML;
	//  create array to store ignore tag
	var tagStorage = new Array();
	var imgArray = new Array();
	tempDivText = placeImg(tempDivText,imgArray);
	for(var i=0; i<ignoreTags.length;i++) {
		tagStorage[i] = new Array();
		tempDivText = skipTags(ignoreTags[i], tagStorage[i], tempDivText);
	}
	var divText = "";
	var i = 0;
	// key phrases insensistive
	var capsDivText = '';
	if(!keyPhrasesCaseSensistive) {
		capsDivText = tempDivText.toUpperCase();
	}
	var j; 
	// find the contextual link
	for (var i=0; i<phrases.length; i++) {
		if((keyPhrasesCaseSensistive && tempDivText.indexOf(phrases[i][0])>=0) || (!keyPhrasesCaseSensistive && capsDivText.indexOf(phrases[i][0].toUpperCase())>=0)) {	
			// create a link for eligible key phrases
			var prevIndex = -1;
			if(keyPhrasesCaseSensistive) {
				prevIndex = tempDivText.indexOf(phrases[i][0]);
			} else {
				prevIndex = capsDivText.indexOf(phrases[i][0].toUpperCase());
			}
			replaceString = "<a name='"+phrases[i][0]+"' href=" + phrases[i][1] + ">"+phrases[i][0]+"</a>";
			while(prevIndex != -1) {
				var origWord = tempDivText.substring(prevIndex, prevIndex+phrases[i][0].length);
				if(navigator.appName.indexOf('Netscape') != -1) {
					tempDivText = tempDivText.replace(phrases[i][0],"<a name='"+phrases[i][0]+"' href=" + phrases[i][1] + ">"+origWord+"</a>","i");
				} else {
					tempDivText = tempDivText.replace(new RegExp( phrases[i][0], "i" ),"<a name='"+phrases[i][0]+"' href=" + phrases[i][1] + ">"+origWord+"</a>");
				}
				divText += tempDivText.substring(0,prevIndex + replaceString.length + 1);
				tempDivText = tempDivText.substring(prevIndex + replaceString.length + 1);
				if(!keyPhrasesCaseSensistive) {
					capsDivText = tempDivText.toUpperCase();
					prevIndex = capsDivText.indexOf(phrases[i][0].toUpperCase());
				} else {
					prevIndex = tempDivText.indexOf(phrases[i][0]);
				}
			}
			tempDivText = divText + tempDivText;
			if(!keyPhrasesCaseSensistive)capsDivText = tempDivText.toUpperCase();
				 
		}
		divText = "";
	}
	tempDivText = replaceImg(tempDivText, imgArray);
	// replace ignored tag	
	for(var i=0; i<ignoreTags.length;i++) {
		var tagPattern = "##"+ignoreTags[i]+"##";
		tempDivText = placeHeadings(tempDivText, tagStorage[i], tagPattern);
	}
	document.getElementById(idOfElementToModify).innerHTML = tempDivText;
}

/*
	This function is called for each tag to be ignored.
	It prepares the start and end tags for each tag to be ignored.
	With these start and end tags, it calls the ignoreHeading function.
*/
function skipTags(given, storageArray, tempDivText) {
	var givenSmall = false;
	var other = '';
	for(var i=0;i<given.length;i++) {
		var j = given.charCodeAt(i);
		var t = j;
		if(j >=65 && j<=90) {
			var t = j + 32;
		} else if(j >= 97 && j<=122){
			givenSmall = true;
			var t = j - 32;
		}
		other += String.fromCharCode(t);
	}

	if(givenSmall) {
		smallStart = "<"+given;
		smallEnd = "</"+given+">";
		capStart = "<"+other;
		capEnd = "</"+other+">";
	} else {
		smallStart = "<"+other;
		smallEnd = "</"+other+">";
		capStart = "<"+given;
		capEnd = "</"+given+">";
	}
	var tagPattern = "##"+given+"##";
	tempDivText = ingoreHeadings(smallStart,capStart,smallEnd,capEnd,storageArray,tagPattern, tempDivText);
	return tempDivText;
}


/*
	This function does the actual work of ignoring the tags.
	It replaces the content of a tag with a pattern related to that tag and returns
	the modified text as content to be processed for placing links.
*/
function ingoreHeadings(smallStart, capitalStart, smallEnd, capitalEnd, storageArray, replacePattern, tempDivText) {
	var startIndex = tempDivText.indexOf(smallStart);
	if(startIndex < 0) {
		startIndex = tempDivText.indexOf(capitalStart);
	}
	var i = 0;
	while( startIndex >= 0)  {
		var endIndex = tempDivText.indexOf(smallEnd);
		if(endIndex == -1) {
			endIndex = tempDivText.indexOf(capitalEnd);
		}
		headingText = tempDivText.substring(startIndex, endIndex+capitalEnd.length);
		storageArray[i++] = headingText;
		tempDivText = tempDivText.replace(headingText,replacePattern);
		startIndex = tempDivText.indexOf(smallStart);
		if(startIndex < 0) {
			startIndex = tempDivText.indexOf(capitalStart);
		}
	}
	return tempDivText;
}
/*
	This function is responsible for replacing the patterns with the original
	contents of ignored tags.
*/
function placeHeadings(tempDivText, storageArray, replacePattern) {
	var i=0;
	while(tempDivText.indexOf(replacePattern) >= 0) {
		tempDivText = tempDivText.replace(replacePattern, storageArray[i++]);
	}
	return tempDivText;
}
function placeImg(tempDivText,imgArray)
{	
	var i=0;
	var replacePattern = "##img##"
	var startIndex = tempDivText.indexOf("<img");
	if (startIndex == -1)
	{
		startIndex = tempDivText.indexOf("<IMG");
	}
	while( startIndex >= 0)  {
		var endIndex = tempDivText.indexOf(">",startIndex);
		var headingText = tempDivText.substring(startIndex, endIndex+1);
		imgArray[i++] = headingText;
		tempDivText = tempDivText.replace(headingText,replacePattern);
		startIndex = tempDivText.indexOf("<img");
		if (startIndex < 0)	{
			startIndex = tempDivText.indexOf("<IMG");
		}
	}
	return tempDivText; 
}
function replaceImg(tempDivText, imgArray) {
	var i=0;
	var replacePattern = "##img##";
	while(tempDivText.indexOf(replacePattern) >= 0) {
		tempDivText = tempDivText.replace(replacePattern, imgArray[i++]);
	}
	return tempDivText;
}


