function validateMail(str, mandatory){
  if (str.length == 0) {
    if (mandatory == true) {
      alert("Email-ul este obligatoriu")
      return (false)
    }
    else return (true)
  }
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(str))
    return (true)
  else{
    alert("Email-ul este invalid")
    return (false)
  }
}

function validateMail2(str, mandatory){
  if (str.length == 0) {
    if (mandatory == true) {

      return (false)
    }
    else return (true)
  }
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(str))
    return (true)
  else{
    
    return (false)
  }
}

function validateCommaSeparatedMails(str, mandatory) {
    // DE TERMINAT
  if (str.length == 0) {
    if (mandatory == true) {
      alert("Email-ul este obligatoriu")
      return (false)
    }
    else return (true)
  }
  
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(str))
    return (true)
  else{
    alert("Email-ul este invalid")
    return (false)
  }
}

//testeaza str sa fie alfanumeric, sa fie de minim len caractere
//fieldname este numele campului testat (folosit pt alert)
function validateAlphanum(str, fieldname, mandatory, len) {
  if (str.length == 0) {
    if (mandatory == true) {
      alert(fieldname + "este obligatoriu")
      return (false)
    }
    else return (true)
  }
  if (str.length <len) {
    alert(fieldname + "trebuie sa fie cel putin" + len + "lung")
    return (false)
  }
  var filter=/^[a-zA-Z0-9]+(\s[a-zA-Z?-ü0-9]+)*$/  
  if (filter.test(str))
    return (true)
  else{
    alert(fieldname + "trebuie sa fie alfanumeric")
    return (false)
  }
}

function validatePrintable(str, fieldname, mandatory, len) {
  if (str.length == 0) {
    if (mandatory == true) {
      alert(fieldname + LANG_is_mandatory)
      return (false)
    }
    else return (true)
  }
  if (str.length <len) {
    alert(fieldname + LANG_has_to_be_at_least + len + LANG_long)
    return (false)
  }  
  var filter=/^[\w\s?-ü.,:;'!?<>//=()-]*$/
  if (filter.test(str))
    return (true)
  else{
    alert(fieldname + LANG_must_printable)
    return (false)
  }
}

//testeaza str sa fie alfanumeric, sa fie de minim len caractere
//fieldname este numele campului testat (folosit pt alert)
function validateAlphaSpaces(str, fieldname, mandatory, len) {
  if (str.length == 0) {
    if (mandatory == true) {
      alert(fieldname + LANG_is_mandatory)
      return (false)
    }
    else return (true)
  }
  if (str.length <len) {
    alert(fieldname + LANG_has_to_be_at_least + len + LANG_long)
    return (false)
  }
  var filter=/^[a-zA-Z]+(\s[a-zA-Z?-ü]+)*$/
  if (filter.test(str))
    return (true)
  else{
    alert(fieldname + LANG_must_alpha_spaces)
    return (false)
  }
}

function validateName(str, fieldname, mandatory, len) {
  if (str.length == 0) {
    if (mandatory == true) {
      alert(fieldname + LANG_is_mandatory)
      return (false)
    }
    else return (true)
  }
  if (str.length <len) {
    alert(fieldname + LANG_has_to_be_at_least + len + LANG_long)
    return (false)
  }
  var filter=/^[\w\s?-ü.'-]*$/
  if (filter.test(str))
    return (true)
  else{
    alert(fieldname + LANG_must_name)
    return (false)
  }
}

function validateUsername(str, len) {
  var filter=/^([\w]+(?:\.[\w]+)*)$/
  if (str.length <len) {
    alert(LANG_username + LANG_has_to_be_at_least + len + LANG_long)
    return (false)
  }
  if (filter.test(str))
    return (true)
  else{
    alert(LANG_invalid_username)
    return (false)
  }
}

function validatePosInteger(str, fieldname, mandatory) {
  if (str.length == 0) {
    if (mandatory == true) {
      alert(fieldname + LANG_is_mandatory)
      return (false)
    }
    else return (true)
  }
  var filter=/^\d+$/
  if (filter.test(str))
    return (true)
  else{
    alert(fieldname + LANG_must_pos_int)
    return (false)
  }
}

function validateInteger(str, fieldname, mandatory) {
  if (str.length == 0) {
    if (mandatory == true) {
      alert(fieldname + LANG_is_mandatory)
      return (false)
    }
    else return (true)
  }
  var filter=/^[+-]?(\d)+$/
  if (filter.test(str))
    return (true)
  else{
    alert(fieldname + LANG_must_int)
    return (false)
  }
}

function validateFloat(str, fieldname, mandatory) {
  if (str.length == 0) {
    if (mandatory == true) {
      alert(fieldname + LANG_is_mandatory)
      return (false)
    }
    else return (true)
  }
  var filter=/^[0-9]+([\.|,][0-9]+)?$/
  if (filter.test(str))
    return (true)
  else{
    alert(fieldname + LANG_must_float)
    return (false)
  }
}

function checkTheSame(str1,str2) {
  if (str1 == str2)
    return (true)
  else {
    alert(LANG_pass_dont_match);
    return (false)
  }
}

//testeaza str sa fie alfanumeric, sa fie de minim len caractere
//fieldname este numele campului testat (folosit pt alert)
function validateFilename(str, len, fieldname) {
  
  if (str.length <len) {
    alert(fieldname + LANG_has_to_be_at_least + len + LANG_long)
    return (false)
  }
  return (true);
  /*if (filter.test(str))
    return (true)
  else{
    alert(fieldname + LANG_must_filename)
    return (false)*/
}

function validateNotEmpty(str, fieldname) {
  if (str.length <= 0) {
    alert(fieldname + LANG_is_mandatory)
    return (false)
  }
  return (true)
}

function validatePhone(str, mandatory, fieldname) {
  if (str.length == 0) {
    if (mandatory == true) {  
      alert(fieldname + LANG_is_mandatory)
      return (false)
    }
    else return (true)
  }
  var filter=/^(\+)?[\d]+([\s./-]*[\d]+)*$/
  if (filter.test(str))
    return (true)
  else{
    alert(fieldname + LANG_must_phone)
    return (false)
  }
}

function validateSite(str, mandatory, fieldname) {
  if (str.length == 0) {
    if (mandatory == true) {  
      alert(fieldname + LANG_is_mandatory)
      return (false)
    }
    else return (true)
  }
  var filter=/^[\w-]+(.[\w-]+)*$/ /*/^([a-z]+:(/)(/))?[\w-]+(.[\w-]+)*(:[\d]+)?(/[~\w-])*(/)?$/*/  
  if (filter.test(str))
    return (true)
  else{
    alert(fieldname + LANG_must_site)
    return (false)
  }
}

//returns the timestamp of a date, given in the 'DD.MM.YYYY hh.mm' format
function parseDate(str) {
  var datetime=str.split(' ')
  var date=datetime[0].split('.')
  var time=datetime[1].split(':')
  return (new Date(date[2], date[1] - 1, date[0], time[0], time[1])).getTime()
}

function validateDateFuture(str, fieldname) {
  var timestamp = parseDate(str)
  if (timestamp <= (new Date()).getTime()) {
    alert(fieldname + LANG_must_be_future)
    return (false)
  }
  return (true)
}

function validateDateAfter(str, fieldname, str2, fieldname2) {
  var timestamp = parseDate(str)
  var timestamp2 = parseDate(str2)
  if (timestamp <= timestamp2) {
    alert(fieldname + LANG_must_be_after + fieldname2)
    return (false)
  }
  return (true)
}