	
	var http_request = false;
	var isOk = 0;
	
	function check_username_exist() {
		//alert(document.getElementById("userName").value);
		if (document.getElementById("userName").value.length > 2) {
			var poststr = "&action=check_username_exist&user_name=" + escape(encodeURI(document.getElementById("userName").value));
			makePOSTRequest('index.php', poststr, 'return_username_exist');
		} else {
			isOk = 1;
			document.getElementById('user_name_req').style.color = '#FF0000';
			document.getElementById('user_name_req_text').style.color = '#FF0000';
			document.getElementById('user_name_req_text').innerHTML = 'Detta användarnamn är för kort';
		}
	}
	
	function return_username_exist() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
		
				result = http_request.responseText;
				//alert(result);
			
				if (result == '1') {
					//fel++;
					document.getElementById('user_name_req').style.color = '#FF0000';
					//alert("Detta användarnamn är upptaget o vi väntar lite");
					//return false;
					
					//username_exists = '1';
					document.getElementById('user_name_req_text').style.color = '#FF0000';
					document.getElementById('user_name_req_text').innerHTML = 'Detta användarnamn är tyvärr upptaget';
					
					isOk = 2;
					
				} else {
					document.getElementById('user_name_req').style.color = '#00FF00';
					document.getElementById('user_name_req_text').style.color = '#00FF00';
					document.getElementById('user_name_req_text').innerHTML = 'Ledigt användarnamn';
					
					isOk = 0;
					//alert('OK');
					//return true;
					
					//username_exists = '0';
				}
				
			} else {
				
				
				alert('There was a problem with the request.');
				
			}
		}
	}
	
	function check_tagnames() {
		sub_tag_suggestion();
		xx = document.getElementById('tag_name');
		//yy = document.getElementById('tag_name_trace');
		
		var poststr = "&action=look_for_tag_name&tag_name=" + escape(encodeURI(xx.value));
		makePOSTRequest('index.php', poststr, 'return_tagnames');
		/*
		if (xx.value.length > 8) {
			add_tag_suggestion(xx.value);
			
			yy.innerHTML = '';
			xx.value = '';
		}
		
		yy.innerHTML = xx.value;
		*/
	}
	
	function add_tag_suggestion(tag_name,tag_value) {
		var zz = document.getElementById('tag_name_suggestions');
		
		try{
			zz.add(new Option(tag_name, tag_value), null) //add new option to end of "sample"
			//zz.add(new Option(tag_name, "0"), zz.options[0]) //add new option to beginning of "sample"
		}
		catch(e){ //in IE, try the below version instead of add()
			zz.add(new Option(tag_name, tag_value)) //add new option to end of "sample"
			//zz.add(new Option(tag_name, "0"), 0) //add new option to beginning of "sample"
		}
	}
	
	function sub_tag_suggestion() {
		var zz = document.getElementById('tag_name_suggestions');
		
		var antal = zz.length;
		for (i=0;i<antal;i++) {
			zz.remove(zz.length-1);
		}
		
	}
	
	function return_tagnames() {
		
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
		
				result = http_request.responseText;
				
				if (result.length > 0) {
					var tag_array = result.split(",");
					var antal_ord = tag_array.length;
					//alert(antal_ord);
					
					for (i=0;i<antal_ord;i++) {
						add_tag_suggestion(tag_array[i],tag_array[i]);
					}
					//alert(result);
					
				}
				
			} else {
				
				
				alert('There was a problem with the request.');
				
			}
		}
	}
	
	function makePOSTRequest(url, parameters, tillbaka) {
		
	
		//http_request = false;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				// set type accordingly to anticipated content type
				//http_request.overrideMimeType('text/xml');
				http_request.overrideMimeType('text/html');
			}
		} 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('Cannot create XMLHTTP instance');
			return false;
		}
		
		/*
		http_request.onreadystatechange = function() {
			//alert('hejsan');
			if(http_request.readyState==4){
				alert(http_request.responseText);
			}
		}
		*/
		if (tillbaka == 'return_username_exist') {
			http_request.onreadystatechange = return_username_exist;
		} else if (tillbaka == 'return_tagnames') {
			http_request.onreadystatechange = return_tagnames;
		}
		
		http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", parameters.length);
		http_request.setRequestHeader("Connection", "close");
		http_request.send(parameters);
	}
	
	

