/* Password validation */
var acceptedNewPwd = false;
var acceptedRepeatedPwd = false;

function emptyField(inputId){
	var id = document.getElementById(inputId);
	if(id.value.match(/^\s*$/)){
		return true;
	}
	else return false;
}

/* Checks input field for invalid characters */
function pwdValidCharacters(inputId){
	var id = document.getElementById(inputId);
	if(id.value.match(/^[A-Öa-ö0-9 _]*[A-Öa-ö0-9][A-Öa-ö0-9 _]*$/)){
		return true;
	}
	else return false;
}

/* Checks input field for minimum amount of characters(6) */
function pwdMininumValue(inputId){
	var id = document.getElementById(inputId);
	if(id.value.length > 5){
		return true;
	}
	else return false;
}

/* Checks if newPwd and repeatPwd are the same */
function pwdMatch(){
	var firstPwd = document.getElementById("newPwd").value;
	var secondPwd = document.getElementById("repeatPwd").value;
	if(firstPwd == secondPwd){
		return true;
	}
	else return false;
}

function validNewPwd(){
	var empty = emptyField("newPwd");
	var validchar = pwdValidCharacters("newPwd");
	var minimumchar = pwdMininumValue("newPwd");

	if (empty==false && validchar==true && minimumchar==true){
		document.getElementById("newPwd").className="Valid";
		$('.errorMessage[name="newPwd"]').animate({opacity:0}, 0);
		acceptedNewPwd=true;
	}
	else if(empty==true){
		acceptedNewPwd=false;
		document.getElementById("newPwd").className="";
		$('.errorMessage[name="newPwd"]').animate({opacity:0}, 0);
	}
	else if(empty==false && validchar==false || minimumchar==false){
		document.getElementById("newPwd").className="notValid";
		$('.errorMessage[name="newPwd"]').animate({opacity:1}, 0);
		acceptedNewPwd=false;
	}
}

function validRepeatedPwd(){
	var empty = emptyField("repeatPwd");
	var match = pwdMatch("repeatPwd");
	
	if(validNewPwd){
		if(empty==true){
			document.getElementById("repeatPwd").className="";
			$('.errorMessage[name="repeatPwd"]').animate({opacity:0}, 0);
			acceptedRepeatedPwd=false;
		}
		else if(empty==false && match==false){
			document.getElementById("repeatPwd").className="notValid";
			$('.errorMessage[name="repeatPwd"]').animate({opacity:1}, 0);
			acceptedRepeatedPwd=false;
		}
		else if(empty==false && match==true){
			document.getElementById("repeatPwd").className="Valid";
			$('.errorMessage[name="repeatPwd"]').animate({opacity:0}, 0);
			acceptedRepeatedPwd=true;
		}
	}
	else{
		document.getElementById("repeatPwd").className="";
	}
}

/* baseURL skickas med som parameter */
function confirm(baseURL){
	if(acceptedNewPwd && acceptedRepeatedPwd){
		changePwd(baseURL);
	}
	else{
		alert("WRONG WRONG WRONG!! FILL OUT THE FORM AGAIN YOU FKIN PIECE OF SHIT!");
		return false;
	}
}

function changePwd(baseURL)
{
	var currentPwd = document.getElementById("currentPwd").value;
	var newPwd = document.getElementById("newPwd").value;
	var hintPwd = document.getElementById("hintPwd").value;

	$.ajax({
		type: 'POST',
		url: baseURL+'ajax/pwdchange',
		data: { 
			currentPwd : currentPwd,
			newPwd : newPwd,
			hintPwd : hintPwd
		},
		success:  function(result){
			var resultObject = JSON.parse(result);
			if(resultObject.status==true){
				alert('Password change is successful!');
				$('#formContainer').fadeOut(300);
				$('#passwordChanger').fadeOut(300);
				$('body *').animate({
				opacity:1}, 400);
				$('#changePassword')[0].reset();
			}
			else{
				alert('Wrong password!');
				}
			}
	});
}

function passwordHint(baseURL){
	var userName = $("#username").val();
	
	$.ajax({
		type: 'POST',
		url: baseURL+'ajax/pwdhint',
		data: { 
			hintPwd : userName
		},
		success:  function(result){
			var resultObject = JSON.parse(result);
			var hinttext = document.getElementById('hinttext');

			if(resultObject.hint.length>0){

				hinttext.innerHTML = resultObject.hint;
			}
			else{
				hinttext.innerHTML = "User does not exist!";
				}
			}
	});
}