var cpa = false;
var regemail = new RegExp("^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$");

Event.observe(window,"load",function(ev) {
	
	$$(".registration_form").invoke("observe","submit", function(ev) {
		ev.stop();
		
		first_el = false;
		errors = 0;

		// Special stuff for PAC
		if (formtype == "online-pac") {
			if (!$("us_citizen").checked) {
				new Effect.Appear($("us_citizen").up("p").next("div.error"));
		    	errors++;
		    	if (!first_el)
		    		first_el = $("us_citizen");
			} else {
				$("us_citizen").up("p").next("div.error").hide();
			}
			
			if (!$("contribute").checked) {
				new Effect.Appear($("contribute").up("p").next("div.error"));
		    	errors++;
		    	if (!first_el)
		    		first_el = $("contribute");
			} else {
				$("contribute").up("p").next("div.error").hide();
			}
			
			if ($("total").value == "" || parseInt($("total").value) == NaN) {
				new Effect.Appear("amount_error");
				errors++;
				if (!first_el)
					first_el = $$("div.tabbed")[0];
			} else {
				$("amount_error").hide();
			}
		}
		// End Special For PAC

		// Dairy Forum Special
		if (formtype == "dairy-forum") {
			if (user_type_price == 0) {
				new Effect.Appear("reg_error");
				first_el = $("reg_error").previous("h3");
				errors++;
			} else
				$("reg_error").hide();
		}
		
		// Validate numeric fields.
		$$(".validate_numeric").each(function(el) {
			num = parseInt(el.value);
			if (isNaN(num)) {
				new Effect.Appear(el.up("p").next("div.error"));
				first_el = el.up("p");
				errors++;
			}
		});
		
		$$("input.required, select.required").each(function(ell) {
		    if (ell.value != "")
		    	ell.up().next("div.error").hide();
		});
		$$("input.required, select.required").each(function(ell) {
		    if (ell.value == "") {
		    	new Effect.Appear(ell.up().next("div.error"));
		    	errors++;
		    	if (!first_el)
		    		first_el = ell.up("div");
		    }
		});
		//$$("input.email").each(function(ell) {
		// if (!regemail.test(ell.value)) {
		//    	new Effect.Appear(ell.up().next("div.error"));
		//    	errors++;
		//    	if (!first_el)
		//    		first_el = ell.up("div");
		//    }
		//});
		
		if (errors == 0)
			this.submit();
		else
			Effect.ScrollTo(first_el);
	
	});
	
	$$(".pac_amount").invoke("observe","click",function(ev) {
		if (this.value == "other") {
			$("total").value = $("other_amt").value;
			cpa = true;
		} else {
			$("total").value = this.value;
			cpa = false;
		}
	});
	
	if ($("other_amt")) {
		$("other_amt").observe("keyup",function(ev) {
			if (cpa)
				$("total").value = parseInt(this.value);
		});
	}
	
	// All stuff for Board Meeting Page
	if ($("number_registering")) {
		$("number_registering").observe("change", function(ev) {
			num = parseInt(this.value);
			if (isNaN(num)) {
				new Effect.Appear(this.up("div").down("div.error"));
				num = 0;
			} else
				this.up("div").down("div.error").hide();
			onum = parseInt($("number_playing_golf").value);
			if (isNaN(onum))
				onum = 0;
			total = (num * 250) + (onum * 165);
			$("total_span").innerHTML = "$" + total + ".00";
			$("total").value = total;
		});
		$("number_playing_golf").observe("change", function(ev) {
			num = parseInt(this.value);
			if (isNaN(num)) {
				new Effect.Appear(this.up("div").down("div.error"));
				num = 0;
			} else
				this.up("div").down("div.error").hide();
			onum = parseInt($("number_registering").value);
			if (isNaN(onum))
				onum = 0;
			total = (onum * 250) + (num * 165);
			$("total_span").innerHTML = "$" + total + ".00";
			$("total").value = total;
		});
	}
});