// BOI, followed by one or more whitespace characters, followed by EOI.
var reWhitespace = /^\s+$/;
// whitespace characters as defined by this sample code
var whitespace = " \t\n\r";
// Check whether string s is empty.

function isEmpty(s)
{
   return((s == null) ||(s.length == 0));
}
// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace(s)
{
   // Is s empty?
   return(isEmpty(s) || reWhitespace.test(s));
}
// charInString (CHARACTER c, STRING s)
//
// Returns true if single character c (actually a string)
// is contained within string s.

function charInString(c, s)
{
   for(i = 0; i < s.length; i++)
   {
      if(s.charAt(i) == c)
      return true;
   }
   return false;
}

function isValidOutputName(s)
{
   var specialCharString = "\\/:*?\"|;";
   var lessThan = "<";
   var graterThan = ">";
   
   for(i = 0; i < s.length; i++)
   {
      var curChar = s.charAt(i);
      if (specialCharString.indexOf(curChar) != -1) 
        return false;
      else if (lessThan.indexOf(curChar) != -1) 
      	   return false;
      else if (graterThan.indexOf(curChar) != -1) 
          return false;
   }
   return true;
}

function isValidOutputNameEx(inputString , excludeChars)
{
   var specialCharString = excludeChars ;
   var lessThan = "<";
   var graterThan = ">";
   
	var index ;

   for(i = 0; i < inputString.length; i++)
   {
      var curChar = inputString.charAt(i);
index = specialCharString.indexOf(curChar) ;

      if (specialCharString.indexOf(curChar) != -1) 
        return false;
      else if (lessThan.indexOf(curChar) != -1) 
      	   return false;
      else if (graterThan.indexOf(curChar) != -1) 
          return false;
   }
   return true;
}


// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripInitialWhitespace(s)
{
   var i = 0;
   while((i < s.length) && charInString(s.charAt(i), whitespace)) i++;
   return s.substring(i, s.length);
}
// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripTrailingWhitespace(s)
{
   var i = s.length - 1;
   while((i >= 0) && charInString(s.charAt(i), whitespace)) i--;
   return s.substring(0, i + 1);
}

function validateKeywordSearchPhrase(msg)
{
   // Validate user name
   var phraseLength = document.advancedsearchform.db_keyword.value.length;
   if(phraseLength == 0)
   {
      alert(msg);
      document.MM_returnValue = false;
      return false;
   }
   document.MM_returnValue = true;
}

function validateLoginForm(invalidusernamemsg)
{
   var temp = document.loginform.username.value;
   if((temp == null) ||(temp.length == 0))
   {
      alert(invalidusernamemsg);
      document.MM_returnValue = false;
      return false;
   }
   // Note that the Date timezone offset is in minutes and that the
   // Javascript time zone offsets seem to be negative of Java time zone offsets.
   var dt = new Date();
   document.loginform.timezoneOffset.value = dt.getTimezoneOffset();
   document.MM_returnValue = true;
}

function updateNumericSearchTypeLabel(select, labelfieldname, tolerancefieldname, negativeToleranceErrorMsg)
{
   var label = document.getElementById(labelfieldname);
   if(select.value == "equals")
   {
      label.childNodes.item(0).nodeValue = "+/-";
      var toleranceValue = tolerancefieldname.value;
      if(toleranceValue != null)
      {
         if(toleranceValue.indexOf("-") != - 1)
         {
            // Tell them that negative tolerances are not allowed
            alert(negativeToleranceErrorMsg);
            return false;
         }
      }
   }
   else 
   {
      label.childNodes.item(0).nodeValue = '<=';
   }
   return true;
}

function validateFloat(element, errorMsg)
{
   var value = element.value;
   // Blank field is allowed
   if(value == "")
   return true;
   var checkNum = parseFloat(value);
   if(isNaN(checkNum))
   {
      // They did not enter a number...
      alert(errorMsg);
      //element.value="";
      return false;
   }
   if(checkNum != value)
   {
      // They entered something with a number at the front - e.g. 12Y
      alert(errorMsg);
      //element.value=checkNum;
      return false;
   }
   return true;
}

function validateFloatWithOptions(element, errorMsg, allowBlank, allowNegative, allowZero)
{
   var value = element.value;
   // Blank field is allowed
   if(value == "")
   {
      if(allowBlank)
      return true;
      else 
      {
         alert(errorMsg);
         return false;
      }
   }
   var checkNum = parseFloat(value);
   if(isNaN(checkNum))
   {
      // They did not enter a number...
      alert(errorMsg);
      //element.value="";
      return false;
   }
   if(checkNum != value)
   {
      // They entered something with a number at the front - e.g. 12Y
      alert(errorMsg);
      //element.value=checkNum;
      return false;
   }
   if(!allowNegative &&(checkNum < 0))
   {
      alert(errorMsg);
      return false;
   }
   if(!allowZero &&(checkNum == 0))
   {
      alert(errorMsg);
      return false;
   }
   return true;
}

function validateInt(element, errorMsg)
{
   var value = element.value;
   // Blank field is allowed
   if(value == "")
   return true;
   var checkNum = parseInt(value);
   if(isNaN(checkNum))
   {
      // They did not enter an integer...
      alert(errorMsg);
      //element.value="";
      return false;
   }
   if(checkNum != value)
   {
      // They entered something with a number at the front - e.g. 12Y
      alert(errorMsg);
      //element.value=checkNum;
      return false;
   }
   if(value.indexOf(".") != - 1)
   {
      // They entered something with a decimal point.
      // JavaScript thinks 100.0 is an int but Java
      // on the server disagrees.
      alert(errorMsg);
      //element.value=checkNum;
      return false;
   }
   return true;
}

function validateStrictPositiveInt(element, errorMsg)
{
	var value = validatePositiveInt(element, errorMsg);
	if (value == true )
	{
		var checkNum = parseInt(element.value);   	
		if (checkNum == 0)
		{
	      alert(errorMsg);
	      return false;
		} 
	} 
	return value ;
}

//JAMI > same as validateInt but accepts only positive numbers
function validatePositiveInt(element, errorMsg)
{
   var value = element.value;
   // Blank field is allowed
   if(value == "") 
   {
   	return true;
   }
    
   var checkNum = parseInt(value);
   
   if(isNaN(checkNum))
   {
      // They did not enter an integer...
      alert(errorMsg);
      return false;
   }
   if(checkNum != value)
   {
      // They entered something with a number at the front - e.g. 12Y
      alert(errorMsg);
      //element.value=checkNum;
      return false;
   }
   if(value.indexOf(".") != - 1)
   {
      // They entered something with a decimal point.
      // JavaScript thinks 100.0 is an int but Java
      // on the server disagrees.
      alert(errorMsg);
      //element.value=checkNum;
      return false;
   }
   if (checkNum < 0)
   {
   	  alert(errorMsg);
   	  return false;
   }
   return true;
}


// Use validatePartialFloat and validatePartialInt to validate
// numbers that may not yet be fully typed (e.g., just a decimal
// point so far)

function validatePartialFloat(element, allowNegative, errorMsg)
{
   var value = element.value;
   // Blank field is allowed
   if(value == "")
   return true;
   // The user has only entered a decimal point and/or negative sign so far
   if(allowNegative)
   {
      if(value == "-")
      return true;
      if(value == "-.")
      return true;
   }
   if(value == ".")
   return true;
   var checkNum = parseFloat(value);
   if(isNaN(checkNum))
   {
      // They did not enter a number...
      alert(errorMsg);
      element.value = "";
      return false;
   }
   if(checkNum != value)
   {
      // They entered something with a number at the front - e.g. 12Y
      alert(errorMsg);
      element.value = checkNum;
      return false;
   }
   if(!allowNegative &&(checkNum < 0))
   {
      alert(errorMsg);
      return false;
   }
   return true;
}

function validatePartialInt(element, allowNegative, errorMsg)
{
   var value = element.value;
   // Blank field is allowed
   if(value == "")
   return true;
   // The user has only entered a negative sign so far
   if(allowNegative &&(value == "-"))
   return true;
   var checkNum = parseInt(value);
   if(isNaN(checkNum))
   {
      // They did not enter an integer...
      alert(errorMsg);
      element.value = "";
      return false;
   }
   if(checkNum != value)
   {
      // They entered something with a number at the front - e.g. 12Y
      alert(errorMsg);
      element.value = checkNum;
      return false;
   }
   if(value.indexOf(".") != - 1)
   {
      // They entered something with a decimal point.
      // JavaScript thinks 100.0 is an int but Java
      // on the server disagrees.
      alert(errorMsg);
      element.value = checkNum;
      return false;
   }
   if(!allowNegative &&(checkNum < 0))
   {
      alert(errorMsg);
      return false;
   }
   return true;
}

function validatePartialFloatForTolerance(select, toleranceErrorMsg, element, allowNegative, errorMsg)
{
   if(select.value == "equals")
   return validatePartialFloat(element, false, toleranceErrorMsg);
   else 
   return validatePartialFloat(element, allowNegative, errorMsg);
}

function validatePartialIntForTolerance(select, toleranceErrorMsg, element, allowNegative, errorMsg)
{
   if(select.value == "equals")
   return validatePartialInt(element, false, toleranceErrorMsg);
   else 
   return validatePartialInt(element, allowNegative, errorMsg);
}

function validateEmail(value) 
{
	//does the email contain personal info ?
	if (value.indexOf('<') != -1) // process only the email part
        		{ // find start index of email part
        			var startIndex = value.indexOf('<')+1;
        			if (value.indexOf('>') != -1) 
        			{ //find end index of email part
        				var endIndex = value.indexOf('>');
        				// strip the value only to the email part
        				value = stripInitialWhitespace(value.substring(startIndex, endIndex)); 
        			} else return false;
        		} 
	var filter  = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	if (filter.test(value)) return true;
	else return false;
}

function imposeMaxLength(Object, MaxLen, errorMsg)
{
	//alert('obj=' + Object.value + ' len=' + Object.value.length);
	if (Object.value.length > MaxLen + 1)
	{
		if (errorMsg) 
		{
			alert(errorMsg);
		}
		Object.value = Object.value.substring(0, MaxLen);
	}
	else if (Object.value.length > MaxLen) 
	{
		Object.value = Object.value.substring(0, MaxLen);
	}
}

//JANH to be extend!
function validateFileName(s)
{
  return isEmpty(s);
}


//JAMI int validation for hours (only int between 0...24)
function validateHours(element, errorMsg)
{
	var value = element.value;
	
   //Blank field is allowed
   if(value == "") {
   	return true;
   }
    
   var checkNum = parseInt(value);
   
   //Check for type
   if(isNaN(checkNum))
   {
      // They did not enter an integer...
      alert(errorMsg);
      //element.value="";
      return false;
   }
   
   //Check for mixup of value type
   if(checkNum != value)
   {
      // They entered something with a number at the front - e.g. 12Y
      alert(errorMsg);
      return false;
   }
   
   //Check if a floating number was not entered
   if(value.indexOf(".") != - 1)
   {
      // They entered something with a decimal point.
      // JavaScript thinks 100.0 is an int but Java
      // on the server disagrees.
      alert(errorMsg);
      return false;
   }

   //now check for actual int value - accept only a number between 0 and 24
   if ((checkNum < 0) || (checkNum > 24)) 
   {
   		//the value entered is not valid for an hour value
   		alert(errorMsg);
	    return false;
   }
   
   return true;   
}