<!--//

/* form validation scripts 
/* MOT
/* DW / HDR - 3/05

/* MAIN FORM VALIDATOR */

var alpha = /[^A-Za-z]/ig;
var alphaNum =/[^\w \-]/ig;
var num = /[^\d]/ig;
var phone =/[^\d \-()\.]/ig
var notNull = /[^\s]/ig;
var email = /(\w{1,})@.*\.(\w{2,})/ig;
var URL = /(http:\/\/)?[\w\.-]+\.[A-Za-z\.]{2,}/ig;
var dte = /\d{1,2}\/\d{1,2}\/[20]?[\d{2}][ ]?[\d:ampm ]?/ig;
var img = /\.(gif|jp[e]?g)$/ig;
	
	function charCheck(f,ty,m,e) {
		var e = (e || !e) ? e : true;
		if (e) {
			var f_type = f.type;
			var f_val= getValue(f);
			if (ty == "alpha") {
				e = (f_val.search(alpha) == -1) ? true : false;
			} else if (ty == "alphaNum") {
				e = (f_val.search(alphaNum) == -1) ? true : false;
			} else if (ty == "num") {
				e = (f_val.search(num) == -1) ? true : false;
			} else if (ty == "phone") {
				e = (f_val.search(phone) == -1) ? true : false;
			} else if (ty.toLowerCase() == "notnull") {
				e = (f_val.search(notNull) == -1) ? false : true;
			} else if (ty.toLowerCase() == "url") {
				e = (f_val.match(URL) == null) ? false : true;
			} else if (ty.toLowerCase() == "email") {
				e = (f_val.match(email) == null) ? false : true;
			} else if (ty == "date") {
				e = (f_val.match(dte) == null) ? false : true;
			}
			
			if (!e) {
				if (ty == "date") {
					alert("The "+m+" you've entered is invalid.  \nPlease enter dates using the following format:\n\n\tMM/DD/YYYY");
				} else if (m != "" && m != "no") {
					var msg = (ty.toLowerCase() != "notnull") ? "The value you have entered for " + m + " is invalid.  Please re-enter this information." : "The " + m + " field is required.  Please enter this information before saving.";
					alert(msg);
				}	
				if (f_type == "text" || f_type == "select-one" || f_type == "select-multiple" || f_type == "checkbox") {
					f.focus();
				}
			}
			
		}
		return e;
	}
	
	function old_getValue(f) {
		msg = "GetVal: \n";
		var val = "";
		msg += "field exists? " + (f);
		msg += "field type? " + f.type;
		var fType = String(f.type);
		if (fType == "select-one" || fType == "select-multiple") {
			for (x = 0 ; x < f.options.length; x++) {
				if (f.options[x].selected == true) {
					val = (f.type == "select-one") ? f.options[x].value : val + "|" + f.options[x].value;
				}
			}
		} else if (fType == "undefined" || fType == "radio") {
			if (f.length > 1) { 
				for (y = 0; y < f.length; y++) {
					if (f[y].checked) {
						val = f[y].value;
					}
				}
			} else {
				val = f.value;
			}
		} else {
			val = f.value;
		}
		if (String(val) == "undefined" || val == undefined) {
			val = "";
		}
		msg += "field value: " + val;
		alert(msg);
		return val;
	}
	
	
	function getValue(f) {
		msg = "GetVal: \n";
		var val = f.value;
		msg += "field exists? " + (f);
		msg += "field type? " + f.type;
		msg += "init val: " + val;
		var fType = String(f.type).toLowerCase();
		if (fType == "select-one" || fType == "select-multiple") {
			for (x = 0 ; x < f.options.length; x++) {
				if (f.options[x].selected == true) {
					val = (f.type == "select-one") ? f.options[x].value : val + "|" + f.options[x].value;
				}
			}
		} else if (fType == "undefined" || fType == "radio") {
			if (f.length > 1) { 
				for (y = 0; y < f.length; y++) {
					if (f[y].checked) {
						val = f[y].value;
					}
				}
			} else {
				val = f.value;
			}
		}
		if (String(val) == "undefined" || val == undefined) {
			val = "";
		}
		msg += "field value: " + val;
		//alert(msg);
		return val;
	}
	
	
	
	function zipAuth(zip,country) {
		var z = getValue(zip);
		var c = country;
		var zFit, zCountry, zMask;
		var procZip = "";
			if (c == 1) { // USA
				zFit = /\d{5}[\s-]?(\d{4})?/i;
				zCountry = "USA";
				zMask = "\t- #####\n\t- #####-####";
			} else if (c == 32) { // canada
				zFit = /\w\d\w([\s-]?)[0-9][a-zA-Z][0-9]/i;
				zCountry = "Canada";
				zMask = "\t- !#!-#!#";
			} else if (c == 60 || c == 143 || c == 218 || c == 209) { // england, n.ireland, wales
				zFit = /[\d\w]{3}[\s-]?[\d\w]{3}/i;
				zCountry = "England, Northern Ireland or Wales";
				zMask = "\t- XXX-XXX";
			} else if (c == 170) { // scotland
				zFit = /[\d\w]{2}[\s-]?[\d\w]{3}/i;
				zCountry = "Scotland";
				zMask = "\t- XX-XXX";
			} else if (c == 227) { // australia
				zFit = /[\d]{4,}/i;
				zCountry = "Australia";
				zMask = "\t- ####";
			} else {
				zFit = /[\d\w]+/i;
				zMask = "";
			}
			procZip = String(z.match(zFit));
			//alert(procZip);
			if (procZip == "" || procZip == "null") {
				msg = (zMask != "") ? "The postal code you have entered is incorrectly formatted\nfor "+zCountry+"  \n\nPlease enter your postal code in the following format:\n\n"+zMask+"\n\n where " : "The Postal Code field is required.\nPlease enter your postal code";
				if (zMask != "") {
					if (zMask.indexOf("X") > -1) {
						msg += "X can be either a letter or a number.";
					} else if (zMask.indexOf("!") > -1) {
						msg += "! represents a letter and # represents a number.";
					} else if (zMask.indexOf("!") == -1 && zMask.indexOf("X") == -1) {
						msg += "# represents a number.";
					}
				}
				alert(msg);
				zip.focus();
				return false;
			} else {
				
				zip.value = z.replace(/[^\w]/ig,"");
				return true;
			}
	}	
	
/* FORM VALIDATION SCRIPTS */
	
	function checkLoginRegister() {
		var f = document.login;
		var e = true;
		var formIs = f.formIs.value;
			if (formIs == "log") {
				e = charCheck(f.l_username,"notnull","Email",e);
				e = charCheck(f.l_password,"notnull","Password",e);
				/*if (f.l_promoCode) {
					e = (charCheck(f.l_promoCode,"notnull","",e)) ? charCheck(f.l_promoCode,"num","Promo Code",e) : e;
				}*/
			    /*if (f.l_promoCode) {
					e = charCheck(f.l_promoCode,"notnull","",e);
				}*/

			} else if (formIs == "reg") {
				e = charCheck(f.r_email,"notnull","Email Address",e);
				e = charCheck(f.r_email,"email","Email Address",e);
				e = charCheck(f.r_password,"notnull","Password",e);
				e = charCheck(f.r_password,"alphaNum","Password",e);
				e = charCheck(f.r_passwordConfirm,"notnull","Confirm Password",e);
				if (e && f.r_password.value != f.r_passwordConfirm.value) {
					e = false;
					alert("The Password and Confirm Password values do not match.  Please re-enter this information.")
					f.r_passwordConfirm.focus();
				}
				e = charCheck(f.r_prefix,"notnull","Title",e);
				e = charCheck(f.r_firstName,"notnull","First Name",e);
				e = charCheck(f.r_firstName,"alphaNum","First Name",e);
				e = charCheck(f.r_lastName,"notnull","Last Name",e);
				e = charCheck(f.r_lastName,"alphaNum","Last Name",e);
				e = charCheck(f.r_addr1,"notnull","Address",e);
				e = charCheck(f.r_city,"notnull","City",e);
				if (e && (f.r_country[f.r_country.selectedIndex].value == 1 || f.r_country[f.r_country.selectedIndex].value == 32)) {
					e = charCheck(f.r_state,"notnull","State",e);
				}
				e = charCheck(f.r_country,"notnull","Country",e);
				e = (e) ?  zipAuth(f.r_zip,f.r_country[f.r_country.selectedIndex].value) : e;
				e = charCheck(f.r_phone,"notnull","Phone",e);
				e = charCheck(f.r_phone,"phone","Phone",e);
				if (e) {
					if (f.r_phone.value.replace(num,"").length < 10) {
						e = false;
						alert("Please provide both your area code and phone number.\n\texample: (313) 555-5555");
					} else {
						f.r_phone.value = f.r_phone.value.replace(num,"");
					}
				}
				/*if (f.r_promoCode) {
					e = (charCheck(f.r_promoCode,"notnull","",e)) ? charCheck(f.r_promoCode,"num","Promo Code",e) : e;
				}*/
			}
		return e;
	}	
	
	function checkAccount() {
		var f = document.acct;
		var e = true;
		if (f.formIs.value == "update") {
			e = charCheck(f.prefix,"notnull","Title",e);
			e = charCheck(f.email,"notnull","Email Address",e);
			e = charCheck(f.email,"email","Email Address",e);
			if (f.whereTo.value == "renew") {
				e = charCheck(f.pwd1,"notnull","Password",e);
				e = charCheck(f.pwd1,"alphaNum","Password",e);
				e = charCheck(f.pwd2,"notnull","Confirm Password",e);
			} else {
				e = (charCheck(f.pwd1,"notnull","",e)) ? charCheck(f.pwd1,"alphaNum","Password",e) : e;
				e = (charCheck(f.pwd1,"notnull","",e)) ? charCheck(f.pwd2,"notnull","Confirm Password",e) : e;
			}
			if (e && f.pwd1.value != f.pwd2.value) {
				e = false;
				alert("The Password and Confirm Password values do not match.  Please re-enter this information.")
				f.pwd2.focus();
			}			
			e = charCheck(f.addr1,"notnull","Address",e);
			e = charCheck(f.city,"notnull","City",e);
			if (e && (f.country[f.country.selectedIndex].value == 1 || f.country[f.country.selectedIndex].value == 32)) {
				e = charCheck(f.state,"notnull","State",e);
			}
			e = charCheck(f.country,"notnull","Country",e);
			e = (e) ?  zipAuth(f.zip,f.country[f.country.selectedIndex].value) : e;
			e = charCheck(f.phone,"notnull","Phone",e);
			e = charCheck(f.phone,"phone","Phone",e);
			if (e) {
				if (f.phone.value.replace(num,"").length < 10) {
					e = false;
					alert("Please provide both your area code and phone number.\n\texample: (313) 555-5555");
				} else {
					f.phone.value = f.phone.value.replace(num,"");
				}
			}
		}
		return e;
	}	
	
	function old_checkSeatSel() {
		var f = document.tix;
		e = charCheck(f.selPrice,"notnull","Section",e); //alert("selPrice: " + e);
		e = charCheck(f.seatNum,"notnull","# of Seats",e); //alert("seatNum: " + e);
		e = charCheck(f.seatNum,"num","# of Seats",e); //alert("seatNum: " + e);
		if (Number(f.seatNum.value) > Number(f.maxIndTix.value)) {
			e = false;
			alert("You may buy " + f.maxIndTix.value + " or less individual tickets at a time.  If you need to purchase tickets for a group, please contact our Group Sales Department at XXX-XXX-XXXX.");
		}
		return e;
	}
	
	function checkShipping() {
		var f = document.acct;
		var e = true;
		e = charCheck(f.a_email,"notnull","Email Address",e);
		e = charCheck(f.a_email,"email","Email Address",e);
		e = charCheck(f.a_addr1,"notnull","Address",e);
		e = charCheck(f.a_city,"notnull","City",e);
		if (e && (f.a_country[f.a_country.selectedIndex].value == 1 || f.a_country[f.a_country.selectedIndex].value == 32)) {
			e = charCheck(f.a_state,"notnull","State",e);
		}
		e = charCheck(f.a_country,"notnull","Country",e);
		e = (e) ?  zipAuth(f.a_zip,f.a_country[f.a_country.selectedIndex].value) : e;
		e = charCheck(f.a_phone,"notnull","Phone",e);
		e = charCheck(f.a_phone,"phone","Phone",e);
		if (e) {
			if (f.a_phone.value.replace(num,"").length < 10) {
				e = false;
				alert("Please provide both your area code and phone number.\n\texample: (313) 555-5555");
			} else {
				f.a_phone.value = f.a_phone.value.replace(num,"");
			}
		}
		
		var sMeth = getValue(f.s_meth);
		if (e && sMeth == "ACCT" && f.a_country[f.a_country.selectedIndex].value != 1 && f.a_country[f.a_country.selectedIndex].value != 32) {
			e = false;
			alert("We are not shipping overseas at this time.\n\nPlease choose to hold your tickets at the Detroit Opera House box office\nor choose to have your order shipped to a location in the US or Canada.");
		} else if (e && sMeth == "OTHER") {
			e = charCheck(f.s_addr1,"notnull","Shipping Address",e);
			e = charCheck(f.s_city,"notnull","Shipping City",e);
			if (e && (f.s_country[f.s_country.selectedIndex].value == 1 || f.s_country[f.s_country.selectedIndex].value == 32)) {
				e = charCheck(f.s_state,"notnull","Shipping State",e);
			}
			e = charCheck(f.s_country,"notnull","Shipping Country",e);
			e = (e) ?  zipAuth(f.s_zip,f.s_country[f.s_country.selectedIndex].value) : e;
		}
		return e;
	}
	
	function checkConfirm() {
		var f = document.conf;
		var e = true;
		e = (f.notes.value.length > 255) ? false : true;
			if (!e) {
				alert("Your order notes cannot exceed 255 characters.");
				f.notes.focus();
			}
		var cardType = f.ccType[f.ccType.selectedIndex].text.toUpperCase();
		if (cardType == "American Express") {
			cardType = "AMEX";
		} else if (cardType == "Diner's Club") {
			cardType = "DINERS";
		}
		e = charCheck(f.ccName,"notnull","Name on Card",e);
		f.ccNum.value = f.ccNum.value.replace(/\s/ig,"");
		e = (e) ? CheckCardNumber(f.ccNum.value, f.ccMo[f.ccMo.selectedIndex].value, f.ccYr[f.ccYr.selectedIndex].value, cardType, true) : e;
		e = charCheck(f.ccAuthCode,"notnull","Card Security Code",e);
		e = charCheck(f.ccAuthCode,"num","Card Security Code",e);
		if (e & !f.agreeToTC.checked) {
			alert("You must agree to Michigan Opera's online sales terms and conditions before you can complete your order.");
			f.agreeToTC.focus();
			e = false;
		}
		return e;
	}
	
/// 4.24.06
/// for subs order form

	function formatCurrency(v) {
		var totalAmt = Math.round((v)*100)/100 - ((Math.round((v)*100)%100)*.01);
		var totalCents = Math.round((v)*100)%100;
		if (totalCents > 0) {
			if (totalCents.toString().length == 1) {
				totalCents += "0";
			}
			totalAmt = totalAmt + "." + totalCents;
		} else {
			totalAmt += ".00";
		}
		return totalAmt
	}

	function orderTotals() {
		var f = document.conf;
		if (f.contribTotal.value.search(/[^\d]/ig) == -1) {
		
			var ot = (f.orderTotal.value != "") ? Number(f.orderTotal.value) : 0;
			var ct = (f.contribTotal.value != "") ? Number(f.contribTotal.value) : 0;
			var st = (f._subTotal.value != "") ? Number(f._subTotal.value) : 0;
			var ft = (f.feeTotal.value != "") ? Number(f.feeTotal.value) : 0;
	
			var displayST = ct + st;
			var displayOT = displayST + ft;
	
			f.subTotal.value = displayST;
			f.orderTotal.value = displayOT;
			
			document.getElementById("cartSubTotal").innerHTML = "$" + formatCurrency(displayST);
			document.getElementById("cartTotal").innerHTML = "$" + formatCurrency(displayOT);
		
		} else {
			alert("Please only enter numbers in the donation field.");
			f.contribTotal.value = "";
		}
	}	
	
	function checkPkg() {
		var f = document.subs;
		//alert("id: " + getValue(f.id));
		//alert("selPrice: " + getValue(f.selPrice));
		//alert("f.secondSection: " + getValue(f.secondSection));
		//alert("f.seatNum: " + getValue(f.seatNum));
		var e = true;
		e = charCheck(f.id,"notnull","Series",e);
		if (getValue(f.id) == "RSVP") {
			e = false;
			alert("Please select another series.\n" + f.id[f.id.selectedIndex].text + " is already in your order.");
		}
		e = charCheck(f.selPrice,"notnull","Section",e);		
		e = charCheck(f.secondSection,"notnull","Second Section",e);		
			if (e && getValue(f.secondSection) != "None") {
				if (getValue(f.selPrice).indexOf(getValue(f.secondSection)) > -1) {
					e = false;
					f.secondSection.focus();
					alert("Your primary and secondary seating section choices must be different.");
				}
			}
		e = charCheck(f.seatNum,"notnull","Number of Seats",e);
		if (e && Number(f.prk_numSpots.value) > Number(f.seatNum.value))
		{
			alert("The number of reserved parking spots must be less than or equal to the number of seats.");
			f.prk_numSpots.focus();
			e = false;
		}
		return e;
	}
	
	function checkSeatSel() {
		var f = document.tix;
		//alert("id: " + getValue(f.id));
		//alert("zone: " + getValue(f.zone));
		//alert("f.secondSection: " + getValue(f.secondSection));
		//alert("f.seatNum: " + getValue(f.seatNum_1));
		
		var e = true;
		
		e = charCheck(f.zone,"notnull","Section",e);		
		e = charCheck(f.seatNum_1,"notnull","Number of Seats",e);
		if (e && Number(f.prk_numSpots_0.value) > Number(f.seatNum_1.value))
		{
			alert("The number of reserved parking spots must be less than or equal to the number of seats.");
			f.prk_numSpots_0.focus();
			e = false;
		}
		return e;
	}
	
	function checkFlex() {
		var f = document.ppkg;
		var e = true;
		e = charCheck(f.ppkgID,"notnull","Series Type",e);
		e = charCheck(f.value_perfList,"notnull","Selected Performances",e);
		if (e && f.selperf.size > f.selperf.options.length) {
			e = false;
			alert("You must select " + (f.selperf.size-f.selperf.options.length) + " more performance(s) before continuing.");
		}
		return e;
	}
	
	function checkFlexSeat() {
		var f = document.ppkg;
		var e = true;
		e = charCheck(f.zoneNoPrice,"notnull","Section",e);
		e = charCheck(f.seatNum,"notnull","# of Seats",e);
		return e;
	}
	
	
//-->