this.headEnds = new headEnds();
var pagename = "";
var zipcode = "";
var contentdiv = "content-headends";
function headEnds() {
    this.requesturl = restfulServicePath + "getheadends.aspx";
    this.zipcodeParam = "zip";
    this.typeParam = "type";
    
    this.findReelzChannelHeadends = function(zip, type, title) {
        if (zip.length == 5)
        {
            contentdiv = 'content-headends';
	        this.find(zip, type, title);
	    }
    }
    
    this.findReelzChannelHeadends1 = function(zip, type, title, resultdiv) {
        if (zip.length == 5)
        {
            contentdiv = resultdiv;
	        this.find(zip, type, title);
	    }
    }

    this.find = function(zip, type, title) {
	    this.ajax = new sack();            
        this.setZipCodeCookie(zip); 
        this.ajax.setVar( this.zipcodeParam, zip); 
        this.ajax.setVar( this.typeParam, type); 
        this.ajax.setVar( "format", "html");
        this.ajax.requestFile = this.requesturl;
        this.ajax.method = "GET"; 
        this.ajax.element = contentdiv;
        
        contentObj = document.getElementById(contentdiv);
        if (contentObj) { contentObj.style.display = "block";}
        if (contentObj.parentNode) { contentObj.parentNode.style.display = "block";}
        obj = document.getElementById("allcities_reelzchannel");
        if (obj) { obj.style.display = "block";}           
        
        document.getElementById(contentdiv).innerHTML = "";	   
        zipcode = zip;
        pagename  = title;
        this.ajax.onCompletion = this.whenCompleted;
        this.ajax.runAJAX();
    }


    this.whenCompleted = function() {
        if (document.getElementById(contentdiv ).innerHTML.length > 0)
        {
            if (document.getElementById(contentdiv ).innerHTML.toLowerCase() != 'please enter a valid zip code.')
            {
                // If the member is logged in and the User's zipcode hasn't been changed
                // then skip the google analytics event.
                var previousZipcode = getCookie("__PREV_ZIP");
                var zipcodeChanged = false;
                if (previousZipcode != null)
                {
                    if (previousZipcode != zipcode)
                    {
                        zipcodeChanged = true;
                    }
                }
                else
                {
                    zipcodeChanged = true;
                }
                if (getCookie("__REELZ_ACCOUNT_ID") != null || zipcodeChanged )
                {
                    if (pagename.length <= 0)
	                    pagename = 'home';
	                if (!pageTracker) var pageTracker = _gat._getTracker(_uacct);
	                pageTracker._trackPageview(); // seems wrong to have this, but is necessary avoid 'y is undefined error'
                    pageTracker._trackEvent ('ZIP Code Lookups', pagename, zipcode);
                }
            }
       }
    }   

    this.keyDown = function(e, zip, type, pagename) {
         if (e.keyCode && e.keyCode == 13) { this.findReelzChannelHeadends(zip, type, pagename) ; }
    }
    
    this.displayAll = function() {
        obj = document.getElementById("allcities");
        if (obj) { obj.style.display = "block"; obj.focus();}
    }
    this.setZipCodeCookie = function (zipcode)
    {
        var tmpCookie = getCookie("__TEMP_ZIP");
        if (tmpCookie != null)
        {
            setCookie("__PREV_ZIP", tmpCookie);
        }        
        setCookie("__TEMP_ZIP", zipcode); 
    }
    
    this.getZipCode = function ()
    {
        var zipcode = "";
        var cookie = getCookie("__TEMP_ZIP");
        if (cookie == null) { 
            cookie = getCookie("__REELZ_PHDZ"); 
            if (cookie != null)
            {
                 var values = cookie.split('|');
                 if (values != null && values.length >=4)
                    zipcode = values[3];
            }
        } else zipcode = cookie;
        
        return zipcode;
    }
}
