/**
 * $Id: help.js,v 1.1 2007/02/13 12:35:33 p551291 Exp $
 *
 */
var popu;
var wHelp;	// Contient la fenêtre d'aide contextuelle


function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

/**
 * Popup code
 */
function openpopup(fieldToFill, openerFields, fieldsAlias,outputFieldsNames, popup, width, height, e) {
	var xx, yy;
	xx = e.screenX;
	yy = e.screenY;
    var RETURN_OUTPUTFIELDS_NAMES = "outputfieldsnames"; // NOT USED YET
    var RETURN_FIELDSNAMES = "returnFieldName"
	var outputfields;
	
	if (outputFieldsNames!=null && outputFieldsNames!=""){
		outputfields = outputFieldsNames;
	}else{
		outputfields = fieldToFill;
	}

	var url = popup+"?"+RETURN_FIELDSNAMES+"="+outputfields+"&";
	if ((openerFields!=null) && (openerFields.length>0))
	{
		var tabInput = openerFields.split(',');
		var tabAlias = fieldsAlias.split(',');
	
		if ( (tabInput.length != tabAlias.length) || ( (tabInput.length == tabAlias.length) && (trim(tabAlias[0]).length==0) ) )
		{
		tabAlias = openerFields.split(',');
		}

		 var l = tabInput.length;
		 var nbForms = window.document.forms.length;
	
		for (var i=0;i<l;i++){
	  		var inputField = null;
	  		var inputName = trim(tabInput[i]);
	  		var targetNb = -1;
			var inputNameSplited = inputName.split('[');

			if (inputNameSplited.length>1)
				{
					inputName = trim(inputNameSplited[0]);
					targetNb = trim(inputNameSplited[1].split(']')[0]);
					
					for (var j=0;j< nbForms; j++){
			 		inputField = window.document.forms[j][trim(inputName)][trim(targetNb)];
	 	 	 		if (inputField != null){
			  			break;
			 			}
		 	 		}
				}
				else
				{
					for (var j=0;j< nbForms; j++){
			 		inputField = window.document.forms[j][trim(inputName)];
	 	 	 		if (inputField != null){
	 	 	 			//alert(inputName+" = "+(inputField.value==null?"":inputField.value));
			  			break;
			 			}
		 	 		}
				}
			
			 if (inputField != null) {
				 url += trim(tabAlias[i])+"="+(inputField.value==null?"":inputField.value);
				 //alert("|"+trim(tabAlias[i])+"="+(inputField.value==null?"":inputField.value)+"|");
				 if (i!=(l-1)) 
				 {
			 		url+="&";
				 }
				 else
				 {// alway add a & char at the end of the url,
				  // because if the last value is finished by space char,
				  // these space char is not retrieve with the request.getParameter()
				 	url+="&"
				 }
			 } else {	
	    	 	alert("FieldName : '"+trim(tabInput[i])+"' not found in HTML Form !");
	     	}
	     }
	     //alert("url : "+url);
	}
	
	// popu = window.open(url, 'popup', 'directories=0, location=0, menubar=0, status=1, toolbar=0, scrollbars=1, resizable=1, width=' + width + ', height=' + height + ', top=' + yy + ', left=' + xx); 	
	
	var args = ''; // 'directories=0, location=0, menubar=0, status=1, toolbar=1, scrollbars=1, resizable=1';
	var returnFunc ='';
	//, width=' + width + ', height=' + height + ', top=' + yy + ', left=' + xx
	//	popu = window.open(url, 'popup', args); 	
	 openDGDialog(url, width, height, returnFunc, args)
	
	//checkModal();		
	//var sfeatures = "dialogWidth: "+ width + "px; dialogHeight:" + height + "px;scroll=no;";
	//window.showModalDialog(url, "", sfeatures); 	


}

/**
 * Ferme la popup et remplie la valeur de l'opener
 *
 * @param openerField nom du champ à completer
 * @param value valeur du champs à completer
 * @param value Valeur du champ de Description à completer
 */
function closepopup(outputFields, values) {
/*
alert(outputFields);
alert(values);
*/
 	var nbForms = window.opener.document.forms.length;
	var tabOutputs = outputFields.split(','); 	
	var tabValues = values.split('#|#');
    var outputField;
    var outputNameSplited;
	var nbOutputs =  tabOutputs.length;
	var nbValues =  tabValues.length;
	if(nbOutputs != nbValues){
		alert('Error in number arguments');
	}else{
		for (var i = 0; i< nbOutputs; i++){
			for (var j=0;j< nbForms; j++){
				var outputValue = tabValues[i];
				var outputName  = trim(tabOutputs[i]);

				outputNameSplited = outputName.split('[');
				if (outputNameSplited.length>1)
				{
					outputName = trim(outputNameSplited[0]);
					var targetNb = trim(outputNameSplited[1].split(']')[0]);
					outputField = window.opener.document.forms[j][outputName][targetNb];
					if (outputField != null){
						outputField.value = outputValue;
						}
				}
				else
				{
					outputField = window.opener.document.forms[j][outputName];
					if (outputField != null){
						outputField.value = outputValue;
						}
				}
			}
		}
	}
	window.close();
}

/**
 * list all parameters from the form
 */
function submitHelpParameterForm(path){

	var value='';

 	var elements = window.document.forms[0].elements;
 	for (i=0; i < elements.length; i++){
		var element = elements[i];
		value='&'+element.name+'='+element.value;
    }
    var url = webContext+path+'?param=true'+value;
	window.document.forms[0].action=url;
	window.document.forms[0].submit();
}

function openHelp( helpURL )
{
	wHelp = window.open( helpURL,'MeowHelpWindow', 'directories=0, location=0, menubar=0, status=1, toolbar=0, resizable=1, scrollbars=1, width=500, height=350');
	wHelp.focus();
}

function closeHelp()
{
	if( wHelp != null )
	{
		wHelp.close();
		wHelp = null;
	}
}

//*******************************************************************************

// Global for brower version branching.
var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4))

// One object tracks the current modal dialog opened from this window.
var dialogWin = new Object()


// Generate a modal dialog.
// Parameters:
//    url -- URL of the page/frameset to be loaded into dialog
//    width -- pixel width of the dialog window
//    height -- pixel height of the dialog window
//    returnFunc -- reference to the function (on this page)
//                  that is to act on the data returned from the dialog
//    args -- [optional] any data you need to pass to the dialog
function openDGDialog(url, width, height, returnFunc, args) {
	if (!dialogWin.win || (dialogWin.win && dialogWin.win.closed)) {
		// Initialize properties of the modal dialog object.
		dialogWin.returnFunc = returnFunc
		dialogWin.returnedValue = ""
		dialogWin.args = args
		dialogWin.url = url	
		dialogWin.width = width
		dialogWin.height = height
		// Keep name unique so Navigator doesn't overwrite an existing dialog.
		dialogWin.name = (new Date()).getSeconds().toString()
		// Assemble window attributes and try to center the dialog.
		if (Nav4) {
			// Center on the main window.
			dialogWin.left = window.screenX + 
			   ((window.outerWidth - dialogWin.width) / 2)
			dialogWin.top = window.screenY + 
			   ((window.outerHeight - dialogWin.height) / 2)
			var attr = "screenX=" + dialogWin.left + 
			   ",screenY=" + dialogWin.top + ",resizable=no,width=" + 
			   dialogWin.width + ",height=" + dialogWin.height
		} else {
			// The best we can do is center in screen.
			dialogWin.left = (screen.width - dialogWin.width) / 2
			dialogWin.top = (screen.height - dialogWin.height) / 2
			var attr = "left=" + dialogWin.left + ",top=" + 
			   dialogWin.top + ",resizable=1,width=" + dialogWin.width + 
			   ",height=" + dialogWin.height+",directories=0, location=0, menubar=0, status=1, toolbar=0, scrollbars=1"
		}
		
		// Generate the dialog and make sure it has focus.
		dialogWin.win=window.open(dialogWin.url, dialogWin.name, attr);
		//window.open(url, 'popup', 'directories=0, location=0, menubar=0, status=1, toolbar=1, scrollbars=1, resizable=1, width=' + width + ', height=' + height + ', top=' + yy + ', left=' + xx);
		//dialogWin.win=window.open(dialogWin.url, dialogWin.name, attr)
		dialogWin.win.focus()
	} else {
		dialogWin.win.focus()
	}
}

// Event handler to inhibit Navigator form element 
// and IE link activity when dialog window is active.
function deadend() {
	if (dialogWin.win && !dialogWin.win.closed) {
		dialogWin.win.focus()
		return false
	}
}

// Since links in IE4 cannot be disabled, preserve 
// IE link onclick event handlers while they're "disabled."
// Restore when re-enabling the main window.
var IELinkClicks

// Disable form elements and links in all frames for IE.
function disableForms() {
	IELinkClicks = new Array()
	for (var h = 0; h < frames.length; h++) {
		for (var i = 0; i < frames[h].document.forms.length; i++) {
			for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
				frames[h].document.forms[i].elements[j].disabled = true
			}
		}
		IELinkClicks[h] = new Array()
		for (i = 0; i < frames[h].document.links.length; i++) {
			IELinkClicks[h][i] = frames[h].document.links[i].onclick
			frames[h].document.links[i].onclick = deadend
		}
		frames[h].window.onfocus = checkModal
    	frames[h].document.onclick = checkModal
	}
}

// Restore IE form elements and links to normal behavior.
function enableForms() {
	for (var h = 0; h < frames.length; h++) {
		for (var i = 0; i < frames[h].document.forms.length; i++) {
			for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
				frames[h].document.forms[i].elements[j].disabled = false
			}
		}
		for (i = 0; i < frames[h].document.links.length; i++) {
			frames[h].document.links[i].onclick = IELinkClicks[h][i]
		}
	}
}

// Grab all Navigator events that might get through to form
// elements while dialog is open. For IE, disable form elements.
function blockEvents() {
	if (Nav4) {
		window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
		window.onclick = deadend
	} else {
		disableForms()
	}
	window.onfocus = checkModal
}
// As dialog closes, restore the main window's original
// event mechanisms.
function unblockEvents() {
	if (Nav4) {
		window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
		window.onclick = null
		window.onfocus = null
	} else {
		enableForms()
	}
}

// Invoked by onFocus event handler of EVERY frame,
// return focus to dialog window if it's open.
function checkModal() {
	setTimeout("finishChecking()", 50)
	return true
}

function finishChecking() {
	if (dialogWin.win && !dialogWin.win.closed) {
		dialogWin.win.focus() 
	}
}
//**************************
//  END MODAL DIALOG CODE
//**************************/



// Function to run upon closing the dialog with "OK".
function setPrefs() {
	// We're just displaying the returned value in a text box.
	document.returned.searchURL.value = dialogWin.returnedValue
}

// Called by "Apply Settings" button to reload the page with
// search string data appended to URL.
function applySettings() {
	if (document.returned.searchURL.value) {
		location.href = "main.html" + document.returned.searchURL.value
	}
}

//var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 4))

// Close the dialog
function closeme() {
	window.close()
}

// Handle click of OK button
function handleOK() {
	if (opener && !opener.closed) {
		top.dlogBody.transferData()
		opener.dialogWin.returnFunc()
	} else {
		alert("You have closed the main window.\n\nNo action will be taken on the choices in this dialog box.")
	}
	closeme()
	return false
}

// Handle click of Cancel button
function handleCancel() {
	closeme()
	return false
}