var http_request = false;
function checkField(field_id)
{
  //if IE - return false
  if(!!(window.attachEvent && !window.opera))
    return false;

	if(field_id == 're_email')
	{
		var hint_text = 'Podane adresy e-mail nie są jednakowe. Proszę sprawdzić i pooprawić.';
		if(document.getElementById('email').value != document.getElementById('re_email').value)
		{
 			document.getElementById(field_id).style.background = '#ffd1d1';
			if(document.getElementById(field_id+'_hint'))
				document.getElementById(field_id+'_hint').innerHTML = hint_text;
			else
			{
	   		var hint_node = document.createTextNode(hint_text);
  	 		var hint = document.createElement("span");
  	 		hint.appendChild(hint_node);
	  	 	hint.setAttribute('id', field_id+'_hint');
   			hint.setAttribute('class', 'error_hint');
   			hint.className = 'error_hint';
   			document.getElementById(field_id+'_div').appendChild(hint);
			}
		}
		else
		{
			document.getElementById(field_id).style.background = '#daf5c5';
			if(document.getElementById(field_id+'_hint'))
			{
				var hint_span = document.getElementById(field_id+'_hint');
				document.getElementById(field_id+'_div').removeChild(hint_span);
			}
		}
	}
	else
		if(field_id == 're_password')
		{
			var hint_text = 'Podane hasła nie są jednakowe. Proszę sprawdzić i pooprawić.';
			if(document.getElementById('password').value != document.getElementById('re_password').value)
			{
   			document.getElementById(field_id).style.background = '#ffd1d1';
				if(document.getElementById(field_id+'_hint'))
					document.getElementById(field_id+'_hint').innerHTML = hint_text;
				else
				{
		   		var hint_node = document.createTextNode(hint_text);
	  	 		var hint = document.createElement("span");
	  	 		hint.appendChild(hint_node);
		  	 	hint.setAttribute('id', field_id+'_hint');
	   			hint.setAttribute('class', 'error_hint');
          hint.className = 'error_hint';
 	   			document.getElementById(field_id+'_div').appendChild(hint);
				}
			}
			else
			{
				document.getElementById(field_id).style.background = '#daf5c5';
				if(document.getElementById(field_id+'_hint'))
				{
					var hint_span = document.getElementById(field_id+'_hint');
					document.getElementById(field_id+'_div').removeChild(hint_span);
				}
			}
		}
		else
			makeRequest('/ajax_func.php', 'page=registration&field_id='+field_id+'&value='+document.getElementById(field_id).value);
}

function makeRequest(url, data)
{
	http_request = false;
  if(window.XMLHttpRequest)
  { // Mozilla, Safari,...
  	http_request = new XMLHttpRequest();
    if(http_request.overrideMimeType)
    {
      http_request.overrideMimeType('text/xml');
    }
  }
  else if (window.ActiveXObject)
  { // IE
    try
    {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {}
    }
  }

  if(!http_request)
  {
    alert('Nie można nawiązać połączenia z serwerem.');
    return false;
  }
  http_request.onreadystatechange = resolveAnswer;
  http_request.open('POST', url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  http_request.send(data);
}

function resolveAnswer()
{
  //if IE - return false
  if(!!(window.attachEvent && !window.opera))
    return false;
  if(http_request.readyState == 4)
  {
	  if(http_request.status == 200)
	  {
			var xmldoc = http_request.responseXML;
			var root_node = xmldoc.getElementsByTagName('field').item(0);
      if(!root_node)
        return false;			
			var field_id = root_node.attributes[0].value;
	   	if(document.getElementById(field_id+'_hint'))
	   	{
	   		hint_span = document.getElementById(field_id+'_hint');
	   		if(trim(root_node.firstChild.data) == 'OK')
	   		{
		   		parent_node = document.getElementById(field_id+'_div');
	   			parent_node.removeChild(hint_span);
					document.getElementById(field_id).style.background = '#daf5c5';
	   		}
	   		else
	   		{
	   			hint_span.innerHTML = root_node.firstChild.data;
	   			document.getElementById(field_id).style.background = '#ffd1d1';
	   		}
	  	}
	   	else
	   	{
		   	if(trim(root_node.firstChild.data) != 'OK')
		   	{
		   	 	var hint = document.createElement("span");
		   		var hint_text = document.createTextNode(root_node.firstChild.data);
	  	 		hint.appendChild(hint_text);
		  	 	hint.setAttribute('id', field_id+'_hint');
	   			hint.setAttribute('class', 'error_hint');
	   			document.getElementById(field_id+'_div').appendChild(hint);
   				document.getElementById(field_id).style.background = '#ffd1d1';
		   	}
		   	else
		   	{
				document.getElementById(field_id).style.background = '#daf5c5';
		   	}
	   	}
    }
  }
}


