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 setInvalid(el) {
	$(el).parent().addClass('err');
	valid = false;
}

function IsTelNumber(tel) {
	var regex_tel = /^[0-9]{9}|(Numer telefonu)$/;
	return regex_tel.test(tel);
}


function validateForm() {
	valid = true;
	kom = '';
	
	var _el;
	$(".err").removeClass('err');
	
    _el = $('#bAdr1, #bAdr2, #bAdr4, #bAdr5, #bEmail, #bZip');
    _el.each(function(i, el){if($(el).val()==$(el).parent().children('span').html() || $(el).val()=='') setInvalid($(el));})
   	if(!valid) kom = 'Nie wszystkie pola zostały wypełnione. ';
   
	_el = $("#bAgree");
    if(!_el.attr("checked")) {setInvalid(_el); kom = kom + 'Aby wysłać formularz nalezy zaakceptować regulamin. ';}
	
    if(!$(":input[name=bSex]:checked").length > 0) {setInvalid($(":input[name=bSex]")); kom = kom + 'Nie została zaznaczona płeć. '; }
   
    _el = $('#bEmail');
    if(!IsEmail(_el.val())){ setInvalid(_el); kom = kom + 'Niepoprawny adres e-mail. '; }
	
    _el = $('#bZip');
    if(!IsZipCode(_el.val())) {setInvalid(_el);  kom = kom + 'Kod pocztowy jest niepoprawny. '; }
	
    _el = $('#bTelephone');
	if(!IsTelNumber(_el.val())) {setInvalid(_el);   kom = kom + 'Poprawny format numeru telefonu to 9 cyfr bez odstępów. '; }
	if(valid && _el.val()==_el.parent().children('span').html()) $('#bTelephone').val('');
	
		 if(!valid) 
	 	alert( kom )  

	return valid; 
}


function validateTellFriendForm() {
	valid = true;
	var _el;
	$(".err").removeClass('err');
	
    _el = $('input, textarea');
    _el.each(function(i, el){if($(el).val()=='') setInvalid($(el));})

    _el = $('input:[name=email]');
    if(!IsEmail(_el.val())) setInvalid(_el);
   
	return valid; 
}


function setLabel(el) {
	el = $(el);
	if(el.parent().children('span').text() && el.parent().children('span').text() != ''){
		el.val(el.parent().children('span').text());
		el.focus(function(){if($(this).val() == $(this).parent().children('span').text()) $(this).val('').addClass('usr');});
		el.focusout(function(){if($(this).val() == '') $(this).val($(this).parent().children('span').text()).removeClass('usr');});
	}
}
	
$(document).ready(function() {
	if($("body").hasClass('thp')){
		//$('#form form').submit(function(){return validateTellFriendForm();});
	}
	else {
		$('#form form').submit(function(){return validateForm();});
		$("#form input:[type=text]").each(function(i,el){setLabel(el);});
	}

	//$('#slide .button.l').click(function(){moveRight();});
	//$('#slide .button.r').click(function(){moveLeft();});
	//$('#slider ul a').lightbox();
}); 
