$(document).ready(function() {

	//open any anchor with a rel="" in a new window
	if($("a[@rel]")){
		$("a[@rel]").click(function() {
			newWindow = window.open(this, "newWin", "width=750,height=550,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes")
			newWindow.focus();
			return false; //stop link from working normally
		});
	}

	//removing and replacing the "Search" text in the search field
	$("#q").focus(function() {
		if($("#q").val() == 'Search'){ $("#q").val(""); }
	});
	$("#q").blur(function() {
		if($("#q").val() == ''){ $("#q").val("Search"); }
	});

	//delete all cart contents
	$("#delete-cart").click(function() {
	var answer = confirm("Do you want to DELETE ALL ITEMS?");
    if(answer){
		$.ajax({
			type: "POST",
			url: "delete.pl",
			data: "del=reset",
			success: function() {
				$("tbody").remove();
					alert( "Your Shopping List has been deleted!");
			}//end success
		});//end $.ajax
	}//end if
	});

	//delete item from cart on order1.pl page
	$(".delete-item").click(function() {
		/*var itemAnswer = confirm( "Are you sure you want to delete this item?");
		var item = $(this).val();
		if(itemAnswer){
		$.ajax({
			type: "POST",
			url: "delete.pl",
			data: "del=" + item,
			success: function() {*/
				window.location = "order2.pl";//window.location;
		/*		alert( "Your item has been deleted!");
			}//end success
		});//end $.ajax
		}//end if*/
	});

	//fade and round "Success" div on cart addition
	$("#success").fadeOut(6000);
	$("#success").corner();

	//redirect to cart when clicking "view cart" button
	$("#view-cart").click(function(){
		window.location = "order2.pl";
	});

	//redirect to 10% page when clicking "continue shopping" button
	$("#continue-shopping").click(function(){
		window.location = "order1.pl?type=10";
	});

	//validate form when clicking the purchase button on billing page
	$("#purchase").click(function(){
		val();
	});

	//toggle "same as billing" in shpping area of billing page
	$("#ship").click(function(){
		reBill = /shipping/
		re = /--req/
		var xLen = document.form_val.length
		for(x=0;x<xLen;x++) {
			if (reBill.test(document.form_val.elements[x].name)) {
				if (re.test(document.form_val.elements[x].name)) {
					if (document.form_val.elements[x].value == "same as billing"){document.form_val.elements[x].value = "";}
					else if (document.form_val.elements[x].value == ""){document.form_val.elements[x].value = "same as billing";}
				}
			}
		}
	});

	//flash successful cart addition on order1.pl and no items in cart on order2.pl
	if($("#flash"))
	{
		$("#flash").animate({ opacity: 0.0 }, 500 ).animate({ opacity: 1.0 }, 500 ).animate({ opacity: 0.0 }, 500 ).animate({ opacity: 1.0 }, 500 ).animate({ opacity: 0.4 }, 1500 );
	}

	//jump to subject on order1.pl using anchor tags in table
	if($("#jump select"))
	{
		$("#jump select").change(function(){
			var target = $("#jump select").val();
			$("#jump select").val("");
			var page = window.location;
			page = page.toString();
			var newPage = new Array();
			newPage = page.split('#');
			window.location = newPage[0] + "#" + target;
		});
	}

	/* :KLUDGE: fixes non-working :hover class in IE/5+ */
	$("#search").hover(
		function () {
			$("#search").addClass("searchHover");
		},
		function () {
			$("#search").removeClass("searchHover");
		}
	);

});

