
// JavaScript functions to show/hide information

function showInfo(choice){
	document.getElementById(choice).style.visibility="visible";
}

function hideInfo(choice){
	document.getElementById(choice).style.visibility="hidden";

}
	
//JavaScript functions for validating form

function validData(){
	
	var error_string = "";
	

	// check the name fields
	if ((document.members_form.first_name.value == '')|| (document.members_form.first_name.value.length <=1)){
		error_string += "Missing or invalid first name.\n";
	}
	
	if ((document.members_form.last_name.value == '')|| (document.members_form.last_name.value.length <=1)){
		error_string += "Missing or invalid last name.\n";
	}
	
	//check address field
	if ((document.members_form.address1.value =='')||(document.members_form.address1.value.length <=1)){
		error_string += "Missing or invalid address.\n";
	}
	
	//check city field
	if ((document.members_form.city.value =='')||(document.members_form.city.value.length <=1)){
		error_string += "Missing or invalid city.\n";
	}
	//check county field
	if ((document.members_form.county.value =='')||(document.members_form.county.value.length <=1)){
		error_string += "Missing or invalid county.\n";
	}	
	//check state information
	if(document.members_form.state.selectedIndex <0){
		error_string += "Missing or invalid state.\n";
	}
	
	//check zipcode
	
		if (checkZip(document.members_form.zip.value)== true){
			error_string += "Missing or invalid zipcode.\n";
		}
		
	
	//check phone
		
		if (checkPhone(document.members_form.phone.value) == true){
			error_string += "Missing or invalid phone number.\n";	
		}
	
	//check e-mail
		
		if (checkEmail(document.members_form.email.value) == true){
			error_string += "Missing or invalid e-mail address.\n";	
		}
	
	//check website
	
		if ((document.members_form.website.value != "") && (isURL(document.members_form.website.value) == true)){
			error_string +="Invalid website address.\n";
		}
	
	//check dues radio buttons
	
	var rad = "no";
	

	for (var loop = 0; loop < document.members_form.dues.length; loop++){
		if(document.members_form.dues[loop].checked == true){
		
			rad = "yes";
		}
	}
	
	if (rad == "no"){
		error_string += "You must choose a type of membership: Owner or Professional.\n";
	}
	
	//check communication method radio buttons
		
		var radCom = "no";
		
	
		for (var loop = 0; loop < document.members_form.communicationMethod.length; loop++){
			if(document.members_form.communicationMethod[loop].checked == true){
			
				radCom = "yes";
			}
		}	
		if (radCom == "no"){
			error_string += "Please tell us how best to get in contact with you--phone, email, fax or mail.";
	}
	
	
	if(error_string ==""){
		return true;
	}
	else{
		error_string = "We found the following omissions in your form: \n" +error_string;
		alert(error_string);
		return false;
	}
}
	
	
	
function checkEmail(addy){

	var emailFilter = /^[a-z][\w\.]*@[\w\.]+\.[a-z]{2,3}/i;
	var illegalChars = /[\(\)\<\>\,\;\:\\\"[\]]/;
	
	
	if((addy == "") || (!(emailFilter.test(addy))) || (addy.match(illegalChars))){
		return true;
	}
}

function checkZip(zipcode){
	var i;
	var legalChars = "0123456789()-+ " 
	
	if((zipcode=="") || (!(zipcode.length<=10))|| (!(zipcode.length>=5))){
		return true;
	}
	
	for (i =0; i <= zipcode.length -1; i++) {
		if (legalChars.indexOf(zipcode.charAt(i)) == -1) {
		
		return true;
		}
	}
}
		
function checkPhone(numString){
	
	var i;
	var legalChars = "0123456789()-+ " 
	
	if((numString=="") || (numString<7) || (numString >19)){
		return true;
	}
	
	for (i =0; i <= numString.length -1; i++) {
		if (legalChars.indexOf(numString.charAt(i)) == -1) {
		
		return true;
		}
	}
}


function isURL (url) {
 	 var urlPattern = /^(?:(?:ftp|https?):\/\/)?(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)+(?:com|edu|biz|org|gov|int|info|mil|net|name|museum|coop|aero|[a-z][a-z])\b(?:\d+)?(?:\/[^;"'<>()\[\]{}\s\x7f-\xff]*(?:[.,?]+[^;"'<>()\[\]{}\s\x7f-\xff]+)*)?/i;

	 if (!(urlPattern.test(url))){
		 return true;
	 }
}

