


//used for validation
var regExp = new RegExp("\'|>|<|;|,|\"|\`|%|\&");
function validEmail(checkString,notFiled)
{
    var isOk = true;
    var strToCheck = notFiled ? checkString : checkString.value;
    var AtSignValid = DoublePeriod = PeriodValid = SpaceValid = 0;
	var x = -2;




    var emailError = "You are not allowed to use this email domain";
    var Reason = "Sorry, the email address you entered \nis not in the correct format.";
   arrayOfStrings = strToCheck.split("@")


    // DO SOME PRELIMINARY CHECKS ON THE DATA
    if (strToCheck.length == 0)
      return true;

	//we don't allow the user to use out email domain
	if (arrayOfStrings[1] == 'wms.telemessage.com') {
      unescapeAlert (emailError);
      if (!notFiled) checkString.value = "";
      return false;
    }

    if (strToCheck.charAt(strToCheck.length-1) == ';')
		checkString.value = strToCheck.substring(0,strToCheck.length-1)

    for (i = 0;  i < strToCheck.length;  i++)
    {
      if (strToCheck.charAt(i) == '@')
         AtSignValid++;
      else if (strToCheck.charAt(i) == '.')
      {
         if (x == (i-1))
           DoublePeriod++;
         else
         {
           x = i;
           PeriodValid++;
         }
      }
      else if (strToCheck.charAt(i) == ' ')
         SpaceValid ++;
    }

    if (AtSignValid == 0) {
       Reason += "\nAddress must contain at least one" + " '@'.";
       isOk = false;
    }

    if (AtSignValid > 1) {
       Reason += "\nOnly one %27@%27 allowed," + " " + AtSignValid + " " + "found" + ".";
       isOk = false;
    }
    if (PeriodValid == 0) {
       Reason += "\nAddress must contain at least one period.";
	 isOk = false;
    }
    if (SpaceValid > 0) {
       Reason += "\nNo Spaces allowed. Address contains" + " " + SpaceValid + " " + "space";
       isOk = false;
    }
    if (SpaceValid > 1)
       Reason += "s.";
    if (DoublePeriod > 0){
       Reason += "\nAddress contains multiple periods in a row.";
       isOk = false;
    }
    if (strToCheck.length > 120) {
       Reason += "\nPlease limit the email address to 120 characters.";
       isOk = false;
    }

    if (isOk) {
        return true;
    }
    else {
      // DISPLAY ERROR MESSAGE
      unescapeAlert (Reason);
      if (!notFiled) checkString.value = "";
      return false;
    }
}

function statusMsg(s) {
  window.status = (s ? unescape(s) : '')
  return true
}

function checkNumber(entry,notField)
{
   var result = true;
   var strToCheck = notField ? entry : entry.value;
   if (strToCheck.length !=0) {
	if (String(strToCheck*1)=="NaN") {
		unescapeAlert("Please use digits only")
		if (!notField)
			entry.value = "";
       		result = false;
	}
   }
   return result;
}

function checkPhoneNumber(entry,notField)
{
   var result = true;
   var strToCheck = notField ? entry : entry.value;
   if (strToCheck.length != 0) {
   	var stringArray = strToCheck.split("-");
   	for (var i = 0; (i < stringArray.length) && (result); i++)
      		if (String(stringArray[i]*1)=="NaN")
      		{
	 		unescapeAlert("Please use digits only")
                        if (!notField) entry.value = "";
         		result = false;
      		}
   }
   return result;
}

function checkContactName(entry)
{
	var value = entry.value;
	if(regExp.test(value)) {
		unescapeAlert("Field includes invalid characters");
		return false;
	}

	return true;
}

var validLoginChar = '!$0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ^_abcdefghijklmnopqrstuvwxyz'

function checkLoginName(entry)
{
	var charFound = false;
	var value = entry.value;
	for (var j=0;j< value.length; j++) {
		for (var i = 0; i < validLoginChar.length && !charFound; i++){
			if (value.charAt(j) == validLoginChar.charAt(i))
				charFound =true;
		}
		if (!charFound) {
			unescapeAlert("The Field includes invalid characters\nPlease use only the following characters:\n"
			+validLoginChar);
			return false;
		}
		else charFound = false;
	}
	return true;
}

// To check that a field is all numeric digits use
// returns true if str is numeric that is it contains only the digits 0-9
// returns false otherwise
// returns false if empty
function isNumeric(str)
{
	var len= str.length;
	if (len==0)
		return false;
  	//else
  	var p=0;
	var ok= true;
	var ch= "";
	while (ok && p<len) {
		ch= str.charAt(p);
		if ('0'<=ch && ch<='9')
			p++;
		else
			ok= false;
	}
	return ok;
}

// To check that a field is all numeric digits and if not display an unescapeAlert
function isNumericWithAlert(str)
{
	var ok = isNumeric(str);
	if (!ok)
		unescapeAlert("Please use digits only");
	return ok;
}

// To get the number from a field use
function numericValue(str)
// returns the numeric value of str (which must be all digits)
{
  if (isNumeric(str))
  {
    while (str.charAt(0)=="0" && str.length>1) // if has leading zero (but not just "0")
      str= str.substring(1, str.length); // remove any leading zeros (otherwise assumes octal)
    return(parseInt(str));
  }
  else
  {
    unescapeAlert("Attempt to take numericValue of non-Numeric string "+str);
    return "NaN";
  }
}

// To check that a field is a possibly signed (+ or -) number use
// returns true if str is (optionally) signed numeric
// that is it contains only the digits 0-9 with an option leading sign (+ or -)
// returns false otherwise
// returns false if empty (or contains only a sign)
function isSignedNumeric(str)
{
  var len= str.length;
  if (len==0)
    return false;
  //else
  var ch= str.charAt(0);
  if (ch=="+" || ch=="-")
    return isNumeric(str.substring(1,str.length));
  else
    return isNumeric(str);
}

// To get the number from a signed numeric field use
// returns the numeric value of str (which must be a (optionally) signed number)
function signedNumericValue(str)
{
  if (isSignedNumeric(str))
  {
    var s=str.charAt(0);
    if (s=="+" || s=="-")
      str= str.substring(1, str.length);  // cut off sign
    while (str.charAt(0)=="0" && str.length>1) // if has leading zero (but not just "0")
      str= str.substring(1, str.length); // remove any leading zeros (otherwise assumes octal)
    if (s=="+" || s=="-")
      str= s+str;                        // restore sign
    return(parseInt(str));
  }
  else
  {
    unescapeAlert("Attempt to take signedNumericValue of non-Numeric string "+str);
    return "NaN";
  }
}

// To check that a field is alphabetic use
// returns true if str is alphabetic
// that is only A-Z a-z or space
// returns false otherwise
// returns false if empty
function isAlphabetic(str)
{
  var len= str.length;
  if (len==0)
    return false;
  //else
  var p=0;
  var ok= true;
  var ch= "";
  while (ok && p<len)
  {
    ch= str.charAt(p);
    if (  ('A'<=ch && ch<='Z')
        ||('a'<=ch && ch<='z')
        ||(ch==" ")
          )
      p++;
    else
      ok= false;
  }
  return ok;
}

//To check that a field is alphabetic but may include some other characters use, where plus is a string containing those extra permissible characters:
// returns true if str is alphabetic with addition of characters in plus
// that is only A-Z a-z or space or any of the characters in the string plus
// returns false otherwise
// returns false if empty
function isAlphabeticPlus(str, plus)
{
  var len= str.length;
  if (len==0)
    return false;
  //else
  var p=0;
  var ok= true;
  var ch= "";
  while (ok && p < len)
  {
    ch= str.charAt(p);
    if (  ('A' <= ch && ch <= 'Z')
        ||('a' <= ch && ch <= 'z')
        ||(ch==" ")
        ||(plus.indexOf(ch,0)>-1)
       )
      p++;
    else
      ok= false;
  }
  return ok;
}

function unescapeAlert(str) {
	alert (unescape(str));
}

function unescapeConfirm(str) {
	return confirm (unescape(str));
}

function isArray() {
	if(typeof arguments[0] == 'array'){
		return true;
	}else if (typeof arguments[0] == 'object') {  
		var criterion = arguments[0].constructor.toString().match(/array/i); 
		return (criterion != null);  
	}
 	return false;
}

function containHebChar (str , isLocaleRtl)
{
	if (!isLocaleRtl)
		return false;

	// passing arrays are by reference, which is much more efficient in IE
	// so we want to pass an array containing a single string object
	if (isArray(str))
	  str = str[0];

	var i = 0;
	var len=str.length;

	while(i< len){
	 	  if (str.charCodeAt(i) > 255)
		  	return true;
		  i++;
	}
	return false;

}


function trim(s)
{
  // Remove leading spaces and carriage returns
    while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}
