function validateForm(thisForm) {
  errString = '';
  if (noText(thisForm.Name)) {
    errString = errString + '\tName\n';
  }
  if (noText(thisForm.Phone)) {
    errString = errString + '\tPhone\n';
  }
  if (noText(thisForm.Email)) {
    errString = errString + '\tEmail\n';
  } else {
    errString = errString + emailCheck(thisForm.Email.value);
  }
  
// If there were any errors, display the alert box
  if (errString.length > 0) {
    alert("Please fill in the following fields: \n" + errString);
    return false;
  } else {
    return true;
  }
}

// For Required input fields
function noText(thisElement) {
  // Validate text boxes and textarea
  if (gobbleWhiteSpace(thisElement.value)) {
    return false;
  }
  return true;
}

// For Required select fields
function nothingSelected(thisElement) {
  // Validate single and multiple drop down boxes
  if(thisElement.options.type == 'select-one') {
    if(thisElement.options[thisElement.options.selectedIndex].value) {
      return false;
    }
  } else if(thisElement.options.type == 'select-multiple') {
    for (var x=0;x<thisElement.options.length;x+=1){
      if (thisElement.options[x].selected && thisElement.options[x].value) {
        return false;
      }
    }
  } else if(thisElement.options[thisElement.options.selectedIndex].value) {
    // Netscape 6 check. Seems to work for single drop downs, may
    // barf on multiple.
    return false;
  }
 return true;
}

function gobbleWhiteSpace(textstring){
  // Get rid of whitespace from both ends of text string
  var lastChar, counter=0;
  var whiteSpaceChars = " \n\r\f\t";
 
  //gobble whitespace from beginning of string
  while ( textstring && whiteSpaceChars.indexOf( textstring.charAt(0) ) !=  -1 ){
    textstring = textstring.substring(1);
  }
 
  //gobble whitespace from end of string
  lastChar = textstring.length-1;
  while ( textstring && whiteSpaceChars.indexOf( textstring.charAt(lastChar) ) != -1 ){
     textstring = textstring.substring(0,lastChar--);
  }
  return textstring;  
} 

function emailCheck (emailStr) {
  // Validate an email address format
  var emailPat=/^(.+)@(.+)$/
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
  var validChars="\[^\\s" + specialChars + "\]"
  var quotedUser="(\"[^\"]*\")"
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
  var atom=validChars + '+'
  var word="(" + atom + "|" + quotedUser + ")"
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
  var matchArray=emailStr.match(emailPat)

  if (matchArray==null) {
    return "\tEmail address appears invalid -- please check @ and .'s\n";
  }

  var user = matchArray[1]
  var domain = matchArray[2]

  // See if "user" is valid 
  if (user.match(userPat)==null) {
    // user is not valid
    return "\tEmail address -- username doesn't seem to be valid.\n";
  }

  var IPArray=domain.match(ipDomainPat)
  if (IPArray!=null) {
    // this is an IP address
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
        return "\tEmail address -- Destination IP address appears invalid.\n";
      }
    }
    return "";
  }

  // Domain is symbolic name
  var domainArray=domain.match(domainPat)
  if (domainArray==null) {
    return "\tEmail address -- domain name doesn't seem to be valid.\n";
  }
  
  /* domain name seems valid, but now make sure that it ends in a
     three-letter word (like com, edu, gov) or a two-letter word,
     representing country (uk, nl), and that there's a hostname preceding 
     the domain or country. */
  
  /* Now we need to break up the domain to get a count of how many atoms
     it consists of. */
  var atomPat=new RegExp(atom,"g")
  var domArr=domain.match(atomPat)
  var len=domArr.length
  if (domArr[domArr.length-1].length<2 || 
      domArr[domArr.length-1].length>3) {
        // the address must end in a two letter or three letter word.
        return "\tEmail address -- must end in a three-letter domain, or two letter country code.\n";
  }

  // Make sure there's a host name preceding the domain.
  if (len<2) {
    return "\tEmail address -- missing a hostname\n";
  }
  return "";
}

//function crapAble() {
//	if (document.quoteForm.craps.checked) {
//		document.quoteForm.crapsdeal.disabled = false;
//	} else {
//		document.quoteForm.crapsdeal.disabled = true;
//	}
//}


