// JavaScript, form, and PHP coded by Nikola Vrtis and Nicholas Vrtis, vrtisjobs@vrtisworks.com, www.vrtisworks.com
// includes an email check script from Sandeep V. Tamhankar (stamhankar@hotmail.com) on javascript.internet.com

function Validate(njv) {

//Everybody needs to have both first & last names
if (document.form.cgrFName.value == "" || document.form.cgrLName.value == "") {
		alert("Please provide both your first and last name.");
		return false; }

if (document.form.cgrType.value == "i") {
  //Info request must have an email or phone, and a message
  var email = document.form.cgrEmail.value;
  if (email != "") {
    if (!emailCheck (email)) {
      return false; } }         //Poorly formed email
  var phone = document.form.cgrPhone.value;
  if (phone != "") {
	if (!phoneCheck (phone)) {
  		return false; } }

  var max=1000;
  if (document.form.cgrNote.value.length > max) {
    alert("Please shorten your message to under 1000 characters and submit again.");
    return false; }
  if (document.form.cgrNote.value.length == 0) {
	alert ("Please enter an information request message in the message area.");
   return false; }
  if (document.form.cgrUpdates[0].checked) {
    if (document.form.cgrEmail.value == "") {
   	alert ("We cannot send you updates without an email address.");
        return false;}}
  if (document.form.cgrEmail.value == "" && document.form.cgrPhone.value == "") {
		alert("We cannot respond to a message without an email address or phone number.");
		return false; }
}

if (document.form.cgrType.value == "b") {

  //From here on, a good address is required.
  if (document.form.cgrAddress1.value == "") {
	alert("Please type in your street address.");
	return false; }
  if (document.form.cgrCity.value == "") {
	alert("Please type in your city.");
	return false; }
  if (document.form.cgrState.options[document.form.cgrState.selectedIndex].value == "XX") {
	alert("Please select your state or province.");
	return false; }
  if (document.form.cgrCountry.options[document.form.cgrCountry.selectedIndex].value == "XX") {
	alert("Please select your country.");
	return false; }
  if (document.form.cgrZip.value == "") {
	alert("Please type in your Zip or postal code.");
	return false; }
  var z = new String(document.form.cgrZip.value);
  var z4 = new String(document.form.cgrZip4.value);
  if (document.form.cgrCountry.options[document.form.cgrCountry.selectedIndex].value == "USA") {
	var valid = "0123456789";
	if (z.length != 5) {
		alert("Please correct your Zip code.");
		return false; }
	for (var i=0; i < z.length; i++) {
		temp = "" + z.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			alert("Please correct your Zip code");
			return false; } } 
	if (document.form.cgrZip4.value != "") {
		if (z4.length != 4) {
			alert("Please correct your Plus 4.");
			return false; }
	for (var i=0; i < z.length; i++) {
		temp = "" + z.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			alert("Please correct your Zip code");
			return false; } } } }
  if (document.form.cgrCountry.options[document.form.cgrCountry.selectedIndex].value == "CAN") {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
	if (z.length != 6) {
		if (z.length != 7) {
			alert("Please correct your postal code.");
			return false;
        }
   }
	for (var i=0; i < z.length; i++) {
		temp = "" + z.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			alert("Please correct your postal code");
			return false;
        }
   } 
	if (document.form.cgrZip4.value != "") {
		alert("There is no need to fill in the Plus 4 box.");
		document.form.cgrZip4.value = "";
		return false;
  }
}
if (document.form.cgrUpdates[0].checked) {
	if (document.form.cgrEmail.value == "") {
   	alert ("We cannot send you updates without an email address.");
        return false;
   }
   else {
		if (!emailCheck (document.form.cgrEmail.value)) {
        	return false;	//Poorly formed email
        }
   }
}
if (document.form.cgrHear.options[document.form.cgrHear.selectedIndex].value == "XX") {
	alert("Please let us know how you found our site.");
	return false; }
}

return true; }

function phoneCheck (phoneStr) {
   var valid = "0123456789";
   var skips = "()-. ";
   var testPhone = "";
   var temp = "";
   var chrsOK = 0;
   for (var i=0; i<phoneStr.length; i++) {	//Strip out all non-numerics
     temp = phoneStr.substring(i, i+1);
     if (skips.indexOf(temp) != -1) {
     } else {
        if (valid.indexOf(temp) != -1) {
        testPhone = testPhone + temp;
        } else {
            chrsOK=1;   //wasn't one of the allowable "junk" and wasn't numeric.
        }
     }
   }
   if (chrsOK == 1) {
       alert("Your phone number has invalid characters, please correct");
       return false;
   }
   if (phoneStr.substring(0,1) == "0") {
       return true;     //0 at start means international.. so any length is OK.
   }
   if (testPhone.substring(0,1) == "1") {   //See if it starts with a '1'
       if (testPhone.length != 11) {        //YES-then it needs to be 1aaaxxxnnnn
           alert("Please correct your phone number");
           return false;}
   } else {
       if (testPhone.length != 10) {        //NO-then it needs to be aaaxxxnnnn
           alert ("Please correct your phone number");
           return false;}
   }
   return true;
}

function emailCheck (emailStr) {
// This script from JavaScript Source!! http://javascript.internet.com
// V1.1.3: Sandeep V. Tamhankar (stamhankar@hotmail.com)
// The following is the list of known TLDs that an e-mail address must end with.
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var checkTLD=1;
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) {
    alert("Please correct your email address.");
    return false;}

var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i<user.length; i++) {
  if (user.charCodeAt(i)>127) {
      alert("Please correct your email address.");
      return false; }}
for (i=0; i<domain.length; i++) {
  if (domain.charCodeAt(i)>127) {
    alert("Please correct your email address.");
    return false; }}
if (user.match(userPat)==null) {
  alert("Please correct your email address.");
  return false; }

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
  for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
      alert("Please correct your email address.");
      return false; }} }

var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
  if (domArr[i].search(atomPat)==-1) {
  alert("Please correct your email address.");
  return false; }}
if (checkTLD && domArr[domArr.length-1].length!=2 && 
  domArr[domArr.length-1].search(knownDomsPat)==-1) {
    alert("Please correct your email address.");
    return false; }
if (len<2) {
  alert("Please correct your email address.");
  return false; }

return true;
}

