//$Id: SPMSwitchResult.js,v 1.5 2009/02/24 07:06:56 karuppannan Exp $
function showHideSwitchInfo(id)
{
        if (id==1)
        {
		document.getElementById("switchInfoTable").style.display="none"
                document.getElementById("portInfoTable").style.display="block"
                document.hostForm.selectedTab.value="portInfo";
        }else if (id==2)
        {
                document.getElementById("switchInfoTable").style.display="block"
                document.getElementById("portInfoTable").style.display="none"
                document.hostForm.selectedTab.value="switchInfo";
        }
}
function checkSPMSelection(menuItemName,refId,additionalParams,index)
{
  var tblModel = getTableModel(refId);
  var selectRow=tblModel.getSelectedRowIndices(refId);
  var title = "Block Switch Port";
  if(menuItemName=="enablePort") {
        title = "Unblock Switch Port";
  } else if(menuItemName=="modifyIfAliasName") {
        title = "Modify ifAlias Name";
  } 
  if(selectRow.length<=0)
  {
    if(menuItemName=="enablePort")
    {
        alert("Select port(s) to unblock");
    }
    else  if(menuItemName=="BlockPort")
    {
        alert("Select port(s) to block");
    }else if(menuItemName=="modifyIfAliasName")
    {
        alert("Select  port(s) to modify Alias Name");
    }
    return false;
  }
  var menuItemObj = getMenuItemObj(menuItemName);
  var actUrl = menuItemObj.getActionURL(refId,additionalParams,index);
  var selectedSwitch = document.hostForm1.selectedSwitch.value;
  
  actUrl = actUrl+"&selectedSwitch="+selectedSwitch;
 // menuItemObj.invokeActionURL(actUrl,refId);
  showURLInDialog(actUrl,'position=auto,modal=yes,width=580,scrollbars=no,title='+title);
    
}
function openMultipleMACWindow(linkParams)
{
	if (navigator.appName == 'Netscape')
	{
		newwin=window.open("/network/MultipleMAC.jsp?"+ linkParams,"MultipleMAC","screenX=300,screenY=250,scrollbars=yes,toolbar=no,width=800,height=450","centre");
	}
	else if(navigator.appName == 'Microsoft Internet Explorer')
	{
		newwin=window.open("/network/MultipleMAC.jsp?"+ linkParams,"MultipleMAC","scrollbars=yes,toolbar=no,width=800,height=450","centre");
	}
}
function setAction(f , action)
{
	f.actionFrom.value=action;
	if(action == 'refresh')
	{
		update( f , f.selectedSwitch.value, action);
	}
}

function checkSwitchStatus()
{
        window.clearInterval(intervalStatusID);
        //alert(document.getElementById("status").value);
        var currentTab = document.hostForm.selectedSwitchResult.value;
        if(currentTab  != "switchAlerts" &&  currentTab  != "switchResultHistory")
        {
            if(document.hostForm1.status.value == "1")
            {
                    document.getElementById("statusImg").src = "/images/loading_img.gif";
                    document.getElementById("statusImg").width = "15";
                    document.getElementById("statusImg").height = "15";
                    document.getElementById("refreshMessage").innerHTML="Scanning. Please wait..."
                    document.getElementById("status").value = "1";	

                    intervalID = window.setInterval('getSwitchStatus()',10000);
            }
         }
}

function setActionValue(f, actionFrom)
{
        f.actionFrom.value = actionFrom;
	f.selectedSwitch.value = f.selectedSwitch1.value;
	f.submit();
}
/*function selectSwitch(f, selectSwitchID)
{
    document.hostForm.actionFrom.value = "viewSwitch";
    var oldSelectSwitchid = document.hostForm.selectedSwitch.value;
     alert(document.hostForm.RESOURCEID.value);
    //if(oldSelectSwitchid != selectSwitchID)
    {
        document.hostForm.selectedSwitch.value = selectSwitchID;
        document.hostForm.submit();
    }
}
*/
var intervalStatusID;
function selectSwitch(selectSwitchID , selectSwitchName)
{
    document.hostForm.actionFrom.value = "viewSwitch";
    document.hostForm.selectedSwitch.value = selectSwitchID;  
    document.hostForm.switchName.value = selectSwitchName;
    refreshCurrentSPMViewName();
    checkSPMResultStatus();
 }
 function checkSPMResultStatus()
 {
    //alert("##############")
    window.clearInterval(intervalStatusID);
    intervalStatusID = window.setInterval('checkSwitchStatus()',1000);  
}
 function refreshCurrentSPMViewName()
 {
    var selectSwitchID = document.hostForm.selectedSwitch.value;  
    var selectSwitchName = document.hostForm.switchName.value; 
    var currentTab = document.hostForm.selectedSwitchResult.value;
    
    var currentResultView = "/SPMSwitchResultView.cc";
    if(currentTab  == "switchAlerts")
        {
            currentResultView = "SPMSwitchAlertTableView.cc";
            
        }else if(currentTab  == "switchResultHistory")
        {
            currentResultView = "SPMHistoryView.cc";
        }
    var search = document.hostForm.search.value;
    var switchName = document.hostForm.switchName.value;
    
    document.getElementById("switchNameId").innerHTML=selectSwitchName;
    collapseMenus("switchDiv");
    var url = currentResultView+"?&switchDir="+switchName+"&switchName="+switchName+"&switchIP="+selectSwitchID+"&selectedSwitch="+selectSwitchID+"&search="+search+"&isTab=true";
    //alert(url);
    showSPMResult(currentTab , selectSwitchID )
    
   // renderViewCurrentView( url , "switchResultID");
    
}


var req;
function update(f , switchName, actionFrom)
{
        
        document.getElementById("statusImg").src = "/images/loading_img.gif";
	document.getElementById("statusImg").width = "15";
	document.getElementById("statusImg").height = "15"; 
	document.getElementById("refreshMessage").innerHTML="Scanning. Please wait..."
	document.getElementById("status").value = "1";	
        f.refresh.disabled=true;
        f.refresh.className= "ButtonRescanDisabled";
                
        var url = '/SwitchPortMapper.do?actionFrom='+actionFrom+'&selectedSwitch='+switchName;
	
        if (window.XMLHttpRequest)
        {
                req = new XMLHttpRequest();
                req.onreadystatechange = processReqChangeSPM;
                req.open("GET", url, true);
                req.send(null);
        }

        else if (window.ActiveXObject)
        {
                isIE = true;
                req = new ActiveXObject("Microsoft.XMLHTTP");
                if (req)
                {
                        req.onreadystatechange = processReqChangeSPM;
                        req.open("GET", url, true);
                        req.send();
                }
        }

}
var intervalID = "";
function processReqChangeSPM()
{
	if (req.readyState == 4)
	{
		if (req.status == 200)
		{
			intervalID = window.setInterval('getSwitchStatus()',10000);

		}
		else
		{

			alert("There was a problem");
		}
	}
}
function getSwitchStatus() 
{
       // alert(document.getElementById("status").value)
       
        var currentTab = document.hostForm.selectedSwitchResult.value;
        
	if(document.getElementById("status").value == "1")
	{
            if(currentTab  != "switchAlerts" &&  currentTab  != "switchResultHistory")
            {
                document.hostForm1.refresh.disabled=true;
                document.hostForm1.refresh.className= "ButtonRescanDisabled";
                updateStatus(document.hostForm1.selectedSwitch.value, "switchStatus");
            }
           
	}
	else
	{
             if(currentTab  != "switchAlerts" &&  currentTab  != "switchResultHistory")
             {
                document.hostForm1.refresh.disabled=false;
                document.hostForm1.refresh.className= "ButtonRescan";
             }
             window.clearInterval(intervalID);
	}	
}
function updateStatus(switchName, actionFrom)
{
	var req1;
	var url = '/SwitchPortMapper.do?actionFrom='+actionFrom+'&selectedSwitch='+switchName;
	if (window.XMLHttpRequest)
	{
		req1 = new XMLHttpRequest();
		req1.onreadystatechange = function(){
			if (req1.readyState == 4)
			{
				if (req1.status == 200)
				{
					var x = req1.responseXML.getElementsByTagName('switchInfo');
					if(x[0].getAttribute("status") != 1)
					{
						document.hostForm1.status.value = x[0].getAttribute("status");
						document.getElementById("statusImg").src = "/images/spacer.gif";
                                                document.getElementById("refreshMessage").innerHTML=""
                                                document.hostForm1.refresh.disabled=false;
                                                document.hostForm1.refresh.className= "ButtonRescan";
						document.hostForm1.actionFrom.value="viewSwitch";
                                                refreshCurrentSPMViewName();
						//document.hostForm.submit();
					}

				}
				else
				{

					alert("There was a problem");
				}
			}
		};
		req1.open("GET", url, true);
		req1.send(null);
	}

	else if (window.ActiveXObject)
	{
		isIE = true;
		req1 = new ActiveXObject("Microsoft.XMLHTTP");
		if (req1)
		{
			req1.onreadystatechange =  function(){
				if (req1.readyState == 4)
				{
					if (req1.status == 200)
					{
						var x = req1.responseXML.getElementsByTagName('switchInfo');

						if((x[0].getAttribute("status")) != 1)
						{
							document.getElementById("statusImg").src = "";
							document.hostForm.actionFrom.value="viewSwitch";
							document.hostForm.submit();
						}
					}
					else
					{

						alert("There was a problem");
					}
				}
			};
			req1.open("GET", url, true);
			req1.send();

		}

	}
}
function validateSPMSwitch()
{

    var button = trim(document.hostForm.actionFrom.value)
    if(button == "cancel")
    {
        showSwitchSettings();
        return false;
        
    }else if(button == "locationMap")
    {
        var path = trim(document.hostForm.csvBrowser.value)
        if(path.length == 0)
        {
               alert("Select a CSV file ");
               return false;
        }else
        {
            return true;
        }
        
    }else if(button == "blockPort" || button == "unblockPort")
    {
        
        validatePortSelection(button);
        return false;
    }
    var settingVal = document.hostForm.actionFromTemp.value 
    if( settingVal == 'cancel' || settingVal == "getAlertStatus" || settingVal == "saveDays" )
    {
        return false;
    }
    return true;
}
function setPortSelection(f , actionFrom)
{
    collapseMenus("actionDiv");
    document.hostForm.actionFrom.value=actionFrom;
    var button = trim(document.hostForm.actionFrom.value)
    validatePortSelection(button);
    
}
var selectedArgs;
function validatePortSelection(button)
{
        var alertMessage = "Please Select a port to  \"Unblock Port\"" ;
        if(button == "blockPort" )
        {
            alertMessage = "Please Select a port to  \"Block Port\"" ;
        }else if(button == "modifyIfAlias")
        {
            alertMessage = "Please Select a port to  \"Modify ifAlias Name\"" ;
        }       
        
        if(checkCheckBoxSelection("update" , alertMessage ))
        {
            var selctedList =  document.hostForm.checked.value;
            var selectedIfName = document.hostForm.ifNameList.value;
            var selectedSwitch = document.hostForm.selectedSwitch.value;
            var ifDescriptionList =  document.hostForm.ifDescriptionList.value;
            var ifAliasList =  document.hostForm.ifAliasList.value;
            var switchName = document.hostForm.switchName.value;
            var url = "/network/SPMBlockPort.jsp?actionFrom="+button+"&selctedList="+selctedList+"&selectedSwitch="+selectedSwitch+"&switchName="+switchName+"&selectedIfName="+selectedIfName+"&ifDescriptionList="+ifDescriptionList+"&ifAliasList="+ifAliasList;
      
            if(trim(navigator.appName) == "Microsoft Internet Explorer")
            {
                    window.open(url);
            }else
            {
                 window.open(url,"Switch Port Mapper","resizable=yes,screenX=100,screenY=100,scrollbars=yes,toolbar=no,menubar=no,width=600,height=400","centre")
            }
                     
        }
}

function showSwitchSettings() {

   var switchSettingElem = document.getElementById('csvTableID');
   if(switchSettingElem.style.visibility =="hidden") {
        collapseMenus("actionDiv");
       switchSettingElem.style.visibility="visible";
       
   } else {
       switchSettingElem.style.visibility="hidden";
   }
   
}
var selectedIfNameArgs;
var ifDescriptionArgs;
var ifAliasArgs;
function checkCheckBoxSelection(value , msg )
{
       
	var status = false
        selectedArgs = "";
        selectedIfNameArgs = "";
        ifDescriptionArgs="";
        ifAliasArgs="";
       
		/*if(document.hostForm.selectAll.value == "all" & document.hostForm.selectAll.checked) {
			status = true
		}
                */

		var x = document.getElementsByName("select")
		for(var i=0; i< x.length; i++)
		{
			if(x[i].checked)
			{
                                var ifIndex = x[i].value;
                                var ifName = document.getElementById(ifIndex+"ifName").value;
                                var ifDescription = document.getElementById(ifIndex+"ifDescription").value;
                                var ifAlias = document.getElementById(ifIndex+"ifAlias").value;
                                
                                
				if(selectedArgs.length > 0)
				{
                                    selectedArgs = selectedArgs+","+ifIndex
                                    selectedIfNameArgs = selectedIfNameArgs+","+ifName;
                                    ifDescriptionArgs = ifDescriptionArgs+","+ifDescription;
                                    ifAliasArgs = ifAliasArgs+","+ifAlias;
                                 }else
                                 {
                                    selectedArgs = ifIndex;
                                    selectedIfNameArgs = ifName;
                                    ifDescriptionArgs = ifDescription;
                                    ifAliasArgs = ifAlias;
                                 }
                                 
					status = true
			}
		}
          
	if(!status)
	{
                        alert(msg);
			return false
	}
       
        document.hostForm.checked.value =selectedArgs;

        document.hostForm.ifNameList.value =selectedIfNameArgs;

        document.hostForm.ifDescriptionList.value =ifDescriptionArgs;

        document.hostForm.ifAliasList.value =ifAliasArgs;
  
        return true

}

function showCSVInfo()
{
	newwin=window.open("/help/CSVFormat.txt");

}

var selectGroupDetailsObj;


function selectGroupName(id)
{  
  var oldGroupID = document.hostForm.selGroupID.value;
  if(oldGroupID != id)
  {
        var url = '/SPMCsvModify.do?actionFrom=getGroupDetails&selectedGroupID='+id;
        collapseMenus("groupDiv");
        sendRequestForAjaxUpdate(url,selectGroupNameResponse)
       
     
  }
  
}

function selectGroupNameResponse()
{
    if (groupConfigReq.readyState == 4) 
	{
		if(groupConfigReq.status == 200) 
                {
                    //alert(groupConfigReq.responseText);
                    var gIDObj = groupConfigReq.responseXML.getElementsByTagName('groupID');
                    var gId = gIDObj[0].getAttribute("selectedGroupID");
                    document.hostForm.selGroupID.value =gId;
                    var gNameObj = groupConfigReq.responseXML.getElementsByTagName('groupName');
                    var gName = gNameObj[0].getAttribute("selectedGroupName");
                    document.getElementById("gNameId").innerHTML=gName;
                    document.getElementById("switchNameId").innerHTML="Select Switch";
                    var x = groupConfigReq.responseXML.getElementsByTagName('allSwitchName');
                    if(x.length > 14)
                    {
                        document.getElementById("switchDiv$Menus").style.overflow ="auto";
                        document.getElementById("switchDiv$Menus").style.height ="290px";
                    }else
                    {
                        document.getElementById("switchDiv$Menus").style.overflow ="visible";
                        document.getElementById("switchDiv$Menus").style.height ="";
                    }
                    var switchMenus = "<table class=menu>"
                    if(x.length == 0)
                    {
                       switchMenus = switchMenus+" <tr><td >No Switch is available</td></tr>"; 
                    }else
                    {
                        for(var i=0 ; i< x.length ; i++)
                        {
                                    var switchIDValue = x[i].getAttribute("switchID");
                                    var switchNameValue= x[i].getAttribute("switchName");
                                   // alert(switchNameValue)
                                   
                                    switchMenus = switchMenus+"<tr><td ><a href='javascript:selectSwitch(\""+switchIDValue+"\" , \""+switchNameValue+"\")' class='menuitem'>"+switchNameValue+"</a></td></tr>";
                        }
                    }
                    switchMenus=switchMenus+"</tabe>"    
                    document.getElementById("switchMenu").innerHTML = switchMenus;
                }       
	} 
}

function sendRequestForAjaxUpdate1(url,onChangeMethod)
{
        isBlockedAction = true;

        if (window.XMLHttpRequest)
        {
                groupConfigReq = new XMLHttpRequest();
                groupConfigReq.onreadystatechange = onChangeMethod;
                groupConfigReq.open("GET", url, true);
                groupConfigReq.send(null);
        }

        else if (window.ActiveXObject)
        {
                isIE = true;
                groupConfigReq = new ActiveXObject("Microsoft.XMLHTTP");
                if (groupConfigReq)
                {
                        groupConfigReq.onreadystatechange = onChangeMethod;
                        groupConfigReq.open("GET", url, true);
                        groupConfigReq.send();
                }
        }
}
function closeSwitchMsg(actionFrom , switchID , messgType , user )
{    
    document.getElementById('trailMsg').style.display='none';
    var url='/SwitchPortMapper.do?actionFrom='+actionFrom+'&switchID='+switchID+'&messgType='+messgType+'&userName='+user+'&uid='+new Date().getTime();    
    sendUrl(url);
}

function sendUrl(url)
{

var req = null;
if(window.XMLHttpRequest)
{
 req = new XMLHttpRequest();
}else if (window.ActiveXObject)
{
       isIE=true;
       req  = new ActiveXObject("Microsoft.XMLHTTP");
        
        
}
req.onreadystatechange = function()
{
    if(req.readyState == 4)
    {
        if(req.status == 200)
        {
	    var text=req.responseText;	
	}
        else
        {
            alert("Error");
        }
   }
};
req.open("POST",url, true);
req.send(null);
}
function openHistoryFile( selectedSwitch ,file , rootDir , switchName ,groupName)
{
    
    window.open("/network/SPMHistory.jsp?groupName="+groupName+"&filePath="+rootDir+"&switchName="+switchName+"&selectedSwitch="+selectedSwitch+"&file="+file ,"UpdateManager","resizable=yes,screenX=100,screenY=100,scrollbars=yes,toolbar=no,menubar=yes,width=1200,height=800","centre")
    
}

function downLoadFile( file , switchName ,groupName)
{
    var url ='/SwitchPortMapper.do?actionFrom=downLoadCSV&fileName='+file+'&groupName='+groupName+'&switchName='+switchName;
    window.open(url,"htmlExportForm","menubar=1,scrollbars=1,status=1,toolbar=1,resizable","center");    
}
function BlockOrUnBlockPortWhenSearch(menuItemName,refId,additionalParams,index) {
  var tblModel = getTableModel(refId);
  var selectRow=tblModel.getSelectedRowIndices(refId);
  var title = "Block Switch Port";
  if(menuItemName=="enablePort") {
        title = "Unblock Switch Port";
  } else if(menuItemName=="modifyIfAliasName") {
        title = "Modify ifAlias Name";
  } 
  
  if(selectRow.length<=0) {
    if(menuItemName=="UnBlockInterfaces") {
        alert("Select port(s) to unblock");
    } else  if(menuItemName=="BlockInterfaces") {
        alert("Select port(s) to block");
    }
    return false;
  }
  var menuItemObj = getMenuItemObj(menuItemName);
  var actUrl = menuItemObj.getActionURL(refId,additionalParams,index);
  var selectedSwitch = "2"; //"document.hostForm1.selectedSwitch.value;
  actUrl = actUrl+"&selectedSwitch="+selectedSwitch+"&fromSearch=fromSearch";
  //menuItemObj.invokeActionURL(actUrl,refId);
  showURLInDialog(actUrl,'position=auto,modal=yes,width=580,scrollbars=no,title='+title);
   
}
function ModifyIfAliasName(menuItemName,refId,additionalParams,index) {
    var menuItemObj = getMenuItemObj(menuItemName);
    var tblModel = getTableModel(refId);

    var colInd1  = tblModel.getColumnIndex("SwitchPort.RESOURCEID");
    var colInd2  = tblModel.getColumnIndex("SwitchPort.IFINDEX");
    var colInd3  = tblModel.getColumnIndex("SwitchPort.IFALIASNAME");
    var colInd4  = tblModel.getColumnIndex("SPMInputConfig.SWITCHNAME");
    
    var resourceID = tblModel.getValueAt(index,colInd1);
    var ifIndex = tblModel.getValueAt(index,colInd2);
    var ifAliasName = tblModel.getValueAt(index,colInd3);
    var switchName = tblModel.getValueAt(index,colInd4);
    
    showURLInDialog('/network/ModifyIfAliasName_SPM.jsp?resourceID='+resourceID+'&ifIndex='+ifIndex+'&ifAliasName='+ifAliasName+'&switchName='+switchName,'position=relative,modal=no,scrollbars=no,title=Modify ifAliasName');
    
}
function ModifyPhysicalLocation(menuItemName,refId,additionalParams,index) {
    var menuItemObj = getMenuItemObj(menuItemName);
    var tblModel = getTableModel(refId);

    var colInd1  = tblModel.getColumnIndex("SwitchPort.RESOURCEID");
    var colInd2  = tblModel.getColumnIndex("SwitchPort.IFINDEX");
    var colInd3  = tblModel.getColumnIndex("SwitchPort.LOCATION");
    
    var resourceID = tblModel.getValueAt(index,colInd1);
    var ifIndex = tblModel.getValueAt(index,colInd2);
    var location = tblModel.getValueAt(index,colInd3);
    
    showURLInDialog('/network/ModifyPhysicalLocation_SPM.jsp?resourceID='+resourceID+'&ifIndex='+ifIndex+'&location='+location,'position=relative,modal=no,scrollbars=no,title=Modify Physical Location');
    
}
function refreshSPMView(id,useOldState)
{
    ViewAPI.refreshView({VIEWTOREFRESH_RN:id,USEOLDSTATE_RN:(useOldState == false),NAVIGABLE_RN:true});
    if(useOldState == true) {
        closeDialog();
    }
}