/// SET THIS TO LIVE DIRECTORY
// WHEN PUBLISHING

/*********************************************************
*/
var jsBase = "/";
/**********************************************************
*/


function loginUser(sUserName,sPassword)
{
	$.getJSON("includes/ajax_scripts/login.php",({ getMethod: 'login', username: sUserName, pass: sPassword }),
	function(databack)
	{
		$.each(databack,function(i, val)
		{
			if(i == "username" && val != "")
			{
				window.location = "/users/"+val;
			}
		});
	});
}


function checkemail(email){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(email))
	{
		testresults=true;
	}
	else
	{
		testresults=false;
	}
	return (testresults);
}


function checkAvailable()
{
	var status;

	$.get(jsBase+"includes/ajax_scripts/ajax_hub.php",({ getMethod: 'checkAvailableEmail', email: $("#email").attr("value")} ),
	function(databack)
	{
		if(databack == "ok")
		{
			status =  true;
		}
		else
		{
			status = false;
		}

		return status;
	});
}

function checkRegForm()
{
	$("#reg_button").click(function()
	{ 
		var bFalse = true;

		if($("#first_name").attr("value") == "")
		{
			$("#reg_error").html("Please enter your first name");
			bFalse = true;
		}
		else if($("#last_name").attr("value") == "")
		{
			$("#reg_error").html("Please enter your last name");
			bFalse = true;
		}
		else if($("#email").attr("value") == "")
		{
			$("#reg_error").html("Please enter valid email.");
			bFalse = true;
		}
		else if($("#password_reg").attr("value") == "")
		{
			//alert($("#password_reg").attr("value"));
			$("#reg_error").html("Please enter password.");
			bFalse = true;
		}
		else if($("#password_reg").attr("value").length < 6)
		{
			$("#reg_error").html("Your password should be at least 6 characters.");
			bFalse = true;
		}
		else if($("#email").attr("value") != "")
		{
			if(checkemail($("#email").attr("value")))
			{
				$.get(jsBase+"includes/ajax_scripts/ajax_hub.php",({ getMethod: 'checkAvailableEmail', email: $("#email").attr("value")} ),
				function(databack)
				{
					if(databack == "ok")
					{
						$.post(jsBase+"includes/ajax_scripts/register_user.php", ( { first_name: $("#first_name").attr("value"), 
						                                                             last_name: $("#last_name").attr("value"),
						                                                             email : $("#email").attr("value"),
						                                                             password: $("#password_reg").attr("value")
						     }),
						  function(databack)
						  {
						  	if(databack == "ok")
						  	{
						  		window.location  = jsBase+"register/action.welcome";
						  	}
						  	else
						  	{
						  		window.location  = jsBase+"register/action.failed";
						  	}
						  });   
					}
					else
					{
						bFalse = true;
						$("#reg_error").html("This email is already registered.");
					}

				});
			}
			else
			{
				bFalse = true;
				$("#reg_error").html("Please enter valid email.");
			}
		}

		return false;
		
	});
}