$(document).ready(function() {
	
	//focus the cursor in any input with the id #formFocus
	$("#formFocus").focus();
	
	if (typeof $.fn.tipsy == "function") {
		$(".hoverInfo").tipsy({gravity:'w',fade:true});
	}
	
	/********************************
	 * FORM 1
	 ********************************/
	//hide existing customer box
	if($("#existingCustomerYes").is(":checked"))
		$("#existingCustomer").show();
	else
		$("#existingCustomer").hide();
		
	//toggle existing customer box
	$("#existingCustomerYes").click(function(){
		$("#existingCustomer").show();
	});
	$("#existingCustomerNo").click(function(){
		$("#existingCustomer").hide();
	});
	
	/********************************
	 * FORM 2
	 ********************************/
	if ($("#stocklist").length) {
		stockGame.init();
		
		//if the page is refreshed, modern browsers remember which were checked - so need to run this to update the page
		$.each($("input[id^='stockOption_']:checked"), function(key, val) {
			var arrUId = $(this).attr("id").split("_");
			var uId = arrUId[1];
	    	stockGame.addStockSelection(uId);
		});
		
	    $("input[id^='stockOption_']").click(function() {
	    	var arrUId = this.id.split("_");
			var uId = arrUId[1];
	    	if($(this).is(":checked"))
	    		stockGame.addStockSelection(uId);
	    	else
	    		stockGame.removeStockSelection(uId);
		});
	}
	
	/********************************
	 * THANK YOU / PRIZE DRAW
	 ********************************/
	//hide existing customer box
	if ($("#requestEquityInsight").is(":checked")) {
		$("#equityInsight").show();
		$("#address1").focus();
	} else 
		$("#equityInsight").hide();
		
	//toggle existing customer box
	$("#requestEquityInsight").click(function(){
		if ($("#requestEquityInsight").is(":checked")) {
			$("#equityInsight").show();
			$("#address1").focus();
		} else 
			$("#equityInsight").hide();
	});
	
//end (document).ready(function() 	
});

stockGame = {
	limit:10,
	hiClassName:"selected",
	selectedHiClassName:"stockselected",
	disabledClassName:"disabled",
	arrStockSelection:[],
	arrStockSelectionDisplay:[],
	init:function() {
		this.arrStockSelectionDisplay = $("[id^='stockSelection_']");
	},
	addStockSelection:function(stockID) {
		this.arrStockSelection.push(stockID);
		this.updateStockSelectionDisplay();
		this.highlightOption(stockID, 1);
		if(this.arrStockSelection.length >= this.limit)
			this.disableStockOptions();
	},
	removeStockSelection:function(stockID) {
		this.arrStockSelection.splice(jQuery.inArray(stockID,this.arrStockSelection), 1);
		this.updateStockSelectionDisplay();
		this.highlightOption(stockID, 0);
		if(this.arrStockSelection.length == this.limit-1)
			this.enableStockOptions();
	},
	clearStockSelectionDisplay:function() {
		this.arrStockSelectionDisplay.removeClass(this.selectedHiClassName).text("Empty");
	},
	updateStockSelectionDisplay:function() {
		this.clearStockSelectionDisplay();
		var selectedHiClassName = this.selectedHiClassName;
		$.each(this.arrStockSelection, function(key, val) {
			$("#stockSelection_" + key).addClass(selectedHiClassName).text($("#stockTitle_" + val).text());
		});
	},
	enableStockOptions:function() {
		var target = $("input[id^='stockOption_']:disabled");
		var targetParent = target.parent().parent();
		target.attr("disabled", false);
		targetParent.removeClass(this.disabledClassName);
	},
	disableStockOptions:function() {
		var target = $("input[id^='stockOption_']:not(:checked)");
		var targetParent = target.parent().parent();
		target.attr("disabled", true);
		targetParent.addClass(this.disabledClassName);
	},
	highlightOption:function(stockID, hi) {
		var target = $("#stockRow_" + stockID);
		if(hi)
			target.addClass(this.hiClassName);
		else
			target.removeClass(this.hiClassName);
	}
};
