$(function() {
	var cp = 1; // current page
	var op = null; // old page

	$(".multipage").filter(":first").show();

	disable('prev');
	if ($(".multipage").length < 2)
		disable('next');
	else
		enable('next');

	$("#page_prev").click( function() {
		if (!$(this).attr("disabled")) {
			if (cp > 1) {
				op = cp;
				cp--;
			}
			redraw();
		}
	});

	$("#page_next").click( function() {
		if (!$(this).attr("disabled")) {
			if (cp < $(".multipage").length) {
				op = cp;
				cp++;
			}
			redraw();
		}
	});

	function redraw() {
		if ($("#current_page").length) $("#current_page")[0].innerHTML = cp;
		if (cp == 1)
			disable('prev');
		else
			enable('prev');
		if (cp == $(".multipage").length)
			disable('next');
		else
			enable('next');
		$(".multipage").each(function(i) {
			if ((i+1) == op)
				$(this).fadeOut("fast", function() {
					$(".multipage").each(function(i) {
						if ((i+1) == cp)
							$(this).fadeIn("fast");
					});
				});
		});
	}

	function disable(p) {
		$("#page_"+p).fadeTo(0, .5);
		$("#page_"+p).removeClass("nav");
		$("#page_"+p).addClass("disabled_nav");
		$("#page_"+p).hover(function() {}, function() {});
	}

	function enable(p) {
		$("#page_"+p).fadeTo(0, 1);
		$("#page_"+p).addClass("nav");
		$("#page_"+p).removeClass("disabled_nav");
		$("#page_"+p).hover(function() {
					$(this).addClass("nav_hover");
				}, function() {
					$(this).removeClass("nav_hover");
				});
	}
});

function viewInvoice(id) {
  	window.open('invoice.php?orderid='+id,'invoice','scrollbars=1,resizable=1,width=640,height=500');
}

function validatePO() {
	var invalidOption = validateInput();
	if (!invalidOption) invalidOption = validateSelect();
	return !invalidOption;
}

function validateBangle() {
	var invalidOption = validateSelect();
	if (!invalidOption) {
		if (($('#po496')[0].value == "4385") && ($('#po498')[0].value == "")) {
			alert('Please specify at least one and up to three letters');
			$('#po498')[0].focus();
			invalidOption = true;
		} else if (($('#po496')[0].value == "4379") && ($('#po498')[0].value != "")) {
			alert('Please select Yes if you would like to enter letters');
			$('#po496')[0].focus();
			invalidOption = true;
		}
	}
	return !invalidOption;
}

function validateInput() {
	var invalidOption = false;
	$('input').filter(function() {
		return (this.id && (this.id.substr(0, 2) == "po"));
	}).each(function() {
		if (this.value == '') {
			alert('Please specify product option text');
			this.focus();
			invalidOption = true;
			return false;
		}
		return true;
	});
	return invalidOption;
}

function validateSelect() {
	var invalidOption = false;
	$('select').filter(function() {
		return (this.id && (this.id.substr(0, 2) == "po"));
	}).each(function() {
		if (this.selectedIndex == 0) {
			var alertSuffix = "product option";
			// assumes first entry is "Select Foo"
			if (this.options[0].text.substr(0, 7) == "Select ") alertSuffix = this.options[0].text.substr(7);
			alert('Please specify '+alertSuffix);
			this.focus();
			invalidOption = true;
			return false;
		}
		return true;
	});
	return invalidOption;
}