// JavaScript Document

$(function() {
	
/*	$(".feed_item .feed_preview_date").each(function() {
		var thisDay = parseInt($(this).children(".day").text(), 10);
		var thisMonth = parseInt($(this).children(".month").text(), 10) - 1;
		var thisYear = parseInt($(this).children(".year").text(), 10) + 2000;
		$(this).text($.datepicker.formatDate('M dd, yy', new Date(thisYear,thisMonth,thisDay)));
	});
	
	$(".upcoming-event .upcoming-event-date").each(function() {
		var thisDay = parseInt($(this).children(".ued-day").text(), 10);
		var thisMonth = parseInt($(this).children(".ued-month").text(), 10) - 1;
		var thisYear = parseInt($(this).children(".ued-year").text(), 10);
		$(this).text($.datepicker.formatDate('M dd, yy', new Date(thisYear,thisMonth,thisDay)));
	});*/
	
	if($("#home-left").outerHeight() < $("#home-right").outerHeight())
	{
		$("#home-left").css("height",$("#home-right").outerHeight() - 87);
	}
	else
	{
		$("#home-right").css("height",$("#home-left").outerHeight() + 7);
	}
	
	if( ($("#int-left").outerHeight() + 67) < $("#int-right").outerHeight())
	{
		$("#int-left").css("height",$("#int-right").outerHeight() - 87);
	}
	else
	{
		$("#int-right").css("height",$("#int-left").outerHeight() + 7);
	}
	
	$("#main-menu > ul > li > a").each(function() {
		var aryText = $(this).text().split(" ",2);
		$(this).html($(this).text().replace(aryText[0], "<span>"+aryText[0]+"</span>"));
	});
	
	if($("#home-main").length)
	{
		$("#home-main").html("");
		
		$.get("/media/slideshow.xml", function (xml) {
			$(xml).find("image-item").each(function(i) {
			
				output = '';
				output += '<a href="'+$(this).find("image-link").text()+'"><img src="'+$(this).find("image-path").text()+'" alt="'+$(this).find("image-title").text()+'" width="581" height="304" /></a>';
				$("#home-main").append(output);
			 
			});	
			
			var slideSpd = parseFloat($(xml).find("slideshow-speed").text()) * 1000;
			var transSpd = parseFloat($(xml).find("transition-speed").text()) * 1000;
			var transType = $(xml).find("transition-type").text();
			$("#home-main").cycle({
				fx: transType,
				timeout: slideSpd,
				speed: transSpd,
				cleartype: true,
				cleartypeNoBg: true
			});
					
		});
	}
	
	$("#newsletterModal").click(function(e){
		e.preventDefault();
		$('#newsletter').modal({position: ["30%","35%"]});
	});
	
	$("#frmNewsletterForm").submit(function() {  
    // validate and process form here
		
		var strReqMsg = "";
		var strValidationMsg = "";
		
		if($("#frmNewsletterForm input#name").val().replace(/\s+/g, "") == "")
		{ strReqMsg += "    - Name\n"; }
		
		if($("#frmNewsletterForm input#email").val().replace(/\s+/g, "") == "")
		{ strReqMsg += "    - Email\n"; }
		
		if($("#frmNewsletterForm input#email").val().length && !isEmail($("input#email").val()))
		{ strValidationMsg += "    - Email must be in the format username@domain.com\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 += "The following fields are not filled in correctly:\n\n" + strValidationMsg; }
	
			alert(strDisplay);
			return false;
		}
		else
		{
			var name = $("input#name").val();
			var email = $("input#email").val();
			
			 var dataString = 'name='+ name + '&email=' + email + '&newsletter=yes';  
			 $.ajax({  
				 type: "POST",  
				 url: "themes/jnba/contactProcess.php",  
				 data: dataString,  
				 success: function() {  
					 $('#newsletterForm').html("<div id='message'></div>");  
						$('#message').html("<h2>Newsletter Request Submitted!</h2>")  
					 .append("<p>We will be in touch soon.</p>") 
				 }  
			 });  
			return false;
		}
	});

	//var totalWidth = 0;

		//$("#main-nav > ul > li").each(function() {
		//	totalWidth += $(this).outerWidth();
		//});
		//$("#main-nav > ul").css("width", totalWidth);
		
	$("#pie area").mouseover(function(e) {
		//alert($(this).attr("src").split(".").join($(this).attr("title").replace("#","") + "."));
    $($(this).attr("title")).show();
		$("#pie-image").attr("src", $("#pie-image").attr("src").split(".").join("-" + $(this).attr("title").replace("#","") + "."));
  });
	
	$("#pie area").mouseout(function(e) {
		//alert("bye");
    $($(this).attr("title")).hide();
		$("#pie-image").attr("src", "/media/six-areas-pie.png");
  });

});
	
	
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 isZIP(strValue) { return /(^\d{5}$)|(^\d{5}-\d{4}$)/.test(strValue); } 

function validateContact(objForm)
{

	var strReqMsg = "";
	var strValidationMsg = "";
	
	if(objForm.fname.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - First Name\n"; }
	
	if(objForm.lname.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Last Name\n"; }
	
	if( (objForm.email.value.replace(/\s+/g, "") == "") && (objForm.phone.value.replace(/\s+/g, "") == "") )
	{ strReqMsg += "    - Please enter an email address or phone number\n"; }
	
	if(objForm.email.value.length && !isEmail(objForm.email.value))
	{ strValidationMsg += "    - Email must be in the format username@domain.com\n"; }
	
	if(objForm.phone.value.length && !isPhone(objForm.phone.value))
	{ strValidationMsg += "    - Phone must be in the format 999-999-9999\n"; }
		
	if(objForm.comments.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Comments\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 += "The following fields are not filled in correctly:\n\n" + strValidationMsg; }

		alert(strDisplay);
		return false;
	}
	else
	{
		return true;
	}

}

function validateRSVP(objForm)
{

	var strReqMsg = "";
	var strValidationMsg = "";
	
	if(objForm.fname.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - First Name\n"; }
	
	if(objForm.lname.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Last Name\n"; }
	
	if( $("input[name='attend']:checked").length == 0 ) 
	{ strReqMsg += "    - Are you attending?\n"; }
	
	if( (objForm.email.value.replace(/\s+/g, "") == "") )
	{ strReqMsg += "    - Please enter an email address\n"; }
	
	if(objForm.email.value.length && !isEmail(objForm.email.value))
	{ strValidationMsg += "    - Email must be in the format username@domain.com\n"; }
	
	if(objForm.phone.value.length && !isPhone(objForm.phone.value))
	{ strValidationMsg += "    - Phone must be in the format 999-999-9999\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 += "The following fields are not filled in correctly:\n\n" + strValidationMsg; }

		alert(strDisplay);
		return false;
	}
	else
	{
		return true;
	}

}

function validateSurvey(objForm)
{

	var strReqMsg = "";
	var strValidationMsg = "";
	
	if(objForm.email.value.length && !isEmail(objForm.email.value))
	{ strValidationMsg += "    - Email must be in the format username@domain.com\n"; }
	
	var stuff = "";
	
	$(":checked").each(function(i) {
    stuff += $(this).val();
  })
	
	if(stuff.replace(/\s+/g, "") == ""  && objForm.other.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Please select an option\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 += "The following fields are not filled in correctly:\n\n" + strValidationMsg; }

		alert(strDisplay);
		return false;
	}
	else
	{
		return true;
	}

}


function validateNewsletter(objForm)
{

	var strReqMsg = "";
	var strValidationMsg = "";
	
	if(objForm.name.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Name\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"; }
		
	// 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 += "The following fields are not filled in correctly:\n\n" + strValidationMsg; }

		alert(strDisplay);
		return false;
	}
	else
	{
		return true;
	}

}

function validateWebinar(objForm)
{

	var strReqMsg = "";
	var strValidationMsg = "";
	
	if(objForm.fname.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - First Name\n"; }
	
	if(objForm.lname.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Last Name\n"; }
	
	if( (objForm.email.value.replace(/\s+/g, "") == "") )
	{ strReqMsg += "    - Please enter an email address\n"; }
	
	if(objForm.email.value.length && !isEmail(objForm.email.value))
	{ strValidationMsg += "    - Email must be in the format username@domain.com\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 += "The following fields are not filled in correctly:\n\n" + strValidationMsg; }

		alert(strDisplay);
		return false;
	}
	else
	{
		return true;
	}

}



//XMLHTTPrequest
function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_type = new XMLHttpRequest();
	}
	return request_type;
}

var http = createObject();

function search()
{
	var term = document.getElementById('searchTerm').value;
	window.location = "/search/"+term+"/1";
}

  
