$(document).ready(function() {
	//Tabs
	var selectedTab = $($(".find-nav ul li").first());
	
	var selectTab = function(tab){
		if(!tab)
			return;
		selectedTab = tab;
		
		tab.addClass("active");
		
		var linkedId = tab[0].getAttribute("linkedTabId");
		$("#" + linkedId).css("display", "");
	};
	var deselectTab = function(tab)
	{
		if(!tab)
			return;
		tab.removeClass("active");
		
		var linkedId = tab[0].getAttribute("linkedTabId");
		$("#" + linkedId).css("display", "none");
	}
	
	$(".find-nav ul li").mousedown(function(event){
		event.preventDefault();
	});

	$(".find-nav ul li").click(function(event){
		event.preventDefault();
		var tab = $(this);
		deselectTab(selectedTab);
		selectTab(tab);
	});
	
	var getNextTab = function(){
		var selectedTabIndex = parseInt(selectedTab[0].getAttribute("tabIndex"));
		var newSelectedTabIndex = selectedTabIndex == $(".find-nav ul li").length ? selectedTabIndex: selectedTabIndex + 1;
		var selector = 'li[tabIndex="' + newSelectedTabIndex + '"]';
		return $(selector); 
	};
	
	var getPreviousTab = function(){
		var selectedTabIndex = parseInt(selectedTab[0].getAttribute("tabIndex"));
		var newSelectedTabIndex = selectedTabIndex == 1 ? selectedTabIndex: selectedTabIndex - 1;
		var selector = 'li[tabIndex="' + newSelectedTabIndex + '"]';
		return $(selector); 
	};
	
	$(".find-finish ul li.prev a").click(function(event){
		event.preventDefault();
		var prevTab = getPreviousTab();
		if (prevTab[0].id != selectedTab[0].id) 
		{
			deselectTab(selectedTab);
			selectTab(prevTab);
		}
	});
	
	$(".find-finish ul li.next a").click(function(event){
		event.preventDefault();
		var prevTab = getNextTab();
		if (prevTab[0].id != selectedTab[0].id) 
		{
			deselectTab(selectedTab);
			selectTab(prevTab);
		}
	});
});

function submitForm()
{
	var attributes = $('.jqTransformHidden');    
    attributes = JSON.stringify(attributes.serializeObject());    

    var dest_url = parent.location;
    dest_url = dest_url + '?aff-cash-portal=results';
    dest_url = dest_url + '&att=' + attributes;    

    // console.log (dest_url);
    parent.location = dest_url;
    return false;
}

