﻿    // Detect if the browser is IE or not.
    // If it is not IE, we assume that the browser is NS.
    var IE = document.all ? true : false

    // If NS -- that is, !IE -- then set up for mouse capture
    if (!IE) document.captureEvents(Event.MOUSEMOVE)

    // Set-up to use getMouseXY function onMouseMove
    document.onmousemove = getMouseXY;

    // Temporary variables to hold mouse x-y pos.s
    var tempX = 0
    var tempY = 0

    
    var isTooltipShown = false;
    var tooltipCloseTimeout = 0;
    function showTooltip(id, content, contentId, o) {
        clearTimeout(tooltipCloseTimeout);
        if (!isTooltipShown || document.getElementById('ctrl_Tooltip_' + id).getAttribute("contentid") != contentId) {

            document.getElementById('ctrl_Tooltip_' + id).setAttribute("contentid", contentId);
            
            while (o.getAttribute("tooltip") != "true") {
                o = o.parentNode;
            }
            positionX = getX(o) + ($(o).width() / 2);
            positionY = getY(o);

            $('#ctrl_TooltipContent_' + id).html(content);
            
            var tooltipWidth = $('#ctrl_Tooltip_' + id).width();
            if (positionX - tooltipWidth < 10) {
                document.getElementById('ctrl_Tooltip_' + id).className = "ctrl_TooltipR_1";
                document.getElementById('ctrl_TooltipContent_' + id).className = "ctrl_TooltipContentR_1";
                
                $('#ctrl_Tooltip_' + id).css({ position: "absolute",
                    marginLeft: 0, marginTop: 0,
                    top: positionY - ($('#ctrl_Tooltip_' + id).height() / 2), left: positionX
                });
            }
            else {
                document.getElementById('ctrl_Tooltip_' + id).className = "ctrl_TooltipL_1";
                document.getElementById('ctrl_TooltipContent_' + id).className = "ctrl_TooltipContentL_1";
            
                $('#ctrl_Tooltip_' + id).css({ position: "absolute",
                    marginLeft: 0, marginTop: 0,
                    top: positionY - ($('#ctrl_Tooltip_' + id).height() / 2), left: positionX - $('#ctrl_Tooltip_' + id).width()
                });
            }
            
            
            isTooltipShown = true;
        }
    }

    function hideTooltip(id) {
        tooltipCloseTimeout = setTimeout("hideTooltipCallback("+id+")", 500);
    }

    function hideTooltipCallback(id) {
        $('#ctrl_Tooltip_' + id).css({ left: -2500 });
        isTooltipShown = false;
    }
    function getX(oElement) {
        var iReturnValue = 0;
        while (oElement != null) {
            iReturnValue += oElement.offsetLeft;
            oElement = oElement.offsetParent;
        }
        return iReturnValue;
    } 
    
    function getY(oElement) {
        var iReturnValue = 0;
        while (oElement != null) {
            iReturnValue += oElement.offsetTop;
            oElement = oElement.offsetParent;
        }
        return iReturnValue;
    }

    function showPhoneTooltip(tooltipId, phoneId, e) {
        clearTimeout(tooltipCloseTimeout);
        if (document.getElementById('ctrl_Tooltip_1').getAttribute("contentid") == phoneId) {
            return;
        }
        if (!e) {
            e = window.event;
        }
        var o = e.srcElement ? e.srcElement : e.target;

        showTooltip(tooltipId, "Loading...", phoneId, o);
        serviceGetContent.GetPhoneTooltip(phoneId, showPhoneTooltipCallback);
        
    }

    function showPhoneTooltipCallback(result) {
        document.getElementById('ctrl_TooltipContent_1').innerHTML = result;
    }

    function GetSearchValues_Style() {
        var phoneStyle;
        var phoneStyleValue = "";
        
        if (typeof (phoneExtendedSearch) == "undefined" || phoneExtendedSearch) {
            phoneStyle = $("input[search_type='phone_style']:checked");

            for (var i = 0; i < phoneStyle.length; i++) {
                if (phoneStyle[i].getAttribute("search_attr") == null) {
                    if (phoneStyle[i].getAttribute("value") != null && phoneStyle[i].getAttribute("value") != "") {
                        phoneStyleValue += phoneStyle[i].getAttribute("value");
                    }
                }
                else {
                    phoneStyleValue += phoneStyle[i].getAttribute("search_attr");
                }

                if (i + 1 < phoneStyle.length) {
                    phoneStyleValue += ",";
                }
            }
        }

        return phoneStyleValue;       
    }

    function GetSearchValues_Prefs() {
        var phonePrefs;
        if (typeof (phoneExtendedSearch) == "undefined" || phoneExtendedSearch) {
            phonePrefs = $("input[search_type='phone_prefs']:checked");
        }
        else {
            phonePrefs = $("*[search_type='phone_model'] option:selected");
        }

        var phonePrefsValue = "";
        for (var i = 0; i < phonePrefs.length; i++) {
            if (phonePrefs[i].getAttribute("search_attr") == null) {
                if (phonePrefs[i].getAttribute("value") != null && phonePrefs[i].getAttribute("value") != "") {
                    phonePrefsValue += phonePrefs[i].getAttribute("value");
                }
            }
            else {
                phonePrefsValue += phonePrefs[i].getAttribute("search_attr");
            }
            if (i + 1 < phonePrefs.length) {
                phonePrefsValue += ",";
            }
        }

        return phonePrefsValue;
    }

    function GetSearchValues_Plan() {
        
        var planPrefs;
        if (typeof (planExtendedSearch) == "undefined" || planExtendedSearch) {
            planPrefs = $("input[search_type='plan_prefs']:checked");
        }
        else {
            planPrefs = $("*[search_type='plan_prefs'] option:selected");
        }

        var planPrefsValue = "";
        
        for (var i = 0; i < planPrefs.length; i++) {
            if (planPrefs[i].getAttribute("search_attr") == null) {
                if (planPrefs[i].getAttribute("value") != null && planPrefs[i].getAttribute("value") != "") {
                    planPrefsValue += planPrefs[i].getAttribute("value");
                }
                else {
                    continue;
                }
            }
            else {
                if (planPrefs[i].getAttribute("search_attr") != null && planPrefs[i].getAttribute("search_attr") != "") {
                    planPrefsValue += planPrefs[i].getAttribute("search_attr");
                }
                else {
                    continue;
                }
            }
            if (i + 1 < planPrefs.length) {
                planPrefsValue += ",";
            }
        }

        return planPrefsValue;
    }

    function GetSearchValues_Brands() {
        var brands = $("input[search_type='brand']:checked");
        var brandValue = "";
        
        for (var i = 0; i < brands.length; i++) {
            brandValue += brands[i].getAttribute("search_attr");
            if (i + 1 < brands.length) {
                brandValue += ",";
            }
        }

        return brandValue;
    }

    function GetSearchValues_Providers() {
        var providers = $("input[search_type='provider']:checked");

        var providerValue = "";

        for (var i = 0; i < providers.length; i++) {
            providerValue += providers[i].getAttribute("search_attr");
            if (i + 1 < providers.length) {
                providerValue += ",";
            }
        }

        return providerValue;
    }


    function ExecuteSearch() {

        var phoneStyle;
        var phonePrefs;
        var planPrefs;
        var brands;
        var providers;

        if (typeof(phoneExtendedSearch)!="undefined" && phoneExtendedSearch) {
            phoneStyle = $("input[search_type='phone_style']:checked");
            phonePrefs = $("input[search_type='phone_prefs']:checked");
        }
        else {
            phoneStyle = $("*[search_type='phone_brand'] option:selected");
            phonePrefs = $("*[search_type='phone_model'] option:selected");
        }
        if (typeof (planExtendedSearch) != "undefined" && planExtendedSearch) {
            planPrefs = $("input[search_type='plan_prefs']:checked");
        }
        else {
            planPrefs = $("*[search_type='plan_prefs'] option:selected");
        }
        brands = $("input[search_type='brand']:has(input:checked)");
        providers = $("input[search_type='provider']:checked");
        
        var f = document.getElementsByTagName("form")[0];

        var phoneStylePostData = document.createElement("input");
        var phoneStyleValue = "";

        var phonePrefsPostData = document.createElement("input");
        var phonePrefsValue = "";

        var planPrefsPostData = document.createElement("input");
        var planPrefsValue = "";

        var brandPostData = document.createElement("input");
        var brandValue = "";

        var providerPostData = document.createElement("input");
        var providerValue = "";
        
        phoneStylePostData.setAttribute("id", "phoneStylePostData");
        phoneStylePostData.setAttribute("name", "phoneStylePostData");
        phoneStylePostData.setAttribute("type", "hidden");

        phonePrefsPostData.setAttribute("id", "phonePrefsPostData");
        phonePrefsPostData.setAttribute("name", "phonePrefsPostData");
        phonePrefsPostData.setAttribute("type", "hidden");

        planPrefsPostData.setAttribute("id", "planPrefsPostData");
        planPrefsPostData.setAttribute("name", "planPrefsPostData");
        planPrefsPostData.setAttribute("type", "hidden");        

        brandPostData.setAttribute("id", "brandsPostData");
        brandPostData.setAttribute("name", "brandsPostData");
        brandPostData.setAttribute("type", "hidden");

        providerPostData.setAttribute("id", "providersPostData");
        providerPostData.setAttribute("name", "providersPostData");
        providerPostData.setAttribute("type", "hidden");

        for (var i = 0; i < phoneStyle.length; i++) {
            if (phoneStyle[i].getAttribute("search_attr") == null) {
                if (phoneStyle[i].getAttribute("value") != null && phoneStyle[i].getAttribute("value") != "") {
                    phoneStyleValue += phoneStyle[i].getAttribute("value");
                }
            }
            else {
                phoneStyleValue += phoneStyle[i].getAttribute("search_attr");
            }
            
            if (i + 1 < phoneStyle.length) {
                phoneStyleValue += ",";
            }
        }

        for (var i = 0; i < phonePrefs.length; i++) {
            if (phonePrefs[i].getAttribute("search_attr") == null) {
                if (phonePrefs[i].getAttribute("value") != null && phonePrefs[i].getAttribute("value") != "") {
                    phonePrefsValue += phonePrefs[i].getAttribute("value");
                }
            }
            else {
                phonePrefsValue += phonePrefs[i].getAttribute("search_attr");
            }
            if (i + 1 < phonePrefs.length) {
                phonePrefsValue += ",";
            }
        }

        for (var i = 0; i < planPrefs.length; i++) {
            if (planPrefs[i].getAttribute("search_attr") == null) {
                if (planPrefs[i].getAttribute("value") != null && planPrefs[i].getAttribute("value") != "") {
                    planPrefsValue += planPrefs[i].getAttribute("value");
                }
                else {
                    continue;
                }
            }
            else {
                if (planPrefs[i].getAttribute("search_attr") != null && planPrefs[i].getAttribute("search_attr") != "") {
                    planPrefsValue += planPrefs[i].getAttribute("search_attr");
                }
                else {
                    continue;
                }
            }
            if (i + 1 < planPrefs.length) {
                planPrefsValue += ",";
            }
        }
        
        for (var i = 0; i < brands.length; i++) {
            brandValue += brands[i].getAttribute("search_attr");
            if (i + 1 < brands.length) {
                brandValue += ",";
            }
        }

        for (var i = 0; i < providers.length; i++) {
            providerValue += providers[i].getAttribute("search_attr");
            if (i + 1 < providers.length) {
                providerValue += ",";
            }
        }              

        //$("input:not([id^='__'])").remove();
        //$("select").remove();
        
        phoneStylePostData.setAttribute("value", phoneStyleValue);
        phonePrefsPostData.setAttribute("value", phonePrefsValue);
        planPrefsPostData.setAttribute("value", planPrefsValue);
        brandPostData.setAttribute("value", brandValue);
        providerPostData.setAttribute("value", providerValue);
        
        f.appendChild(phoneStylePostData);
        f.appendChild(phonePrefsPostData);
        f.appendChild(planPrefsPostData);
        f.appendChild(brandPostData);
        f.appendChild(providerPostData);

        f.action = "SearchResults.aspx";
        f.submit();
    }


    function ShowTrustedBrandInfo(id) {
        if (document.getElementById("BrandStaticText_468") == null) {
            return;
        }
        document.getElementById("BrandStaticText_468").style.display = "none";
        document.getElementById("BrandStaticText_424").style.display = "none";
        document.getElementById("BrandStaticText_422").style.display = "none";
        document.getElementById("BrandStaticText_427").style.display = "none";
        document.getElementById("BrandStaticText_453").style.display = "none";
        document.getElementById("BrandStaticText_426").style.display = "none";
        document.getElementById("BrandStaticText_428").style.display = "none";
        document.getElementById("BrandStaticText_446").style.display = "none";
        document.getElementById("BrandStaticText_425").style.display = "none";

        document.getElementById("BrandStaticText_" + id).style.display = "block";
    }

    function cancelSubmit(event) {
        if (document.all != null) {
            if (window.event.keyCode == 13) {
                window.event.cancelBubble = true; 
                return false;
            }
        }
        else {
            if (event.keyCode == 13) {
                event.stopPropagation();
                return false;
            }
        }
    }


    function showExpertReviewsPopup(text) {
        document.getElementById("expertReviewsPopup").style.display = "block";
        document.getElementById("expertReviewsPopup").style.visibility = "visible";
        document.getElementById("expertReviewsPopupContent").innerHTML = "";        
        document.getElementById("expertReviewsPopup").style.left = tempX-30 + "px";
        document.getElementById("expertReviewsPopup").style.top = tempY-120 + "px";
        document.getElementById("expertReviewsPopupContent").innerHTML = text;
    }

    function hideExpertReviewsPopup() {
        document.getElementById("expertReviewsPopup").style.display = "none";
        document.getElementById("expertReviewsPopup").style.visibility = "hidden";
    }

    function getMouseXY(e) {
        if (IE) { // grab the x-y pos.s if browser is IE
            if (event.pageX)
                tempX = event.pageX;
            else if (event.clientX)
                tempX = event.clientX + (document.documentElement.scrollLeft != null ? document.documentElement.scrollLeft : document.body.scrollLeft);
            else
                tempX = 0;

            if (event.pageY)
                tempY = event.pageY;
            else if (event.clientY)
                tempY = event.clientY + (document.documentElement.scrollTop != null ? document.documentElement.scrollTop : document.body.scrollTop);
            else
                tempY = 0;

        } else {  // grab the x-y pos.s if browser is NS
            tempX = e.pageX
            tempY = e.pageY
        }
        // catch possible negative values in NS4
        if (tempX < 0) { tempX = 0 }
        if (tempY < 0) { tempY = 0 }

        if (document.getElementById("expertReviewsPopup") != null && document.getElementById("expertReviewsPopup").style.visibility == "hidden") {
            document.getElementById("expertReviewsPopup").style.left = "-200px";
            document.getElementById("expertReviewsPopup").style.top = "-200px";

            if (navigator.appName != "Microsoft Internet Explorer") {
                tempX = tempX + "px";
                tempY = tempY + "px";
            }

        }
        return true;
    }

    function allowOnlyPhoneChars(e) {
        if (window.event)
            key = window.event.keyCode;     //IE
        else
            key = e.which;     //firefox
        if ((key < 48 || key > 57) && (key != 43 && key != 45 && key != 40 && key != 41 && key != 32 && key != 0 && key != 8)) {
            return false;
        }
        else
            return true;
    }

    function allowOnlyNumericChars(e) {
        if (window.event)
            key = window.event.keyCode;     //IE
        else
            key = e.which;     //firefox
        if ((key < 48 || key > 57) && (key != 46 && key != 43 && key != 45 && key != 110 && key != 0 && key != 8)) {
            return false;
        }
        else
            return true;
    }

    function getUrlRewriteForTextSearch(term, page) {
        if (page != 0 && page != null && typeof (page) != "undefined") {
            return "/get-" + term.replace(/ /gi, "-") + "_p" + page + ".htm";
        }
        else {
            return "/get-" + term.replace(/ /gi, "-") + ".htm";
        }

    }
