/* 
	Pomocnicze skrypty do formularzy formularzy
	Wersja: 1.0.0

  * o------------------------------------------------------------------------------o
  * | Biblioteki te zostały dołączone do oprogramowania tej strony na licencji     | 
  * | IT4Business i bez pisemnej zgody IT4Business nie mogą zostać użyte na        |
  * | żadnej innej stronie oraz w żadnej innej aplikacji. Po więcej szczegółów     |
  * | zapraszmy pod adres:                                                         |
  * |                                                                              |
  * |       http://it4business.pl/license.txt                                      |
  * |                                                                              |
  * o------------------------------------------------------------------------------o
  * 
  * o------------------------------------------------------------------------------o
  * | This package is licensed under the IT4Business license. A quick summary is   |
  * | that it is attached to this site as library and can not be used on other     |
  * | sites or application without permission of IT4Business. For more details     |
  * | please look at:                                                              |
  * |                                                                              |
  * |       http://it4business.pl/license.txt                                      |
  * |                                                                              |
  * o------------------------------------------------------------------------------o
  *
  * © Copyright IT4Business

*/  

/** Obsługa combobox'ów */
$(function(){
	// kliknięcie na combobox pokazuje podpowiedzi w selekcie
	$(".form input.combo").focus(function(){
		var comboselect = "#"+$(this).attr('id')+"_combo";
		$(comboselect).show();
	});

	// wpisanie czegoś w kontrolkę usuwa zaznaczenie z selecta
	$(".form input.combo").keydown(function(){
		var comboselect = "#"+$(this).attr('id')+"_combo";
		$(comboselect).find("option").removeAttr("selected");
	});

	// kliknięcie na podpowiedź przepisuje wartość do okienka
	$(".form .combobox_select select").click(function(event){
		var comboinput = "#"+$(this).attr('rel');
		$(comboinput).val($(event.target).filter(":selected").val());
	});

	// kliknięcie na dokument usuwa wszystkie podpowiedzi
	$(document).click(function(event){
		if(!$(event.target).hasClass('combo'))
			$(".form .combobox_select select").hide();
	});
});

/** Miernik jakości haseł */
$(function(){
	// mamy załadowaną bibliotekę do miernika?
	if(typeof  $("body").pstrength != "undefined")
	{
		// inicjuj
		$(".passwordMeter").pstrength();
	}
});


/** Obsługa przycisków plus / minus przy kontrolce plusminus */
$(function(){
	$(".form .value_control a.plus").click(function(e){
		var val = parseFloat($(this).parent().prev().val());
		$(this).parent().prev().val(val+1);
		e.preventDefault();
	});
	$(".form .value_control a.minus").click(function(e){
		var val = parseFloat($(this).parent().prev().val());
		if(val >= 1)
			$(this).parent().prev().val(val-1);
		e.preventDefault();
	});
});

/** Przepuszcza tylko wartości liczbowe 
 * @param e event
 * @return bool
*/
function onlyNumbers(e)
{ 
		var keynum;
		var keychar;
		var numcheck;

		if(window.event) // IE
		  keynum = e.keyCode
		else if(e.which) // Netscape/Firefox/Opera
		  keynum = e.which
		
		if (e.target) target = e.target;
		else if (e.srcElement) target = e.srcElement;


		keychar = String.fromCharCode(keynum);
		//alert(keynum);
		//alert(keychar);
		
		// zamień przecinek na kropkę
		if(keynum == 110 || keynum == 188)
		{
		  // dopisz kropkę i zablokuj przecinek
		  if(target.value.indexOf(".") == -1)
			target.value += ".";
		  return false;
		}

		// minus
		if(keynum == 109)
		  return true;
		// cyfry
		if(keychar >= '0' && keychar <= '9')
		return true;
		// kropka
		if(keynum == 190 && target.value.indexOf(".") == -1)
		  return true;
		// ctrl+v
		if(keynum == 86 || keynum == 17)
		  return true;
		// ctrl+c
		if(keynum == 67 || keynum == 17)
		  return true;
		// backspace / del
		if(keynum == 8 || keynum == 46)
		  return true;
		// home, end
		if(keynum == 35 || keynum == 36)
		  return true;
		// strzałki prawo / lewo
		if(keynum == 37 || keynum == 39)
		  return true;
		// klawiatura numeryczna
		if(keynum >= 96 && keynum <= 105)
		  return true;
		// enter
		if(keynum == '13')
		  return true;
		// tab
		if(keynum == '9')
		  return true;
		// ctrl
		if(keynum == '17')
		  return true;
		  
		// pozostałych nie akceptujemy
		return false;
}


/** Pobiera elementy grupy */
function getFormGroup(name)
{
	var tr = new Array();
	var parent = $("#"+name).parent().parent();

	// wloz p[ierwszy element <tr>
	tr.push(parent);
	cur = parent.next();

	// wloz kolejne az do napotkania kolejnej grupy
	while(cur.length > 0 && cur.find(".form_group").length == 0)
	{
		tr.push(cur);
		cur = cur.next();
	}

	return tr;
}

/** Ukrywa grupe */
function hideFormGroup(name)
{
	// pobierz elementy
	var tr = getFormGroup(name);

	// ukryj kolejno
	for(var i=0; i<tr.length; i++)
		tr[i].hide();
}

/** Pokazuje grupe */
function showFormGroup(name)
{
	// pobierz elementy
	var tr = getFormGroup(name);

	// pokaz kolejno
	for(var i=0; i<tr.length; i++)
		tr[i].show();

}
