/* $Id: general.js,v 2.2 2006/01/05 01:16:34 Lubos Exp $ */

// overeni ruznych datovych typu
// input: string - textovy retezec pro overeni
//        type - typ
//        length - minimalni povolena delka u textu, maximalni delka ID
// output: is_ok - T x F - je X neni v poradku

function check_string_type(string, type, length) {
  var is_ok = true;  // inicializace

  switch(type) {
    case "date":
      if(!string.match("^(([0-2]?[1-9])|([1-3][0])|(31))\.(([0]?[1-9])|([1][0-2]))\.([0-9]{4})$")) is_ok = false;
      break;
    case "mail":
      if(!string.match("^[^.]+(\.[^.]+)*@([^.]+[.])+[a-zA-Z]{2,4}$")) is_ok = false;
      break;
    case "zip":
      if(!string.match("^[a-z0-9 ]{5,8}$")) is_ok = false;
      break;
    case "phone":
      if(!string.match("^[0-9 \(\)+]{7,20}$")) is_ok = false;
      break;
    case "ic":
      if(!string.match("^[0-9a-zA-Z ]{7,15}$")) is_ok = false;
      break;
    case "dic":
      if(!string.match("^[0-9a-zA-Z \-]{7,15}$")) is_ok = false;
      break;
    case "id":
      if(!string.match("^[0-9a-zA-Z_\-]{1,"+length+"}$")) is_ok = false;
      break;
    case "number":
      if(!string.match("^[0-9]{1,10}[\.,\,]{0,1}[0-9]*$")) is_ok = false;
      break;
    case "text":
      if(string.length < length) is_ok = false;
      break;
  }

  return is_ok;
}

// =============================================================================

// overi udaje o zakaznikovi

function check_cust_general_data() {
  // inicializace promennych
  var cust_f_name = get_element("cust_f_name").value;
  var cust_l_name = get_element("cust_l_name").value;
  var cust_mail = get_element("cust_mail").value;
  var cust_phone = get_element("cust_phone").value;
  var cust_fax = get_element("cust_fax").value;
  var cust_firm = get_element("cust_firm").value;
  var cust_ic = get_element("cust_ic").value;
  var cust_dic = get_element("cust_dic").value;
  var cust_street = get_element("cust_street").value;
  var cust_city = get_element("cust_city").value;
  var cust_zip = get_element("cust_zip").value;
  var cust_del_f_name = get_element("cust_del_f_name").value;
  var cust_del_l_name = get_element("cust_del_l_name").value;
  var cust_del_firm = get_element("cust_del_firm").value;
  var cust_del_street = get_element("cust_del_street").value;
  var cust_del_city = get_element("cust_del_city").value;
  var cust_del_zip = get_element("cust_del_zip").value;
  var cust_del_mail = get_element("cust_del_mail").value;
  var cust_del_phone = get_element("cust_del_phone").value;
  var cust_del_fax = get_element("cust_del_fax").value;

  var err_message = "";
  var is_ok = true;

  // overeni jmena
  if(!check_string_type(cust_f_name, "text", 2)) {
    err_message = err_message+"Jméno musí obsahovat minimálně 2 znaky\n";
    is_ok = false;
  }

  // overeni prijmeni
  if(!check_string_type(cust_l_name, "text", 2)) {
    err_message = err_message+"Příjmení musí obsahovat minimálně 2 znaky\n";
    is_ok = false;
  }

  // overeni firmy
  if(cust_firm != "" && !check_string_type(cust_firm, "text", 2)) {
    err_message = err_message+"Název firmy musí obsahovat minimálně 2 znaky\n";
    is_ok = false;
  }

  // overeni IC
  if(cust_ic != "" && !check_string_type(cust_ic, "ic")) {
    err_message = err_message+"IČ není platné\n";
    is_ok = false;
  }

  // overeni DIC
  if(cust_dic != "" && !check_string_type(cust_dic, "dic")) {
    err_message = err_message+"DIČ není platné\n";
    is_ok = false;
  }

  // overeni ulice
  if(!check_string_type(cust_street, "text", 2)) {
    err_message = err_message+"Ulice musí obsahovat minimálně 2 znaky\n";
    is_ok = false;
  }

  // overeni mesta
  if(!check_string_type(cust_city, "text", 2)) {
    err_message = err_message+"Město musí obsahovat minimálně 2 znaky\n";
    is_ok = false;
  }

  // overeni PSC
  if(!check_string_type(cust_zip, "zip")) {
    err_message = err_message+"PSČ není platné\n";
    is_ok = false;
  }

  // overeni telefonu
  if(!check_string_type(cust_phone, "phone")) {
    err_message = err_message+"Telefon není platný\n";
    is_ok = false;
  }

  // overeni faxu
  if(cust_fax != "" && !check_string_type(cust_fax, "phone")) {
    err_message = err_message+"Fax není platný\n";
    is_ok = false;
  }

  // overeni mailu
  if(!check_string_type(cust_mail, "mail")) {
    err_message = err_message+"E-mailová adresa není platná\n";
    is_ok = false;
  }

  // overeni jmena pro doruceni
  if(cust_del_f_name != "" && !check_string_type(cust_del_f_name, "text", 2)) {
    err_message = err_message+"Jméno pro doručení musí obsahovat minimálně 2 znaky\n";
    is_ok = false;
  }

  // overeni prijmeni pro doruceni
  if(cust_del_l_name != "" && !check_string_type(cust_del_l_name, "text", 2)) {
    err_message = err_message+"Příjmení pro doručení musí obsahovat minimálně 2 znaky\n";
    is_ok = false;
  }

  // overeni firmy pro dorucení
  if(cust_del_firm != "" && !check_string_type(cust_del_firm, "text", 2)) {
    err_message = err_message+"Název firmy pro doručení musí obsahovat minimálně 2 znaky\n";
    is_ok = false;
  }

  // overeni ulice pro doruceni
  if(cust_del_street != "" && !check_string_type(cust_del_street, "text", 2)) {
    err_message = err_message+"Ulice pro doručení musí obsahovat minimálně 2 znaky\n";
    is_ok = false;
  }

  // overeni mesta pro doruceni
  if(cust_del_city != "" && !check_string_type(cust_del_city, "text", 2)) {
    err_message = err_message+"Město pro doručení musí obsahovat minimálně 2 znaky\n";
    is_ok = false;
  }

  // overeni PSC pro doruceni
  if(cust_del_zip != "" && !check_string_type(cust_del_zip, "zip")) {
    err_message = err_message+"PSČ pro doručení není platné\n";
    is_ok = false;
  }

  // overeni telefonu pro doruceni
  if(cust_del_phone != "" && !check_string_type(cust_del_phone, "phone")) {
    err_message = err_message+"Telefon pro doručení není platný\n";
    is_ok = false;
  }

  // overeni faxu pro doruceni
  if(cust_del_fax != "" && !check_string_type(cust_del_fax, "phone")) {
    err_message = err_message+"Fax pro doručení není platný\n";
    is_ok = false;
  }

  // overeni mailu pro doruceni
  if(cust_del_mail != "" && !check_string_type(cust_del_mail, "mail")) {
    err_message = err_message+"E-mailová adresa pro doručení není platná\n";
    is_ok = false;
  }

  return_arr = new Array(err_message, is_ok);

  return return_arr;
}
