/*===============================================================================================================
	Owner:	Hill's Pet Nutrition
	Title:		FIB > 3BagChallenge Form(s) Javascript Document
	Author:	Steve Kiernan
	Date: 		12/29/09
	Disc:		This document is responsible for the client-side functionality used during the 3BagChallenge submission
					process...	(i.e.  Form Validation, AJAX, jQuery) 
	Note(s):	The following jQuery Libraries/Plug-ins are REQUIRED...
						- jquery-1.3.2.js (or jquery-1.3.2.min.js)
						- jquery.form.js (version: 2.36)
						- jquery.validate.js (version: 1.5.5)
					The javascript calls need to be loaded into the HTML in the following order within the <head></head> tags.
						<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
						<script type="text/javascript" src="jquery.form.js"></script>
						<script type="text/javascript" src="jquery.validate.js"></script>
						<script type="text/javascript" src="FIB_3BagForms.js"></script>
===============================================================================================================*/
$(document).ready(function() {
	var targObj = $('#targetForm').val();
	var targForm;
	switch(targObj) {
		case 'signInForm': targForm='signin.do'; break;
		case 'forgotPassForm': targForm='showForgotPassword.do'; break;
		case 'createPassForm': targForm='showCreatePassword.do'; break;
	}
	fetchForm(targObj, targForm);
});

function fetchForm(targObj, targForm) {
	//Load the form (HTML form generated by java)
	$("#threebagForm_container").load(targForm, function(){
		//Initialize client-side validation
		switch(targObj) {
			case 'signInForm': SignInForm(); break;
			case 'forgotPassForm': ForgotPassForm(); break;
			case 'createPassForm': CreatePassForm(); break;
		}
		classimizeInputs();
	});
}

function SignInForm() {
	var options = { 
		target: '#threebagForm_container',
		success: function() {
			$("#errorDisplay").css("display", "block");
			classimizeInputs();
			SignInForm();
		}
	}; 
	$("#fm1").submit(function() {
		$(this).ajaxSubmit(options);
		return false;
	});
}

function ForgotPassForm() {
	var options = { 
		target: '#threebagForm_container',
		success: function() {
			$("#errorDisplay").css("display", "block");
			classimizeInputs();
			ForgotPassForm();
		}
	}; 
	$("#sendNewPasswordFormBean").submit(function() {
		$(this).ajaxSubmit(options);
		return false;
	});
}

function CreatePassForm() {
	var options = { 
		target: '#threebagForm_container',
		success: function() {
			$("#errorDisplay").css("display", "block");
			classimizeInputs();
			CreatePassForm();
		}
	}; 
	$("#sendNewPasswordFormBean").submit(function() {
												  
		
		
		var s_zxw=s_gi(s_account);
    	s_zxw.linkTrackVars="events"; 
   		s_zxw.linkTrackEvents="event34";   
    	s_zxw.events=s_zxw.linkTrackEvents;        
    	s_zxw.tl(this,'o','Create New Password - FIB');
		
		
		$(this).ajaxSubmit(options);
		return false;
	});
}

//==== START: ADD CLASSNAMES TO INPUT FIELDS =====================================================//
//Add a classname to specific input fields that will allow us to target them via CSS (i.e. buttons, checkboxes, etc..)
//This is primarily necessary because IE6 doesn't recognize attribute selectors in CSS2
function classimizeInputs() {
	var inputs = document.getElementsByTagName('input');
	var inputsLen = inputs.length;
	var i = 0;
	for ( i=0;i<inputsLen;i++ ) {
		if (inputs[i].getAttribute('type') == "radio") {
			inputs[i].className += ' radio';
		}
		if (inputs[i].getAttribute('type') == "checkbox") {
			inputs[i].className += ' checkbox';
		}
		if(inputs[i].type == "image" || inputs[i].type == "button" || inputs[i].type == "submit" || inputs[i].type == "reset") {
			inputs[i].className = "button";
		}
	}
	$('input.button').attr('onsubmit', "pageTracker._linkByPost('http://hills.promo.eprize.com');return false;");
	//$('input.button').click(function(){
		//alert("I've been clicked!");
		//pageTracker._linkByPost('http://hills.promo.eprize.com');return false;								 
	//});
}
//==== END: ADD CLASSNAMES TO INPUT FIELDS =====================================================//

//==== START: INITIALIZE FORM VALIDATION RULES  =====================================================//
function validateForm() {
	var container = $("div.errorContainer");
	var options = { 
		target: '#threebagForm_container',
		success: function(){
			validateForm();
		}
	}; 
	var validator = $("#tellAFriend").validate({
		debug: true,
		errorContainer: container,
		errorLabelContainer: $("ol", container),
		wrapper: "li",
		meta: "validate",
		
		rules: {
			friendsEmail: "required",
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			friendsEmail: "Please enter your <a href='javascript:void(0);' onclick='document.fib_tellAFriendPopup.friendsEmail.focus(); return false;'>Friend(s) Email</a>",
			email: {
				required: "Please enter your <a href='javascript:void(0);' onclick='document.fib_tellAFriendPopup.email.focus(); return false;'>Email Address</a>",
				email: "Your <a href='javascript:void(0);' onclick='document.fib_tellAFriendPopup.email.focus(); return false;'>Email Address</a> must be in the format of name@domain.com"
			}
		},
		showErrors: function() {
			$("#errorCount").text(validator.numberOfInvalids() + " error(s) found");
			this.defaultShowErrors();
		},
		invalidHandler: function() {
			$("#errorCount").text(validator.numberOfInvalids() + " error(s) found");
		},
		submitHandler: function(form) {
			$("#errorCount").text(validator.numberOfInvalids() + " error(s) found");
			$("#tellAFriend").ajaxSubmit(options);
		}
	});
}
//==== END: INITIALIZE FORM VALIDATION RULES  =====================================================//
