// used in apps/view/membership/frmLevel
function validateMembershipLevel(strForm) {	
	var emptyFields = "";
	var i = 0;
	var	isOneChecked = false;
	var theForm = document.getElementById(strForm);
	//alert(theForm + "\n" + strForm);
	if (strForm == "individual" || strForm == "friend" || strForm ==  "family" || strForm ==  "senior" || strForm == "contributing" ) {
		for (i=0; i < theForm.costDuration.length; i++) {
			if (theForm.costDuration[i].checked) {
				isOneChecked = true;
				break;
			}
		}
		if (isOneChecked == false) {
			emptyFields += "Please select a membership duration." + "\n" + "i.e. 1 year or 2 years";
		}
	}
	
	if (strForm == "supporting") {
		membershipAmount = theForm.cost.value;
		if (isCurrency(membershipAmount)) {
			if (membershipAmount < 250) {				   
			   emptyFields += "A Supporting Membership must be at least $250." + "\n" + "Please update the value." + "\n" ;
		   } else if (membershipAmount >= 500) {
			   emptyFields += "A Supporting Membership of more than $499 qualifies as a Sustaining Member." + "\n" + "Please select a Sustaining membership." + "\n" ;
		   }
			
		} else  {
			emptyFields += "Please enter a dollar amount." + "\n" + "Use the format 250.00" ;
		}		
	}
	
	if (strForm == "sustaining") {
		membershipAmount = theForm.cost.value;
		if (isCurrency(membershipAmount)) {
			if (membershipAmount < 500) {				   
			   emptyFields += "A Supporting Membership must be at least $500." + "\n" + "Please either update the value or select another membership." + "\n" ;
		   } 			
		} else  {
			emptyFields += "Please enter a dollar amount." + "\n" + "Use the format 250.00" ;
		}		
	}	
	
	if (emptyFields.length >= 1) {
		alert(emptyFields);
		return false;
	} else {
		theForm.submit();
		return true;
	}
}

function isCurrency(txtValue) {
  isValidCurrency = RegExp(/^[0-9\,]+(\.\d{2})?$/).test(String(txtValue).replace(/^\s+|\s+$/g, ""));
  return isValidCurrency;
}

function validateMembershipOptions() {
	var emptyFields = "";
	var i = 0;
	var	isOneChecked = false;
	var theForm = document.membershipOptions;	
	for (i=0; i < theForm.membershipRenewal.length; i++) {
		if (theForm.membershipRenewal[i].checked) {
			isOneChecked = true;
			break;
		}
	}
	if (isOneChecked == false) {
		emptyFields += "What kind of membership is this?" + "\n";
	}
	
	var	isOneChecked = false;
	for (i=0; i < theForm.isGift.length; i++) {
		if (theForm.isGift[i].checked) {
			isOneChecked = true;
			break;
		}
	}
	if (isOneChecked == false) {
		emptyFields += "Is this membership is being offered as a gift?" + "\n";
	}
	

	var	isOneChecked = false;	
	if (theForm.isGift[1].checked) {

		if (theForm.fname.value == "") {
			emptyFields += "First Name" + "\n" ;
		}
		if (theForm.lname.value == "") {
			emptyFields += "Last Name" + "\n" ;
		}
		if (theForm.address1.value == "") {
			emptyFields += "Address" + "\n" ;
		}
		if (theForm.city.value == "") {
			emptyFields += "City" + "\n" ;
		}		
		if (theForm.postal.value == "") {
			emptyFields += "Postal/Zip code" + "\n" ;
		}	
		if (theForm.countryId.selectedIndex == "") {
			emptyFields += "Country" + "\n" ;
		}	
		if (theForm.provId.selectedIndex == "" & (theForm.countryId.selectedIndex == 1 || theForm.countryId.selectedIndex == 2 || theForm.countryId.selectedIndex == 229)) {
			emptyFields += "Province/State" + "\n" ;
		}
		if (! validateInternationalPhone(theForm.phone.value)) {
			emptyFields += "Phone Number (including area code)" + "\n" ;
		}		
	}

	
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		theForm.submit();
		return true;
	}	
}

function validatePersonalInfo() {
	var emptyFields = "";	
	var theForm = document.personalInfo;
	var isOneChecked = false;
	
	if (theForm.formType.value == "donate") {
		for (i=0; i < theForm.donation.length; i++) {
			if (theForm.donation[i].checked) {
				isOneChecked = true;
				break;
			}
		}
		if (isOneChecked == false) {
			if (theForm.donation[4].value == "" || !isCurrency(theForm.donation[4].value)) {
				emptyFields += "Donation amount" + "\n";
			}
		}			
	}
	
		if (theForm.fname.value == "") {
			emptyFields += "First Name" + "\n" ;
		}
		if (theForm.lname.value == "") {
			emptyFields += "Last Name" + "\n" ;
		}	
		if (theForm.address1.value == "") {
			emptyFields += "Address" + "\n" ;
		}		
		if (theForm.city.value == "") {
			emptyFields += "City" + "\n" ;
		}				
		if (theForm.postal.value == "") {
			emptyFields += "Postal/Zip code" + "\n" ;
		}	
		if (theForm.countryId.selectedIndex == "") {
			emptyFields += "Country" + "\n" ;
		}	
		if (theForm.provId.selectedIndex == "" & (theForm.countryId.selectedIndex == 1 || theForm.countryId.selectedIndex == 2 || theForm.countryId.selectedIndex == 229)) {
			emptyFields += "Province/State" + "\n" ;
		}		
		if (! validateInternationalPhone(theForm.phone.value)) {
			emptyFields += "Phone Number (including area code)" + "\n" ;
		}				
	
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		theForm.submit();
		return true;
	}	
}


function validatePayment() {
	var emptyFields = "";
	var credCardNumber = document.payment.ccNumber.value;
	var nonDigitRegExp = /[^0-9]/;
	var	isOneChecked = false;
	var theForm = document.payment;
	
	for (i=0; i < theForm.creditCard.length; i++) {
		if (theForm.creditCard[i].checked) {
			isOneChecked = true;
			break;
		}
	}
	if (isOneChecked == false) {
		emptyFields += "Method of payment. i.e. Visa" + "\n";
	}	

	if (credCardNumber == ""){
		emptyFields += "Card Number (missing)" + "\n" ;
	} else if ( credCardNumber.search(nonDigitRegExp) >= 0 ) {
		emptyFields += "Card Number (must contain only digits without spaces)" + "\n" ;
	} else if ( credCardNumber.length <= 9 ) {
		emptyFields += "Card Number (not enough digits?)" + "\n" ;
	}
	
	if (theForm.ccName.value == ""){
		emptyFields += "Name on Card" + "\n" ;
	}
	if (theForm.expiryMonth.options[theForm.expiryMonth.selectedIndex].value == "unselected"){
		emptyFields += "Expiry Date (month)" + "\n" ;
	}
	if (theForm.expiryYear.options[theForm.expiryYear.selectedIndex].value == "unselected"){
		emptyFields += "Expiry Date (year)" + "\n" ;
	}

	
	var	isOneChecked = false;
	for (i=0; i < theForm.sameBillingAddress.length; i++) {
		if (theForm.sameBillingAddress[i].checked) {
			isOneChecked = true;
			break;
		}
	}
	if (isOneChecked == false) {
		emptyFields += "Is your credit card billing address the same as your address?" + "\n";
	}	

	if (theForm.sameBillingAddress[1].checked == true) {
		if (theForm.address1.value == "") {
			emptyFields += "Address" + "\n" ;
		}
		if (theForm.city.value == "") {
			emptyFields += "City" + "\n" ;
		}		
		if (theForm.postal.value == "") {
			emptyFields += "Postal/Zip code" + "\n" ;
		}	
		if (theForm.countryId.selectedIndex == "") {
			emptyFields += "Country" + "\n" ;
		}	
		if (theForm.provId.selectedIndex == "" & (theForm.countryId.selectedIndex == 1 || theForm.countryId.selectedIndex == 2 || theForm.countryId.selectedIndex == 229)) {
			emptyFields += "Province/State" + "\n" ;
		}
	}	
	
	
	if (theForm.formType.value == "membership") {
		var	isOneChecked = false;
		for (i=0; i < theForm.membershipDelivery.length; i++) {
			if (theForm.membershipDelivery[i].checked) {
				isOneChecked = true;
				break;
			}
		}
		if (isOneChecked == false) {
			emptyFields += "Membership Delivery Option" + "\n";
		}	
	}
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		return true;
	}
}

function validateContactInfo(language) {
	var emptyFields = "";	
	var theForm = document.contactInfo;
	
	if (theForm.fname.value == "") {
		if (language == "fr") {
			emptyFields += "Prenom" + "\n" ;
		} else {
			emptyFields += "First Name" + "\n" ;
		}
	}
	if (theForm.lname.value == "") {
		if (language == "fr") {
			emptyFields += "Nom" + "\n" ;
		} else {
			emptyFields += "Last Name" + "\n" ;
		}		
	}	
	
	if (! verifyEmail(theForm.email.value)) {
		if (language == "fr") {
			emptyFields += "Courriel" + "\n" ;
		} else {
			emptyFields += "Email" + "\n" ;
		}				
	}				

	if (emptyFields.length >= 1) {
		if (language == "fr") {
			alert("Veuillez vous assurer de bien remplir les champs suivants : " + "\n" + emptyFields);
		} else {
			alert("Please ensure that the following are completed correctly:" + "\n" + emptyFields);
		}					
		return false;
	} else {
		theForm.submit();
		return true;
	}	
}

function validateInternationalPhone(number) {
/*	by Laurence 2005 Apr 29
	similar to validatePhone(), above
	The pattern matches a total of 10 digits found anywhere in the string; it's better than nothing...
*/
	var myRegExp = /(\d.*){9,}\d/;
	var sPos = number.search(myRegExp);
	if ( sPos >= 0 ) {
		return true;
	} else {
		return false;
	}
}

function verifyEmail(email) {
/*	new version by Laurence 2004 Nov 26
	RegExp is a Javascript1.2 feature; tested successfully in WinIE 5.0+, MacIE 5.1, Safari 1.0, WinNN 4.7, WinNN 6.2, WinMoz 1.7
	The pattern matches: 
	- at least one word character ( a-zA-Z0-9_ )
	- followed by any number more including periods
	- then a single @
	- then at least one alphanumeric character plus any number more including hyphens and periods
	- plus an ending of an alphanumeric char, a single period, and at least 2 more letters. 
	There can be nothing before or after the pattern; all whitespace, punctuation, etc. not explicitly allowed is forbidden.
*/
	var myRegExp = /^[\w][\w\.]*@[a-zA-Z0-9]+[\w\.\-]*[a-zA-Z0-9]\.[a-zA-Z]{2,}$/;
	var sPos = email.search(myRegExp);
	if (sPos >= 0) {
		return true;
	} else {
		return false;
	}
}

/* used in apps/view/membership/payment and apps/view/donate/payment */
 function toggleBillingAddress() {
	if (document.payment.sameBillingAddress[0].checked == true) {
		document.payment.address1.disabled = true;
		document.payment.address2.disabled = true;
		document.payment.city.disabled = true;
		document.payment.postal.disabled = true;
		document.payment.countryId.disabled = true;
		document.payment.provId.disabled = true;
	} else {
		document.payment.address1.disabled = false;
		document.payment.address2.disabled = false;
		document.payment.city.disabled = false;
		document.payment.postal.disabled = false;
		document.payment.countryId.disabled = false;
		document.payment.provId.disabled = false;
	}
} 

/* used in apps/view/donate/payment */
function clearTextField(theForm) {
	if (theForm.donation[4].value != "") {
		theForm.donation[4].value = "";
	}		
}
function clearRadioBtns(theForm) {
	if (theForm.donation[4].value != "") {
		theForm.donation[0].checked = false;
		theForm.donation[1].checked = false;
		theForm.donation[2].checked = false;
		theForm.donation[3].checked = false;		
	}		
}



/* used in apps/view/login/frmLogin
	check that required fields are filled in
*/
function validateLogin() {
	var emptyFields = "";

//	alert('validating Login...');

	if (document.frmLogin.login.value == ""){
		emptyFields += "Login (user name)" + "\n" ;
	}
	if (document.frmLogin.password.value == ""){
		emptyFields += "Password" + "\n" ;
	}
	
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		document.frmLogin.submit();
		return true;
	}
}


/* used in apps/view/admin/frmPeGroupAddEdit
	check that required fields are filled in
*/
function validateProgramEventGroup() {
	var emptyFields = "";

//	alert('validating Group...');
	if (document.frmProgramEventGroup.areaId.selectedIndex == 0) {
		emptyFields += "P. E. Area must be specified." + "\n" ;
	}

	if (document.frmProgramEventGroup.groupName.value == ""){
		emptyFields += "Group Name cannot be blank." + "\n" ;
	}
	
	if (document.frmProgramEventGroup.groupDesc.value.length > 2000){
		emptyFields += "P. E. Group Description is " + (document.frmProgramEventGroup.groupDesc.value.length - 2000).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}

	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		document.frmProgramEventGroup.submit();
		return true;
	}
}

/* used in apps/view/admin/frmPEventAddEdit
	check that required fields are filled in
*/
function validateProgramEvent() {
	var emptyFields = "";

//	alert('validating Event...');
	if (document.frmProgramEvent.areaId.selectedIndex == 0) {
		emptyFields += "P. E. Area must be specified." + "\n" ;
	}

	if (document.frmProgramEvent.fullTitle.value == ""){
		emptyFields += "Full Title cannot be blank." + "\n" ;
	} else if (document.frmProgramEvent.fullTitle.value.length > 255){
		emptyFields += "Full Title is " + (document.frmProgramEvent.fullTitle.value.length - 255).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}
	
	if (document.frmProgramEvent.shortTitle.value == ""){
		emptyFields += "Short Title cannot be blank." + "\n" ;
	}

	if (document.frmProgramEvent.dateDesc.value == ""){
		emptyFields += "Date Description cannot be blank." + "\n" ;
	} else if (document.frmProgramEvent.dateDesc.value.length > 255){
		emptyFields += "Date Description is " + (document.frmProgramEvent.dateDesc.value.length - 255).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}

	if (document.frmProgramEvent.fullDesc.value == ""){
		emptyFields += "Full Description cannot be blank." + "\n" ;
	} else if (document.frmProgramEvent.fullDesc.value.length > 2000){
		emptyFields += "Full Description is " + (document.frmProgramEvent.fullDesc.value.length - 2000).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}

	if (document.frmProgramEvent.shortDesc.value == ""){
		emptyFields += "Short Description cannot be blank." + "\n" ;
	} else if (document.frmProgramEvent.shortDesc.value.length > 255){
		emptyFields += "Short Description is " + (document.frmProgramEvent.shortDesc.value.length - 255).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}

	if (document.frmProgramEvent.speakerDesc.value.length > 255){
		emptyFields += "About the Speaker / Instructor / Guest is " + (document.frmProgramEvent.speakerDesc.value.length - 255).toString() + " characters too long. Please edit text to fit." + "\n" ;
	}

	if (((document.frmProgramEvent.relatedEventText.value == "") && (document.frmProgramEvent.relatedEventUrl.value != "")) || ((document.frmProgramEvent.relatedEventText.value != "") && (document.frmProgramEvent.relatedEventUrl.value == ""))){
		emptyFields += "Related Event link, if present, must have both link text and a URL." + "\n" ;
	}

	if (((document.frmProgramEvent.relatedPubText.value == "") && (document.frmProgramEvent.relatedPubUrl.value != "")) || ((document.frmProgramEvent.relatedPubText.value != "") && (document.frmProgramEvent.relatedPubUrl.value == ""))){
		emptyFields += "Related Publication link, if present, must have both link text and a URL." + "\n" ;
	}

	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		document.frmProgramEvent.submit();
		return true;
	}
}
