$("#bbraunFlash").ready(function() {
	swfobject.embedSWF("swf/bbraun.swf", "bbraunFlash", "370", "120", "8.0.0", "");
});

var baseurl = '';
var password_format = /^[A-Za-z0-9_-]{3,16}$/;
var email = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var nip_format1 = /^[0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2}$/;
var nip_format2 = /^[0-9]{3}-[0-9]{2}-[0-9]{2}-[0-9]{3}$/;
var price_format = /^[0-9]+(\.{1}[0-9]{2}){0,1}$/;
var postcode_format = /^[0-9]{2}-[0-9]{3}$/;

jQuery.validator.addMethod( 
	"nip", 
	function(value, element) { 
		if(value!='' && !nip_format1.test(value) && !nip_format2.test(value)) 
		{
			return false; 
		} 
		else return true; 
	}, 
	"Wprowadzony numer NIP jest niepoprawny.<br />Poprawny format NIP xxx-xxx-xx-xx lub xxx-xx-xx-xxx"
);

jQuery.validator.addMethod( 
		"postcode", 
		function(value, element) {
			if(!postcode_format.test(value))
			{ 
				return false; 
			} 
			else return true; 
		}, 
		"Wprowadzony kod pocztowy jest niepoprawny.<br />Poprawny format 00-000"
	);

jQuery.validator.addMethod( 
	"price", 
	function(value, element) {
		if(!price_format.test(value))
		{ 
			return false; 
		} 
		else return true; 
	}, 
	"Wprowadzona liczba jest niepoprawna.<br />Poprawny format 100 lub 100.00"
);

var checklogin_result = true;

function empty(){ }

jQuery.validator.addMethod(
	"checklogin", 
	function(value, element) {
		$.post(globalBaseUrl+"scripts/checklogin.php", { login: value },	 
			function(data){	
				if(data > 0) 
					checklogin_result = false;
			    else 
			    	checklogin_result = true;
			}
		);
		setTimeout('empty()', 1500)
		return checklogin_result;
	}, 
	"Podany login jest już zajęty"
);

jQuery.validator.addMethod(
		"agree", 
		function(value, element) {
			if($('input#agree').is(':checked')) return true;
			else return false;
		}, 
		"Musisz wyrazić zgodę na przetwarzanie Twoich danych osobowych."
	);

jQuery.validator.addMethod(
		"other", 
		function(value, element) {
			if($("#answer6_5").is(':checked')){
				if($("#answer6_other").val()) 
					return true;
				else 
					return false;
			} else {
				return true;
			}
		}, 
		"Proszę podaj rodzaj zamówienia."
	);

$(document).ready(function(){
	
	$.post(globalBaseUrl+"scripts/message.php",	 
		function(data){	
			if(data.length > 3)
			$.prompt("EFAPEL KOMUNIKAT", data,{
			      buttons:{Ok:true},
			      prefix:'extblue'
			});
		}
	);
	
	
	$('a[rel*=lightbox]').lightBox();
	
	$('form#contact_form').validate({
		rules: {
			contact_email: {
				required: true,
				email: true
			},
			contact_question: {
				required: true
			}
		},
		messages: {
			title: {
				required: "Wpisz poprawny adres email",
				email: "Wpisz poprawny adres email"
			},
			footage: {
				required: "Wpisz treść wiadomości"
			}
		},
		debug: true,
		submitHandler: function(form) {
			$.post(globalBaseUrl+"scripts/contact.php", 
					{ 
						contact_email: $('input#contact_email').val(),
						contact_question: $('textarea#contact_question').val(),
						contact_phone: $('input#contact_phone').val()
					},	 
					function(data){	
						$.prompt("Formularz kontaktowy", data,{
						      buttons:{Ok:true},
						      prefix:'extblue',
						      callback: function(){ window.location.href=globalBaseUrl+'kontakt/'; }
						});
						
					}
				);
			return false;
		}
	});
	
	$('form#registration_form').validate({
		rules: {
			company_login: {
				required: true,
				remote: {
			        url: globalBaseUrl+"scripts/checklogin.php",
			        type: "post",
			        data: {
						login: function() {
			            	return $("input#company_login").val();
			          	}
			        }
				}
			},
			company_password: {
				required: true
			},
			company_password_confirm: {
				required: true,
				equalTo: "#company_password"
			},
			contact_name: {
				required: true
			},
			company_name: {
				required: true
			},
			company_city: {
				required: true
			},
			company_postcode: {
				required: true,
				postcode: true
			},
			company_street: {
				required: true
			},
			company_nr1: {
				required: true
			},
			company_nip: {
				required: true,
				nip: true
			},
			contact_email: {
				required: true,
				email: true
			},
			company_mobile: {
				required: true
			},
			agree: {
				agree: true
			}
		},
		messages: {			
			company_login: {
				required: "Proszę wpisz login użytkownika",
				remote: "Podany login jest już zajęty"
			},
			company_password: {
				required: "Proszę wpisz hasło użytkownika"
			},
			company_password_confirm: {
				required: "Proszę wpisz ponownie hasło użytkownika",
				equalTo: "Hasło oraz potwierdzenie hasła muszą być identyczne"
			},
			contact_name: {
				required: "Proszę wpisz imię i nazwisko"
			},
			company_name: {
				required: "Proszę wpisz nazwę firmy"
			},
			company_city: {
				required: "Proszę wpisz miasto, w którym znajduje się Twoja firma"
			},
			company_postcode: {
				required: "Proszę wpisz kod pocztowy",
				postcode: "Wprowadzony kod pocztowy jest niepoprawny.<br />Poprawny format 00-000"
			},
			company_street: {
				required: "Proszę wpisz ulicę, na której znajduje się Twoja firma"
			},
			company_nr1: {
				required: "Proszę wpisz nr budynku"
			},
			company_nip: {
				required: "Proszę wpisz numer NIP.<br />Poprawny format NIP xxx-xxx-xx-xx lub xxx-xx-xx-xxx",
				nip: "Proszę wpisz numer NIP.<br />Poprawny format NIP xxx-xxx-xx-xx lub xxx-xx-xx-xxx"
			},
			contact_email: {
				required: "Proszę wpisz poprawny email",
				nip: "Proszę wpisz poprawny email"
			},
			company_mobile: {
				required: "Proszę wpisz numer telefony komórkowego"
			}
		},
		debug: true,
		submitHandler: function(form) {
			$("#conctact_form_submit").hide();
			if($('input#location').attr('checked')){
				var location = 1;
			}else{
				var location = 0;				
			}
			$.post(globalBaseUrl+"scripts/adduser.php",	 
					{ 
						company_login: $('input#company_login').val(),
						company_password: $('input#company_password').val(),
						contact_name: $('input#contact_name').val(),
						company_name: $('input#company_name').val(),
						company_city: $('input#company_city').val(),
						company_postcode: $('input#company_postcode').val(),
						company_street: $('input#company_street').val(),
						company_nr1: $('input#company_nr1').val(),
						company_nr2: $('input#company_nr2').val(),
						company_nip: $('input#company_nip').val(),
						contact_email: $('input#contact_email').val(),
						company_mobile: $('input#company_mobile').val(),
						lat: $('input#lat').val(),
						lng: $('input#lng').val(),
						descr: $('textarea#descr').val(),
						location: location
					},	 
					function(data){	
						if(data=="OK"){
							$.prompt("Potwierdzenie rejestracji", "Zarejestrowałeś się pomyślnie w Strefie Elektryka Efapel<br /><br />Na podany adres e-mailowy wysłaliśmy informacje o Twoim koncie wraz z linkiem aktywacyjnym.",{
							      buttons:{Zamknij:true},
							      prefix:'extblue',
							      callback: function(){ window.location.reload(); }
							});
						}
					}
				);
				
						
			return false;
		}
	});
	
	$.metadata.setType("attr", "validate");
	
	$('form#survey_form input.first_radio').attr('validate','required:true');
	
	$('#survey_form').validate({
		rules: {
			answer6_other: {
				other: true
			}
		},
		messages: {
			answer6_other: {
				other: "<br />Proszę podaj inny sposób wysłania zamówienia"
			}
		},
		debug: true,
		submitHandler: function(form) {
			
			$('#survey_form_submit').hide();
			
			var a1 = $("input:checked[name='answer1']").val();
			var a2 =  $("input:checked[name='answer2']").val();
			var a3 =  $("input:checked[name='answer3']").val();
			var a4 =  $("input:checked[name='answer4']").val();
			var a5 =  $("input:checked[name='answer5']").val();
			var a6 =  $("input:checked[name='answer6']").val();
			var a7 = $("input:checked[name='answer7']").val();
			var a8 = $("input:checked[name='answer8']").val();
			var a9 = $("input:checked[name='answer9']").val();
			var a6_o = $("input#answer6_other").val();
			
			$.post(globalBaseUrl+"scripts/survey.php", 
					{ 
						answer1: a1,
						answer2: a2,
						answer3: a3,
						answer4: a4,
						answer5: a5,
						answer6: a6,
						answer7: a7,
						answer8: a8,
						answer9: a9,
						answer6_other: a6_o
					},	 
					function(data){	
						$.prompt("Potwierdzenie wysłania ankiety", data,{
						      buttons:{Ok:true},
						      prefix:'extblue',
						      callback: function(){ window.location.href=globalBaseUrl+'strefa_elektryka/'; }
						});
					}
				);
			return false;
		}
	});
});

