function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_type = new XMLHttpRequest();
	}
	return request_type;
}
var http = createObject();

function submitLogin() {	
	var username = encodeURI(document.getElementById('usernameLogin').value);
	var password = encodeURI(document.getElementById('passwordLogin').value);

	if (username == ""){
		alert('Please fill in your username.');
	} else if (password == ""){
		alert('Please fill in your password.');
	} else {
		nocache = Math.random();
		http.open('get', 'ajax/login.php?username='+username+'&password='+password+'&nocache='+nocache);
		http.onreadystatechange = loginReply;
		http.send(null);
	}
}

function submitRegister() {	
	var username = encodeURI(document.getElementById('usernameRegister').value);
	var password = encodeURI(document.getElementById('passwordRegister').value);
	var password2 = encodeURI(document.getElementById('passwordRegister2').value);
	
	if (username == ""){
		alert('Please fill in your username.');
	} else if (password == ""){
		alert('Please fill in your password.');
	} else if (password2 == ""){
		alert('Please repeat your password.');
	} else if (password != password2){
		alert('The passwords do not match.');
	} else {
		nocache = Math.random();
		http.open('get', 'ajax/register.php?username='+username+'&password='+password+'&nocache='+nocache);
		http.onreadystatechange = registerReply;
		http.send(null);
	}
}

function logout() {	
	  nocache = Math.random();
	  http.open('get', 'ajax/logout.php?nocache='+nocache);
	  http.onreadystatechange = logoutReply;
	  http.send(null);
	  alert('You have been logged out.');	  
}

function changePassword() {	
	var passwordchange = encodeURI(document.getElementById('passwordChange').value);
	var passwordchange2 = encodeURI(document.getElementById('passwordChange2').value);
	var passwordcurrent = encodeURI(document.getElementById('passwordCurrent').value);
	
	if (passwordchange == ""){
		alert('Please fill in your new password.');
	} else if (passwordchange2 == ""){
		alert('Please repeat your new password.');
	} else if (passwordcurrent == ""){
		alert('Please fill in your current password.');
	} else if (passwordchange != passwordchange2){
		alert('The new passwords do not match.');
	} else {
		nocache = Math.random();
		http.open('get', 'ajax/changepassword.php?passwordchange='+passwordchange+'&passwordcurrent='+passwordcurrent+'&nocache='+nocache);
		http.onreadystatechange = changeReply;
		http.send(null);
	}  
}

function addFavorite(videoID){
	nocache = Math.random();
	http.open('get', '../../../ajax/addfavorite.php?video='+videoID+'&nocache='+nocache);
	http.onreadystatechange = addFavoriteReply;
	http.send(null);
}

function removeFavorite(videoID){
	nocache = Math.random();
	http.open('get', '../../../ajax/removefavorite.php?video='+videoID+'&nocache='+nocache);
	http.onreadystatechange = removeFavoriteReply;
	http.send(null);
}

function report(videoID){
	var question = confirm('Do you want to report this video to the MobiFap team?');
	if (question){
		nocache = Math.random();
		http.open('get', '../../../ajax/report.php?video='+videoID+'&nocache='+nocache);
		http.onreadystatechange = reportReply;
		http.send(null);
	}
}

function loginReply() {	
	if(http.readyState == 4){
		var response = http.responseText;
		if(response == 3){
			alert('Login failed. Please check your username and password.');
		} else if(response == 2){
			alert('Please fill in your username & password.');
		} else if (response == 1){
			alert('Welcome to MobiFap. You have been logged in.');
			location.href="index.php";
		} else {
			alert('Error while logging in.');
		}
	}
}

function registerReply() {	
	if(http.readyState == 4){
		var response = http.responseText;
		if(response == 3){
			alert('This username is already taken, please think of something else.');
		} else if(response == 2){
			alert('Please fill in your username & password.');
		} else if (response == 1){
			alert('You have successfully registered at MobiFap and you have automatically been logged in.');
			location.href="index.php";
		} else {
			alert('Error while registering.');
		}
	}
}

function logoutReply() {	
	if(http.readyState == 4){
		var response = http.responseText;
		location.href="index.php";
	}
}

function reportReply() {	
	if(http.readyState == 4){
		var response = http.responseText;
		if (response == 2){
			alert('Incorrect video identifier.');
		} else if (response == 1){
			alert('The video has been reported to the MobiFap team.');
			window.close();
		} else {
			alert('Error while reporting this video.');
		}
	}
}

function addFavoriteReply() {	
	if(http.readyState == 4){
		var response = http.responseText;		
		if (response == 2){
			alert('You are not logged in. Log in or register at the index page to add favorites.');
		} else if (response == 3){
			alert('Incorrect video identifier.');
		} else if (response == 1){
			alert('Video added to your favorites.');
			window.close();
		} else {
			alert('Error while adding video to favorites.');
		}
	}
}

function removeFavoriteReply() {	
	if(http.readyState == 4){
		var response = http.responseText;	
		if (response == 2){
			alert('You are not logged in. Please log in or register at the index page.');
		} else if (response == 3){
			alert('Incorrect video identifier.');
		} else if (response == 1){
			alert('Video removed from your favorites.');
			window.close();
		} else {
			alert('Error while removing video from your favorites.');
		}
	}
}

function changeReply() {	
	if(http.readyState == 4){
		var response = http.responseText;
		if(response == 3){
			alert('There was an error changing your password.');
		} else if(response == 2){
			alert('Your current password is not correct.');
		} else if (response == 1){
			alert('Your password has been changed.');
			location.href="index.php";
		} else {
			alert('Error while changing your password.');
		}
	}
}

function enableSearch(){
	document.getElementById('rightButton').innerHTML = '<a class="button" href="#search" onclick="javascript:disableSearch();">Search</a>';
}

function disableSearch(){
	document.getElementById('rightButton').innerHTML = '';
}

