// decimal point character differs by language and culture
var decimalPointDelimiter = "."
var mPrefix = "You did not enter a value into the "
var mSuffix = " field. This is a required field. Please enter it now."
// whitespace characters
var whitespace = " \t\n\r";

// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnEmpty (theField, s)
{   theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}


// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}


function Trim(str)
{
   var text = str;
   while ((text.length) && (text.charAt(0) == " ")) 
      text = text.substring(1, text.length);
   while ((text.length) && (text.charAt(text.length-1) == " "))
	text = text.substring(0, text.length-1);
    return text;
}

function Clear_Other_onclick(theForm, question)
{	
//Generic function for clearing the Other Text Box in a group of options
//theForm - Then name of the form
//question - the name of the question
   theForm.elements[question].value = "" ;  
}


function Clear_Other_onclick2(theForm, question, question2)
{	
//Generic function for clearing the Other Text Box in a group of options
//theForm - Then name of the form
//question - the name of the question
   theForm.elements[question].value = "" ;  
   theForm.elements[question2].value = "" ; 
}

function Q_onClick(Q, Q_Other)
{
   if (!Q.checked) Q_Other.value = "" ;  
}

function Q_Other_onchange(Q, Q_Other) {
	if (!isWhitespace(Q_Other.value)) {
		Q.checked=true;
	}
	else {
	    Q.checked=false;
	}
}

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}


function isEmail_old (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;
    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }
    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }
    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


function isEmail2 (s)
{
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
	var returnval=emailfilter.test(e.value)
	return returnval  
}

// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// isFloat (STRING s [, BOOLEAN emptyOK])
// 
// True if string s is an unsigned floating point (real) number. 
//
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isInteger, then call isFloat.
//
// Does not accept exponential notation.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isFloat (s)
{   var i;
    var seenDecimalPoint = false;
    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);
    if (s == decimalPointDelimiter) return false;
    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)
{   var i;
    // Is s empty?
    if (isEmpty(s)) return true;
    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    // All characters are whitespace.
    return true;
}


function isRadioSelected(opt)
{
//For the server side version in VBScript see the EmpId search in the PSReporting system.
//Look at article - Q190742 on MS Knowledge Base.  See this also Q178070
//An alternative solutions would be to use the RegEx object.
  var radioSelected = false;
  for(i=0; i < opt.length; i++)
  {  
	if (opt[i].checked)
	{
	   radioSelected = true;
	   break;
	}
  }
  if (!radioSelected)
  {
    return (false);
  }
  return (true);
}



function ClearRadio(opt)
{
//This function will clear a radio box back to it's default value
  var radioSelected = false;
  for(i=0; i < opt.length; i++)
  {  
	opt[i].checked = false;
  }
  return (true);
}
