/** $Id: SweepToolFunctions.js,v 1.17 2006/09/04 10:52:08 karuppannan Exp $*/
function validate()
{
	
	f = document.hostForm;

	if(f.actionFrom.value == 'stopButton')
        {
		return true;
	}
        if(f.actionFrom.value == 'deleteButton')
        {
		return true;
	}
        if(f.actionFrom.value == 'scanButton')
        {
		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;
		}
	}
        if(f.actionFrom.value == 'addButton')
        {
            if(f.inputType.value == 'IPRange')
            {
                
                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;
                }
                */
            }
            if(f.inputType.value == 'AddIPList')
            {
                ipList = f.ipList.value;
                if(ipList.length == 0)
                {	
                    alert("Please enter the List of IPs to be added separated by comma");
                    f.ipList.focus();
                    return false;
                }
                return true;
            }
            if(f.inputType.value == 'ImportCSV')
            {
                fileString = f.impBrowser.value;
                if(fileString.length == 0)
                {	
                    alert("Please enter proper CSV file");
                    f.impBrowser.focus();
                    return false;
                }
                return true;
            }
        }

	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 setDiscoveryData(elementID , v)
{
	// will be implemented later
	//var obj = document.getElementById(elementID);
	//obj.value = v;

	if(elementID == 'resolvedIPCount')
	{
		if(obj = document.hostForm.resolvedIPCount)
				obj.value = v;
	}
	if(elementID == 'respIPCount')
	{
		if(obj = document.hostForm.respIPCount)
				obj.value = v;
	}
	if(elementID == 'notRespIPCount')
	{
		if(obj = document.hostForm.notRespIPCount)
				obj.value = v;
	}
	if(elementID == 'nonSnmpIPCount')
        {
                if(obj = document.hostForm.nonSnmpIPCount)
                                obj.value = v;
        }
	if(elementID == 'totalIPCount')
	{
		if(obj = document.hostForm.totalIPCount)
				obj.value = v;
	}
        if(elementID == 'notScannedIPCount')
	{
		if(obj = document.hostForm.notScannedIPCount)
				obj.value = v;
	}

}


function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = /  /g;
   while (temp.match(obj)) { temp = temp.replace(obj, ""); }
   return temp;
}

