// JavaScript Document
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function CheckRecoverForm()
{
	var brakuje_danych = false;
	var napis='';
	if((document.getElementById('login').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie podano loginu\n';
	}
	if (!brakuje_danych)
		return true;
	else
	{
		alert (napis);
		return false;
	}
}

function checkOrderForm()
{
	var brakuje_danych = false;
	var napis='';
	if((document.getElementById('pracownik').value)=='0')
	{
		brakuje_danych = true;
		napis+='Proszę wybrać pracownika\n';
	}
	if (!brakuje_danych)
		return true;
	else
	{
		alert (napis);
		return false;
	}
}

function checkRegForm(edit)
{
	var brakuje_danych = false;
	var napis='';
	if((document.getElementById('name').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano nazwy\n';
	}
	if(document.getElementById('branche').value=="0")
	{
        brakuje_danych = true;
        napis+='Proszę wybrać branżę\n';
    }
	if(document.getElementById('branche_activity').value=="0")
	{
        brakuje_danych = true;
        napis+='Proszę wybrać rodzaj działalności\n';
    }
	if(edit==0 && (document.getElementById('pass').value).trim()!=(document.getElementById('pass2').value).trim())
	{
		brakuje_danych = true;
		napis+='Podane hasła są różne\n';
	}
	if((document.getElementById('email').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano adresu e-mail\n';
	}
	else
	{
		if(check_email(document.getElementById('email').value)==false)	
		{
			brakuje_danych = true;
			napis+='Niepoprawna forma adresu e-mail\n';
		}        
    }
	if((document.getElementById('country').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano kraju\n';
	}
	if((document.getElementById('city').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano miasta\n';
	}        
	if((document.getElementById('street').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano ulicy\n';
	}
	if((document.getElementById('postcode').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano kodu pocztowego\n';
	}
	if((document.getElementById('street_number').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano numeru domu\n';
	}
	if((document.getElementById('phone').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano numeru telefonu\n';
	}
	else
	{
        if(check_number('phone')<7)
        {
            brakuje_danych = true;
            napis+='Niepoprawny numeru telefonu\n';
        }
    }
	if((document.getElementById('nip').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano NIPu\n';
	}
	else
	{
        if(!check_is_number(document.getElementById('nip').value, false))
        {
            brakuje_danych = true;
            napis+='Niepoprawny format NIPu, proszę wpisać tylko cyfry\n';
        }
        if(check_number('nip')!=10)
        {
            brakuje_danych = true;
            napis+='Niepoprawny format NIPu\n';
        }
    }
    if(edit)
    {
    	if((document.getElementById('person_name').value).trim()=='')
    	{
    		brakuje_danych = true;
    		napis+='Nie wpisano imienia osoby kontaktowej\n';
    	}
    	if((document.getElementById('person_surname').value).trim()=='')
    	{
    		brakuje_danych = true;
    		napis+='Nie wpisano nazwiska osoby kontaktowej\n';
    	}
    	if((document.getElementById('person_phone').value).trim()=='')
    	{
    		brakuje_danych = true;
    		napis+='Nie wpisano telefonu osoby kontaktowej\n';
    	}
    	else
    	{
            if(check_number('person_phone')<7)
            {
                brakuje_danych = true;
                napis+='Niepoprawny numeru telefonu osoby kontaktowej\n';
            }            
        }
    	if((document.getElementById('person_mobile').value).trim()=='')
    	{
    		brakuje_danych = true;
    		napis+='Nie wpisano telefonu komórkowego osoby kontaktowej\n';
    	}
    	else
    	{
            if(check_number('person_mobile')<9)
            {
                brakuje_danych = true;
                napis+='Niepoprawny numeru telefonu komórkowego osoby kontaktowej\n';
            }            
        }
    	if((document.getElementById('person_email').value).trim()=='')
    	{
    		brakuje_danych = true;
    		napis+='Nie wpisano adresu e-mail osoby kontaktowej\n';
    	}
    	else
    	{
    		if(check_email(document.getElementById('person_email').value)==false)	
    		{
    			brakuje_danych = true;
    			napis+='Niepoprawna forma adresu e-mail osoby kontaktowej\n';
    		}        
        }
    	if(document.getElementById('reg').checked == false)
    	{
    		brakuje_danych = true;
    		napis+='Musisz zapoznać się z regulaminem\n';
    	}
    }   
	if (!brakuje_danych)
		return true;
	else
	{
		alert (napis);
		return false;
	}
	
}

function checkPersonForm(edit)
{
	var brakuje_danych = false;
	var napis='';
	if((document.getElementById('name').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano imienia\n';
	}
	if((document.getElementById('surname').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano nazwiska\n';
	}
	if(document.getElementById('branche').value=="0")
	{
        brakuje_danych = true;
        napis+='Proszę wybrać branżę\n';
    }
	if(document.getElementById('branche_activity').value=="0")
	{
        brakuje_danych = true;
        napis+='Proszę wybrać rodzaj działalności\n';
    }
	if(edit==0 && (document.getElementById('pass').value).trim()!=(document.getElementById('pass2').value).trim())
	{
		brakuje_danych = true;
		napis+='Podane hasła są różne\n';
	}
	if((document.getElementById('email').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano adresu e-mail\n';
	}
	else
	{
		if(check_email(document.getElementById('email').value)==false)	
		{
			brakuje_danych = true;
			napis+='Niepoprawna forma adresu e-mail\n';
		}        
    }
	if((document.getElementById('country').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano kraju\n';
	}
	if((document.getElementById('city').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano miasta\n';
	}        
	if((document.getElementById('street').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano ulicy\n';
	}
	if((document.getElementById('postcode').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano kodu pocztowego\n';
	}
	if((document.getElementById('street_number').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano numeru domu\n';
	}
	if((document.getElementById('phone').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano numeru telefonu\n';
	}
	else
	{
        if(check_number('phone')<7)
        {
            brakuje_danych = true;
            napis+='Niepoprawny numeru telefonu\n';
        }
    }
    if(edit)
    {
    	if((document.getElementById('mobile').value).trim()=='' && edit)
    	{
    		brakuje_danych = true;
    		napis+='Nie wpisano telefonu komórkowego\n';
    	}
    	else
    	{
            if(check_number('mobile')<9)
            {
                brakuje_danych = true;
                napis+='Niepoprawny numeru telefonu komórkowego\n';
            }            
        }
       	if(document.getElementById('reg').checked == false)
    	{
    		brakuje_danych = true;
    		napis+='Musisz zapoznać się z regulaminem\n';
    	}
    }
	if((document.getElementById('pesel').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano PESELUu\n';
	}
	else
	{
        if(!check_is_number(document.getElementById('pesel').value, false))
        {
            brakuje_danych = true;
            napis+='Niepoprawny format PESELu, proszę wpisać tylko cyfry\n';
        }
        if(check_number('pesel')!=11)
        {
            brakuje_danych = true;
            napis+='Niepoprawny format PESELu\n';
        }
    }
	if (!brakuje_danych)
		return true;
	else
	{
		alert (napis);
		return false;
	}
	
}

function checkAddForm()
{
    var brakuje_danych = false;
    var napis='';
    var pola = new Array("user_name", "user_surname", "user_phone", "user_mobile", "user_email");
    var komunikaty = new Array("Nie wpisano imienia\n", "Nie wpisano nazwiska\n", "Nie wpisano numeru telefonu\n", "Nie wpisano numeru telefonu komórkowego\n",
                    "Nie wpisano adresu e-mail\n");
    for(i=0;i<pola.length;i++)
    {
        if((document.getElementById(pola[i]).value).trim()=='')
        {
            brakuje_danych = true;
            napis+=komunikaty[i];
        }
    }
    var pola_dlugosc = new Array("user_phone", "user_mobile");
    var pola_dlugosc_wymagana = new Array(7,9);
    var komunikaty_dlugosc = new Array("Niepoprawny numer telefonu\n", "Niepoprawny numer telefonu komórkowego\n");
    for(i=0;i<pola_dlugosc.length;i++)
    {
        if(check_number(pola_dlugosc[i])<pola_dlugosc_wymagana[i])
        {
            brakuje_danych = true;
            napis+=komunikaty_dlugosc[i];
        }
    }
	if(check_email(document.getElementById('user_email').value)==false)	
	{
		brakuje_danych = true;
		napis+='Niepoprawna forma adresu e-mail\n';
	}         
	if (!brakuje_danych)
		return true;
	else
	{
		alert (napis);
		return false;
	}
}

function checkInviteForm()
{
    var brakuje_danych = false;
    var napis='';
    var pola = new Array("users", "to", "to_email");
    var komunikaty = new Array("Nie wybrano użytkownika\n", 
                    "Nie wpisano imienia i nazwiska osoby zapraszanej\n", "Nie wpisano adresu e-mail osoby zapraszanej\n");
    for(i=0;i<pola.length;i++)
    {
        if((document.getElementById(pola[i]).value).trim()=='')
        {
            brakuje_danych = true;
            napis+=komunikaty[i];
        }
    }
	if(check_email(document.getElementById('to_email').value)==false)	
	{
		brakuje_danych = true;
		napis+='Niepoprawna forma adresu e-mail osoby zapraszanej\n';
	}    
	if (!brakuje_danych)
		return true;
	else
	{
		alert (napis);
		return false;
	}
}

function checklogform()
{
	var brakuje_danych = false;
	var napis='';
	if((document.getElementById('login').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano loginu\n';
	}
	if((document.getElementById('password').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano hasła\n';
	}
	if (!brakuje_danych)
		return true;
	else
	{
		alert (napis);
		return false;
	}
}

function checkPhoneForm()
{
	var brakuje_danych = false;
	var napis='';
	if((document.getElementById('phone').value).trim()=='')
	{
		brakuje_danych = true;
		napis+='Nie wpisano numeru telefonu\n';
	}
	if (!brakuje_danych)
		return true;
	else
	{
		alert (napis);
		return false;
	}
}
function check_email(email) 
{
   if (email.match(/^[_.a-zA-Z0-9]+([\.%!][_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/))
   		return true;
   else
    	return false
}

function check_number(name)
{
   var ile;
   var k,i;
   var cyfry = "0123456789";
   var znak;
   k = 0;
   tekst = (document.getElementById(name).value);
   for(i=0;i<tekst.length;i++)
   {
      znak = tekst.charAt(i); 
      if (cyfry.indexOf(znak) != -1) 
         {
            k++;
         }
   }
   return k;
}

function check_is_number(number_to_check,is_float)
{
	number_to_check=number_to_check.replace(',','.');
	if (isNaN(number_to_check))
	{					
		return false; 				
	}
	else
	{
		if (is_float==false)
		{
			if (Math.floor(number_to_check)==number_to_check)
			{
				if 	(number_to_check<1)
					return false; 				
				else
					return true; 
			}
			else
				return false;
		}
		if 	(number_to_check<0)
			return false; 				
		else
			return true; 

	}
	
}

