// JavaScript Document

$(function() {
	//If main nav element has subnav elements, add the right arrow background image
	$("#nav > ul > li > ul").parent().addClass("dropdown");
	
	//On hover of main nav element, change background to down arrow background image
	$("#nav > ul > li > ul").parent().hover(function() {
		$(this).removeClass("dropdown");
		$(this).addClass("dropdown-hover");
		},
		function() {
		$(this).removeClass("dropdown-hover");
		$(this).addClass("dropdown");
	});
	
	//Slideshow
	if ($("#slideshow").length>0) {
		$("#slideshow").cycle({
			fx: "scrollLeft",
			timeout: 5000,
			slideExpr: "img",
			pager: "#slideshow-pager",
			pagerAnchorBuilder: function(idx, slide) { 
				return '<li><a href="#"></a></li>'; 
			},
			activePagerClass: "activeSlide"
		});
	}
});

//Contact form validation
function isZip(strValue) { return /(^\d{5}$)|(^\d{5}-\d{4}$)/.test(strValue); } 
function isPhone(strValue) { return /^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?|\.?))[2-9]\d{2}[- \.]?\d{4}$/.test(strValue); }
function isEmail(strValue) { return /^[-!#\$%\*\+\/\?\|\^&{}`~\w]+(\.[-!#\$%\*\+\/\?\|\^&{}`~\w]+)*@[-\w]+(\.[-\w]+)+$/.test(strValue); } 

function validateContact(objForm)
{
	var strReqMsg = "";
	var strValidationMsg = "";

	if(objForm.name.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Name\n"; }
	
	if(objForm.street.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Street\n"; }
	
	if(objForm.city.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - City\n"; }
	
	if(objForm.state.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - State\n"; }
	
	if(objForm.zip.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Zip\n"; }
	
	if(objForm.zip.value.length && !isZip(objForm.zip.value))
	{ strValidationMsg += "    - Zip must be in the format 12345 or 12345-6789\n"; }
	
	if(objForm.phone.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Phone\n"; }
	
	if(objForm.phone.value.length && !isPhone(objForm.phone.value))
	{ strValidationMsg += "    - A valid, properly formatted phone number must be entered\n"; }
	
	if(objForm.email.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Email\n"; }
	
	if(objForm.email.value.length && !isEmail(objForm.email.value))
	{ strValidationMsg += "    - Email must be in the format username@domain.com\n"; }
	
	if(objForm.interestEmployment.checked==0 && objForm.interestContract.checked==0)
	{ strReqMsg += "    - Employment or Contract Opportunities must be selected\n"; }
	
	if(objForm.interestEmployment.checked==1) {
		exposureChecked=false;
		for(i=0; i<objForm.exposure.length; i++) {
			if(objForm.exposure[i].checked)
			exposureChecked=true;
		}
		if(!exposureChecked) {
			strReqMsg += "    - You must select Yes or No for exposure to hogs\n";
		}
		else if(objForm.exposureYes.checked && objForm.exposureExplain.value.replace(/\s+/g, "") == "") {
			strReqMsg += "    - Please explain your exposure to hogs or livestock\n";
		}
	}
	
	if(objForm.interestEmployment.checked==1) {
		experienceChecked=false;
		for(i=0; i<objForm.experience.length; i++) {
			if(objForm.experience[i].checked)
			experienceChecked=true;
		}
		if(!experienceChecked) {
			strReqMsg += "    - You must select Yes or No for experience with hogs\n";
		}
		else if(objForm.experienceYes.checked && objForm.experienceExplain.value.replace(/\s+/g, "") == "") {
			strReqMsg += "    - Please explain your experience with hogs\n";
		}
	}
	
	if(objForm.interestContract.checked==1) {
		contractChecked=false;
		for(i=0; i<objForm.contract.length; i++) {
			if(objForm.contract[i].checked)
			contractChecked=true;
		}
		if(!contractChecked) {
			strReqMsg += "    - You must select Yes or No for currently contracting hogs\n";
		}
		else if(objForm.contractYes.checked && objForm.contractExplain.value.replace(/\s+/g, "") == "") {
			strReqMsg += "    - Please describe your contract hog set up\n";
		}
	}
	
	// Assemble all of the error messates together to display to the user
	if(strReqMsg.length || strValidationMsg.length)
	{
		var strDisplay = "";
		if(strReqMsg.length)
		{ strDisplay += "The following fields are required to be completed:\n\n" + strReqMsg; }

		if(strValidationMsg.length)
		{ strDisplay += "\nThe following fields are not filled in correctly:\n\n" + strValidationMsg; }

		alert(strDisplay);
		return false;
	}
	else
	{
		return true;
	}
}
