function $(elename)
{
	return document.getElementById(elename);
}
	
var justCheckAll=false;

function toggleAll(checked)
{    
    //note: check into the All checkbox will always return all selected
    if(checked!=true)
        justCheckAll=false;
        
    for(i=0; i< maxcarrier; i++)
    {
        carrierDisplay[i]=true;
        if(typeof(CWcarrierDisplay)!='undefined')
        {
            CWcarrierDisplay[i]=true;     
            $('ctl00_CW_CarriersCheckboxes_'+i).checked=true;           
        }
    }    
    toggleOverlay(-1,true);
    
    if(justCheckAll==false && checked==true)
        justCheckAll=true;    
}
	
function toggleOverlay(carrier, show)
{   
    var ind;
    if(justCheckAll==true)
    {
        for(ind=0; ind< maxcarrier; ind++)
        {
            carrierDisplay[ind]=false; 
            if(typeof(CWcarrierDisplay)!='undefined')
            {
                CWcarrierDisplay[ind]=false;     
                $('ctl00_CW_CarriersCheckboxes_'+ind).checked=false;                               
            }
        }
        
        justCheckAll=false;
        
        toggleOverlay(carrier,show);
        
        return;
    }
    
    //alert('toggle overlay');
    //determine the index of this carrier  
    index=-1;
    for(ind=0; ind< maxcarrier; ind++)
    {
        if (carrierID[ind]==carrier)
        {
            index=ind;
            break;
        }
    }
    
    //toggle this carrier's displayability
    if(index!=-1)
    {
        carrierDisplay[index]=show;            
        if(typeof(CWcarrierDisplay)!='undefined')
        {
            CWcarrierDisplay[ind]=show;     
            $('ctl00_CW_CarriersCheckboxes_'+index).checked=show;
        }
    }
    
    //reset query strings
    carriers="";
    colors="";
    
    
    
    //make up query strings
    for(ind=0; ind < maxcarrier; ind++)
    {  
        if(carrierDisplay[ind]==true)
        {
            carriers += carrierID[ind] + "_";
            colors += color[ind] + "_";
        }
    }   
    
    //mapMoveend();   //call the mapMoveend function just to update the URL textbox
        
    //create and replace overlay        
    bounds = new GLatLngBounds( new GLatLng(-90, -180), new GLatLng(90, 180));
    copyright = new GCopyright( 'MaxMindLLC', bounds, 0, "(c) 2008 MaxMindLLC" + "<http://www.example.org/>");
    copyrights = new GCopyrightCollection();
    copyrights.addCopyright(copyright);            
    heatmap = new GTileLayer(copyrights, 10, 0);
    heatmap.getTileUrl = function (tile, zoom) 
        {                                           
        url = 'GetHeatMap.aspx?';             
        startpixx = tile.x * 256 - 90; 
        endpixx = tile.x * 256 + 256 + 90;            
        startpixy = tile.y * 256 - 90; 
        endpixy = tile.y * 256 + 256 + 90;                    
                    
        currentProjection = G_NORMAL_MAP.getProjection();                
        gp1=currentProjection.fromPixelToLatLng(new GPoint(startpixx, startpixy),zoom);
        gp2=currentProjection.fromPixelToLatLng(new GPoint(endpixx, endpixy),zoom);                             
        		    
        url += "frLat=" + gp1.lat(); 
        url += "&toLat=" +  gp2.lat();       
        url += "&frLong=" +  gp1.lng();
        url += "&toLong=" + gp2.lng();
        url += "&imgWidth=" + 256;
        url += "&imgHeight=" + 256;
        url += "&colorScheme=" + colors;
        url += "&carrier=" + carriers;
        url += "&zoom=" + zoom;
        url += "&cacheInfo=" + zoom + "-" + tile.x + "-" + tile.y + "-" + carriers + "-" + colors;        
        return url;			
        }
    heatmap.isPng = function () {return true;}
    heatmap.getOpacity = function () {return 1.0;}              
    if(MHMoverlay)
        map.removeOverlay(MHMoverlay);
    MHMoverlay=new GTileLayerOverlay(heatmap);      
    map.addOverlay(MHMoverlay);	       
    updateEmbedURL();           
    //refreshComments();
}

function isCarrierChecked(cariName)
{    
    var j=0;
    for(j=0; j< maxcarrier; j++)
    {
        if (carrierName[j] == cariName) //why not trim()?
        {
            return carrierDisplay[j];
            break;            
        }
    }
    return false;
}

function isCarrierIDChecked(cariID)
{
    var j=0;
    for(j=0; j< maxcarrier; j++)
    {
        if (carrierID[j] == cariID)
        {
            return carrierDisplay[j];
            break;            
        }
    }
    return false;
}

function getCarrierName(cariID)
{
    var j=0;
    for(j=0; j< maxcarrier; j++)
    {
        if (carrierID[j] == cariID)
        {
            return carrierName[j];
            break;            
        }
    }
    return "";
}
function getCarrierIndexByID(cariID)
{
    var j=0;
    for(j=0; j< maxcarrier; j++)
    {
        if (carrierID[j] == cariID)
        {
            return j;
            break;            
        }
    }
    return false;
}