// Avon PPP E-invite JS

var emailPattern = /^([a-zA-Z0-9-_\+]+\.)*[a-zA-Z0-9-_\+]+@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]+$/;

//clear guest input boxes
function guestInputClear(){
	var guestfieldtext = "Guest email";
	$('.guestfield').unbind();
	$('.guestfield').focus(function() {
		if ($(this).val() == guestfieldtext) {
			$(this).val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).val(guestfieldtext);
		}
	});
}

function validateInvite(){
	if(!$('#name').val().length){
		alert('Please enter your name');
		$('#name').focus();
		return false;
	}
	if(!$('#email').val().length){
		alert('Please enter your email address');
		$('#email').focus();
		return false;
	}
	if(!emailPattern.test($('#email').val())){
		alert('Please enter a valid email address');
		$('#email').focus();
		return false;
	}
	if(!$('#date').val().length){
		alert('Please enter the date of your party');
		$('#date').focus();
		return false;
	}
	if(!$('#location1').val().length || $('#location1').val()=="First line of address"){
		alert('Please enter the location of your party');
		$('#location1').focus();
		return false;
	}

	guestOk=true;
	guestCounter=0;
	$('.guestlist input').each(function(){
		if($(this).val() !="Guest email" && !emailPattern.test($(this).val())){
			alert('The invitee email address "'+ $(this).val() +'" is invalid, please correct it');
			guestOk=false;
			return false;
		}else if($(this).val() !="Guest email"){
			guestCounter++;
		}
	});
	
	if(!guestCounter && guestOk){
		alert('Please specify some invitee email addresses');
		return false;
	}
	
	return guestOk;
}



$(document).ready(function() {
	
	// location1 clear input box
	var location1 = "First line of address";
	$('#location1').focus(function() {
		if ($(this).val() == location1) {
			$(this).val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).val(location1);
		}
	});
	
	// location2 clear input box
	var location2 = "Second line of address";
	$('#location2').focus(function() {
		if ($(this).val() == location2) {
			$(this).val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).val(location2);
		}
	});
	
	guestInputClear();
	
	//addmore buttons
	$('.addmore').click(function(){
		//find total number of fields at present
		guestMaxId=parseInt($('.guestlist input').length);
	
		for(i=1; i<=5; i++){
			$('.guestlist').append('<input type="text" id="guest'+ (guestMaxId+i) +'" name="guest'+ (guestMaxId+i) +'" class="guestfield" value="Guest email" />');
		}
		
		guestInputClear();
		
		//limit to 30
		if(guestMaxId+5 >= 30){
			$('.addmore').hide();
		}
	});	
	
});
