/* menu */
function mainmenu(){
$(" #nav ul ").css({display: "none"}); // Opera Fix
$(" #nav li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(1);
		},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
		});
}


$(document).ready(function(){	

	// Home Page jQuery Cycle
    $('#main-image').cycle({
	fx: 'fade',
	slideExpr: 'div.slide'
	});
	$("#footer ul li a:not(:first)").addClass('border-left');

	/* SLIDE TOGGLES */	
	
	$(function() {
    $('#quotes').cycle({
        fx:     'fade',
        speed:  'fast',
        timeout: 6000,
		slideExpr: 'p'
		});
	});

	mainmenu();

	$(".qa strong").click(function(){
		$(this).parent().parent().find(".answer").slideToggle('slow');
	});
	
	$(".qa h2").click(function(){
		$(this).parent().find(".answer").slideToggle('slow');
	});
	
	$('a#TermsLink').click(function(){
		$('#terms').show(100);
	});
	
	/* Smooth scroll anchors */
	$('a[href^=#]').click(function(){
        var target_id = $(this).attr('href');
        var target = $(target_id);
        if (target.length)
        {
            var top = target.offset().top;
            $('html,body').animate({scrollTop: top}, 1000);
            return false;
        }
    });
	
	/* Form variables */
	$('.option').change(function(){
		OptionValue = $(this).val();
		if (OptionValue == 'Yes') 
		{
			$(this).parent().next().find('.input').addClass('required');
			$(this).parent().next().css('display','block');
		}
		else
		{
			$(this).parent().next().find('.input').removeClass('required');
			$(this).parent().next().css('display','none');
		}
	});

	
	/* FORM SUBMISSION */
	$(".submit").click(function() {
		/* Check if the Terms and Conditions have been accepted */
		TermsCheck = $('input[name="Terms"]').is(':checked');
			if (TermsCheck == false){alert ('Please accept the Terms and Conditions'); return false;}
		error = false;
		ErrorString = "";
		$(".errorCross").remove();
		$(this).parent().parent().find(".required").each(function(index) {
			TheLength = $(this).val().length;
			TheFieldValue = $(this).val();
			TheFieldName = $(this).parent().find("label").text();

			TheFieldName = TheFieldName.replace("*", "");
			TheFieldName = TheFieldName.replace(":", "");
			TheFieldName = TheFieldName.replace("XX", "");
			
			// check for x
			FirstChar = TheFieldName.charAt(0);
			if (FirstChar == 'X'){TheFieldName = TheFieldName.substring(1);}
			
			if (TheLength < 1 || TheFieldValue == 'Select ...')
			{
				error = true;
				$(this).parent().find("label").prepend('<span class="errorCross">X</span>');
				ErrorString = ErrorString + "<li>Please enter a value for the field: <strong>" + TheFieldName + "</strong></li>\n";
			}
			else if (TheFieldValue == 'Choose Day'  || TheFieldValue == 'Choose Month'  || TheFieldValue == 'Choose Year')
			{
				error = true;
				$(this).parent().find("label").prepend('<span class="errorCross">X</span>');
				TheFieldValue = TheFieldValue.replace("Choose ", "");
				ErrorString = ErrorString + "<li>Please enter a value for the field: <strong>" + TheFieldName + " - " + TheFieldValue + "</strong></li>\n";
			}
			else 
			{
				$(this).parent().find(".errorCross").remove();
			}
		});
		/* CHECK IF EMAIL ADDRESS IS VALID */
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		$(this).parent().parent().find(".email").each(function(index) {
		EmailAddress = $(this).val();
		if(!emailReg.test(EmailAddress)) {
			EmailFieldName = $(this).parent().find("label").text();
            ErrorString = ErrorString + "<li>Please enter a valid Email Address for: <strong>" + EmailFieldName + "</strong></li>";
            error = true;
			$(this).parent().parent().find(".email").parent().find("label").prepend('<span class="errorCross">x</span>');
        }
		});
		/* SUBMIT FORM IF NO ERRORS */	
		if (error == false){
			$(".errorMessage").remove();
			$(this).parent().parent().submit();
		}
		/* DISPLAY ALERT IF ERRORS */
		else
		{
			$(".errorMessage").remove();
			$(this).parent().parent().prepend('<div class="errorMessage" id="errorList"><p>Errors in form submission data, please review the highlighted fields.</p><ul>' + ErrorString + '</ul></div>');
			$('#errorLink').click();
			return false;
		}
	});
	
	$('.GalImage').fancybox();

});


