
function checkMemberProfileForm() {
/*******************************************************************************
  Function: 
--------------------------------------------------------------------------------
	 INPUT: form element values, accessed via DOM
	OUTPUT: error message if applicable
*******************************************************************************/

	/* If any error condition evaluates to true, sentinel 
	 * variable "dataValid" fill be flipped to 'false' and 
	 * no further processing will be done after validations */	

	// initialize variables
	var errors = "";
	var dataValid = true; 

	// Check for empty fields

	/*
	if (document.profile.fname.value == "") {
		dataValid = false;
		errors = errors +"\n » First Name";}

	if (document.profile.lname.value == "") {
		dataValid = false;
		errors = errors +"\n » Last Name";}

	if (document.profile.street.value == "") {
		dataValid = false;
		errors = errors +"\n » Street";}

	if (document.profile.city.value == "") {
		dataValid = false;
		errors = errors +"\n » City";}

	if (document.profile.state.value == "") {
		dataValid = false;
		errors = errors +"\n » State";}

	if (document.profile.postal.value == "") {
		dataValid = false;
		errors = errors +"\n » Zip Code";}

	if (document.profile.phone.value == "") {
		dataValid = false;
		errors = errors +"\n » Phone Number";}
*/
	// data validation is complete
	// of data not valid provide friendly error message
	if (!dataValid) {
		alert("We're sorry, one or more of the following \nitems was not entered or is invalid;\n" 
			+ errors + "\n\nPlease correct and re-submit your information.\n ");
		return false;
	}
}





function removeSpaces(string) {
/*******************************************************************************
  Function: 
--------------------------------------------------------------------------------
	 INPUT: form element values, accessed via DOM
	OUTPUT: input string with spaces stripped
*******************************************************************************/
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}


function comparePasswords() {
/*******************************************************************************
  Function: 
--------------------------------------------------------------------------------
	 INPUT: form element values, accessed via DOM
	OUTPUT: Alert box pop-up message
*******************************************************************************/

	if (document.profile.password1.value != document.profile.password2.value) {
		alert("Sorry,\n\nThe password and confirmation do not match.\n\nPlease correct your information.\n ");
	}

}


function validatePostal() {
/*******************************************************************************
  Function: validatePostal()
			validates a zip code in 5 digit or 5+4 format
			or a canadian format A1A 1A1

	NOTE: To satisfy the Canadian postal code format;
		The first alpha character must be one of:   ABCEGHJKLMNPRSTVXY
		Subsequent alpha characters must be one of: ABCEGHJKLMNPRSTVWXYZ
--------------------------------------------------------------------------------
	 INPUT: string value to be tested for validity, captured from form
	OUTPUT: true if value, false if not
*******************************************************************************/
	// initialize variables
	var isValid = false; // Sentinel variable: set to true on pattern match

	// establish RegExp strings for pattern matching
	var patternCA = /^[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]$/i;
	var patternUS = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	var patternSp = /\s/g; // identifies spaces
	// alternative pattern for CA code - not used: /^([a-zA-Z]\d{3}$/
	// capture string and prepare for pattern matching
	var strPostal = document.profile.postal.value;		// capture postal code 
	strPostal.toUpperCase(); 						// convert to uppercase
	strPostal = strPostal.replace(patternSp,"");	// strip spaces

	// ~~~~~~~~~~~~~ Check for Regular Expression Pattern Match ~~~~~~~~~~~~~~//

	// test for canadian postal code pattern
	if (patternCA.test(strPostal)) {
		// flip sentinel variable
		isValid = true;
		// insert space in middle
		strPostal = strPostal.substring(0,3) + " " + strPostal.substring(3,6);
		// convert to uppercase
		strPostal = strPostal.toUpperCase();
		// write cleaned up value back to form
		document.profile.postal.value = strPostal;
	}

	// test for american zip code pattern
	if (patternUS.test(strPostal)) {
		// flip sentinel variable
		isValid = true;
		// write cleaned up value back to form
		document.profile.postal.value = strPostal;
	}

	// code to execute if postal code did not validate
	if (!isValid) {
		// notify user with a pop-up
		alert("We're Sorry;\n\nThe postal code you entered does not appear \n"+
			"to be a valid Canadian or American Postal code.\n\n"+
			"Please enter a valid postal code and re-submit.");
		//return focus to the field for editing
		document.profile.postal.focus();
	}
}

function validatePhone() {
/*******************************************************************************
  Function: validatePhone()
			evaluate phone number entered to ensure that is matches a valid
			north american format and that the area code has been included.
-------------------------------------------------------------------------------
	 INPUT: data to be tested for validity, captured from form
	OUTPUT: message to user if data entered does not match regex pattern
*******************************************************************************/

	// alert("inside validatePhone");
	
	// ~~ now format as (###) ###-#### ~~ //
	// capture phone # from form
	var strPhone = document.profile.phone.value;
	// strip "()- " characters
	var pattern = /\(/g; // identifies "("
		strPhone = strPhone.replace(pattern,"");
	var pattern = /\)/g; // identifies ")"
		strPhone = strPhone.replace(pattern," ");
	var pattern = /\s/g; // identifies spaces
		strPhone = strPhone.replace(pattern,"");
	var pattern = /[-]/g; // identifies hyphens
		strPhone = strPhone.replace(pattern,"");
	// data is now ready to be tested with regex

	// ~~ establish RegExp strings for pattern matching ~~ //
	var patternPh = /^\d{10}$/; // cleaned up string must now be a string of 10 numeric characers
	

	// test the phone # entered against the regex
//	if (!patternPh.test(document.profile.phone.value)) {
	if (!patternPh.test(strPhone)) {
		// alert user if data did not validate
		alert("We're Sorry;\n\nThe phone number you entered does \n"+
			"not appear to be valid.\n\n"+
			"Please enter a valid phone number and re-submit.");
		//return focus to the field for editing
		document.profile.phone.focus();
		document.profile.phone.focus();
	} else {
	
		// format number as (###) ###-####
		strPhone = "(" + strPhone.substring(0,3) + ") " + strPhone.substring(3,6)+ "-" + strPhone.substring(6,10);
		// write formatted string back to form
		 document.profile.phone.value = strPhone;		
	}


	// the regex strings below were used prior to mofifying the code to 
	// strip the "()- " characters before testing the number - retained here for reference
			// flexible 
			//	var patternPh = /^\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/;
			// strict
			//	var patternPh = /^(\(\d{3}\)\d{3}[-]\d{4})|(\d{3}[-]\d{3}[-]\d{4})|(\d{3}[\s]\d{3}[\s]\d{4})$/;
			//	var patternPh = /^(\(\d{3}\)\d{3}[-]\d{4})$/|/^(\d{3}[-]\d{3}[-]\d{4})$/|/^(\d{3}[\s]\d{3}[\s]\d{4})$/;

	
}




function validatePhone2(form,field) {
/*******************************************************************************

	NOTE: using this to test passing the formname and fieldname so that one script
	      can be used with all phone number fields

  Function: validatePhone2()
			evaluate phone number entered to ensure that is matches a valid
			north american format and that the area code has been included.
-------------------------------------------------------------------------------
	 INPUT: data to be tested for validity, captured from form
	OUTPUT: message to user if data entered does not match regex pattern
*******************************************************************************/

	//alert("inside validatePhone2 with " + form + " and " + field);


	// ~~ establish RegExp strings for pattern matching ~~ //
	// flexible 
	var patternPh = /^\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/;
	// strict
	//	var patternPh = /^(\(\d{3}\)\d{3}[-]\d{4})|(\d{3}[-]\d{3}[-]\d{4})|(\d{3}[\s]\d{3}[\s]\d{4})$/;
	//	var patternPh = /^(\(\d{3}\)\d{3}[-]\d{4})$/|/^(\d{3}[-]\d{3}[-]\d{4})$/|/^(\d{3}[\s]\d{3}[\s]\d{4})$/;

	// test the phone # entered against the regex
	if (!patternPh.test(document.form.field.value)) {
		// alert user if data did not validate
		alert("We're Sorry;\n\nThe phone number you entered does \n"+
			"not appear to be valid.\n\n"+
			"Please enter a valid phone number with \narea code and re-submit.");
		//return focus to the field for editing
		document.form.field.focus();
		document.form.field.focus();
	}
}



function validatePhoneh() {
/*******************************************************************************
  Function: validatePhone()
			evaluate phone number entered to ensure that is matches a valid
			north american format and that the area code has been included.
-------------------------------------------------------------------------------
	 INPUT: data to be tested for validity, captured from form
	OUTPUT: message to user if data entered does not match regex pattern
*******************************************************************************/

//	alert("inside validatePhoneh");
	// ~~ now format as (###) ###-#### ~~ //
	// capture phone # from form
	var strPhone = document.profile.$attribute.value;
	// strip "()- " characters
	var pattern = /\(/g; // identifies "("
		strPhone = strPhone.replace(pattern,"");
	var pattern = /\)/g; // identifies ")"
		strPhone = strPhone.replace(pattern," ");
	var pattern = /\s/g; // identifies spaces
		strPhone = strPhone.replace(pattern,"");
	var pattern = /[-]/g; // identifies hyphens
		strPhone = strPhone.replace(pattern,"");
	// data is now ready to be tested with regex

	// ~~ establish RegExp strings for pattern matching ~~ //
	var patternPh = /^\d{10}$/; // cleaned up string must now be a string of 10 numeric characers
	

	// test the phone # entered against the regex
//	if (!patternPh.test(document.profile.phoneh.value)) {
	if (!patternPh.test(strPhone)) {
		// alert user if data did not validate
		alert("We're Sorry;\n\nThe phone number you entered does \n"+
			"not appear to be valid.\n\n"+
			"Please enter a valid phone number and re-submit.");
		//return focus to the field for editing
		document.profile.phoneh.focus();
		document.profile.phoneh.focus();
	} else {
	
		// format number as (###) ###-####
		strPhone = "(" + strPhone.substring(0,3) + ") " + strPhone.substring(3,6)+ "-" + strPhone.substring(6,10);
		// write formatted string back to form
		 document.profile.phoneh.value = strPhone;		
	}


	// the regex strings below were used prior to mofifying the code to 
	// strip the "()- " characters before testing the number - retained here for reference
			// flexible 
			//	var patternPh = /^\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/;
			// strict
			//	var patternPh = /^(\(\d{3}\)\d{3}[-]\d{4})|(\d{3}[-]\d{3}[-]\d{4})|(\d{3}[\s]\d{3}[\s]\d{4})$/;
			//	var patternPh = /^(\(\d{3}\)\d{3}[-]\d{4})$/|/^(\d{3}[-]\d{3}[-]\d{4})$/|/^(\d{3}[\s]\d{3}[\s]\d{4})$/;

	
}


function validatePhoneo() {
/*******************************************************************************
  Function: validatePhone()
			evaluate phone number entered to ensure that is matches a valid
			north american format and that the area code has been included.
-------------------------------------------------------------------------------
	 INPUT: data to be tested for validity, captured from form
	OUTPUT: message to user if data entered does not match regex pattern
*******************************************************************************/

	alert("inside validatePhoneo");
	// ~~ now format as (###) ###-#### ~~ //
	// capture phone # from form
	var strPhone = document.profile.phoneo.value;
	// strip "()- " characters
	var pattern = /\(/g; // identifies "("
		strPhone = strPhone.replace(pattern,"");
	var pattern = /\)/g; // identifies ")"
		strPhone = strPhone.replace(pattern," ");
	var pattern = /\s/g; // identifies spaces
		strPhone = strPhone.replace(pattern,"");
	var pattern = /[-]/g; // identifies hyphens
		strPhone = strPhone.replace(pattern,"");
	// data is now ready to be tested with regex

	// ~~ establish RegExp strings for pattern matching ~~ //
	var patternPh = /^\d{10}$/; // cleaned up string must now be a string of 10 numeric characers
	

	// test the phone # entered against the regex
//	if (!patternPh.test(document.profile.phone.value)) {
	if (!patternPh.test(strPhone)) {
		// alert user if data did not validate
		alert("We're Sorry;\n\nThe phone number you entered does \n"+
			"not appear to be valid.\n\n"+
			"Please enter a valid phone number and re-submit.");
		//return focus to the field for editing
		document.profile.phoneo.focus();
		document.profile.phoneo.focus();
	} else {
	
		// format number as (###) ###-####
		strPhone = "(" + strPhone.substring(0,3) + ") " + strPhone.substring(3,6)+ "-" + strPhone.substring(6,10);
		// write formatted string back to form
		 document.profile.phoneo.value = strPhone;		
	}


	// the regex strings below were used prior to mofifying the code to 
	// strip the "()- " characters before testing the number - retained here for reference
			// flexible 
			//	var patternPh = /^\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/;
			// strict
			//	var patternPh = /^(\(\d{3}\)\d{3}[-]\d{4})|(\d{3}[-]\d{3}[-]\d{4})|(\d{3}[\s]\d{3}[\s]\d{4})$/;
			//	var patternPh = /^(\(\d{3}\)\d{3}[-]\d{4})$/|/^(\d{3}[-]\d{3}[-]\d{4})$/|/^(\d{3}[\s]\d{3}[\s]\d{4})$/;

	
}


function toggleDiv(divid){
	/*******************************************************************************
	  Function: toggleDiv()
				toggle visibility of <div> elements
	-------------------------------------------------------------------------------
		 INPUT: id of <div> to be shown
		OUTPUT: visibility behaviour of <div> element
	-------------------------------------------------------------------------------
	  Source: http://www.harrymaugans.com/2007/03/05/how-to-create-a-collapsible-div-with-javascript-and-css/
	See Also: http://www.brainjar.com/css/tabs/default3.asp
	          http://nontroppo.org/test/tab1.html
						http://www.alistapart.com/articles/eatcake (on hiding the other divs)
	
	*******************************************************************************/
	// alert(divid);
	if(document.getElementById(divid).style.display == 'none'){
		document.getElementById(divid).style.display = 'block';
	}else{
		document.getElementById(divid).style.display = 'none';
	}
}




 function checkAll(checkname, exby) {
	/*******************************************************************************
	  Function: checkAll()
				Check/Clear all checkboxes in a list
	-------------------------------------------------------------------------------
		OUTPUT: state change in checkbox controls
	-------------------------------------------------------------------------------
	  Source: 
	*******************************************************************************/
	var action = (exby.checked)? true : false;
	var checkname = exby.form.elements[checkname];
	for (i = 0; i < checkname.length; i++) {
		checkname[i].checked = action; 
   }
 }

