﻿function highlightSearchContent(text) {
    var bodyText = document.getElementById('SearchResults').innerHTML;
    highlightSearchTerms(bodyText, text);
}

/*
* 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.
*/
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) {
    // the highlightStartTag and highlightEndTag parameters are optional
    if ((!highlightStartTag) || (!highlightEndTag)) {
        highlightStartTag = "<font style='color:#f08600;'>";
        highlightEndTag = "</font>";
    }

    // 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) {
        i = lcBodyText.indexOf(lcSearchTerm, i + 1);
        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)) {
                    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;
}

/*
* This is sort of a wrapper function to the doHighlight function.
* It takes the searchText that you pass, optionally splits it into
* separate words, and transforms the text on the current web page.
* Only the "searchText" parameter is required; all other parameters
* are optional and can be omitted.
*/
function highlightSearchTerms(bodyText, searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag) {
    // if the treatAsPhrase parameter is true, then we should search for 
    // the entire phrase that was entered; otherwise, we will split the
    // search string so that each word is searched for and highlighted
    // individually

    if (treatAsPhrase) {
        searchArray = [searchText];
    } else {
        searchArray = searchText.split(" ");
    }

    if (!document.body || typeof (document.body.innerHTML) == "undefined") {
        if (warnOnFailure) {
            alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
        }
        return false;
    }

    //var bodyText = document.body.innerHTML;
    for (var i = 0; i < searchArray.length; i++) {
        bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
    }

    document.getElementById('SearchResults').innerHTML = bodyText;
    return true;
}

function handleSelectedFilters(strFilters) {
    try {
        if (strFilters != '') {
            var arr = strFilters.split(',');
            var fmobj = document.forms[0].searchtypes;
            for (var i = 0; i < arr.length; i++) {
                for (var j = 0; j < fmobj.length; j++) {
                    var e = fmobj[j];
                    if (e.value == arr[i]) {
                        e.checked = true;
                    }
                }
            }
        }
    }
    catch (e) { }
}


function handleBottomRest(intSwitch, strId) {
	var strCasesBottomPrevNext = '';
    switch (intSwitch) {
        case 1:
            document.getElementById('peer_start').style.display = "inline";
            document.getElementById('peer_left_1').style.display = "none";
            document.getElementById('peer_left_2').style.display = "none";
            document.getElementById('peer_tussen').style.display = "none";
            document.getElementById('peer_right_1').style.display = "inline";
            document.getElementById('peer_right_2').style.display = "inline";
            document.getElementById('peer_right_3').style.display = "inline";
            document.getElementById('peer_end').style.display = "none";
			indexCasesBottom = 1; handleCasesBottomPrevNext('left', false);
            break;
        case 2:
            document.getElementById('peer_start').style.display = "none";
            document.getElementById('peer_left_1').style.display = "inline";
            document.getElementById('peer_left_2').style.display = "none";
            document.getElementById('peer_tussen').style.display = "inline";
            document.getElementById('peer_right_1').style.display = "inline";
            document.getElementById('peer_right_2').style.display = "none";
            document.getElementById('peer_right_3').style.display = "inline";
            document.getElementById('peer_end').style.display = "none";
			indexCasesBottom = 3; handleCasesBottomPrevNext('left', false);
            break;
        case 3:
            document.getElementById('peer_start').style.display = "none";
            document.getElementById('peer_left_1').style.display = "inline";
            document.getElementById('peer_left_2').style.display = "inline";
            document.getElementById('peer_tussen').style.display = "inline";
            document.getElementById('peer_right_1').style.display = "none";
            document.getElementById('peer_right_2').style.display = "none";
            document.getElementById('peer_right_3').style.display = "none";
            document.getElementById('peer_end').style.display = "inline";
			indexCasesBottom = 2; handleCasesBottomPrevNext('right', false);
            break;
    }
	setBottomCases(intSwitch);
}


function setBottomCases(intSwitch) {
    try {
        if (document.getElementById('bottom_url_' + intSwitch).innerHTML != '') {
            document.getElementById('bottom_href').innerHTML = document.getElementById('bottom_url_' + intSwitch).innerHTML;
        } else {
            document.getElementById('bottom_href').innerHTML = '';
        }
    } catch (e) { }
}

function initBottomCases(){
	var url = strBottom.replace("~", "");
	try{
		for(var i = 1;i < 4;i++){
			if(document.getElementById('bottom_url_'+i).innerHTML.indexOf(url) != -1){
				if(i<3){
				    switch (i) {
				        case 1:
							var strEen = document.getElementById('bottom_url_1').innerHTML;
							var strTwee = document.getElementById('bottom_url_2').innerHTML;
							document.getElementById('bottom_url_1').innerHTML = strTwee;
							document.getElementById('bottom_url_2').innerHTML = strEen;
						break;
				        case 2:
							var strEen = document.getElementById('bottom_url_1').innerHTML;
							var strTwee = document.getElementById('bottom_url_3').innerHTML;
							document.getElementById('bottom_url_1').innerHTML = strTwee;
							document.getElementById('bottom_url_3').innerHTML = strEen;
						break;
					}
				}
				break;
			}
		}
	}catch(e){}
	document.getElementById('casesTitleBottom').innerHTML = document.getElementById('casesTitle').innerHTML;
	setBottomCases(1);
}

var indexCasesBottom = 1;

function handleCasesBottomPrevNext(direction, blnHBR){
    switch (indexCasesBottom) {
        case 1:
			if(direction=='left'){
				indexCasesBottom = 1;
				document.getElementById('previous_case').style.display = "none";
				document.getElementById('next_case').style.display = "block";
			}else{
				indexCasesBottom = 2;
				document.getElementById('previous_case').style.display = "block";
				document.getElementById('next_case').style.display = "block";
			};
		break;
        case 2:
			if(direction=='left'){
				indexCasesBottom = 1;
				document.getElementById('previous_case').style.display = "none";
				document.getElementById('next_case').style.display = "block";
			}else{
				indexCasesBottom = 3;
				document.getElementById('previous_case').style.display = "block";
				document.getElementById('next_case').style.display = "none";
			};
		break;
        case 3:
			if(direction=='left'){
				indexCasesBottom = 2;
				document.getElementById('previous_case').style.display = "block";
				document.getElementById('next_case').style.display = "block";
			};
		break;
	}
	if(blnHBR){handleBottomRest(indexCasesBottom, 0)};
}
