/*******************************************************************/
/* functions.js - Funkcje walidacji danych                         */
/* COPYRIGHT msz. ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.*/
/* REVISION  0.2.1   LAST MODIFIED: 30/01/05   BY: Darek Michalik  */
/* REVISION  0.2.3   LAST MODIFIED: 21/02/05   BY: Darek Michalik  */
/********************************************************************/

/* Funkcja wyliczająca date ważności rekordu
   na podstawie wprowadzonej wartości przez
   użytkownika wyrażonej w dniach */
function SetCtryDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.ctry_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.ctry_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}

function ValidateCtryOnSubmit(){
    if(document.form.ctry_code.value == '' || document.form.ctry_name.value == '' || document.form.ctry_name_en == ''){
        alert("Żadne z pól: Kod Kraju, Nazwa Kraju i Nazwa Angielska Kraju nie może być puste!!!");
    } 
    else{
        if(document.form.ctry_LT.value == ''){
	    document.form.ctry_LT.value = '0';
	}
	document.form.submit();
    }
}

function CtryExtraData(){
    if(document.form.ctry_info.value == 'T'){
        document.form.com_page.disabled = false;
	document.form.com_lng_id.disabled = false;
	document.form.com_data.disabled = false;
    }
    if(document.form.ctry_info.value == 'N'){
        document.form.com_page.disabled = true;
	document.form.com_lng_id.disabled = true;
	document.form.com_data.disabled = true;
    }
}

function getFieldToEdit(){
    document.choice.submit();
}

function SetDptDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.dpt_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.dpt_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}

function ValidateDeptOnSubmit(){
    if(document.form.dpt_name.value == ''){
        alert("Pole Nazwa Departamentu nie może być puste.");
    }
    else{
        document.form.submit();
    }
}

function SetFunkDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.fun_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.fun_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}

function ValidateFunkOnSubmit(){
    if(document.form.fun_name.value == ''){
        alert("Pole Nazwa Funkcji nie może być puste.");
    }
    else{
        document.form.submit();
    }
}

function SetLangDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.lng_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.lng_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}

function ValidateLangOnSubmit(){
   if(document.form.lng_desc.value == '' || document.form.lng_desc_en.value =='') {
       alert("Pola Nazwa i Nazwa angielska nie mogą być puste.");
   }
   else {
       document.form.submit();
   }
}

function SetLoctDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.loct_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.loct_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}

function ValidateLoctOnSubmit(){
    if(document.form.loct_name.value == ''){
        alert("Pole Nazwa Placówki nie może być puste.");
    }
    else {
        document.form.submit();
    }
}

function SetPosDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.pos_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.pos_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}

function ValidatePosOnSubmit(){
    if(document.form.pos_name.value == '') {
        alert("Pole Nazwa Stanowiska nie może być puste.");
    }
    else {
        document.form.submit();
    }
}

function SetRegDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.reg_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.reg_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}

function ValidateRegOnSubmit(){
    if(document.form.reg_name.value == '') {
        alert("Pole Nazwa Regionu nie może być puste.");
    }
    else {
        document.form.submit();
    }
}

function SetEmpDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.emp_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.emp_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}

function ValidateEmpOnSubmit(){
    if(document.form.emp_first_name.value == '' || document.form.emp_last_name.value == '' || document.form.emp_title.value == ''){
        alert("Żadne z pól: Imię, Nazwisko, Tytuł nie może być puste.");
    }
    else {
        if((document.form.emp_func.value == 'T') && ((document.form.job_date_start == '') || (document.form.job_date_end == ''))) {
	    alert("Pola data rozpoczęcia oraz data zakończenia wykonywania funkcji dodatkowych nie mogą być puste!");
	}
	else {
            if(document.form.emp_mail.value != ''){
                var valid_email = emailCheck(document.form.emp_mail.value);
	        if(valid_email == true){
                    document.form.submit();
	        }
	    }
	    else {
	        document.form.submit();
	    }
	}
    }
}

function EmplExtraData(){
    if(document.form.emp_func.value == 'T'){
        document.form.job_fun_id.disabled = false;
        document.form.job_date_start.disabled = false;
        document.form.job_date_end.disabled = false;
    }
    if(document.form.emp_func.value == 'N'){
        document.form.job_fun_id.disabled = true;
        document.form.job_date_start.disabled = true;
        document.form.job_date_end.disabled = true;
    }
}


function PlacExtraData(){
    if(document.form.loc_plac.value == 'T'){
        document.form.loc_ctry_ext.disabled = false;
        document.form.loc_loc_id.disabled = false;
         alert ("Wprowad nazwę kraju i placówki delegowanej do obsługi tego kraju.");
    }
    if(document.form.loc_plac.value == 'N'){
 alert ("Placowka istnieje w tym kraju.");
        document.form.loc_ctry_ext.disabled = true;
        document.form.loc_loc_id.disabled = true; 
    }
}


function SetLocDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.loc_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.loc_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}


function ValidateLocOnSubmit(){
    if(((document.form.loc_name.value == '') || (document.form.loc_city.value == '') || (document.form.loc_addr1.value == '') || (document.form.loc_zip.value == '')) && (document.form.loc_plac.value == 'N')) {
        alert("Żadne z pól: Nazwa, Miasto, Adres, Kod Pocztowy nie może być puste.");
    }
    else {
        if(document.form.loc_mail.value != ''){
            var valid_email = emailCheck(document.form.loc_mail.value);
	    if(valid_email == true){
                document.form.submit();
	    }
	}
	else {
	    document.form.submit();
	}
    }
}


function SetOpisDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.com_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.com_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}

function ValidateOpisOnSubmit(){
    if(document.form.com_page.value == ''){
        alert("Musisz podać numer wprowadzanej strony informacyjnej.");
    }
    else {
        document.form.submit();
    }
}
/* Missions Maintenance  Darek Michalik listopad 2004    */

function SetMpaDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.mpa_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.mpa_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}

// 11.24 dm

function ValidateMpaOnSubmit(){

    if(document.form.mpa_name.value == '' || document.form.mpa_city.value == '' || document.form.mpa_addr1.value == '' || document.form.mpa_zip.value == ''){
        alert("Żadne z pól: Nazwa, Miasto, Adres, Kod Pocztowy nie może być puste.");
    }
    else {
        if(document.form.mpa_mail.value != ''){
            var valid_email = emailCheck(document.form.mpa_mail.value);
	    if(valid_email == true){
                document.form.submit();
	    }
	}
	else {
	    document.form.submit();
	}
    }


}


function MpaSearch() {
   var input = true;
   if((document.form.name.value == "") && (document.form.kraj.value == "NULL") && (document.form.city.value == "") && (document.form.region.value == "NULL") && (document.form.loct.value == "")) {
       input = false;
   }
   if(input == true) {
       document.form.submit();
   }
   else {
       alert("Musisz określić choć jedną wartość");
   }
}

function SetMpeDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.mpe_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.mpe_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}

function ValidateMpeOnSubmit(){
    if(document.form.mpe_first_name.value == '' || document.form.mpe_last_name.value == '' || document.form.mpe_title.value == ''){
        alert("Żadne z pól: Imię, Nazwisko, Tytuł nie może być puste.");
    }
    else {
        if((document.form.mpe_date_start == '') || (document.form.mpe_date_end == '')) {
	    alert("Pola data rozpoczęcia oraz data zakończenia wykonywania funkcji dodatkowych nie mogą być puste!");
	}
	else {
            if(document.form.mpe_mail.value != ''){
                var valid_email = emailCheck(document.form.mpe_mail.value);
	        if(valid_email == true){
                    document.form.submit();
	        }
	    }
	    else {
	        document.form.submit();
	    }
	}
    }
}


function MpeExtraData(){
    if(document.form.mpe_func.value == 'F'){
        
    }
    if(document.form.mpe_func.value == 'M'){
        
    }
}

function MpePersData(){
    if(document.form.mpe_pers.value == 'F'){
        
    }
    if(document.form.mpe_pers.value == 'M'){
        
    }
}





function MpeSearch() {
    var input = true;
    if((document.form.name.value == "") && (document.form.kraj.value == "NULL") && (document.form.imie.value == "") && (document.form.lokacja.value == "NULL") && (document.form.dept.value == "NULL") && (document.form.stan.value == "NULL")) {
        input = false;
    }
   if(input == true) {
       document.form.submit();
   }
   else {
       alert("Musisz określić choć jedną wartość");
   }
}

function SetMpdDateExp(){
    var myDate = new Date();
    var LTDate = myDate.getTime();
    var LTMilisec = document.form.mpd_LT.value * 86400000;
    var ExpDate = new Date(LTDate + LTMilisec);
    document.form.mpd_date_exp.value = ExpDate.getFullYear() + "-" + (ExpDate.getMonth() + 1) + "-" + ExpDate.getDate() + " 23:59:59";
}

function ValidateMpdOnSubmit(){

    if(document.form.mpd_name.value == '' || document.form.mpd_city.value == '' || document.form.mpd_addr1.value == '' || document.form.mpd_zip.value == ''){
        alert("Żadne z pól: Nazwa, Miasto, Adres, Kod Pocztowy nie może być puste.");
    }
    else {
        if(document.form.mpd_mail.value != ''){
            var valid_email = emailCheck(document.form.mpd_mail.value);
	    if(valid_email == true){
                document.form.submit();
	    }
	}
	else {
	    document.form.submit();
	}
    }


}

function MpdSearch() {
   var input = true;
   if((document.form.name.value == "") && (document.form.kraj.value == "NULL") && (document.form.city.value == "") && (document.form.lokacja.value == "NULL") && (document.form.loct.value == "")) {
       input = false;
   }
   if(input == true) {
       document.form.submit();
   }
   else {
       alert("Musisz określić choć jedną wartość");
   }
}

function MppSearch() {
    var input = true;
    if(document.form.stan.value == "NULL") {
        input = false;
    }
   if(input == true) {
       document.form.submit();
   }
   else {
       alert("Musisz określić wartość");
   }
}

function PrintForm() {
    document.form.submit();
}




/*     End of Missions                                 */

function ResetForm() {
    document.form.reset();
}

function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("Podany adres e-mail jest nieprawidłowy.\nSprawdź '@' oraz '.'");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Podany adres e-mail jest nieprawidłowy.\nNazwa użytkownika zawiera niedozwolone znaki.");
return false;
   }
   }
   for (i=0; i<domain.length; i++) {
   if (domain.charCodeAt(i)>127) {
   alert("Podany adres e-mail jest nieprawidłowy.\nNazwa domeny zamiera niedozwolone znaki.");
   return false;
      }
      }

      // See if "user" is valid 

      if (user.match(userPat)==null) {

      // user is not valid

      alert("Podany adres e-mail jest nieprawidłowy.\nNazwa użytkownika jest niepoprawna.");
      return false;
      }

      /* if the e-mail address is at an IP address (as opposed to a symbolic
      host name) make sure the IP address is valid. */

      var IPArray=domain.match(ipDomainPat);
      if (IPArray!=null) {

      // this is an IP address

      for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
      alert("Podany adres e-mail jest nieprawidłowy.\nZły adres IP.");
      return false;
         }
	 }
	 return true;
	 }

	 // Domain is symbolic name.  Check if it's valid.
	  
	  var atomPat=new RegExp("^" + atom + "$");
	  var domArr=domain.split(".");
	  var len=domArr.length;
	  for (i=0;i<len;i++) {
	  if (domArr[i].search(atomPat)==-1) {
	  alert("Podany adres e-mail jest nieprawidłowy.\nNazwa domeny jest niepoprawna.");
	  return false;
	     }
	     }

	     /* domain name seems valid, but now make sure that it ends in a
	     known top-level domain (like com, edu, gov) or a two-letter word,
	     representing country (uk, nl), and that there's a hostname preceding 
	     the domain or country. */

	     if (checkTLD && domArr[domArr.length-1].length!=2 && 
	     domArr[domArr.length-1].search(knownDomsPat)==-1) {
	     alert("The address must end in a well-known domain or two letter " + "country.");
	     return false;
	     }

	     // Make sure there's a host name preceding the domain.

	     if (len<2) {
	     alert("This address is missing a hostname!");
	     return false;
	     }

	     // If we've gotten this far, everything's valid!
	     return true;
}

function LocSearch() {
   var input = true;
   if((document.form.name.value == "") && (document.form.kraj.value == "NULL") && (document.form.city.value == "") && (document.form.region.value == "NULL") && (document.form.loct.value == "")) {
       input = false;
   }
   if(input == true) {
       document.form.submit();
   }
   else {
       alert("Musisz określić choć jedną wartość");
   }
}

function ShowDetails(url) {
    window.open(url, "DodatkoweInformacje","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=400, height=400");
}

function EmpSearch() {
    var input = true;
    if((document.form.name.value == "") && (document.form.kraj.value == "NULL") && (document.form.imie.value == "") && (document.form.lokacja.value == "NULL") && (document.form.dept.value == "NULL") && (document.form.stan.value == "NULL")) {
        input = false;
    }
   if(input == true) {
       document.form.submit();
   }
   else {
       alert("Musisz określić choć jedną wartość");
   }
}

function OpisSearch() {
    var input = true;
    if((document.form.kraj.value == "NULL") && (document.form.jezyk.value == "NULL")) {
        input = false;
    }
   if(input == true) {
       document.form.submit();
   }
   else {
       alert("Musisz określić choć jedną wartość");
   }
}

function ValidateUserOnSubmit(){
    if((document.form.usrd_login.value == '') || (document.form.usrd_passwd.value == '')){
        alert("Pola login ani hasło nie mogą być puste!");
    }
    else {
        document.form.submit();
    }
}
