function GetValidChars(validCharSet, inputString)
{
  return MakeValid ("[" + validCharSet + "]+", inputString);
}

function MakeValid (validCharRegExp, inputString)
{
  var allValidChars = "";
  var index;
  var matchResults 
  matchResults = inputString.match (RegExp (validCharRegExp, "g"));
  if (matchResults != null) {for (index = 0; index < matchResults.length; index++) { allValidChars += matchResults[index];} }
  return allValidChars;
}

function MakeFirstValid (validCharRegExp, inputString)
{
  //alert("MakeFirstValid:inputString="+inputString);
  var allValidChars = "";
  var mChar;
  var matchResults;
  var index;
  var newStart = 0;
  
  //find the index of the first alphanumeric character
  for (index = 0; index < inputString.length; index++) 
  {
    mChar = inputString.charAt(index);
    matchResults = mChar.match (RegExp (validCharRegExp, "g"));
    if(matchResults == null) newStart = index+1;
    else break;
  }
  
  //now create the new string
  if(newStart > 0)
  {
    for (index = newStart; index < inputString.length; index++)
    {
      allValidChars += inputString.charAt(index)
    }
  }
  
  return allValidChars;
}

function ValidateRange (object, lValue, hValue, message)
{
  var value = parseInt (object.value);

  if(value < lValue || value > hValue)
  {
    alert(message)
    object.value = ((value < lValue) ? lValue : hValue);
    return false;        
  }
  return true;
}

function ValidateData (element, chars, message, charsAreValid)
{
  var invalidChars;
  var tocheck = element.value;
  var rValue = true;
  var carrot;
  
  // alert("noQuotes tocheck="+tocheck);
  carrot = (charsAreValid)?"^":""
  invalidChars = tocheck.match (RegExp ("["+carrot+chars+"]+", "g"));
  if (invalidChars != null)
  {
    alert(message);
    carrot = (!charsAreValid)?"^":""
    element.value = MakeValid ("["+carrot+chars+"]+", tocheck);
    rValue = false;
  }
  return rValue;
}

function ValidateFirstChar (element, chars, message, charsAreValid)
{
  var invalidChars;
  var tocheck = element.value.charAt(0);
  var rValue = true;
  var carrot;
  
  // alert("noQuotes tocheck="+tocheck);
  carrot = (charsAreValid)?"^":""
  invalidChars = tocheck.match (RegExp ("["+carrot+chars+"]+", "g"));
  if (invalidChars != null)
  {
    //alert(message);
    carrot = (!charsAreValid)?"^":""
    element.value = MakeFirstValid ("["+carrot+chars+"]+", element.value);
    rValue = false;
  }
  return rValue;
}

function DNSInput(element)
{
  return ValidateData (element, "a-zA-Z0-9-", PLEENTLETNUMHYPH, true);
}

function AlphaOnlyInput(element)
{
  return ValidateData (element, "a-zA-Z", PLEENTLETTERSONLY, true);
}

function NoQuotes (element)
{
  return ValidateData (element, "\'\"", NOQUOTES, false);
}

function NoSpace (element)
{
  return ValidateData (element, "\ ", PSWD_INVALID_REENTER, false);
}
function NoExclamation (element)
{
  return ValidateData (element, "!", PLEENTVALIDCHARS, false);
}

function CheckGatewayInput(element)
{
  return ValidateData (element, "0-9#*.,:", VALIDCHAR_GATEWAY, true);
}
 
function DialingPrefix(editBox)
{
  return ValidateData (editBox, "0-9~", PLEENTNUMONLY, true)
}
 
function IntegersOnly(editBox)
{
  return ValidateData (editBox, "0-9", PLEENTNUMONLY, true)
}
 
function NumbersOnly(editBox)
{
  return ValidateData (editBox, "0-9-", PLEENTNUMONLY, true);
}

function ExtendedAsciiOnly (editBox)
{
  return ValidateData (editBox, "\x00-\xFF", PLEENTASCII, true)
}

function EmailOnly(editBox)
{
  return tmt_RegExpValidator(editBox,'%5E%5B%5Cw%5C.=-%5D%2B@%5B%5Cw%5C.-%5D%2B%5C.%5Ba-z%5D%7B2,3%7D$', INVALID_EMAIL,'','');
}
function CheckPhoneInput(editBox)
{
  return ValidateData (editBox, "0-9#*.,/ -", VALIDCHAR_PHONE, true);
}

function CheckISDNInput(editBox)
{
  return ValidateData (editBox, "0-9#*,~", VALIDCHAR_PHONE, true);
}

function KeypadOnly(editBox)
{
  return ValidateData (editBox, "0-9#*.", VALIDCHAR_GATEWAY, true);
}

var keyboardChars;
keyboardChars = "*#\\w/;@,.&$!\\\\ ";
keyboardChars += "\\xC8\\xC9\\xC0\\xC7\\xE0\\xE8\\xE9\\xE7";/* Add French Chars */
keyboardChars += "\\xDC\\xDF\\xD6\\xC4\\xFC\\xF6\\xE4";/* Add German Chars */
keyboardChars += "\\xC1\\xD1\\xD3\\xE1\\u0241\\xF3";/* Add Italian Chars */
keyboardChars += "\\xD2\\xF2";/* Add Spanish Chars */
/* The dash must be at the end of a regular expression */
keyboardChars += "-";

var alphaNumeric = "a-zA-Z0-9-";
function KeyboardInputOnly(editBox)
{
  return ValidateData (editBox, keyboardChars, PLEENTVALIDCHARS, true);
}

var kDoubleByteLength = 6;
var kSingleByteLength = 1;
var kMaxTextFieldLength = 210;

function ValidateDoubleByte(editBox)
{
  var value = editBox.value;
  var maxLength = editBox.length;
  var currentLength = 0;
  var currentValue = "";
  var rValue = true;
  var currentCharLength;
  var unicodeValue;

  for(var index = 0; index < value.length; index++)
  {
    unicodeValue = value.charCodeAt(index);
    // If the character is a part of the extended ascii character set
    // count it as kSingleByteLength.
    if(unicodeValue > 0 && unicodeValue < 256)
    {
      currentCharLength = kSingleByteLength;
    }
    else
    {
      currentCharLength = kDoubleByteLength;
    }
    
    if (currentLength + currentCharLength > kMaxTextFieldLength)
    {
      alert(FIELD_TOOLONG);
      editBox.value = currentValue;
      rValue = false;
      break;
    }
    currentValue += value.charAt (index);
    currentLength += currentCharLength;
  }
  
  return rValue;
}

function CheckIntRange(element, start, end) 
{
  var value = parseInt(element.value);
  if(value < start || value > end) 
  {
    /* Need to fix this message */
    alert (SPrintF ("Please enter a value between %s and %s", start, end));
    element.value = element.getAttribute ("oValue")
    return false;
  }
  return true;
}

function ValidateLANHostName(element)
{
  var validCharSet = "a-zA-Z0-9-";
  var validChars;
  var invalidChars;
  var name;
  var rvalue;
  var match;
  rvalue = true;
  name = element.value;
  invalidChars = name.match (RegExp ("[^" + validCharSet + "]+", "g"));
  /* Notify the user if they have entered any invalid chars */
  if (invalidChars != null)  {
    alert(HOSTNAME_INVALID)
    rvalue = false;
  }
  /* Remove any invalid characters */
  name = GetValidChars(validCharSet, name); 
  /* Make sure that the first character is not a number of a hyphen  */
  if (/^[0-9-]/.test (name))  {
    alert ("The Host Name must start with a letter.");
    match = name.match (/^[0-9-]+(.*)$/);
    if (match != null);
    name = match [1];
    rvalue = false;
  }
  /* Make sure that the last character is not a hyphen  */
  if (/[-]$/.test (name))  {
    alert ("The Host Name must end with a letter or digit.");
    match = name.match (/^(.*[^-])[-]+$/);
    if (match != null);
    name = match [1];
    rvalue = false;
  }
  /* Make sure that the length does not exceed  63 characters */
  if (name.length > 63) {
    alert ("The maxim lenght of the Host Name is 63 characters");
    name = name.substr (0, 63)
  }
  /* Make sure that after adjusting the lenght the last character is not a number of a hyphen */   
  if (/[-]$/.test (name)) {
    alert ("The Host Name must end with a letter or digit.");
    match = name.match (/^(.*[^0-9-])[-]+$/);
    if (match != null);
    name = match [1];
    rvalue = false;
  }
  element.value = name;   
  return rvalue;
}

function ValidateLANDomainName(element)
{
  	var validCharSet = "a-zA-Z0-9-.";
  	var validChars;
  	var invalidChars;
  	var name;
  	var rvalue;
  	var match;
  	rvalue = true;
  	name = element.value;
  	invalidChars = name.match (RegExp ("[^" + validCharSet + "]+", "g"));
  	/* Notify the user if they have entered any invalid chars */
  	if (invalidChars != null)  
	{
    		alert (HOSTNAME_INVALID)
    		rvalue = false;
  	}
  	/* Remove any invalid characters */
  	name = GetValidChars(validCharSet, name); 
  	/* Make sure that the first character is not a number of a hyphen  */
  	if (/^[0-9-.]/.test (name))  
	{
     		alert ("The Domain Name must start with a letter.");
    		match = name.match (/^[0-9-.]+(.*)$/);
    		if (match != null);
    		name = match [1];
    		rvalue = false;
  	}
  	/* Make sure that the last character is not a hyphen  */
  	if (/[-.]$/.test (name))  
	{
    		alert ("The Domain Name must end with a letter or digit.");
    		match = name.match (/^(.*[^-])[-.]+$/);
    		if (match != null);
    		name = match [1];
    		rvalue = false;
  	}
  	/* Make sure that the length does not exceed  64 characters */
  	if (name.length > 64) 
	{
    		alert ("The maximum length of the Domain Name is 64 characters.");
   	 	name = name.substr (0, 63)
  	}
  	/* Make sure that after adjusting the lenght the last character is not a number of a hyphen */   
  	if (/[-.]$/.test (name)) 
	{
    		alert ("The Domain Name must end with a letter or digit.");
    		match = name.match (/^(.*[^0-9-.])[-.]+$/);
    		if (match != null);
    		name = match [1];
    		rvalue = false;
  	}
  	element.value = name;   
  	return rvalue;
}

function VerifyNotBlank(formobj, text)
{
  if (GetObjectValue (formobj) == "")
  { 
    alert(text)
    ResetObjectToPreviousValue (formobj)
    return false;
  }
  return true;
}

function RegExpValidator (regExp, object, message)
{
  if(!regExp.test (object.value))
  {
    alert(message)
    return false
  }
  return true
}

function IPorDomainNameOnly(editBox)
{
  	var results = true;
  	var rvalue  = true;
	
 	//Validate IP Address. 
  	var regExp = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
   	if(!regExp.test (editBox.value))
  	{
    		results = false
  	}
	else
	{
		// 0.A.B.C is invalid IP address
  		var regExp2 = /^([0]*\.)((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
		if(!regExp2.test (editBox.value))
        	{
  			results = true
        	}
		else
		{
  			results = false
			// 0.0.0.0 is valid IP addresss
			// 00.00.0.00 is also valid
			var regExp3 = /^([0][0]?[0]?\.){3}([0][0]?[0]?)$/
        		if(regExp3.test (editBox.value))
        		{
  				results = true
       			}
		}
	}
  	results = (editBox.value == "" || editBox.value == "0.0.0.0" || results)

	//Validate for Domain Name if it is not valid IP
 	if (!results)
 	{
  		var validCharSet = "a-zA-Z0-9-.";
	  	var validChars;
  		var invalidChars;
  		var name;
  		var match;
  		rvalue = true;
  		name = editBox.value;
  		invalidChars = name.match (RegExp ("[^" + validCharSet + "]+", "g"));
  		/* Notify the user if they have entered any invalid chars */
  		if (invalidChars != null)  
		{
    			// alert(HOSTNAME_INVALID)
    			rvalue = false;
  		}
  		/* Remove any invalid characters */
  		name = GetValidChars(validCharSet, name); 
  		/* Make sure that the first character is not a number of a hyphen  */
  		if (/^[0-9-.]/.test (name))  
		{
    			// alert ("The Domain Name must start with a letter.");
    			match = name.match (/^[0-9-.]+(.*)$/);
    			if (match != null);
    			name = match [1];
    			rvalue = false;
  		}
  		/* Make sure that the last character is not a hyphen  */
  		if (/[-.]$/.test (name))  
		{
   			// alert ("The Domain Name must end with a letter or digit.");
    			match = name.match (/^(.*[^-])[-.]+$/);
    			if (match != null);
    			name = match [1];
    			rvalue = false;
  		}
  		/* Make sure that the length does not exceed  64 characters */
  		if (name.length > 64) 
		{
    			// alert ("The maximum length of the Domain Name is 64 characters.");
    			var name = name.substr (0, 64)
  		}
  		/* Make sure that after adjusting the lenght the last character is not a number of a hyphen */   
  		if (/[-.]$/.test (name)) 
		{
    			// alert ("The Domain Name must end with a letter or digit.");
    			match = name.match (/^(.*[^0-9-.])[-.]+$/);
    			if (match != null);
    			name = match [1];
    			rvalue = false;
  		}
  		editBox.value = name;  
 	} 
	//Reset Value if it is invalid IP address and invalid Domain Name 
  	if (!results && !rvalue)
  	{
		ResetObjectToPreviousValue (editBox, false)
    		alert (ENTRY_IP_HELP);
  	}
  	return (results || rvalue)
}


function IpOnly (editBox)
{
  var results
  
  results = editBox.value == "" || 
    RegExpValidator (/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
      editBox, ERROR_INVALID_IPADDRESS)
  if (!results) ResetObjectToPreviousValue (editBox, false)
  return results
}

function tmt_RegExpValidator(f,re,eMsg,ru,r)
{
  var myErr="";
//  var fv=MM_findObj(f).value;
  var fv = f.value;
  var rex=new RegExp(unescape(re));
  var t=eval(ru+rex.test(fv));
  var retval=true;
  if(r)
  {
    if(fv.length<=0||!t)
    {
      alert(eMsg);myErr+="eMsg";
      retval=false;
    }
  }
  else if(fv.length>0&&!t)
  {
    alert(eMsg);myErr+="eMsg";
    retval=false;
  }
  return retval;
}

function VerifyPassword (element)
{
  return ValidateData (element, " ", INVALID_PSWD, false);
}
