function initInvitePopup() {
	var openElements = YAHOO.util.Dom.getElementsByClassName('open_invite');
	var closeElements = YAHOO.util.Dom.getElementsByClassName('close_invite');
	var selects = document.getElementsByTagName('SELECT');
	var bubble = document.getElementById('emailInvitationBox');
	
	for(var i=0;  i < openElements.length; i++) {
		YAHOO.util.Event.addListener(openElements[i], 'click', function() {
			for(var j=0;  j < selects.length; j++) {
				selects[j].style.visibility = 'hidden';
			}
			//captures the position of the clicked button
			var x = YAHOO.util.Dom.getX(this);
			var y = YAHOO.util.Dom.getY(this);
			var xLoc = x - 150;
			var yLoc = y - 60;
			bubble.style.display = "block";
			YAHOO.util.Dom.setX(bubble, xLoc);
			YAHOO.util.Dom.setY(bubble, yLoc);
		 });
	}

	for(var k=0;  k < closeElements.length; k++) {
		YAHOO.util.Event.addListener(closeElements[k], 'click', function() {
			for(var j=0;j<selects.length; j++) {
				selects[j].style.visibility = 'visible';
			}
			bubble.style.display = 'none';
		 });
	 }
	 
}

function onInviteByEmail(form) {
	var toEmailAddress = new Array();
	toEmailAddress[0] = dwr.util.getValue("toEmailAddress1");
	toEmailAddress[1] = dwr.util.getValue("toEmailAddress2");
	toEmailAddress[2] = dwr.util.getValue("toEmailAddress3");
	toEmailAddress[3] = dwr.util.getValue("toEmailAddress4");
	toEmailAddress[4] = dwr.util.getValue("toEmailAddress5");
	var message = dwr.util.getValue("message");
	FriendshipService.addFriendByEmail(toEmailAddress, message);
	
	
	/*var inputs = YAHOO.util.Dom.getElementsByClassName('inviteEmailAddress', 'input', 'email_addresses');
	for (var i=1; i<5; i++) {
		inputs[i].style.display = 'none';
	}*/
	
	form.reset();
	inviteFormReset();
	
	//All this is to display the email addresses properly
	var sentTo = '';
	var filledEmails = new Array();
	for (var j=0; j<5; j++) {
		if (toEmailAddress[j] != "") {
			filledEmails.push(toEmailAddress[j]);
		}
	}
	
	for (var h=0; h<filledEmails.length; h++) {
		if (h == (filledEmails.length-1)) {
			sentTo += filledEmails[h];
		} else {
			sentTo += filledEmails[h] + ", ";
		}
	}
	
	document.getElementById('sentEmails').innerHTML = sentTo;
	document.getElementById('success_message').style.display = "block";
	document.getElementById('add_another').style.display = "block";
}

function onInviteExisting(form) { 
	var toUserName = dwr.util.getValue("toUserName");
	var message = dwr.util.getValue("message");
	FriendshipService.addFriendExisting(toUserName, message);
	form.reset();
	
}

function activeButtonCheck() {
	var emailReq = document.getElementById('toEmailAddress1');
	var textReq  = document.getElementById('message');
	var inputs = YAHOO.util.Dom.getElementsByClassName('inviteEmailAddress', 'input', 'email_addresses');
	var canSubmit = false;
	
	for(var i=0;i<inputs.length;i++){
		if(inputs[i].style.display!='none'){
			if(inputs[i] .value != "" && inputs[i] .value != "Example: mickey@disney.com") {
				canSubmit = true;
			}
		}
	}		
	
	if(canSubmit && textReq .value != ""){
		YAHOO.util.Dom.addClass('button_container', 'active');
	} else {
		YAHOO.util.Dom.removeClass('button_container', 'active');
	}
}

function inviteFormReset(){
	var inputs = YAHOO.util.Dom.getElementsByClassName('inviteEmailAddress', 'input', 'email_addresses');
	inputs[0].value = "Example: mickey@disney.com";
	for(var i=1;i<inputs.length;i++){
		inputs[i].style.display='none';
		YAHOO.util.Dom.removeClass(inputs[i], 'required');
		inputs[i].value = "";
	}
	
}

function addAnother() {
	var emailReq = document.getElementById('toEmailAddress1');
	var textReq  = document.getElementById('message');
	var inputs = YAHOO.util.Dom.getElementsByClassName('inviteEmailAddress', 'input', 'email_addresses');
	var button = document.getElementById('add_another');
	
	for(var i=0;i<inputs.length;i++){
		if(inputs[i].style.display=='none'){
			YAHOO.util.Dom.addClass(inputs[i], 'required');
			inputs[i].style.display='block';
			
			if(i == 4){
				button.style.display = 'none';
			}
			
			break;
		}
	}
}