/*****************************************************************
*                                                                *
*	validates02-supportus.js                                     *
*	For use with ProChoiceAmerica.org and affiliated websites    *
*                                                                *
*	written by Michael Rathmann                                  *
*	(c)2003 RathmannDesign.com. All rights reserved.             *
*                                                                *
*****************************************************************/

if (window.focus) { self.focus() }

var chasm = screen.availWidth;
var mount = screen.availHeight;
var width = 0;
var height = 0;
var aux = null;

function formWindow(width,height) {
	if (aux != null && aux.open) aux.close();
	aux = window.open('','form','resizable=yes,status=yes,toolbar=yes,scrollbars=yes,width=' + width + ',height=' + height + ',left=' + ((chasm - width - 10) * .5) + ',top=' + ((mount - height - 30) * .5));
}

function eradic() {
	if (aux != null && aux.open) aux.close();
}

// form validation
function validateForm(form) {

	alertStart = "To submit your donation, please ";

//	Section 1
	if (!optionChecked(form) && !optionChecked3(form)) {
		alert(alertStart + "complete one or both of the following steps:\n\n(1) Select one of the available donation options.\n(2) Select one of the available options to include your name in our 2005 NY Times full-page signature ad.");
		form.donationOption[0].focus(); return false;
	} if (form.donationOption[6].checked) {
		if ((!form.amt06.value)) {
			alert(alertStart + "indicate the amount you wish to donate.");
			form.amt06.focus(); return false;
		} if (!validateInteger(form.amt06.value) || (form.amt06.value < 5)) {
			alert("Please enter a valid number for the amount.");
			form.amt06.focus(); form.amt06.select(); return false;
		}
	} if (form.donationOption[7].checked) {
		if ((!form.amt07.value)) {
			alert(alertStart + "indicate the amount you wish to donate monthly.");
			form.amt07.focus(); return false;
		} if (!validateInteger(form.amt07.value) || (form.amt07.value < 1)) {
			alert("Please enter a valid number for the amount.");
			form.amt07.focus(); form.amt07.select(); return false;
		} if (!form.paymentMethod[1].checked) {
			alert("In order to make a monthly contribution, please select to pay by credit card.");
			form.paymentMethod[1].focus(); return false;
		}
	} if (form.donationOption[8].checked) {
		if ((!form.amt08.value)) {
			alert(alertStart + "indicate the amount you wish to donate quarterly.");
			form.amt08.focus(); return false;
		} if (!validateInteger(form.amt08.value) || (form.amt08.value < 1)) {
			alert("Please enter a valid number for the amount.");
			form.amt08.focus(); form.amt08.select(); return false;
		} if (!form.paymentMethod[1].checked) {
			alert("In order to make a quarterly contribution, please select to pay by credit card.");
			form.paymentMethod[1].focus(); return false;
		}
	} if ((optionChecked2(form) || (form.amt06.value >= 50) || ((form.amt07.value*12) >= 50) || ((form.amt08.value*4) >= 50) || optionChecked3(form)) && !form.adName.value) {
		alert(alertStart + "indicate the sponsorship name as is should appear on the ad.");
		form.adName.focus(); return false;
	} // end section

//	Section 2
	if (!form.contactName01.value) {
		alert(alertStart + "indicate your first name.");
		form.contactName01.focus(); return false;
	} if (!form.contactName03.value) {
		alert(alertStart + "indicate your last name.");
		form.contactName03.focus(); return false;
	} if (form.submissionType[1].checked) {
		if (!form.contactEmail.value) {
			alert(alertStart + "indicate your email address.");
			form.contactEmail.focus(); return false;
		}
	} if (form.contactEmail.value) {
		var str = form.contactEmail.value;
		if (!isEmail(str)) {
			alert("" + str + " is an invalid email address!");
			form.contactEmail.focus(); form.contactEmail.select(); return false;
		}
	} if (!form.contactAddress01.value) {
		alert(alertStart + "indicate your mailing address.");
		form.contactAddress01.focus(); return false;
	} if (!form.contactCity.value) {
		alert(alertStart + "indicate the city of your mailing address.");
		form.contactCity.focus(); return false;
	} if (!form.contactState.value) {
		alert(alertStart + "indicate the state of your mailing address.");
		form.contactState.focus(); return false;
	} if (!form.contactZip.value) {
		alert(alertStart + "indicate the zip code of your mailing address.");
		form.contactZip.focus(); return false;
	} if (form.contactHome.value) {
		var str = form.contactHome.value;
		if (!isPhoneNum(str)) {
			alert("" + str + " is an invalid phone number!\r\nUse this format: ###-###-####");
			form.contactHome.focus(); form.contactHome.select(); return false;
		}
	} if (form.contactWork.value) {
		var str = form.contactWork.value;
		if (!isPhoneNum(str)) {
			alert("" + str + " is an invalid phone number!\r\nUse this format: ###-###-####");
			form.contactWork.focus(); form.contactWork.select(); return false;
		}
	} // end section

//	Section 3
	if ((!form.paymentMethod[0].checked) && (!form.paymentMethod[1].checked)) {
		alert(alertStart + "indicate the method of payment.");
		form.paymentMethod[0].focus(); return false;
	} if (form.paymentMethod[1].checked) {
		if ((!form.submissionType[0].checked) && (!form.submissionType[1].checked)) {
			alert(alertStart + "indicate the method in which you would like to submit your donation.");
			form.submissionType[0].focus(); return false;
		} if ((!form.cardType[0].checked) && (!form.cardType[1].checked)) {
			alert(alertStart + "indicate the type of credit card.");
			form.cardType[0].focus(); return false;
		} if (!form.cardName.value) {
			alert(alertStart + "indicate the name that appears on the credit card.");
			form.cardName.focus(); return false;
		} if (!form.cardNumber.value) {
			alert(alertStart + "indicate the account number of the credit card.");
			form.cardNumber.focus(); return false;
		} if (!form.cardExpdate.value) {
			alert(alertStart + "indicate the expiration date of the credit card.");
			form.cardExpdate.focus(); return false;
		} var str = form.cardExpdate.value;
		if (!isExpDate(str)) {
			alert("" + str + " is an invalid expiration date!\r\nUse this format: mm/yy");
			form.cardExpdate.focus(); form.cardExpdate.select(); return false;
		}
	} // end section

	if (form.paymentMethod[0].checked) {
		alert("**READ THIS FIRST**\n\nOnce you click the 'OK' button below, you will be provided with a page\nto PRINT AND MAIL IN with your CHECK payment.");
	} else if (form.paymentMethod[1].checked && form.submissionType[0].checked) {
		alert("**READ THIS FIRST**\n\nOnce you click the 'OK' button below, you will be provided with a page\nto PRINT AND MAIL IN with your CREDIT CARD signature authorization.");
	} else {
		alert("**READ THIS FIRST**\n\nOnce you click the 'OK' button below, you will be provided with an authorization page.\n\nYou must accept the statement of authorization at the bottom of that page\nand click \"SUBMIT\" for your form to be submitted.");
	} formWindow(489,360);
	return true; //change this to return false to disable
}

function optionChecked(form) {
	for (i=0; i<form.donationOption.length; i++) {
		if (form.donationOption[i].checked) { return true; }
	} return false;
}

function optionChecked2(form) {
	for (i=1; i<5; i++) {
		if (form.donationOption[i].checked) { return true; }
	} return false;
}

function optionChecked3(form) {
	for (i=0; i<form.adOption.length; i++) {
		if (form.adOption[i].checked) { return true; }
	} return false;
}

function validateInteger(strValue) {
	var objRegExp = /(^-?\d\d*$)/; return objRegExp.test(strValue);
}

function typeChecked(form) {
	for (i=0; i<form.donationType.length; i++) {
		if (form.donationType[i].checked) { return true; }
	} return false;
}

function isEmail(str) {
	if (str.length <= 6) { return false; }
	else { return ((str != "") && (str.indexOf("@") != -1) && (str.indexOf(".") != -1)); }
}

function isPhoneNum(str) {
	if (str.length != 12) { return false; }
	for (j=0; j<str.length; j++) {
		if (j == 3) { if (str.charAt(j) != "-") { return false; } }
		else if (j == 7) { if (str.charAt(j) != "-") { return false; } }
		else { if ((str.charAt(j) < "0") || (str.charAt(j) > "9")) { return false; } }
	} return true;
}

function isExpDate(str) {
	if (str.length != 5) { return false; }
	for (j=0; j<str.length; j++) {
		if (j == 2) { if (str.charAt(j) != "/") { return false; } }
		else { if ((str.charAt(j) < "0") || (str.charAt(j) > "9")) { return false; } }
	}
	var month = str.charAt(0) == "0" ? parseInt(str.substring(1,2)) : parseInt(str.substring(0,2));
	if (month == 0 || month > 12) { return false; }
	return true;
}