/* Parkwood Master Builder JavaScript */

window.addEvent('domready', function() {
		
	// Smooth Scroll page links
	var mySmoothScroll = new SmoothScroll();
	
	
	// Open External Links in a new window
	externalLinks = function() {
		var allAnchors = $(document.body).getElements('a');
		for (var i=0; i<allAnchors.length; i++) {
			var myCurrentAnchor = allAnchors[i];
			if (myCurrentAnchor.get('href') && myCurrentAnchor.get('rel') == "externalLink") {
				myCurrentAnchor.target = "_blank";
			}
		}
	}
	externalLinks();


	
	
	
	// Client Login Panel
	
	if($('clientLoginPanel')){
		
		var clientLoginPanel = new Fx.Slide('clientLoginPanel',{duration: 600}).hide();
	
		// Toggle the slider
		$('clientSupportButton').addEvent('click', function() {
			clientLoginPanel.toggle();
		});
		
		// Close Button
		$('loginPanelClose').addEvent('click', function() {
			clientLoginPanel.slideOut();
		});
		
	} // End if clientLoginPanel
	
	
	// Home Page Large Client Support Button function
	if($('clientSupportBtn')){
		
		var myBody = $(document.body);
		var myBodyScroll = new Fx.Scroll(myBody);
		
		// Toggle the slider
		$('clientSupportBtn').addEvent('click', function() {
			clientLoginPanel.slideIn();
			myBodyScroll.toTop();
		});
				
	} // End if clientSupportBtn
	
	
	// Building Process page Client Support link function
	if($('csLink')){
		
		var myBody = $(document.body);
		var myBodyScroll = new Fx.Scroll(myBody);
		
		// Toggle the slider
		$('csLink').addEvent('click', function() {
			clientLoginPanel.slideIn();
			myBodyScroll.toTop();
		});
				
	} // End if csLink
	

	// Check contact form
	
	if($('contactForm')) {
		
		checkForm = function() {
			
			if ($('name').value ==""){
				alert("Please provide your name.");
				$('name').focus();
				return false;
			}
		
			if ($('email').value ==""){
				alert("Please enter your email address.");
				$('email').focus();
				return false;
			}
		
		
			if ($('email').value !=""){
				var x = $('email').value;
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					if (!filter.test(x)) {
						alert("Oops, looks like there's a problem with your email address.");
						$('email').focus();
						return false;
					}
			}
		
			if ($('phone').value ==""){
				alert("Please provide your phone number.");
				$('phone').focus();
				return false;
			}
			
			var phoneValue = $('phone').getValue();
			var phoneFilter  = /^\(\d{3}\) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}/;
				if (!phoneFilter.test(phoneValue)) {
					alert("Please enter your full 10 digit phone number. Format: 123-456-7890");
					$('phone').focus();
					return false;
				}
			
			if ($('message').value ==""){
				alert("Please provide a brief message.");
				$('message').focus();
				return false;
			}
			
			if ($('wsp_code').value ==""){
				alert("Please enter the 5 letter code. This protects against spammers using this form.");
				$('wsp_code').focus();
				return false;
			}
			
			if ($('wsp_code').value.length !==5){
				alert("The spam protection code must be 5 lower-case letters.");
				$('wsp_code').focus();
				return false;
			}
			
			return true;
		}
		
	} // end if contactForm
	
	
	// Subscribe Form for Mailing List
	
	
	if($('contactP2')) {
	
		//If All communities is selected then deselect everything else
		$('cm177747').addEvent('click', function()
		{
   			if($('cm177747').checked == true)
   			{
   				$('cm177736').checked = false;
   				$('cm177737').checked = false;
   				$('cm177738').checked = false;
   				$('cm177739').checked = false;
   				$('cm177740').checked = false;
   				$('cm177741').checked = false;
   				$('cm177742').checked = false;
   				$('cm177743').checked = false;
   				$('cm177744').checked = false;
   				$('cm177745').checked = false;
   				$('cm177746').checked = false;
   			}
   			
		});

	
	
		checkSubscribeFields = function() {
			if ($('CustomFields_4_12').value ==""){
				alert("Please provide your name.");
				$('CustomFields_4_12').focus();
				return false;
			}
			if ($('email').value ==""){
				alert("Please enter your email address.");
				$('email').focus();
				return false;
			}
			if ($('email').value !=""){
				var x = $('email').value;
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (!filter.test(x)) {
					alert("Oops, looks like there's a problem with your email address.");
					$('email').focus();
					return false;
				}
			}
			if ($('cm177736').checked == false && 
				$('cm177737').checked == false &&
				$('cm177738').checked == false &&
				$('cm177739').checked == false &&
				$('cm177740').checked == false &&
				$('cm177741').checked == false &&
				$('cm177742').checked == false &&
				$('cm177743').checked == false &&
				$('cm177744').checked == false &&
				$('cm177745').checked == false &&
				$('cm177746').checked == false &&
				$('cm177747').checked == false
				){
				alert("Please select at least one development that you are interested in.");
				return false;
			}
			return (true);
		}
		
	} // End if contactP2
	
	
	

	
	// Filter home styles -------------------------------------------
	
	// If the Home Styles search bar is present
	if ($('hsSearchBar')) {
		
		// Set some variables and get a few arrays for filtering the home styles
		var elCount = $$('.homeStyleContainer');
		var homeStyleCount = $$('.homeStyle').get('text');
		var sqFtCount = $$('.sqFt').get('text');
		
		// Function to check the home style select and fade appropriately
		checkHomeStyle = function() {
			for(i=0; i<elCount.length; i++){
				var thisHomeStyle = homeStyleCount[i];
				
				if($('selectHomeStyle').value == "All") {
					elCount[i].fade(1.0);
				}
				
				if($('selectHomeStyle').value !== "All") {
					elCount[i].fade(1.0);
					if (thisHomeStyle !== $('selectHomeStyle').value ) {
						elCount[i].fade(0.35);
					}
				}
			}
		}
		
		// Function to check the home size select and fade appropriately
		checkHomeSize = function() {
			for(i=0; i<elCount.length; i++){
				var thisHomeSize = sqFtCount[i];
				
				if($('selectHomeSize').value == "lessThan1500") {
					if (thisHomeSize > "1500" ) {
						elCount[i].fade(0.35);
					}
				}
				
				if($('selectHomeSize').value == "1500-1800") {
					if (thisHomeSize <= "1499" || thisHomeSize >= "1801" ) {
						elCount[i].fade(0.35);
					}
				}
				
				if($('selectHomeSize').value == "1801-2400") {
					if (thisHomeSize <= "1800" || thisHomeSize >= "2401" ) {
						elCount[i].fade(0.35);
					}
				}
				
				if($('selectHomeSize').value == "greaterThan2400") {
					if (thisHomeSize <= "2400" ) {
						elCount[i].fade(0.35);
					}
				}
			}
		}	
		
		// When ever the page is loaded (or reloaded... eg. user hits the back button) run the functions
		checkHomeStyle();
		checkHomeSize();
	
		// When ever the home style select changes run the functions
		$('selectHomeStyle').addEvent('change', function() {
			checkHomeStyle();
			checkHomeSize();		
		});
		
		// When ever the home size select changes run the functions
		$('selectHomeSize').addEvent('change', function() {
			checkHomeStyle();
			checkHomeSize();		
		});
		
		
		// Submit the search and return the correct results page ---
		$('searchSubmit').addEvent('click', function() {
			if ($('selectHomeStyle').value == "All" && $('selectHomeSize').value =="All") {
				alert("Please choose a criteria by which you would like to filter your results by.");
				return false;
			}
			if ($('selectHomeStyle').value !== "All" || $('selectHomeSize').value !=="All") {
				var homeStyleValue = $('selectHomeStyle').value;
				var homeSizeValue = $('selectHomeSize').value;
				var srPage = "/index.php/searchResults/" + homeStyleValue + homeSizeValue;
				location.href = srPage;
				return false;
			}
			
		});
	
		// End filter home styles functionality --------------------------------
	
	} // End if hsSearchBar
	
	
	
	// Search Results Page Error : Display is no results
	if ($('srl')) {
		var titleText = $$('.stTitle').get('text');
		if (titleText == "") {
			$('srlError').set('html', '<p style="margin-left:20px;">We&rsquo;re sorry, no homes match your search criteria.<br /><br /></p>');
		}
	} // End if srl
	
	
	
	// Quick Possessions Communities : Display if no results
	/*
	if ($('qpl')) {
		var qpTitleText = $$('.stTitle').get('text');
		if (qpTitleText == "") {
			$('qplError').set('html', '<p style="margin-left:20px;">There are currently no quick possessions available in this community.<br /><br /></p>');
		}
	} // End if qpl
	*/
	
	
	
	// Home Style Features SHOW and HIDE buttons
	if ($('hsFeatures')) {
		
		$('featuresView').fade(1.0);
		$('featuresHide').fade(0.0);
		var hsFeaturesPanel = new Fx.Slide('hsFeaturesPanel',{transition: Fx.Transitions.Expo.easeInOut, duration: 700}).hide();
		
		$('featuresView').addEvent('click', function(){
			$('featuresView').fade(0.0);
			$('featuresHide').fade(1.0);
			hsFeaturesPanel.slideIn();
		});
		
		$('featuresHide').addEvent('click', function(){
			$('featuresView').fade(1.0);
			$('featuresHide').fade(0.0);
			hsFeaturesPanel.slideOut();
		});
		
	} // End if hsFeatures
	
	
	
	
	// Field focus on Mailing List form on communities pages
	
	if ($('mlFullName')) {
		$('mlFullName').addEvent('focus', function(){
			if($('mlFullName').value == "Full Name")
			{
				$('mlFullName').set('value', '');
			};
		});
		$('mlFullName').addEvent('blur', function(){
			if($('mlFullName').value == "")
			{
				$('mlFullName').set('value', 'Full Name');
			};
		});
		$('mlEmailAddress').addEvent('focus', function(){
			if($('mlEmailAddress').value == "Email Address")
			{
				$('mlEmailAddress').set('value', '');
			};
		});
		$('mlEmailAddress').addEvent('blur', function(){
			if($('mlEmailAddress').value == "")
			{
				$('mlEmailAddress').set('value', 'Email Address');
			};
		});
	} // End if mlFullName
	


	// Map ToolTips
	var mapToolTip = new Tips($$('.mapToolTip'), {
		showDelay: 0,
		hideDelay: 0,
		fixed: false,
		offset: {x:-35, y:-45},
		text: false
	});
	
	
	// Feature Packages accordion
	var featurePackageAccordion = new Fx.Accordion($('featurePackageAccordion'), '#featurePackageAccordion h2', '#featurePackageAccordion .content', 
	{
	display:-1,
	alwaysHide: true
	});


	
	
}); // Close DomReady function




// Subscribe Form for Mailing Lists on communities pages

checkMailingListForm = function() {
	if ($('mlFullName').value =="" || $('mlFullName').value =="Full Name"){
		alert("Please provide your name.");
		$('mlFullName').focus();
		return false;
	}
	if ($('mlEmailAddress').value ==""){
		alert("Please enter your email address.");
		$('mlEmailAddress').focus();
		return false;
	}
	if ($('mlEmailAddress').value !=""){
		var x = $('mlEmailAddress').value;
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(x)) {
			alert("Oops, looks like there's a problem with your email address.");
			$('mlEmailAddress').focus();
			return false;
		}
	}
	return (true);
}





