if(navigator.appName == "Netscape"||navigator.appName == "Opera") {
   var tr = "table-row";
   var ti = "table-inline";
   var il = "inline";
} else {
   var tr = "block";
   var ti = "inline";
   var il = "inline";
}

var t_int = 1;
var t_str = 2;
var t_float = 3;
var t_date = 4;

function isRadioChecked(el) {
   for(ei = 0; ei < el.length; ei++)
      if(el[ei].checked) return true;

   return false;
}

function isAnyCheckboxChecked(fo, name) {
   for(j = 0; j < fo.elements.length; j++) {
      if(fo.elements[j].type == 'checkbox' && fo.elements[j].name == name && fo.elements[j].checked) {
         return true;
      }
   }
   return false;
}

function getElementGroupIndexes(el_name, all_groups) {
   for(var egi = 0; egi < all_groups.length; egi++) {
      for(var egj = 0; egj < all_groups[egi][1].length; egj++) {
         if(all_groups[egi][1][egj] == el_name) return new Array(egi, egj);
      }
   }
   return false;
}

function validateForm(fo,f_names,f_type,attr,len,groups,silent) {
   var value = "";
   var result = true;
   var this_result = true;
   var f_bad = new Array();
   var group = null;
   var indexes = 0;

   /* Attīram laukiem sarkanu fonta krāsu */
   for(i = 0; i < f_names.length; i++) {
      el = document.getElementById(f_names[i]);
      if(el != undefined) el.style.color = "#000000";
   }

   /* Izejam cauri visiem ievadlaukiem */
   for(i = 0; i < f_names.length; i++) {
      this_result = true;
      el = fo.elements[f_names[i]];
      if(el != undefined) {
         indexes = getElementGroupIndexes(f_names[i], groups);

//         alert(i + ") Attr: " + attr[i] + " Type: " + f_type[i] + " Name: " + f_names[i] + " Len: " + len[i] + " El_type: " + el.type + " El_value: " + el.value + " Result: " + result + " El: " + el + " Indexes: " + indexes);
         if(el.type == undefined) {
            if(attr[i] == 1 && !isRadioChecked(el)) {
               if(!indexes) {
                  result = false;
                  f_bad[f_bad.length] = f_names[i];
               }
               this_result = false;
            }

         } else if(attr[i] == 0) el.value = ""; /* Ja lauks nav ievadformā, attīram tā vērtību */
         else {
            if(attr[i] == 1&&(el.value.length == 0||el.value == 0||el.value == "")) { /* Ja lauks ir obligāts un tā garums ir 0, vai vērtība ir 0 vai tukša simbolu virkne, atgriežam kļūdu */

               if(!indexes) {
                  result = false;
                  f_bad[f_bad.length] = f_names[i];
               }
               this_result = false;

            } else {
               if(el.value.length > len[i] && len[i] != 0) { /* Ja garums (simbolos) ir lielāks par atļauto, atgriežam kļūdu */
                  if(!indexes) {
                     result = false;
                     f_bad[f_bad.length] = f_names[i];
                  }
                  this_result = false;

               } else if(el.value.length > 0) {

                  if(f_type[i] == t_int) {
                     if(validInt(el.value) == false) {
                        if(!indexes) {
                           result = false;
                           f_bad[f_bad.length] = f_names[i];
                        }
                        this_result = false;
                     }

                  } else if(f_type[i] == t_float) {
                     if(validFloat(el.value) == false) {
                        if(!indexes) {
                           result = false;
                           f_bad[f_bad.length] = f_names[i];
                        }
                        this_result = false;
                     }

                  } else if(f_type[i] == t_date) {
                     if(validDateFormat(el, 8) == false) {
                        if(!indexes) {
                           result = false;
                           f_bad[f_bad.length] = f_names[i];
                        }
                        this_result = false;
                     }
                  }
               }
            }
         }
         if(this_result && indexes != false) {
            groups[indexes[0]][2] = true;
//            if(indexes) alert("Group: " + groups[indexes[0]][0] + "; This result: " + this_result + "; Result: " + result + "; Valid: " + groups[indexes[0]][2] + "; El name: " + f_names[i]);
         }
      }
   }

   var bad_groups = false;
   /* Ja grupas nav validētas, tad ierakstām to grupu laukus f_bad masīvā */
   for(var i = 0; i < groups.length; i++) {
//      alert("SUMMARY: Group: " + groups[i][0] + "; Valid: " + groups[i][2] + "; Index: " + groups[i][3]);
      if(groups[i][2] == false) {
         bad_groups = true;
         result = false;
         for(var j = 0; j < groups[i][1].length; j++) f_bad[f_bad.length] = groups[i][1][j];
      }
   }

   /* Reorganizējam nepareizus laukus saskaņā ar lauku secību */
   var f_bad_out = new Array();
   if(bad_groups) {
      for(i = 0; i < f_names.length; i++) {
         for(j = 0; j < f_bad.length; j++)
            if(f_bad[j] == f_names[i]) {
               f_bad_out[f_bad_out.length] = f_names[i];
               break;
            }
      }
      f_bad.length = 0;
      f_bad = f_bad_out;
   }
//   alert(f_bad + " Result: " + result);

   if(!result) {
      /* Parādam nepareizus laukus ar sarkanu krāsu */
      for(i = 0; i < f_bad.length; i++) {
         el = document.getElementById(f_bad[i]);
         if(el != undefined) el.style.color = "#FF0000";
      }
      if(!silent) alert("Vienam vai vairākiem laukiem ir nepareizs garums vai vērtība!");
      el = fo.elements[f_bad[0]];
      if(el.type != "hidden") {
         if(el.type != undefined) el.focus();
         else el[0].focus();
      }
   }
   return result;
}

function validFloat(val) {
   var sep_cnt = 0;
   if(val.length == 0||val.length == undefined) return false;
   else {
      for(jf = 0; jf < val.length; jf++) {
         if(val.charAt(jf) < "0"||val.charAt(jf) > "9") {
            if(val.charAt(jf) == "."&&sep_cnt == 0) sep_cnt++;
            else return false;
         }
      }
      return true;
   }
}

function validInt(val) {
   if(val.length == 0||val.length == undefined) return false;
   for(jf = 0; jf < val.length; jf++)
      if(val.charAt(jf) < "0"||val.charAt(jf) > "9") return false;
   return true;
}

function validDateFormat(field, minlen) {
   var strDate = field.value;
   var strDateArray;
   var strDay;
   var strMonth;
   var strYear;

   if(strDate.length < minlen){
      field.focus();
      return false;
   }

   if(strDate.length > 0) {
      strDateArray = strDate.split('.');
      if(strDateArray.length != 3){
         field.focus();
         return false;
      } else {
         strYear = strDateArray[0];
         strMonth = strDateArray[1];
         strDay = strDateArray[2];
      }
      if((isNaN(strDay)) || (isNaN(strMonth)) || (isNaN(strYear))) {
         field.focus();
         return false;
      }
      if((strMonth > 12) || (strDay > 31)  || (strYear < 1900)) {
         field.focus();
         return false;
      }
   }
   return true;
}

function popup(url, w, h) {
   var dx = Math.round((screen.availWidth - w) / 2);
   var dy = Math.round((screen.availHeight - h) / 2);
   window.open(url,'','width='+w+', height='+h+', left='+dx+', top='+dy+', toolbar=no, statusbar=no, scrollbars=yes, resizable=yes, menubar=no');
}

function popup_custom(url,w,h,params) {
   var dx = Math.round((screen.availWidth - w) / 2);
   var dy = Math.round((screen.availHeight - h) / 2);
   var all_params = 'width='+w+', height='+h+', left='+dx+', top='+dy;
   if(params.length > 0) all_params += ',' + params;
   return window.open(url,'',all_params);
}

function str_append(separator, str1, str2) {
   if(str1 != "") str1 += separator;
   str1 += str2;
   return str1;
}

/* NACE function begin */

function getArrayIndexOf(source, value) {
   for(n = 0; n < source.length; n++) {
      if(source[n] == value) return n;
   }
   return false;
}

function popupNaceCommon(fo, f_nace_id, f_nace_text, url, w, h) {
   var p_el = fo.elements[f_nace_id];
   url = url+'&parent_form='+fo.name+'&nace_id_prev='+p_el.value+'&field='+f_nace_id+'&field_text='+f_nace_text;
   popup(url, w, h);
}

function setNaceAndShowCommon(fo, f_nace_id, f_nace_text, parent_form, id, text) {
   var p_el = window.opener.document.forms[parent_form].elements[f_nace_id];
   var p_el_text = window.opener.document.getElementById(f_nace_text);
   p_el.value = id;

   /* Dzēšam apakšelementus, ja tie eksistē */
   while(p_el_text.firstChild) p_el_text.removeChild(p_el_text.childNodes[0]);

   for(ni = 0; ni < text.length; ni++) {
      el_div = window.opener.document.createElement("div");
      txt = window.opener.document.createTextNode(text[ni]);
      el_div.appendChild(txt);
      p_el_text.appendChild(el_div);
   }
   self.close();

   return true;
}

function naceSaveNext(fo) {
   fo.elements["nace_save"].value = "1";

   fo.submit();
   return true;
}

function naceNextPage(fo, section, ident) {
   fo.elements["section"].value = section;
   fo.elements["ident"].value = ident;

   fo.submit();
   return true;
}

/* NACE functions end */

function rowOver(el) {
   el.className = 'list_row_over';
   return true;
}

function rowOut(el) {
   el.className = 'list_row_out';
   return true;
}

var original_color = "#FFFFFF";

function setBgcolor(el, color) {
   original_color = el.style.backgroundColor;
   el.style.backgroundColor = color;
   el.style.cursor = "pointer";
}

function clearBgcolor(el) {
   el.style.backgroundColor = original_color;
   el.style.cursor = "default";
}

function go(url) {
   window.location = url;
   return true;
}

function cursor(el, type) {
   el.style.cursor = type;
   return true;
}

