<!-- // *** submit_review.js ***

// --- global variables ---

var oForm = 0;       // object set by initPage

// --- functions ---

function validate_form(thisForm)
{
  var wFirstName = sTrim(thisForm.first_name.value,'B');
  var wLastName = sTrim(thisForm.last_name.value,'B');
  var wCity = sTrim(thisForm.city.value,'B');
  var wEmail = sTrim(thisForm.email.value,'B');
  var wReview = sTrim(thisForm.reviewText.value,'B');

  thisForm.first_name.value = wFirstName;
  thisForm.last_name.value = wLastName;
  thisForm.city.value = wCity;
  thisForm.email.value = wEmail;
  thisForm.reviewText.value = wReview;

  var wClub = thisForm.cannabis_club.options[thisForm.cannabis_club.selectedIndex].text;
  var wRating = thisForm.rating.value;

  if (wFirstName.length <= 0)
  {
    showError(thisForm.first_name,"Your First Name is required.");
    return false;
  }
  if (wLastName.length <= 0)
  {
    showError(thisForm.last_name,"Your Last Initial is required.");
    return false;
  }
  if (wCity.length <= 0)
  {
    showError(thisForm.city,"City is required.");
    return false;
  }

  if (wEmail.length <= 0)
  {
    showError(thisForm.email,"An email address is required.")
    return false;
  }

  if (!isValidEmail(wEmail))
  {
    showError(thisForm.email,"You entered an invalid email address.")
    return false;
  }

  if ( (wClub.substr(0,1) == "-") || (wClub.length <=0) )
  {
    showError(thisForm.cannabis_club,"Please select a cannabis club to review.")
    return false;
  }

  if (wRating.length <= 0)
  {
    showError("","Please rate this cannabis club.")
    return false;
  }

  if (wReview.length <= 0)
  {
    showError(thisForm.reviewText,"There is no Review.");
    return false;
  }


  //thisForm.first_name.focus();

  return true;
}

function showError(oFField,imsg)
{
  alert(imsg+"\nPlease try again.");
  if (oFField) oFField.focus();
}


// ***** common functions ******

function isValidEmail(sText)
{
  var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.?)*\w+$/;

  apos=sText.indexOf("@");
  dotpos=sText.lastIndexOf(".");
  if (apos<1||dotpos-apos<2) 
       {return false;}
  return reEmail.test(sText);
}

// ---- Trim a string of leading or trailing spaces or both ----

function sTrim(iString,iFlag)
{
  // iFlag:  L = Leading space(s) removal
  //         T = Trailing space(s) removal
  //         B = Both leading and trailing space(s) removal

  iFlag = iFlag.toUpperCase();
  if ((iFlag == "L") || (iFlag == "B"))
  {
    while (''+iString.charAt(0)==' ')
       iString = iString.substr(1,iString.length);
  }
  if ((iFlag == "T") || (iFlag == "B"))
  {
    while (''+iString.charAt(iString.length-1)==' ')
       iString = iString.substr(0,iString.length-1);
  }

  return iString;
}
