function IsZipCode(zip)
{
	var regex_zip = /^[0-9]{2}-[0-9]{3}$/;
	 
	 return regex_zip.test(zip);
}

function IsEmail(src) 
{
  var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  return regex.test(src);
}


function valid_form()
{
	var valid = true;
             
  
   if (!$("#bAgree").attr("checked"))
   {                                         
    	valid = false;
    	$("#agree_label").addClass('error_label');
   }
   else
   {
   	$("#agree_label").removeClass('error_label');
   } 
    			  
   var bsex = $(':input[name=bSex]:checked');
   if (bsex.val()!=1 && bsex.val()!=2 )
   { 
   	$("#sex_label1").addClass('error_label');
    	$("#sex_label2").addClass('error_label');
    	valid = false;
   }
   else
   {
    	$("#sex_label1").removeClass('error_label');
    	$("#sex_label2").removeClass('error_label');
   }
   
   var name = $(':input[name=bAdr1]');
   if (name.val()=='Imię' || name.val()=='')
   {
 		name.addClass('invalid');
 	   if (name.val()=='') name.attr('value','Imię');
		valid = false;            	
   }
   else
   {
      name.removeClass('invalid');
   }
          
   var secondname = $(':input[name=bAdr2]');  
   if(secondname.val()=='Nazwisko' || secondname.val()=='')
   {
 		secondname.addClass('invalid');
 		if (secondname.val()=='') secondname.attr('value','Nazwisko');
		valid = false;            	
   }
   else
   {
      secondname.removeClass('invalid');
   }
             
   var tel = $(':input[name=bTelephone]');
   if(tel.val()=='')
   {                                          
    	tel.attr('value','Numer telefonu');
   }
     
   var address = $(':input[name=bAdr4]');        
   if(address.val()=='Ulica (nr domu i mieszkania)' || address.val()=='')
   {
 		address.addClass('invalid');
 		if (address.val()=='') address.attr('value','Ulica (nr domu i mieszkania)');
		valid = false;            	
   }
   else
   {
      address.removeClass('invalid');
   }
          
   var city = $(':input[name=bAdr5]'); 
   if(city.val()=='Miejscowość' || city.val()=='')
   {
 		city.addClass('invalid');
 		if (city.val()=='') city.attr('value','Miejscowość');
		valid = false;            	
   }
   else
   {
     	city.removeClass('invalid');
   }
           
   var email = $(':input[name=bEmail]'); 
   if((email.val()=='E-mail') || (!IsEmail(email.val())))
   {
 		email.addClass('invalid');
 		if (email.val()=='') email.attr('value','E-mail');
		valid = false;            	
  	}
   else
   {
       email.removeClass('invalid');
   }
     
   var code = $(':input[name=bZip]');      
   if(code.val()=='Kod pocztowy' || code.val()=='' || (!IsZipCode(  code.val()  )) )
   {
 		code.addClass('invalid');
 		if (code.val()=='') 
 		{
 			code.attr('value','Kod pocztowy');
 		}
		valid = false;            	
   }
   else
   {
      code.removeClass('invalid');
   }
	
	
	return valid; 

}

   
$(document).ready(function(){
   	
	var name = $(':input[name=bAdr1]');
	name.focusout(function () {
		if (name.val()=='') name.attr('value','Imię');
	});
   			 
	var secondname = $(':input[name=bAdr2]');
	secondname.focusout(function () {
		if (secondname.val()=='') secondname.attr('value','Nazwisko');
	});
   			
	var tel = $(':input[name=bTelephone]');
	tel.focusout(function () {
 	  if (tel.val()=='') tel.attr('value','Numer telefonu');
	});
   			 
	var address = $(':input[name=bAdr4]');
	address.focusout(function () {
		if (address.val()=='') address.attr('value','Ulica (nr domu i mieszkania)');
	});
   			 
	var city = $(':input[name=bAdr5]');
	city.focusout(function () {
   	if (city.val()=='') city.attr('value','Miejscowość');
	});
   			 
	var email = $(':input[name=bEmail]');
	email.focusout(function () {
 	  if (email.val()=='') email.attr('value','E-mail');
	});
   			 
	var code = $(':input[name=bZip]');
	code.focusout(function () {
	  	if (code.val()=='') code.attr('value','Kod pocztowy');
	});
   			 
	$(':input[type!=radio][type!=checkbox]', this).click(function(){
 	  $(this).val('');
	});
  
}); 
