

function checkUsername(input_element, output_element)
{

	if(input_element.value.length < 2)
	{
		output_element.innerHTML = '<span style="color:red;">Too Short</span>';
	}
	else
	{
		output_element.innerHTML = 'Checking Username';
		var params = 'username=' + input_element.value;
		new Ajax.Request('controllers/check_username.php',
		  {
		    method:'post',
		parameters: params,
		    onSuccess: function(transport){
		      var response = transport.responseText;
			
			if(response == "true")
			{
				output_element.innerHTML = '<img src="images/checked.gif" alt="" />';
				usernameOK = true;
			}
			else
			{
				output_element.innerHTML = '<span style="color:red;">Invalid</span>';	
				usernameOK = false;
			}
		    },
		    onFailure: function(){ 
			showLogin(); 
			}
		 });
	}
}

function validateEmail(input_element, output_element) 
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = input_element.value;
	if(reg.test(address) == false) 
	{
		output_element.innerHTML = '<span style="color:red;">Invalid</span>';	
		emailOK = false;
	}
	else
	{
		output_element.innerHTML = '<img src="images/checked.gif" alt="" />';
		emailOK = true;	
	}
}

function validateConfirmationPassword(input_element1, input_element2, output_element)
{
	if(passwordOK)
	{
		if(input_element1.value != input_element2.value)
		{
			output_element.innerHTML = '<span style="color:red;">Doesn\'t Match</span>';
			passwordConfirmOK = false;
		}
		else
		{
			output_element.innerHTML = '<img src="images/checked.gif" alt="" />';
			passwordConfirmOK = true;
		}
	}
	else
	{
		output_element.innerHTML = '<span style="color:red;">Doesn\'t Match</span>';;
		passwordConfirmOK = false;
	}
}

function validateLocation(input_element, output_element)
{
	if(input_element.value.length > 0)
	{
		output_element.innerHTML = '<img src="images/checked.gif" alt="" />';
		locationOk = true;
	}
	else
	{
		output_element.innerHTML = '<span style="color:red;">Invalid</span>';
		locationOk = false;
	}
}

function validateName(input_element, output_element)
{
	if(input_element.value.length > 0)
	{
		output_element.innerHTML = '<img src="images/checked.gif" alt="" />';
		personnameOK = true;		
	}
	else
	{
		output_element.innerHTML = '<span style="color:red;">Invalid</span>';
		personnameOK = false;
	}
}

function validateGender(input_element, output_element)
{
	if(input_element.options[input_element.selectedIndex].value.length > 0)
	{
		output_element.innerHTML = '<img src="images/checked.gif" alt="" />';
		genderOk = true;
	}
	else
	{
		output_element.innerHTML = '';
		genderOk = false;
	}
}

function checkPasswordStrength(input_element, output_element)
{
	var strength = output_element;
	var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
	var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
	var enoughRegex = new RegExp("(?=.{6,}).*", "g");
	var pwd = input_element;
	if (pwd.value.length==0) 
	{
		strength.innerHTML = 'Type Password';
		passwordOK = false;
	} 
	else if (false == enoughRegex.test(pwd.value)) 
	{
		strength.innerHTML = '<span style="color:red;">Too Short</span>';
		passwordOK = false;
	} 
	else if (strongRegex.test(pwd.value)) 
	{
		strength.innerHTML = '<img src="images/checked.gif" alt="" /><span style="color:green">Strong</span>';
		passwordOK = true;
	} 
	else if (mediumRegex.test(pwd.value)) 
	{
		strength.innerHTML = '<img src="images/checked.gif" alt="" /><span style="color:orange">Medium</span>';
		passwordOK = true;
	} 
	else 
	{
		strength.innerHTML = '<img src="images/checked.gif" alt="" /><span style="color:red">Weak</span>';
		passwordOK = true;
	}
}

function disconnectTwitter(connect_button, disconnect_button)
{
	if(confirm('Are you sure you want to disconnect your twitter account?  Your updates will no longer be posted to twitter automatically.'))
	{
		new Ajax.Request('controllers/twitter_disconnect.php',
		{
		 method:'post',parameters:"",
		   onSuccess: function(transport){
		      	var response = transport.responseText;
			if(response != "")
			{
				alert(response);
			}
			else
			{
				connect_button.style.display = 'block';
				disconnect_button.style.display = 'none';
			}
			
			}
		});	

		
	}
}

function disconnectFB(connect_button, disconnect_button)
{
	if(confirm('Are you sure you want to disconnect your Facebook account?  Your updates will no longer be posted to Facebook automatically.'))
	{
		new Ajax.Request('controllers/fb_disconnect.php',
		{
		 method:'post',parameters:"",
		   onSuccess: function(transport){
		      	var response = transport.responseText;
			if(response != "")
			{
				alert(response);
			}
			else
			{
				connect_button.style.display = 'block';
				disconnect_button.style.display = 'none';
			}
			
			}
		});	

		
	}
}


function validateForm()
{
	if(!emailOK)
	{
		alert("The email address you entered is invalid");
		$('email').focus();
		return false;
	}
	if(!usernameOK)
	{
		alert("The username you entered is invalid");
		$('username').focus();
		return false;
	}
	if(!passwordOK)
	{
		alert("The password you entered is invalid");
		$('phash').focus();
		return false;
	}
	if(!passwordConfirmOK)
	{
		alert("The passwords you entered do not match");
		$('phash').focus();
		return false;
	}
	if(!personnameOK)
	{
		alert("The name you entered is not valid");
		$('personname').focus();
		return false;
	}
	if(!locationOk)
	{
		alert("The location you entered is not valid");
		$('location').focus();
		return false;
	}
	if(!genderOk)
	{
		alert("The gender you entered is not valid");
		$('gender').focus();
		return false;
	}

	return true;
}



