jQuery(document).ready(function() {

 
});
function fnActiveTabCssupdate(tabid) {
     	if (tabid==0){jQuery("#tabsheader").css("background-Position","top");}
     	if (tabid==1){jQuery("#tabsheader").css("background-Position","0 -40px");}
     	if (tabid==2){jQuery("#tabsheader").css("background-Position","0 -80px");}
     	if (tabid==3){jQuery("#tabsheader").css("background-Position","bottom");}
}



var tabcount=4
jQuery(function() {
	
	//initialize jquery objects in use on the page 
	jQuery("#registrationtabs").tabs({
	    select: function(event, ui) {
			var isvalid =  jQuery("#registrationform").validate().form();
			var selectedTab = jQuery( "#registrationtabs" ).tabs( "option", "selected" );
			thisimgid = "#tabs"+ selectedTab + "img"
			if (isvalid) {
			jQuery(thisimgid).attr("src","clientincludes/templates/windfall/images/registration3btabs/valid.png");
			fnActiveTabCssupdate(ui.index)
			}else{
			jQuery(thisimgid).attr("src","clientincludes/templates/windfall/images/registration3btabs/not_valid.png");
			} 
			
	     	return isvalid;
	    }
	});
	// initialise -- Tab start up --> we will disable all but the first tab
	tabcount = jQuery('#registrationtabs').tabs( "length" );
	if (tabcount>1) {
	for(var tt=1;tt<=(tabcount-1);tt++) {
		jQuery("#registrationtabs").tabs('disable', tt) ;
	}
	}
	jQuery("#registrationtabs").tabs( "option", "selected", 0 );
	
	jQuery("#registrationtabs").css('visibility','visible');
	
	jQuery("#dialog" ).dialog();
	
	jQuery('#securityquestioncustom').change(function() {
		var activeval = jQuery("#securityquestioncustom").val(); /* alert(jQuery('select#securityquestion').val()); */
		if (activeval.length>0 &&jQuery('select#securityquestion').val() !=""){
		jQuery('select#securityquestion').get(0).selectedIndex = 0;
		}

	});
	
	jQuery('select#securityquestion').change(function() {
	  var activeval = jQuery("select#securityquestion").val(); /* alert(jQuery('select#securityquestion').val()); */
	  if (activeval.length>0 ){  jQuery("#securityquestioncustom").attr("value","");}
	 });  
	
	

	// validate signup form on keyup and submit
	var validator = jQuery("#registrationform").validate({
		//debug: true,
  		ignore: ':hidden',
		onkeyup: false,
		onblur: false,
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {  
			var selectedTab = jQuery( "#registrationtabs" ).tabs( "option", "selected" );
			error.appendTo(jQuery("#" +  element.attr('id') + "status"));
			var thislabel = "#" +  element.attr('id') + "req"; 
			jQuery(thislabel).addClass("error");
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
			var Adderrorfield = "";
			if (label.attr('for') =="securityquestion"){ Adderrorfield = "securityquestioncustom";}
			if (label.attr('for') =="securityquestioncustom"){ Adderrorfield = "securityquestion";}
			if (Adderrorfield.length>0){
				var thislabel = "#" +  Adderrorfield + "status"; 
				jQuery(thislabel).html("&nbsp;").addClass("checked");
			
			}
		}
	});
	


	// back buttons do not need to run validation
	jQuery("#formBack").click(function(){
		current = 0;
	    jQuery("#registrationtabs").tabs('enable', current) ;
	    jQuery("#registrationtabs").tabs("option", "selected", current );
	}); 
	jQuery("#registrationtabs :button").click(function(){
		var selectedTab = jQuery( "#registrationtabs" ).tabs( "option", "selected" );
		current = selectedTab +1;
		if  (jQuery("#registrationform").validate().form() ) {
			thisimgid = "#tabs"+ selectedTab + "img"
			jQuery(thisimgid).attr("src","clientincludes/templates/windfall/images/registration3btabs/valid.png");

			jQuery("#registrationtabs").tabs('enable', current) ;
	     	jQuery("#registrationtabs").tabs("option", "selected", current); 
	     	fnActiveTabCssupdate(current)
	     	//jQuery("#registrationtabs").tabs('disable', selectedTab) ;
	  	}else {
	  		thisimgid = "#tabs"+ selectedTab + "img"
			jQuery(thisimgid).attr("src","clientincludes/templates/windfall/images/registration3btabs/not_valid.png");
			
	  		tabcount = jQuery('#registrationtabs').tabs( "length" );
			if (tabcount>1 && (current < tabcount)) {
			for(var tt=current;tt<=(tabcount-1);tt++) {
				jQuery("#registrationtabs").tabs('disable', tt) ;
			}}
	  	}
	});
	

});

//*******************************************
function countElement(a,b)
 {
	var section = document.getElementById(a);
	var x = section.getElementsByTagName("input");
	var y = section.getElementsByTagName("select");
	var counter = 0;

	for (i = 0; i < x.length; i++) {
	    if (x[i].parentNode.parentNode.style.display != "none") { counter = counter + 1; }
	}

	for (i = 0; i < y.length; i++) {
	    if (y[i].parentNode.parentNode.style.display != "none") { counter = counter + 1; }
	}

	if (section != "#tabs3") {
	document.getElementById(b).innerHTML = "(" + (counter-1);
	}else{
	document.getElementById(b).innerHTML = "(" + (counter);
	}
	
 }


/*

var selectedTab = jQuery( "#registrationtabs" ).tabs( "option", "selected" );

*/




