function patmatches(search_pat) {
    search_pat = search_pat.replace(/\./g, "\\.");
    search_pat = search_pat.replace(/\*/g, ".*");
    search_pat = search_pat.replace(/\?/g, ".?");
    return "^" + search_pat + "$";
}
function read_parameter(parameter_name)
{
    parameter_Str = document.location.search;
    if (parameter_Str == "")
        return "";
    if (parameter_Str.charAt(0) == "?")
        parameter_Str = parameter_Str.substring(1, parameter_Str.length);
    arg = (parameter_Str.split("&"));
    for (i=0; i < arg.length; i++) {
        arg_values = arg[i].split("=")
        if (unescape(arg_values[0]) == parameter_name) {
            if (self.decodeURIComponent)
                ret = decodeURIComponent(arg_values[1]);
            else
                ret = unescape(arg_values[1]);
            return ret;
        }
    }
    return;
}
// Passed data and array initialisation
var rhyme_search_word = read_parameter("rhyme_search_parameter");
rhyme_search_word = rhyme_search_word.replace(/\+/g, " ");
var rhymes_a_page = 15;
var page = parseInt(read_parameter("rhyme_search_page_on"));
if (isNaN(page)) page = 1;
var searchWords = new Array();
var data = new Array();
var newdata = new Array();
var output = new Array();
function NurseryFind() {
// Form Top
    document.writeln("<FORM name=\"search\" method=\"GET\" action=\"" + document.location.href + "\">");
    document.writeln("<INPUT type=\"text\" name=\"rhyme_search_parameter\" size=\"25\" value=\"" + rhyme_search_word + "\">");
	document.writeln("&nbsp;&nbsp;")
    document.writeln("<INPUT type=\"submit\" value=\"Search\">");
    document.writeln("</FORM>");
    document.writeln("<HR>");
    if (rhyme_search_word.length == 0) {
		document.search.rhyme_search_parameter.focus();
        return;
    }
    searchWords = rhyme_search_word.split(" ")
    document.write("<H3>Search Results for "+'"' + rhyme_search_word + '"'+"</H3>");
    numwords = searchWords.length;
//Search
    keyword_pointer = 0;
    outputline = 0;
    ipage = 0;
    for (sw = 0; sw < numwords; sw++) {
        re = new RegExp(patmatches(searchWords[sw]), "gi");
        for (keyword_pointer = 0; keyword_pointer < rhyme_keywords.length; keyword_pointer++) {
            data = rhyme_keywords[keyword_pointer].split(",");
            match_result = data[1].search(re);
            if (match_result != -1) {
                for (kw = 2; kw < data.length; kw += 1) {
                    pageexists = 0;
                    ipage = data[kw];
                    for (ol = 0; ol < outputline; ol++) {
                        if (output[ol] == ipage) {
                            pageexists = 1;
                        }
                    }
                    if (pageexists == 0) {
                        output[outputline] = new Array();
                        // now fill in the values for each field
                        output[outputline] = ipage;
                        outputline++;
                    }
                }
            }
        }
    }
// Get Rhyme Names & reset output arrayline content including sort
	arrayline = 0;
    while (arrayline < output.length) {
        ipage = output[arrayline];
		new_count =0;
		while (new_count < rhyme_name.length){
	        newdata = rhyme_name[new_count].split("-");
			if (newdata[1]== ipage){
				output[arrayline]=rhyme_name[new_count];
				new_count = rhyme_name.length+1;
			}
			new_count ++;
		}		
		arrayline++;
	}	
	output.sort();
// Get number of pages matched
    matches = outputline;
//Display Header Search results.
	if (matches == 0)
        document.writeln("No Nursery Rhymes found.<BR>");	
	if (matches == 1)
        document.writeln("One Nursery Rhyme found.<BR>");	
	if (matches > 1)
        document.writeln(matches + " Nursery Rhymes found.<BR>");
    num_pages = Math.ceil(matches / rhymes_a_page);
    if (num_pages > 1)
        document.writeln("<BR>" + num_pages + " pages of Nursery Rhymes.<BR>\n");
// Set up variables for page being displayed.
    if (page == 1) {
        outStartsAt = 0;
    } else {
        outStartsAt = ((page - 1) * rhymes_a_page);
    }
    arrayline = 0;
    out_counter = 0;
    outFinishesAt = outStartsAt + rhymes_a_page;
// Display the nursery rhyme links
    while (arrayline < output.length && out_counter < outFinishesAt) {
        out_counter++;
        if (out_counter <= outStartsAt)
        {
            arrayline++;
            continue;
        }
        newdata = output[arrayline].split("-");
        document.writeln("<P><B>");
		if (out_counter < 10) {
			document.writeln("&nbsp;");
		}
        document.writeln(out_counter + ".</B>&nbsp;&nbsp;<A href=\"" + "rhyme"+newdata[1]+".htm" + "\" target=\"blank\">" + newdata[0] + "</A>");
        document.writeln("&nbsp;&nbsp;(rhyme" + newdata[1] + ".htm)" + "</P>\n");
        arrayline++;
    }
// Bottom of page if more than one page of Nursery Rhymes found.
    if (num_pages > 1) {
        start_range = page - 10;
        if (start_range < 1)
            start_range = 1;
        end_range = page + 10;
        if (end_range > num_pages)
            end_range = num_pages;
		document.writeln("<HR>")	
        document.writeln("<P><H3>Other Pages:&nbsp;&nbsp;");
        if (page > 1)
            document.writeln("&nbsp;&nbsp;<A href=\"" + document.location.pathname + "?rhyme_search_parameter=" + rhyme_search_word + "&rhyme_search_page_on=" + (page-1) + "\">&lt;&lt;  Previous</A>&nbsp;&nbsp;");
        for (i = start_range; i <= end_range; i++) {
            if (i == page) {
                document.writeln("  " + page + "  ");
            } else {
                document.writeln("&nbsp;&nbsp;<A href=\"" + document.location.pathname + "?rhyme_search_parameter=" + rhyme_search_word + "&rhyme_search_page_on=" + i + "\">" + i + "</A>&nbsp;&nbsp;");
            }
        }
        if (page != num_pages)
            document.writeln("&nbsp;&nbsp;<A href=\"" + document.location.pathname + "?rhyme_search_parameter=" + rhyme_search_word + "&rhyme_search_page_on=" + (page+1) + "\">Next &gt;&gt;</A>&nbsp;&nbsp;");
    }
        document.writeln("<BR><BR></H3>");
}
