// whitespace characters
var whitespace = " \t\n\r";

function emailcheck(str) {

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
       return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false
    }

     if (str.indexOf(at,(lat+1))!=-1){
        return false
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false
     }

     if (str.indexOf(dot,(lat+2))==-1){
        return false
     }
    
     if (str.indexOf(" ")!=-1){
        return false
     }

     return true          
  }


/****************************************************************/

// Check whether string s is empty.
function isEmpty(s)
{ return ((s == null) || (s.length == 0)) }

/****************************************************************/

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 ForceEntry(val, str, one_msg) {
      var strInput = new String(val.value);

      if (isWhitespace(strInput)) {  
          if (true == one_msg)          
            document.getElementById("MsgToUser").innerHTML = str;
          else
            document.getElementById("MsgToUser").innerHTML += str;              
          return false;
      } else
          return true;

}

function ForceEntrySelectBox(val, str, one_msg) {
    if (val.options [val.selectedIndex].value == -7) {
        if (true == one_msg)          
          document.getElementById("MsgToUser").innerHTML = str;
        else
          document.getElementById("MsgToUser").innerHTML += str;              
        return false;
    } else
      return true;
}

function ForceEmailAddress(val, str, one_msg) {    
  if (true == emailcheck(val.value) )
    return true;
  else {
    if (true == one_msg)          
      document.getElementById("MsgToUser").innerHTML = str;
    else
      document.getElementById("MsgToUser").innerHTML += str;              
    return false;      
  }    
} 

function IsEmpty(val) {
  return  isWhitespace(val.value);
}   
  
function ForceSelectionRadio(val, str, one_msg) {
  // check if one of the radiobuttons is selected:
  var bAnySelected = false;
  var iCnt = 0;  
  for (iCnt = 0; iCnt < val.length; iCnt++)
  {
	  if (val[iCnt].checked == true)
		  bAnySelected = true;
  }
  //after loop:
  if (bAnySelected == false)
  {
	  document.getElementById("MsgToUser").innerHTML = str;
	  return false;
  } else {
    return true;
  }
}

function ForceSelectionCheckbox(max, str, one_msg) {
  // check if one of the checkboxes has been checked:
  var bAnySelected = false;
  var iCnt = 1;  
  for (iCnt = 1; iCnt <= max; iCnt++)
  {
    object = document.getElementById(iCnt);
	  if (object.checked == true)
		  bAnySelected = true;
  }
  //after loop:
  if (bAnySelected == false)
  {
	  document.getElementById("MsgToUser").innerHTML = str;
	  return false;
  } else {
    return true;
  }
}
