
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 contact 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;				
}


$(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(){
	$('#bookings').submit(function(){
		// Assume we are ready
		var ready = true;
		
		// Cycle through each of the input fields
		$('input.form-input-required, requiredPlaces required_dropdown').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, your message, and ensure you have entered a valid email address');
			// Return false
			return false;
		}
	});
});
