
if( !invite) var invite = {
	
	addressesToAdd: new Array(),
	connectLock: false,
	emailOrNetwork: false,
	error5000: "unknown error",
	error5001: "unknown error",
	error5002: "unknown error",
	error5003: "mail error",
	error5004: "invalid email",
	error5005: "Login failed. Check your email/password and try again",
	error5006: "social netowrk problem",
	THANKS_STEP: "thanks",
	SERVICE_STEP: "service",
	EMAILS_STEP: "emails",
	SEND_INVITATION_STEP: "send_invitation",
	DEFAULT_MESSAGE: "Enter emails separated by commas"
	
	
	
};

invite.getError = function( errorString )
{
	if( errorString.indexOf("5000") > -1)
		return eval("invite.error" + 5000);
	if( errorString.indexOf("5001") > -1)
		return eval("invite.error" + 5001);
	if( errorString.indexOf("5002") > -1)
		return eval("invite.error" + 5002);
	if( errorString.indexOf("5003") > -1)
		return eval("invite.error" + 5003);
	if( errorString.indexOf("5004") > -1)
		return eval("invite.error" + 5004);
	if( errorString.indexOf("5005") > -1)
		return eval("invite.error" + 5005);
	if( errorString.indexOf("5006") > -1)
		return eval("invite.error" + 5006);
	
	
	return eval("invite.error" + 5000);
}

invite.checkClick = function( event )
{
	var element = utils.whichElement( event );
	if( element && element.name && element.name == 'adrs')
	{
		if( element.checked )
			invite.addAddress( element.value );
		else
			invite.removeAddress( element.value );
	}
	if( element && element.id && element.id == 'se')
	{
		var checkboxes = document.getElementsByName('adrs');
		
		for(var index=0; index<checkboxes.length; index++) {
		
			checkboxes[index].checked = element.checked;
			if( checkboxes[index].checked )
				invite.addAddress( checkboxes[index].value );
			else
			{
				invite.removeAddress( checkboxes[index].value );
			}
		}
	}
}
invite.addAddress = function( address )
{
	invite.addressesToAdd.push( address )	
}
invite.removeAddress = function( address )
{
	for(var index=0; index<invite.addressesToAdd.length; index++) {
		
		if( invite.addressesToAdd[index] == address  )
		{
			invite.addressesToAdd.splice(index,1);
		}
	}
}

invite.populateAddresses = function(serverList, emailOrNetwork)
{
	var addresses = "";
	invite.emailOrNetwork  = emailOrNetwork;
	invite.addressesToAdd = new Array();
	
	document.getElementById('se').checked = true;
	
	if( emailOrNetwork )
	{
		for(var index=0; index<serverList.length; index++) {
			
			addresses+= '<input checked type="checkbox" id="' + index + '" name="adrs" value="' + serverList[index][1] + '"/><label for="' + index + '">' + serverList[index][0] + '</label>'
		}
	}
	else
	{
		for(var index=0; index<serverList.length; index++) {
			
			addresses+= '<input checked type="checkbox" id="' + index + '" name="adrs" value="' + serverList[index][1] + '"/><label for="' + index + '">' + serverList[index][0] + '</label>'
		}
	}
	
	
	document.getElementById('logginButton').className = document.getElementById('logginButton').className.replace(/ajaxTransCommandButton/,'');
	document.getElementById('logginButton').value = "Continue";
	document.getElementById('socialDataLoading').className = document.getElementById('socialDataLoading').className.replace(/\bvisible/,'invisible');
	
//	alert('mycustomscroll_contentwrapper: ' + document.getElementById('mycustomscroll_contentwrapper'));
//	alert('mySimpleScroll: ' + document.getElementById('mySimpleScroll'));
	
//	if( document.getElementById('mycustomscroll_contentwrapper') )
//		document.getElementById('mycustomscroll_contentwrapper').innerHTML = addresses;
//	else
//	{
//		alert('b4 ' + document.getElementById('mySimpleScroll').innerHTML);
		document.getElementById('mySimpleScroll').innerHTML = addresses;
//		alert('after ' + document.getElementById('mySimpleScroll').innerHTML);
//	}	
	serverList = null;
	
//	var scrollDiv = document.getElementById('mycustomscroll');
//	
//	if(scrollDiv && scrollDiv.scrollUpdate ) 
//	{
//		
//			scrollDiv.scrollUpdate();
//	}

	
}
invite.sendAnonymous = function()
{
	var to = document.getElementById('anonymousTo').value;
	var email = document.getElementById('anonymousEmail').value;
	var inviterName =	document.getElementById('anonymousName').value;
	var message = document.getElementById('anonymousMessage').value;
	
	
	if( to.isEmpty() || email.isEmpty() || inviterName.isEmpty() )
		return false;
	else if( !invite.connectLock )
	{
		document.getElementById('anonymousInvitationButton').className = "commandButton ajaxTransCommandButton";
		document.getElementById('anonymousInvitationButton').value = "Sending..."
		document.getElementById('anonymousInvitationLoading').className = document.getElementById('anonymousInvitationLoading').className.replace(/\binvisible/,'visible');
		
		invite.connectLock = true;			
		
		UserInvitationBean.sendInvitations(to, message, inviterName, true, {
			
			//callback: function(errorList){
			callback: function(){
				
				document.getElementById('anonymousInvitationButton').className = document.getElementById('anonymousInvitationButton').className.replace(/ajaxTransCommandButton/,'');
				document.getElementById('anonymousInvitationButton').value = "Invite them in!";
				document.getElementById('anonymousInvitationLoading').className = document.getElementById('anonymousInvitationLoading').className.replace(/\bvisible/,'invisible');
				invite.connectLock = false;
				invite.goToStep( invite.THANKS_STEP );
//				if( errorList && errorList.length > 0 )
//				{
//					document.getElementById('inviteError').className = document.getElementById('inviteError').className.replace(/invisible/,'visible');
//				}
//				else
//				{
//					invite.goToStep( invite.THANKS_STEP );
//					document.getElementById('inviteError').className = document.getElementById('inviteError').className.replace(/visible/,'invisible');
//				}
				
			},
			errorHandler: function(error){
				
				document.getElementById('anonymousInvitationButton').className = document.getElementById('anonymousInvitationButton').className.replace(/ajaxTransCommandButton/,'');
				document.getElementById('anonymousInvitationButton').value = "Invite them in!";
				document.getElementById('anonymousInvitationLoading').className = document.getElementById('anonymousInvitationLoading').className.replace(/\bvisible/,'invisible');
				invite.connectLock = false;
				
			}
			
		})
		
	}
	
}
invite.sendLoggedInInvitation = function()
{
	if( invite.connectLock ) return;
	var addresses = invite.addressesToAdd.join() + "," + document.getElementById('loggedInTo').value;
	var message = document.getElementById('message').value;
	invite.connectLock =true;
	
	if( invite.emailOrNetwork )
	{
		
		UserInvitationBean.sendInvitations(addresses,message, null, false,{
		
			callback: function(){ 
				
				document.getElementById('sendInvitationButton').className = document.getElementById('sendInvitationButton').className.replace(/ajaxTransCommandButton/,'');
				document.getElementById('sendInvitationButton').value = "Invite them in!";
				document.getElementById('invitationLoading').className = document.getElementById('invitationLoading').className.replace(/\bvisible/,'invisible');
				invite.connectLock = false;
				invite.goToStep( invite.THANKS_STEP );
			},
			errorHandler: function(error){
				
				
				
				invite.connectLock = false;
			}
		
		});	
		
		
		
	}
	else
	{
		
		var username = document.getElementById('username').value;
		var password = document.getElementById('password').value;
		var networkBox = document.getElementById('networkOptions');
		var network = networkBox.options[networkBox.selectedIndex].value;
		
		UserInvitationBean.sendInivtationToSocialNetwork(username, password, invite.addressesToAdd,message,network, {
		
			callback: function(){ 
				
				invite.connectLock = false;
				document.getElementById('sendInvitationButton').className = document.getElementById('sendInvitationButton').className.replace(/ajaxTransCommandButton/,'');
				document.getElementById('sendInvitationButton').value = "Invite them in!";
				document.getElementById('invitationLoading').className = document.getElementById('invitationLoading').className.replace(/\bvisible/,'invisible');
				invite.goToStep( invite.THANKS_STEP );
			},
			errorHandler: function(error){
				
				invite.connectLock = false;
				
			}
		
		})
	}
	
	document.getElementById('sendInvitationButton').className = "commandButton ajaxTransCommandButton";
	document.getElementById('sendInvitationButton').value = "Sending..."
	document.getElementById('invitationLoading').className = document.getElementById('invitationLoading').className.replace(/\binvisible/,'visible');
		
}
invite.loginToService = function()
{
	if( invite.connectLock ) return;
	
	var service = 		null;
	var emailOrNetwork = false;
	var getContacts = false;
	var simpleSend = false;
	var emailList = 	document.getElementById('emailOptions');
	var networkList = 	document.getElementById('networkOptions');
	var username = 		document.getElementById('username').value;
	var password = 		document.getElementById('password').value; 
	var simpleAddresses= document.getElementById('loggedInTo').value;
	
	document.getElementById('loginErrorMessage').innerHTML = "";
	
	if( ( !username.isEmpty() && !password.isEmpty() ) )
		getContacts = true;
	
	if( !simpleAddresses.isEmpty() && (trim(simpleAddresses) != invite.DEFAULT_MESSAGE ) )
	{
		simpleSend = true;
		invite.emailOrNetwork = true;
	}
		
	if( !simpleSend && !getContacts ) return;
	
	
	if( emailList.selectedIndex > 0)
	{
		service = emailList.options[emailList.selectedIndex].value;
		emailOrNetwork = true;
	}
	if( networkList.selectedIndex > 0)
	{
		service = networkList.options[networkList.selectedIndex].value;
		emailOrNetwork = false;
	}
	if( service == null) return;
	

	
	if( getContacts )
	{
			invite.connectLock = true;
		
			UserInvitationBean._path = utils.getSecuredPagesURL() + "dwr/"
			
			UserInvitationBean.getContacts(username, password, service, emailOrNetwork,{
				
				callback: function(serverList){ 
				
					invite.connectLock = false;
					invite.populateAddresses(serverList,emailOrNetwork);
					invite.goToStep( invite.EMAILS_STEP );
					UserInvitationBean._path = utils.getNonSecuredPagesURL() + "dwr/"
					
				},
				rpcType: dwr.engine.ScriptTag,
				httpVerb:"POST",
				
				errorHandler: function( error ){
					
					var errorCause = invite.getError(error);
					document.getElementById('loginErrorMessage').innerHTML = errorCause;
					document.getElementById('logginButton').className = "commandButton";
					document.getElementById('logginButton').value = "Continue"
					document.getElementById('socialDataLoading').className = document.getElementById('socialDataLoading').className.replace(/\bvisible/,'invisible');
					UserInvitationBean._path = utils.getNonSecuredPagesURL() + "dwr/"
					invite.connectLock = false;	
				}
				
			});
			
			document.getElementById('logginButton').className = "commandButton ajaxTransCommandButton";
			document.getElementById('logginButton').value = "Connecting..."
			document.getElementById('socialDataLoading').className = document.getElementById('socialDataLoading').className.replace(/\binvisible/,'visible');
	
			
	}
	else if( simpleSend )
	{
		invite.goToStep(invite.SEND_INVITATION_STEP);
	}
	
	
	
	
}
invite.goToStep = function( step )
{
	if( step == invite.THANKS_STEP)
	{
		if(document.location.href.indexOf("invite.html") < 0)
			invite.openThanks();
		else
		
			document.location.href = "/thanks.html";
	}
	else if(step == invite.EMAILS_STEP)
	{
		document.getElementById('socialNetsStep').className = document.getElementById('socialNetsStep').className.replace(/\bvisible/,'invisible');
		document.getElementById('contactsStep').style.position = 'relative';
		document.getElementById('contactsStep').style.visibility= 'visible';
		
		document.getElementById('contactsStep').style.zIndex = 0;
		var scrollDiv = document.getElementById('mycustomscroll')
		if(scrollDiv && scrollDiv.scrollUpdate)
		{
		 	scrollDiv.scrollUpdate();
		
			document.getElementById('mycustomscroll_mcontentwrapper').style.width = "240px";
			document.getElementById('mycustomscroll_mcontentwrapper').style.height = "210px";
		}
		
	}
	else if( step == invite.SERVICE_STEP)
	{
		document.getElementById('socialNetsStep').style.display = "block";
	}
	else if( step == invite.SEND_INVITATION_STEP)
	{
		document.getElementById('socialNetsStep').className = document.getElementById('invitationMessageStep').className.replace(/\bvisible/,'invisible');
		document.getElementById('contactsStep').className = document.getElementById('invitationMessageStep').className.replace(/\bvisible/,'invisible');
		
		document.getElementById('invitationMessageStep').className = document.getElementById('invitationMessageStep').className.replace(/\binvisible/,'visible');
	}
	
}
invite.openInvitation = function()
{
	if( document.getElementById('mpl'))
	{
		document.getElementById('mpl').style.display = "none";
	}

	document.getElementById('inlineInvitationDiv').className = document.getElementById('inlineInvitationDiv').className.replace(/invisible/,'visible');

	
	if( document.location.href.indexOf("invite.html") > -1 || 
		document.location.href.indexOf("join.html") > -1 || 
		document.location.href.indexOf("thanks.html") > -1 || 
		document.location.href.indexOf("create-profile.html") > -1 )
		return;
	
	
	
	
	utils.dimThePage();
	//document.getElementById('inlineInvitationDiv').style.display = "block";
	document.getElementById('inlineInvitationDiv').style.zIndex = "200";
	if( globalVariables.isUserSignIn && generalFunctions.isSignedIn() )	
	{
		invite.openLoggedIn();
	}
	else
	{
		invite.openAnonymous();
	}
}
invite.openAnonymous = function()
{
	document.getElementById('anonymousTo').value = invite.DEFAULT_MESSAGE;
	document.getElementById('anonymousInviteStep').className = document.getElementById('anonymousInviteStep').className.replace(/\binvisible/,'visible');
	
}
invite.openLoggedIn = function()
{

	document.getElementById('socialNetsStep').className = document.getElementById('socialNetsStep').className.replace(/\binvisible/,' visible ');
	
}
invite.openWebMail = function()
{
	invite.closeInvitation('anonymousInviteStep');
	
	generalFunctions.showLoginBox( true );
	globalVariables.wantedToInvite = true;
}
invite.openMailClient = function()
{
	var to = document.getElementById('anonymousTo').value;
	var email = document.getElementById('anonymousEmail').value;
	var name =	document.getElementById('anonymousName').value;
	var message = document.getElementById('anonymousMessage').value;
	
	utils.unDimThePage();
	invite.closeInvitation('anonymousInviteStep');
	
	document.location.href = "mailto:" + to;
}

invite.closeInvitation = function( step )
{
	if( document.getElementById('mpl') )
		document.getElementById('mpl').style.display = "block";
	
	document.getElementById('inlineInvitationDiv').className = document.getElementById('inlineInvitationDiv').className.replace(/visible/,'invisible');
	
	if( step != 'contactsStep')
		document.getElementById(step).className = document.getElementById(step).className.replace(/visible/,'invisible');
	else
	{
		document.getElementById('contactsStep').style.position = "absolute";
		document.getElementById('contactsStep').style.visibility = "hidden";
		document.getElementById('contactsStep').style.top = "0px";
		document.getElementById('contactsStep').style.left = "0px";
		
	}
	
	if( step == 'anonymousInviteStep')
	{
		
		invite.resetAnonymous();
	}
	
	utils.unDimThePage();
}
invite.openThanks = function()
{
	document.getElementById('anonymousInviteStep').className = document.getElementById('anonymousInviteStep').className.replace(/\bvisible/,'invisible');
	document.getElementById('invitationMessageStep').className = document.getElementById('invitationMessageStep').className.replace(/\bvisible/,'invisible');
	
	document.getElementById('thanksBox').innerHTML = "Success! Invites sent."
	document.getElementById('thanksBox').className = document.getElementById('thanksBox').className.replace(/\binvisible/,'visible');
	
	window.setTimeout(function(){invite.closeInvitation('thanksBox')},5000);
	
}
invite.validate = function( input )
{
	if( input.id == 'anonymousTo') invite.anonymousBlur();
	
	var button = document.getElementById('anonymousInvitationButton')
	if( !invite.isValidAnonymousTo()  								 || 
		trim(document.getElementById('anonymousEmail').value) == ""  || 
		trim(document.getElementById('anonymousName').value) == "" ) 
		{
			if( button.className == "commandButton")
				button.className  = "disabledCommandButton";
		}
		else
		{
			 button.className = "commandButton"
		}
}
invite.anonymousFocus = function()
{
	var val = document.getElementById('anonymousTo');
	if( trim(val.value) == invite.DEFAULT_MESSAGE)
	{
		val.value = "";
	}
	
}
invite.anonymousBlur = function()
{
	var val = document.getElementById('anonymousTo');
	if( trim(val.value) == "")
	{
		val.value = invite.DEFAULT_MESSAGE;
	}	
}
invite.loggedInFocus = function()
{
	var val = document.getElementById('loggedInTo');
	if( trim(val.value) == invite.DEFAULT_MESSAGE)
	{
		val.value = "";
	}
	
}
invite.loggedInBlur = function()
{
	var val = document.getElementById('loggedInTo');
	if( trim(val.value) == "")
	{
		val.value = invite.DEFAULT_MESSAGE;
	}	
}
invite.isValidAnonymousTo = function()
{
	var val = trim(document.getElementById('anonymousTo').value);
	if( val == "" || val == invite.DEFAULT_MESSAGE )
		return false;
	
	return true;	
}
invite.resetAnonymous = function()
{
	document.getElementById('anonymousTo').value = invite.DEFAULT_MESSAGE;
	//document.getElementById('anonymousEmail').value = "";
	//document.getElementById('anonymousName').value = "";
	//document.getElementById('anonymousMessage').value = "";
	
	document.getElementById('anonymousInvitationButton').className = "disabledCommandButton";
	document.getElementById('anonymousInvitationLoading').style.display = "none";
	//if( document.getElementById('inviteError').className.startsWith("visible") )
	//	document.getElementById('inviteError').className = document.getElementById('inviteError').className.replace(/visible/,'invisible');
		
	 
}
document.onclick = invite.checkClick;



