// ------------------------------------------------------------------ //
// Fonction javascript permettant d'effectuer le tri sur les colonnes //
// ------------------------------------------------------------------ //
function triTable (sortColumn, sortOrder, formName) {
  var form = document.forms[formName];
  form.target = "_self";
  form.sortColumn.value=sortColumn;
  form.sortOrder.value=sortOrder;
  form.submit();
}

function changePage (pageNumberVar, formName) {
  var form = document.forms[formName];
  form.target = "_self";
  form.pageNumber.value=pageNumberVar;
  form.submit();
}

function changePageGo (formName) {
  var form = document.forms[formName];
  var pageGo = form.goToPage.value;
  form.target = "_self";
  form.pageNumber.value=pageGo;
  form.submit();
}

// ------------------------------------------------------------------ //
// Fonctions javascript utilisable pour la validation de formulaire   //
// ------------------------------------------------------------------ //
function checkForPositiveInteger (string) {
  var re=/^\d+$/;
  if (re.test(string)) 
    return true;
  else
    return false;
}

function checkForExclusivePositiveInteger (string) {
  return ( checkForPositiveInteger(string) && (string > 0) );
}

function checkForPhoneNumber (string) {
  if (string.length < 4)
    return false;
  else {
    var re=/^(\d|\s)+$/;
    if (re.test(string)) 
      return true;
    else
      return false;
  }
}

function checkForCountryPhoneCode (string) {
  var re=/^((\+)?(\d){1,3}?)+$/;
  if (re.test(string)) 
    return true;
  else
    return false;  
}

function checkEmail (string) {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(string))
    return (true);
  return (false)
}

function checkDate (string) {
  var dateStr = string;
  if (string.length == 10) {
    dateStr = string.substring(0,4)+string.substring(5,7)+string.substring(8,10);
  }
  if (dateStr.length != 8)
    return false;
  else {
    var re=/^(\d|\s)+$/;
    if ( ! re.test(dateStr.substring(0,4))
      || ! re.test(dateStr.substring(4,6))
      || ! re.test(dateStr.substring(6,8))
        )
      return false;
    else
      return true;
  }
  
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isDate (day,month,year) {
// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 1 and 12, and the
// year in ccyy format.

    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    else
        return false
}

function trim (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

// ------------------------------------------------------------------ //
// Fonctions javascript utilisable pour les appels de pop-up          //
// ------------------------------------------------------------------ //

function openCountryPopUp (formName, fieldName, codeFieldName, hiddenFieldName) {
  var url = 'goToPopUpCountry.do?formName='+formName+'&fieldName='+fieldName+'&codeFieldName='+codeFieldName+'&hiddenFieldName='+hiddenFieldName;
  window.open(url, 'country', 'width=400, height=400, menubar=no,scrollbars=yes,statusbar=no,resizable=yes');
}

function openLanguagePopUp (formName, fieldName, codeFieldName, hiddenFieldName) {
  var url = 'goToPopUpLanguage.do?formName='+formName+'&fieldName='+fieldName+'&codeFieldName='+codeFieldName+'&hiddenFieldName='+hiddenFieldName;
  window.open(url, 'language', 'width=400, height=400, menubar=no,scrollbars=yes,statusbar=no,resizable=yes');
}

function openCompanyPopUp (formName, fieldName, hiddenFieldName) {
  openCompanyPopUpDef(formName, fieldName, hiddenFieldName, eval(formName+'.'+fieldName+'.value'));
}

function openCompanyPopUpDef (formName, fieldName, hiddenFieldName, defaultSearchValue) {
  var url = 'goToPopUpCompany.do?formName='+formName+'&fieldName='+fieldName+'&hiddenFieldName='+hiddenFieldName+'&defaultSearchValue='+defaultSearchValue;
  window.open(url, 'company', 'width=400, height=400, menubar=no,scrollbars=yes,statusbar=no,resizable=yes');
}

function openParticipantPopUp (formName, lastnameFieldName, firstnameFieldName, hiddenFieldName) {
  var url = 'goToPopUpParticipant.do?formName='+formName+'&lastnameFieldName='+lastnameFieldName+'&firstnameFieldName='+firstnameFieldName+'&hiddenFieldName='+hiddenFieldName;
  window.open(url, 'participant', 'width=400, height=400, menubar=no,scrollbars=yes,statusbar=no,resizable=yes');
}

function openWorkTypeHelpPopUp() {
  var url = 'goToPopUpWorkTypeHelp.do';
  window.open(url, 'workTypeHelp', 'width=400, height=400, menubar=no,scrollbars=yes,statusbar=no,resizable=yes');
}

function openWorkKindHelpPopUp() {
  var url = 'goToPopUpWorkKindHelp.do';
  window.open(url, 'workKindHelp', 'width=400, height=400, menubar=no,scrollbars=yes,statusbar=no,resizable=yes');

}

function openClientGroupPopUp (formName, fieldName, hiddenFieldName) {
  var url = 'goToPopUpClientGroup.do?formName='+formName+'&fieldName='+fieldName+'&hiddenFieldName='+hiddenFieldName;
  window.open(url, 'clientGroup', 'width=400, height=400, menubar=no,scrollbars=yes,statusbar=no,resizable=yes');
}


// ------------------------------------------------------------------ //
// Fonctions javascript utilisable pour la gestion des Isans          //
// ------------------------------------------------------------------ //
function toHexString  (cypher) {
  if (cypher >= 0 && cypher <= 9)
    return cypher;
  else if (cypher == 10)
    return 'A';
  else if (cypher == 11)
    return 'B';
  else if (cypher == 12)
    return 'C';
  else if (cypher == 13)
    return 'D';
  else if (cypher == 14)
    return 'E';
  else if (cypher == 15)
    return 'F';
  else return -1;
}

function getCheckDigit (isanNumber) {
  var modulo = 36;
  var length = 16;
  var conversionTable = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0';
  var P = modulo;
  if (isanNumber.length != 17)
    length = 25;
  for (var i=0; i<length; i++) {
    if ( (length == 25 && i != 16) || (length == 16) ) {
      P = P%(modulo+1) + parseInt(isanNumber.charAt(i),16);
      if (P%modulo == 0 ) P = modulo*2;
      else P = (P%modulo)*2;
    }
  }
  return conversionTable.charAt(modulo+1-P%(modulo+1));
}

function validateIsan (isanNumber) {
  
  var hexregexp = /^[0-9a-fA-F]+$/;
  var checkDigit = '';
  var submitForm = true;

  isanNumber = trim(isanNumber.toUpperCase());

  // remove 'ISAN ' if scanned form 2D barcode
  isanNumber = isanNumber.replace(/ISAN /, '');

  // remove all the '-' in the isan
  while (isanNumber.indexOf('-') != -1) {
    isanNumber = isanNumber.replace(/\-/, '');
  }
  
  // remove all the ' ' in the isan
  while (isanNumber.indexOf(' ') != -1) {
    isanNumber = isanNumber.replace(/ /, '');
  }  

  // check that the length can math an ISAN16 or an ISAN24
  if (isanNumber.length != 17 && isanNumber.length != 26) {
    submitForm = false;
  }
  
  // check that the ISAN match the regular expression
  if (submitForm) {
    // le check digit est le dernier caractere
    checkDigit = isanNumber.charAt(isanNumber.length-1);
    var isanTmp = isanNumber;
    if (isanTmp.length = 26) {
      isanTmp = isanNumber.substring(0,16)+isanNumber.substring(17,25);
    }
    if (isanTmp.search(hexregexp) == -1) {
      submitForm = false;
    }
  }
  
  // check that the ISAN is correct
  if (submitForm) {
    if (getCheckDigit(isanNumber) != checkDigit) {
      submitForm = false;
    }
  }

  return submitForm;
}

function openWindow (p_url, p_name, p_fullScreen, p_width, p_height) {
	var width = 1004;
	var height = 700;
  var left = 0;
  var top = 0;
  if (p_url != '#')	{
    if (p_fullScreen == 1)	{
      width = screen.width - 16;
      height = screen.height - 100;
    } else {
      if (p_width > 0) {
        width = p_width;
      } else { 
        width = screen.width - 16;
      }
      if (p_height > 0) {
        height = p_height;      
      } else { 
        height = screen.height - 70;
      }
    }

    if (width > 1024) {
      width = 1004;
    }

    left = (screen.width - width) / 2;
    if (left < 0) {
      left = 0;
    }
    var params = 'height='+height+', width='+width+', toolbar=0, location=0, directories=0, status=1, menubar=0, scrollbars=1, resizable=yes, screenX='+left+', screenY='+top;
    window.open(p_url, p_name, params);
    return false;
  }
}