// JScript File

var DevCount1;
var DevCount2;
var DevCount3;

function OnLoad()
{
	if(deviceCount==0)
	{
	    document.getElementById("SelectedDevice").value = "DeviceNotInList";
	}
	else
	{
	    document.getElementById("SelectedDevice").value = IPAddress[0] + "-" + HostName[0] + "-" + MACAddress[0];
	}
	    
	document.getElementById("ButtonNext").focus();
}

function OnSelectionChange()
{
   if((deviceCount != 0)&&(document.getElementById("SortOrderSelection").value != "Default"))
   {
       if(document.getElementById("SortOrderSelection").value == "IPAddress")
       {
            var TempIPAddress = IPAddress.slice();
            var TempHostName = new Array(deviceCount);
            var TempMACAddress = new Array(deviceCount);
            var TempModelName = new Array(deviceCount);
            
            TempIPAddress.sort(SortIP);
            for(DevCount1=0; DevCount1<deviceCount; DevCount1++)
            {
                for(DevCount2=0; DevCount2<deviceCount; DevCount2++)
                {
                    if(TempIPAddress[DevCount1] == IPAddress[DevCount2])
                    {
                        TempHostName[DevCount1] = HostName[DevCount2];
                        TempMACAddress[DevCount1] = MACAddress[DevCount2];
                        TempModelName[DevCount1] = ModelName[DevCount2];
                        break;
                    }
                }
            }        
        }
       else if(document.getElementById("SortOrderSelection").value == "HostName")
       {
            var TempHostName = HostName.slice();
            var TempIPAddress = new Array(deviceCount);
            var TempMACAddress = new Array(deviceCount);
            var TempModelName = new Array(deviceCount);
            
            TempHostName.sort(SortHostName);
            for(DevCount1=0; DevCount1<deviceCount; DevCount1++)
            {
                for(DevCount2=0; DevCount2<deviceCount; DevCount2++)
                {
                    if(TempHostName[DevCount1] == HostName[DevCount2])
                    {
                        TempIPAddress[DevCount1] = IPAddress[DevCount2];
                        TempMACAddress[DevCount1] = MACAddress[DevCount2];
                        TempModelName[DevCount1] = ModelName[DevCount2];
                        break;
                    }
                }
            }
        }
       else if(document.getElementById("SortOrderSelection").value == "MACAddress")
       {
            var TempMACAddress = MACAddress.slice();
            var TempIPAddress = new Array(deviceCount);
            var TempHostName = new Array(deviceCount);
            var TempModelName = new Array(deviceCount);
      
            TempMACAddress.sort();
            for(DevCount1=0; DevCount1<deviceCount; DevCount1++)
            {
                for(DevCount2=0; DevCount2<deviceCount; DevCount2++)
                {
                    if(TempMACAddress[DevCount1] == MACAddress[DevCount2])
                    {
                        TempHostName[DevCount1] = HostName[DevCount2];
                        TempIPAddress[DevCount1] = IPAddress[DevCount2];
                        TempModelName[DevCount1] = ModelName[DevCount2];
                        break;
                    }
                }
            }
       }
       ModelName = TempModelName;
       IPAddress = TempIPAddress;
       HostName = TempHostName;
       MACAddress = TempMACAddress;
   }
   else if(deviceCount==0)
   {
       document.getElementById("SelectedDevice").value = "DeviceNotInList";
       document.getElementById("ButtonNext").focus();
       return; 
   }   
   document.getElementById('InFrame').contentWindow.document.getElementById('DeviceList').innerHTML=DevListLoad();
   document.getElementById("SelectedDevice").value = IPAddress[0] + "-" + HostName[0] + "-" + MACAddress[0];
   
   document.getElementById("ButtonNext").focus();

}


function DevListLoad()
{
 var NetworkDeviceList="";
 var DevCount3;
 var CheckStatus = false;
				
 for(DevCount3=0; DevCount3<deviceCount; DevCount3++)
 {
    NetworkDeviceList += "<div";
    if(!CheckStatus)
    {
    	    NetworkDeviceList += " style=\"background-color:#9cf;\"";
    }
   	NetworkDeviceList += "><div class=\"Network_0\"><input type=\"radio\" name=\"Network_Device\"";
    if(!CheckStatus)
    {
        NetworkDeviceList += " checked=\"checked\"";
        CheckStatus = true;
    }
    NetworkDeviceList += " onclick=\"OnRadioClick(event)\" id=\"" + IPAddress[DevCount3] + "\">";
    if(IsDefaultSort())
        NetworkDeviceList += "<b>";
    NetworkDeviceList += "<label>" + ModelName[DevCount3] + "</label>";
    if(IsDefaultSort())
        NetworkDeviceList += "</b>";
    NetworkDeviceList += "</div>";
    
    NetworkDeviceList += "<div class=\"Network_1\">";
    if(IsIPAddressSort())
        NetworkDeviceList += "<b>";
    NetworkDeviceList += "IP Address: " + IPAddress[DevCount3];
    if(IsIPAddressSort())
        NetworkDeviceList += "</b>";
    NetworkDeviceList += "</div>";
    
    NetworkDeviceList += "<div class=\"Network_1\">"; 
    if(IsMACAddressSort())
        NetworkDeviceList += "<b>";
    NetworkDeviceList += "Hardware Address: " + MACAddress[DevCount3];
    if(IsMACAddressSort())
        NetworkDeviceList += "</b>";
    NetworkDeviceList += "</div>";
    
    NetworkDeviceList += "<div class=\"Network_1\">";
    if(IsHostNameSort())
        NetworkDeviceList += "<b>";
    NetworkDeviceList += "Host Name: " + HostName[DevCount3];
    if(IsHostNameSort())
        NetworkDeviceList += "</b>";
    NetworkDeviceList += "</div></div>";
 }   
    
 if(deviceCount == 0)
 {   
    NetworkDeviceList += "<div><div class=\"Network_0\"><input type=\"radio\" name=\"Network_Device\" checked=\"checked\"";
 }
 else
 {
    NetworkDeviceList += "<div><div class=\"Network_0\"><input type=\"radio\" name=\"Network_Device\"";
 }
 
 NetworkDeviceList += "onclick=\"OnRadioClick(event)\" id=\"DeviceNotInList\"><b><label>" + DeviceNotFoundText + "</label></b></div></div>";   
 return NetworkDeviceList;
}

function IsDefaultSort()
{
    return (document.getElementById("SortOrderSelection").value == "Default");
}

function IsIPAddressSort()
{
    return (document.getElementById("SortOrderSelection").value == "IPAddress");
}

function IsHostNameSort()
{
    return (document.getElementById("SortOrderSelection").value == "HostName");
}

function IsMACAddressSort()
{
    return (document.getElementById("SortOrderSelection").value == "MACAddress");
}

function SortIP(x,y)
{
   var TempX = new String(x);
   var TempY = new String(y);
   
   TempX = TempX.split(".");
   TempY = TempY.split("."); 
   
   if(TempX[0]!=TempY[0])
   {
        return TempX[0]-TempY[0];
   }
   else if(TempX[1]!=TempY[1])
   {
        return TempX[1]-TempY[1];
   } 
   else if(TempX[2]!=TempY[2])
   {
        return TempX[2]-TempY[2];
   } 
   else if(TempX[3]!=TempY[3])
   {
        return TempX[3]-TempY[3];
   } 
   else
   {
        return x-y;
   } 
}

function SortHostName(x,y)
{
   var TempX = x.toLowerCase();
   var TempY = y.toLowerCase();
   
   if(TempX > TempY)
    return 1;

   if(TempX < TempY)
    return -1;

   return 0;
}



