function validate(x) {
 var problem=false;
 var phone=checkPhone(x.phone.value);
 var alertBox="";
 if (trim(x.uName.value)=="") { alertBox+="Please enter your desired user name.\n"; problem=true; }
 if (trim(x.uPW2.value)!="") { alertBox+="Please enter your desired password.\n"; problem=true; }
 if (trim(x.fname.value)=="") { alertBox+="Please enter your first name.\n"; problem=true; }
 if (trim(x.lname.value)=="") { alertBox+="Please enter your last name.\n"; problem=true; }
 problem=checkEmail(x.email.value);
 if (trim(x.addr1.value)=="") { alertBox+="Please enter your street address.\n"; problem=true; }
 if (trim(x.city.value)=="") { alertBox+="Please enter your city.\n"; problem=true; }
// if (trim(x.st.value)=="") { alertBox+="Please select your state.\n"; problem=true; }
 if (trim(x.zip.value)=="") { alertBox+="Please enter your zip/postal code.\n"; problem=true; }
 if (trim(x.country.value)=="") { alertBox+="Please select your country.\n"; problem=true; }
 if (problem==true) { alert(alertBox); }
 if (x.uPW2.value!=x.uPW3.value) { alert("Your passwords do not match. Please re-enter your password."); problem=true; }
 if (x.eighteen!=null) { if (x.eighteen[1].checked) { alert("You must be at least 18 years old to register for this site."); problem=true; } }
 return !problem;
}

function trim(str) { return str.replace(/^\s+|\s+$/g, ''); }

function permission() {
 var r=document.form1;
 var pStr ="You have chosen to grant the Arizona-Sonora Desert Museum pemission to use ";
     pStr+="all of your contributed high resolution images for Museum uses beyond the ";
     pStr+="Digital Library. You will find a special license contract via the \"Contracts\" link ";
     pStr+="on the left navigation bar. Please sign this form and mail it to the address on the form.\n";
     pStr+="\nThank you.";
 var permission=confirm(pStr);
 if (permission==false) r.Use_All[0].checked=true;
}

function checkEmail(x) {
   var em1=/^([a-z0-9]+([_\.-]?[a-z0-9_-]+)*)+@[a-z0-9]+([\.-][a-z0-9]+)*\.[a-z]{2,4}$/i;
   if (x=="") {
    alert("Please enter your email address.");
    return true;
   } else if (!em1.test(x)) {
    alert("The email address you entered is invalid. Please enter a valid email address and try again.");
    return true;
   }
   return false;
}

function checkPhone(x) {
 x=trim(x);
 if (x!="") {
  var nac=/^(\d\d\d)[ |\.|-]?(\d\d\d\d)$/i;
  if (nac.test(x)) {
   x="(520) "+x;
  }
  var ac=/^\(?(555)\)?[ |\.|-]?(\d\d\d)[ |\.|-]?(\d\d\d\d)$/i;
  if (ac.test(x)) {
   alert("555 is not a valid area code.");
   return true;
  } else {
   var p=/^($|\(?(\d\d\d)\)?[ |\.|-]?(\d\d\d)[ |\.|-]?(\d\d\d\d)$)/i;
   if (!p.test(x)) {
    alert("Please re-enter your phone number using the correct format of (555) 555-5555.");
    return true;
   } else {
    x=x.replace(p, "($2) $3-$4");
    //alert(x);
    document.form1.phone.value=x;
   }
  }
 }
 return false;
}

function checkURL(x) {
 x=trim(x);
 var u=/^($|((https?:\/\/)?[a-z0-9]+([\.-][a-z0-9]+)*\.[a-z]{2,4})(\/[a-z0-9\.\?&#%~\+=@\-]*)*$)/i;
 if (!u.test(x)) {
  alert("The homepage you entered is not a properly formatted URL. Please enter a valid URL and try again.");
  return true;
 }
 return false;
}