﻿jQuery.fn.highlight = function(b) { function a(e, j) { var l = 0; if (e.nodeType == 3) { var k = e.data.toUpperCase().indexOf(j); if (k >= 0) { var h = document.createElement("span"); h.className = "highlight"; var f = e.splitText(k); var c = f.splitText(j.length); var d = f.cloneNode(true); h.appendChild(d); f.parentNode.replaceChild(h, f); l = 1 } } else { if (e.nodeType == 1 && e.childNodes && !/(script|style)/i.test(e.tagName)) { for (var g = 0; g < e.childNodes.length; ++g) { g += a(e.childNodes[g], j) } } } return l } return this.each(function() { a(this, b.toUpperCase()) }) }; jQuery.fn.removeHighlight = function() { return this.find("span.highlight").each(function() { this.parentNode.firstChild.nodeName; with (this.parentNode) { replaceChild(this.firstChild, this); normalize() } }).end() };

var results = '';
var divData = '';
var divLoad = '';
var divStatus = '';
var inpSearch = '';
var ddlType = '';
var ddlState = '';
var statusCount = 0;

var arrStates = [{ State: '--', Name: '---' }, { State: 'XX', Name: 'Nationwide (No State)' }, { State: 'BG', Name: 'Bulgaria' }, { State: 'CAN', Name: 'Canada' }, { State: 'UK', Name: 'United Kingdom' }, { State: 'VI', Name: 'Virgin Islands' }, { State: '--', Name: '---' }, { State: 'AL', Name: 'Alabama' }, { State: 'AK', Name: 'Alaska' }, { State: 'AZ', Name: 'Arizona' }, { State: 'AR', Name: 'Arkansas' }, { State: 'CA', Name: 'California' }, { State: 'CO', Name: 'Colorado' }, { State: 'CT', Name: 'Connecticut' }, { State: 'DC', Name: 'Washington DC' }, { State: 'DE', Name: 'Delaware' }, { State: 'FL', Name: 'Florida' }, { State: 'GA', Name: 'Georgia' }, { State: 'HI', Name: 'Hawaii' }, { State: 'ID', Name: 'Idaho' }, { State: 'IL', Name: 'Illinois' }, { State: 'IN', Name: 'Indiana' }, { State: 'IA', Name: 'Iowa' }, { State: 'KS', Name: 'Kansas' }, { State: 'KY', Name: 'Kentucky' }, { State: 'LA', Name: 'Louisiana' }, { State: 'ME', Name: 'Maine' }, { State: 'MD', Name: 'Maryland' }, { State: 'MA', Name: 'Massachusetts' }, { State: 'MI', Name: 'Michigan' }, { State: 'MN', Name: 'Minnesota' }, { State: 'MS', Name: 'Mississippi' }, { State: 'MO', Name: 'Missouri' }, { State: 'MT', Name: 'Montana' }, { State: 'NE', Name: 'Nebraska' }, { State: 'NV', Name: 'Nevada' }, { State: 'NH', Name: 'New Hampshire' }, { State: 'NJ', Name: 'New Jersey' }, { State: 'NM', Name: 'New Mexico' }, { State: 'NY', Name: 'New York' }, { State: 'NC', Name: 'North Carolina' }, { State: 'ND', Name: 'North Dakota' }, { State: 'OH', Name: 'Ohio' }, { State: 'OK', Name: 'Oklahoma' }, { State: 'OR', Name: 'Oregon' }, { State: 'PA', Name: 'Pennsylvania' }, { State: 'RI', Name: 'Rhode Island' }, { State: 'SC', Name: 'South Carolina' }, { State: 'SD', Name: 'South Dakota' }, { State: 'TN', Name: 'Tennessee' }, { State: 'TX', Name: 'Texas' }, { State: 'UT', Name: 'Utah' }, { State: 'VT', Name: 'Vermont' }, { State: 'VA', Name: 'Virginia' }, { State: 'WA', Name: 'Washington' }, { State: 'WV', Name: 'West Virginia' }, { State: 'WI', Name: 'Wisconsin' }, { State: 'WY', Name: 'Wyoming'}];

function setResults(arr) {
    results = eval(arr);
}

function displayResults(state) {
    var Type = ddlType.options[ddlType.selectedIndex].value;
    var laststate = '';
    divData.html('');
    if (state != '') {
    	$.each(results, function(i, o) {
    		if (o.State == state && o.Type == Type) {
    			if (laststate != o.State) {
    				displayHeader(o.State);
    				laststate = o.State;
    			}
    			displayRow(o.School);
    			statusCount++;
    		}
    	});
    }
    else {
        $.each(results, function (i, o) {
            if (o.Type == Type) {
                if (laststate != o.State) {
                    displayHeader(o.State);
                    laststate = o.State;
                }
                displayRow(o.School);
                statusCount++;
            }
        });
    }
    divData.show();
    divLoad.hide();
    displayStatus();
    resetCount();
}

function searchResults(sSearch) {
    var Type = ddlType.options[ddlType.selectedIndex].value;
    var searchString = trim(inpSearch.value.toUpperCase());
    if (searchString == '') {return;}
	var laststate = '';
	divData.html('');
	if (searchString.length > 0) {
	    $.each(results, function (i, o) {
	        if (o.Type == Type) {
	            if (o.School.toUpperCase().indexOf(searchString) > -1) {
	                if (laststate != o.State) {
	                    displayHeader(o.State);
	                    laststate = o.State;
	                }
	                displayRow(o.School);
	                statusCount++;
	            }
	        }
	    });
		displayStatus(statusCount + ' mathing school(s).');
		resetCount();
		resetStateFilter();
		$('.school').highlight(searchString);
	}
	else {
		displayStatus('0 mathing school(s).');
	}
}

function displayRow(school) {
    divData.append('<span class="school">' + school + '</span>');
}

function displayHeader(state) {
    divData.append('<span class="state-header">' + state + '</span>');
}

function displayStateFilter() {
	$.each(arrStates, function(i, o) {
		var opt = '<option value="' + o.Name + '"';
		if (o.State == '--') {
			opt += ' disabled="disabled"';
		}
		opt += '>' + o.Name + '</option>'
		$('#state-filter').append(opt)
	});
}

function filterByState(slctBx) {
    divData.show();
    divLoad.hide();
    var state = slctBx.options[slctBx.selectedIndex].value;
    displayResults(state);
    resetSearch();
}

function filterByType() {
    divData.show();
    divLoad.hide();
    var searchString = trim(inpSearch.value.toUpperCase());
    if (searchString.length > 0) {
        searchResults(null);
    } else {
        var state = ddlState.options[ddlState.selectedIndex].value;
        filterByState(ddlState);
    }
}

function LoadInitialResults(data) {
    divData = $('#network-members');
    divLoad = $('#network-loading');
    divStatus = $('#statusMsg');
    inpSearch = document.getElementById('school-search');
    ddlState = document.getElementById('state-filter');
    ddlType = document.getElementById('member-type');
    $(inpSearch).keydown(isEnter);
    $(document).ready(function() {
        setResults('[' + data.substr(2) + ']');
        displayStateFilter();
        displayResults('');
    });
}

function displayStatus() {
	divStatus.html(statusCount + ' matching school(s)');
}

function resetCount() {
	statusCount = 0;
}

function resetSearch() {
    inpSearch.value = '';
}

function resetStateFilter() {
	ddlState.selectedIndex = 0;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function isEnter(e) {
    var k = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    if (k == 13) {
        e.preventDefault();
        searchResults(inpSearch);
    }
}

function printMembers() {
    var w = window.open('');
    w.document.write('<html><head><title>Scrip-Safe - Network Members - ' + ddlType.options[ddlType.selectedIndex].text + '</title><link href="/Portals/scrip-safe/Skins/scrip-safe/skin.css" rel="Stylesheet" type="text/css" /></head><body style="background-color: #fff ; padding: 20px ;"><div style="width:700px;"><div><img alt="Scrip-Safe International - Secure Document Solutions" src="/Portals/scrip-safe/Images/scrip-safe-logo.gif"></div><h1>Network Members - ' + ddlType.options[ddlType.selectedIndex].text + '</h1>' + divData.html() + '</div><script type="text/javascript">window.onload = function() { window.print(); }</script></body></html>');
    w.document.close();
}