// Declare global string to be removed from the string s
var bagstring = " \t\n\r";

//  stripCharsInBag (STRING s, STRING bag)
//
// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag)
{
	var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
	if (returnString.length <1) return false;
	else return true;
}

// Removes all whitespace characters from s.
// Global variable bagstring (see above)
// defines which characters are considered whitespace.

function stripWhitespace (s)
{
return stripCharsInBag (s, bagstring);
}

// 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)
{   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 (bagstring.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// isEmail (STRING s)
//
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
function isEmail (s){
  if (isEmpty(s))
    if (isEmail.arguments.length == 1)
      return false;
    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;
}

////////////////////////////////////////////////////
// check that both web domain and email domain match
function web_email_domain_check(str_email, str_web) {
  var dom_str = "";
  var dom_str2 = "";
  var w = str_web.indexOf ('www.',0)+4;
  var e = str_email.indexOf ('@',0)+1;
  domok = true;
  for (e=e; e < str_email.length; e++) {
    var ce = str_email.charAt(e);
    var cw = str_web.charAt(w);
    if (ce!=cw)
      return false;
    dom_str=dom_str+ce;
    dom_str2=dom_str2+cw;
    w=w+1;
  }
  if (dom_str.length <1)
    return false;
  else
    return true;
}

// Validate contact form
function validateForm_contact(f) {
  if (f.name.value == "") {
    alert("Please enter your name");
    f.name.focus();
    return(false);
  }
  if (f.email.value == "") {
    alert("Please enter your email address");
    f.email.focus();
    return(false);
  }
  if (!isEmail(f.email.value)) {
    alert("\nYour email address does not appear to be valid.\n\nPlease re-enter your e-mail address.");
    f.email.select();
    f.email.focus();
    return(false);
  }
  if (f.telephone.value == "") {
    alert("Please enter your contact telephone number");
    f.telephone.focus();
    return(false);
  }
}

