//$Id: InputDetails.js,v 1.6 2005/08/24 09:50:37 jprasanna Exp $
function validate()
{
	startAction(document.hostForm);

	document.hostForm.doAction.value = "startAction";

        var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;

	document.hostForm.ipOrHost.value = trim(document.hostForm.ipOrHost.value);

        if(document.hostForm.ipOrHost.value.length==0)
        {
                alert("Please enter an IP address or a host name.")
                document.hostForm.ipOrHost.focus();
		defaultAction(document.hostForm);
                return false;
        }

        if(document.hostForm.community.value.length==0)
        {
                alert("Please enter the community.")
                document.hostForm.community.focus();
		defaultAction(document.hostForm);
                return false;
        }

        key = document.hostForm.ipOrHost.value

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

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

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

function validateMacResolver(){
	document.hostForm.ipOrHost.value = trim(document.hostForm.ipOrHost.value);
        if(document.hostForm.ipOrHost.value.length==0)
        {
                alert("Please enter an IP address, a host name or a MAC address.")
                document.hostForm.ipOrHost.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;
                }
                for (var i=0; i<parts.length; i++)
                {
                        if (parseInt(parseFloat(parts[i])) > 255) { return false; }
                }
                return true;
        } 
	else
        {
                return false;
        }
}

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;
}

