// Helper for loading access codes

function isAccessCodeValid (code) {
	// Given a code, return whether it is a valid code or not
	var program = $("input[name='test_selection_radio']:checked").val();
	
	// Make sure program is set.  If not, show error msg and quit.
	if (typeof(program) == "undefined") {
		$('#accesscodefeedback').text("You must select a test before entering code.");
		return false;
	}
	
	
	uppercasecode = $.trim(code.toUpperCase());
	//console.log("Checking code " + uppercasecode + " against program: " + program);
	foundmatch = false;
	// Iterate over the json access code data.  Return true if we see it.  If not, return false:
	$.each($("#accesscodefield").data('accesscodedata'), function() {
		//console.log("AccessCode " + this["code"] + " - Code Entered: " + uppercasecode + " against program: " + program);
		// if the code is in the json file, AND the last character program identifier (P or E) matches the Program
		if (uppercasecode == this["code"]
			&& uppercasecode.charAt(uppercasecode.length-1)== program.charAt(0).toUpperCase() ){
				//console.log("MATCH!");
			foundmatch= true;
			// Set the name of the contract
			$("#accesscodefield").data('contractname', this["name"])
		}
	});
	
	
	return foundmatch;
}

function loadAccessCodes() {
	//debugger;
	//console.log("in loadaccesscodes")
//	$.getJSON('js/accesscodes.json', function (json) {
		//console.log("in loadaccesscodes getJSON callback")
		//accesscodes_json = json;

		$("#accesscodefield").data('accesscodedata', $.json.decode($('#contracts').val()));
		//console.log(json[0]);
//	});
	
}
