// JavaScript Document

function validatelname(field) 
{ var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.- "
  var ok = "yes";
  var temp;
  
  for (var i=0; i<field.value.length; i++) 
  { temp = "" + field.value.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") ok = "no";
  }
  if (ok == "no") 
  { alert("Last Name entry invalid! Only letters, periods and hyphens are accepted!");
	field.focus();
	field.select();
  }
}

function validatemember(field) 
{ var valid = "0123456789"
  var ok = "yes";
  var temp;
  
  for (var i=0; i<field.value.length; i++) 
  { temp = "" + field.value.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") ok = "no";
  }
  if (ok == "no") 
  { alert("Member ID entry invalid! Only numbers are accepted!");
	field.focus();
	field.select();
  }
  if (!(field.value.length == 9))
  { alert("Member ID must be 9 numbers long!");
	field.focus();
	field.select();
  }
}


function ValidateCWFContact() 
{ var nl			= '\n';
  var tElem, tGrp, elem	= 0;
  var alert_msg 		= new Object();
  if(document.cwfform.name.value == '')
  { alert('______________________________' + nl + 'Notice:' + nl + 'Please fill out the *Name* field.' + nl + 'Thank you!' + nl + '______________________________');
    document.cwfform.name.focus() == true
	return false; }
  if(document.cwfform.organization.value == '')
  { alert('_____________________________' + nl + 'Notice:' + nl + 'Please fill out the *Organization* field.' + nl + 'Thank you!' + nl + '_____________________________');
	document.cwfform.organization.focus() == true
	return false; }
  if(document.cwfform.email.value == '')
  { alert('_______________________________' + nl + 'Notice:' + nl + 'Please fill out the *Email Address* field.' + nl + 'Thank you!' + nl + '_______________________________');
	document.cwfform.email.focus() == true
	return false; }
  if(document.cwfform.address.value == '')
  { alert('_______________________________' + nl + 'Notice:' + nl + 'Please fill out the *Mailing Address* field.' + nl + 'Thank you!' + nl + '_______________________________');
	document.cwfform.address.focus() == true
	return false; }
  if(document.cwfform.city.value == '')
  { alert('____________________________________' + nl + 'Notice:' + nl + 'Please fill out the *City* field.' + nl + 'Thank you!' + nl + '____________________________________');
	document.cwfform.city.focus() == true
	return false; }
  if(document.cwfform.zip.value == '')
  { alert('________________________________' + nl + 'Notice:' + nl + 'Please fill out the *Zip Code:* field.' + nl + 'Thank you!' + nl + '________________________________');
	document.cwfform.zip.focus() == true
	return false; }
  return true;
}

function isEmpty(str)
{ return (str == null) || (str.length == 0); }

// returns true if the string is a valid email
function isEmail(str)
{ if(isEmpty(str)) return false;
  var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
  return re.test(str);
}

// returns true if the string only contains characters A-Z or a-z
function isAlpha(str)
{ var re = /[^a-zA-Z]/g
  if (re.test(str)) return false;
  return true;
}

// returns true if the string only contains characters 0-9
function isNumeric(str)
{ var re = /[\D]/g
  if (re.test(str)) return false;
  return true;
}
// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str)
{ var re = /[^a-zA-Z0-9]/g
  if (re.test(str)) return false;
  return true;
}

// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str)
{ var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
  return re.test(str);
}

function validatethecity(field) 
{ var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.- "
  var ok = "yes";
  var temp;
  
  for (var i=0; i<field.value.length; i++) 
  { temp = "" + field.value.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") ok = "no";
  }
  if (ok == "no") 
  { alert("City entry invalid! Only letters, periods and hyphens are accepted!");
	return false;
  }
}

function validatethenumber(field) 
{ var valid = "0123456789.- "
  var ok = "yes";
  var temp;
  
  for (var i=0; i<field.value.length; i++) 
  { temp = "" + field.value.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") ok = "no";
  }
  if (ok == "no") 
  { alert("Phone Number entry invalid! Only numbers, periods and hyphens are accepted!");
	return false;
  }
}

function validateZIP(field) 
{ var valid = "0123456789-";
  var hyphencount = 0;
  if (field.length!=5 && field.length!=10) 
  { alert("Please enter your 5 digit or 5 digit+4 zip code.");
	return false; }
  for (var i=0; i < field.length; i++) 
  { temp = "" + field.substring(i, i+1);
	if (temp == "-") hyphencount++;
	if (valid.indexOf(temp) == "-1") 
	{ alert("Invalid characters in your zip code.  Please try again.");
	  return false; }
	if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) 
	{ alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
	  return false; }
  }
  return true;
}

function tocontact()
{ if(document["contact"]["iscontact"].checked)
  { document.getElementById("contacted").style.visibility="visible" 
    emailCheck(document.contact.email)
  }
  else{ document.getElementById("contacted").style.visibility="hidden" }
}

function checkquick(themail)
{ if(document["contact"]["iscontact"].checked)
  { if(emailCheck(themail))
  	{ return true }
	else{ return false }
  }
  return true
}

function emailCheck (emailStr) 
{ var arr = new Array('.com','.net','.org','.biz','.coop','.info','.museum','.name', '.pro','.edu','.gov','.int','.mil','.us','.ws');
  var mai = emailStr;
  var val = true;
  var dot = mai.lastIndexOf(".");
  var dname = mai.substring(0,dot);
  var ext = mai.substring(dot,mai.length);
  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("Email address seems incorrect (check @ and .'s)")
	return false
  }
  var user=matchArray[1]
  var domain=matchArray[2]
  if (user.match(userPat)==null) 
  { alert("The part of your email address before the '@' doesn't seem to be valid.")
    return false
  }
  var IPArray=domain.match(ipDomainPat)
  if (IPArray!=null) 
  { for (var i=1;i<=4;i++) 
    { if (IPArray[i]>255) 
	  { alert("Destination IP address is invalid!")
		return false
	  }
    }
    return true
  }
  var domainArray=domain.match(domainPat)
  if (domainArray==null) 
  { alert("Part of your email address after the '@' doesn't seem to be valid")
    return false
  }
  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>6) 
  { alert("The address must end in a top level domain (e.g. .com), or two letter country.")
    return false
  }
  if (len<2) 
  { var errStr="This address is missing a hostname!"
    alert(errStr)
	return false
  }
  
  if(dot>2 && dot<57)
  { for(var i=0; i<arr.length; i++)
	{ if(ext == arr[i])
	  { val = true;
		break; }	
	  else
	  { val = false; }
	}
	if(val == false)
	{ alert("Your domain extension "+ext+" is not correct");
	  return false;
	}
  }
  else
  { alert("Your Domain name is too short/long");
	return false;
  }
  return true;
}