// orderentry.js
// Controls the display logic of questions on education/order/index.html


$(document).ready(function() {

$('#districtorderinfo').cluetip({
    splitTitle: '|',
    arrows: true,
    activation: 'click',
	closePosition: 'title',
	closeText: 'X',
    sticky: true,
    onActivate: function(e) {
        if (typeof urchinTracker != 'undefined') {
            urchinTracker('/education/order/districtorderinfo');
        }
        return true;
    }

});
$('#schoolorderinfo').cluetip({
    splitTitle: '|',
    arrows: true,
    activation: 'click',
	closePosition: 'title',
	closeText: 'X',
    sticky: true,
    onActivate: function(e) {
        if (typeof urchinTracker != 'undefined') {
            urchinTracker('/education/order/schoolorderinfo');
        }
        return true;
    }
});
$("#account_contract").hide();
$("#goto_orderform").hide();
$("#accesscodefield").hide();
$("#accesscodefield").val("");
$("#customer_type").hide();
$("input[name='test_selection_radio']").removeAttr('checked');

//for MSIE, we need to respond to the click event as well as the change event
var evt = $.browser.msie ? "click" : "change";

// Callback for when change of the test selection
$("input[name='test_selection_radio']").bind(evt, function() {
	//console.log("Test Selection changed");
	test = $(this).val().toUpperCase();
	$("#account_contract").fadeOut("fast");
	$("#accesscodefield").val("");
	$("#accesscodefeedback").text("");
	$("#customer_type").fadeOut("fast", function () {
		//clear out any checked values of the account_contract_radio
		$("input[name='customer_type_radio']").removeAttr('checked');
		
		$("#goto_orderform").fadeOut("fast");
		
		//console.log(test);
		

		$("input[name='customer_type_radio']:checked").focus();
	}).fadeIn();
	
	
	$("#goto_orderform:visible").each(function () {
		setOrderFormLink();
	})
		
});

$("input[name='customer_type_radio']").bind(evt, function() {
	//console.log("customer type change");
	var type = $(this).val();
	
	
	$("#account_contract").fadeOut("fast", function () {
		//clear out any checked values of the account_contract_radio
		$("input[name='account_contract_radio']").removeAttr('checked');
		$("#goto_orderform").fadeOut("fast");
		
		//console.log(type);
		if (type == 'district'){
			$("#account_contract legend").text("Do you have a contract access code?");
			$("label[for='account_contract_radio_yes']").text("Yes, I have it right here!")
			$("label[for='account_contract_radio_no']").text("No, my district will be paying for this testing program (and will NOT be reimbursed as part of a contract).")
			$("label[for='account_contract_radio_lost']").text("No, but I am testing as part of a contract.")
			$("#accesscodefield").show();
			

		}
		else if (type == 'school'){
			var test_strings = 
			{ "PLAN": "a PLAN",
			  "EXPLORE": "an EXPLORE"
			};
			//var test set in the callback for test_selection
			$("#account_contract legend").text("Do you have " + test_strings[test] + " account username and password?");
			$("label[for='account_contract_radio_yes']").text("Yes, I have it right here!")
			$("label[for='account_contract_radio_no']").text("No, I do not.")
			$("label[for='account_contract_radio_lost']").text("Yes I do, but I cannot find it")
			$("#accesscodefield").hide();
			$("#accesscodefeedback").text("");
		}
		$("input[name='customer_type_radio']:checked").focus();
	}).fadeIn();
	
	
	$("#goto_orderform:visible").each(function () {
		setOrderFormLink();
	})
	
	
			
});

$("input[name='account_contract_radio']").bind(evt, function() {
	 //console.log("account_contract_radio changed");
	
	//      console.log(val);
	
	 val = $(this).val();
	//console.log("entering if statement");
   if (val == "yes"){
	//console.log("enable logic");
	$("#accesscodefield").removeAttr("disabled");
   }
   else {
	//console.log("disable logic");
	$("#accesscodefield").attr("disabled", "disabled");
	$("#accesscodefield").val("");
	$("#accesscodefeedback").text("");
   }
	//console.log("passed over if statement");
	
	$("input[name='account_contract_radio']:checked").focus();
	setOrderFormLink();
	
})

// This function hides/shows the link to the order form based on the selections entered by the user
function setOrderFormLink () {
	//console.log("setOrderFormLink")
	var selectedform = $("input[name='customer_type_radio']:checked").val() + $("input[name='account_contract_radio']:checked").val();
	//console.log(selectedform);
	formlink = 
	{"text": {
		"schoolyes":"Place a Single School Order",
		"schoolno":"Place a Single School Order",
		"districtyes":"Place a District Contract Order",
		"districtno":"Place a District Order",
		"schoollost":"Please contact " + test + " Customer Services at ",
		"districtlost":"Please contact " + test + " Customer Services at "
		},
	"url": {
		"schoolyes":"http://www.act.org/"+test.toLowerCase()+"/order",
		"schoolno":"form.php?t=2&p=" + test,
		"districtyes":"form.php?c=",
		"districtno":"form.php?p=" + test + "&t=1",
		"schoollost":"#",
		"districtlost":"#"
		}
	
	}

	// If they have a district contract code, remove the link.
	// This link should only be displayed if they have a valid contract code
	// which we check for in the #accesscodefield.keyup callback
	if (selectedform == 'districtyes'){
		$("#goto_orderform").fadeOut("fast");
		
	}
	else {
		$("#goto_orderform").fadeOut("fast", function () {
		
		
			//set link text and url
			// if they've lost the letter, remove the order form link
			$("#orderformlink").text(formlink["text"][selectedform]);
	
			if (selectedform == "districtlost" || selectedform == "schoollost") {
				$("#orderformlink").removeAttr("href");
				//append contact details for appropriate test
				// NOTE - these phone numbers are now stored in the database
				// We should probably be pulling the phone numbers from there
				if (test == 'PLAN'){
					$("#orderformlink").append("800/553-6244, ext. 1029.")
				} else if (test == 'EXPLORE'){
					$("#orderformlink").append("800/553-6244, ext. 1892.")
				};
				$('#orderformlink').addClass('lost');
			}
			else{
				$('#orderformlink').removeClass('lost');
				$("#orderformlink").attr("href", formlink["url"][selectedform]);
			};
			
		}).fadeIn();
	}
}

// After each key press, check if the entered code is valid
// Only show the order form link if the entered contract code is valid
$("#accesscodefield").keyup(function () {
	//console.log("Keyup")
	if ($(this).val().length > 0){
		code = $.trim($(this).val());
		isValidCode = isAccessCodeValid(code);
		//console.log(isValidCode);
		if (isValidCode){
			var validationtxt = $("#accesscodefield").data('contractname');
			$(this).next().text("Valid Code: " + validationtxt)
			$("#orderformlink").text(formlink["text"]["districtyes"]);
			$("#orderformlink").attr("href", formlink["url"]["districtyes"] + code);
			$("#goto_orderform").fadeIn('fast');
			}
		else {
			$(this).next().text("Unrecognized Code!")
			$("#goto_orderform").fadeOut();
			}
		}
	}
	
)

// load the access codes

loadAccessCodes();


});


	
