/**
 * @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.careernomics.com/webboosterFiles/usr_photo/" + 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 SignUp(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;
		
		if (typ == 'chat') {
			remoteCall('prtChatSignUp', [mail, id], function(r){
				if (r.responseJSON.success) {
					$('signUp_' + id).hide();
					$('respTxt_' + id).update(r.responseJSON.data.resp);
					$('signed_' + id).show();
					$('RegButton_' + id).hide();
					
					window.setTimeout(function() {
						$('email_' + id).value='enter email address';
						$('respTxt_' + id).value='';
						$('signed_' + id).hide();
						$('signUp_' + id).hide();
						$('RegButton_' + id).show();
							
					}, 5000);
					
				}
				else {
					parent.location.href = urlRegForm + '?sUSR_EMAIL=' + mail + '&mId=' + id + '&typ=chat';
				}
				
			});
		}
		else {
			if (typ == 'meeting') {
				remoteCall('prtMeetingSignUp', [mail, id], function(r){
					if (r.responseJSON.success) {
						$('signUp_' + id).hide();
						$('respTxt_' + id).update(r.responseJSON.data.resp);
						$('signed_' + id).show();
						$('RegButton_' + id).hide();
						
						window.setTimeout(function() {
							$('email_' + id).value='enter email address';
							$('respTxt_' + id).value='';
							$('signed_' + id).hide();
							$('signUp_' + id).hide();
							$('RegButton_' + id).show();
							
						}, 5000);
						
					}
					else {
						parent.location.href = urlRegForm + '?sUSR_EMAIL=' + mail + '&mId=' + id + '&typ=meeting';
					}
					
				});
			}
		}
		
	}
}

function setReminder(omeId, prtId)
{
	var reminderValue = $('slcReminder_' + omeId).value;
	remoteCall('setReminder', [omeId, prtId, 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, prtId)
{
	var reminderValue = $('slcReminderChat_' + chaId).value;
	remoteCall('setReminderChat', [chaId, prtId, 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 SetExtUrl(id)
{
	var oldImg = $('saveExtUrl_' + id).src;
	$('saveExtUrl_' + id).src = '/Assets/images/wait16trans.gif';
	
	var adminUrl = $('txtAdminUrl_' + id).value;
	var speakerUrl = $('txtSpeakerUrl_' + id).value;
	var participantUrl = $('txtParticipantUrl_' + id).value;
	
	
	remoteCall('setMeetingUrls', [id, adminUrl, speakerUrl, participantUrl], function (r) {
		if (r.responseJSON.success) {
			$('saveExtUrl_' + id).src = oldImg;
			$('extUrls_' + id).hide();
			document.forms[0].submit();
		}else{
			alert("An error occured, please try again\nor contact us if the problem persists.");
		}
		
	});
	
	return false;
}



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;
	}
	
	var fId = '';
	var tId = '';
	var pSec = 'participant';
	
	if(typeof(frmId) != 'undefined') {
		fId = frmId;
	}
	if(typeof(tapId) != 'undefined') {
		tId = tapId;
	}
	if(typeof(PHP_section) != 'undefined') {
		pSec = PHP_section;
	}
	
	new Ajax.Request(link("../Ajax/OnlineMeeting.php"), {
		parameters: {
			request: $H({
				method: method,
				params: params
			}).toJSON(),
			plaCode: plaCode,
			frmId: fId,
			tapId: tId,
			section: pSec
		},
		onSuccess: callback,
		onComplete: completeCall
	});
}