var req;
var select;

function changeSkillType(SKILL)
{
	var url = "ajaxrequest.html?skill=" + encodeURIComponent(SKILL.options[SKILL.selectedIndex].value) +"&sid="+ Math.random();

    req = GetXmlHttpObject();
    
    req.open("GET", url, true);
    req.onreadystatechange = callback;
    select = SKILL;
    req.send(null);
}

function callback() 
{
    if (req.readyState == 4) 
    {
        if (req.status == 200) 
        {
        	var skillsResp = req.responseText.substr(0, req.responseText.length-3);
			var json_obiekt = eval('{[' + skillsResp + ']}');

            for (var i = 0; i < select.parentNode.parentNode.childNodes.length; i++)
            {
	            try
	            {
	            	var child = select.parentNode.parentNode.childNodes[i].firstChild;
	            	var childParent = child.parentNode;
	            	
					if (child.name == "nSkillName")
					{
						childParent.removeChild(child);
						var newSelect = document.createElement("select");
						newSelect.setAttribute("name", "nSkillName");
						newSelect.style.width = "100%";
						
						for (var y = json_obiekt.length - 1; y >= 0 ; y--)
						{
							var newOption = document.createElement("option");
							newOption.appendChild(document.createTextNode(json_obiekt[y]));
							newOption.setAttribute("value", json_obiekt[y]);
							newSelect.insertBefore(newOption, newSelect.firstChild);
						}
						
						if (json_obiekt.length == 0)
						{
							var newOption = document.createElement("option");
							newOption.appendChild(document.createTextNode("Wybierz typ"));
							newOption.setAttribute("value", "-1");
							newOption.setAttribute("selected", "selected");
							newSelect.insertBefore(newOption, newSelect.firstChild);
						}
						else
						{
							var newOption = document.createElement("option");
							newOption.appendChild(document.createTextNode("-"));
							newOption.setAttribute("value", "-1");
							newOption.setAttribute("selected", "selected");
							newSelect.insertBefore(newOption, newSelect.firstChild);
						}
						
						newSelect.selectedIndex = 0;
						childParent.insertBefore(newSelect, childParent.firstChild);
					}
				}
				catch(Exce)
				{
				}
            }
        }
        else
        {
        	alert("Request error status: "+ req.status);
        }
    }
    else
    {

    }
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	
	try
	{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	  // Internet Explorer
	  try
	  {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch (e)
	  {
	    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}
	
	if (xmlHttp == null)
	{
		alert("Your browser doesn't support AJAX!<br />Probably You have disabled JavaScript in Your browser.");
	}
	
	return xmlHttp;
}