
/*
 * tymczasowe wartosci pól (placeholdery)
 *
 * $().inputPlaceholders({
			'selektor inputa':'placeholder text',
			'inny selektor':'inny text'
		});
 *
 * lub
 *
 * $('selektor inputa').inputPlaceholders('placeholder text')
 *
 */

jQuery.fn.inputPlaceholders = function(holders){
	if(typeof holders == 'object'){
		jQuery.each(holders, function(i, v){
			$(i).attr('value',v)
				.focus(function(){
					this.value = this.value == v ? '' : this.value
				})
				.blur(function(){
					this.value = this.value=='' ? v : this.value
				})
		});
	}
	else if(typeof holders == 'string'){
		$(this).attr('value',holders)
		$(this).focus(function(){
				this.value = this.value == holders ? '' : this.value
			})
			.blur(function(){
				this.value = this.value=='' ? holders : this.value
			})
	}
}

/*
 * validacja live
 *
 * $('input, select').validateLive(errors, validateOpts);
 *
*/

jQuery.fn.validateLive = function(errors, opts){
	opts = opts || {};
	var limit = opts['limit'] || 0;
	this.bind('keyup', function(){
		if($(this).val().length>=limit)$(this).parent().validate(errors, opts)
	}).bind('change', function(){
		if($(this).val().length>=limit)$(this).parent().validate(errors, opts)
	}).bind('blur', function(){
		if($(this).val().length>=limit)$(this).parent().validate(errors, opts)
	})
}

/*
 * validacja formularza
 *
 * $('form').submit( function(){ return $(this).validate(errors, validateOpts) })
 *
*/


jQuery.fn.validate = function(errors, opts){
	errors = errors || {};
	opts = opts || {};
	var errorcount = 0;

	opts = jQuery.extend({
		appendError : false,
		parentClass : 'error',
		errorWrapper : '<p class="error"></p>'
	}, opts);

	errors = jQuery.extend({
		required : 'to pole jest wymagane',
		requiredBox : 'musisz zaznaczyć to pole',
		requiredOne : 'musisz zaznaczyć przynajmniej jedną opcję',
		email : 'nieprawidłowy format adresu',
		number : 'należy wpisać wartość liczbową',
		notnumber : 'pole nie może zawierać cyfr',
		phone : 'nieprawidłowy format numeru',
		password : 'hasła nie są jednakowe'
	}, errors)


  	var frm = $(this);
  		var _tt = new Date();
	var errorSelector = (function(){ return $(opts.errorWrapper)[0].nodeName + ($(opts.errorWrapper).attr('class') ? ('.' + $(opts.errorWrapper).attr('class')) : '') })();

	//przy ponownej validacji wywala poprzednie komunikaty
	var removeErrors = function(){
		(opts.appendError) && frm.find(errorSelector).replaceWith('');
	  	(opts.parentClass) && frm.find('.'+opts.parentClass).removeClass(opts.parentClass);
	  		frm.removeClass(opts.parentClass);
	}
	removeErrors();
  	var displayError = function(elm, errorinfo){
  		if(opts.appendError){
  			$(elm).parent().append($(opts.errorWrapper).append(errorinfo))
  		}
  		if(opts.parentClass){
  			$(elm).parent().addClass(opts.parentClass);
  		}
  		errorcount++
  	}

	if(errors['ajax']){
		var inputs = frm.find(':input');
		var formdata = {};
		jQuery.each(inputs, function(){
			if(this.getAttribute('name')){
				if(this.getAttribute('type') && this.getAttribute('type').match(/checkbox|radio/i)){
					this.checked && (formdata[this.getAttribute('name')] = this.value);
				}
				else formdata[this.getAttribute('name')] = this.value;
			}
		});
		if(!window['__validator_holdon']){
			window['__validator_holdon'] = 1;
			window.setTimeout(function(){window['__validator_holdon']=undefined}, 600);
			jQuery.post(errors['ajax'], formdata, function(data){
				eval('var info = '+data);
				for(var i in info){
					displayError($('*[@name='+i+']'), info[i]);
					errorcount++
				}
				if(!errorcount) frm.unbind('submit').submit();
			})
		}
		return false;
	}
	else {

  		//pole wymagane
		frm.find('.required').each(
			function(){
				if(this.type=='checkbox'){
					(!this.checked) && displayError(this, errors[this.id] || errors['requiredBox'])
				}
				else if(this.nodeName.match(/input/i)||this.nodeName.match(/select/i)||this.nodeName.match(/textarea/i)){
					(this.value=='') && displayError(this, errors[this.id] || errors['required'])
				}
				else if($(this).find('input:checkbox')||$(this).find('input:radio')){
					if(!$(this).find('input:checked').length){
						displayError(this, errors[this.id] || errors['requiredOne']);
					}
				}
			}
		);
  		//telefon:
		frm.find('.phone').each(
			function(){
				(this.value!=''&&!/^[\+]?[\d\.\s\-()]+$/.test(this.value))
				&& displayError(this, errors[this.id] || errors['phone'])
			}
		);

		//HTML5 type=""
		frm.find('input').each(
			function(){
				// type="number"
				(this.getAttribute('type')=='number'||$(this).hasClass('number'))
				&& (this.value!=''&&!/^[\+\-]?\d*[\.,]?\d*$/.test(this.value))
				&& displayError(this, errors[this.id] || errors['number']);

				// type="notnumber"
				($(this).hasClass('notnumber'))
				&& (this.value!=''&&/\d*$/.test(this.value))
				&& displayError(this, errors[this.id] || errors['notnumber']);

				//type="email"
				(this.getAttribute('type')=='email'||$(this).hasClass('email'))
				&& (this.value!=''&&!/^[\w\.-_+]{1,}@[\w\.-]+\.[a-z]{2,5}$/.test(this.value))
				&& displayError(this, errors[this.id] || errors['email']);
			}
		);
		//powtorz haslo missmatch
		if(frm.find('input:password').length==2){
			if(frm.find('input:password:last').val() !='' && (frm.find('input:password:first').val() != frm.find('input:password:last').val())){
				displayError(frm.find('input:password:last'), errors[frm.find('input:password:last').attr('id')] || errors['password'] )
			}
		}


	}
	return errorcount ? false : true;

}
/*
 * podpowiedzi
 */
jQuery.fn.formTooltips = function(){

	jQuery.each(this, function(){
		$(this).bind('focus', function(){
			$(this).parent().find('.help').fadeIn()
		}).bind('blur', function(){
			$(this).parent().find('.help').fadeOut()
		})
	});
}