
function setupHovers(){
	$('ul#topNav li').hover(function(){
		$(this).attr('id',$(this).attr('id').replace("Item","Selected"));		
	},function(){
		if(!$(this).hasClass('selected')){
			$(this).attr('id',$(this).attr('id').replace("Selected","Item"));
		}
	}).click(function(){
		$(this).siblings().each(function(index){
			$(this).removeClass('selected').attr('id',$(this).attr('id').replace("Selected","Item"));
		});
		$(this).addClass('selected').attr('id',$(this).attr('id').replace("Item","Selected"));
	});
}

$(function(){
	setupHovers();
});

/**
 * Validates the new address form in accounts section
 * 
 * @return
 */
/**
 * Email validator
 * LEAVE THIS ALONE!!
 * @param str
 * @return
 */
function validateEmail(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if(str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false;
	}
	if(str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}
	if(str.indexOf(at,(lat+1))!=-1){
		return false;
	}
	if(str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	}
	if(str.indexOf(dot,(lat+2))==-1){
		return false;
	}
	if(str.indexOf(" ")!=-1){
		return false;
	}
	return true;		
}


/**
 * Email Validator Function
 * 
 * @param 	str
 * @return	boolean
 */
function checkValidEmailWithoutMessage(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1)
	{
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1)
	{
		return false;
	}
		
	if (str.indexOf(" ")!=-1)
	{
		return false;
	}

 	return true;				
}

/**
 * AJAX Banner animations
 * 
 */
//Add new function to pickup the homepage click events
$(function(){
	setupFeatureBanners();
});

function setupFeatureBanners(){
	$('ul.tabs li a').unbind('click').click(function(){
		
		$('ul.tabs li a').removeClass('selected').unbind('click').click(function(e){
			e.preventDefault();
			return false;
		});
		
		$(this).addClass('selected');
		
		$.get("/ajax-banner/",{
			section:$(this).attr('id')
		},function(data){
			if(data != ""){	
				$('.feature-banner').append(data);
				$('.banner-content2').hide();
				$('.banner-content1').fadeOut('1000',function(){
					$('.banner-content1').detach();
					$('.banner-content2').attr('class','banner-content1').fadeIn('1000',function(){
						setupFeatureBanners();
					});
				});
			}
		});
		
		return false;
	});
}

/**
 * jQuery function to clear searchbox on click and focus
 * Will automatically add 'search' to the box if it's clear on focusout
 * 
 */
$(function(){
	$('#search').focusin(function(){
		var search = $('#search').val();
		if(search == "Search..."){
			$('#search').val("");
		}
	});
	
	$('#search').focusout(function(){
		var search = $('#search').val();
		if(search == ""){
			$('#search').val("Search...");
		}
	});
	
	$('#search').click(function(){
		var search = $('#search').val();
		if(search == "Search..."){
			$('#search').val("");
		}
	});
});



$(function(){
	$('#terms').submit(function(){
		// Assume we are ready
		var ready = true;
		
		// Cycle through each of the input fields
		$('input.form-input-required').each(function(index){
			
			// If this input is blank
			if($(this).val() == ""){
				// Set ready to false
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$(this).addClass('error');

				
			}else{
				// Otherwise do nothing as we are assuming ready and don't want to reset it
				
				// Make sure we remove any error classes
				$(this).removeClass('error');
			}
		});
		
		// We need to check for a valid email address
		if(ready == true){
			
			// Pass the email address to the valid email checking thingy
			if(checkValidEmailWithoutMessage($('input#email').val()) == true){
				// Has passed don't do anything
				
				// Make sure we remove any error classes
				$('input#email').removeClass('error');
				
			}else{
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$('input#email').addClass('error');
			}
		}
		
		if(ready == true){
			
			// Allow the form to submit
			return true;
			
		}else{
			// Alert user and don't let the form submit
			alert('Please fill in all the required fields, and ensure you have entered a valid email address');
			// Return false
			return false;
		}
	});
});


$(function(){
	$('#enquiryForm').submit(function(){
		// Assume we are ready
		var ready = true;
		
		// Check and enquiry type has been selected
		$('select.required').each(function(index){
			
			// If this input is blank
			if($(this).val() == ""){
				// Set ready to false
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$(this).addClass('error');

				
			}else{
				// Otherwise do nothing as we are assuming ready and don't want to reset it
				
				// Make sure we remove any error classes
				$(this).removeClass('error');
			}
		});
		
		
		
		// Cycle through each of the input fields
		$('input.required').each(function(index){
			
			// If this input is blank
			if($(this).val() == ""){
				// Set ready to false
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$(this).addClass('error');

				
			}else{
				// Otherwise do nothing as we are assuming ready and don't want to reset it
				
				// Make sure we remove any error classes
				$(this).removeClass('error');
			}
		});
		
		// We need to check for a valid email address
		if(ready == true){
			
			// Pass the email address to the valid email checking thingy
			if(checkValidEmailWithoutMessage($('input[name=email]').val()) == true){
				// Has passed don't do anything
				
				// Make sure we remove any error classes
				$('input[name=email]').removeClass('error');
				
			}else{
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$('input[name=email]').addClass('error');
			}
		}
		
		if(ready == true){
			
			// We need to check the textarea
			if($('textarea#message').val() == ""){
				// If we are blank set ready to false
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$('textarea#message').addClass('error');
	
				
			}else{
				// we don't need to change anything
				
				// Make sure we remove any error classes
				$('textarea#message').removeClass('error');
			}
		}
		if(ready == true){
			
			// Allow the form to submit
			return true;
			
		}else{
			// Alert user and don't let the form submit
			alert('Please fill in all the required fields, and ensure you have entered a valid email address');
			// Return false
			return false;
		}
	});
});
