// JavaScript Document


$(document).ready (function() {
							
// Put down arrow on first li that has children, otherwise leave li with class of active only...
					
	try {
		$('.left-column ul li.active:has(> ul)').addClass('submenu');
		$('.left-column ul li:hasnot(> ul)').removeClass('submenu');
		$('.left-column ul ul li.active, .left-column ul ul li').removeClass('submenu');
	} catch (ex) {

	}
// Sitemap - every third list item adds a class of 'end'...

	$("#ctl00_Content_ctl00_menu ul li:nth-child(3)").addClass("end");
	$("#ctl00_Content_ctl00_menu ul li:nth-child(6)").addClass("end");	
	
// h2 following an h1 has different margin-top

	$('h1 + h2').addClass('inline');
	$('h2 + h3').addClass('h3inline');


  $('.contactsubmit').click(function(event) {
      
      $("#contact label").removeClass("error");
  		var aErrors = new Array(0);

  		if($("#ctl00_Content_ctl00_firstName").val() == "") {
  			$("#ctl00_Content_ctl00_firstName").prev("label").addClass("error");
  			aErrors.push("- Please enter your name.");
  		}

  		if($("#ctl00_Content_ctl00_emailAddress").val() == "") {
  			$("#ctl00_Content_ctl00_emailAddress").prev("label").addClass("error");
  			aErrors.push("- Please enter the primary contacts email address.");
  		} else {
  		    if(!/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test($('#ctl00_Content_ctl00_emailAddress').val())) {
  	            $("#ctl00_Content_ctl00_emailAddress").prev("label").addClass("error");
  			    aErrors.push("- The primary contact email address is invalid.");
              }
  		}
  		
  		if($("#ctl00_Content_ctl00_comments").val() == "") {
  			$("#ctl00_Content_ctl00_comments").prev("label").addClass("error");
  			aErrors.push("- Please enter your enquiry.");
  		}
  		

  		if (aErrors.length) {
  		    displayErrorsAlert(aErrors);
  		    return false;
  		}

  		return true;

  	});

  	
});



// Form Validation
function displayErrorsAlert(aErrors) {

    if (aErrors.length) {
        if (aErrors.length > 1) {
            var prefix = 'Some errors occurred:\n\n';
            var suffix = '\nPlease correct these errors and try again.';
        }
        else {
            var prefix = 'An error has occurred:\n\n';
            var suffix = '\nPlease correct this error and try again.';
        }

        var errorMessage = prefix;

        for (var i = 0; i < aErrors.length; i++) {
            errorMessage += aErrors[i] + "\n";
        }

        errorMessage += suffix;

        alert(errorMessage);
    }

}
