$(document).ready(function() {
	$('#nav > li > a').click(
		function() {
		//$("#nav > li > ul").slideUp('slow');
			if($(this).siblings('ul').css('display') == 'none') {
				$(this).siblings('ul').slideDown('slow');
			} else {
				$(this).siblings('ul').slideUp('slow');
			}
		}
	)
	$('#joinButton').click (
		function() {
			$("#joinForm").slideDown();
		}
	)
	$("#joinForm").css('display','none');
	
	$('input, textarea').click(
		function() {
			if (this.value == this.defaultValue) {
				this.value = "";
				$('#btnSubmit').attr('value', 'Submit');
				$('.formbutton').attr('value', 'Find a Stockist');
			}
		}
	)
});

function join() {
	//Get form data
	$('#joinError').hide();
	
	if($('#joinemail').val() == '' || $('#joinemail').val() == 'Email' || echeck($('#joinemail').val()) == false) {
		$('#joinError').show();
	} else {
	
	var tp;
	var email = $('#joinemail').val();
	
	if ($("#joindatard").is(":checked")) {
		tp = 1;
	} else {
		tp = 0;
	}
	
	$.post("/join.asp", { email: email, data: tp }, function(data){
		$('#joinResult').text(data);
	});
	
	}

	return false;
}

function validateContact() {
	errorstring = "";
	error = 0;
	if($('#email').val() == "Email Address" || $('#email').val() == "") {
		errorstring = "<li>Email</li>";
		error = 1;
	} else if ($('#email').val() != $('#email1').val()) {
		errorstring = errorstring + "<li>Both email addresses must match</li>";
		error = 1;
	} else if (echeck($('#email').val()) == false){
		errorstring = errorstring + "<li>Invalid email address</li>";
		error = 1;
	}
	if($('#firstname').val() == "First Name" || $('#firstname').val() == "") {
		errorstring = errorstring + "<li>First Name</li>";
		error = 1;
	}
	if($('#lastname').val() == "Last Name" || $('#lastname').val() == "") {
		errorstring = errorstring + "<li>Last name</li>";
		error = 1;
	} 
	if($('#address').val() == "Address" || $('#address').val() == "") {
		errorstring = errorstring + "<li>Address</li>";
		error = 1;
	}
	if($('#telephone').val() == "Telephone Number" || $('#telephone').val() == "") {
		errorstring = errorstring + "<li>Telephone Number</li>";
		error = 1;
	} 
	if($('#message').val() == "Message" || $('#message').val() == "") {
		errorstring = errorstring + "<li>Message</li>";
		error = 1;
	} 
	if (error == 1) {
		$('#formerrorreport').html("<p>Please complete the following fields:</p><ul>" + errorstring + "</ul>");
		$('html').animate({scrollTop:0}, 'slow'); 
		return false;
	}
}

function echeck(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					
}


