function checkoutnav(step) {
	var doinput = document.getElementById("do");
	var vinput = document.getElementById("v");
	var checkform = document.getElementById("checkoutform");
	if (vinput.value == 1 && step == 2) {
		var valid = valform(checkform);
		if (valid != true) {
			alert("The following field(s) must be completed to continue:\n\n" + valid);
			return false;
		} else {
			doinput.value = "step";
			vinput.value = step;
			checkform.submit();
		}
	} else {
		doinput.value = "step";
		vinput.value = step;
		checkform.submit();
	}
}
function valform(thisform) {
	var checkfields = new Array('s_first','s_last','s_addr1','s_city','s_phone','b_first','b_last','b_addr1','b_city','b_phone','cardname','cardnum');
	var fieldnames = new Array('Shipping First Name','Shipping Last Name','Shipping Address Line 1','Shipping City','Shipping Phone','Billing First Name','Billing Last Name','Billing Address Line 1','Billing City','Billing Phone','Cardholder Name','Card Number');
	var valid = true;
	var valmessage = "";
	for (var i=0; i<checkfields.length; i++) {
		thisfield = thisform.elements[checkfields[i]];
		if (thisfield.value == "" || thisfield.value == null) {
			valid = false;
			valmessage = valmessage + fieldnames[i] + "\n";
			thisfield.style.borderColor='#C00';
		} else {
			thisfield.style.borderColor='#888';
		}
	}
	thisfield = thisform.elements['cardmonth'];
	if (thisfield.options[thisfield.selectedIndex].value == "Month") {
		valid = false;
		valmessage = valmessage + "Card Expiration Month\n";
		thisfield.style.borderColor='#C00';
	} else {
		thisfield.style.borderColor='#888';
	}
	thisfield = thisform.elements['cardyear'];
	if (thisfield.options[thisfield.selectedIndex].value == "Year") {
		valid = false;
		valmessage = valmessage + "Card Expiration Year\n";
		thisfield.style.borderColor='#C00';
	} else {
		thisfield.style.borderColor='#888';
	}
	if (valid) return true;
	else return valmessage;
}
function calcship() {
	var shipping = document.getElementById("shipping");
	var doinput = document.getElementById("do");
	var scinput = document.getElementById("sc");
	var form =  document.getElementById("checkoutform");
	var shipcost = shipping.options[shipping.selectedIndex].value;
	doinput.value = "ship";
	scinput.value = shipcost;
	form.submit();
}
function dobillsame() {
	if (document.getElementById("billsame").checked) {
		var shipfields = new Array('s_first','s_middle','s_last','s_suffix','s_company','s_addr1','s_addr2','s_addr3','s_city','s_state','s_zip','s_country','s_email','s_phone');
		var billfields = new Array('b_first','b_middle','b_last','b_suffix','b_company','b_addr1','b_addr2','b_addr3','b_city','b_state','b_zip','b_country','b_email','b_phone');
		for (var i=0; i<shipfields.length; i++) {
			document.getElementById(billfields[i]).value = document.getElementById(shipfields[i]).value;
		}
		document.getElementById("b_title").selectedIndex = document.getElementById("s_title").selectedIndex;
	}
}
function calcstate() {
	var state = $("#s_state").val().toLowerCase();
	var subtotal = $("#subtotal").html().substring(1, $("#subtotal").html().length);
	var finaltotal = $("#final").html().substring(1, $("#final").html().length);
	var shipcost = $("#shipping").val();
	var newtotal = parseFloat(subtotal) + parseFloat(shipcost);
	if (state == "tx" || state == "tex" || state == "texas") {
		var taxtotal = parseFloat(newtotal * 0.0825).toFixed(2);
		$("#txtax").html("$"+taxtotal);
		$("#tax_cost").val(taxtotal);
		var finalcost = parseFloat(parseFloat(newtotal) + parseFloat(taxtotal)).toFixed(2);
		$("#final").html("$"+finalcost);
		$("#final_total").val(finalcost);
		$("#s_state").val("TX");
	} else {
		$("#txtax").html("$0.00");
		var finalcost = parseFloat(newtotal).toFixed(2);
		$("#final").html("$"+finalcost);
		$("#final_total").val(finalcost);
	}
}

$(function () {
	  $('.bubbleInfo').each(function () {
			var distance = 10;
			var time = 250;
			var hideDelay = 500;

			var hideDelayTimer = null;

			var beingShown = false;
			var shown = false;
			var trigger = $('.trigger', this);
			var info = $('.popup', this).css('opacity', 0);


			$([trigger.get(0), info.get(0)]).mouseover(function () {
				 if (hideDelayTimer) clearTimeout(hideDelayTimer);
				 if (beingShown || shown) {
					  // don't trigger the animation again
					  return;
				 } else {
					  // reset position of info box
					  beingShown = true;

					  info.css({
							top: -90,
							left: -33,
							display: 'block'
					  }).animate({
							top: '-=' + distance + 'px',
							opacity: 1
					  }, time, 'swing', function() {
							beingShown = false;
							shown = true;
					  });
				 }

				 return false;
			}).mouseout(function () {
				 if (hideDelayTimer) clearTimeout(hideDelayTimer);
				 hideDelayTimer = setTimeout(function () {
					  hideDelayTimer = null;
					  info.animate({
							top: '-=' + distance + 'px',
							opacity: 0
					  }, time, 'swing', function () {
							shown = false;
							info.css('display', 'none');
					  });

				 }, hideDelay);

				 return false;
			});
	  });
 });

function openTicket(logon_mem_id,logon_valid_id) {
	winOpts = "height=840,width=670,scrollbars=no,location=no,toolbar=yes,menubar=no,resizable=no,status=no";
	var cert = window.open("certprinter.php?logon_mem_id="+logon_mem_id+"&logon_valid_id="+logon_valid_id,"_blank",winOpts);
	cert.print();
	//cert.close();

}

function openResults(logon_mem_id,logon_valid_id) {
	winOpts = "height=800,width=800,scrollbars=no,location=no,toolbar=yes,menubar=no,resizable=no,status=no";
	var exam = window.open("printexamresults.php?logon_mem_id="+logon_mem_id+"&logon_valid_id="+logon_valid_id,"_blank",winOpts);
	exam.print();
	//exam.close();
}

function examAnswerKey(answer_name,answer_value) {
	if(!validAnswer(answer_value)) {
		alert("You can only answer with A, B, C, D, E, F or T in either upper or lower case.");
		document.getElementById(answer_name).value = "";
		return;
	}
	
	document.getElementById(answer_name).value = document.getElementById(answer_name).value.toUpperCase();
	var theNum = (parseInt(answer_name.split("_")[1])) + 1;
	if(document.getElementById("answer_"+theNum))	document.getElementById("answer_"+theNum).focus();
}

function validAnswer(value) {
	allowedChars = "abcdeftABCDETF";

	for(i=0;i<value.length;i++) {
		theChar = value.charAt(i);
		if(allowedChars.indexOf(theChar,0) == -1) return false;
	}
	return true;
}

function testsubmit() {
	return confirm("Are you sure you are ready to submit your exam? (You can only submit it once, so make sure you are satisfied with it before answering YES)");
}
