function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
       if (oldonload) {
         oldonload();
       }
       func();
     }
   }
   //alert(typeof window.onload);
}

function addResizeEvent(func) {
  var oldonresize = window.onresize;
  if (typeof window.onresize != 'function') {
   window.onresize = func;
  } else {
    window.onresize = function() {
       if (oldonresize) {
         oldonresize();
       }
       func();
     }
   }
}

function isNumberKey(evt , text)
{
         var e = (evt) ? evt : window.event;
         var charCode = (e.which) ? e.which : e.keyCode;
         if (charCode > 31 && (charCode < 48 || charCode > 57)){
            if(text){
              alert(text);
            }
            return false;
         }
         return true;
}


function IntegerRandom(number){
  return Math.floor(Math.random()*number);  //number 8 -> 0 à 7
}


function	checknumeric(field)
{
        pattern = /^[1-9][0-9]*\.?[0-9]*$/;
        if(pattern.test(field.value)==false)
        {
                alert("Valeur incorrecte pour le champ numerique " + field.name);
                field.value = 0;
                return false;
        }
         return true;
}

function	checkint(field)
{
        pattern = /^[0-9]*$/;
        if(pattern.test(field.value)==false)
        {
                alert("Valeur incorrecte pour le champ entier " + field.name);
                field.value = 0;
                return false;
        }
         return true;
}


function	confirmURL (url , question , window)
{
        if(confirm(question))
        {
            if(window)
                window.location = url;
            else
                document.location = url;
        }
}


function trim(s)
{
   	// Remove leading spaces and carriage returns
   	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
   	 { s = s.substring(1,s.length); }

   	// Remove trailing spaces and carriage returns
        while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
   	 { s = s.substring(0,s.length-1); }

   	return s;
}

	// Test d'un champ obligatoire
function checkMandatory(field)
{
        checkMandatory(field,true);
}
	// Test d'un champ obligatoire
function checkMandatory(field , trimvalue)
{
        if(trimvalue){
          field.value =  trim(field.value);
        }
        if( field.value == '' )
        {
          return false;
        }
        else{
          return true;
        }
}


// Test de la validité d'un e-mail
function checkEmail(field)
{
	valeur = field.value
	if ((valeur.indexOf('@')!=-1)&& (valeur.indexOf('..')==-1)&&(valeur.indexOf(' ')==-1)&&(valeur.indexOf('.')!=-1))
	{
		return true;
	}
	else
		return false;
}

function validateStringLength(field, nb)
{
	if (field.value.length < nb)
	{
            alert("Le champ " + field.name + " doit comporter au moins " + nb + " caractères");
            field.select();
            return false;
	}
	return true;
}
// Test de la longueur d'une chaîne
function checkStringLength(field, nb)
{
	if (field.value.length < nb)
	{
		return false;
	}
	return true;
}

// Comparaison entre deux chaînes (deux mots de passe par exemple)
function compare(field1, field2)
{
	if( field1.value != field2.value )
	{
		return false;
	}
	else
		return true;
}

function pageWidth() {
  return window.innerWidth != null?
    window.innerWidth
    :
    document.documentElement && document.documentElement.clientWidth ?
      document.documentElement.clientWidth
      :document.body != null?
          document.body.clientWidth
          :
          null;
}



function pageHeight() {
 return window.innerHeight != null?
    window.innerHeight
    :
    document.documentElement && document.documentElement.clientHeight ?
        document.documentElement.clientHeight
        :
        document.body != null?
            document.body.clientHeight
            :
            null;
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function


function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      if(buttonGroup.value){
         return buttonGroup.value;
      } else {
         return "";
      }
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
}




