var is_logged = false;

// OPERA VERTICAL POISTION RE-ADJUST FIX
// IE 6 crashes on this one, when width of the window is smaller
// than width of the dialogbox
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) {
	window.onresize = redrawDialog; 
}

function redrawDialog() {
	var cur_class = document.getElementById("fixedbox").className;
	document.getElementById("fixedbox").className = cur_class;
}

function drawDialog(content, dlgclass) {
	document.getElementById("fixedbox").className="fixedbox "+dlgclass;
	document.getElementById("screenlock").className="screenlock";
	document.getElementById("fixedbox").innerHTML='';
	document.getElementById("fixedbox").innerHTML=content;
}

function destroyDialog() {
	document.getElementById("fixedbox").className="fixedbox_off";
	document.getElementById("screenlock").className="screenlock_off";
	document.getElementById("fixedbox").innerHTML='';
}


function hideshowPopup(vis) {
	document.getElementById("modal_popup").style.visibility = vis;
}

function showPopupReg() {
	window.open('reg_ochrona.html','windows_reg','resizable=yes,scrollbars=yes,width=640,height=520');
}

function checkFilesStep2() {
   var sections = new Array('atrament', 'laser', 'proof');
   var ilosc = eval(document.form2.pliki_ilosc.value);
   var pos_count = 0;

   for (i = 0, n = sections.length; i < n; i++) {
      var pos_count = eval(document.form2.elements['zamow_'+sections[i]+'_ilosc_poz'].value);
      for (j = 0; j < pos_count; j++) {
         var ok_flag = false;
         for (k = 0; k < ilosc; k++) {
            if (document.form2.elements['plik_sekcja['+k+']'].value == sections[i] && document.form2.elements['plik_poz['+k+']'].value == j) {
               ok_flag = true;
            }
         }

         if (!ok_flag) {
            alert('Do każdej pozycji musi być przypisany przynajmniej jeden plik.');
            return false;
         }
      }
   }
  
   document.form2.goto_next.value=true; 
   document.form2.submit();
   // return true;
}

function xmlToArray(x) {
   
   var out_array = new Array();
   var col_names = new Array();

   for (j=0; j < x[0].childNodes.length; j++) {
      if (x[0].childNodes[j].nodeType != 1) continue;
      col_names[j] = x[0].childNodes[j].nodeName;
   }

   for (i=0; i < x.length; i++) {
      out_array[i] = new Array();
      for (j=0; j < x[i].childNodes.length; j++) {
         if (x[i].childNodes[j].nodeType != 1) continue;
         if (x[i].childNodes[j].firstChild) {
            out_array[i][col_names[j]] = x[i].childNodes[j].firstChild.nodeValue;
         }
         else {
            out_array[i][col_names[j]] = null;
         }
      }
   }

   return out_array;
}

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
      }
   }
   return IsNumber;
}

function checkOrderPosNumber(section, position, max_count, upload_click, cur_file_pos) {
   var f2 = document.form2;

   var count = 0;
   var sum = 0;
   var cur_val = 0;
   
   while (f2.elements['plik_poz['+count+']']) {
      if (f2.elements['plik_sekcja['+count+']'].value == section && f2.elements['plik_poz['+count+']'].value == position) {
         cur_val = eval(f2.elements['plik_kopie['+count+']'].value);
         sum += cur_val; 
      }

      count++;
   }
   
   if (sum > max_count || (upload_click && sum >= max_count)) {
      alert('Uwaga: Łączna ilość kopii danego pliku nie może przekraczać liczby kopii dla wybranej pozycji.\nW przypadku obecnej pozycji nr '+eval(position+1)+' w sekcji '+section+' jest to '+max_count+' kopii.');

      if (!upload_click) {
         f2.elements['plik_kopie['+cur_file_pos+']'].value = 1;
      }
      
      return false;
   }

   return true;
}

function regCheckFirma() {
  if (document.rejestracja.typ_klient.checked) {
     document.getElementById('reg_NIP_marker').innerHTML = '';
     document.getElementById('reg_NIP_marker').innerHTML = '<span style="color: #ffc000;">NIP:</span>';
     document.getElementById('reg_Tel_marker').innerHTML = '';
     document.getElementById('reg_Tel_marker').innerHTML = '<span style="color: #ffc000;">Telefon stacjonarny:</span>';
  }
  else {
     document.getElementById('reg_NIP_marker').innerHTML = '';
     document.getElementById('reg_NIP_marker').innerHTML = 'NIP:';
     document.getElementById('reg_Tel_marker').innerHTML = '';
     document.getElementById('reg_Tel_marker').innerHTML = 'Telefon stacjonarny:';
  }
}

function trim(s) {
   while (s.substring(0,1) == ' ') {
      s = s.substring(1,s.length);
   }
   while (s.substring(s.length-1,s.length) == ' ') {
      s = s.substring(0,s.length-1);
   }
   return s;
}

function isArray(a) {
   return isObject(a) && a.constructor == Array;
}
