var xmlhttpUser;

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 hideElement(id){
	document.getElementById(id).style.display="none";	
}

function showElement(id){
	document.getElementById(id).style.display="inline";	
}

function user(){
	document.getElementById("dimBackground").style.display="block";
	document.getElementById("settings").style.display="block";
	//document.getElementById("settings").innerHTML="settings";
	
	xmlhttpUser=GetXmlHttpObject();
	
	if (xmlhttpUser==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="igluu/user/user.php";
	url=url+"?sid="+Math.random();
	xmlhttpUser.onreadystatechange=function(){
		if (xmlhttpUser.readyState!=4){
			document.getElementById("settings").innerHTML="<div class='load'></div>";
		}
		if (xmlhttpUser.readyState==4){
			document.getElementById("settings").innerHTML=xmlhttpUser.responseText;
			parseJavascript();
		}
	};
	xmlhttpUser.open("GET",url,true);
	xmlhttpUser.send(null);
}

function hideUser(){
	document.getElementById("settings").style.display="none";
	document.getElementById("dimBackground").style.display="none";
}

function updateUserField(field_name,value){
	
	xmlhttpUser=GetXmlHttpObject();
	
	if (xmlhttpUser==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="igluu/user/ajax/updateUserField.php";
	
	if(field_name=="password"){
		password_old=document.getElementById("password_old").value;
		password=document.getElementById("password").value;
		password_confirmed=document.getElementById("password_confirmed").value;
		
		url=url+"?field_name="+field_name+"&value="+value+"&value_old="+password_old+"&value_new="+password;
	}
	else{
		url=url+"?field_name="+field_name+"&value="+value;
	}
	
	url=url+"&sid="+Math.random();
	xmlhttpUser.onreadystatechange=function(){
		if (xmlhttpUser.readyState!=4){
			document.getElementById("validate_"+field_name).innerHTML="<div class='admin_load'></div>";
		}
		if (xmlhttpUser.readyState==4){
			//document.getElementById("validate_"+field_name).innerHTML=xmlhttpUser.responseText;
			if(field_name=="password")
				field_name="password_confirmed";
			if(xmlhttpUser.responseText==1){
				document.getElementById("validate_"+field_name).setAttribute("class", "success");
				document.getElementById("validate_"+field_name).style.display="inline";
				document.getElementById("validate_"+field_name).innerHTML='<span class="pointer">&nbsp;</span>'+'OK';
				if(field_name=="password_confirmed"){//then disable all password fields so that the user can not change the password too quick
					document.getElementById("password_old").value="";
					//$("#password_old").attr("disabled", "disabled").delay(1000).removeAttr("disabled");
					document.getElementById("password").value="";
					document.getElementById("password").setAttribute("disabled", "disabled");
					document.getElementById("password_confirmed").value="";
					document.getElementById("password_confirmed").setAttribute("disabled", "disabled");
				}
				$("#validate_"+field_name).delay(1000).fadeOut(1000);
			}
			else if(xmlhttpUser.responseText==0){
				document.getElementById("validate_"+field_name).setAttribute("class", "error");
				document.getElementById("validate_"+field_name).style.display="inline";
				document.getElementById("validate_"+field_name).innerHTML='<span class="pointer">&nbsp;</span>'+'Error';
				$("#validate_"+field_name).delay(3000).fadeOut(1000);
			}
			else{
				document.getElementById("validate_"+field_name).setAttribute("class", "error");
				document.getElementById("validate_"+field_name).style.display="inline";
				document.getElementById("validate_"+field_name).innerHTML='<span class="pointer">&nbsp;</span>'+xmlhttpUser.responseText;
				$("#validate_"+field_name).delay(3000).fadeOut(1000);
			}
		}
	};
	xmlhttpUser.open("GET",url,true);
	xmlhttpUser.send(null);
}

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

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
		
		xmlhttpUser=GetXmlHttpObject();
		
		if (xmlhttpUser==null){
			alert ("Browser does not support HTTP Request");
			return;
		}
		
		var url="igluu/user/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();
		
		xmlhttpUser.open("POST",url,false);
		xmlhttpUser.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttpUser.setRequestHeader("Content-length", params.length);
		xmlhttpUser.setRequestHeader("Connection", "close");
		xmlhttpUser.send(params);
		
		if(xmlhttpUser.responseText==1){
			if(element=="password")
				$("#password_strength").show();
			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);
			if(element=="password_old")
				document.getElementById("password").removeAttribute("disabled");
			if(element=="password")
				document.getElementById("password_confirmed").removeAttribute("disabled");
			if(element=="password_confirmed")
				updateUserField("password", value);
				
			if(element=="email")
				updateUserField(element, value);
		}		
		else{
			if(element=="password")
				$("#password_strength").show();
			document.getElementById("validate_"+element).setAttribute("class", "error");
			document.getElementById("validate_"+element).style.display="inline";
			document.getElementById("validate_"+element).innerHTML='<span class="pointer">&nbsp;</span>'+xmlhttpUser.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
	
	xmlhttpUser=GetXmlHttpObject();
	
	if (xmlhttpUser==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="igluu/user/ajax/elementInfo.php";
	url=url+"?"+element+"&sid="+Math.random();
	
	xmlhttpUser.open("GET",url,false);
	xmlhttpUser.send(null);
	
	if(element=="password")
		$("#password_strength").show();
	document.getElementById("validate_"+element).style.display="inline";
	document.getElementById("validate_"+element).innerHTML='<span class="pointer">&nbsp;</span>'+xmlhttpUser.responseText;
	$("#validate_"+element).delay(3000).fadeOut(1000);
}
