
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 ValidateDomainName(element)
{
  return ValidateData (element, "a-zA-Z0-9.-", LDAP_PROVISIONING_DOMAIN_N_POPUP, true);
}

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 = 369;

function ValidateDoubleByte(editBox)
{
  var value = editBox.value;
  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 (ERROR_VALUE_RANGE, 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 VerifyNotBlank(formobj, text)
{
  if (!goingBack && GetObjectValue (formobj) == "")
  { 
    alert(text)
    ResetObjectToPreviousValue (formobj)
    return false;
  }
  return true;
}

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

function Ipv6Only(editBox)
{
  var results
  
  results = editBox.value == "" || 
    RegExpValidator (/^(((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:)))(\/([1-9][0-9]?|\1[0-2][0-9]))?$/,
      editBox, ERROR_INVALID_IPADDRESS)
  if (!results) ResetObjectToPreviousValue (editBox, false)
  return results
}

function Ipv4OrIpv6Only(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) 
  if(!results)
  {
    results = RegExpValidator (/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/,
      editBox, ERROR_INVALID_IPADDRESS)
  }
	  
  if (!results) 
  {
    ResetObjectToPreviousValue (editBox, false)
  }
  return results
}

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 true
  //return ValidateData (element, " ", INVALID_PSWD, false);
}
