//$Id: SysUpdate.js,v 1.17 2006/08/31 13:16:50 karuppannan Exp $

var selectedArgs = "";
function setActionValue(currentForm,value)
{
        currentForm.inputFrom.value=value;
	currentForm.actionFrom.value=value;
}
function enterKeySearch(val,value , ev)
{
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		if(window.event.keyCode == 13)
		{
			document.hostForm.actionFrom.value = value;
			document.hostForm.enterSearch.value = "yes";
		}
	}
	else if(navigator.appName == "Netscape")
	{
		if(ev.which == 13)
		{
			document.hostForm.actionFrom.value = value;
			document.hostForm.enterSearch.value = "yes";
		}
	}
	else
	{
		document.hostForm.actionFrom.value = value;
		document.hostForm.enterSearch.value = "yes";
	}
}
function searchDetail(f,value)
{
	f.actionFrom.value = value;
        f.inputFrom.value = value;
	f.enterSearch.value = "no";
	document.hostForm.searchString.focus();
}
function deleteDetail(value)
{
        document.hostForm.actionFrom.value = value;
}
function checkSelection(value , msg )
{
        
	var status = false
        selectedArgs = "";
		
		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)
			{
				selectedArgs=x[i].value + "," + selectedArgs
					status = true
			}
		}
           
	if(!status)
	{
                        alert(msg);
			return false
	}
	else if(value == "delete")
	{
		var confirmation = confirm("Are you sure you want to delete all the items ?"    )

			if(confirmation==true)
			{
				document.hostForm.checked.value =selectedArgs

			}
			else
			{
				return false;
			}
	}
	else if(value == "update")
	{
            var confirmation = confirm("Are you sure you want to Update all the items ?"    )

            if(confirmation==true)
            {
                    document.hostForm.checked.value =selectedArgs

            }
            else
            {
                    return false;
            }
		
	}else if(value == "stopScheduler" || value == 'startScheduler' )
	{
                
                document.hostForm.checked.value =selectedArgs
	}else if(value == 'addInterface' )
        {
                
                document.hostForm.checked.value =selectedArgs
        }
	return true

}

function buttonDetails(value)
{
	document.hostForm.actionFrom.value = value;
}


function selectAllIps()
{

	var status = document.hostForm.selectAll.checked;

	if(!eval("document.hostForm.select.length"))
	{
		document.hostForm.select.checked = status;
		ToggleStyle(document.hostForm.select, status);
	}
	else
	{
		var x = document.getElementsByName("select");
                
		for(var i=0; i<x.length; i++)
		{
			x[i].checked = status;
			ToggleStyle(x[i], status);
		}
	}

}



function ToggleStyle(e, status)
{
	var r = null;
	if (e.parentNode && e.parentNode.parentNode)
	{
		r = e.parentNode.parentNode;
	}
	else if (e.parentElement && e.parentElement.parentElement)
	{
		r = e.parentElement.parentElement;
	}
	if (r)
	{
		if (r.className == "oddRow")
		{
			r.className = "selectedRowOdd";
		}
		else if(r.className == "selectedRowOdd" & !status)
		{
			r.className = "oddRow";
		}
		else if (r.className == "evenRow")
		{
			r.className = "selectedRowEven";
		}
		else if(r.className == "selectedRowEven" & !status)
		{
			r.className = "evenRow";
		}
	}
}
function validateSysUpdate()
{

	var button = document.hostForm.actionFrom.value;

   //     alert("2222"+button);
	if(button == "scan")
	{
                 return false;
        }
        if(button == "get")
	{
                 return false;
        }
        //addInterFace
	var inputType = document.hostForm.inputType.value
        /*
		if(inputType != null && inputType == "Multiple" )
		{
			var button = document.hostForm.actionFrom.value;
			if(button == "scan")
			{
                            return true;
				if(!validate1())
				{
					return false;
				}
				else if(document.hostForm.communityList.value.length==0)
				{
					alert("Please enter the community List.")
						document.hostForm.communityList.focus();
					return false;
				}
			}

		}
		else if(inputType != null && inputType == "Single" )
		{
			var ipOrHost = document.hostForm.ipOrHost
				var button = document.hostForm.actionFrom.value;
			if(button == "get")
			{
				if(document.hostForm.ipOrHost.value.length==0)
				{
					alert("Please enter an End IP address ")
						document.hostForm.ipOrHost.focus();
					return false;
				}
				else if(document.hostForm.community.value.length==0)
				{
					alert("Please enter the community .")
						document.hostForm.community.focus();
					return false;
				}
				else if(!checkip(ipOrHost))
				{
					return false;
				}
			}

		}
               */
        if(document.hostForm.actionFrom.value == 'update')
	{
                
                return false;
                 
	}

	else if(document.hostForm.actionFrom.value=="delete" )
	{
                
		if(!checkSelection("delete" , "Please Select a host to \"Delete\""))
			return false;
		if(eval(document.hostForm.selectAll))
		{
			document.hostForm.selectAll.checked = false;
		}
	
        }else if(document.hostForm.actionFrom.value == 'search')
	{
		if(document.hostForm.searchString.value.length == 0)
		{
			alert("The search box is Empty ");
			document.hostForm.searchString.focus();
			return false;
		}
	}
	return true;

}
function checkip(IP)
{

	key = IP.value

		var parts = key.split(".");

	if(parts[0].length == 0)
	{
		alert("Please enter a valid IP address");
		IP.focus();
		return false;
	}


	if(isNumber(parts[0]))
	{
		if(!isValidIPAddress(IP.value))
		{
			alert("Please enter a valid IP address.");
			IP.focus();
			return false;
		}
	}
	else
	{
		if(!isSpecialCharacter(IP.value))
		{
			alert("Please enter a hostname without special characters.");
			IP.focus();
			return false;
		}
	}
	return true;

}

function isValidIPAddress(ipaddr)
{
	var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;

	if (re.test(ipaddr))
	{
		var parts = ipaddr.split(".");
		if (parts.length < 4)
		{
			return false;
		}
		if (parseInt(parseFloat(parts[0])) == 0)
		{
			return false;
		}
		if (parseInt(parseFloat(parts[0])) == 0)
		{
			return false;
		}

		for (var i=0; i<parts.length; i++)
		{
			if (parseInt(parseFloat(parts[i])) > 255) { return false; }
		}
		return true;
	}

	return false;
}

function isSpecialCharacter(dnsname)
{
	for (i=0; i<dnsname.length; i++)
	{
		var ipChar = dnsname.charCodeAt(i);
		if ((ipChar < 48 && ipChar != 45 && ipChar != 46) || (ipChar > 57 && ipChar < 65) || (ipChar > 90 && ipChar != 95 && ipChar < 97 ) || (ipChar > 122 && ipChar <= 125) || ipChar == 126)
		{
			return false;
		}
	}
	return true;
}





function validate1()
{

	f = document.hostForm;

	if(f.actionFrom.value == 'stopButton')
	{
		return true;
	}

	if(f.actionFrom.value == 'searchButton')
	{
		var search = trim(f.searchString.value);

		if(search == 0)
		{
			alert("Please enter a string to search.");
			f.entersearch.value = 'no';
			f.searchString.focus();
			return false;
		}
		else
		{
			return true;
		}
	}

	startIP = f.startIP.value;
	endIP = f.endIP.value;
	if(startIP.length == 0)
	{
		alert("Please enter the starting IP address.");
		f.startIP.focus();
		return false;
	}
	if(!isValidIPAddress(startIP))
	{
		alert("Please enter a valid starting IP address.");
		f.startIP.focus();
		return false;
	}

	if(endIP.length == 0)
	{
		alert("Please enter the ending IP address.");
		f.endIP.focus();
		return false;
	}

	if(!isValidIPAddress(endIP))
	{
		alert("Please enter a valid ending IP address.");
		f.endIP.focus();
		return false;
	}

	if(!isValidSweep(startIP,endIP))
	{
		alert("Maximum range that can be given is 1000 nodes.");
		f.endIP.focus();
		return false;
	}

	if(!isFirstIsGreaterThanSecond(endIP , startIP))
	{
		alert("Ending IP address should be greater than starting IP address.");
		f.endIP.focus();
		return false;
	}
	if(!isValidCount(startIP,endIP))
	{
		alert("Maximum range that can be given is 1000 nodes.");
		f.endIP.focus();
		return false;
	}

	return true;
}

function isValidSweep(check1,check2)
{
	var compare1 = check1.split(".");
	var compare2 = check2.split(".");

	if(!(parseInt(compare1[0]) == parseInt(compare2[0])))
	{
		return false;
	}

	if(!(parseInt(compare1[1]) == parseInt(compare2[1])))
	{
		return false;
	}

	return true;

}
function isValidCount(check1,check2)
{
	var compare1 = check1.split(".");
	var compare2 = check2.split(".");

	var network = (compare2[2]-compare1[2])
		var nodes = (compare2[3] - compare1[3])
		var totnodes = (network*255) + nodes

		if(totnodes >= 1000)
		{
			return false;
		}

	return true;
}

function isFirstIsGreaterThanSecond(ipAddr1 , ipAddr2)
{
	ipAddrArray1 = ipAddr1.split(".");
	ipAddrArray2 = ipAddr2.split(".");

	if(ipAddrArray1.length != 4 && ipAddrArray2.length != 4)
	{
		return false;
	}

	for(var i = 0 ; i < 4 ; i ++)
	{

		if(!(parseInt(ipAddrArray1[i], 10) >= parseInt(ipAddrArray2[i], 10)))
		{
			if(parseInt(ipAddrArray1[2], 10) < parseInt(ipAddrArray2[2], 10))
			{
				return false;
			}

			if(parseInt(ipAddrArray1[2], 10) != parseInt(ipAddrArray2[2], 10))
			{
				return true;
			}
			return false;
		}
	}
	return true;
}
function showEditDivs(val){
        document.getElementById(val+"Display").style.display="none";
        document.getElementById(val+"Edit").style.display="block";

        document.getElementById(val+"SysNameEdit").style.display="block";
        document.getElementById(val+"SysNameDisplay").style.display="none";

        document.getElementById(val+"communityEdit").style.display="block";
        document.getElementById(val+"communityDisplay").style.display="none";

        document.getElementById(val+"contactEdit").style.display="block";
        document.getElementById(val+"contactDisplay").style.display="none";

        document.getElementById(val+"locationEdit").style.display="block";
        document.getElementById(val+"locationDisplay").style.display="none";
}
function updateDetail(actionFrom)
{
        
	document.hostForm.actionFrom.value = actionFrom;
        
        selectedArgs="";
        if(checkSelection("update" , "Please Select a host to \"Update\"" ))
        {
            var ips = TrimString(selectedArgs);
            var ip = ips.split(",")
            
            for(var i=0; i<ip.length; i++)
            {
                if(ip[i].length > 0)
                {
                    var ipVal = ip[i];
                    window.parent.document.getElementById(ipVal+"statusImage").src = "/images/loading_img.gif";
                    
                 }
                
            }    
        
        
       var url = '/SYS_UPDATE.do?&methodToCall=execute&checked='+selectedArgs+'&actionFrom='+actionFrom;
       persist(url);
       
       document.getElementById("errorAnchor").innerHTML="";
       document.getElementById("updateMsgAnchor").innerHTML="Updating System details . Please wait .......";
       
    }

}
function saveValue(val)
{

        var community = document.getElementById(val+"communitytext").value;
        var sName = document.getElementById(val+"SysNametext").value;
        var sLocation = document.getElementById(val+"locationtext").value;
        var sContact = document.getElementById(val+"contacttext").value;
        document.hostForm.actionFrom.value="save"

        var action = document.hostForm.actionFrom.value
        var url = '/SYS_UPDATE.do?&methodToCall=execute&id='+val+'&community='+community+'&name='+sName+'&location='+sLocation +'&contact='+sContact+'&actionFrom='+action;

        persist(url);

    
}
function setValue(val)
{
        
        var community = document.getElementById(val+"communitytext").value;
        var sName = document.getElementById(val+"SysNametext").value;
        var sLocation = document.getElementById(val+"locationtext").value;
        var sContact = document.getElementById(val+"contacttext").value;
        document.hostForm.actionFrom.value="save"
        
        document.getElementById(val+"SysNameAnchor").innerHTML=sName;
        document.getElementById(val+"communityAnchor").innerHTML=community;
        document.getElementById(val+"contactAnchor").innerHTML=sContact;
        document.getElementById(val+"locationAnchor").innerHTML=sLocation;

        document.getElementById(val+"Display").style.display="block";
        document.getElementById(val+"Edit").style.display="none";
        document.getElementById(val+"SysNameEdit").style.display="none"
        document.getElementById(val+"SysNameDisplay").style.display="block"

        document.getElementById(val+"communityEdit").style.display="none"
        document.getElementById(val+"communityDisplay").style.display="block"

        document.getElementById(val+"contactEdit").style.display="none"
        document.getElementById(val+"contactDisplay").style.display="block"

        document.getElementById(val+"locationEdit").style.display="none"
        document.getElementById(val+"locationDisplay").style.display="block"

}

function scanDetail(f,value)
{
    f.actionFrom.value = value;
    document.getElementById("errorAnchor").innerHTML="";

    if(value == "scan")
    {
        if(!validate1())
        {
            return false;
	}
	else if(document.hostForm.communityList.value.length==0)
	{
            alert("Please enter the community List.")
                    document.hostForm.communityList.focus();
            return false;
        }
        var startIP= f.startIP.value;
        var endIP= f.endIP.value;
        var communityList= f.communityList.value;
        document.hostForm.inputType.value = "Multiple";
        var url='/SYS_UPDATE.do?&methodToCall=execute&actionFrom='+value+'&startIP='+startIP+'&endIP='+endIP+'&communityList='+communityList;
   
     }else if(value == "get")
     {
        document.hostForm.inputType.value = "Single";
        var ipOrHost = document.hostForm.ipOrHost
        if(document.hostForm.ipOrHost.value.length==0)
        {
                alert("Please enter an End IP address ")
                        document.hostForm.ipOrHost.focus();
                return false;
        }
        else if(document.hostForm.community.value.length==0)
        {
                alert("Please enter the community .")
                        document.hostForm.community.focus();
                return false;
        }
        else if(!checkip(ipOrHost))
        {
                return false;
        }
        var ipOrHost= ipOrHost.value;
        var community= f.community.value;
        f.actionFrom.value = value;
        var url='/SYS_UPDATE.do?&methodToCall=execute&actionFrom='+value+'&community='+community+'&ipOrHost='+ipOrHost;
     }
     document.hostForm.scan.disabled=true;
     document.hostForm.scan.className= "buttonDisabled";
     document.hostForm.get.disabled=true;
     document.hostForm.get.className= "buttonDisabled";
     window.parent.document.getElementById("statusImage1").src = "/images/loading_img.gif";
     document.getElementById("updateMsgAnchor").innerHTML="Getting System details . Please wait .......";
     persist(url);
}
var req;
function persist(url)
{
        if (window.XMLHttpRequest)
        {
                req = new XMLHttpRequest();
                req.onreadystatechange = processReqChange;
                req.open("GET", url, true);

                req.send(null);
        }

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

function processReqChange() 
{
        
	if (req.readyState == 4) 
	{
		if (req.status == 200) 
                {
                    if(document.hostForm.actionFrom.value == "update")
                     {
                        complitionUpdate()
                     }else if(document.hostForm.actionFrom.value == "scan")
                     {
                        
                        scanValues();
                        
                     }else if(document.hostForm.actionFrom.value == "get")
                     {
                        var stat = TrimString(req.responseText);
                        if(stat.length == 0 )
                        {
                            scanValues();
                            return;
                        }
                        document.hostForm.scan.disabled=false;
                        document.hostForm.scan.className= "Button";
                        document.hostForm.get.disabled=false;
                        document.hostForm.get.className= "Button";
                        document.getElementById("updateMsgAnchor").innerHTML="";
                        document.getElementById("errorAnchor").innerHTML=stat;
                        window.parent.document.getElementById("statusImage1").src = "/images/spacer.gif";
                        
                     }
                     else 
                     {
                            checkStatus();

                     }
                  
                 }       
		else
                {
			alert("There was a problem retrieving the XML data:\n" +req.statusText);
                 }
	}
}
function scanValues()
{
    var startIP = document.hostForm.startIP.value;
    var endIP = document.hostForm.endIP.value;
    var ipOrHost = document.hostForm.ipOrHost.value;
    var communityList= document.hostForm.communityList.value;
     var community= document.hostForm.community.value;
     var inputType = document.hostForm.inputType.value;
    window.parent.document.location.href="/MainLayout.do?selectedToolID=SYS_UPDATE&selectedTab=NETWORK_MGMT&selectedParentID=OPTools&startIP="+startIP+"&endIP="+endIP+"&communityList="+communityList+"&community="+community+"&ipOrHost="+ipOrHost+"&inputType="+inputType
}
	
function checkStatus(){
	
        
        var x = req.responseXML.getElementsByTagName('saved');
        if(x[0].getAttribute("status")=='Success')
        {
                setValue(x[0].getAttribute("ipAdd"));
		return;		
	}
	else{
		document.getElementById("errorAnchor").innerHTML="The data updates is : "+req.responseText;
	}
}
function complitionUpdate()
{
        var x = req.responseXML.getElementsByTagName('saved');
        document.getElementById("updateMsgAnchor").innerHTML="";
        //window.parent.document.getElementById("errorImage").src = "/images/spacer.gif";
        for(var i=0 ; i< x.length ; i++)
        {
            var result = x[i].getAttribute("result");
            var ip = x[i].getAttribute("ip");
            //var msg ="";
            if(result == "3")
            {
                window.parent.document.getElementById(ip+"statusImage").src = "/images/statusUp.gif";
                var msg = "System details successfully  updated"
                window.parent.document.getElementById(ip+"statusImage").title = msg ;
                //window.parent.document.getElementById(ip+"statusImage").onmouseover="javascript:showTip(this, msg , event)"
                //window.parent.document.getElementById(ip+"statusImage").onmouseout="javascript:hideddrivetip();" 
             }
             else if(result == "2")
             {
                var msg = "System details Update is FAILED"
                window.parent.document.getElementById(ip+"statusImage").src = "/images/statusDown.gif";
                window.parent.document.getElementById(ip+"statusImage").title = msg ;
                
             }
             
         }
         var count = req.responseXML.getElementsByTagName('count');
         document.getElementById("updateCount").innerHTML = count[0].getAttribute("updated");
         document.getElementById("updateFailCount").innerHTML = count[0].getAttribute("updateFailure");
         document.getElementById("notDoneCount").innerHTML = count[0].getAttribute("notDone");
}
//onmouseover="javascript:showTip(this, '<%= stausTips%> ' , event)"  onmouseout="javascript:hideddrivetip();" 
function showInputField(choice){
        	if(choice == 'Multiple'){
			ShowTab('Multiple');
			HideTab('Single');
                        document.hostForm.inputType.value="Multiple";
		}
		else{
			ShowTab('Single');
			HideTab('Multiple');
                        document.hostForm.inputType.value="Single";
		}
}

function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function toggle() {
 if( document.getElementById("hidethis").style.display=='none' ){
   document.getElementById("hidethis").style.display = '';
 }else{
   document.getElementById("hidethis").style.display = 'none';
 }
}
function showhide() {
 if( document.getElementById("hidethis1").style.display=='none' ){
   document.getElementById("hidethis1").style.display = '';
 }else{
   document.getElementById("hidethis1").style.display = 'none';
 }
}
function ShowTab(divId)
{
	var id = document.getElementById(divId);
	id.style.display = "block";
}
function HideTab(divId){
	var id = document.getElementById(divId);
	id.style.display = "none";
}

function ToggleCheckbox(e)
    {
   	if (e.checked) {
	    Highlight(e);
	    document.hostForm.selectAll.checked = AllChecked();
	}
	else {
	    Unhighlight(e);
	    document.hostForm.selectAll.checked = false;
	}
    }
            
    function ToggleAll(e)
    {
	if (e.checked) {
	    CheckAll();
	}
	else {
	    ClearAll();
	}
    }

    function Check(e)
    {
	e.checked = true;
	Highlight(e);
    }

    function Clear(e)
    {
	e.checked = false;
	Unhighlight(e);
    }

    function CheckAll()
    {
	var ml = document.hostForm;
	var len = ml.elements.length;
	for (var i = 0; i < len; i++) {
	    var e = ml.elements[i];
	    if (e.name == "Mid") {
		Check(e);
	    }
	}
	ml.selectAll.checked = true;
    }

    function ClearAll()
    {
	var ml = document.hostForm;
	var len = ml.elements.length;
	for (var i = 0; i < len; i++) {
	    var e = ml.elements[i];
	    if (e.name == "Mid") {
		Clear(e);
	    }
	}
	ml.selectAll.checked = false;
    }

    function Highlight(e)
    {

	var r = null;
	if (e.parentNode && e.parentNode.parentNode) {
	    r = e.parentNode.parentNode;
	}
	else if (e.parentElement && e.parentElement.parentElement) {
	    r = e.parentElement.parentElement;
	}
	if (r)
	 {
	    if (r.className == "oddRow")
		 {
		r.className = "selectedRowOdd";
	    }
	    else if (r.className == "evenRow")
		 {
		r.className = "selectedRowEven";
	    }
		
		if (r)
	 {
	    if (r.className == "oddRowNew")
		 {
		r.className = "selectedRow";
	    }
	    else if (r.className == "evenRowNew")
		 {
		r.className = "selectedRow";
	    }
	}	
		
		
	}
    }

    function Unhighlight(e)
    {
	var r = null;
	if (e.parentNode && e.parentNode.parentNode) {
	    r = e.parentNode.parentNode;
	}
	else if (e.parentElement && e.parentElement.parentElement) {
	    r = e.parentElement.parentElement;
	}
	if (r) {
	    if (r.className == "selectedRowOdd") {
		r.className = "oddRow";
	    }
	    else if (r.className == "selectedRowEven") {
		r.className = "evenRow";
	    }
	}
	
	if (r) {
	    if (r.className == "oddRowSelNew") {
		r.className = "oddRowNew";
	    }
	    else if (r.className == "evenRowSelNew") {
		r.className = "evenRowNew";
	    }
	}
    }

    function AllChecked()
    {
	ml = document.hostForm;
        var x = document.getElementsByName("select")
        for(var i = 0 ; i < x.length ; i++) {
	    if (!x[i].checked) {
		return false;
	    }
	}
	return true;
    }




