// [request here] links handler
// Adds the selected="selected" attributed to the
// correct option tag and removes it from the old one
function onAclick(e) {
	var dom = $('select[name="platform"] option');
	dom.filter('[selected="selected]').removeAttr("selected");
	dom.eq(e.data.index + 1).attr('selected', 'selected');
}

$(document).ready(function() {
	// handle the onclick event for the [request here] links
	$('#platforms-list li').each(function (i) {
		$(this).bind("click", {index: i}, onAclick);
	});
	
	// handle the onchange event on the country select
	$('select[name="country"]').change(function () {
		var stateRow = $('select[name="state"]').parent().parent();
		// check for USA (value = 222) and display
		// state select if so, or hide the state select
		// if it was visible before the change
		
		if (this.value === "222") {
			if (stateRow.is(':hidden')) {
				stateRow.show();
			}
		} else if (stateRow.is(':visible')) {
			stateRow.hide();
		}
	});
	
	// handle the onclick event for the approve checkbox
	// The handler toggles the visibility of the required divs
	$('input[name="approve"]').click(function () {
		// if checked, display all the following fields		
		if ($(this).attr('checked') === true) {
			$(this).parent().nextAll().show();
		} else {
			// else hide them
			$(this).parent().nextAll().hide();
		}
	});
	
	//
	$('select[name="platform"]').change(function () {
		var dom = $(this).next();
		if ($(this).val() === "other" && dom.is(":hidden")) {
			dom.show();
		} else if (dom.is(":visible")) {
			dom.hide();
		}
	});
});
