function htmlEncoder() {
  this.encode = function(str) {
  	var encoded = str.replace(/ & /g, ' &amp; ');
  	encoded = encoded.replace(/'/g, '&#39;');
  	encoded = encoded.replace(/\"/g, '&quot;');
  	return encoded;
  }
}

myEncoder = new htmlEncoder();

function forgottenPasswordSubmitIt() {
  var passwordRecovery = document.getElementById('password-recovery-form');
  if (!isNotEmpty(passwordRecovery.username.value)) {
    alert("Username is Required.");
    passwordRecovery.username.focus();
    return false;
  }

  if (!isNotEmpty(passwordRecovery.email.value)) {
    alert("Email is Required.");
    passwordRecovery.email.focus();
    return false;
  }

  passwordRecovery.submit();
}

function LoginSubmitIt() {
  var loginForm = document.getElementById('login-form');
  
  if (!isNotEmpty(loginForm.username.value)) {
    alert("Username is Required.");
    return false;
  }

  if (!isNotEmpty(loginForm.password.value)) {
    alert("Password is Required.");
    return false;
  }
  
  loginForm.submit();
}

function RegisterSubmitIt() {
  var registrationForm = document.getElementById('registration-form');
  if (!isNotEmpty(registrationForm.username.value)) {
    alert("Username is Required.");
    registrationForm.username.focus();
    return false;
  }

  if (!isNotEmpty(registrationForm.name.value)) {
    alert("Your name is Required.");
    registrationForm.name.focus();
    return false;
  }

  if (!isNotEmpty(registrationForm.password1.value)) {
    alert("Password is Required.");
    registrationForm.password1.focus();
    return false;
  }

  if (!isNotEmpty(registrationForm.password2.value)) {
    alert("Confirm Password is Required.");
    registrationForm.passwords2.focus();
    return false;
  }

  if (registrationForm.password1.value != registrationForm.password2.value) {
    alert("The password do not match, please try again.");
    registrationForm.password1.focus();
    return false;
  }

  if (!isNotEmpty(registrationForm.email.value)) {
    alert("Email is Required.");
    registrationForm.email.focus();
    return false;
  }

  registrationForm.submit();
}

function UpdateUsersSubmitIt() {
  var selectUser = document.getElementById("select-user-form");
  if (!isNotEmpty(selectUser.lsUsers.value)) {
    alert("Please select an option");
    selectUser.lsUsers.focus();
    return false;
  }

  selectUser.submit();
}


function UpdateProductsSubmitIt() {
  var selectProduct = document.getElementById("select-product-form");
  if (!isNotEmpty(selectProduct.lsUsers.value)) {
    alert("Please select an option");
    selectProduct.lsUsers.focus();
    return false;
  }

  selectProduct.submit();
}


function UpdateSubmitIt() {
  if (!isNotEmpty(form1.name.value)) {
    alert("Your name is Required.");
    form1.name.focus();
    return false;
  }

  if (!isNotEmpty(form1.password1.value)) {
    alert("Password is Required.");
    form1.password1.focus();
    return false;
  }
     
  if (!isNotEmpty(form1.password2.value)) {
    alert("Confirm Password is Required.");
    form1.passwords2.focus();
    return false;
  }

  if (form1.password1.value != form1.password2.value) {
    alert("The password do not match, please try again.");
    form1.password1.focus();
    return false;
  }

  if (!isNotEmpty(form1.email.value)) {
    alert("Email is Required.");
    form1.email.focus();
    return false;
  }

  form1.submit();
}


function AdminUpdateSubmitIt() {
  var updateUser = document.getElementById("update-user-form");
  
  if (!isNotEmpty(updateUser.name.value)) {
    alert("Name is Required.");
    updateUser.name.focus();
    return false;
  }

  if (!isNotEmpty(updateUser.password1.value)) {
    alert("Password is Required.");
    updateUser.password1.focus();
    return false;
  }

  if (!isNotEmpty(updateUser.Usergroup.value)) {
    alert("Usergroup is Required.");
    updateUser.Usergroup.focus();
    return false;
  }

  if (!isNotEmpty(updateUser.email.value)) {
    alert("Email is Required.");
    updateUser.email.focus();
    return false;
  }

  updateUser.submit();
}


function UpdateDescriptionsSubmitIt() {
  var selectDescription = document.getElementById("select-description-form");

  if (!isNotEmpty(selectDescription.category.value)) {
    alert("Description is Required.");
    selectDescription.category.focus();
    return false;
  }
  
  selectDescription.submit();
}


function ProductSubmitIt() {
  var productUpdateForm = document.getElementById('product-update-form');
  
  if (productUpdateForm == null) {
    productUpdateForm = document.getElementById('add-product-form');
  }
  
  if (!isNotEmpty(productUpdateForm.Productname.value)) {
    alert("Product name is Required.");
    productUpdateForm.Productname.focus();
    return false;
  } else {
    productUpdateForm.Productname.value 
      = myEncoder.encode(productUpdateForm.Productname.value);
  }
  
  if (!isNotEmpty(productUpdateForm.producttype.value)) {
    alert("Product type is Required.");
    productUpdateForm.producttype.focus();
    return false;
  }
 
  if (!isNotEmpty(productUpdateForm.productprice.value)) {
    alert("Price is Required.");
    productUpdateForm.productprice.focus();
    return false;
  }

  if (!isNotEmpty(productUpdateForm.productdescription.value)) {
    alert("Description is Required.");
    productUpdateForm.productdescription.focus();
    return false;
  } else {
    productUpdateForm.productdescription.value 
      = myEncoder.encode(productUpdateForm.productdescription.value);
  }
 
  productUpdateForm.submit();
}


function CatSubmitIt() {
  var updateDescription = document.getElementById('update-description-form');
  if (!isNotEmpty(updateDescription.description.value)) {
    alert("Description is Required.");
    updateDescription.description.focus();
    return false;
  } else {
    updateDescription.description.value 
      = myEncoder.encode(updateDescription.description.value);
  }
  
  updateDescription.submit();
}


function isNotEmpty(field) {
  // loop until a non-blank character is found
  i = 0;
  while (i < field.length) {
    ch = field.charAt(i);
    if ((ch != " ") && (ch != "\t") && (ch != "\n")) {
      return true;
    }
    i++;
  } 
  
  // field is empty
  return false;
}


function forgottenPasswordbrowserRedirect() {
  URLStr = "index.asp";
  location = URLStr;
}

function ErrorbrowserRedirect() {
  URLStr = "register.asp";
  location = URLStr;
}


function HomeRedirect() {
  URLStr = "home.asp";
  location = URLStr;
}


function AdminRedirect() {
  URLStr = "adminsection.asp";
  location = URLStr;
}

function ProductRedirect() {
  URLStr = "productselect.asp";
  location = URLStr;
}


function CatRedirect() {
  URLStr = "descriptionselect.asp";
  location = URLStr;
}


function UsersRedirect() {
  URLStr = "adminupdateusers.asp";
  location = URLStr;
}


function loadProductPreview(url) {
  var productPreview = document.getElementById('product-preview');
  productPreview.innerHTML = "<img src=\"" + url + "\" alt=\"product image\" />";
}
