
function isEmailValid(element,returnOnly)
{
  var bad = false, email = element.value;
  var tld = email.substring(email.length-7, email.length)
  var host = email.substring(email.indexOf("@")+1, email.indexOf(".",email.lastIndexOf("@")))
  var user = email.substring(0, email.indexOf("@"))

  bad |= (email.indexOf("@") == -1 || email.indexOf("@") != email.lastIndexOf("@"));
  bad |= (email.indexOf(".") == -1)
  bad |= (host.length == 0)
  bad |= (user.length == 0)
  bad |= (email.indexOf(" ") != -1)
  bad |= (email.indexOf(",") != -1)

  if( !bad )
  {
    return true;
  }
  else
  {
    if( !returnOnly )
    {
      alert("Your email address appears to be incorrect. Please check that you have entered it correctly.");
      element.focus()
    }
    return false;
  }
}

function checkForm()
{
  var err = [];
  var edit = $("#passwordConfirm").length>0;

  if( !$("#username").val() ){ err.push("username missing"); }
  if( edit && !$("#passwordConfirm").val() ){ err.push("confirmation of your current password"); }
  if( !edit && !$("#prfl_password").val() ){ err.push("password missing"); }
   else if( !$("#prfl_password").val() != !$("#password2").val() ){ err.push("passwords do not match"); }
  if( !$("#email").val() ){ err.push("email address missing"); }
   else if( !isEmailValid($("#email")[0], true) ){ err.push("incorrect email address"); }

  if( err.length > 0 )
  {
    notify("<h3>Oh Dear</h3><p>Your login details aren't complete or are incorrect:</p><ul><li>" + err.join("</li><li>") +"</li></ul>", "I'll sort it");

    return false;
  }
  else
  {
    return true;
  }
}

function switchTab( liveTab, ahref )
{

  $("#regWrp").css({height:$("#regWrp").height()});
  $(".tabs a").addClass("tabOff").removeClass("tabOn");
  $("#tabButton"+liveTab).addClass("tabOn").removeClass("tabOff");

  $(".tabPane:visible").animate( {opacity:0}, 50, function()
  {
    $(this).hide().css({opacity:1});
    $("#tab"+liveTab).fadeIn(300);
    var h = $("#tab"+liveTab).outerHeight();
    $("#regWrp").animate({height:h},200);
  });

  if( typeof(ahref) != "undefined" )
  {
    ahref.blur();
  }
  return false;
}

function checkSig( element, evnt )
{
  if( element.value == null || element.value == "" || isKeyDeleteBackSpace( getKeyCode( evnt ) ) || isKeyCursor( getKeyCode( evnt ) ) )
  {
    return true;
  }
  lines = element.value.split("\n");

  if( lines.length > 3 ||  element.value.length > 240 )
  {
    element.value = element.value.substring(0,240);
    return false;
  }
  return true;
}


function getImageSize(w,h,f)
{
  with(document.forms[f])
  {
    if( w > 80 || h > 80 )
    {
      alert("Your avatar measures "+w+" wide by "+h+" high.\n"+
            "This is greater than the 80 by 80 maximum.\n"+
            "Please alter the image or provide an alternative.")
      window.loader.location.href="/util/imageDiscovery.asp<%= forumAvatar %>";
      $("#avatarCaption").show();
    }
    else
    {
      $("#avatarCaption").hide();
    }
  }
}
