/**
 * Buyers login form verification routines
 *
 * Site: www.avenueshops.com
 * Related files: youraccount.php
 *
 * Author: Rubén A. Mansilla
 * Last update: 20:15 22/03/2005
 *
 */

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

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

  return true;
}

function verify(){
  var goOn = verifyNotNull(document.getElementById("lUserEmail"), "the user e-mail address");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("lPassword"), "the password");

  return goOn;
}

function showPopupMessage(){
  var args = "width = 450, height = 100, scrollbars = no, dependent = yes";
  var summeryWnd = window.open("./inc/forgotLoginMessage.php", "Forgot", args);

  summeryWnd.moveTo(0,0);
  summeryWnd.focus();

  return;
}

function verifyForgottenLogin(){
  var goOn = verifyNotNull(document.getElementById("lStoreEmail"), "the mail that you've register in your signup");
  return goOn;
}

function verifyNewShopper(){
  var goOn = verifyNotNull(document.getElementById("firstName"), "the First name");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("lastName"), "the Last name");

//  if (goOn)
//    goOn = verifyNotNull(document.getElementById("organization"), "the organization information");
if (goOn)
    goOn = verifyNotNull(document.getElementById("address1"), "your first address");

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

  if (goOn)
    goOn = verifyNotNull(document.getElementById("stateProvince"), "your address state");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("country"), "your address country");

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

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

//  if (goOn)
//    goOn = verifyNotNull(document.getElementById("faxArea"), "the fax area");

//  if (goOn)
//    goOn = verifyNotNull(document.getElementById("faxNumber"), "the fax number");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("email"), "your e-mail address");

  //shipping information

  if (goOn)
    goOn = verifyNotNull(document.getElementById("sFirstName"), "the First name");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("sLastName"), "the Last name");

/*  if (goOn)
    goOn = verifyNotNull(document.getElementById("sOrganization"), "the organization information");
*/
if (goOn)
    goOn = verifyNotNull(document.getElementById("sAddress1"), "your first address");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("sStateProvince"), "your address state");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("sCountry"), "your address country");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("sPhoneArea"), "the phone area");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("sPhoneNumber"), "the phone number");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("sCity"), "the city");
	
/*
  if (goOn)
    goOn = verifyNotNull(document.getElementById("sFaxArea"), "the fax area");

  if (goOn)
    goOn = verifyNotNull(document.getElementById("sFaxNumber"), "the fax number");
*/
  if (goOn)
    goOn = verifyNotNull(document.getElementById("sEmail"), "your e-mail address");

  return goOn;
}

function duplicate(sourceElement, targetElement){
  targetElement.value = sourceElement.value;
}

function erase(element){
  element.value = "";
}

function copyBillingToShipping(){
  if (document.getElementById("shopperSignupForm").sameAsAbove.checked){
    duplicate(document.getElementById("firstName"), document.getElementById("sFirstName"));
    duplicate(document.getElementById("lastName"), document.getElementById("sLastName"));
//    duplicate(document.getElementById("organization"), document.getElementById("sOrganization"));
    duplicate(document.getElementById("address1"), document.getElementById("sAddress1"));
    duplicate(document.getElementById("address2"), document.getElementById("sAddress2"));	
    duplicate(document.getElementById("city"), document.getElementById("sCity"));
    duplicate(document.getElementById("country"), document.getElementById("sCountry"));
	var sCountry = document.getElementById("sCountry");
	changeCountry('sStateProvince',sCountry.options[sCountry.selectedIndex].value,'sStateDiv');
	duplicate(document.getElementById("stateProvince"), document.getElementById("sStateProvince"));
    duplicate(document.getElementById("zip"), document.getElementById("sZip"));
    duplicate(document.getElementById("phoneArea"), document.getElementById("sPhoneArea"));
    duplicate(document.getElementById("phoneNumber"), document.getElementById("sPhoneNumber"));
//    duplicate(document.getElementById("faxArea"), document.getElementById("sFaxArea"));
//    duplicate(document.getElementById("faxNumber"), document.getElementById("sFaxNumber"));
    duplicate(document.getElementById("email"), document.getElementById("sEmail"));
  }else{
    erase(document.getElementById("sFirstName"));
    erase(document.getElementById("sLastName"));
//    erase(document.getElementById("sOrganization"));
    erase(document.getElementById("sAddress1"));
    erase(document.getElementById("sAddress2"));	
    erase(document.getElementById("sCity"));
    erase(document.getElementById("sStateProvince"));
    erase(document.getElementById("sCountry"));
    erase(document.getElementById("sZip"));
    erase(document.getElementById("sPhoneArea"));
    erase(document.getElementById("sPhoneNumber"));
//    erase(document.getElementById("sFaxArea"));
//    erase(document.getElementById("sFaxNumber"));
    erase(document.getElementById("sEmail"));
  }
  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) {
    return false;
  }
}


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

  //login form
  document.getElementById("loginForm").onsubmit = function(){return verify();}
  document.getElementById("loginForm").onreset = function(){document.getElementById("lUserEmail").focus();}

  //request user id and password form
  document.getElementById("forgotLoginForm").onsubmit = function(){return verifyForgottenLogin();}

  //new shopper form
  document.getElementById("shopperSignupForm").onsubmit = function(){return verifyNewShopper();}

  return;
}

document.onkeydown = captureKey;
