var customerInfo;

function Address(key, firstName, lastName, nickname) {
    this.addressKey = key;
    this.firstName = firstName;
    this.lastName = lastName;
    this.nickname = nickname;
}

function Customer(loggedIn) {
    this.addressList = new Array();
    this.loggedIn = loggedIn;
    this.addAddress = addAddress;
    this.getNumAddress = getNumAddress;
    this.getAddressList = getAddressList;
}

function getNumAddress() {
    return this.addressList.length;
}

function getAddressList() {
    return this.addressList;
}

function addAddress(newAddress) {
    this.addressList.push(newAddress);
}

var _customerDataFunc = function (data) {
    var cInfo;
    doDebug("in _customerDataFunc_");
    if (data !== null && typeof data == "object") {
        cInfo = new Customer(data.loggedIn);
        for (i = 0; i < data.addressList.length; i++) {
            var addr = new Address(data.addressList[i].addressKey, data.addressList[i].firstName, data.addressList[i].lastName, data.addressList[i].nickname);
            doDebug("i=" + i + " addressKey=" + addr.addressKey + " nickname = " + addr.nickname);
            cInfo.addAddress(addr);
        }
        var addrFoo = cInfo.getAddressList();
    } else {
      cInfo = initBasicCustomerInfo();
    }
    customerInfo = cInfo;
};

function initBasicCustomerInfo() {
    doDebug("inside initBasicCustomerInfo()");
    cInfo = new Customer(false);
    var address = new Address(-1, "", "", "Me");
    cInfo.addAddress(address);
    return cInfo;
}
    
// this is just an error handler for dwr to catch the cases when
// the sid specified is invalid.
function custInfoErrorHandler() {
    doDebug("error handler executed");
    customerInfo = initBasicCustomerInfo();
}

/*********************************************************************
     loads the basic customer information via an AJAX call after the page
     has loaded. To use this function requires the following items:
    
      1 - include the javascript files. This first 3 are DWR specific, the last is an LE utility
       /ic_content_display_web/AJAX/interface/JCustomerDataAccessImpl.js
       /ic_content_display_web/AJAX/engine.js
       /ic_content_display_web/AJAX/util.js
       /ic_content_display_web/js/le.utility.js
    
      2 - add a call to this function in the <body onload="">
    
     Upon successful execution of this function the global variable customerInfo
     should contain the fields we are interested in:
    
     loggedIn   - indicates whether the user is logged in or not.
     addrList   - array of address objects for this customer which contain:
                     addressKey - the PK for NAME_ADDRESS
                     firstName
                     lastName
                     nickname
    
     If the sid is missing or invalid customerInfo defaults to
        loggedIn = false
        addrList of one with dummy values.
    */
    
function loadBasicCustomerData() {
    
       // if no sid is found, initialize to the default, basic customer info.
    if (theSid == null || theSid == "") {
        customerInfo = initBasicCustomerInfo();
       // if a sid is found try to get the data from the server.
    } else {
       
          // initialize the communications to syncronous and assign our own
          // error handler to address the case when the sid is specified but
          // invalid.
        doDebug("before findCustomerWithAddressOnly(" + theSid + ")");
        DWREngine.setAsync(false);
        DWREngine.setErrorHandler(custInfoErrorHandler);
        doDebug("before findCustomerWithAddressOnly(" + theSid + ")");
        JCustomerDataAccessImpl.findCustomerWithAddressOnly(theSid, _customerDataFunc);
        doDebug("after findCustomerWithAddressOnly(" + theSid + ")");
        DWREngine.setAsync(true);
    }
}
