// ------------------------------------------
function showPhoto(obrazek,width,height){

  width = (!width || width==0) ? 300 : width;
  height = (!height || height==0) ? 340 : height;
  if (obrazek.src) {
    obrazek = obrazek.src;
    k = obrazek.lastIndexOf('.');
    obrazek = obrazek.substring(0,k)+'_d'+obrazek.substring(k,200);
  }

  leftPos = parseInt((screen.availWidth-width)/2);
	topPos  = parseInt((screen.availHeight-height)/2);
	if (leftPos<20) leftPos = 20;
	if (topPos<20) topPos = 20;
  
  OknoFoto = window.open('showPhoto.php?obrazek='+obrazek,'HelimedFoto','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,copyhistory=0,top='+topPos+',left='+leftPos+',width='+width+',height='+height);
}

// ------------------------------------------
function showPopupWindow(link,width,height) {
  width  = (!width || width==0) ? 470 : width;
  height = (!height || height==0) ? 480 : height;
  var left = parseInt(screen.width/2-width/2);
  var top  = parseInt(screen.height/2-height/2);
  noweOknoPopup = window.open(link,'PopupWin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,left='+left+',top='+top+',width='+width+',height='+height);
  noweOknoPopup.focus();
}

// ------------------------------------------
function pobierzPlik(plik) {
  if ( plik.indexOf("index.php")!=-1 )
    OknoDownload = window.open(plik,'download_win');
  else
    OknoDownload = window.open('index.php?download/'+plik,'download_win');
  //OknoDownload = window.open('download.php?plik='+plik,'download_win');
}

//-----------------------------------
function sprawdzPole(pole,komunikat){
  pole=document.getElementById(pole);
  if(pole && (pole.value=='' || pole.value==undefined)) {
     alert("Wypełnij pola wymagane:\n\n"+komunikat);
     pole.select();
     pole.focus();
     return false;
  }
  else {
     return true;
  }
}


//---------------------------------
function sprawdzEmail(adres) {
var emailPat=/^(.+)@(.+)\.(.+)$/ ;
var matchArray=adres.match(emailPat);

 	if (matchArray==null)
 	   return false;
   else
      return true;
}

//---------------------------------
function replace(s, t, u) {
// s  string
// t  zamien co
// u  zamien  na
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + u;
  if ( i + t.length < s.length)
    r += replace(s.substring(i + t.length, s.length), t, u);
  return r;
}//function

//---------------------------------
function submitFormLogin() {

  if ( !sprawdzPole('login', '* login') )
    return false;
  if ( !sprawdzPole('password', '* hasło') )
    return false;
  
  return true;
}//f

function submitFormFP() {

  if ( !sprawdzPole('login', '* login') )
    return false;
    
  return true;
  
}//f

//---------------------------------
//pokaz/ukryj wiersz tabeli
function showTableRow(id) {
var myElem = document.getElementById(id);

  try {
    myElem.style.display = 'table-row';
  }
  catch(e) {
    myElem.style.display = 'inline';
  }
}
function hideTableRow(id) {
var myElem = document.getElementById(id);
  myElem.style.display = 'none';
}

var wypelnij_wymagane = "Wypełnij pola wymagane";
var bledny_email      = "Podany adres e-mail jest nieprawidłowy.";

function checkForm(pola) {
  
  for (i=0;i<pola.length;i++) {
    gdzie_sr  = pola[i].indexOf(';');//gdzie jest srednik
    nazwa     = pola[i].substring(0, gdzie_sr);
    komunikat = pola[i].substring(gdzie_sr+1);
    //alert(nazwa+'-'+komunikat);
    if (nazwa.indexOf('email')!=-1) {
      if(!checkField(nazwa,komunikat))
        return false;  
      else
        if(!checkEmail(nazwa))
          return false;  
    }
    else {
      if(!checkField(nazwa,komunikat))
        return false;  
    }
  }//for
  
  return true;
}

//-----------------------------------
function checkField(id,komunikat) {
  pole = document.getElementById(id);
  //alert(id+'-'+pole.value);
  if (pole.value=='' || pole.value==undefined) {
     alert(wypelnij_wymagane+":\n\n*"+komunikat);
     if (pole.type=='textarea' || pole.type=='text')
       pole.select();
     pole.focus();
     return false;
  }
  else {
     return true;
  }
}

//---------------------------------
function checkEmail(id) {
  var pole = document.getElementById(id);
  var emailPat=/^(.+)@(.+)\.(.+)$/;

  if (pole.value.match(emailPat)==null) {
    alert(bledny_email);
    if (pole.type=='textarea' || pole.type=='text')
      pole.select();
    pole.focus();
    return false;
  }
  
  return true;
}

function checkPesel(id) {
  var pole = document.getElementById(id);
  var peselPat=/([0-9]{11})/;

  if (pole.value.match(peselPat)==null) {
    alert("Podany numer PESEL ma nieprawidłową długość");
    if (pole.type=='textarea' || pole.type=='text')
      pole.select();
    pole.focus();
    return false;
  }
  
  return true;
}

function checkTelephone(id) {
  var pole = document.getElementById(id);
  var phonePat=/([0-9]+)/;

  if (pole.value.match(phonePat)==null) {
    alert("Podany numer telefonu zawiera nieprawidłowe znaki");
    if (pole.type=='textarea' || pole.type=='text')
      pole.select();
    pole.focus();
    return false;
  }
  
  return true;
}
