function showPicto( picto )
{
	if ( picto != null && picto.length > 0 ) document.write( picto );
}

function clear(who){
 document.forms['data_form'].elements[who].value='';
 try
 {
	 setSpan( 'span_' + who, '');
 }catch(e){
 }
}
function setSpan( who, what )
{
	document.getElementById( who ).innerHTML = what;
}

function list_over(oObj){
	if (oObj.parentElement) oObj.parentElement.parentElement.bgColor = '#eeeeee';
	else return;
}
function list_out(oObj){
	if (oObj.parentElement) oObj.parentElement.parentElement.bgColor = '#FFFFFF';  
	else return;
}

function layerWrite(sLayer, sHTML){
	if (document.layers) {
		// N4x non supporté
    } else if (document.all) {
        document.all("" + sLayer).innerHTML = "" + sHTML;
    }
	else if(navigator.vendor == "Netscape6"){
		document.getElementById("" + sLayer).innerHTML = "" + sHTML;
	}
}

function layerDisplay(sLayer, sDisplay){
	document.getElementById("" + sLayer).style.display = "" + sDisplay;
}

function checkDateField(dVal){
	if (checkDate(dVal) != ''){
		alert(checkDate(dVal));
	}
}


/* GESTION DES COOKIES JAVASCRIPT */
function SetCookie(name, value)
{
         var argv = SetCookie.arguments;
         var argc = SetCookie.arguments.length;
         var expires = (argc > 2) ? argv[2] : null;
         var path = (argc > 3) ? argv[3] : null;
         var domain = (argc > 4) ? argv[4] : null;
         var secure = (argc > 5) ? argv[5] : false;
         document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}

function getCookieVal(offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1) endstr = document.cookie.length;
   return unescape (document.cookie.substring(offset, endstr));
}
function GetCookie(name) {
   var arg = name+"=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen) {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) break;
   }
   return null;
}

/* GESTION DES COOKIES JAVASCRIPT */



/*GESTION DES ALERTES DE SAISIES*/
var editInterval = null;

function initializeCheck() {
	editInterval = window.setInterval(checkAll, 300)
}

function clearCheck() {
	window.clearInterval(editInterval);
}

function checkAll()
{
	clearCheck();
	try {
		checkFormAll(document.forms["data_form"]);
	} catch (ex) {
	}
	initializeCheck();
}

function checkFormAll(objectForm) {
	for (var i = 0; i < objectForm.elements.length ; i++) {
		var obj = objectForm.elements[i];
		var stype = obj.type;
		var ssubtype = obj.subtype;
		
		//alert("id = " + obj.id + " - stype=" + stype + " ssubtype=" + ssubtype);
		
		if ( stype == 'select-one') {
			temoigne( objectForm, obj.id, 'child', obj.options[obj.selectedIndex].value );
		} else if ( stype == "hidden" || 
					stype == "text" || 
					stype == "textarea" || 
					stype == "password"
					)
		{
		if (ssubtype == "child") {
				temoigne( objectForm, obj.id, 'child', obj.value );
			} else {
				temoigne( objectForm, obj.id, 'string', obj.value );
			}
		}
	}
}

function temoigne(objectForm, sField, sNohetoType, sValue)
{
	try
	{
		var sType = "";
		if ( sNohetoType != "dater" && sNohetoType != "child") {
			sType = "string";
		}else{
			sType = sNohetoType;
		}
		

		var bFilled = false;
		if (sType == 'child')
		{
			//alert(sValue);
			if ( Trim(sValue) != "0")
			{
				bFilled = true;
			}
		}else if( sType == 'dater' ){
			if ( Trim(sValue).length > 0)
			{
				if ( checkDate(Trim(sValue)).length == 0){
				 bFilled = true;
				}else{
					objectForm[sField].select();
					checkDateField(Trim(sValue));
				}
			}
		}else if( sType == 'string' ){
			if ( Trim(sValue).length > 0)
			{
				if ( Trim(sValue) != "<P>&nbsp;</P>")
				{
					bFilled = true;	
				}
			}
		}
		
		// On fait la couleur !
		if ( bFilled ) {
			changeTemoin(sField, 'verte');
		}else{
			changeTemoin(sField, 'rouge');
		}
			
	}catch(ex){
		//alert(sField + " " + ex.message);	
	}
}


var lstTemoin = new Array();



function makeTemoin(sFieldName) {
	//alert("make temoin " + sFieldName );
	//var s = "<img id='temoin_" + sFieldName + "' name='temoin_" + sFieldName + "' src='model/__roles__/ressourceschartes/img/puce_rouge.gif' style='display:inline'>";
	var s = "<img id='temoin_" + sFieldName + "' name='temoin_" + sFieldName + "' src='pipo' onError='javascript:referenceTemoin(this);' style='display:inline'>";
	document.write(s);
}

function referenceTemoin( srcObject ) {
	//alert("referenceTemoin " + srcObject);
	lstTemoin[lstTemoin.length]=srcObject;
	srcObject.src = 'model/__roles__/ressourceschartes/img/puce_rouge.gif';
}

function changeTemoin( sFieldName, sCouleur ) {
	for(var i=0; i<lstTemoin.length; i++) {
		var srcObject = lstTemoin[i];
		if (srcObject.id == 'temoin_' + sFieldName) {
			var newname = srcObject.id + "_" + sCouleur;
			if (newname !=srcObject.name) {
				//alert("chgt " + newsrc + " " + srcObject.src);
				var newsrc = 'model/__roles__/ressourceschartes/img/puce_' + sCouleur + '.gif';
				srcObject.src = newsrc;
				srcObject.name = srcObject.id + "_" + sCouleur;
			}
		}
	}
}

/*GESTION DES ALERTES DE SAISIES*/

/* FONCTIONS DE GESTION DU BO */

function orderLight( frm, nature, parents , redirect )
{
	var criteres = "";
	frm.action = "ebn.ebn?form_action=data_customactions&action=orderlight&form_object=" + nature;
	if ( parents.length == 0 )
	{
		criteres = "pparent = '' or pparent is null";
	}else{
		criteres = "UPPER(pparent)=UPPER('" + parents + "')";
	}
	frm.elements["parents"].value = parents;
	frm.elements["criteres"].value = criteres;
	frm.elements["orderon"].value = "position";
	
	frm.submit();
}


/* calendar */


function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    // if we add this call we close the calendar on single-click.
    // just to exemplify both cases, we are using this only for the 1st
    // and the 3rd field, while 2nd and 4th will still require double-click.
    cal.callCloseHandler();
}

function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  calendar = null;
}

function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (calendar != null) {
    // we already have some calendar created
    calendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(true, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    calendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  calendar.setDateFormat(format);    // set the specified date format
  calendar.parseDate(el.value);      // try to parse the text in field
  calendar.sel = el;                 // inform it what input field we use
  

  
  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  //calendar.showAtElement(el.nextSibling, "Br");        // show the calendar
  calendar.showAtElement(el, "Br");        // show the calendar
  

  return false;
}

/* calendar */

function preparePopUp( wname, width, height, url )
{
	if (typeof(url)=='undefined') url = 'about:blank';
	var left = (screen.availWidth - width )/2;
	var top = (screen.availHeight - height )/2;
	return w = window.open(url, wname, 'width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + ', location=no, menubar=no, status=yes, toolbar=no, scrollbars=yes, resizable=yes');
}


function displayMessage( msg )
{
	if ( msg.indexOf('wsnoheto.engine.CTObjectLock$AlreadyLockedException' ) > -1)
	{
		msg = "<center><span style='font-weight:bold;color:red'>";
		msg += getRes( 'charte_contributeur_AlreadyLockedException' );
		msg += "</span></center>";
		document.write( msg );	
	}
}


function placerModules( fld , stay , layout , charte , nbrElts )
{
	if ( typeof( layout ) == 'undefined' ) var layout = 0;
	if ( typeof( stay ) == 'undefined' ) var stay = false;
	if ( typeof( charte ) == 'undefined' ) var charte = document.forms['data_form'].elements['charte'].value;
	if ( typeof( nbrElts ) == 'undefined' ) var nbrElts = 0;
	
	if ( layout == 0 && document.forms['data_form'].elements[fld].value.indexOf( ":" ) > -1 )
	{
		layout = document.forms['data_form'].elements[fld].value.substr( 0, document.forms['data_form'].elements[fld].value.indexOf( ":" ) );
	}
	if ( charte != '0' )
	{
		if ( document.forms['data_form'].elements[fld].value.length == 0 )
		{
			if ( nbrElts > 0 )
			{
				var str = layout + ":";
				for ( var i = 1 ; i <= nbrElts; i++ )
				{
					str += "0_0";
					if ( i < nbrElts ) str += ",";
				}	
				document.forms['data_form'].elements[fld].value = str;
			}
		}
		var url = "ebn.ebn?form_action=data_customactions&action=placemodules&nodisplay=1&field="+ fld + "&charte=" + charte + "&placement=" + document.forms['data_form'].elements[fld].value + "&layout=" + layout;
		
		if ( stay )
		{
			location.href = url ;
		}else{
			preparePopUp( 'placerModules', screen.availWidth, screen.availHeight, url );
			//window.open( url );	
		}
	}else{
		alert( 'Veuillez choisir une charte à appliquer' );	
	}
}