var alreadySubmitted = false;

function MM_openBrWindow(theURL,winName,features) { //v2.0
	  window.open(theURL,winName,features);
	  window.focus;
	}

function Trim(inString) {
  var retVal = "";
  var start = 0;
  while ((start < inString.length) && (inString.charAt(start) == ' ')) {
    ++start;
  }
  var end = inString.length;
  while ((end > 0) && (inString.charAt(end - 1) == ' ')) {
    --end;
  }
  
  if(start <= end){
    retVal = inString.substring(start, end);
  }
  
  return retVal;
}//Trim()

function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}//isEmailAddr()

function isValidZip(strZip) {
var valid = "0123456789-";
var hyphencount = 0;

if (strZip.length!=5 && strZip.length!=10) {
  return false;
}
for (var i=0; i < strZip.length; i++) {
  temp = "" + strZip.substring(i, i+1);
  if (temp == "-") hyphencount++;
  if (valid.indexOf(temp) == "-1") {
    return false;
  }
  if ((hyphencount > 1) || ((strZip.length==10) && ""+strZip.charAt(5)!="-")) {
    return false;
  }
}
return true;
}//isValidZip()

function IsNumeric(strString)
{
  var strValidChars = "0123456789";
  var strChar;
  var blnResult = true;
  
  //test strString consists of valid characters listed above
  for (i = 0; i < strString.length && blnResult == true; i++)
  {
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1)
    {
      blnResult = false;
    }
  }
  return blnResult;
}//IsNumeric

function IsValidPhone(areacode,prefix,suffix){
  var phone = areacode.toString() + prefix.toString() + suffix.toString()
  return ((phone.length == 10) && IsNumeric(areacode) && IsNumeric(prefix) && IsNumeric(suffix))
}//IsValidPhone

// BASIC FORM CHECK FUNCTION
function checkForm(theForm) {
  if (alreadySubmitted) {
    return false;
  }
  errMsg = '';
  frontPadding = ' - ';
  divLine = '-----------------------------------------------------------------------------\n'
  
  /* TRIM FORM FIELDS */
  theForm.firstName.value = Trim(theForm.firstName.value);
  theForm.lastName.value = Trim(theForm.lastName.value);
  theForm.address.value = Trim(theForm.address.value);
  theForm.city.value = Trim(theForm.city.value);
  theForm.zip.value = Trim(theForm.zip.value);
  theForm.email.value = Trim(theForm.email.value);
  theForm.phone1.value = Trim(theForm.phone1.value);
  theForm.phone2.value = Trim(theForm.phone2.value);
  theForm.phone3.value = Trim(theForm.phone3.value);
  theForm.comments.value = Trim(theForm.comments.value);
  //theForm.nightphone1.value = Trim(theForm.nightphone1.value);
  //theForm.nightphone2.value = Trim(theForm.nightphone2.value);
 // theForm.nightphone3.value = Trim(theForm.nightphone3.value);

  /* FIRST NAME CHECK */
  if (theForm.firstName.value.length < 1 || theForm.firstName.value.length > 50) errMsg = errMsg + frontPadding + 'First Name\n';
  
  /* LAST NAME CHECK */
  if (theForm.lastName.value.length < 1 || theForm.lastName.value.length > 50) errMsg = errMsg + frontPadding + 'Last Name\n';
  
  /* ADDRESS CHECK */
  if (theForm.address.value.length < 1) errMsg = errMsg + frontPadding + 'Address\n';
  
  /* CITY */
  if (theForm.city.value.length < 1) errMsg = errMsg + frontPadding + 'City\n';
  
  /* STATE CHECK */
  if (theForm.state.selectedIndex < 1) errMsg = errMsg + frontPadding + 'State\n';
   
  /* ZIP CODE CHECK */
  if (!isValidZip(theForm.zip.value)) errMsg = errMsg + frontPadding + 'Zip Code\n';
  
  /* EMAIL CHECK */
  if ( (!isEmailAddr(theForm.email.value))) errMsg = errMsg + frontPadding + 'Email\n';
  
  /* DAYPHONE CHECK */
  if (!IsValidPhone(theForm.phone1.value, theForm.phone2.value, theForm.phone3.value)) errMsg = errMsg + frontPadding + 'Day Phone\n';
  
  /* COMMENTS */
  if (theForm.comments.value.length < 1) errMsg = errMsg + frontPadding + 'Comments\n';
  
  /* NIGHT PHONE CHECK */
  //if (!IsValidPhone(theForm.nightphone1.value, theForm.nightphone2.value, theForm.nightphone3.value)) errMsg = errMsg + frontPadding + 'Night Phone\n';
 
  // ALERT THE USER IF ANY ERRORS WERE GENERATED, OTHERWISE SUBMIT THE FORM
  if (errMsg.length > 0) {
    errMsg =  divLine + 'THE FOLLOWING FIELDS WERE EITHER EMPTY OR INVALID:\n' + divLine + errMsg + divLine
    alert(errMsg);
    return false;
  }
  else {
    alreadySubmitted = true;
    return true;
  }
}
