function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function validateFirstName(firstNameID)
{
  return validateFirstName(firstNameID,false);
}
function validateFirstName(firstNameID,canBeEmpty)
{
document.getElementById(firstNameID).value=trim(document.getElementById(firstNameID).value);
var firstNameValue = document.getElementById(firstNameID).value
if((!canBeEmpty)&&firstNameValue.length==0)
  {
    alert("The first name field must not be empty");
    return false;
  }
  var allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  for(var i=0;i<firstNameValue.length;i++)
  {
    var currChar = firstNameValue.charAt(i);
    if(allowed.indexOf(currChar)==-1)
    {
      if(currChar==" ")
        currChar="space";
      alert("The character ["+currChar+"] is not allowed in the first name.");
      return false;
    }
  }
  return true;
}

function validateLastName(lastNameID)
{
  return validateLastName(lastNameID,false);
}
function validateLastName(lastNameID,canBeEmpty)
{
document.getElementById(lastNameID).value=trim(document.getElementById(lastNameID).value);
var lastNameValue = document.getElementById(lastNameID).value
if((!canBeEmpty)&&lastNameValue.length==0)
  {
    alert("The last name field must not be empty");
    return false;
  }
  var allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'. -";
  for(var i=0;i<lastNameValue.length;i++)
  {
    var currChar = lastNameValue.charAt(i);
    if(allowed.indexOf(currChar)==-1)
    {
      if(currChar==" ")
        currChar="space";
      alert("The character ["+currChar+"] is not allowed in the last name.");
      return false;
    }
  }
  return true;
}
function validateCity(cityID)
{
  return validateCity(cityID,false);
}
function validateCity(cityID,canBeEmpty)
{
document.getElementById(cityID).value=trim(document.getElementById(cityID).value);
var cityValue = document.getElementById(cityID).value
if((!canBeEmpty)&&cityValue.length==0)
  {
    alert("The city field must not be empty");
    return false;
  }
  var allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'. ";
  for(var i=0;i<cityValue.length;i++)
  {
    var currChar = cityValue.charAt(i);
    if(allowed.indexOf(currChar)==-1)
    {
      if(currChar==" ")
        currChar="space";
      alert("The character ["+currChar+"] is not allowed in the city field.");
      return false;
    }
  }
  return true;
}

function validateRegion(regionID)
{
document.getElementById(regionID).value=trim(document.getElementById(regionID).value);
var regionValue = document.getElementById(regionID).value
if(regionValue.indexOf("---")!=-1)
  {
    alert("Please select a region under the country: "+regionValue);
    return false;
  }
return true;
}

function validateCompany(companyID)
{
document.getElementById(companyID).value=trim(document.getElementById(companyID).value);
var companyValue = document.getElementById(companyID).value
  var allowed = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'. ";
  for(var i=0;i<companyValue.length;i++)
  {
    var currChar = companyValue.charAt(i);
    if(allowed.indexOf(currChar)==-1)
    {
      if(currChar==" ")
        currChar="space";
      alert("The character ["+currChar+"] is not allowed in the company field.");
      return false;
    }
  }
  return true;
}

