//sprawdzanie daty
// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=1900;
var maxYear=2200;

function isInteger(s)
{
         var i;
         for (i = 0; i < s.length; i++)
         {
          // Check that current character is number.
          var c = s.charAt(i);
          if (((c < "0") || (c > "9"))) return false;
         }
         // All characters are numbers.
         return true;
}

function stripCharsInBag(s, bag)
{
         var i;
         var returnString = "";
         // Search through string's characters one by one.
         // If character is not in bag, append to returnString.
         for (i = 0; i < s.length; i++)
         {
          var c = s.charAt(i);
          if (bag.indexOf(c) == -1) returnString += c;
         }
         return returnString;
}

function daysInFebruary (year)
{
         // February has 29 days in any year evenly divisible by four,
         // EXCEPT for centurial years which are not also divisible by 400.
         return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n)
{
         for (var i = 1; i <= n; i++)
         {
          this[i] = 31
          if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
          if (i==2) {this[i] = 29}
         }
         return this
}

function checkOkres(a, nazwa)
{
         var dtStr = document.getElementById(a).value;
         var daysInMonth = DaysArray(12)
         var pos1=dtStr.indexOf(dtCh)
         var pos2=dtStr.indexOf(dtCh,pos1+1)
         /*
         var strDay=dtStr.substring(0,pos1)
         var strMonth=dtStr.substring(pos1+1,pos2)
         var strYear=dtStr.substring(pos2+1)
         */
         var strDay=dtStr.substring(pos2+1)
         var strMonth=dtStr.substring(pos1+1,pos2)
         var strYear=dtStr.substring(0,pos1)
         strYr=strYear
									
         if (strDay.charAt(0)=="0" && strDay.length > 1) strDay=strDay.substring(1)
         if (strMonth.charAt(0)=="0" && strMonth.length > 1) strMonth=strMonth.substring(1)
         for (var i = 1; i <= 3; i++)
         {
          if (strYr.charAt(0)=="0" && strYr.length > 1) strYr=strYr.substring(1)
         }
         month=parseInt(strMonth)
         day=parseInt(strDay)
         year=parseInt(strYr)
         if (pos1==-1 || pos2==-1){
            alert("Wprowadź prawidłowy format daty: yyyy-dd-mm w polu: "+ nazwa);
            document.getElementById(a).style.color='#ff0000';
            return false
         }
         if (strMonth.length < 1 || month < 1 || month > 12){
            alert("Wprowadź poprawny miesiąc w polu: "+ nazwa +". Format daty: yyyy-dd-mm");
            document.getElementById(a).style.color='#ff0000';
            return false
         }
         if (strDay.length < 1 || day < 1 || day > 31 || (month==2 && day > daysInFebruary(year)) || day > daysInMonth[month]){
            alert("Wprowadź poprawny dzień w polu: "+ nazwa+". Format daty: yyyy-dd-mm");
            document.getElementById(a).style.color='#ff0000';
            return false
         }
         if (strYear.length != 4 || year==0 || year < minYear || year > maxYear){
            alert("Wprowadź poprawny 4 cyfrowy rok (pomiędzy "+minYear+" i "+maxYear+") w polu: "+ nazwa+". Format daty: yyyy-dd-mm" );
            document.getElementById(a).style.color='#ff0000';
            return false
         }
         if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
            alert("Wprowadź poprawną datę w polu: "+ nazwa);
            document.getElementById(a).style.color='#ff0000';
            return false
         }
									
         document.getElementById(a).style.color='#000000';
         return true
}



function przezuc_godzine(z, dest, godzpocz, mkszosta)
{
         //liczba sekund
         var godzkoncowa =  document.getElementById(z).value * 1800 + parseInt(mkszosta);
         var idgodzpocz = document.getElementById(godzpocz).value;
         var sgodzpocz =  idgodzpocz * 1800 + parseInt(mkszosta);
         //alert (" sgodzpocz "+sgodzpocz);

         var roznica = godzkoncowa - sgodzpocz;
         var roznicamin = roznica/60;
         //alert (godzkoncowa+" - "+sgodzpocz+" roznicamin="+ roznicamin);
	 document.getElementById(dest).value = roznicamin;
}

function przezuc_godzine1(z, dest, idgodzpocz)
{
         //liczba sekund

         var wynik =  document.getElementById(z).value/30;
         var idgodzpo =  document.getElementById(idgodzpocz).value;
         var idgodz = parseInt(wynik) + parseInt(idgodzpo);

	 document.getElementById(dest).value = idgodz;
	
	 //alert ('wynik:'+wynik+' + '+idgodzpo+' = '+idgodz);
}

function przezuc_dane(z, dest)
{
	 document.getElementById(dest).value=document.getElementById(z).value;
}

function checkPOLEout(a)
{
         var y=document.getElementById(a).value;
         if(y=='')
         {
          document.getElementById(a).style.background='#f43333';
          return false;
         }
         else
	 {
          document.getElementById(a).style.background='#ffffff';
          return true;
         }
}

function checkPOLEradio( _prefix, tekst )
{
         var checked = false;
         var i = 1;
         var ctrl = document.getElementById( _prefix + i );
         while ( ctrl )
         {
               if ( ctrl.checked )
               {
                  checked = true;
                  break;
               }
               i++;
               ctrl = document.getElementById( _prefix + i );
         }
         if ( !checked )
         {
              alert( tekst );
              return false;
         }
         else return true;
}

function checkPOLEcheck(a, tekst)
{
					var y=document.getElementById(a).checked;
					if(y=='')
					{
						alert(tekst);
						return false;
					}
					else return true;
}

function sprawdz_wybor(a, nazwa)
{
         var wybor=document.getElementById(a).value;
	 if(wybor=="0")
	 {
		alert('Proszę wybrać odpowiednią opcję w polu ' + nazwa);
		return false;
         }
	 else
	 {
		return true;
         }
}

function ValidNIP(a)
{
	var input_data=document.getElementById(a).value;
	if ( !input_data.match( /^[0-9]{3}-[0-9]{2}-[0-9]{2}-[0-9]{3}$/ )
	 && !input_data.match( /^[0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2}$/ )
	 && !input_data.match( /^[0-9]{10}$/ ) )
	{
		alert('Proszę podać poprawny NIP.\nFormat:XXX-XX-XX-XXX');
		document.getElementById(a).style.color='#ff0000';
		return false;
	}
	var my_nums = input_data.replace(/-/g,'');
	var valid_nums = "657234567";
	var sum=0;
	for (var temp=8;temp>=0;temp--)
	sum += (parseInt(valid_nums.charAt(temp)) * parseInt(my_nums.charAt(temp)));
	if ( (sum % 11) == 10 ? false : ((sum % 11) == parseInt(my_nums.charAt(9))) )
	{
		document.getElementById(a).style.color='#000000';
		return true;
	}
	else
	{
		document.getElementById(a).style.color='#ff0000';
		return false;
	}
}

function checkKodPocztowy(a)
{
         var Kod=document.getElementById(a).value;
				
         if(Kod=="") return false;
						
	 regex=/^([0-9]{2}-[0-9]{3})$/;
	 if(!regex.test(Kod))
	 {
		alert('Proszę podać poprawny kod pocztowy.\n(np. 12-345)');
		document.getElementById(a).style.color='#ff0000';
		return false;
	 }
	 else
	 {
		document.getElementById(a).style.color='#000000';
		return true;
	 }
}

function checkTel(a)
{
	var input_data=document.getElementById(a).value;
	if ( !input_data.match( /^[0-9]{1,4}\s*[0-9]{3}\s*[0-9]{2,3}\s*[0-9]{2,3}$/ ) )
	{
		alert('Proszę podać poprawny numer telefonu.\nNp.: 012 234 56 78\nlub: 0 123 456 789');
		document.getElementById(a).style.color='#ff0000';
		return false;
	}
	
	document.getElementById(a).style.color='#000000';
	return true;
}

