// sortuje liste opcji w select, zachowuje powiazania wartosci z kluczami
function sort_list(lb)
{

	arrTexts = new Array();
	arrValues = new Array();
	arrOldTexts = new Array();
	for(i=0; i<lb.length; i++)
	{
		arrTexts[i] = lb.options[i].text;
		arrValues[i] = lb.options[i].value;
		arrOldTexts[i] = lb.options[i].text;
	}
	arrTexts.sort();
	for(i=0; i<lb.length; i++)
	{
		lb.options[i].text = arrTexts[i];
		for(j=0; j<lb.length; j++)
		{
			if (arrTexts[i] == arrOldTexts[j])
			{
				lb.options[i].value = arrValues[j];
				j = lb.length;
			}
		}
	}
}


// zwraca indeks na liscie elementu o podajej wartosci
function get_index(menu, value)
{
	result = -1;
	index = 0;
	while(index < menu.length && result == -1)
	if(menu[index].value == value)
		result = index;
	else
		index++;
	return result;
}


function URLEncode(clearString, custom) 
{
  if(custom)	// wlasna metoda zakodowania polskich znakow, uzywana wtedy kiedy standardowa nie dziala
  {
	  output = clearString;
	  output = output.replace(/ł/g, "*142");
	  output = output.replace(/ą/g, "*105");
	  output = output.replace(/ę/g, "*119");
	  output = output.replace(/ó/g, "*F3");
	  output = output.replace(/ś/g, "*15B");
	  output = output.replace(/ż/g, "*17C");
	  output = output.replace(/ź/g, "*17A");
	  output = output.replace(/ć/g, "*107");
	  output = output.replace(/ń/g, "*144");
	  
	  output = output.replace(/Ł/g, "*141");
	  output = output.replace(/Ą/g, "*104");
	  output = output.replace(/Ę/g, "*118");
	  output = output.replace(/Ó/g, "*D3");
	  output = output.replace(/Ś/g, "*15A");
	  output = output.replace(/Ż/g, "*17B");
	  output = output.replace(/Ź/g, "*179");
	  output = output.replace(/Ć/g, "*106");
	  output = output.replace(/Ń/g, "*143");
  }
  else
  {
  	  var output = '';
	  var x = 0;
	  clearString = clearString.toString();
	  var regex = /(^[a-zA-Z0-9_.]*)/;
	  while (x < clearString.length) {
	    var match = regex.exec(clearString.substr(x));
	    if (match != null && match.length > 1 && match[1] != '') {
	    	output += match[1];
	      x += match[1].length;
	    } else {
	      if (clearString[x] == ' ')
	        output += '+';
	      else {
	        var charCode = clearString.charCodeAt(x);
	        var hexVal = charCode.toString(16);
	        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
	      }
	      x++;
	    }
	  }
  }
  return output;
}

function URLDecode(encodedString) {
  var output = encodedString;
  /*var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
			 	alert('aaa');
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }*/

	output = output.replace(/%142/g, "ł");
	output = output.replace(/%105/g, "ą");
	output = output.replace(/%119/g, "ę");
	output = output.replace(/%F3/g, "ó");
	output = output.replace(/%15B/g, "ś");
	output = output.replace(/%17C/g, "ż");
	output = output.replace(/%17A/g, "ź");
	output = output.replace(/%107/g, "ć");
	output = output.replace(/%144/g, "ń");
	
	output = output.replace(/%141/g, "Ł");
	output = output.replace(/%104/g, "Ą");
	output = output.replace(/%118/g, "Ę");
	output = output.replace(/%D3/g, "Ó");
	output = output.replace(/%15A/g, "Ś");
	output = output.replace(/%17B/g, "Ż");
	output = output.replace(/%179/g, "Ź");
	output = output.replace(/%106/g, "Ć");
	output = output.replace(/%143/g, "Ń");
	
	output = output.replace(/\+/g, " ");

  return output;
}

function IsDigit( e )
{
	if ( !e )
		e = event ;

	var iCode = ( e.keyCode || e.charCode ) ;

	if( e.target ){
		var sel_start = e.target.selectionStart;
		var sel_end = e.target.selectionEnd;		
		e.target.value = e.target.value.replace(',','.');
		e.target.selectionStart = sel_start;
		e.target.selectionEnd = sel_end;
	} else if( e.srcElement ){
		var sel_start = e.srcElement.selectionStart;
		var sel_end = e.srcElement.selectionEnd;
		e.srcElement.value = e.srcElement.value.replace(',','.');
		e.srcElement.selectionStart = sel_start;
		e.srcElement.selectionEnd = sel_end;
	}
	return (
			( iCode >= 48 && iCode <= 57 )		// Numbers
			|| (iCode >= 37 && iCode <= 40)		// Arrows
			|| iCode == 8						// Backspace
			|| iCode == 46 || iCode == 44		// Delete, dot and comma
			|| iCode == 9						// Tab
	) ;
}


function dc_add_option(list_id, text, value, style)
{
	$(function()
	{
		option = "<option style='" + style + "' value='" + value + "'>" + text + "</option>";
		$('#' + list_id).append(option);
	});
	/*var elOptNew = document.createElement('option');
    elOptNew.text = text;
    elOptNew.value = value;
    var elSel = document.getElementById(list_id);
    try {
      elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
    }
    catch(ex) {
      elSel.add(elOptNew); // IE only
    }*/
}

