$(document).ready(function() {

// Autopopulate Forms	
	if(typeof $('#username').example == 'function') {
		// function exists, so we can now call it
		$('#username').example(function() {
			return $(this).attr('title');
		});		
	}	
	
// Password Clear Text Input
	var $passInput = $('#password');
	var $unInput = $('#username');
	
	$passInput.after('<input type="text" id="fake-password" class="text-input" value="Password" />');
	var $fakePassInput = $('#fake-password');
	
	
	// on refresh don't remove saved un/pass
	if($passInput.val() != ''){
		$fakePassInput.css('left','-9999px');
	}
	
	// when using enter key to select a saved un showing prepopulated pass
	$unInput.keyup(function(){
		 if($passInput.val() != ''){
			 $fakePassInput.css('left','-9999px');
		 }
	});
	
	// when using mouse (on blur) to select a saved un showing prepopulated pass
	$unInput.change(function(){
		if($passInput.val() != ''){
			$fakePassInput.css('left','-9999px');
		}
	});
	
	// move fake pass input on focus & move focus to real pass input
	$fakePassInput.focus(function(){
		$(this).css('left','-9999px');
		$passInput.focus();
	});
	
	// if tabbing to pass input (above in html) move fake pass input
	$passInput.focus(function(){
		$fakePassInput.css('left','-9999px');
	});
	
	// if pass is left blank show fake pass
	$passInput.blur(function(){
		if($(this).val() == '') {
			$fakePassInput.css('left','0px');
		}
	});

// ZISA columns
	/* clears floats in the 2-column format */
	$('div#catB ul:even').addClass('alt');
	$('div#catC ul:even').addClass('alt');
	$('div#catD ul:even').addClass('alt');
	/* removes top borders for first ul */
	$('div#catAGold ul:first').addClass('first');
	$('div#catASilver ul:first').addClass('first');
	$('div#catABronze ul:first').addClass('first');
	$('div#catB ul:first').addClass('first');
	$('div#catC ul:first').addClass('first');
	$('div#catD ul:first').addClass('first');
	/* removes top borders for second ul */
	$('div#catB ul:nth-child(4)').addClass('first');
	$('div#catC ul:nth-child(4)').addClass('first');
	$('div#catD ul:nth-child(4)').addClass('first');

//table striping
	$('.inner table.stripe').each(function(){
		$('tbody tr:nth-child(2n+1)').addClass('alt');
	});
	
//serCreate table striping
	//$('table#serCrafts tr:odd').addClass('odd');	
	
	// calendar flyout
	if(typeof $(".dateinput").dateinput == 'function') {
		$(".dateinput").dateinput({ selectors: true, min: "2000-01-01", max: "2011-12-31", yearRange: [-12, 0]});
		
		var dateelement = $(".dateinput:first").data("dateinput");
		if (dateelement != undefined) {
			
			// use the same callback for two different events. possible with bind
			$(".dateinput").bind("onShow onHide", function()  {
				$(this).parent().toggleClass("active");
			});
			var valueset = function() {
				$(this).data("dateinput").setValue(2011,00,01);
			};
			// use the same callback for two different events. possible with bind
			$(".dateinput").bind("onShow", valueset);
			
			var baseDate = new Date();
			baseDate.setFullYear(2011,00,00);
			$(".dateinput:last").data("dateinput").setMin(baseDate, true);
			
			// when first date input is changed
			$(".dateinput:first").data("dateinput").change(function() {
				$(".dateinput:first").unbind("onShow", valueset);
				var firstdate = this.getValue();
				if (firstdate>baseDate) {
					var valuesetlast = function() {
						$(this).data("dateinput").setValue(firstdate);
					};
					// we use it's value for the seconds input min option
					$(".dateinput:last").bind("onShow", valuesetlast).data("dateinput").setMin(firstdate, true).change(function() {
						$(".dateinput:last").unbind("onShow", valuesetlast);
					});
				} else {
					$(".dateinput:last").data("dateinput").setMin(baseDate, true);
				};
			});
		};
	}
	
	// Form Validation
	if(typeof $('form.validate').validator == 'function') {
		$('form.validate').attr('novalidate', 'novalidate').validator({position:'bottom left', offset: [0,2]}).submit(function(){
			
			// get handle to the Validator API
			var api = $("form.validate").data("validator");
			
			// radio buttons
			$('.required-options').each(function(){
				var $this = $(this),
					selected = $('input:checked',this).val();
				
				if (selected === undefined){
					$this.append('<div class="error"><p>Please make a selection.</p></div>');
					return false;
				}
			});
			$('.required-options input').click(function() { 
				$(this).parent().siblings('.error').remove(); 
			});
			
			// require page 2 radio buttons to have yes selected
			$("#basic1wrapper, #basic2wrapper, #basic3wrapper, #basic4wrapper").each(function(){
				var $this = $(this),
					selected = $('input:checked',this);
				
				if (selected.val() != 1){
					selected.addClass('invalid');
					$this.append('<div class="error"><p>In order to be eligible, "yes" must be selected.</p></div>');
				}
			});
			
			$('#basic1wrapper input:not(.invalid), #basic2wrapper input:not(.invalid), #basic3wrapper input:not(.invalid), #basic4wrapper input:not(.invalid)').click(function() { 
				$(this).siblings('.error').remove(); 
			});
			
			// required fields
			$('.invalid',this).each(function(){
				$(this).siblings('.helptext').addClass('invalid');
			});
			
			$('.invalid').eq(0).focus().addClass('focus').end()
			.focus(function(){
				$(this).addClass('focus');
			})
			.blur(function(){
				var $this = $(this);
				$this.removeClass('focus');
			});
			
			// use API to assign an event listener
			api.onSuccess(function(e, els) {
				api.reflow();
				els.siblings('.helptext').removeClass('invalid');
			});
			
		});
	}
	
	//Show the information message at the bottom if these fail
	if(typeof $('#casesDeaths, #casesAway, #casesTransfer, #casesOther, #daysTransfer, #daysAway').oninvalid == 'function') {	
		$('#casesDeaths, #casesAway, #casesTransfer, #casesOther, #daysTransfer, #daysAway').oninvalid(function(event, errorMessage) {
			$("#eligibilityError").css("display","block");
		});
	}

});
// ZISA Application Form extensions
//Credit card min character
if (typeof $.tools !== 'undefined') {
	$.tools.validator.fn("[minlength]", function(input, value) {
	    var min = input.attr("minlength");
	    
	    return value.length >= min ? true : {     
	    	en: "Please provide at least " +min+ " character" + (min > 1 ? "s" : "")
	    };
	});
}



