/**
 * DOM Ready Functions
 */
$(function(){
	
	/* Setup the top nav hovers */
	setupHovers();
	zIndexHandler();
});

/**
 * Setup the top navigation hovers and such
 */
function setupHovers(){
	/* Add a hover event to all ul topNav li's */
	$('ul#topNav li').hover(function(){		/* Hover in */
		/* Replace the Item, in it's id with Selected byt using the attr function */
		$(this).attr('id',$(this).attr('id').replace("Item","Selected"));		
	},function(){							/* Hover out */
		/* If it doesn't have a class of selected */
		if(!$(this).hasClass('selected')){
			/* Then re-switch the id back to show Item instead of Selected */
			$(this).attr('id',$(this).attr('id').replace("Selected","Item"));
		}
		/* End of Hover in/out, now add a click event function */
	}).click(function(){
		/* Once we click, iterate through this item's Siblings */
		$(this).siblings().each(function(index){
			/* Remove the selected Class and replace all 'Selected' text in id's with 'Item' so they are all unselected*/
			$(this).removeClass('selected').attr('id',$(this).attr('id').replace("Selected","Item"));
		});
		/* Now add the class of selected to this item,
		 * replace 'Item' with 'Selected' in the id 
		 * so we don't lose the selected state if we mouseOut */
		$(this).addClass('selected').attr('id',$(this).attr('id').replace("Item","Selected"));
	});
	
	/* End of setupHovers */
	
}


/**
 * jQuery to call in supersleight (png fix for IE6)
 * 
 * DOES NOT BREAK OTHER BROWSERS!!
 * 
 */
$(function(){
	$('body').supersleight({shim: '/images/blank.gif'});
});

/**
 * Sorts out the z-index bug in ie
 */
function zIndexHandler(){
	var zIndexNumber = 1000;
	$('div, ul, li').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 1;
	});
}

//function clearOverlays()



/**
 * 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(){
	$('#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;
		}
	});
});

