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) {
	
	if ($$(".credit_card_input").length == 0) {
		$$(".row").invoke("observe","click", function(ev) {
		    ev.stop();
		    div = this.next("div");
		    if (div.style.display == "none") {
		    	div.show();
		    	this.addClassName("row_expanded");
		    } else {
		    	div.hide();
		    	this.removeClassName("row_expanded");
		    }
		});
	}

	$$(".add_to_cart").invoke("observe","click",function(ev) {
		ev.stop();
		document.location.href = "http://www.idfa.org/bookstore/cart/add/" + this.readAttribute("title") + "/";
	});
	
	$$(".add_to_cart").invoke("observe","mouseover",function(ev) {
		this.addClassName("add_to_cart_hover");
	});

	$$(".add_to_cart").invoke("observe","mouseout",function(ev) {
		this.removeClassName("add_to_cart_hover");
	});
	
	$$("form.checkout").invoke("observe","submit",function(ev) {
		ev.stop();
		errors = 0;
		var first_el;
		$$("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);
	});
	
	if ($("same_as_billing")) {
		$("same_as_billing").observe("click",function(ev) {
			if (this.checked) {
				$$("#billing input[type=text]").each(function(el) {
					eval("el.value = billing_info." + el.readAttribute("name") + ";");
				});
				$$("#billing select").each(function(el) {
					eval("tval = billing_info." + el.readAttribute("name") + ";");
					x = 0;
					while (x < el.options.length) {
						if (el.options[x].value == tval)
							el.selectedIndex = x;
						x++;
					}
				});
			}
		});
	}
});