/**
 * Signup form verification routines
 *
 * Site: www.avenueshops.com
 * Related files: signup.php
 *
 * Author: Rubén A. Mansilla
 * Last update: 20050312, 1124
 *
 */

function isEmpty(aString){
  return (aString.length == 0);
}

function verifyUserID(){
  var id = document.getElementById("userID");

  if (isEmpty(id.value)){
    alert("Please, specify a nick");
    id.focus();
    return false;
  }

  if (id.value.length < 8){
    alert("Your nick must be at least 8 characters. Please, try again");
    id.value = "";
    id.focus();
    return false;
  }

  return true;
}

function verifyPassword(){
  var passwd = document.getElementById("password");
  var passwordConfirm = document.getElementById("passwordConfirm");

  if (isEmpty(passwd.value)){
    alert("Please, specify the password");
    passwd.focus();
    return false;
  }

  if (isEmpty(passwordConfirm.value)){
    alert("Please, confirm the password");
    passwordConfirm.focus();
    return false;
  }

  if (passwd.value != passwordConfirm.value){
    alert("Your password do not match. Please, try again");
    passwordConfirm.value = "";
    passwordConfirm.focus();
    return false;
  }

  if (passwd.value.length < 8){
    alert("Your password must be at least 8 characters. Please, try again");
    passwd.value = "";
    passwordConfirm.value = "";
    passwd.focus();
    return false;
  }

  return true;
}

function verifyNotNull(element, fieldDescription){
  if (isEmpty(element.value)){
    alert("Please, specify " + fieldDescription);
    element.focus();
    return false;
  }

  return true;
}


function verify(){
  var goOn = verifyUserID();

  if (goOn)
    goOn = verifyPassword();

  if (goOn)
    goOn = verifyNotNull(document.getElementById("shopName"), "the name of the shop");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("streetAddress"), "a street address");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("city"), "a city");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("stateProvince"), "a State/Province");

/*  if (goOn)
    goOn = verifyNotNull(document.getElementById("country"), "a country");
*/
  if (goOn)
    goOn = verifyNotNull(document.getElementById("zip"), "a Zip/Postal code");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("phoneArea"), "a phone area");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("phoneNumber"), "a phone number");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("shopEmail"), "a mail address for the shop");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("shopType"), "a shop type");

/*  if (goOn)
    goOn = verifyNotNull(document.getElementById("shopDescription"), "a shop description");
*/
  if (((document.getElementById("have").checked) == true) && goOn)
    goOn = verifyNotNull(document.getElementById("shopUrl"), "the URL's shop website");

  return goOn;
}

function haveNotWebsite(){
  document.getElementById("shopUrl").value = "";
  document.getElementById("shopUrl").disabled = true;
//  document.getElementById("tellUsYourUrl").style.visibility = "hidden";
  return;
}

function haveAWebsite(){
//  document.getElementById("tellUsYourUrl").style.visibility = "visible";
  document.getElementById("shopUrl").disabled = false;
  document.getElementById("shopUrl").focus();
  return;
}

/**
 * captureKey(e) Manage the event that occur when the user
 *               press the key:
 *                             - ENTER (13)
 */
var key;
var targetObject;
var ENTER = 13;
function captureKey(e){
  if (document.all){
    key = event.keyCode;
  } else {
    key = e.which;
  }

  if (key == ENTER) {
    //verify(); //not a good idea...
    return false;
  }
}


window.onload = function(){
  document.getElementById("firstName").focus();

  if (document.getElementById("shopUrl").value.length == 0){
    document.getElementById("haveNot").checked = true;
    document.getElementById("shopUrl").disabled = true;
//    document.getElementById("tellUsYourUrl").style.visibility = "hidden";
  } else {
    document.getElementById("have").checked = true;
  }

  document.getElementById("signupForm").onsubmit = function(){return verify();}
//  document.getElementById("signupForm").onreset = function(){document.getElementById("firstName").focus();}

  return;
}

document.onkeydown = captureKey;
