var xmlhttpRegister;

function GetXmlHttpObject(){
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	}
	if (window.ActiveXObject){// code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function passwordStrength(password){
	xmlhttpRegister=GetXmlHttpObject();
	
	if (xmlhttpRegister==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	$("#password_strength").show();
	
	var url="ajax/passwordStrength.php";
	params="password="+password+"&sid="+Math.random();
	xmlhttpRegister.onreadystatechange=function(){
		if (xmlhttpRegister.readyState!=4){
			document.getElementById("password_strength").innerHTML="<div class='load'></div>";
		}
		if (xmlhttpRegister.readyState==4){
			document.getElementById("password_strength").innerHTML=xmlhttpRegister.responseText;
			$("#password_strength").delay(3000).fadeOut(1000);
		}
	};
	xmlhttpRegister.open("POST",url,true);
	xmlhttpRegister.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttpRegister.setRequestHeader("Content-length", params.length);
	xmlhttpRegister.setRequestHeader("Connection", "close");
	xmlhttpRegister.send(params);
}

function validate(){
	username=document.getElementById("username").value;
	password=document.getElementById("password").value;
	password_confirmed=document.getElementById("password_confirmed").value;
	email=document.getElementById("email").value;
	
	xmlhttpRegister=GetXmlHttpObject();
	
	if (xmlhttpRegister==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="ajax/validate.php";
	params="username="+username+"&password="+password+"&password_confirmed="+password_confirmed+"&email="+email+"&sid="+Math.random();
	
	xmlhttpRegister.open("POST",url,false);//false for asynchronous requests. after send(), js tests if responseText is 1 or not 1
	xmlhttpRegister.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttpRegister.setRequestHeader("Content-length", params.length);
	xmlhttpRegister.setRequestHeader("Connection", "close");
	xmlhttpRegister.send(params);
	
	if(xmlhttpRegister.responseText==1){
		return true;//than move with the form to the specified action
	}
	else{
		document.getElementById("validate_results").style.display="block";
		document.getElementById("validate_results").innerHTML=xmlhttpRegister.responseText;
		return false;//stay with the form and display errors
	}
}

function validateForgotPassword(){
	forgot_email=document.getElementById("forgot_email").value;
	
	xmlhttpRegister=GetXmlHttpObject();
	
	if (xmlhttpRegister==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="ajax/validateForgotPassword.php";
	params="forgot_email="+forgot_email+"&sid="+Math.random();
	
	xmlhttpRegister.open("POST",url,false);//false for asynchronous requests. after send(), js tests if responseText is 1 or not 1
	xmlhttpRegister.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttpRegister.setRequestHeader("Content-length", params.length);
	xmlhttpRegister.setRequestHeader("Connection", "close");
	xmlhttpRegister.send(params);
	
	if(xmlhttpRegister.responseText==1){
		return true;//than move with the form to the specified action
	}
	else{
		document.getElementById("validate_results").style.display="block";
		document.getElementById("validate_results").innerHTML=xmlhttpRegister.responseText;
		return false;//stay with the form and display errors
	}
}

function validateLogin(){
	login_username=document.getElementById("login_username").value;
	login_password=document.getElementById("login_password").value;
	
	xmlhttpRegister=GetXmlHttpObject();
	
	if (xmlhttpRegister==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="ajax/validateLogin.php";
	params="login_username="+login_username+"&login_password="+login_password+"&sid="+Math.random();
	
	xmlhttpRegister.open("POST",url,false);//false for asynchronous requests. after send(), js tests if responseText is 1 or not 1
	xmlhttpRegister.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttpRegister.setRequestHeader("Content-length", params.length);
	xmlhttpRegister.setRequestHeader("Connection", "close");
	xmlhttpRegister.send(params);
	
	if(xmlhttpRegister.responseText==1){
		return true;//than move with the form to the specified action
	}
	else{
		document.getElementById("validate_results").style.display="block";
		document.getElementById("validate_results").innerHTML=xmlhttpRegister.responseText;
		return false;//stay with the form and display errors
	}
}

function validateElement(element,value){
	value=jQuery.trim(value);//trim white spaces from user's input
	if(value!=""){//try to validate element only if user has inputted something
	
		xmlhttpRegister=GetXmlHttpObject();
		
		if (xmlhttpRegister==null){
			alert ("Browser does not support HTTP Request");
			return;
		}
		
		var url="ajax/validateElement.php";
		if(element=="password_confirmed"){//if current field is the confirm password one, get the password field also to check if the 2 passwords match
			var password=document.getElementById("password").value;
			params="password="+password+"&"+element+"="+value+"&sid="+Math.random();
		}
		else//else, take only the current field
			params=element+"="+value+"&sid="+Math.random();
		
		xmlhttpRegister.open("POST",url,false);
		xmlhttpRegister.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttpRegister.setRequestHeader("Content-length", params.length);
		xmlhttpRegister.setRequestHeader("Connection", "close");
		xmlhttpRegister.send(params);
		
		if(xmlhttpRegister.responseText==1){
			document.getElementById("validate_"+element).setAttribute("class", "success");
			document.getElementById("validate_"+element).style.display="inline";
			document.getElementById("validate_"+element).innerHTML='<span class="pointer">&nbsp;</span>'+'OK';
			$("#validate_"+element).delay(3000).fadeOut(1000);
		}		
		else{
			document.getElementById("validate_"+element).setAttribute("class", "error");
			document.getElementById("validate_"+element).style.display="inline";
			document.getElementById("validate_"+element).innerHTML='<span class="pointer">&nbsp;</span>'+xmlhttpRegister.responseText;
			$("#validate_"+element).delay(3000).fadeOut(1000);
		}
	}
}

function elementInfo(element){
	
	document.getElementById("validate_"+element).removeAttribute("class"); //remove previous errors (if any) and show the normal tooltip
	
	xmlhttpRegister=GetXmlHttpObject();
	
	if (xmlhttpRegister==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="ajax/elementInfo.php";
	url=url+"?"+element+"&sid="+Math.random();
	
	xmlhttpRegister.open("GET",url,false);
	xmlhttpRegister.send(null);
	
	document.getElementById("validate_"+element).style.display="inline";
	document.getElementById("validate_"+element).innerHTML='<span class="pointer">&nbsp;</span>'+xmlhttpRegister.responseText;
	$("#validate_"+element).delay(3000).fadeOut(1000);
}
