/* * * *  this file requires file utils.js from default module to work * * * */

var MIN_WIDTH = 790;
var MIN_HEIGHT = 535;

var POPUP_OPENER_ID = "popupOpenerDiv";

var vbScriptEnabled = false;
// this method is called by a piece of vbScript code to indicate to javascript
// that the current browser supports vbScript
function vbScriptDetected() {
	//alert('vbScriptDetected called!');
	vbScriptEnabled = true;
}

// returns true is the browser of the user is Internet Explorer (identified
// via userAgent and VBScript support detection
function isMSIE() {
	return (navigator.appName.indexOf("Microsoft") != -1 && vbScriptEnabled);
}

/******************************************************************************/
/*            write the html code to include flash file into HTML             */
/******************************************************************************/
var FLASH_MOVIE_NAME = 'flashapp';
function writeFlash(scheme, swf, flashVars, w, h,color) {
	var str = getFlashEmbeddingString(scheme, swf, 'isMSIE=' + isMSIE() + '&' + flashVars,
	                                  FLASH_MOVIE_NAME, w, h, color?color:'#666666','high', false);
	document.write(str);
}


function writeNavis(scheme, swf, flashVars, w, h, color) {
	var str = getFlashEmbeddingString(scheme, swf, 'isMSIE=' + isMSIE() + '&' + flashVars,
	                                  FLASH_MOVIE_NAME, w, h, color,'high',
	                                  false, 'opaque');
	document.write(str);
}

function writeFlashDemo(scheme, swf, flashVars, w, h, color) {
	var str = getFlashEmbeddingString(scheme, swf, 'isMSIE=' + isMSIE() + '&' + flashVars,
	                                  FLASH_MOVIE_NAME, w, h, color,'high',
	                                  false, 'opaque');
	document.write(str);
}
/******************************************************************************/
/*                                Navigation                                  */
/******************************************************************************/
// activates/disables the message displayed by the browser when the user
// attemps to quit the page
// note: assumes a method called confirmExit() has been defined
function enableNavigationPrevent(flag) {
	if (isFramed()) {
		parent.enableNavigationPrevent(flag);
	}
	else {
		if (flag) {
			window.onbeforeunload = confirmExit;
		}
		else {
			window.onbeforeunload = null;
		}
	}
}

// resize the frames' height to display only the flash
// this method has no effect if called out of the frameset window
function showFlash() {
	if (functionExists('resizeRows')) {
		resizeRows('0','0','*');
	}
}

// resize the frames' height to display only the loader
// this method has no effect if called out of the frameset window
function showLoader() {
	if (functionExists('resizeRows')) {
		resizeRows('*','0','0');
	}
}

// resize the frames' height to display only the loader and the back frame
// this method has no effect if called out of the frameset window
function showLoaderAndBack() {
	if (functionExists('resizeRows')) {
		resizeRows('90%','10%','0');
	}
}
function showPopupOpener() {
	if (functionExists('resizeRows')) {
		resizeRows('50','0','*');
	}
}

function resetLoader() {
	if (isFramed()) {
		parent.resetLoader();
	}
	else {
		if (functionExists('getLoaderFrame')) {
			var loaderFrame = getLoaderFrame();
			if (loaderFrame) {
				var url = base + 'empty.do';
				loaderFrame.location.href = url;
			}
		}
	}
}

// show the flash and calls to simulates a click on the quit button
function callQuitInFlash() {
	if (isFramed()) {
		parent.callQuitInFlash();
	}
	else {
		if (functionExists('getFlashFrame')) {
			showFlash();
			var flashFrame = getFlashFrame();
			if (flashFrame) {
				flashFrame.writeToFlash(FLASH_MOVIE_NAME, 'jsCommand', 'close');
			}
		}
	}
}

/******************************************************************************/
/*             methods for interaction between flash and the browser          */
/******************************************************************************/
// function that converts an FSCommand to a javascript method call
// in Internet Explorer, this method is called by a piece of VBScript called by
// Flash, otherwise it is called directly by Flash
function FSCommand(command, args) {
//	alert('FSCommand(' + command + ',' + args + ') js called');
	switch(command){
		case "close":  closeWindow(); break;
		case "toReruns" : toReruns(); break;
		case "openAr": openAr(args); break;
		case "openTs": openTs(args); break;
		case "openCoco": openCoco(args); break;
		case "openDlManager": openDownloadManager(args,false); break;
		case "openDlManagerWithMailError": openDownloadManager(args,true); break;
		case "endDemo":  endDemo(); break;
		// FSCommand from Navis
		case "openRerun": openRerun(args);break;
	  case "openPointerDetails":openPointerDetails(args);break;
	  case "openPointerDetailsFromNemo":openPointerDetailsFromNemo(args);break;
	  case "openLastTempRerun":openLastTempRerun();break;
	  case "startModel":startModel(args);break;
	  case "editModel":editModel(args);break;
	  case "openRelatedAct":openRelatedAct(args);break;
	  case "nodeInRerunGroup":openNodeInRerunGroup(args);
	}
}

// function that should be called by flash client to close the browser window
function closeWindow() {
	if (isFramed()) {
		parent.closeWindow();
	}
	else {
		closeWindowSub(false);
	}
}

// function that should be called by flash client to close the browser window
// and refresh the desk to the
function toReruns() {
	if (isFramed()) {
		parent.toReruns();
	}
	else {
		closeWindowSub(true);
	}
}

function closeWindowSub(toReruns) {
	//alert('closeWindow() called');
	enableNavigationPrevent(false);
	refreshOpener(toReruns);
	window.close();
	// TODO : close all popups
}

function refreshOpener(toReruns) {
  try {
		if (window.opener && !window.opener.closed) {
			if (toReruns && rerunsURL)
				opener.document.location = rerunsURL;
			else
				opener.document.location.reload();
		}
  }
  catch (ex) {
    // ignored
  }
}

function endDemo() {
	//alert('endDemo() called');
	if (isFramed()) {
		parent.endDemo();
	}
	else {
		enableNavigationPrevent(false);
		if (homeURL) {
			document.location = homeURL;
		}
		else {
			document.location = "/";
		}
	}
}

// function that opens a popup with an AR
var arWindow = '';
function openAr(targetURL) {
	// ar window has scrollbars enabled
	arWindow = openCenteredPopup('arWindow', targetURL, 720, 540,
	                             'no', 'no', 'no', 'no', 'no', 'yes', 'yes');

	checkPopup(arWindow, POPUP_AR, targetURL);
	focusOnWindow(arWindow);
	return false;
}

// function that opens a popup with the printable termsheet
var tsWindow = '';
function openTs(targetURL) {
	tsWindow = openCenteredPopup('tsWindow', targetURL, 750, 500,
	                             'no', 'no', 'no', 'no', 'no', 'yes', 'yes');

	checkPopup(tsWindow, POPUP_TS, targetURL);
	focusOnWindow(tsWindow);
	return false;
}

// function that opens a popup with a Coco
var cocoWindow = '';
function openCoco(targetURL) {
	// coco window has scrollbars enabled
	cocoWindow = openCenteredPopup('cocoWindow', targetURL, 720, 540,
	                               'no', 'no', 'no', 'no', 'no', 'yes', 'yes');
	//checkPopup(cocoWindow,
	//           'La convention collective que vous avez demand&eacute; a &eacute;t&eacute; bloqu&eacute;e, pour la visualiser ',
	//           'reOpenCoco(\'' + targetURL + '\')');
	checkPopup(cocoWindow, POPUP_COCO, targetURL);
	focusOnWindow(cocoWindow);
	return false;
}

// function that displays the download manager into the loaderFrame, shows the
// loaderFrame and hides the flashFrame
function openDownloadManager(ticketsSemiColonSeparated, hasMailError) {
	if (isFramed()) {
		parent.openDownloadManager(ticketsSemiColonSeparated,hasMailError);
	}
	else {
		if (functionExists('getLoaderFrame')) {
			var loaderFrame = getLoaderFrame();
			if (loaderFrame) {
				var url = base + 'dlManager.do?tickets=' + ticketsSemiColonSeparated + '&mailError='+hasMailError;
				loaderFrame.location.href = url;
				showLoader();
			}
		}
	}
}

/******************************************************************************/
/*                             Navis commands                                 */
/******************************************************************************/
// function that opens a popup with the rerun
var rerunWindow = '';
// method called by FSCommand
// *** requires flashClientPopup*.js
// @param rerunId
function openRerun(rerunId) {
	var url = getFlashClientPopupURL(rerunId, 'R');
	openRerunURL(url);
}
// *** requires flashClientPopup*.js
// method used by popupOpener
function openRerunURL(targetURL) {
	rerunWindow = openClientSub(targetURL);
	checkPopupDiv(rerunWindow, POPUP_RERUN, targetURL);
	focusOnWindow(rerunWindow);
	return false;
}

// function that opens the details of a pointer for print
var pointerDetailsWindow = '';
// method called by FSCommand
// @param rerunId
function openPointerDetails(rerunId) {
	var url = base + 'desk/actes_realises/imprimer_details.do?rerunId=' + rerunId;
	openPointerDetailsURL(url);
}

// method called by FSCommand
// @param rerunId
function openPointerDetailsFromNemo(rerunId) {
	var url = base + 'actes_realises/imprimer_details.do?rerunId=' + rerunId;
	openPointerDetailsURL(url);
}
// method used by popupOpener
function openPointerDetailsURL(targetURL) {
	pointerDetailsWindow = openCenteredPopup('pointerDetailsWindow', targetURL, 750, 500,
	                                         'no', 'no', 'no', 'no', 'no', 'yes', 'yes');
	checkPopupDiv(pointerDetailsWindow, POPUP_POINTER_DETAILS, targetURL);
	focusOnWindow(pointerDetailsWindow);
	return false;
}

// method called by FSCommand
// *** requires flashClientPopup*.js
function openLastTempRerun() {
	var url = base + 'desk/actes_realises/derniere_sauvegarde_automatique.do';
	openLastTempRerunURL(url);
}
// *** requires flashClientPopup*.js
// method used by popupOpener
function openLastTempRerunURL(targetURL) {
	rerunWindow = openClientSub(targetURL);
	checkPopupDiv(rerunWindow, POPUP_LAST_TEMP_RERUN, targetURL);
	focusOnWindow(rerunWindow);
	return false;
}

// method called by FSCommand
// *** requires flashClientPopup*.js
function openRelatedAct(args) {
	openRelatedActURL(getFlashClientPopupURL(args,'RA'));
}
// *** requires flashClientPopup*.js
// method used by popupOpener
function openRelatedActURL(targetURL) {
	rerunWindow = openClientSub(targetURL);
	checkPopupDiv(rerunWindow, POPUP_RERUN, targetURL);
	focusOnWindow(rerunWindow);
	return false;
}

// method called by FSCommand
// *** requires flashClientPopup*.js
function openNodeInRerunGroup(args) {
	openNodeInRerunGroupURL(getFlashClientPopupURL(args,'NG'));
}
// *** requires flashClientPopup*.js
// method used by popupOpener
function openNodeInRerunGroupURL(targetURL) {
	rerunWindow = openClientSub(targetURL);
	checkPopupDiv(rerunWindow, POPUP_RERUN, targetURL);
	focusOnWindow(rerunWindow);
	return false;
}

// method called by FSCommand
// *** requires flashClientPopup*.js
// @param userProfileId
function startModel(userProfileId) {
//	alert('startModel(' + userProfileId + ') js called');
  var url = getFlashClientPopupURL(userProfileId, 'M');
	startModelURL(url);
}
// *** requires flashClientPopup*.js
// method used by popupOpener
function startModelURL(targetURL) {
	rerunWindow = openClientSub(targetURL);
	checkPopupDiv(rerunWindow, POPUP_RERUN, targetURL);
	focusOnWindow(rerunWindow);
	return false;
}

// method called by FSCommand
// *** requires flashClientPopup*.js
// @param userProfileId
function editModel(userProfileId) {
//	alert('editModel(' + userProfileId + ') js called');
  var url = getFlashClientPopupURL(userProfileId, 'EM');
	editModelURL(url);
}
// *** requires flashClientPopup*.js
// method used by popupOpener
function editModelURL(targetURL) {
	rerunWindow = openClientSub(targetURL);
	checkPopupDiv(rerunWindow, POPUP_RERUN, targetURL);
	focusOnWindow(rerunWindow);
	return false;
}
/******************************************************************************/
/*                        PopupOpener frame                                   */
/******************************************************************************/
var POPUP_AR = 0;
var POPUP_COCO = 1;
var POPUP_TS = 2;

function reOpenPopup(popupCode, targetURL) {
	if (isFramed()) {
		parent.reOpenPopup(popupCode, targetURL);
	}
	else {
		// close the popupOpener first, seems to be ignored if called after the
		// popup is opened
		closePopupOpener();
		enableNavigationPrevent(false);
		if (popupCode == POPUP_AR) {
			openAr(targetURL);
		}
		else if (popupCode == POPUP_TS) {
			openTs(targetURL);
		}
		else if (popupCode == POPUP_COCO) {
			openCoco(targetURL);
		}
		else if (popupCode == POPUP_RERUN) {
			openRerunURL(targetURL);
		}
		else if (popupCode == POPUP_POINTER_DETAILS) {
			openPointerDetailsURL(targetURL);
		}
		enableNavigationPrevent(true);
	}
}

function goToAndShowPopupOpener(popupCode, targetURL) {
	if (isFramed()) {
		parent.goToAndShowPopupOpener(popupCode, targetURL);
	}
	else {
		if (functionExists('getLoaderFrame')) {
			var loaderFrame = getLoaderFrame();
			if (loaderFrame) {
				var url = base + 'popupOpener.do?popupCode=' + popupCode + '&url=' + escape(targetURL) + '';
				loaderFrame.location.href = url;
				showPopupOpener();
			}
		}
	}
}

function closePopupOpener() {
	if (isFramed()) {
		parent.closePopupOpener();
	}
	else {
		showFlash();
		resetLoader();
	}
}

// checks whether the specified popup window is opened or not, if not
// shows the popupOpener
function checkPopup(windowObject, popupCode, targetURL) {
	if (!isWindowOpen(windowObject)) {
		//alert('popupBlocker detected !');
		goToAndShowPopupOpener(popupCode, targetURL);
	}
}

/******************************************************************************/
/*                          PopupOpener div                                   */
/******************************************************************************/
var textPopupOpener1 = 'un bloqueur de popup a &eacute;t&eacute; d&eacute;tect&eacute;.<br />';
var linkContent = 'cliquez ici pour contourner le probl&egrave;me';
var textPopupOpener2 = '.'

// checks whether the specified popup window is opened or not, if not
// shows the popupOpener as a div
function checkPopupDiv(windowObject, popupCode, targetURL) {
	if (!isWindowOpen(windowObject)) {
		//alert('popupBlocker detected !');
		showPopupOpenerDiv(popupCode, targetURL);
	}
}

function showPopupOpenerDiv(popupCode, targetURL) {
	writeLayer('popupOpenerContent', getPopupOpenerDivContent(popupCode, targetURL));
	centerPopupDiv(POPUP_OPENER_ID,500,130);
	makeDivVisibleUsingDisplay(POPUP_OPENER_ID, true);
}

function closePopupOpenerDiv() {
	makeDivVisibleUsingDisplay(POPUP_OPENER_ID, false);
}

function getPopupOpenerDivContent(popupCode, targetURL) {
	return textPopupOpener1 + ' ' +
         getPopupOpenerDivLinkString(popupCode, targetURL, linkContent) +
         textPopupOpener2;
}

function getPopupOpenerDivLinkString(popupCode, targetURL, linkContent) {
	return '<a href="#" onclick="reOpenPopupDiv(\'' + popupCode + '\',\'' + targetURL + '\'); return false">' + linkContent + "</a>"
}

var POPUP_RERUN = 3;
var POPUP_POINTER_DETAILS = 4;
var POPUP_LAST_TEMP_RERUN = 5;

function reOpenPopupDiv(popupCode, targetURL) {
	closePopupOpenerDiv();
	if (popupCode == POPUP_RERUN) {
		openRerunURL(targetURL);
	}
	else if (popupCode == POPUP_POINTER_DETAILS) {
		openPointerDetailsURL(targetURL);
	}
	else if (popupCode == POPUP_LAST_TEMP_RERUN) {
		openLastTempRerunURL(targetURL);
	}
}

function centerPopupDiv(divName, w, h) {
	var viewPortWH = getViewPortWidthAndHeight();
	var scrollPosition = getScrollPosition();
	if (viewPortWH && scrollPosition) {
		var divX = (viewPortWH.width - w) / 2;
		var divY = (viewPortWH.height - h) / 2;
		moveDivTo(divName, divX + scrollPosition.x, divY + scrollPosition.y - 90);
	}
}


/******************************************************************************/
/*                             Ping method                                    */
/******************************************************************************/
function ping(timeout) {
	if (isFramed()) {
		pingInFrame(timeout);
	}
	else {
		pingInIFrame(timeout);
	}
}

var i;
function pingInIFrame(timeout) {
	var d = new Date();
	var u = 'ping.do?tms=' + d.getTime();
	i = frames['ping_iframe'];
	if (i) {
		i.location.href = u;
	}
	else {
		i =  document.getElementById('ping_iframe');
		if (i)
			i.src = u;
	}
	setTimeout('pingInIFrame(' + timeout + ');', timeout);
}
// sends a ping on the server to refresh the session by refreshing the page
// of the frame created for that
// (if method getPingFrame() does not exists in the top frame, this method has
//  no effect)
function pingInFrame(timeout) {
	if (isFramed()) {
		parent.pingInFrame(timeout);
	}
	else if (functionExists('getPingFrame')) {
		var d = new Date();
		var u = 'ping.do?tms=' + d.getTime();
		var frame = getPingFrame();
		if (frame) {
			frame.location.href = u;
		}
		setTimeout('pingInFrame(' + timeout + ');', timeout);
	}
}

/******************************************************************************/
/*                     Resize of the popup window                             */
/******************************************************************************/
function resizeWindow() {
	if (isFramed()) {
		parent.resizeWindow();
	}
	else {
		resizeWindowSub();
	}
}

function resizeWindowSub() {
	// calculates the margins of the window around the portView by setting the
	// window to a known size and mesuring the size of the portView
	var marginW;
	var marginH;
	if (document.all) {
	  window.resizeTo(MIN_WIDTH, MIN_HEIGHT);
	  marginW = MIN_WIDTH - document.body.offsetWidth;
	  marginH = MIN_HEIGHT - document.body.offsetHeight;
	}
	else if (window.outerHeight && window.outerWidth){
	  window.outerHeight = MIN_HEIGHT;
	  window.outerWidth = MIN_WIDTH;
	  marginW = MIN_WIDTH - window.innerWidth;
	  marginH = MIN_HEIGHT - window.innerHeight;
	}

	// the piece of code below maximizes the size of the popup
	var w = (screen.availWidth > MIN_WIDTH ? screen.availWidth : MIN_WIDTH);
	var h = (screen.availHeight > MIN_HEIGHT ? screen.availHeight : MIN_HEIGHT);
	// set condition below to true to maximize the window but keep the ratio
	if (false) {
	  h = Math.round((w - marginW) * MIN_HEIGHT / MIN_WIDTH) + marginH;
	}

	window.moveTo((screen.availWidth - w) / 2, (screen.availHeight - h) / 2);
	if (document.all) {
	  window.resizeTo(w, h);
	}
	else if (window.outerHeight && window.outerWidth && (window.outerHeight < h || window.outerWidth < w)){
	  window.outerHeight = h;
	  window.outerWidth = w;
	}
}
