﻿//Added constants for the menu Iframes, needs to match the style sheet
var MENU_HEIGHT = 24;
var MENU_PADDING = 1;
var MENU_TOP_MARGIN = 0;
var LOC_NAME_LENGTH = 42;

function startList() {
    var menu = document.getElementById("topMenu");
    if (document.getElementById && menu.style.display != "none" && menu.childNodes.length > 0) {
        var menuCss;
        var selectedMenu;
        var menuBackground;

        navItems = menu.getElementsByTagName("li");
        if (navItems.length > 0) {
            for (var i = 0; i < navItems.length; i++) {
                node = navItems[i];
                var link = node.getElementsByTagName("a")[0];
                if (link != null)
                    link.setAttribute("index", i);
                node.onmouseover = overItem;
                node.onmouseout = outItem;
            } 
        } 
    }
}

overItem = function(evt) {
    evt = (evt) ? evt : ((window.event) ? window.event : null);

    var targetElt = this;

    var oLink = targetElt.getElementsByTagName("a")[0];
    if (oLink && oLink.getAttribute("index") != null) {
        var index = oLink.getAttribute("index")

        // Create child lists if needed
        if (index < aMenu.length && targetElt.getElementsByTagName("ul").length == 0) {
            InsertChildren(targetElt, index);
        }
    }

    if (targetElt.parentNode.id == "topMenu") {
        var iframe = this.getElementsByTagName("iframe")[0];
        if (iframe) {
            iframe.style.visibility = "visible";
        }
        else {
            iframe = document.createElement("iframe");
            iframe.setAttribute("src", "/images/dot.gif");
            iframe.setAttribute("scrolling", "no");
            iframe.setAttribute("frameborder", "0");
            iframe.style.zIndex = "0";
            iframe.style.borderWidth = "0";

            var firstC = this.firstChild;
            while (firstC.nodeName == "#text" && firstC.nextSibling != null)
                firstC = firstC.nextSibling;
            if (firstC.nodeName == "#text")
                firstC = null;

            if (firstC != null && firstC.nodeName.toUpperCase() == "A") {
                var ul = this.getElementsByTagName("ul")[0].childNodes.length;
                iframe.width = this.offsetWidth * 2 - 13;
                iframe.height = MENU_HEIGHT * ul + MENU_PADDING;
            }
            else {
                iframe.width = 0;
                iframe.height = 0;
            }
            if (MENU_TOP_MARGIN > 0)
                iframe.style.top = MENU_TOP_MARGIN + "px";
            iframe.style.position = "absolute";
            this.appendChild(iframe);
        }
    }

    // IE hack for rollover state
    if (document.all && document.getElementById) {
        targetElt.className += " sfhover";
    }
}

outItem = function() {
    var targetElt = this;
    if (document.all && document.getElementById) {
        targetElt.className = targetElt.className.replace(new RegExp(" sfhover\\b"), "");
    }
    var iframe = this.getElementsByTagName("iframe")[0];
    if (iframe) {
        iframe.style.visibility = "hidden";
    }
}

function InsertChildren(ListItem, Index) {
    var ul = document.createElement("ul");
    if(location.href.indexOf('http://localhost') > -1)
        ul.innerHTML = aMenu[Index].replace('http://www.comcast.com/', '/');
    else
        ul.innerHTML = aMenu[Index];
    ul.style.zIndex = "99";
    ListItem.appendChild(ul);
}

function SearchTV(anchor, url, fieldId) {
    var field=document.getElementById(fieldId);
    var regex=new RegExp('^(\\d{5})');
    if (regex.exec(field.value) != null) {
        anchor.href = url + field.value;
    }
    else {
        anchor.href = url;
        anchor.target = '_blank';
    }    
    return true;
}

function openExtTextWindow(openURL, WindowName, width, height, scroll) {
    var winl = (screen.width - width) / 2;
    var wint = (screen.height - height) / 2;
    var settings = 'height=' + height + ',';
    settings += 'width=' + width + ',';
    settings += 'top=' + wint + ',';
    settings += 'left=' + winl + ',';
    settings += 'scrollbars=' + scroll + ',';
    settings += 'resizable=yes,toolbar=no,location=no,status=no,menubar=no,resizable=yes';
    var newwindow = window.open(openURL, WindowName, settings);
    newwindow.focus();
    return newwindow;
}

function searchFormClick(searchFieldName) {
    var searchField = document.getElementById(searchFieldName.value);
    if (searchField.value == '' || searchField.value == searchField.defaultValue) {        
        alert("Please enter a search term.");
        return false;
    }
    else {
        return true;
    }
}
