	
// form validation handlers	
jQuery.validator.addMethod("over13", function(value, element) {
	d = new Date();
	y = (!(jQuery.browser.msie))?(d.getYear()+ 1900):d.getYear();
	//alert(y + ' -- '+ value);
	//alert(y - value);
	if ((y - value) < 13) {
		/*if (confirm("According to your birth year, you are under 13. Before finishing registration, make sure your parent approves!")) {
		return true;
		}
		else {*/
			return false;
		//}
	}
	return true;
}, jQuery.format("You must be over 13 and/or have parental consent to proceed.")
);

jQuery.validator.addMethod("dropNull", function(value, element) {
	if (value != "null") {
		return true;
	}
	else {
		return false;
	}
}, jQuery.format("<div class='birtherr'>You must select a birth year.</div>"));

jQuery.validator.addMethod("withRequired", function (value, element) {
	if( $("#overage").is(":checked") ) { 
		//alert(value);
		if(value.length > 0) { return true; }
		else { return false; } 
	}
	else { return true; }
}, "Your parent's email is required.");

jQuery.validator.addMethod("notEmail", function(value, element) { 
	if($("#overage").is(":checked")) {
		if( value != $("#email").val() ) { return true; }
		else { return false; }
	}
	else { return true; }
}, "Your parent's email must be different than your email."); 

jQuery.validator.addMethod("notDefault", function(value, element) {
	switch (element.name) {
		case "fname":
			defaultVal = "Your First Name";
			break;
		case "lname":
			defaultVal = "Your Last Name";
			break;
		case "email":
			defaultVal = "you@youremail.com";
			break;
		case "parent_email":
			if(!($("#overage").is(":checked"))) return true;
			defaultVal = "you@youremail.com";
			break;
		case "conf_p_email":
			if(!($("#overage").is(":checked"))) return true;
			defaultVal = "you@youremail.com";
			break;
		case "county":
			defaultVal = "The County where you live (not country)";
			break;
		case "club_name":
			defaultVal = "Club Name";
			break;
		case "username":
			defaultVal = "your email address";
			break;
	}
	
//		alert(element.name + " -- " + value + " -- " + defaultVal);
	rtn = (value==defaultVal)?false:true;
	
//		alert(rtn);
	return rtn;
}, "This is not a valid entry");		

function loadFormBG() {
	// default form style handlers
	// simple add bg to text boxes
	$('input[type=text]').addClass("jsBG");
	$('input[type=password]').addClass("jsBG");
	$('input[type=text].short').addClass("jsBG");
	
	// need to hide system checkbox and add bg to label
	$('input[type=checkbox]').addClass("jsHide");
	$('input[type=checkbox] + label > span').addClass("jsBG");
	$('label > input[type=checkbox] + span.checkBox').addClass("jsBG");
	// handles checked state on load
	$('input[type=checkbox]:checked').addClass("checked");
	
	//  select menu
/*	$(".select_wrap").custSelectBox(); */ 
// this plugin has bugs - default system selects will display
	
	
	//  handle checkbox events
    $('input[type=checkbox]').bind('focus', function() {
		$(this).addClass("focus");
		if( $(this).is(":checked") ) {
			$(this).addClass("checked");
		}
		else {
			$(this).removeClass("checked");
		}
	}).blur(function() {
		$(this).removeClass("focus");
		if( $(this).is(":checked") ) {
			$(this).addClass("checked");
		}
		else {
			$(this).removeClass("checked");
		}
	}).change(function() {
		if( $(this).is(":checked") ) {
			$(this).addClass("checked");
		}
		else {
			$(this).removeClass("checked");
		}
	}).click(function() {
		if( $(this).is(":checked") ) {
			$(this).addClass("checked");
		}
		else {
			$(this).removeClass("checked");
		}
	});
    
    //  default text handlers for input fields
    $('input[type=text]').bind('focus', function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
		$(this).addClass("focus");
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
		$(this).removeClass("focus");
	});
}
		
/************************************	
*
* start up fucntions when we're ready to roll
*
************************************/
//alert((jQuery.browser.msie) +' -- '+ (parseInt(jQuery.browser.version)) );

$(document).ready(function() {
var oldie = false;
	//IE 6 doesn't like form background images, so they don't get em...
	if (!((jQuery.browser.msie) && (parseInt(jQuery.browser.version) == 6))) {
		loadFormBG();
	} // end !IE 6  


	// fix pngs for IE 6
	if (((jQuery.browser.msie) && (parseInt(jQuery.browser.version) == 6))) {	
		oldie = true;
		$(document).pngFix(); 
		
		
		$("#overage").bind('click', function() {
			$(this).blur();
			if ($(this).is(":checked") ) {
				$("#parent-email-container").show("slow");
			}
			else {
				$("#parent-email-container").hide("slow");
			}
		
		});
	} // IE 6
	
	
	$("#overage").bind('change', function() {
		if ($(this).is(":checked") ) {
			$("#parent-email-container").show("slow");
		}
		else {
			$("#parent-email-container").hide("slow");
		}
	});
	($("#overage").is(":checked"))?$("#parent-email-container").show():$("#parent-email-container").hide();
	
	// form validation rules	
	$("#short-form").validate({
		rules: {
			fname: {
				required: true,
				minlength: 2,
				notDefault: true
			},
			email: {
				required: true,
				email: true,
				notDefault: true
			},
			conf_email: {			
				required: true,
				email: true,
				equalTo: "#email"
			},
			parent_email: {
				notDefault: true,
				email: true,
				withRequired: true,
				notEmail: true
			},
			conf_p_email: {
				email: true,	
				withRequired: true,			
				equalTo: "#parent_email"
			},
			privacy_policy: {
				required: true
			}
		},
		messages: {
			fname: {
				required: "Please enter a first name",
				minlength: "Your first name must consist of at least 2 characters"
			},
			email:  "Please enter a valid email address",
			conf_email: "Your email address don't match",
			parent_email: {
				required: "A parent or guardian email is required",
				notEmail: "You entered the same email for you parent as your email address"
			},
			conf_p_email: "Your email address don't match",
			privacy_policy: {
				required: "<div id='policyerr'>Please accept our privacy policy</div>"
			}
		}
	});
	/*  short form removed messaging ******
	
			lname: {
				required: "Please enter a last name",
				minlength: "Your last name must consist of at least 2 characters"
			},
	
			birth_year:  {
				required: "<div class='birtherr'>You must select a birth year.</div>",				
				over13: "<div class='birtherr'>According to your birth year, you are under 13. Before finishing registration, make sure your parent approves!</div>",
				dropNull:  "<div class='birtherr'>You must select a birth year.</div>"
			},
	*/
	$("#longForm").validate({
		rules: {
			fname: {
				required: true,
				minlength: 2,
				notDefault: true
			},
			email: {
				required: true,
				email: true,
				notDefault: true
			},
			conf_email: {			
				required: true,
				email: true,
				equalTo: "#email"
			},
			parent_email: {
				notDefault: true,
				email: true,
				withRequired: true,
				notEmail: true
			},
			conf_p_email: {
				email: true,	
				withRequired: true,			
				equalTo: "#parent_email"
			},
			state: {
				required: true
			},
			county: {
				required: true,
				notDefault: true
			},
			"position[]": {
				required: true,
				minlength: 1
			},
			privacy_policy: {
				required: true
			}
		},
		messages: {
			fname: {
				required: "Please enter your first name",
				minlength: "Your first name must consist of at least 2 characters"
			},
			email:  "Please enter a valid email address",
			conf_email: "Your email address don't match",
			parent_email: {
				required: "A parent or guardian email is required",
				notEmail: "You entered the same email for you parent as your email address"
			},
			conf_p_email: "Your email address don't match",
			state:  "Please select the state you live in",
			county:  "Please enter a county you live in",
			"position[]":  "Please check at least one position",
			privacy_policy: {
				required: "<div id='policyerr'>You must accept our privacy policy</div>"
			}
		}
	});
	
	/*  long form removed messagin ****
	
			club_name: {
				required: true,
				notDefault: true
			},
			lname: {
				required: true,
				minlength: 2,
				notDefault: true
			},
			birth_year: {
				required: true,
				over13: true,
				dropNull: true
			},
	
			club_name:  "Please enter a 4-H club, after school program, or school name",
			lname: {
				required: "Please enter your last name",
				minlength: "Your last name must consist of at least 2 characters"
			},
			birth_year:  {
				required: "<div class='birtherr'>You must select a birth year.</div>",				
				over13: "<div class='birtherr'>According to your birth year, you are under 13. Before finishing registration, make sure your parent approves!</div>",
				dropNull:  "<div class='birtherr'>You must select a birth year.</div>"
			},
	
	*/
	
	$("#signin-form").validate({
		rules: {
			username: {
				required: true,
				email: true,
				notDefault: true
			}
		},
		messages: {
			username: "<div id='signinerr'>You must enter your email to sign in</div>"
			}
	
	});
	
	$("a.user-sign-in").live("click",function(e) {
		e.preventDefault();
		$(this).hide();
		$("a.user-sign-in-close").show();
		$("#signin-form-container").toggle();
		$("#form-header").height(319);
		// add a swap of bg imgs here
		if(oldie) {
$("#form-header").css("background-image","url('../../misc/images/forms/4h-signin-top-cap-long.gif')");		
			$("#short-form").css("top","5px");	
		$("#form-header p").css("top", "65px");	
		}
		else {
//$("#form-header").css("background-image","url('../../misc/images/forms/4h-signin-top-cap-long.png')");
			$("#form-header").addClass("sign-in-open");
			$("#form-header p").css("top", "95px");	
		}
	});
		
	$("a.user-sign-in-close").live("click",function(e) {
		e.preventDefault();
		$(this).hide();
		$("a.user-sign-in").show();
		$("#signin-form-container").toggle();
		$("#form-header").height(175);
		// add a swap of bg imgs here
		if(oldie) {
$("#form-header").css("background-image","url('../../misc/images/forms/4h-signin-top-cap.gif')");		
			$("#short-form").css("top","0px");
		$("#form-header p").css("top", "50px");	
		}
		else {
//$("#form-header").css("background-image","url('../../misc/images/forms/4h-signin-top-cap.png')");
			
			$("#form-header").removeClass("sign-in-open");
			$("#form-header p").css("top", "50px");	
		}
	});
	
	$("#signin-form").submit(function(e) {
		e.preventDefault();
		var username = $("input#username").val();  
		if(!( (username == "") || (username == "your email address"))) {
			var dataString = 'username='+username;
			//alert(dataString);
			$.ajax({  
				type: "POST",  
				url: "misc/inc/signin-process.php",  
				data: dataString,  
				success: function(res) {  
					var obj = $.evalJSON(res); 
					if(!($(obj).attr("errormsg")) ) {
						//alert('session created');
						//alert('Welcome back '+obj.firstName+'!');
						// replace the sign in block and form with appropriate block (files)
						if(obj.formComplete) {
							$("#form-container").load('misc/inc/loggedin_complete.php');
						}
						
						else {							
							$("#form-container").load('misc/inc/loggedin_notcomplete.php');
						
						}
					} //if			
					else { 
						alert(obj.errormsg +". Please try again."); 
						$("input#username").focus();
					}
				}  //success	
			});  //ajax
			return false;
		} //if
	}); //submit
	
	$("a.user-sign-out").live("click", function(e) {
		e.preventDefault();
		var dataString = 'logout=true';
		//alert(dataString);
		$.ajax({  
			type: "POST",  
			url: "misc/inc/session.php",  
			data: dataString,  
			success: function(res) {  
				var obj = $.evalJSON(res); 
				if($(obj).attr("msg") ) {
				//	alert('session deleted');
					alert($(obj).attr("msg"));
				//	alert('See you later!');	
					location.reload(); 
					//$("#form-container").load('misc/inc/signin_shortform.inc');
				} //if
			} // success
		}); //ajax
		loadFormBG();
		return false;
	}); // sign out
	
	
	//replace county input with drop-down
	if($("#county")) {
		$("#county").replaceWith('<select name="county" id="county" class="full-width"><option value="">- Please Select a State First -</option></select>');			
	}
	//county list
	$("#state").change(function(e) {
		e.preventDefault();
		$("#county").empty();
		$("#county").append('<option value="">- Select a County -</option>');
		getCountyList($('#state').val());
		return false;

	}); // county list
	
	function getCountyList(state) {
		var dataString = 'state='+state;
		//alert(dataString);
		$.ajax({  
			type: "POST",  
			url: "misc/inc/get_county.php",  
			data: dataString,  
			success: function(res) {  
				var $obj = $.evalJSON(res);
				$.each($obj, function(i,item) {
					$("#county").append(
					'<option value="'+item+'">'+item+'</option>'				
					) //append
					// <?php if($state=="'+$obj+'") echo "selected";?>
				}) //.each
				
			} // success
		}); //ajax	
	} //getCountyList
	
	
	// safari & opera alignment fixes
	if((jQuery.browser.safari) || (jQuery.browser.opera)) {
		$("a.user-sign-in").css("top","-8px");
		$("a.user-sign-in-close").css("top","-6px");
		$("select#birth_year").css("top","-30px");
		$("#footer-nav .footer-nav-container").css("margin-right","10px");
		if((jQuery.browser.opera)) {
		$("select#birth_year").css("top","-25px");
			$("button#sign-up").css("top","-30px");
		}
	}
	//fix some oddities with FF 2
	if((jQuery.browser.mozilla) && (jQuery.browser.version.substr(0,3)=="1.8") ) {
		$("#footer-nav .footer-nav-container").css("margin-right","10px");
		$("#global-nav ul.menu li").css("width","130px");
		$("#global-nav ul.menu li.first").css("width","110px");
		$("#global-nav ul.menu li.resources").css("width","150px");
		$("#global-nav ul.menu li.last").css("width","160px");
		$("#global-nav ul.menu li ul.sub-menu li").css("width","260px");
		$("#global-nav ul.menu li.last ul.sub-menu li").css("width","300px");
		$(".bottom-fields #birth_year").css("left","245px");
		
	}
	
	if($('a.feed').gFeed) {
		//alert('feed ok');
		$('a.feed').gFeed({
			url: 'http://news.google.com/news?pz=1&ned=us&hl=en&q=biofuels&output=rss',
			title: 'Biofuel News from Google',
			max: '5',
			target: '#bio-gfeed'
		});	
	} // if feed
}); // on ready
/*
document.onclick = jsddm_close;
*/
