jQuery.extend($.fn, {
	validate: function() {
		$('.email-validator', this).keyup(function(){
			$.validator_email_check(this);
		});
		$('.text-validator', this).keyup(function(){
			$.validator_text_check(this);
		}).blur(function(){
			$.validator_text_check(this);
		});
		$('.phone-validator', this).keyup(function(){
			$.validator_phone_check(this);
		});
		$('.postcode-validator', this).keyup(function(){
			$.validator_postcode_check(this);
		});
		$('.int-validator', this).keyup(function(){
			$.validator_int_check(this);
		});
		$('.float-validator', this).keyup(function(){
			$.validator_float_check(this);
		});
		$('.checkbox-validator', this).change(function(){
			$.validator_checkbox_check(this);
		});
		$('.select-validator', this).change(function(){
			$.validator_select_check(this);
		});
		$('.repeat-password-validator', this).keyup(function(){
			$.validator_repeat_password(this);
		});
		$('.ajax-exist-validator', this).keyup(function(){
			$.validator_ajax_exist_check(this);
		}).blur(function(){
			$.validator_ajax_exist_check(this);
		});
		
		$(this).submit(function (){
			return $.validator(this);
		});
	}
});
var classToRemove = '.system.positive, .system.negative, .system.loader';
//Wyświetlanie komunikatów
$.validatorOk = function( input, message ) {
	$(input).parent().nextAll(classToRemove).remove();
	$(input).parent().after('<span class="system positive">' + message + '</span>');
};
$.validatorError = function( input, message ) {
	$(input).parent().nextAll(classToRemove).remove();
	$(input).parent().after('<span class="system negative">' + message + '</span>');
};
$.validatorLoader = function( input, message ) {
	$(input).parent().nextAll(classToRemove).remove();
	$(input).parent().after('<span class="system loader">' + message + '</span>');
};
//Walidacja ajax
$.validator_ajax_exist_check = function( input ) {
	var type;
	var required = false;
	var minLenght = 6;
	var maxLenght = 100;
	var selfId = $('#selfId').val();
	if($(input).attr('alt')!='') {
		var opt = $(input).attr('alt').split(':');
		type = opt[0];
		required = opt[1]=='required';
		minLenght = opt[2]!=undefined?opt[2]:minLenght;
		maxLenght = opt[3]!=undefined?opt[3]:maxLenght;
	}
	if(required==true || $(input).attr('value')!='') {
		var	reS = /\s/gi;
		var checkStr = $(input).attr('value').replace(reS, "");
		if (checkStr.length==0) {
			$.validatorError( input, locale.RequiredFieldError );
			return false;
		} else if (checkStr.length < minLenght) {
			$.validatorError( input, 'Zbyt krótki tekst' );
			return false;
		} else if (checkStr.length > maxLenght) {
			$.validatorError( input, 'Zbyt długi tekst' );
			return false;
		} else {
			$.validatorLoader(input, 'Sprawdzanie czy istnieje');
			var response;
			$.ajax({
			      url: locale.AjaxURL + 'exist/type/' + type + '/value/' + checkStr + '/selfid/' + selfId,
			      global: false,
			      async: false,
			      type: "GET",
			      dataType: "json",
			      success: function(res){
					response=res;
			      }
			});
			if(response.exist) {
				$.validatorError( input, locale.ValueExist );
				return false;
			} else {
				$.validatorOk( input, 'OK' );
				return true;
			}			
		}
	} else {
		$(input).parent().nextAll('.system.positive, .system.negative').remove();
		return true;
	}
}

$.validator_ajax_exist = function( inputs ) {
	wynik = true;
	for (var i = 0; i<inputs.length; i++) {
		if ($.validator_ajax_exist_check( inputs[i] ) == false)
			wynik = false;
	}
	return wynik;
};

//Walidacja textu
$.validator_text_check = function( input ) {
	var required = false;
	var minLenght = 6;
	var maxLenght = 100;
	if($(input).attr('alt')!='') {
		var opt = $(input).attr('alt').split(':');
		required = opt[0]=='required';
		minLenght = opt[1]!=undefined?opt[1]:minLenght;
		maxLenght = opt[2]!=undefined?opt[2]:maxLenght;
	}		
	if(required==true || $(input).attr('value')!='') {
		var	reS = /\s/gi;
		var checkStr = $(input).attr('value').replace(reS, "");
		if (checkStr.length==0) {
			$.validatorError( input, locale.RequiredFieldError );
			return false;
		} else if (checkStr.length < minLenght) {
			$.validatorError( input, 'Zbyt krótki tekst' );
			return false;
		} else if (checkStr.length > maxLenght) {
			$.validatorError( input, 'Zbyt długi tekst' );
			return false;
		} else {
			$.validatorOk( input, 'OK' );
			return true;
		}
	} else {
		$(input).parent().nextAll('.system.positive, .system.negative').remove();
		return true;
	}
}
$.validator_text = function( inputs ) {
	wynik = true;
	for (var i = 0; i<inputs.length; i++) {
		if ($.validator_text_check( inputs[i] ) == false)
			wynik = false;
	}
	return wynik;
};

//Walidacja adresu Email
$.validator_email_check = function( input ) {
	reEmail = /^[a-z0-9_.-]+([_\\.-][a-z0-9]+)*@([a-z0-9_\.-]+([\.][a-z]{2,4}))+$/i;
	if($(input).attr('alt')=='required' || $(input).attr('value')!='') {
		if ($(input).attr('value').search(reEmail) == -1) {
			$.validatorError( input, 'Błędny adres email' );
			return false;
		} else {
			$.validatorOk( input, 'OK' );
			return true;
		}
	} else {
		$(input).parent().nextAll('.system.positive, .system.negative').remove();
		return true;
	}
}
$.validator_email = function( inputs ) {
	wynik = true;
	for (var i = 0; i<inputs.length; i++) {
		if ($.validator_email_check( inputs[i] ) == false)
			wynik = false;
	}
	return wynik;
};

//Walidacja numeru telefonu
$.validator_phone_check = function( input ) {
	rePhone = /^\(\d{3}\) \d{3}-\d{4}$/i;
	if($(input).attr('alt')=='required' || $(input).attr('value')!='') {
		if ($(input).attr('value').search(rePhone) == -1) {
			$.validatorError( input, 'Błędny numer telefonu' );
			return false;
		} else {
			$.validatorOk( input, 'OK' );
			return true;
		}
	} else {
		$(input).parent().nextAll('.system.positive, .system.negative').remove();
		return true;
	}
}
$.validator_phone = function( inputs ) {
	wynik = true;
	for (var i = 0; i<inputs.length; i++) {
		if ($.validator_phone_check( inputs[i] ) == false)
			wynik = false;
	}
	return wynik;
};

//Walidacja kodu pocztowego
$.validator_postcode_check = function( input ) {
	rePostcode = /^\d{2}-\d{3}$/i;
	if($(input).attr('alt')=='required' || $(input).attr('value')!='') {
		if ($(input).attr('value').search(rePostcode) == -1) {
			$.validatorError( input, 'Błędny kod pocztowy' );
			return false;
		} else {
			$.validatorOk( input, 'OK' );
			return true;
		}
	} else {
		$(input).parent().nextAll('.system.positive, .system.negative').remove();
		return true;
	}	
}
$.validator_postcode = function( inputs ) {
	wynik = true;
	for (var i = 0; i<inputs.length; i++) {
		if ($.validator_postcode_check( inputs[i] ) == false)
			wynik = false;
	}
	return wynik;
};

//Walidacja liczb całkowitych
$.validator_int_check = function( input ) {
	var	reInt = /^-?[0-9]{1,}$/i;
	if($(input).attr('alt')=='required' || $(input).attr('value')!='') {
		if ($(input).attr('value').search(reInt) == -1) {
			$.validatorError( input, 'To nie jest liczba całkowita' );
			return false;
		} else {
			$.validatorOk( input, 'OK' );
			return true;
		}
	} else {
		$(input).parent().nextAll('.system.positive, .system.negative').remove();
		return true;
	}
}
$.validator_int = function( inputs ) {
	wynik = true;
	for (var i = 0; i<inputs.length; i++) {
		if ($.validator_int_check( inputs[i] ) == false)
			wynik = false;
	}
	return wynik;
};

//Walidacja liczb zmiennoprzecinkowych
$.validator_float_check = function( input ) {
	var	reFloat = /^-?[0-9]{1,}[.]{1}[0-9]{1,}$/i;
	if($(input).attr('alt')=='required' || $(input).attr('value')!='') {
		$(input).attr('value', $(input).attr('value').replace(/,/, "."));
		if ($(input).attr('value').search(reFloat) == -1) {
			$.validatorError( input, 'To nie jest liczba zmiennoprzecinkowa' );
			return false;
		} else {
			$.validatorOk( input, 'OK' );
			return true;
		}
	} else {
		$(input).parent().nextAll('.system.positive, .system.negative').remove();
		return true;
	}
}
$.validator_float = function( inputs ) {
	wynik = true;
	for (var i = 0; i<inputs.length; i++) {
		if ($.validator_float_check( inputs[i] ) == false)
			wynik = false;
	}
	return wynik;
};

//Walidacja liczb zmiennoprzecinkowych
$.validator_checkbox_check = function( input ) {
	if ($(input).attr('checked') == false) {
		$.validatorError( input, 'Musisz zaznaczyć to pole' );
		return false;
	} else {
		$.validatorOk( input, 'OK' );
		return true;
	}
}
$.validator_checkbox = function( inputs ) {
	wynik = true;
	for (var i = 0; i<inputs.length; i++) {
		if ($.validator_checkbox_check( inputs[i] ) == false)
			wynik = false;
	}
	return wynik;
};
// Walidacja selecta
$.validator_select_check = function( select ) {
	var zeroInclude = true;
	if($(select).attr('alt')!=undefined) {
		var opt = $(select).attr('alt').split(':');
		zeroInclude = opt[0]=='zeroInclude';
	}
	if( (!zeroInclude && $(select).attr('value')==0) || $(select).attr('value')=='') {
		$.validatorError( select, 'Musisz wybrać opcje' );
		return false;
	} else {
		$.validatorOk( select, 'OK' );
		return true;
	}
}

$.validator_select = function( selects ) {
	wynik = true;
	for (var i = 0; i<selects.length; i++) {
		if($.validator_select_check( selects[i] )==false)
			wynik = false;
	}
	return wynik;
}
// Walidacja powtórzonego hasła
$.validator_repeat_password = function( input ) {
	var required = false;
	var minLenght = 6;
	var maxLenght = 100;
	if($(input).attr('alt')!='') {
		var opt = $(input).attr('alt').split(':');
		required = opt[0]=='required';
		minLenght = opt[1]!=undefined?opt[1]:minLenght;
		maxLenght = opt[2]!=undefined?opt[2]:maxLenght;
	}
	if ($(input).attr('value')=='' && required) {
		$.validatorError( input, 'To pole jest wymagane' );
		return false;
	} else if($(input).attr('value') != $('.pass-to-check').attr('value')) {
		$.validatorError( input, 'Podane hasło jest inne !!!' );
		return false;
	} else {
		$.validatorOk( input, 'OK' );
		return true;
	}
}

//Klasa walidatora
$.validator = function( form ) {
	var wynik = true;
	if ($.validator_email($('.email-validator', $(form))) == false)
		wynik = false;
	if ($.validator_text($('.text-validator', $(form))) == false)
		wynik = false;
	if ($.validator_phone($('.phone-validator', $(form))) == false)
		wynik = false;
	if ($.validator_postcode($('.postcode-validator', $(form))) == false)
		wynik = false;
	if ($.validator_int($('.int-validator', $(form))) == false)
		wynik = false;
	if ($.validator_float($('.float-validator', $(form))) == false)
		wynik = false;
	if ($.validator_checkbox($('.checkbox-validator', $(form))) == false)
		wynik = false;
	if ($.validator_select($('.select-validator', $(form))) == false)
		wynik = false;
	//if ($.validator_repeat_password($('.repeat-password-validator')) == false)
		//wynik = false;
	if ($.validator_ajax_exist($('.ajax-exist-validator')) == false)
		wynik = false;
	
	if(!wynik) {
		$("#form_error_dialog").dialog({
			bgiframe: true,
			modal: true,
			closeOnEscape: false,
			buttons: {
				Ok: function() {
					$(this).dialog('destroy');
				}
			},
			close: function(event, ui) {
				$(this).dialog('destroy');
			}

		});
	}
	
	return wynik;
};

function confirm(a) {
	$("#form_confirm_dialog").dialog({
		bgiframe: true,
		modal: true,
		closeOnEscape: false,
		close: function(event, ui) {
			$(this).dialog('destroy');
		}

	});
	eval('$(\'#form_confirm_dialog\').dialog(\'option\', \'buttons\', {'+locale.Yes+' : function() {document.location=$(a).attr(\'href\');}, '+locale.No+' : function() {$(this).dialog(\'destroy\');} });');
	return false;
}
