// 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('MM dd, yy', new Date(thisYear,thisMonth,thisDay)));
	});
 */

	if($("#int-slideshow").length)
	{
		$("#int-slideshow").html("");
		
		$("#int-slideshow").append('<div id="rotator"></div>')
		
		$.get("/media/slideshow.xml", function (xml) {
			$(xml).find("image-item").each(function(i) {
			
				output = '';
				output += '<img src="'+$(this).find("image-path").text()+'" alt="'+$(this).find("image-title").text()+'" width="540" height="150" />';
				$("#rotator").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();
			$("#rotator").cycle({
				fx: transType,
				timeout: slideSpd,
				speed: transSpd,
				cleartype: true,
				cleartypeNoBg: true
			});
					
		});
	}
		
});

function isPhone(strValue) { return /^\d{3}-\d{3}-\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.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"; }
	
	if(objForm.city.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - City\n"; }
	
	if(objForm.state.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - State\n"; }
	
	if(objForm.address.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Address\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 xxxxx or xxxxx-xxxx\n"; }
	
	if(objForm.homePhone.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Home Phone\n"; }
	
	if(objForm.workPhone.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Work Phone\n"; }

	if(objForm.homePhone.value.length && !isPhone(objForm.homePhone.value))
	{ strValidationMsg += "    - Home Phone must be in the format xxx-xxx-xxxx\n"; }
	
	if(objForm.workPhone.value.length && !isPhone(objForm.workPhone.value))
	{ strValidationMsg += "    - Work Phone must be in the format xxx-xxx-xxxx\n"; }
	
	if(objForm.bestTime.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Please enter the best time to reach you\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 validateQF() {

	var strReqMsg = "";
	var strValidationMsg = "";
	
	if($("input#qf-name").val().replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Name\n"; }

	if($("input#qf-phone").val().replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Phone\n"; }

	if($("input#qf-phone").val().length && !isPhone($("input#qf-phone").val()))
	{ strValidationMsg += "    - Home Phone must be in the format xxx-xxx-xxxx\n"; }
	
	if($("input#qf-email").val().replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Email\n"; }
	
	if($("input#qf-email").val().length && !isEmail($("input#qf-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 dataString = 'name='+ $("input#qf-name").val() + '&email=' + $("input#qf-email").val() + '&comments=phone number: ' + $("input#qf-phone").val();
		//alert (dataString);return false;
		
		$.ajax({
			type: "POST",
			url: "/content/4/contact-lacina-siding",
			data: dataString,
			success: function() {
				$("#int-contact").html("<div align='center'><br /><br /><br /><br /><strong>Thank you for your request.<br />We will contact you soon.</strong></div>");
			}
		});
		return false;
	}

}

