var global_key;
var global_key2;

function registration()
{
	if(xmlHttp==null)
	{
		var frmvalidator  = new Validator("regi");
		frmvalidator.addValidation("name","req","Please Enter Your Name");
		frmvalidator.addValidation("email","req","Please Enter Email Address");
		frmvalidator.addValidation("email","email","Please Enter Valid Email Address");
		frmvalidator.addValidation("address1","req","Please Enter Address Line 1");
		frmvalidator.addValidation("town","req","Please Enter Your Town / City");
		frmvalidator.addValidation("country","req","Please Enter Your Country");
		frmvalidator.addValidation("postcode","req","Please Enter Postcode");
		frmvalidator.addValidation("phone","req","Please Enter Phone Number");
		frmvalidator.addValidation("username","req","Please Enter Username");
		frmvalidator.addValidation("password","req","Please Enter Password");
		if(frmvalidator.validate() == true)
		{
			if(document.getElementById("password").value != document.getElementById("cpassword").value)
			{
				alert("Password and confirmation Password should be same");
			}
			else
			{
			var url="register.php?action=ajax&";
			url +=get_form_values(document.regi);
			xmlHttp=GetXmlHttpObject();
			xmlHttp.onreadystatechange=registration;
			xmlHttp.open("post",url,true);
			xmlHttp.send(null);
			document.getElementById("loader").style.display="block";
			}
		}
	}
	else
	{
		if (xmlHttp.readyState==4)
		{ 
			document.getElementById("loader").style.display="none";
			var result=xmlHttp.responseText;
			if(result == 1)
			{
				alert("Successfully registered! Please check your Email!");
				location.href="index.php";
			}
			else if(result == 2)
			{
				alert("Email Address already exist!");
			}
			else if(result == 3)
			{
				alert("Username already exist!");
			}
			else
			{
				ajax_error();
			}
			xmlHttp=null;
		}
	}
	return false;
}

function account()
{
	if(xmlHttp==null)
	{
		var frmvalidator  = new Validator("frm_account");
		frmvalidator.addValidation("name","req","Please Enter Your Name");
		if(frmvalidator.validate() == true)
		{
			if(document.getElementById("state").value == "")
			{
				document.getElementById("err_state").innerHTML="Please Select State";;
			}
			else if(document.getElementById("city").value == "")
			{
				document.getElementById("err_state").innerHTML="";;
				document.getElementById("err_city").innerHTML="Please Select City";;
			}
			else
			{
				document.getElementById("err_state").innerHTML="";;
				document.getElementById("err_city").innerHTML="";;
				var url="account.php?action=ajax&";
				url +=get_form_values(document.frm_account);
				xmlHttp=GetXmlHttpObject();
				xmlHttp.onreadystatechange=account;
				xmlHttp.open("post",url,true);
				xmlHttp.send(null);
				document.getElementById("loader").style.display="block";
			}
		}
	}
	else
	{
		if (xmlHttp.readyState==4)
		{ 
			document.getElementById("loader").style.display="none";
			var result=xmlHttp.responseText;
			if(result == 1)
			{
				manage_top_msg_status("show","Successfully Updated!");
//				alert("Successfully Updated!");
			}
			else if(result == 2)
			{
				alert("Email Addrss already exist!");
			}
			else
			{
				ajax_error();
			}
			xmlHttp=null;
		}
	}
	return false;
}

function login()
{
	if(xmlHttp==null)
	{
		var frmvalidator  = new Validator("frm_login");
		frmvalidator.addValidation("login_username","req","Please Enter Your Username");
		frmvalidator.addValidation("login_password","req","Please Enter Your Password");
		if(frmvalidator.validate() == true)
		{
			var url="login.php?ajax=login&";
			url +=get_form_values(document.frm_login);
			xmlHttp=GetXmlHttpObject();
			xmlHttp.onreadystatechange=login;
			xmlHttp.open("post",url,true);
			xmlHttp.send(null);
			document.getElementById("login_loader").innerHTML='Loading...'
		}
	}
	else
	{
		if (xmlHttp.readyState==4)
		{ 
			document.getElementById("login_loader").innerHTML=''
			var result=xmlHttp.responseText;
			if(result == 0)
			{
				document.getElementById("login_loader").innerHTML="Invalid Login!";
			}
			else if(result == 1)
			{
				document.getElementById("login_loader").innerHTML="Login Successful!";
				window.location.href="myaccount.php";
			}
			else if(result == 2)
			{
				document.getElementById("login_loader").innerHTML="Login Blocked!";
				window.location.href="renew_account.php";
			}
			else
			{
				ajax_error();
			}
			xmlHttp=null;
		}
	}
	return false;
}

function logout()
{
	if(xmlHttp==null)
	{
		var url="login.php?ajax=logout&";
		xmlHttp=GetXmlHttpObject();
		xmlHttp.onreadystatechange=logout;
		xmlHttp.open("post",url,true);
		xmlHttp.send(null);
	}
	else
	{
		if (xmlHttp.readyState==4)
		{ 
			var result=xmlHttp.responseText;
			if(result == 1)
			{
				location.href="index.php";
			}
			else
			{
				ajax_error();
			}
			xmlHttp=null;
		}
	}
	return false;
}

function forgot_password()
{
	if(xmlHttp==null)
	{
		var frmvalidator  = new Validator("forgot_pass");
		frmvalidator.addValidation("email","req","Please Enter Email Address");
		frmvalidator.addValidation("email","email","Please Enter Valid Email Address");
		if(frmvalidator.validate() == true)
		{
			var url="password_forgotten.php?ajax=forgot_password&";
			url +=get_form_values(document.forgot_pass);
			xmlHttp=GetXmlHttpObject();
			xmlHttp.onreadystatechange=forgot_password;
			xmlHttp.open("post",url,true);
			xmlHttp.send(null);
			document.getElementById("result").innerHTML="Loading...";
		}
	}
	else
	{
		if (xmlHttp.readyState==4)
		{ 
			document.getElementById("result").innerHTML="";
			var result=xmlHttp.responseText;
			if(result == 0)
			{
				document.getElementById("result").innerHTML="Email Address is not found!";
			}
			else if(result == 1)
			{
				document.getElementById("result").innerHTML="Success! Your password has been sent to your Email!";
				clear_form_values(document.forgot_pass);
			}
			else
			{
				ajax_error();
			}
			xmlHttp=null;
		}
	}
	return false;
}

function change_password()
{
	if(xmlHttp==null)
	{
		var frmvalidator  = new Validator("change_pass");
		frmvalidator.addValidation("old_password","req","Please Enter Old Username");
		frmvalidator.addValidation("new_password","req","Please Enter New Password");
		frmvalidator.addValidation("cnew_password","req","Please Enter Confirmation Password");
		if(frmvalidator.validate() == true)
		{
			if(document.getElementById("new_password").value != document.getElementById("cnew_password").value)
			{
				document.getElementById("err_cnew_password").innerHTML="Password and confirmation Password should be same";
			}
			else
			{
				var url="change_password.php?ajax=change_password&";
				url +=get_form_values(document.change_pass);
				xmlHttp=GetXmlHttpObject();
				xmlHttp.onreadystatechange=change_password;
				xmlHttp.open("post",url,true);
				xmlHttp.send(null);
				document.getElementById("result").innerHTML="Loading...";
			}
		}
	}
	else
	{
		if (xmlHttp.readyState==4)
		{ 
			document.getElementById("result").innerHTML="";
			var result=xmlHttp.responseText;
			if(result == 0)
			{
				document.getElementById("result").innerHTML="Old Password is wrong!";
			}
			else if(result == 1)
			{
				document.getElementById("result").innerHTML="Password has been changed successfully!";
			}
			else
			{
				ajax_error();
			}
			xmlHttp=null;
		}
	}
	return false;
}




function profile()
{
	if(xmlHttp==null)
	{
		var frmvalidator  = new Validator("frm_profile");
		frmvalidator.addValidation("name","req","Please Enter Your Name");
		frmvalidator.addValidation("address1","req","Please Enter Address Line 1");
		frmvalidator.addValidation("town","req","Please Enter Your Town / City");
		frmvalidator.addValidation("country","req","Please Enter Your Country");
		frmvalidator.addValidation("postcode","req","Please Enter Postcode");
		frmvalidator.addValidation("postcode","num","Please Enter Valid Postcode");
		frmvalidator.addValidation("phone","req","Please Enter Phone Number");
		if(frmvalidator.validate() == true)
		{
			var url="profile.php?action=ajax&";
			url +=get_form_values(document.frm_profile);
			xmlHttp=GetXmlHttpObject();
			xmlHttp.onreadystatechange=profile;
			xmlHttp.open("post",url,true);
			xmlHttp.send(null);
			document.getElementById("regi_loader").style.display="block";
		}
	}
	else
	{
		if (xmlHttp.readyState==4)
		{ 
			document.getElementById("regi_loader").style.display="none";
			var result=xmlHttp.responseText;
			if(result == 1)
			{
				alert("Successfully Updated!");
			}
			else
			{
				ajax_error();
			}
			xmlHttp=null;
		}
	}
	return false;
}


function manage_feedback()
{
	var frmvalidator  = new Validator("frm_feedback");
	frmvalidator.addValidation("name","req","Please Enter Name");
	frmvalidator.addValidation("address","req","Please Enter Address");
	frmvalidator.addValidation("email","req","Please Enter Email Address");
	frmvalidator.addValidation("email","email","Please Enter Valid Email Address");
	frmvalidator.addValidation("phone","req","Please Enter Phone Number");
	frmvalidator.addValidation("details","req","Please Enter details");
	return frmvalidator.validate();
}


function manage_join()
{
	var frmvalidator  = new Validator("frm_join");
	frmvalidator.addValidation("name","req","Please Enter Name");
	frmvalidator.addValidation("address","req","Please Enter Address");
	frmvalidator.addValidation("email","req","Please Enter Email Address");
	frmvalidator.addValidation("email","email","Please Enter Valid Email Address");
	if(frmvalidator.validate() == true)
	{
		if(document.getElementById('accept1').checked == false)
		{
			alert("Please give your willing");
			return false;
		}
		if(document.getElementById('accept2').checked == false)
		{
			alert("Please do you like to join");
			return false;
		}
		return true;
	}
	else
	{
		return false;
	}
}

