function getHost() {
	var a = document.URL.split("//"); // split at protocol
	a = (a[1] ? a[1] : a[0]).split("/");
	return a[1];
}
 
var req_path = (document.location.host == 'www.effinfunny.com') ? ( '/' ) : ( '/' + getHost() + '/' );

if (Drupal.jsEnabled) {
   $(document).ready(clickInfoAutoAttach);
}

function clickInfoAutoAttach() {
	var obj1 = document.getElementById('edit-name'); 
	obj1.onfocus = function(){
		obj.innerHTML = 'check availability';	
	};
	
	var obj = document.getElementById('check_username');
	uri = req_path + 'check_username/example_handler/';
	
	var fn = function(){
		if(document.getElementById('edit-name').value == ''){
			alert('Enter a Username');
			document.getElementById('edit-name').focus();
			return false;
		}else{
			var username = document.getElementById('edit-name').value;
			$.ajax({
			  type: "GET",
			  url: uri + encodeURIComponent(username),
			  success: function (data) {
				// Parse back result
				if(data == 'available'){
					document.getElementById('edit-name').style.border = '2px solid green';
					obj.innerHTML = '\''+ username + '\' is available. Go ahead!';	
				}else{
					document.getElementById('edit-name').style.border = '2px solid red';
					obj.innerHTML = '\''+ username + '\' is already taken. Try again!';		
				}
			  },
			  error: function (xmlhttp) {
				alert('An HTTP error '+ xmlhttp.status +' occured.\n'+ uri);
			  }
			});
		}
	}
	
	obj1.onblur = fn;
}

