/**
 * @author Fabien
 */

//Preload the spinner
var pic = new Image;
pic.src = '/Assets/images/wait16trans.gif';


function assignSpkMeeting(omeId, spkId)
{
	
	var zChk = $('chkSpeakerContainer_' + spkId).innerHTML;
	var isChecked = $('chkSpeaker_' + spkId).checked;
	
	//First check if we don't already have 4 speakers.
	var nbSpeakers = 0;
	$$('#SpeakerList input.chkSpeaker').each(function(chk) {
		if(chk.checked) {
			nbSpeakers++;
		}
	});
	if (nbSpeakers <= 4) {
		
		$('chkSpeakerContainer_' + spkId).update('<img src="/Assets/images/wait16trans.gif" />');
		
		remoteCall('assignSpeakerToMeeting', [omeId, spkId], function(r){
			$('chkSpeakerContainer_' + spkId).update(zChk);
			if ((r.responseJSON.success)) {
				$('chkSpeaker_' + spkId).checked = r.responseJSON.data.checkbox;
				
				if (r.responseJSON.data.checkbox == true) {
					$('speakerVignettes').down('tr').insert({
						bottom: decodeURIComponent(r.responseJSON.data.addVignette)
					})
				}
				else {
					$('pic_' + r.responseJSON.data.removeVignette).remove();
					$('info_' + r.responseJSON.data.removeVignette).remove();
				}
			}
			else {
				$('chkSpeaker_' + spkId).checked = !isChecked;
				alert("An error occured, please try again\nor contact us if the problem persists.");
			}
		});
	}
	else {
		$('chkSpeaker_' + spkId).checked = !isChecked;
		alert("Sorry but a maximum of 4 speakers is allowed.\nYou have to uncheck one speaker if you want to add this one.");
	}
}


function assignPrtMeeting(omeId, prtId)
{
	var zChk = $('chkParticipantContainer_'+prtId).innerHTML;
	var isChecked = $('chkParticipant_'+prtId).checked;
	
	$('chkParticipantContainer_'+prtId).update('<img src="/Assets/images/wait16trans.gif" />');
	
	remoteCall('assignParticipantToMeeting', [omeId, prtId], function (r) {
		$('chkParticipantContainer_'+prtId).update(zChk);
		if((r.responseJSON.success)) {
			$('chkParticipant_'+prtId).checked = r.responseJSON.data;
			if($('participantInvit_'+omeId+prtId)) {
				if($('chkParticipant_'+prtId).checked) {
					$('participantInvit_'+omeId+prtId).show();
				}
				else {
					$('participantInvit_'+omeId+prtId).hide();
				}
			}
		}
		else {
			$('chkParticipant_'+prtId).checked = !isChecked;
			alert("An error occured, please try again\nor contact us if the problem persists.");
		}
	});
}


function getUserInfos(img, type, id)
{
	var oldImg = img.src;
	img.src = '/Assets/images/wait16trans.gif';
	
	remoteCall('getUserInfos', [type, id], function (r) {
		if (r.responseJSON.success) {
			if (r.responseJSON.data != false) {
				var userInfos = r.responseJSON.data;
				$('sUSR_FIRST_NAME').value = userInfos.USR_FIRST_NAME;
				$('sUSR_LAST_NAME').value = userInfos.USR_LAST_NAME;
				$('sUSR_EMAIL').value = userInfos.USR_EMAIL;
				
				if (userInfos.USP_PATH != '' && userInfos.USP_PATH != null) {
					$('photoDisplay').src = "http://www.mba-exchange.com/photoMBAX/" + userInfos.USP_PATH;
				}
				
				$('userType').value = type;
				if (type == 'speaker') {
					$('sUSR_JOBTITLE').value = (userInfos.SPK_JOBTITLE == null) ? '' : userInfos.SPK_JOBTITLE;
					$('sUSR_BIOGRAPHY').value = (userInfos.SPK_BIO == null) ? '' : userInfos.SPK_BIO;
					$('zUsrId').value = userInfos.SPK_ID;
					showSpeakerDetails();
				}
				else {
					$('sUSR_JOBTITLE').value = (userInfos.PRT_DESC == null) ? '' : userInfos.PRT_DESC;
					$('zUsrId').value = userInfos.PRT_ID;
					hideSpeakerDetails();
				}
				
				window.scrollTo(0,0);
				
				$('UserDetails').show();
				$('ModalDiv').show();
			}
			else {
				alert("An error occured, please try again\nor contact us if the problem persists.");
			}
		}
		else {
			alert("An error occured, please try again\nor contact us if the problem persists.");
		}
		
		img.src = oldImg;
	});
}


function removeUser(img, type, id)
{
	var oldImg = img.src;
	img.src = '/Assets/images/wait16trans.gif';
	
	remoteCall('removeUser', [type, id], function (r) {
		if (r.responseJSON.success) {
			if (r.responseJSON.data == true) {
				$(type + '_' + id).remove();
			}
			else {
				alert("We can't remove this user as he is assigned to 1 or more online meetings.");
			}
		}
		else {
			alert("An error occured, please try again\nor contact us if the problem persists.");
		}
		
		img.src = oldImg;
	});
}


function sendInvitation(omeId, id, type)
{
	if (confirm("Send the invitation by email now?")) {
		remoteCall('sendInvitation', [omeId, id, type], function(r){
			if (r.responseJSON.success) {
				$(type + 'Invit_'+omeId+id).remove();
				
				if ($('spNbInvitePending')) {
					var nbPending = parseInt($('spNbInvitePending').innerHTML);
					nbPending--;
					if (nbPending < 1) {
						$('pendingInvitations').update('');
					}
					else {
						$('spNbInvitePending').update(nbPending);
					}
				}
				alert("Invitation sent successfully");
			}
			else {
				alert("An error occured, please try again\nor contact us if the problem persists.");
			}
		});
	}
	else {
		alert("Invitation not sent");
	}
	return false;
}

function setReminder(omeId)
{
	var reminderValue = $('slcReminder_' + omeId).value;
	remoteCall('setReminder', [omeId, reminderValue], function(r){
		if (r.responseJSON.success) {
			$('changesApplied_' + omeId).appear();
			window.setTimeout(function() {
				$('changesApplied_' + omeId).fade();
				$('signUpDiv_' + omeId).hide();
			}, 5000);
		}
		else {
			alert("An error occured, please try again\nor contact us if the problem persists.");
		}
	});
}



function setReminderChat(chaId)
{
	var reminderValue = $('slcReminderChat_' + chaId).value;
	remoteCall('setReminderChat', [chaId, reminderValue], function(r){
		if (r.responseJSON.success) {
			$('changesAppliedChat_' + chaId).appear();
			window.setTimeout(function() {
				$('changesAppliedChat_' + chaId).fade();
				$('signUpChatDiv_' + chaId).hide();
			}, 5000);
		}
		else {
			alert("An error occured, please try again\nor contact us if the problem persists.");
		}
	});
}


function embedRegister(id, typ)
{
	if($('email_'+id).value=='' || $('email_'+id).value == 'enter email address')
	{
		alert('please enter your email');
		return false;
	}else{
		var mail = $('email_'+id).value;
		remoteCall('registerEvent', [mail, id, typ], function(r){
			if (r.responseJSON.success) {
				if(r.responseJSON.data == "SignUpOk") {
					$('signUp_' + id).hide();
					$('signed_' + id).show();
					$('RegButton_' + id).hide();
				}
				else {
					if(r.responseJSON.data == "NotUser") {
						parent.location.href = urlRegForm + '?sUSR_EMAIL=' + mail + '&mId=' + id + '&typ=' + typ;
					}
				}
			}
			else {
				$('noaccess_' + id).show();
			}
		});
		
	}
}




function link(url, qp)
{
	qp = qp || {};
	qp.session = PHP_session;
	return url + '?' + $H(qp).toQueryString();
}


function remoteCall(method, params, callback, oncomplete)
{
	var completeCall = null;
	if(oncomplete) {
		completeCall = oncomplete;
	}
	new Ajax.Request(link("../Ajax/OnlineMeeting.php"), {
		parameters: {
			request: $H({
				method: method,
				params: params
			}).toJSON(),
			plaCode: plaCode,
			frmId: frmId,
			tapId: tapId,
			section: PHP_section
		},
		onSuccess: callback,
		onComplete: completeCall
	});
}
