<!--

function findAndReplace(haystack, needle, replacement) {
//-- Find all occurences of a string in a screen object.
//-- The whole screen could be wrapped in a <div> and searched.
//-- If the 'replacement' is left blank then the found 'needles' 
//-- will be highlighted.

	var delaytime=1 * 1000;
	setTimeout("doFindAndReplace('"+haystack+"', '"+needle+"', '"+replacement+"')",delaytime);

}

function doFindAndReplace(haystack, needle, replacement) {

	replacement = replacement+'';

	var haystackObj = MM_findObj(haystack);
	if(!haystackObj){
		alert("'Haystack' object '"+haystack+"' is missing from the page.");
	}else{
		var haystackText = haystackObj.innerHTML;
		var matched = new RegExp(needle, "ig");     

		if(replacement.length > 0) {
		  replaced = haystackText.replace(matched, replacement);
		}else {
		  var boldText = "<div style=\"background-color: yellow; display: inline; font-weight: bold;\">" + needle + "</div>";
		  replaced = haystackText.replace(matched, boldText);
		}
		haystackObj.innerHTML = replaced;
	}
}

function Trim(the_string){

	the_string = LTrim(the_string);
	return RTrim(the_string);
}

function RTrim(the_string){

	if(typeof(the_string)=='string'){
		if(the_string.length > 0){
			while(the_string.charAt((the_string.length -1))==" "){
			the_string = the_string.substring(0,the_string.length-1);
			}
		}
	}
	return the_string;
}

function LTrim(the_string){

	if(typeof(the_string)=='string'){
		if(the_string.length > 0){
			while(the_string.charAt(0)==" "){
			the_string = the_string.replace(the_string.charAt(0),"");
			}
		}
	}
	return the_string;
}
	
function removeWhiteSpace(theString) {
//-- Remove all white space from the string.	

	var charStart='!';
	var charEnd='z';
	var newString = '';

	if(typeof(theString) == 'string'){
	
		var slen = theString.length;
		
		if(slen>0){
			for(var i=0; i<slen; i++){
	
				if(theString.charCodeAt(i) >= charStart.charCodeAt(0) && theString.charCodeAt(i) <= charEnd.charCodeAt(0)){
					newString = newString + theString.charAt(i);
				}
	
			}
		}
	}else{
		newString = theString;
	}

	return newString;
}

function convertWhiteSpace(theString){
//-- Convert all white space from the string into a blank space CHAR(32)

	var charStart='!';
	var charEnd='z';
	var newString = '';

	if(typeof(theString) == 'string'){
	
		var slen = theString.length;
		
		if(slen>0){
			for(var i=0; i<slen; i++){
	
				if(theString.charCodeAt(i) >= charStart.charCodeAt(0) && theString.charCodeAt(i) <= charEnd.charCodeAt(0)){
					newString = newString + theString.charAt(i);
				}else{
					if(theString.charCodeAt(i) == 10){
						newString = newString + '|';
					}else{
						newString = newString + ' ';
					}
				}
	
			}
		}
	}else{
		newString = theString;
	}
	
	return newString;
}

function uppercase(textObj) {
//-- Convert the value in the text object to uppercase.

	//-- Only upshift if lowercase.
	//-- otherwise the cursor will continously jump to the end of the line when
	//-- the user is pressing the cursor or delete keys.
	var theString = textObj.value;
	var l = textObj.value.length;
	for(var i=0;i<l;i++){
		onechar = theString.substr(i,1);
		if(onechar >= 'a' && onechar <= 'z'){
			textObj.value = textObj.value.toUpperCase();
			break;
		}
	}
}

function lowercase(textObj) {
//-- Convert the value in the text object to lowercase.

	textObj.value = textObj.value.toLowerCase();
}

function titlecase(fieldObj){
//-- Convert the string in the field object to title case.
//-- Call the function using the field object.

//-- NOTE Do NOT alter any characters that are already in uppercase UNLESS they are ALL in uppercase (CAPS lock is on)

	if(!fieldObj){
		alert('Error... no field object passed to function titlecase.');
	}else{

		var fieldval = Trim(fieldObj.value);
		
		if(fieldObj.type !='undefined'){

			if(fieldval.length>0){
				//-- The user may have had caps lock on so set all to lower case.
				if(fieldval.toUpperCase() == fieldval){
					//-- ALL characters are in UPPERCASE, so convert to lower case.
					fieldval = fieldval.toLowerCase();
				}
				//-- replace double spaces with single spaces.
				fieldval.replace(/  /g," ");
				
				var split_words = fieldval.split(" ");
				
				//-- This may not be a complete list of words that shouldn't be capitalized
				var special_words = new Array('a', 'at', 'an', 'and', 'by', 'for', 'from', 'is', 'in', 'if', 'of', 'the', 'to');
				for(var i=0;i<split_words.length;i++) {
		
					if(i == 0) {
						//-- Always capitalize the first word
						split_words[i] = (split_words[i].substring(0,1)).toUpperCase() + split_words[i].substring(1);
					}else{
						foundSpecialWord = false;
						for(sw=0; sw<special_words.length; sw++){
							if(split_words[i]==special_words[sw]){
								foundSpecialWord = true;
								break;
							}
						}
		
						if(foundSpecialWord == false){
							split_words[i] = (split_words[i].substring(0,1)).toUpperCase() + split_words[i].substring(1);
						}
					}
				}
				
				fieldval = split_words.join(' ');
				
				fieldObj.value = Trim(fieldval);
			}
		}
	}
}

function titleCaseIfLowerCase(fieldObj){
//-- Convert the string in the field object to title case.
//-- BUT does not convert any chars that are already UPPERCASE to lowercase.

	if(!fieldObj){
		alert('Error... no field object passed to function titlecase.');
	}else{

		//var fieldval = Trim(fieldObj.value);
		var fieldval = fieldObj.value;
		//-- replace double spaces with single spaces.
		fieldval = fieldval.replace(/  /g," ");
		
		var ln=fieldval.length -1;
		var last = '';
		var newString = '';
		var foundSpacer = true;
		
		for(var i=0;i<=ln;i++){
			
			origChar=fieldval.substr(i,1); 	
			if(origChar < '!' || origChar > '~'){
				foundSpacer = true;
			}else{
				
				if(origChar>='a' && origChar<='z' && foundSpacer==true){
					if(last>= 'A' && last<='Z'){
						origChar = origChar.toLowerCase();
					}else{
						origChar = origChar.toUpperCase();
					}
				}
				
				last = origChar;
				foundSpacer = false;
			}
			
			newString += origChar;
			
		}

		fieldObj.value = newString; 
	}

}

function upperCaseFirstChar(obj){ 
//-- Converts the first char in the string to upper.
   
	origString = Trim(obj.value);
	var ln = origString.length;
	var newString = '';
	
	if(ln>0){
		newString = origString.substr(0,1).toUpperCase();
		newString += origString.substr(1, ln -1);
	}

	obj.value = newString; 
}

function areStringsSimilar(string1, string2){
//-- http://www.devarticles.com/c/a/Development-Cycles/Tame-the-Beast-by-Matching-Similar-Strings/3/
//-- The Soundex Algorithm 

//-- Letter        Phonetic Code 

//-- B,F,P,V             1 
//-- C,G,J,K,Q,S,X,Z     2 
//-- D,T                 3 
//-- L                   4 
//-- M,N                 5 
//-- R                   6 
//-- A,E,I,O,U,Y,H,W  not coded 

	var areSimilarFlag = false;
	var hold1 = string1;
	return1 = soundexAlgorithm(string1);

	var hold2 = string2;
	return2 = soundexAlgorithm(string2);

	//-- If the soundex does not return an exact match do we have a close match?
	if(return1 == return2){
		areSimilarFlag = true;
	}else{
		if(return1.length > return2.length){
			if(return1.length - return2.length < 3){
				if(return1.indexOf(return2) > -1){
					areSimilarFlag = true;
				}
			}
		}else{
			if(return2.length - return1.length < 3){
				if(return2.indexOf(return1) > -1){
					areSimilarFlag = true;
				}			
			}
		}
	}

/**
if(areSimilarFlag){
	alert('true');
}else{
	alert('true');
}
**/

	return areSimilarFlag;
}

function areStringsSimilarPercent(string1, string2){
//-- See function areStringsSimilar above
//-- Returns the Soundex Algorithm difference between two strings or -1 if not similar.

	var areSimilarFlag = false;
	var hold1 = string1;
	var matchpercent = 0;
	var return1 = soundexAlgorithm(string1);

	var hold2 = string2;
	var return2 = soundexAlgorithm(string2);

	//-- If the soundex does not return an exact match do we have a close match?
	if(return1 == return2){
		areSimilarFlag = true;
		matchpercent = 100;
	}else{
	
		if(return1.length > return2.length){
			if(return1.length - return2.length < 3){
				if(return1.indexOf(return2) > -1){
					areSimilarFlag = true;
					matchpercent = parseInt(return2.length/return1.length*100,10);
				}
			}
		}else{
			if(return2.length - return1.length < 3){
				if(return2.indexOf(return1) > -1){
					areSimilarFlag = true;
					matchpercent = parseInt(return1.length/return2.length*100,10);
				}			
			}
		}

		var leven = levenshtein(return1, return2);
		var lens = (return1.length + return2.length) /2;
		matchpercent = 100 - parseInt(leven / lens * 100, 10);

/**
if(string2=='Northamptonshire'){
alert(string1+'='+return1+'  '+string2+'='+return2+'  matchpercent='+matchpercent+'  leven='+leven);
}
**/
	}

	return matchpercent;



	
	//return metaphone_match(string1, string2);
}

function metaphone_match(str1, str2){
//-- Metaphone is supposedly more accurate than Soundex because it knows the basic rules of English pronunciation.

//alert('function metaphone_match('+str1+'  '+str2+')');

	var matchpercent = 0;
	
alert('1');
	var meta_1 = metaphone('str1');
alert('2');
	var meta_2=metaphone(str2);

	if(meta_1 == meta_2){
		matchpercent = 100;
	}else{
		if(meta_1 > meta_2){
			matchpercent = parseInt(meta_2/meta_1 * 100,10);
		}else{
			matchpercent = parseInt(meta_1/meta_2 * 100,10);
		}
	}
	
	return matchpercent;
	
}


function soundexAlgorithm(theString){

//-- Letter        Phonetic Code

//-- B,F,P,V             1 
//-- C,G,J,K,Q,S,X,Z     2 
//-- D,T                 3 
//-- L                   4 
//-- M,N                 5 
//-- R                   6 
//-- A,E,I,O,U,Y,H,W  not coded 

//-- Conditions:
//-- 1. adjacent pairs of the same consonant are treated as one
//-- 2. adjacent consonants from the same code group are treated as one
//-- 3. a consonant immediately following an initial letter from the same code group is ignored
//-- 4. consonants from the same code group separated by W or H are treated as one

	theString = theString.toLowerCase();
	
	//-- Conditions 1 
	theString = stripOutDuplicateLetters(theString);

	var onechar = '';
	var returnCode = '';
	var lastgroup = '0'; //-- For condition 4 above.
	var prevchar = ''; //-- For condition 4 above.	

	for(var i=0;i<theString.length; i++){
		onechar = theString.substr(i,1);
//alert('onechar='+onechar+' returnCode='+returnCode+' lastgroup='+ lastgroup+' prevchar='+ prevchar);		
		switch(onechar){
			case 'b':
			case 'f':
			case 'p':
			case 'v': returnCode += soundexAlgorithmConditions(returnCode, lastgroup, prevchar, '1'); lastgroup = '1'; break;
			case 'c':
			case 'g':
			case 'j':
			case 'k':
			case 'q':
			case 's':
			case 'x':			
			case 'z': returnCode = soundexAlgorithmConditions(returnCode, lastgroup, prevchar, '2'); lastgroup = '2'; break;
			case 'd':
			case 't': returnCode = soundexAlgorithmConditions(returnCode, lastgroup, prevchar, '3'); lastgroup = '3'; break;
			case 'l': returnCode = soundexAlgorithmConditions(returnCode, lastgroup, prevchar, '4'); lastgroup = '4'; break;
			case 'm':			
			case 'n': returnCode = soundexAlgorithmConditions(returnCode, lastgroup, prevchar, '5'); lastgroup = '5'; break;
			case 'r': returnCode = soundexAlgorithmConditions(returnCode, lastgroup, prevchar, '6'); lastgroup = '6'; break;
		}
		
		prevchar = onechar;
	}
	
	return returnCode;
}

function soundexAlgorithmConditions(returnCode, lastgroup, prevchar, currCode) {

	if(lastgroup == 0){
		returnCode += currCode;
	}else{
		//-- Condition 2
		if(lastgroup != currCode){
			//-- Condition 4	
			if(prevchar != 'w' && prevchar != 'h'){	
				returnCode += currCode;
			}
		}
	}
	
	return returnCode;
}

function stripOutDuplicateLetters(theString){
//-- Strip out duplicate letters and spaces from the input string 
//-- Ie ffre ddd becomes fred

	var ns = '';
	var prev = '';
	
	theString = theString.toLowerCase();
	
	for(var i=0;i<theString.length;i++){
		if(theString.substr(i,1) != prev){
			prev = Trim(theString.substr(i,1));
			if(prev.length > 0){
				ns += prev;
			}
		}
	}
	
	return ns;
}
	
function levenshtein( a, b )
{
        var i;
        var j;
        var cost;
        var d = new Array();
 
        if( a.length == 0 )
        {
                return b.length;
        }
 
        if( b.length == 0 )
        {
                return a.length;
        }
 
        for ( i = 0; i <= a.length; i++ )
        {
                d[ i ] = new Array();
                d[ i ][ 0 ] = i;
        }
 
        for ( j = 0; j <= b.length; j++ )
        {
                d[ 0 ][ j ] = j;
        }
 
        for ( i = 1; i <= a.length; i++ )
        {
                for ( j = 1; j <= b.length; j++ )
                {
                        if( a.charAt( i - 1 ) == b.charAt( j - 1 ) )
                        {
                                cost = 0;
                        }
                        else
                        {
                                cost = 1;
                        }
 
                        d[ i ][ j ] = Math.min( d[ i - 1 ][ j ] + 1, d[ i ][ j - 1 ] + 1, d[ i - 1 ][ j - 1 ] + cost );
                }
        }
 
        return d[ a.length ][ b.length ];
}


-->
