jQuery.noConflict();

clearErrorMsg();

jQuery(".searchStatusImg").hide();
jQuery('.captchaImage').click(function () { 
	reloadCaptchaImage();
});

jQuery(".repFindButton").click(function() {
	jQuery(".searchStatusImg").show();
	clearErrorMsg();
		//
		// Since this is being done via AJAX the form validation has to be done
		// via JavaScript before the request is sent.
		//
	var repState = jQuery(".repSearchStateSelect").val();
	var repFirstName = jQuery(".repSearchFirstName").val();
	var repLastName = jQuery(".repSearchLastName").val();
	var captchaInput = jQuery(".humanValidationInput").val();
	var repSearchValid = true;
	if(repState == null || repState == '') {
		jQuery(".repStateMsg").show();
		repSearchValid = false;
	}
	if(repFirstName == null || repFirstName.length < 3) {
		jQuery(".repFirstNameMsg").show();
		repSearchValid = false;
	}
	if(repLastName == null || repLastName.length < 3) {
		jQuery(".repLastNameMsg").show();
		repSearchValid = false;
	}
	if(captchaInput == null || captchaInput.length < 5) {
		jQuery(".captchaMsg").show();
		repSearchValid = false;
	}
	if(repSearchValid) {
			//
			// Send the AJAX request.
			//
		jQuery.post(
			'/public/requestInfo/matchedReps.jsf',
			{	repSearchStateSelect: repState,
				repSearchFirstName: repFirstName,
				repSearchLastName: repLastName,
				humanValidationInput: captchaInput
			},
			function(data) {
				var currentTable = jQuery(".searchRepResults");
				var resp = jQuery(data);
				currentTable.html(resp);
				jQuery(".matchedRepsTable").remove();
				jQuery(".searchStatusImg").fadeOut();
				reloadCaptchaImage();
				jQuery(".humanValidationInput").val('');
				jQuery(".searchRepResultsInstruct").hide();
			}
		);
	} else {
		jQuery(".searchStatusImg").fadeOut();
		jQuery(".searchRepResultsInstruct").show();
	}
	return false; 
}); 

function reloadCaptchaImage() {
	var image = jQuery('.captchaImage');
	image.hide();
	image.attr('src', image.attr('src')+'?' + Math.floor(Math.random()*100));
	image.fadeIn(); 
}

function clearErrorMsg() {
	jQuery(".captchaMsg").hide();
	jQuery(".repLastNameMsg").hide();
	jQuery(".repFirstNameMsg").hide();
	jQuery(".repStateMsg").hide();
}

function setRepName(name, repNum) {
	if(name != null) {
		document.getElementById('contactForm:sdrName').value = name;
		document.getElementById('contactForm:sdrRepNum').value = repNum;
	}
}