﻿function searchFocus(field) {
    if (field.value == "Search our ETFs") {
        field.value = "";
        field.style.color = "Black";
    }
	if (field.value != "")
		divSearchResults.style.display = "";
}

function searchBlur(field) {
    if (field.value == "") {
        field.value = "Search our ETFs";
        field.style.color = "Silver";
    }
	
	setTimeout(function () {
		divSearchResults.style.display = "none";
	}, 30);
}

function launchEtf(etf) {
    window.open("/pub/en/etfs/?etf=" + etf + "&r=o");
    divSearchResults.style.display = "none";
}

function searchEtfs(searchTerm) {

    if (searchTerm.length > 0) {

        divSearchResults.style.display = "";

        jQuery.getJSON(
                '/other/SearchETFs.ashx',
                { term: searchTerm },
                function (data) {

                    var table = "<TABLE cellpadding=5 cellspacing=0 style='position: absolute; background-color: White; border: 1px solid Gray; z-index: 2; white-space: nowrap'>";

                    jQuery.each(data, function (index, value) {

                        if (index < 10) {

                            if (value.fc == "Not found") {
                                table += "<TR><TD COLSPAN=3>Search yielded no results!</TD></TR>";
                            }
                            else {
                                table += "<TR class='ticker_etf_link searchSelection' style='cursor: pointer' onclick=\"launchEtf('" + value.fc + "');\">";

                                table += "<TD>" + value.fc + "</TD><TD>";
                                table += value.n + "</TD><TD>";

                                if (value.pc >= 0)
                                    table += "<span class=\"ticker_up\"><img src='/Images/up.png' alt='up' /> " + value.pc + "%</TD>";
                                else
                                    table += "<span class=\"ticker_down\"><img src='/Images/down.png' alt='down' /> " + value.pc + "%</TD>";

                                table += "<TD>" + value.p + "</TD></TR>";
                            }
                        }
                        else if (index == 10) {
                            table += "<TR><TD COLSPAN=3>Showing first 10 results only!</TD></TR>";
                        }
                    })

                    table += "</TABLE>";
                    divSearchResults.innerHTML = table;
                });
    } else
        divSearchResults.style.display = "none";
}
