function checkSearch(theForm) {
	if(theForm.Query.value.length == 0) {
		alert('La recherche est vide !');
		theForm.Query.focus();
		return false;
	}
	if(theForm.Query.value.length == 1 || theForm.Query.value.length == 2) {
		alert('La recherche minimale est de trois caractères.');
		theForm.Query.focus();
		return false;
	}
	MWJ_changePosition( 'DragableLayer_searchWarning',200,MWJ_getPosition(MWJ_findObj('anchorSearch'))[1]-125,true );MWJ_changeVisibility('DragableLayer_searchWarning',true);
}
function checkEmailForm(theForm) {
	if (theForm.subject.value == '') {
		alert('Le sujet est vide');
		theForm.subject.focus();
		return false;
	}
	if (theForm.message.value == '') {
		alert('Le message est vide!');
		theForm.message.focus();
		return false;
	} else {
		theForm.submitButton.disabled=true;
		theForm.submitButton.value='En cours';
		theForm.submit();
	}	
}
function swapImgTo(name,path) {
	window.document.images[name].src = path;
}
function decodeAscii(original) {
	var result="";
	arrayofstring=original.split('-');
	for (var i=0; i <arrayofstring.length; i++) {
		result=result+String.fromCharCode(arrayofstring[i]-9);
	}
	return result;
} 
function open_print(){
	window.open ('print.jsp', 'print', 'left=20,top=50,width=700,height=600,scrollbars=yes,resizable=0,status=no,menubar=yes,directories=np');
	return false;
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function extractSilverpeasTag( ctrl ) {
	strResult = null;
	for( var x = 0; x < ctrl.attributes.length; x++ ) {
		if( ctrl.attributes[x].nodeName.toLowerCase() == 'silverpeas_form' ) {
			strResult = ctrl.attributes[x].nodeValue;
		}
	}
	return strResult;
}
function isMandatory( strParam ) {
	strParamTrue  = "SF_mandatory='true'";
	strParamFalse = "SF_mandatory='false'";
	flgResult = false;
	flgResult = (strParam.indexOf(strParamTrue) >= 0);
	return flgResult;
}
function _CF_onError(form_object, input_object, object_value, error_message)
{
	alert(error_message);
	return false;	
}
function _CF_onErrorAlert(input_array)
{
	var strMessage = new String;
	if(input_array.length > 0) {
		for(var i=0; i<input_array.length; i++)
			strMessage = strMessage + input_array[i] + '\n';
		alert(strMessage);
	}
}
function _CF_hasValue(obj, obj_type)
{
	if (obj_type == "TEXT" || obj_type == "TEXTAREA" || obj_type == "PASSWORD")
	{
		if (obj.value.length == 0) 
	  		return false;
		else 
	  		return true;
	}
	else if (obj_type == "SELECT")
	{
		for (i=0; i < obj.length; i++)
		{
			if (obj.options[i].selected)
				return true;
		}
	   	return false;	
	}
	else if (obj_type == "SINGLE_VALUE_RADIO" || obj_type == "SINGLE_VALUE_CHECKBOX")
	{
		if (obj.checked)
			return true;
		else
	   		return false;	
	}
	else if (obj_type == "RADIO" || obj_type == "CHECKBOX")
	{
		for (i=0; i < obj.length; i++)
		{
			if (obj[i].checked)
				return true;
		}
	   	return false;	
	}
}

// -- Standard functions

// Array.concat() - Join two arrays
if( typeof Array.prototype.concat==='undefined' ) {
 Array.prototype.concat = function( a ) {
  for( var i = 0, b = this.copy(); i<a.length; i++ ) {
   b[b.length] = a[i];
  }
  return b;
  };
}

// Array.copy() - Copy an array
if( typeof Array.prototype.copy==='undefined' ) {
 Array.prototype.copy = function() {
  var a = [], i = this.length;
  while( i-- ) {
   a[i] = typeof this[i].copy!=='undefined' ? this[i].copy() : this[i];
  }
  return a;
 };
}

// Array.pop() - Remove and return the last element of an array
if( typeof Array.prototype.pop==='undefined' ) {
 Array.prototype.pop = function() {
  var b = this[this.length-1];
  this.length--;
  return b;
 };
}

// Array.push() - Add an element to the end of an array, return the new length
if( typeof Array.prototype.push==='undefined' ) {
 Array.prototype.push = function() {
  for( var i = 0, b = this.length, a = arguments, l = a.length; i<l; i++ ) {
   this[b+i] = a[i];
  }
  return this.length;
 };
}

// Array.shift() - Remove and return the first element
if( typeof Array.prototype.shift==='undefined' ) {
 Array.prototype.shift = function() {
  for( var i = 0, b = this[0], l = this.length-1; i<l; i++ ) {
   this[i] = this[i+1];
  }
  this.length--;
  return b;
 };
}

// Array.slice() - Copy and return several elements
if( typeof Array.prototype.slice==='undefined' ) {
 Array.prototype.slice = function( a, c ) {
  var i, l = this.length, r = [];
  if( !c ) { c = l; }
  if( c<0 ) { c = l + c; }
  if( a<0 ) { a = l - a; }
  if( c<a ) { i = a; a = c; c = i; }
  for( i = 0; i < c - a; i++ ) { r[i] = this[a+i]; }
  return r;
 };
}

// Array.splice() - Remove or replace several elements and return any deleted elements
if( typeof Array.prototype.splice==='undefined' ) {
 Array.prototype.splice = function( a, c ) {
  var i = 0, e = arguments, d = this.copy(), f = a, l = this.length;
  if( !c ) { c = l - a; }
  for( i; i < e.length - 2; i++ ) { this[a + i] = e[i + 2]; }
  for( a; a < l - c; a++ ) { this[a + e.length - 2] = d[a - c]; }
  this.length -= c - e.length + 2;
  return d.slice( f, f + c );
 };
}

// Array.unshift() - Add an element to the beginning of an array
if( typeof Array.prototype.unshift==='undefined' ) {
 Array.prototype.unshift = function() {
  this.reverse();
  var a = arguments, i = a.length;
  while(i--) { this.push(a[i]); }
  this.reverse();
  return this.length;
 };
}

// -- 4umi additional functions

// Array.forEach( function ) - Apply a function to each element
Array.prototype.forEach = function( f ) {
 var i = this.length, j, l = this.length;
 for( i=0; i<l; i++ ) { if( ( j = this[i] ) ) { f( j ); } }
};

// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function( v, b, s ) {
 for( var i = +b || 0, l = this.length; i < l; i++ ) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.insert( index, value ) - Insert value at index, without overwriting existing keys
Array.prototype.insert = function( i, v ) {
 if( i>=0 ) {
  var a = this.slice(), b = a.splice( i );
  a[i] = v;
  return a.concat( b );
 }
};

// Array.lastIndexOf( value, begin, strict ) - Return index of the last element that matches value
Array.prototype.lastIndexOf = function( v, b, s ) {
 b = +b || 0;
 var i = this.length; while(i-->b) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.random( range ) - Return a random element, optionally up to or from range
Array.prototype.random = function( r ) {
 var i = 0, l = this.length;
 if( !r ) { r = this.length; }
 else if( r > 0 ) { r = r % l; }
 else { i = r; r = l + r % l; }
 return this[ Math.floor( r * Math.random() - i ) ];
};

// Array.shuffle( deep ) - Randomly interchange elements
Array.prototype.shuffle = function( b ) {
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};

// Array.unique( strict ) - Remove duplicate values
Array.prototype.unique = function( b ) {
 var a = [], i, l = this.length;
 for( i=0; i<l; i++ ) {
  if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
 }
 return a;
};

// Array.walk() - Change each value according to a callback function
Array.prototype.walk = function( f ) {
 var a = [], i = this.length;
 while(i--) { a.push( f( this[i] ) ); }
 return a.reverse();
};


function validateForm( _CF_this )
{
	// alert( _CF_this.name );
	var aEls = _CF_this.elements;
	var nNbre = aEls.length;
	var aFldName = new Array();
	var aFldType = new Array();
	var aFldOrdered = new Array();
	
	var fld = null;
	
	_CF_this.__orderedFormFieldsNameList.value = '';
	for (var i=0; i<nNbre; i++) 
	{
		//Liste et ordre des champs du formulaire.
		if ( aEls[i].type != 'hidden' && aEls[i].type != 'submit' ) {
			aFldOrdered[aFldOrdered.length] = aEls[i].name;
		}
		//alert('Field Name: '+aEls[i].name+'\nField Type: '+aEls[i].type+'\nField Value: '+aEls[i].value);
		if ( aEls[i].type != 'radio' ) {
			fld = _CF_this[aEls[i].name];
			if ( strParam = extractSilverpeasTag(fld)) {
				if (isMandatory(strParam)){
					aFldName[aFldName.length] = aEls[i].name;
					aFldType[aFldType.length] = aEls[i].type;
				}
				// alert( fld.name + '\n' + isMandatory(strParam) );
			}
		} else {
			// alert( _CF_this.elements[aEls[i].name].length );
			fld = _CF_this.elements[aEls[i].name][0];
			/*
			if ( strParam = extractSilverpeasTag(fld))
				alert( fld.name + '\n' + isMandatory(strParam) );
			*/
		}
	}
	
	//remove the duplicate fields
	//aFldOrdered = aFldOrdered.unique();
	
    //reset on submit
	_CF_this.__orderedFormFieldsNameList.value = aFldOrdered.unique().join();
    _CF_error_exists = false;
    _CF_error_messages = new Array();
    _CF_error_fields = new Object();
    _CF_FirstErrorField = null;
    _CF_error_messages[0] = "Informations requises : ";
	for (var i=0; i<aFldName.length; i++) {
		
        if( !_CF_hasValue(_CF_this[aFldName[i]], aFldType[i].toUpperCase(), false ) )
        {
            // alert(aFldType[i] + ":" + _CF_this[aFldName[i]].value + "=" + _CF_this[aFldName[i]].value.length );
            //_CF_onError(_CF_this, aFldName[i], _CF_this[aFldName[i]].value, "Cette information est requise : " + aFldName[i]);
            _CF_error_messages[_CF_error_messages.length] = " - " + aFldName[i];
            _CF_error_exists    = true;
            if (!_CF_FirstErrorField) _CF_FirstErrorField = aFldName[i];
        }
	}

    //display error messages and return success
    if( _CF_error_exists )
    {
    	//alert(_CF_error_messages.length);
        if( _CF_error_messages.length > 0 )
        {
            // show alert() message
            _CF_onErrorAlert(_CF_error_messages);
            // set focus to first form error, if the field supports js focus().
            //alert(_CF_this[_CF_FirstErrorField].type.toLowerCase());
            if( _CF_this[_CF_FirstErrorField].type.toLowerCase() == "text" || _CF_this[_CF_FirstErrorField].type.toLowerCase() == "textarea")
            { _CF_this[_CF_FirstErrorField].focus(); }

        }
        return false;
    }else {
        return true;
    }
}
