/****************************************************************
*                                                               *
*	For use with ProChoiceAmerica.org and affiliated websites   *
*                                                               *
*	written by Michael Rathmann                                 *
*	(c)RathmannDesign.com. All rights reserved.                 *
*                                                               *
****************************************************************/

if (window.focus) { self.focus() }

function openTable(tableid) {
	which = document.getElementById(tableid);
	if (which.style.display == "block") {
		which.style.display = "none";
	} else {
		which.style.display = "block";
	}
}

// form validation
function validateForm(form) {
	alertStart	= "To submit your order, please ";

	errorStart  = "The entered ";
	errorEnd	= " is invalid!";
	numError	= " is invalid!\n\nPlease ONLY enter numeric characters in this field.\nSpaces, alpha and other characters WILL NOT be accepted.";

	orderChecked = false;
	if (form.qty00.value && form.qty00.value != "SOLD OUT") {
		if (!validateInteger(form.qty00.value) || form.qty00.value < 1) {
			alert(errorStart + "quantity '" + form.qty00.value + "'" + numError);
			form.qty00.focus(); form.qty00.select(); return false;
		} orderChecked = true;
	} if (form.qty01.value && form.qty01.value != "SOLD OUT") {
		if (!validateInteger(form.qty01.value) || form.qty01.value < 1) {
			alert(errorStart + "quantity '" + form.qty01.value + "'" + numError);
			form.qty01.focus(); form.qty01.select(); return false;
		} orderChecked = true;
	} if (form.qty02.value && form.qty02.value != "SOLD OUT") {
		if (!validateInteger(form.qty02.value) || form.qty02.value < 1) {
			alert(errorStart + "quantity '" + form.qty02.value + "'" + numError);
			form.qty02.focus(); form.qty02.select(); return false;
		} orderChecked = true;
	} if (form.qty03.value && form.qty03.value != "SOLD OUT") {
		if (!validateInteger(form.qty03.value) || form.qty03.value < 1) {
			alert(errorStart + "quantity '" + form.qty03.value + "'" + numError);
			form.qty03.focus(); form.qty03.select(); return false;
		} orderChecked = true;
	} if (form.qty04.value && form.qty04.value != "SOLD OUT") {
		if (!validateInteger(form.qty04.value) || form.qty04.value < 1) {
			alert(errorStart + "quantity '" + form.qty04.value + "'" + numError);
			form.qty04.focus(); form.qty04.select(); return false;
		} orderChecked = true;
	} if (form.qty05.value && form.qty05.value != "SOLD OUT") {
		if (!validateInteger(form.qty05.value) || form.qty05.value < 1) {
			alert(errorStart + "quantity '" + form.qty05.value + "'" + numError);
			form.qty05.focus(); form.qty05.select(); return false;
		} orderChecked = true;
	} if (form.qty06.value && form.qty06.value != "SOLD OUT") {
		if (!validateInteger(form.qty06.value) || form.qty06.value < 1) {
			alert(errorStart + "quantity '" + form.qty06.value + "'" + numError);
			form.qty06.focus(); form.qty06.select(); return false;
		} orderChecked = true;
	} if (form.qty07.value && form.qty07.value != "SOLD OUT") {
		if (!validateInteger(form.qty07.value) || form.qty07.value < 1) {
			alert(errorStart + "quantity '" + form.qty07.value + "'" + numError);
			form.qty07.focus(); form.qty07.select(); return false;
		} orderChecked = true;
	} if (form.qty08.value && form.qty08.value != "SOLD OUT") {
		if (!validateInteger(form.qty08.value) || form.qty08.value < 1) {
			alert(errorStart + "quantity '" + form.qty08.value + "'" + numError);
			form.qty08.focus(); form.qty08.select(); return false;
		} orderChecked = true;
	} if (form.qty09.value && form.qty09.value != "SOLD OUT") {
		if (!validateInteger(form.qty09.value) || form.qty09.value < 1) {
			alert(errorStart + "quantity '" + form.qty09.value + "'" + numError);
			form.qty09.focus(); form.qty09.select(); return false;
		} orderChecked = true;
	} if (form.qty10.value && form.qty10.value != "SOLD OUT") {
		if (!validateInteger(form.qty10.value) || form.qty10.value < 1) {
			alert(errorStart + "quantity '" + form.qty10.value + "'" + numError);
			form.qty10.focus(); form.qty10.select(); return false;
		} orderChecked = true;
	} if (form.qty11.value && form.qty11.value != "SOLD OUT") {
		if (!validateInteger(form.qty11.value) || form.qty11.value < 1) {
			alert(errorStart + "quantity '" + form.qty11.value + "'" + numError);
			form.qty11.focus(); form.qty11.select(); return false;
		} orderChecked = true;
	} if (form.qty12.value && form.qty12.value != "SOLD OUT") {
		if (!validateInteger(form.qty12.value) || form.qty12.value < 1) {
			alert(errorStart + "quantity '" + form.qty12.value + "'" + numError);
			form.qty12.focus(); form.qty12.select(); return false;
		} orderChecked = true;
	} if (form.qty13.value && form.qty13.value != "SOLD OUT") {
		if (!validateInteger(form.qty13.value) || form.qty13.value < 1) {
			alert(errorStart + "quantity '" + form.qty13.value + "'" + numError);
			form.qty13.focus(); form.qty13.select(); return false;
		} orderChecked = true;
	} if (form.qty14.value && form.qty14.value != "SOLD OUT") {
		if (!validateInteger(form.qty14.value) || form.qty14.value < 1) {
			alert(errorStart + "quantity '" + form.qty14.value + "'" + numError);
			form.qty14.focus(); form.qty14.select(); return false;
		} orderChecked = true;
	} if (form.qty15.value && form.qty15.value != "SOLD OUT") {
		if (!validateInteger(form.qty15.value) || form.qty15.value < 1) {
			alert(errorStart + "quantity '" + form.qty15.value + "'" + numError);
			form.qty15.focus(); form.qty15.select(); return false;
		} orderChecked = true;
	} if (form.qty16.value && form.qty16.value != "SOLD OUT") {
		if (!validateInteger(form.qty16.value) || form.qty16.value < 1) {
			alert(errorStart + "quantity '" + form.qty16.value + "'" + numError);
			form.qty16.focus(); form.qty16.select(); return false;
		} orderChecked = true;
	} if (form.qty17.value && form.qty17.value != "SOLD OUT") {
		if (!validateInteger(form.qty17.value) || form.qty17.value < 1) {
			alert(errorStart + "quantity '" + form.qty17.value + "'" + numError);
			form.qty17.focus(); form.qty17.select(); return false;
		} orderChecked = true;
	} if (form.qty18.value && form.qty18.value != "SOLD OUT") {
		if (!validateInteger(form.qty18.value) || form.qty18.value < 1) {
			alert(errorStart + "quantity '" + form.qty18.value + "'" + numError);
			form.qty18.focus(); form.qty18.select(); return false;
		} orderChecked = true;
	} if (form.qty19.value && form.qty19.value != "SOLD OUT") {
		if (!validateInteger(form.qty19.value) || form.qty19.value < 1) {
			alert(errorStart + "quantity '" + form.qty19.value + "'" + numError);
			form.qty19.focus(); form.qty19.select(); return false;
		} orderChecked = true;
	} if (form.qty20.value && form.qty20.value != "SOLD OUT") {
		if (!validateInteger(form.qty20.value) || form.qty20.value < 1) {
			alert(errorStart + "quantity '" + form.qty20.value + "'" + numError);
			form.qty20.focus(); form.qty20.select(); return false;
		} orderChecked = true;
	} if (form.qty21.value && form.qty21.value != "SOLD OUT") {
		if (!validateInteger(form.qty21.value) || form.qty21.value < 1) {
			alert(errorStart + "quantity '" + form.qty21.value + "'" + numError);
			form.qty21.focus(); form.qty21.select(); return false;
		} orderChecked = true;
	} if (form.qty22.value && form.qty22.value != "SOLD OUT") {
		if (!validateInteger(form.qty22.value) || form.qty22.value < 1) {
			alert(errorStart + "quantity '" + form.qty22.value + "'" + numError);
			form.qty22.focus(); form.qty22.select(); return false;
		} orderChecked = true;
	} if (form.qty23.value && form.qty23.value != "SOLD OUT") {
		if (!validateInteger(form.qty23.value) || form.qty23.value < 1) {
			alert(errorStart + "quantity '" + form.qty23.value + "'" + numError);
			form.qty23.focus(); form.qty23.select(); return false;
		} orderChecked = true;
	} if (form.qty24.value && form.qty24.value != "SOLD OUT") {
		if (!validateInteger(form.qty24.value) || form.qty24.value < 1) {
			alert(errorStart + "quantity '" + form.qty24.value + "'" + numError);
			form.qty24.focus(); form.qty24.select(); return false;
		} orderChecked = true;
	} if (form.qty25.value) {
		if (!validateInteger(form.qty25.value) || form.qty25.value < 1) {
			alert(errorStart + "quantity '" + form.qty25.value + "'" + numError);
			form.qty25.focus(); form.qty25.select(); return false;
		} orderChecked = true;
	} if (form.qty26.value) {
		if (!validateInteger(form.qty26.value) || form.qty26.value < 1) {
			alert(errorStart + "quantity '" + form.qty26.value + "'" + numError);
			form.qty26.focus(); form.qty26.select(); return false;
		} orderChecked = true;
	}/* if (form.qty27.value) {
		if (!validateInteger(form.qty27.value) || form.qty27.value < 1) {
			alert(errorStart + "quantity '" + form.qty27.value + "'" + numError);
			form.qty27.focus(); form.qty27.select(); return false;
		} orderChecked = true;
	} if (form.qty28.value) {
		if (!validateInteger(form.qty28.value) || form.qty28.value < 1) {
			alert(errorStart + "quantity '" + form.qty28.value + "'" + numError);
			form.qty28.focus(); form.qty28.select(); return false;
		} orderChecked = true;
	} if (form.qty29.value) {
		if (!validateInteger(form.qty29.value) || form.qty29.value < 1) {
			alert(errorStart + "quantity '" + form.qty29.value + "'" + numError);
			form.qty29.focus(); form.qty29.select(); return false;
		} orderChecked = true;
	}*/
	
	if (orderChecked == false) {
		alert(alertStart + "enter the quantity you wish to order for one or more of the available items.");
		form.qty00.focus(); return false;
	} if ((!form.orderSubstitute[0].checked) && (!form.orderSubstitute[1].checked)) {
		alert(alertStart + "indicate if we may substitute similar bulbs if necessary.");
		form.orderSubstitute[0].focus(); return false;
	} if ((!form.orderPrior[0].checked) && (!form.orderPrior[1].checked)) {
		alert(alertStart + "indicate if you have ordered from us before.");
		form.orderPrior[0].focus(); return false;
	} if ((!form.orderMethod[0].checked) && (!form.orderMethod[1].checked) && (!form.orderMethod[2].checked) && (!form.orderMethod[3].checked)) {
		alert(alertStart + "indicate if you would like to pick-up your order or have it shipped**.\n\n(State sales tax is included in price of bulbs)\n**Shipping $5 Minimum or 10% of order");
		form.orderMethod[0].focus(); return false;
	} // end one

	if (!form.contactName.value) {
		alert(alertStart + "enter your name.");
		form.contactName.focus(); return false;
	} if (!form.contactEmail.value) {
		alert(alertStart + "enter your email address.");
		form.contactEmail.focus(); return false;
	} if (!isEmail(form.contactEmail.value)) {
		alert("'" + form.contactEmail.value + "' is an invalid email address! Please reenter.");
		form.contactEmail.focus(); form.contactEmail.select(); return false;
	} if (!form.contactAddress.value) {
		alert(alertStart + "enter your mailing address.");
		form.contactAddress.focus(); return false;
	} if (!form.contactCity.value) {
		alert(alertStart + "enter the city of your mailing address.");
		form.contactCity.focus(); return false;
	} if (!form.contactState.value) {
		alert(alertStart + "enter the state of your mailing address.");
		form.contactState.focus(); return false;
	} if (!form.contactZip.value) {
		alert(alertStart + "enter the zip code of your mailing address.");
		form.contactZip.focus(); return false;
	} if (!form.contactPhone.value) {
		alert(alertStart + "enter your phone number.");
		form.contactPhone.focus(); return false;
	} if (!isPhoneNum(form.contactPhone.value)) {
		alert("'" + form.contactPhone.value + "' is invalid!\r\nPlease use this format: ###-###-####");
		form.contactPhone.focus(); form.contactPhone.select(); return false;
	}
	
	if (!form.paymentMethod[0].checked && !form.paymentMethod[1].checked) {
		alert(alertStart + "indicate your method of payment.");
		form.paymentMethod[0].focus(); return false;
	} if (form.paymentMethod[1].checked) {
		if (!form.cardSubmission[0].checked && !form.cardSubmission[1].checked) {
			alert(alertStart + "indicate the method in which you would like to submit this form.");
			form.cardSubmission[0].focus(); return false;
		} if (!form.cardType[0].checked && !form.cardType[1].checked && !form.cardType[2].checked && !form.cardType[3].checked) {
			alert(alertStart + "indicate the type of credit card.");
			form.cardType[0].focus(); return false;
		} if (!form.cardName.value) {
			alert(alertStart + "enter the name as it appears on the credit card.");
			form.cardName.focus(); return false;
		} if (!form.cardNumber.value) {
			alert(alertStart + "enter the account number for the credit card.");
			form.cardNumber.focus(); return false;
		} if (!form.cardId.value) {
			alert(alertStart + "enter the security code for the credit card.");
			form.cardId.focus(); return false;
		} if (!form.cardExpiration.value) {
			alert(alertStart + "enter the expiration date for the credit card.");
			form.cardExpiration.focus(); return false;
		} if (!isExpDate(form.cardExpiration.value)) {
			alert("'" + form.cardExpiration.value + "' is invalid!\r\nPlease use this format: mm/yy");
			form.cardExpiration.focus(); form.cardExpiration.select(); return false;
		}
	}
	
	if (form.sectLevel.value == 1 && form.paymentMethod[1].checked && form.cardSubmission[1].checked) {
//		input_box=confirm(form.action.value);
//		if (input_box) {
			openTable('sect1'); openTable('sect2_1'); openTable('printDisplay');
			form.sectLevel.value = 3;
			form.btn01.style.display = 'block';
			form.submitBtn.value = "Submit Order »";
			return true;
//		} else { return false; }
	} else if (form.sectLevel.value == 1) {
//		input_box=confirm(form.action.value);
//		if (input_box) {
			openTable('sect1'); openTable('sect2_2'); openTable('printDisplay');
			form.sectLevel.value = 2;
			form.btn01.style.display = 'block';
			form.printBtn.style.display = 'block';
			form.submitBtn.style.display = 'none';
			return true;
//		} else { return false; }
	}
	
	if (form.sectLevel.value == 3) {
		form.action.value = 1;
//		input_box=confirm(form.action.value);
//		input_box=confirm('Press \'OK\' to submit');
//		if (input_box) {
//			form.action = "/cgi-bin/forms/groupreservation.pl";
			openTable('sect2_1'); openTable('sect3'); openTable('btm');
//			document.getElementById('printDisplay').style.visibility = 'hidden'; document.getElementById('submission').height = 1;
			form.sectLevel.value = 3;
			return true;
//		} else { return false; }
	}
}

function validateInteger(strValue) {
	var objRegExp = /(^-?\d\d*$)/;

	//check for integer characters
	return objRegExp.test(strValue);
}

function isEmail(str) {
	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; }	}
	} return true;
}
