function validateRegistrationForm()
{
 	var returnVal = true;
 
	document.getElementById("usernameError").style.display = "none";
	document.getElementById("email1Error").style.display = "none";
	document.getElementById("email2Error").style.display = "none";
	document.getElementById("passwordError").style.display = "none";
	document.getElementById("password2Error").style.display = "none";
	document.getElementById("termsError").style.display = "none";
	
	if(document.getElementById("username").value == "")
	{
		document.getElementById("usernameError").style.display = "block";			
		returnVal = false;
	}

	if(document.getElementById("email1").value == "")
	{
		document.getElementById("email1Error").style.display = "block";			
		returnVal = false;
	}

	if(document.getElementById("email2").value == "")
	{
		document.getElementById("email2Error").style.display = "block";	
		returnVal = false;
	}
	
	if(document.getElementById("email2").value != document.getElementById("email1").value)
	{
		document.getElementById("email1Error").style.display = "block";		
		document.getElementById("email2Error").style.display = "block";		
		returnVal = false;
	}
	
	if(document.getElementById("password").value == "")
	{
		document.getElementById("passwordError").style.display = "block";	
		returnVal = false;
	}

	if(document.getElementById("password2").value == "")
	{
		document.getElementById("password2Error").style.display = "block";	
		returnVal = false;
	}
	
	if(document.getElementById("password2").value != document.getElementById("password").value)
	{
		document.getElementById("passwordError").style.display = "block";	
		document.getElementById("password2Error").style.display = "block";	
		returnVal = false;
	}

	if(document.registerForm.terms.checked == false)
	{
		document.getElementById("termsError").style.display = "block";		
		returnVal = false;
	}

	return returnVal;
	//return returnVal;
}

function checkOldPassword(password)
{
    var date = new Date();
    var xmlhttp;

    xmlhttp=GetXmlHttpObject()

    if (xmlhttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }

    var url="checkpassword.php";
    
    var parameters = "pw="+password+"&t="+date.getSeconds();

    xmlhttp.open("GET",url+"?"+parameters,false);
    xmlhttp.send(null);
	return xmlhttp.responseText;
//    document.getElementById("addpost").innerHTML=xmlhttp.responseText;
//	alert(xmlhttp.responseText);
}

function validateProfile()
{
 
	var oldPassword = document.getElementById("oldPassword").value;
	var password = document.getElementById("password1").value;
 	var password2 = document.getElementById("password2").value;

	if(oldPassword != "" || password != "" || password2 != "") 	
		returnVal = validatePasswordChange();
		
		
	returnVal = validatePersonDetails(returnVal);
	
	avatarUpload = document.getElementById("avatarUpload").value;
	if(avatarUpload != "")
		returnVal = validateAvatar(avatarUpload);
	return returnVal;
}

function validateAvatar(avatarUpload)
{
 	document.getElementById("avatarError").style.display = "none";
 	fileArray = avatarUpload.split(".");
 	var returnVal = true;	

 	if(fileArray[1] != "gif" && fileArray[1] != "GIF" && fileArray[1] != "jpg" && fileArray[1] != "JPG" && fileArray[1] != "png" && fileArray[1] != "PNG")
 	{
 	 	document.getElementById("avatarError").style.display = "block";
		document.getElementById("avatarError").innerHTML = "Incorrect file type";
	}
	return returnVal;
}

function validatePersonDetails()
{
	var firstname = document.getElementById("firstname").value;
	var surname = document.getElementById("surname").value;
	var email = document.getElementById("email1").value;
	
	document.getElementById("firstnameError").style.display = "none";
 	document.getElementById("surnameError").style.display = "none";
	document.getElementById("email1Error").style.display = "none";
	if(firstname == "")
	{
		document.getElementById("firstnameError").style.display = "block";
		document.getElementById("firstnameError").innerHTML = "Enter firstname";
		returnVal = false;
	}
	if(surname == "")
	{
		document.getElementById("surnameError").style.display = "block";
		document.getElementById("surnameError").innerHTML = "Enter surname";
		returnVal = false;
	}
	if(email == "")
	{
		document.getElementById("email1Error").style.display = "block";
		document.getElementById("email1Error").innerHTML = "Enter email address";
		returnVal = false;
	}
	return returnVal;
}

function validatePasswordChange()
{
 	var oldPassword = document.getElementById("oldPassword").value;
	var password = document.getElementById("password1").value;
 	var password2 = document.getElementById("password2").value;

 	document.getElementById("oldPasswordError").style.display = "none";
 	document.getElementById("password1Error").style.display = "none";
	document.getElementById("password2Error").style.display = "none";	   	 

	var returnVal = true;	
 	if(oldPassword == "")
 	{
 	 	document.getElementById("oldPasswordError").style.display = "block";
 	 	document.getElementById("oldPasswordError").innerHTML = "You must enter your old password";
		returnVal = false;
	}

	if(password == "")
	{

		document.getElementById("password1Error").style.display = "block";
 	 	document.getElementById("password1Error").innerHTML = "You must enter a new password ";
		returnVal = false;

	}

	if(password2 == "")
	{
		document.getElementById("password2Error").style.display = "block";
 	 	document.getElementById("password2Error").innerHTML = "You must confirm your new password ";
		returnVal = false;

	}
	
	if(password != "" && password2 != "" && password != password2)
 	{
  	 	document.getElementById("password2Error").style.display = "block";
 	 	document.getElementById("password2Error").innerHTML = "Passwords do not match";

		returnVal = false;
	}
	
	correctPassword = checkOldPassword(oldPassword);
	if(correctPassword != 1)
	{
		document.getElementById("password2Error").style.display = "none";
		document.getElementById("password1Error").style.display = "none";
		
		document.getElementById("oldPasswordError").style.display = "block";
 	 	document.getElementById("oldPasswordError").innerHTML = "Incorrect Password";
		returnVal = false;
	}
	
	return returnVal;
}

function showControlPanel()
{
	var date = new Date();
    var xmlhttp;

    xmlhttp=GetXmlHttpObject()

    if (xmlhttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }

    var url="controlpanel.php";
    
    var parameters = "t="+date.getSeconds();

    xmlhttp.open("GET",url+"?"+parameters,false);
    xmlhttp.send(null);
	
    document.getElementById("proCenreTitle").innerHTML = "Control Panel";

	document.getElementById("proCentre").innerHTML = xmlhttp.responseText;
	
}



function GetXmlHttpObject()
{
    var objXMLHttp=null
    if (window.XMLHttpRequest)
    {
        objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp
}






