//////////////////////////////////////////////////////////////////////////////////////////////
//account details
//////////////////////////////////////////////////////////////////////////////////////////////
function isblank(s) {
   for(var i = 0; i < s.value.length; i++) {
      var c = s.value.charAt(i);
      if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
   }
   return true;
}  
function checkemail(email_var){
	if (isblank(email_var))
	{
		alert("Please enter an email address.");
		email_var.focus();
		return false;
	}
	if (!isblank(email_var))
	{
		var valid = true;	// Initially set valid to true
		var atcount = 0;
		var email = email_var.value;
		var email_length = email_var.value.length;
		
		for (index=0; index<email_length; index++)
		{
			if ((email.substring(index,index+1) == ":") || (email.substring(index,index+1) == ",") || (email.substring(index,index+1) == "'") || (email.substring(index,index+1) == " ") )
			{
				valid = false;
			}
		}
		
		for (i=0; i<email_length; i++)
		{
			if (email.substring(i,i+1) == "@")
			{
				++atcount;
				if (email.substring(i-1,i) == ".")
					valid = false;
				if (email.substring(i+1,i+2) == ".")
					valid = false;
			}
		}
			if (email.substring(email_length-1,email_length) == "@")
			valid = false;
			if (email.substring(email_length-1,email_length) == ".")
			valid = false;
			if (email.substring(0,1) == "@")
			valid = false;
			if (email.substring(0,1) == ".")
			valid = false;
			if (email_var.value.indexOf(".") == -1)
			valid = false;
			if (atcount > 1 || atcount == 0)
			valid = false;
		
		if (!valid) 
		{
			alert("Please include a proper email address (Generally in the form 'username@hostname.domain').");
			email_var.focus();
			return false;
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////
//account details
//////////////////////////////////////////////////////////////////////////////////////////////
function validateAccountDetails(obj) {
	if (isblank(obj.display_name))
	{
		alert("Please enter screen name.");
		obj.display_name.focus();
		return false;
	}
	if(obj.gender.selectedIndex==0)
	{
		alert("Please select gender.");
		obj.gender.focus();
		return false;
	}
	if(obj.birthday_day.selectedIndex==0)
	{
		alert("Please select your birth day.");
		obj.birthday_day.focus();
		return false;
	}
	if(obj.birthday_month.selectedIndex==0)
	{
		alert("Please select your birth month.");
		obj.birthday_month.focus();
		return false;
	}
	if(obj.birthday_year.selectedIndex==0)
	{
		alert("Please select your birth year.");
		obj.birthday_year.focus();
		return false;
	}
	if(obj.location.selectedIndex==0)
	{
		alert("Please select your country of residence.");
		obj.location.focus();
		return false;
	}
	if(obj.occupation.selectedIndex==0)
	{
		alert("Please select your occupation.");
		obj.occupation.focus();
		return false;
	}
	
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//SETTINGS
//////////////////////////////////////////////////////////////////////////////////////////////
function toggle_settings(name){
	//open table
	$(name+'_table').toggle();
	
	//button
	if($(name+'_but').innerHTML=='edit'){
		show=true;
		$(name+'_but').innerHTML='hide';
	}else{
		show=false;
		$(name+'_but').innerHTML='edit';
	}
	
	//subheading
	switch(name){
		case 'email': 
			if(show) $(name+'_sub').innerHTML='Your contact email must always be a valid address.'; 
			else $(name+'_sub').innerHTML='Choose where you receive notifications.'; 
		break;
		case 'password': 
			if(show) $(name+'_sub').innerHTML="By changing your password you will be logged off this computer"; 
			else $(name+'_sub').innerHTML='What you use to login.'; 
		break;
	}
}
function validateSettingsName(obj) {
	if (isblank(obj.first_name))
	{
		alert("Please enter first name.");
		obj.first_name.focus();
		return false;
	}
	if (isblank(obj.last_name))
	{
		alert("Please enter last name.");
		obj.last_name.focus();
		return false;
	}
return true;
}
function validateSettingsEmail(obj) {
	if(obj.new_contact_email_other_radio.checked){
	return checkemail(obj.email);
	}else{
	alert("Enter a new email address.");
	return false;
	}
}
function validateSettingsPassword(obj) {
	if (isblank(obj.old_password))
	{
		alert("Please enter old password.");
		obj.old_password.focus();
		return false;
	}
	if (isblank(obj.new_password))
	{
		alert("Please enter new password.");
		obj.new_password.focus();
		return false;
	}
	if (isblank(obj.confirm_password))
	{
		alert("Please confirm new password.");
		obj.confirm_password.focus();
		return false;
	}
	if (obj.new_password.value!=obj.confirm_password.value)
	{
		alert("Passwords do not match please re-enter password");
		obj.confirm_password.value="";
		obj.confirm_password.focus();
		return false;
	}
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//MESSAGES
//////////////////////////////////////////////////////////////////////////////////////////////
function deleteMessage(){
	if(confirm('Are you sure you want to delete this message?')) return true;
	else return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//LOGIN PAGE
//////////////////////////////////////////////////////////////////////////////////////////////
function validateLogin(obj) {
	checkemail(obj.log_username);
	if (isblank(obj.log_password))
	{
		alert("Please enter password.");
		obj.log_password.focus();
		return false;
	}
return true;
}
function validateForgotPassword(obj) {
	if (isblank(obj.ret_email))
	{
		alert("Please enter email.");
		obj.ret_email.focus();
		return false;
	}
return true;
}
