jQuery(document).ready(function($){
	// button mouse overs 
	jQuery.preLoadImages(hhiVars.imgDir + "btn_donate_roll.png", hhiVars.imgDir + "btn_sign_roll.png");  
    $("a#donateBtn").hover(
		function(){ 
			$(this).children("img").attr("src", hhiVars.imgDir + "btn_donate_roll.png");
		},
		function(){
			$(this).children("img").attr("src", hhiVars.imgDir + "btn_donate_off.png");
		}	
	); 
	
	$("#signUpBtn").hover(
		function(){ 
			$(this).attr("src", hhiVars.imgDir + "btn_sign_roll.png");
		},
		function(){
			$(this).attr("src", hhiVars.imgDir + "btn_sign_off.png");
		}	
	);
	
	// signup form 
	// store intial values
	hhiVars.signupFieldLabels = {};
	$("#signupForm input[type='text']").each(function(){ 
		var key = $(this).attr('id');
		var val = $(this).val();
		hhiVars.signupFieldLabels[key] = val;
	});
	
	// select text with focus, so typing will overwrite the default value/label 
	$("#signupForm input[type='text']").bind("focus",
		function(){   
			$(this).removeClass("error");
			// clear out default value
			if( $(this).val() == hhiVars.signupFieldLabels[$(this).attr("id")] )
			{
				$(this).val("");
			}   
	}); 
	
	// replace default label in blank fields on blur
	$("#signupForm input[type='text']").bind("blur",
		function(){   
			if( $(this).val() == "")
			{
				$(this).val(hhiVars.signupFieldLabels[$(this).attr("id")]);
			}   
	});
	
	
	// darken the color when input is entered
	$("#signupForm input[type='text']").bind("keypress",
		function(){
			$(this).addClass("dark");
	});	
	
	// when the email field is focused on, the 2nd part of the form displays
	$("#email").bind("focus", function(){
		$("#formStep2").show();
	});
	
	// now that events are bound, hide the 2nd part
	// live() wasn't working in IE so doing this
	$("#formStep2").hide();
}); // end doc ready        


// form processing 
// super light weight because the existing site did none at all
function hhiProcessSignupForm(formRef){ 
	$ = jQuery;
	var errors = 0;
	//check email lightly
	if ( !hhiValidateEmail($("#email").val()) ){
		$("#email").addClass("error");
		errors++;
	}	
		
	// process name if available 
	// not considering it an error if not available
	var fullName = $("#full_name").val();
	if ((fullName != hhiVars.signupFieldLabels.full_name) && (fullName.length > 0)){ 
		// split into 1st & last if there's a space in the name
		if (fullName.indexOf(" ") >=1){
			var nameAr = fullName.split(" ");  
			// last name is the last word in the name
			$("#last_name").val(nameAr.pop());  
			// first name is everything else
			$("#first_name").val(nameAr.join(" "));  
		} else {
			// no spaces, make it the first name (arbitrary decision)
			$("#first_name").val(fullName);
		}		
	}	
	
	if (errors){
		return false;
	} else {
		return true;
	}	
}	  

// takes a string (email addy) returns true if it passes the email test
// very light weight validation to avoid false negatives on international addresses
function hhiValidateEmail(emailStr){   
	var errors = 0;
	if (emailStr.indexOf("@") <= 0)
		errors++;
	if (emailStr.indexOf(".") <= 0)
		errors++;
	if (emailStr == hhiVars.signupFieldLabels.email)
		errors++;
	// return true for an acceptable email, false for no go	
	var ret = (errors > 0) ? false : true;
	return ret;	 	
}	
