
$(init);

var addButton;

function init(){

	$("input[type='button']").click(addNewFriend);
	//$("input.friend").val("");
	$("input.friend").keyup(validate);
	
	var okSymbol = createSymbol("okSymbol", "This email is valid.");
	$("input.friend").after(okSymbol);
	
	var invalidSymbol = createSymbol("invalidSymbol", "This is not a valid email");
	$(okSymbol).after(invalidSymbol);
	
	//make an initial validation of the first friend input, in case the user has navigated back and this field is prepopulated
	$("input.friend").keyup();
	
}

function validate(){
	
	if( validateEmail($(this).val()) ){
		//valid so show the tick, hide the cross
	$(this).next().show();
	$(this).next().next().hide();
	} else {
	//invalid, show the cross, hide the tick
	$(this).next().hide();
	$(this).next().next().show();
	}
	
	updateValidationResult();
}

function validateEmail(email){
	var regx = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	if(email.match(regx)){
	return true;	
	} else {
	return false;	
	}
}

function addNewFriend(){
	
	var target;
	target = $("img.invalidSymbol:last");
	if($("input.friend").length > 1){
	target = $("a.remove:last");
	}
	
	var newFriend = document.createElement("input");
	newFriend.size = 30;
    newFriend.name = "email[]";
	
	newFriend = $(newFriend);
	newFriend.addClass("friend");
	
	newFriend.insertAfter(target);
	newFriend.keyup(validate);
	
	var okSymbol = createSymbol("okSymbol", "This email is valid.");
	$(okSymbol).css("display", "none");
	$(okSymbol).insertAfter(newFriend);
	
	var invalidSymbol = createSymbol("invalidSymbol", "This is not a valid email " + $("img.invalidSymbol").length);
	$(okSymbol).after(invalidSymbol);
	
		var newLink = createRemoveLink();
		
		$(newLink).insertAfter(invalidSymbol);
	
		if($("input.friend").length > 9){
		//we've reached the maximum emails...
			$("input[type='button']").attr("disabled", true);
			$("input[type='button']").css("display", "none");
		}
	
	updateValidationResult();
	
}

function createRemoveLink(){
	var removeLink = document.createElement("a");
	$(removeLink).addClass("remove")
	removeLink.href = "#";
	removeLink.innerHTML = "Remove";
	$(removeLink).click(removeFriend);
	return removeLink;
}

function createSymbol(symbolName, title){
		var symbol = document.createElement("img");
		symbol.src = "img/" + symbolName + ".gif";
		$(symbol).addClass(symbolName);
		symbol.title = title;
		//symbol.style.display = "none";
		return symbol;
}

function removeFriend(){
		
		var removeLinks = $("a.remove").get();
		var linkIndex = $.inArray($(this).get(0), removeLinks);
		$("input.friend").eq(linkIndex + 1).remove();
		$("img.okSymbol").eq(linkIndex + 1).remove();
		$("img.invalidSymbol").eq(linkIndex + 1).remove();
		$(this).remove();
		
		if($("input.friend").length < 10) {
			$("input[type='button']").removeAttr("disabled");
			$("input[type='button']").css("display", "block");
		}
		
		updateValidationResult();
	
}

function validationResult(){
	
	var numInvalidEmails = $("img.okSymbol:hidden").length;
	var numValidEmails = $("img.okSymbol:visible").length;
	return [numValidEmails, numInvalidEmails];
	
}

function updateValidationResult(){

	var numInvalidEmails = validationResult()[1];
	var numValidEmails = validationResult()[0];
	
	if(numInvalidEmails > 0 && numValidEmails > 0){
		$("p#validationResult").html(numInvalidEmails + " of your emails are invalid.");
	} else {
		$("p#validationResult").html("");
	}
	
	if(numValidEmails < 1){
	//disable the submit button. No valid emails exist
		$("input[type='submit']").attr("disabled", true);
		$("p#validationResult").html("Please enter at least 1 valid email.");
	} else {
		if(numInvalidEmails > 0){
		$("p#validationResult").html(numInvalidEmails + " of your emails are invalid.");
		}
		$("input[type='submit']").attr("disabled", false);	
	}
	
}

function send(){
	var n = validationResult()[1];
	var conf;
	if(n > 0){
	conf = confirm(n + " of your emails are invalid. Invalid emails will not be sent.\n\nContinue?");
	}
	
	if( $("input[name='senderName']").val() == "" || $("input[name='sender_email']").val() == "" ){
	conf = false;
	alert("Please enter your name and email.");
	}
	
	return conf;
	
}