$(document).ready(function(){
	
	$("#MainNav li").hover(
		function(){
			$(this).addClass('Hover');
		},
		function(){
			$(this).removeClass('Hover');
		}
	);
	
	$("#txtSearch").focus(function(){
		if ($(this).val() == 'Search...')
		{
			$(this).val('');
		}
	});
	
	$("#txtSearch").blur(function(){
		if ($(this).val() == '')
		{
			$(this).val('Search...');
		}
	});


	if($.cookie("css")) {
		$("link").attr("href",$.cookie("css"));
		$('#TextOnly').html('Graphical');
	}
	
	$("#TextOnly").click(function() { 
	if($.cookie("css")) {
		$.cookie("css",null, {expires: 365, path: '/'});
		$('#TextOnly').html('text only');
		$("link").attr("href","/styles/cscs-v4.css");
	}else{
		$("link").attr("href",$(this).attr('rel'));
		$.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
		$('#TextOnly').html('Graphical');
		return false;
	}
	
});

$("#em").attr("href", "mailto:?subject=CSCS%20website&body=Thought%20you%20might%20be%20interested%20this%20webpage: http://www.cscs.uk.com" + window.location.pathname);

	var $z = "0";
	
		function validfield($id){
			
			$($id).removeClass("red");
		
			if($($id).val() == ''){
				//alert($id);
				$($id).addClass("redb");
				$($id).focus();
				$z = "1";
				/*return false;*/
			}
		}	


	$('#conform').submit(function() {
		$z = "0";
			
		validfield('#xPostcode');
		validfield('#xDoB');
		validfield('#xTelephone');
		validfield('#xEmail');
		validfield('#xName');
		
			if($z == "1"){
				return false;
			}
	});
	

	$("#xMessage").click(function(){
			$(this).val("");
			$(this).addClass("cblack");
	});





var surcook = get_cookie ( "sur" );
if(surcook == "x")
	{
		$("#popupContact").hide();
		$("#backgroundPopup").hide();	
	}


$("#popupContactClose").click(function () {
		var cookie_date = new Date ( );  // current date & time
		cookie_date.setDate ( cookie_date.getDate() + 1 );
		document.cookie = "sur=x; expires=" + cookie_date.toGMTString();
    });


	$('#eventbook').submit(function() {					
									
			if($('#n').val() == ''){
				$("#frmmsg").html('<p>Please enter ensure you enter a name</p>');
				return false; 
			};

			if($('#t').val() == ''){
				$("#frmmsg").html('<p>Please ensure you enter a telephone number</p>');
				return false; 
			};
			
			if(isNaN($('input[name=eventloc]:checked').val())){
				$("#frmmsg").html('<p>Please ensure you select a date</p>');
				return false; 
			};
			
			if(isValidEmailAddress($('#e').val())){
				// do nothing
			} else {
				$("#frmmsg").html('<p>Please ensure you select a valid email address</p>');
				return false; 
			}
			
			
	});





});




function get_cookie(cookie_name)
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}


function isValidEmailAddress(emailAddress) {

var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

return pattern.test(emailAddress);

}

//alert(surcook);


function suggest(inputString){
		if(inputString.length == 0) {
			$('#suggestions').fadeOut();
		} else {
		$('#ins').addClass('load');
			$.post("ins.php", {queryString: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions').fadeIn();
					$('#suggestionsList').html(data);
					$('#ins').removeClass('load');
				}
			});
		}
	}

	function fill(thisValue) {
		$('#ins').val(thisValue);
		setTimeout("$('#suggestions').fadeOut();", 600);
	}


