
//------------------------------------------------------------------

function isRequired(obj, sName)
{
  var validExp = /\s/;
  var bResult = obj.value.replace(validExp, '').length > 0;

  if (bResult == true)
    obj.style.backgroundColor = "white";
  else {
    obj.style.backgroundColor = "pink";
    if (sName != "") alert(sName);
  }
    
  return bResult;
}

//------------------------------------------------------------------

function isValidSelectOption(obj, sName)
{
  var bResult = obj.selectedIndex > 0;
  
  if (bResult == true)
    bResult = obj[obj.selectedIndex].value != "";
    
  if (bResult == true)
    obj.style.backgroundColor = "white";
  else {
    obj.style.backgroundColor = "pink";
    if (sName != "") alert(sName);
  }
  
  return bResult;
}

//------------------------------------------------------------------

function isValidEmailAddress(obj, sName)
{
  var objValue = obj.value;
  obj.value = objValue.replace(/^\s*/, "").replace(/\s*$/, "");  //trim()

  var validExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
  var bResult = obj.value.match(validExp) != null;

  if (bResult == true)
    obj.style.backgroundColor = "white";
  else {
    obj.style.backgroundColor = "pink";
    if (sName != "") alert(sName);
  }
    
  return bResult;
}

// ---------------------------------------------------------------------

function isValidUSZipCode(obj, bMsg)
{
  var objValue = obj.value;
  obj.value = objValue.replace(/^\s*/, "").replace(/\s*$/, "");  //trim()

  var validExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
  var bResult = obj.value.match(validExp) != null;

  if (!bResult && bMsg)
    alert("Invalid US Zipcode. \n\nUS Zipcode format is 99999 or 99999-9999.");
    
  return bResult;
}

// ---------------------------------------------------------------------

function isValidCanadaPostCode(obj, bMsg)
{
  // Canadian postal code format: A9A 9A9
  // A - alphbetic
  // 9 - Numeric

  var objValue = obj.value;
  obj.value = objValue.replace(/^\s*/, "").replace(/\s*$/, "");  //trim()

  var validExp = /[A-Z|a-z]\d[A-Z|a-z]\s\d[A-Z|a-z]\d/;
  var bResult = obj.value.match(validExp) != null;

  if (!bResult && bMsg)
    alert("Invalid Canadian Postal Code. \n\nCanadian Postal Code format is A9A 9A9.");
    
  return bResult;
}

// ---------------------------------------------------------------------

function isValidZipPostCode(obj, bMsg)
{
  var bResult = isValidUSZipCode(obj, false) || isValidCanadaPostCode(obj, false);
  
  if (!bResult && bMsg)
    alert("Invalid Zip/Postal Code. \n\nValid Format: \nUS Zipcode: 99999 or 99999-9999. \nCanadian Postal Code: A9A 9A9");
    
  return bResult;
}
