/* ---------------- AJAX related ---------------------- */
function makeRequest(url, parameters) {
	
	/* First we need to write a function which will call the server.
		It will create the XMLHttpRequest object. */
	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) {}
		}
	}
	
	/* Then we will open a connection to the web server. Third parameter is
		responsible for type of communication (synchronous — false, asynchronous — true).*/
	if (!http_request) {
		alert('ERROR AJAX:( Cannot create an XMLHTTP instance');
		return false;
	}
	
	/* Before sending the request you have to specify the name of a call
		back function which will receive results from the sever. */
	http_request.onreadystatechange = function() { 
		alertContents(http_request);
	};

	http_request.open('POST', url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

/* First readyState is check if the respond from the server was received.
	Then the http status of the respond have to be ok (200). */
function alertContents(http_request) {
	if (http_request.readyState == 4) {	
		if (http_request.status == 200) {						
			//TODO implement your function
			//alert(http_request.responseText); 
			//result = http_request.responseText;
			http_object = eval('(' + http_request.responseText + ')');
			if (http_object.param == 'username'){
				if (http_object.unique == 1)				
						document.getElementById('ajax_response').innerHTML = "";		
				else
					document.getElementById('ajax_response').innerHTML = "Korisnicko ime koje ste uneli je vec iskorisceno! Izaberite drugo.";
				document.getElementById('valid_username').value = http_object.unique;
			}
			else if (http_object.param == 'email'){
				if (http_object.unique == 1)				
						document.getElementById('ajax_response_email').innerHTML = "";		
				else
					document.getElementById('ajax_response_email').innerHTML = "Neko je vec registrovan pod tim e-mailom!";
				document.getElementById('valid_email').value = http_object.unique;
			}
		}
		else {
			alert('ERROR AJAX: There was a problem with the request.');
		}
	}
}

function get(obj, site_suffix) {
	if (obj.id == 'username'){
		var poststr = "username=" + encodeURI(document.getElementById("username").value) + "&site_suffix=" + encodeURI(site_suffix);
	}
	if (obj.id == 'email1'){
		var poststr = "email=" + encodeURI(document.getElementById("email1").value) + "&site_suffix=" + encodeURI(site_suffix);
	}
    makeRequest('ajax_details.php', poststr);
}