$(document).ready(function() {
	initSearchInput();
	initProdSuppHover();
	initProductCodeHelpLink();

	if ($('#contactForm').length != 0) {
		$('<div id="contactMsg">&nbsp;</div>').insertBefore('#contactSubmitButton');
		$('#contactMsg').hide();
		$('#contactResetButton').click(function() {
			$('#contactMsg').slideUp();
			$(this).blur();
		});
		$('#contactSubmitButton').click(function() {
			if (checkContactForm()) {
				sendContactForm();
			}
			$(this).blur();
			return false;
		});
	}
});

function initSearchInput() {
	$('#search').focus(function() {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
			$(this).removeClass('searchTextDefault');
		}
	});
	$('#search').blur(function() {
		if ($(this).val().length < 1 || $(this).val() == $(this).attr('title')) {
			$(this).addClass('searchTextDefault');
			$(this).val($(this).attr('title'));
		}
	});
	$('#searchBox').submit(function() {
		$('#search').each(function() {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		});
	});

$('#search').blur();
}

function initProdSuppHover() {
	$('#productSupplementationText a').each(function() {
		$(this).mouseover(function() {
			$('#productSupplementationHelp').fadeIn();
			$('#productSupplementationHelp').dropShadow();
		})
		$(this).mouseout(function() {
			$('#productSupplementationHelp').removeShadow();
			$('#productSupplementationHelp').hide();
		})
 });
}

function initProductCodeHelpLink() {
	$('.productCodeHelp a').click(function(event) {
		event.preventDefault();
		$('#productCodeHelpText').fadeIn('slow');
		$('#productCodeHelpText').dropShadow();
	});
	$('#productCodeHelpText .pcHelpClose a').click(function(event) {
		event.preventDefault();
		$('#productCodeHelpText').removeShadow();
		$('#productCodeHelpText').hide();
	});
}

function checkContactForm() {
	$('#contactMsg').hide();
	var that = document.contactForm;
	var errmsg = '';
	if (that.name.value.length < 1) {
		errmsg += '&middot; Your Name cannot be blank<br />\n';
	}
	if (that.email.value.length < 1) {
		errmsg += '&middot; E-mail cannot be blank<br />\n';
	} else {
		if (!validateEmail(that.email.value)) {
			errmsg += '&middot; E-mail address is invalid<br />\n';
		}
	}
	// check if publication checkboxes exist and make sure at least one is checked
	if (document.getElementById('pub_01')) {
		var chk = false;
		var pObj = that.elements['publication[]'];
		for (i=0; i<pObj.length; i++) {
			if (pObj[i].checked == true && chk == false) {
				chk = true;
			}
		}
		if (chk == false) {
			errmsg += '&middot; Check at least one publication<br />\n';
		}
	}
	if (that.comments) {
		if (that.comments.value.length < 1) {
			errmsg += '&middot; Comments cannot be blank<br />\n';
		}
	}
	if (errmsg.length > 1) {
		errmsg = '<b>Please fix the following errors:</b><br />\n'+errmsg;
		showContactFormError(errmsg);
		return false;
	} else {
		return true
	}
}

function showContactFormError(errmsg) {
	$('#contactMsg').hide();
	$('#contactMsg').removeClass('contactTextMsg').addClass('contactErrorMsg');
	$('#contactMsg').html(errmsg);
	$('#contactMsg').slideDown();
}

function showContactFormMsg(txt) {
	$('#contactMsg').hide();
	$('#contactMsg').removeClass('contactErrorMsg').addClass('contactTextMsg');
	$('#contactMsg').html(txt);
	$('#contactMsg').slideDown();
	$('#contactSubmitButton').attr('disabled',false);
	$('#contactResetButton').attr('disabled',false);
}

function showAjaxLoader() {
	$('#contactSubmitButton').attr('disabled',true);
	$('#contactResetButton').attr('disabled',true);
	$('#contactMsg').hide();
	$('#contactMsg').removeClass('contactErrorMsg contactTextMsg');
	$('#contactMsg').html('<div align="center"><img src="images/ajax-loader.gif" width="16" height="11" /></div>');
	$('#contactMsg').slideDown();
}

function validateEmail(e) {  
	var pattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	return pattern.test(e); 
}

function sendContactForm() {
	var dataString = $('#contactForm').serialize();
	var sendURL = $('#contactForm').attr('action');
	dataString += '&ajax=1';
	$.ajax({
		type: "POST",
		url: sendURL,
		data: dataString,
		beforeSend: function() {
			showAjaxLoader();
		},
		success: function(msg){
			showContactFormMsg(msg);
		}
	});
	document.contactForm.reset();
}

function checkAll(that, n) {
	var chk = that.checked;
	if (obj = document.getElementsByName(n)) {
		for (i=0; i<obj.length; i++) {
			obj[i].checked = chk;
		}
	}
}
