function clearField(thefield) {
  if (thefield.defaultValue==thefield.value)
  thefield.value = ""
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function logout()
{
  deleteCookie("planner_login_cookie");
  window.location.href = '/bathrooms/planner';
}

function deleteCookie(name)
{
  if(readCookie(name))
  {
    var expires = "; expires=Thu, 01-Jan-1970 00:00:01 GMT";
    document.cookie = name+"="+expires+"; path=/";
    return true;
  }
  else
  {
    return false;
  }
}





function verifyRegister(myBathroomFormObj) {
  if (myBathroomFormObj.reg_email.value == '' || myBathroomFormObj.reg_password.value == '' || myBathroomFormObj.reg_confirm_password.value == '') {
      alert('Please fill out all fields before registering.');
      return false;
  }
  else {
    if (! verifyEmail(myBathroomFormObj.reg_email.value)) {
      alert ('That does not appear to be a valid email address. Please re-enter.');
      return false;
    }
    if (myBathroomFormObj.reg_password.value != myBathroomFormObj.reg_confirm_password.value) {
      alert('The two password fields do not match. Please re-enter your password.');
      return false;
    }
  }
  return true;
}

function verifyDetails(myBathroomFormObj) {
  if (document.getElementById('upd_email') == null) {
    if (myBathroomFormObj.upd_current_password.value == '' && myBathroomFormObj.upd_new_password.value == '' && myBathroomFormObj.upd_confirm_password.value == '' && myBathroomFormObj.upd_current_email.value == '') {
      return false;
    }
    if (myBathroomFormObj.upd_current_password.value == '' || myBathroomFormObj.upd_new_password.value == '' || myBathroomFormObj.upd_confirm_password.value == '') {
        alert('Please fill out all password fields to change your password.');
        return false;
    }
    if (myBathroomFormObj.upd_current_email.value == '') {
      alert ('Please enter your email address so we can identify your account.');
      return false;
    } else if (! verifyEmail(myBathroomFormObj.upd_current_email.value)) {
      alert ('That does not appear to be a valid email address. Please re-enter.');
      return false;
    }
  }
  else {
    if (myBathroomFormObj.upd_current_password.value == '' && myBathroomFormObj.upd_new_password.value == '' && myBathroomFormObj.upd_confirm_password.value == '' && myBathroomFormObj.upd_email.value == '') {
      return false;
    }
    if (myBathroomFormObj.upd_email.value != '' && ! verifyEmail(myBathroomFormObj.upd_email.value)) {
      alert ('That does not appear to be a valid email address. Please re-enter.');
      return false;
    }
  }
  if (myBathroomFormObj.upd_current_password.value != '' || myBathroomFormObj.upd_new_password.value != '' || myBathroomFormObj.upd_confirm_password.value != '') {
    if (myBathroomFormObj.upd_current_password.value == '' || myBathroomFormObj.upd_new_password.value == '' || myBathroomFormObj.upd_confirm_password.value == '') {
        alert('Please fill out all password fields to change your password.');
        return false;
    }
    else {
      if (myBathroomFormObj.upd_new_password.value != myBathroomFormObj.upd_confirm_password.value) {
        alert('The two new password fields do not match. Please re-enter your password.');
        return false;
      }
    }
  }
  return true;
}

function verifyCreate(myBathroomFormObj) {
  if (myBathroomFormObj.blength.value == '' || myBathroomFormObj.bwidth.value == '' || myBathroomFormObj.bname.value == '') {
    alert ('Please fill out all fields to create your new bathroom.');
    return false;
  }
  if (!IsNumeric(myBathroomFormObj.blength.value) || !IsNumeric(myBathroomFormObj.bwidth.value)) {
    alert ('Length and Width must both be numeric (in mm).');
    return false;
  }
  if (myBathroomFormObj.blength.value > 32000 || myBathroomFormObj.bwidth.value > 32000) {
    alert ('Please keep dimensions to less than 32 metres.');
    return false;
  }
}

function verifyForgot(myBathroomFormObj) {
  if (myBathroomFormObj.forgot_email.value == '') {
    return false;
  }
  if (! verifyEmail(myBathroomFormObj.forgot_email.value)) {
    alert ('That does not appear to be a valid email address. Please re-enter.');
    return false;
  }
}

function verifyEmail(email) {
  if (email.indexOf('@') > 0) {
    return true;
  }
  else {
    return false;
  }
}

function IsNumeric(strInput) {
  var strValidChars = "0123456789";
  var strChar;
  var blnResult = true;

  for (i = 0; i < strInput.length && blnResult == true; i++) {
    strChar = strInput.charAt(i);
    if (strValidChars.indexOf(strChar) == -1) {
      blnResult = false;
    }
  }
  return blnResult;
}

function showSignin()
{
  if(document.getElementById('signin').style.display=="block") {
    document.getElementById('signin').style.display="none";
  }
  else {
    document.getElementById('signin').style.display="block";
  }
}

