/***********************************************************************************************************************
| object/Cust.js
***********************************************************************************************************************/



/***********************************************************************************************************************
| CONSTRUCTOR - Data members are determined by the JSON passed in (ostensibly from the server).  Data members are in
|               the format t<tablename>_f<field#>.  (eg) tncust_f1 = customer's last name.
|               By convention, primary billing and shipping addresses are first in the address arrays(arrSAddr,arrBAddr).
| json: json containing customer data.
\**********************************************************************************************************************/
function Cust(json) {
   var objBAddr, objSAddr, undefined;
   var now =new Date();

   this.className ="Cust";
   this.locked =false;
   this.boolDistributor =false;
   this.arrBAddr =new Array();
   this.arrSAddr =new Array();
   this.arrPhone =new Array();
   this.arrEmail =new Array();
   this.charCustomerCode ="R";  //defaut customer type
   this.boolDistributor =false;
   this.boolPreferred =false;

   if(json && json.className && json.className == "Cust") {
      var fieldName;

      for(fieldName in json) {
         this[fieldName] =json[fieldName];
      }

      if(json.tdistributor_f91) { this.dateDistributorExpires =new Date("20" +json.tdistributor_f91.substr(6,2),json.tdistributor_f91.substr(0,2),json.tdistributor_f91.substr(3,2)); }
      if(json.tdistributor_f92) { this.datePreferredExpires =new Date("20" +json.tdistributor_f92.substr(6,2),json.tdistributor_f92.substr(0,2),json.tdistributor_f92.substr(3,2)); }

      this.evaluateExpiryDates();

      this.charCustType =json["tncust_f20"];
      this.charCustomerCode ="R";
      if(this.charCustType == "R") { this.strCustType ="Retail"; this.charCustomerCode ="R"; }
      if(this.charCustType == "W") { this.strCustType ="Wholesale"; this.charCustomerCode ="W"; }
      if(this.boolDistributor)     { this.strCustType ="Distributor"; this.charCustomerCode ="D"; }
      if(this.boolPreferred)     { this.strCustType ="Distributor"; this.charCustomerCode ="D"; }
      
      objBAddr =this.getPrimaryBillingAddress();
      if(!objBAddr) {
         objBAddr =new Address();
         objBAddr.strId ="Billing";
         setPrimaryBillingAddress(objBAddr);
      }
      objSAddr =this.getPrimaryShippingAddress();
      if(!objSAddr) {
         objSAddr =new Address();
         objSAddr.strId ="Shipping";
         setPrimaryBillingAddress(objSAddr);
      }

      this.initBAddr(objBAddr);
      this.initSAddr(objSAddr);

      try { this.setPrimaryPhone(new Phone(this.tncust_f17)); } catch(e){ ; }
      try { this.setPrimaryEmail(new Email(this.tncust_f310)); } catch(e){ ; }
   }

   if(json && json.tncust_f5) { this.dateFirstContact =new Date("20" +json.tncust_f5.substr(6,2),json.tncust_f5.substr(0,2),json.tncust_f5.substr(3,2)); }
   else { this.dateFirstContact =undefined; }

   if(!this.tncust_f262) { this.tncust_f262 ="R"; }   //default shipping address type is (R)esidential

   /*this.tncust_f1 =strLName;
   this.tncust_f2 =strFNname;
   this.tncust_f4 =intCustNum;

   this.tncust_f6 =strBAddrLine1;
   this.tncust_f7 =strBAddrLine2;
   this.tncust_f8 =strBAddrCity;
   this.tncust_f9 =strBAddrRegion;
   this.tncust_f10 =strBAddrPostalCode;
   this.tncust_f304 =strBAddrCountry;

   this.tncust_f11 =strSAddrLine1;
   this.tncust_f12 =strSAddrLine2;
   this.tncust_f13 =strSAddrCity;
   this.tncust_f14 =strSAddrRegion;
   this.tncust_f15 =strSAddrPostalCode;
   this.tncust_f306 =strSAddrCountry;
   this.tncust_f262 =charAddressType;

   this.tncust_f17 =strPhone;
   this.tncust_f18 =strGender;
   this.tncust_f19 =dateBDay;
   this.tncust_f20 =strCustType;
   this.tncust_f21 =boolTaxable;

   this.tncust_f22 =dateLastPurchase;
   this.tncust_f23 =$YTDPurchases;
   this.tncust_f24 =$TotalPurchases;
   this.tncust_f30 =intSponsorNum;
   this.tncust_f310 =strEmail;

   this.tdistributor_f34 =boolPreferredCust;
   this.tdistributor_f41 =dateDistCancelled;
   this.tdistributor_f67 =boolEmailBad;*/
}







/***********************************************************************************************************************
| toString
\**********************************************************************************************************************/
Cust.prototype.toString =function() {
   var strToString ="";

   strToString =this.tncust_f1 ? this.tncust_f4 +" " +tncust_f1 +" " +tncust_f2 : "<unitialized>";

   return strToString;
}





Cust.prototype.initBAddr =function(objAddr) {
   if(objAddr && objAddr.className && objAddr.className == "Address") {
      if(this.tncust_f4) { objAddr.strParentId =this.tncust_f4; }
      if(this.tncust_f2) { objAddr.strAddressee =this.tncust_f2; }
      if(this.tncust_f1) { objAddr.strAddressee +=" " +this.tncust_f1; }
      if(this.tncust_f7) { objAddr.strAddr1 =this.tncust_f7; }
      if(this.tncust_f6) { objAddr.strAddr2 =this.tncust_f6; }
      if(this.tncust_f8) { objAddr.strCity =this.tncust_f8; }
      if(this.tncust_f9) { objAddr.strRegion =this.tncust_f9; }
      if(this.tncust_f10) { objAddr.strPostalCode =this.tncust_f10; }
      if(this.tncust_f304) { objAddr.strCountry =this.tncust_f304; }
      if(this.tncust_f262) { objAddr.charAddressType =this.tncust_f262; }
      objAddr.strId ="Billing";
   }
}





Cust.prototype.initSAddr =function(objAddr) {
   if(objAddr && objAddr.className && objAddr.className == "Address") {
      if(this.tncust_f4) { objAddr.strParentId =this.tncust_f4; }
      if(this.tncust_f2) { objAddr.strAddressee =this.tncust_f2; }
      if(this.tncust_f1) { objAddr.strAddressee +=" " +this.tncust_f1; }
      if(this.tncust_f12) { objAddr.strAddr1 =this.tncust_f12; }
      if(this.tncust_f11) { objAddr.strAddr2 =this.tncust_f11; }
      if(this.tncust_f13) { objAddr.strCity =this.tncust_f13; }
      if(this.tncust_f14) { objAddr.strRegion =this.tncust_f14; }
      if(this.tncust_f15) { objAddr.strPostalCode =this.tncust_f15; }
      if(this.tncust_f306) { objAddr.strCountry =this.tncust_f306; }
      if(this.tncust_f262) { objAddr.charAddressType =this.tncust_f262; }
      objAddr.strId ="Shipping";
   }
}





/***********************************************************************************************************************
| sanityCheck
| Performs a sequence of checks on vital data to ensure all values are legal.
| boolNewCustomer: flag to skip existing customer checks
| returns: either "sane" or a '|\n' delimited list of sanity check failures.
\**********************************************************************************************************************/
Cust.prototype.sanityCheck =function(boolNewCustomer) {
   var strDiagnosis ="";
   var strDiagnosisChild ="";
   var objBAddr =this.getPrimaryBillingAddress();
   var objSAddr =this.getPrimaryShippingAddress();
   var objPhone =this.getPrimaryPhone();
   var objEmail =this.getPrimaryEmail();

   if(!this.tncust_f4) { this.tncust_f4 =""; }

   if(this.tncust_f4 != (1*this.tncust_f4)) { this.tncust_f4 =""; }

   this.clean();

   if(!boolNewCustomer) {
      if(!this.tncust_f4) { strDiagnosis +="missing~Customer #|"; }
      if(!this.tncust_f30) { strDiagnosis +="missing~Sponsor #|"; }
   }

   if(!this.tncust_f1) { strDiagnosis +="missing~Last Name|"; }
   if(!this.tncust_f2) { strDiagnosis +="missing~First Name|"; }

   if(objPhone) {
      strDiagnosisChild =objPhone.sanityCheck();
      if(strDiagnosisChild && strDiagnosisChild != "sane") { strDiagnosis +=+strDiagnosisChild; }
   }
   else { strDiagnosis +="missing~Phone|"; }

   if(objEmail) {
      strDiagnosisChild =objEmail.sanityCheck();
      if(strDiagnosisChild && strDiagnosisChild != "sane") { strDiagnosis +="Email: \n|" +strDiagnosisChild; }
   }
   else { strDiagnosis +="missing~Email|"; }

   if(objBAddr) {
      strDiagnosisChild =objBAddr.sanityCheck();
      if(strDiagnosisChild && strDiagnosisChild != "sane") { strDiagnosis +=strDiagnosisChild; }
   }
   else { strDiagnosis +="missing~Billing Address|"; }

   if(objSAddr) {
      strDiagnosisChild =objSAddr.sanityCheck();
      if(strDiagnosisChild && strDiagnosisChild != "sane") { strDiagnosis +=strDiagnosisChild; }
   }
   else { strDiagnosis +="missing~Shipping Address|"; }

   if(strDiagnosis == "") { strDiagnosis ="sane"; }
   else { strDiagnosis ="className~Cust|id~" +this.tncust_f4 +"|" +strDiagnosis; }

   return strDiagnosis;
}





/***********************************************************************************************************************
| clean
| Clean illegal characters from data members.
| return: boolean: successful?
\**********************************************************************************************************************/
Cust.prototype.clean =function() {
   var boolSuccess =true;
   var objAddr;
   
   if(!this.tncust_f4) { this.tncust_f4 =""; }
   if(this.tncust_f4 != (1*this.tncust_f4)) { this.tncust_f4 =""; }

   this.tncust_f1 =("" +(trim(this.tncust_f1))).replace( /[!-\/,:-@,\[-`,\{-~]/g, " ");
   this.tncust_f2 =("" +(trim(this.tncust_f2))).replace( /[!-\/,:-@,\[-`,\{-~]/g, " ");
   this.tncust_f4 =("" +(trim(this.tncust_f4))).replace( /[!-\/,:-@,\[-`,\{-~]/g, " ");
   this.tncust_f18 =("" +(trim(this.tncust_f18))).replace( /[!-\/,:-@,\[-`,\{-~]/g, " ");
   this.tncust_f19 =("" +(trim(this.tncust_f19))).replace( /[!-\/,:-@,\[-`,\{-~]/g, " ");
   this.tncust_f20 =("" +(trim(this.tncust_f20))).replace( /[!-\/,:-@,\[-`,\{-~]/g, " ");
   this.tncust_f30 =("" +(trim(this.tncust_f30))).replace( /[!-\/,:-@,\[-`,\{-~]/g, " ");

   objAddr =this.getPrimaryBillingAddress();
   if(objAddr) { boolSuccess =objAddr.clean(); }

   objAddr =this.getPrimaryShippingAddress();
   if(objAddr) { boolSuccess =objAddr.clean(); }

   return boolSuccess;
}





/***********************************************************************************************************************
| evaluateExpiryDates
| Set membership flags & dates.
\**********************************************************************************************************************/
Cust.prototype.evaluateExpiryDates =function() {
   var now =new Date();

   if(this.dateDistributorExpires && this.dateDistributorExpires >= now) { this.boolDistributor =true; }

   if(this.datePreferredExpires && this.datePreferredExpires >= now && !this.boolDistributor) { this.boolPreferred =true; }
   else { delete this.datePreferredExpires; }
}





/***********************************************************************************************************************
| isPhonePresent
| Check if a given Phone# is in the cust's file.
| strPhone: phone# to check.
| returns: position of address in array, or -1.
\**********************************************************************************************************************/
Cust.prototype.isPhonePresent =function(strPhone) {
   var intIndexInArray =-1;
   var objNewPhone =new Phone(strPhone);
   var index;

   if(objNewPhone) {
      for(index =0; index < this.arrPhone; index++) {
         if(this.arrPhone[index].strPhone == objNewPhone.strPhone) {
            inIndexInArry =index;
            break;
         }
      }
   }

   return intIndexInArray;
}





/***********************************************************************************************************************
| isEmailPresent
| Check if a given Email is in the cust's file.
| strEmail: Email to check.
| returns: position of address in array, or -1.
\**********************************************************************************************************************/
Cust.prototype.isEmailPresent =function(strEmail) {
   var intIndexInArray =-1;
   var objNewEmail =new Email(strEmail);
   var index;

   if(objNewEmail) {
      for(index =0; index < this.arrEmail; index++) {
         if(this.arrEmail[index].strEmail == objNewEmail.strEmail) {
            inIndexInArry =index;
            break;
         }
      }
   }

   return intIndexInArray;
}





/***********************************************************************************************************************
| isBillingAddressPresent
| Check if a given billing address is in the cust's file.
| objAddress: address to check.
| returns: position of address in array, or -1.
\**********************************************************************************************************************/
Cust.prototype.isBillingAddressPresent =function(objAddress) {
   var intIndexInArray =objAddress.isAddressInArray(this.arrBAddr);
   return intIndexInArray;
}





/***********************************************************************************************************************
| isShippingAddressPresent
| Check if a given shipping address is in the cust's file.
| objAddress: address to check.
| returns:  position of address in array, or -1.
\**********************************************************************************************************************/
Cust.prototype.isShippingAddressPresent =function(objAddress) {
   var intIndexInArray =objAddress.isAddressInArray(this.arrSAddr)
   return intIndexInArray;
}





/***********************************************************************************************************************
| serialize
| Collapse data into a | delimited string.
| return: string w/serialized data
\**********************************************************************************************************************/
Cust.prototype.serialize =function() {
   var strSerialized ="";

   this.clean();

   strSerialized += "|" +this.tncust_f4 +"|";                       //field 1, cust#
   strSerialized += "|" +this.tncust_f1 +"|";                       //field 2, lname
   strSerialized += "|" +this.tncust_f2 +"|";                       //field 3, fname
   strSerialized += "|" +this.tncust_f17 +"|";                      //field 4, ph#
   strSerialized += "|" +this.tncust_f310 +"|";                     //field 5, email
   strSerialized += "|" +this.tncust_f30 +"|";                      //field 6, sponsor ID
   if(this.dateDistributorExpires) {                                //field 7, dist exp date
      strSerialized += "|" +this.dateDistributorExpires.valueOf() +"|";
   }
   else {
      strSerialized += "|0|";
   }
   if(this.datePreferredExpires) {                                  //field 8, pref exp date
      strSerialized += "|" +this.datePreferredExpires.valueOf() +"|";
   }
   else {
      strSerialized += "|0|";
   }

   return strSerialized;
}





/***********************************************************************************************************************
| deserialize
| Expand data from a | delimited string.
| return: boolean - successful?
\**********************************************************************************************************************/
Cust.prototype.deserialize =function(strSerialized) {
   var boolSuccess =true;

   try {
      if(strSerialized.indexOf("|") >= 0) {
         var arrMembers =strSerialized.split("||");

         this.tncust_f4 =("" +arrMembers[0]).substr(1);
         this.tncust_f1 =arrMembers[1];
         this.tncust_f2 =arrMembers[2];
         this.tncust_f17 =arrMembers[3];
         this.tncust_f310 =arrMembers[4];
         this.tncust_f30 =arrMembers[5];
         this.dateDistributorExpires =arrMembers[6];
         this.datePreferredExpires =(arrMembers[7]).substr(0,("" +arrMembers[7]).length -1);

         if(this.dateDistributorExpires) { this.dateDistributorExpires =new Date(1 *this.dateDistributorExpires); }
         if(this.datePreferredExpires) { this.datePreferredExpires =new Date(1 *this.datePreferredExpires); }
         
         this.evaluateExpiryDates();
      }
   } catch(err) { boolSuccess =false; }

   return boolSuccess;
}






/***********************************************************************************************************************
| ACCESSORS ------------------------------------------------------------------------------------------------------------
\**********************************************************************************************************************/

/***********************************************************************************************************************
| getPrimaryPhone
| returns: primary phone#, if present; undefined otherwise
\**********************************************************************************************************************/
Cust.prototype.getPrimaryPhone =function() {
   var objPrimaryPhone;

   if(this.arrPhone && this.arrPhone[0]) { objPrimaryPhone =this.arrPhone[0]; }

   return objPrimaryPhone;
}

/***********************************************************************************************************************
| getPrimaryEmail
| returns: primary email, if present; undefined otherwise
\**********************************************************************************************************************/
Cust.prototype.getPrimaryEmail =function() {
   var objPrimaryEmail;

   if(this.arrEmail && this.arrEmail[0]) { objPrimaryEmail =this.arrEmail[0]; }

   return objPrimaryEmail;
}

/***********************************************************************************************************************
| getPrimaryBillingAddress
| If no primary address exists, a blank one will be created.
| returns: primary billing Address
\**********************************************************************************************************************/
Cust.prototype.getPrimaryBillingAddress =function() {
   var objAddress;

   if(!this.arrBAddr || !this.arrBAddr[0]) {   //if nothing in addresses, create array & populate with new address
      objAddress =new Address();
      objAddress.strParentId =this.strId;
      this.initBAddr(objAddress);
      this.arrBAddr =new Array(objAddress);
   }
   else { objAddress =this.arrBAddr[0]; }


   return objAddress;
}

/***********************************************************************************************************************
| getPrimaryShippingAddress
| If no primary address exists, a blank one will be created.
| returns: primary shipping Address
\**********************************************************************************************************************/
Cust.prototype.getPrimaryShippingAddress =function() {
   var objAddress;

   if(!this.arrSAddr || !this.arrSAddr[0]) {   //if nothing in addresses, create array & populate with new address
      objAddress =new Address();
      objAddress.strParentId =this.strId;
      this.initSAddr(objAddress);
      this.arrSAddr =new Array (objAddress);
   }
   else { objAddress =this.arrSAddr[0]; }


   return objAddress;
}



/***********************************************************************************************************************
| MUTATORS -------------------------------------------------------------------------------------------------------------
\**********************************************************************************************************************/

/***********************************************************************************************************************
| addPhone - Add a phone# to the cust if unique; otherwise it will displace it's duplicate.
| objPhone: phone# to add.
| returns: index in array if phone# added.
\**********************************************************************************************************************/
Cust.prototype.addPhone =function(objPhone) {
   if(objPhone && objPhone.className && objPhone.className == "Phone") {
      var intIndexInArray =this.isPhonePresent(objPhone.strPhone);

      if(intIndexInArray == -1) { this.arrPhone[this.arrPhone.length] =objPhone; }
      else { this.arrPhone[intIndexInArray] =objPhone; }
   }

   return intIndexInArray;
}

/***********************************************************************************************************************
| addEmail - Add a email to the cust if unique; otherwise it will displace it's duplicate.
| objEmail: email to add.
| returns: index in array if email added.
\**********************************************************************************************************************/
Cust.prototype.addEmail =function(objEmail) {
   if(objEmail && objEmail.className && objEmail.className == "Email") {
      var intIndexInArray =this.isEmailPresent(objEmail.strEmail);

      if(intIndexInArray == -1) { this.arrEmail[this.arrEmail.length] =objEmail; }
      else { this.arrEmail[intIndexInArray] =objEmail; }
   }

   return intIndexInArray;
}

/***********************************************************************************************************************
| addBillingAddress - Add a billing address to the cust if unique; otherwise it will displace it's duplicate.
| objAddress: address to add.
| returns: index in array if address added.
\**********************************************************************************************************************/
Cust.prototype.addBillingAddress =function(objAddress) {
   var intIndexInArray =this.isBillingAddressPresent(objAddress);

   if(intIndexInArray == -1) { this.arrBAddr[this.arrBAddr.length] =objAddress; }
   else { this.arrBAddr[intIndexInArray] =objAddress; }

   return intIndexInArray;
}

/***********************************************************************************************************************
| addShippingAddress - Add a shipping address to the cust if unique; otherwise it will displace it's duplicate.
| objAddress: address to add.
| returns: index in array if address added.
\**********************************************************************************************************************/
Cust.prototype.addShippingAddress =function(objAddress) {
   var intIndexInArray =this.isShippingAddressPresent(objAddress);

   if(intIndexInArray == -1) { this.arrSAddr[this.arrSAddr.length] =objAddress; }
   else { this.arrSAddr[intIndexInArray] =objAddress; }

   return intIndexInArray;
}

/***********************************************************************************************************************
| setPrimaryBillingAddress
| Sets primary shipping address.  If new address is the same as the old, the old is displaced; otherwise the old is
| added to the array of non-primary addresses.
| objAddress: new primary shipping address.
| returns: true if the address was valid and is now the primary shipping address.
\**********************************************************************************************************************/
Cust.prototype.setPrimaryBillingAddress =function(objAddress) {
   var boolSuccess =false;
   var objOldPrimaryAddress;
   var intNewAddressArrayPosition;

   if(objAddress && objAddress.className == "Address") {
      if(!this.arrBAddr || !this.arrBAddr[0]) { this.arrBAddr =new Array (objAddress); }  //if nothing in addresses, create array & populate with new address
      else if(this.arrBAddr && this.arrBAddr[0] && this.arrBAddr[0].isAddressBlank()) { this.arrBAddr[0] =objAddress; }  //if primary address is blank, replace with new address
      else {
         //is new address already in array? if primary, abort and return; else remove array entry
         intNewAddressArrayPosition =this.isBillingAddressPresent(objAddress);
         if(intNewAddressArrayPosition === 0) { //address is already primary, skip
            this.arrBAddr[0] =objAddress;
            boolSuccess =true;
         }
         else {
            if(intNewAddressArrayPosition > 0) { delete this.arrBAddr[intNewAddressArrayPosition]; }
            objOldPrimaryAddress =this.arrBAddr[0];
            this.arrBAddr[0] =objAddress;
            this.addBillingAddress(objOldPrimaryAddress);
            boolSuccess =true;
         }
      }
   }

   return boolSuccess;
}

/***********************************************************************************************************************
| setPrimaryShippingAddress
| Sets primary shipping address.  If new address is the same as the old, the old is displaced; otherwise the old is
| added to the array of non-primary addresses.
| objAddress: new primary shipping address.
| returns: true if the address was valid and is now the primary shipping address.
\**********************************************************************************************************************/
Cust.prototype.setPrimaryShippingAddress =function(objAddress) {
   var boolSuccess =false;
   var objOldPrimaryAddress;
   var intNewAddressArrayPosition;

   if(objAddress && objAddress.className == "Address") {
      if(!this.arrSAddr || !this.arrSAddr[0]) { this.arrSAddr =new Array (objAddress); }  //if nothing in addresses, create array & populate with new address
      else if(this.arrSAddr && this.arrSAddr[0] && this.arrSAddr[0].isAddressBlank()) { this.arrSAddr[0] =objAddress; }  //if primary address is blank, replace with new address
      else {
         //is new address already in array? if primary, abort and return; else remove array entry
         intNewAddressArrayPosition =this.isShippingAddressPresent(objAddress);
         if(intNewAddressArrayPosition === 0) { //address is already primary, skip
            this.arrSAddr[0] =objAddress;
            boolSuccess =true;
         }
         else {
            if(intNewAddressArrayPosition > 0) { delete this.arrSAddr[intNewAddressArrayPosition]; }
            objOldPrimaryAddress =this.arrSAddr[0];
            this.arrSAddr[0] =objAddress;
            this.addShippingAddress(objOldPrimaryAddress);
            boolSuccess =true;
         }
      }
   }

   return boolSuccess;
}

/***********************************************************************************************************************
| setPrimaryPhone
| Sets primary phone.  If new phone is the same as the old, the old is displaced; otherwise the old is
| added to the array of non-primary phones.
| objPhone: new primary phone.
| returns: true if the phone was valid and is now the primary shipping phone.
\**********************************************************************************************************************/
Cust.prototype.setPrimaryPhone =function(objPhone) {
   var boolSuccess =false;
   var objOldPrimaryPhone;
   var intNewPhoneArrayPosition;

   if(objPhone && objPhone.className == "Phone") {
      if(!this.arrPhone || !this.arrPhone[0]) { this.arrPhone =new Array (objPhone); }  //if nothing in addresses, create array & populate with new address
      else if(this.arrPhone && this.arrPhone[0] && !this.arrPhone[0].strPhone) { this.arrPhone[0] =objPhone; }  //if primary address is blank, replace with new address
      else {
         //is new address already in array? if primary, abort and return; else remove array entry
         intNewPhoneArrayPosition =this.isPhonePresent(objPhone.strPhone);
         if(intNewPhoneArrayPosition === 0) { //address is already primary, skip
            this.arrPhone[0] =objPhone;
            boolSuccess =true;
         }
         else {
            if(intNewPhoneArrayPosition > 0) { delete this.arrPhone[intNewPhoneArrayPosition]; }
            objOldPrimaryPhone =this.arrPhone[0];
            this.arrPhone[0] =objPhone;
            this.addPhone(objOldPrimaryPhone);
            boolSuccess =true;
         }
      }
   }

   return boolSuccess;
}

/***********************************************************************************************************************
| setPrimaryEmail
| Sets primary email.  If new email is the same as the old, the old is displaced; otherwise the old is
| added to the array of non-primary emails.
| objEmail: new primary email.
| returns: true if the email was valid and is now the primary shipping email.
\**********************************************************************************************************************/
Cust.prototype.setPrimaryEmail =function(objEmail) {
   var boolSuccess =false;
   var objOldPrimaryEmail;
   var intNewEmailArrayPosition;

   if(objEmail && objEmail.className == "Email") {
      if(!this.arrEmail || !this.arrEmail[0]) { this.arrEmail =new Array (objEmail); }  //if nothing in addresses, create array & populate with new address
      else if(this.arrEmail && this.arrEmail[0] && !this.arrEmail[0].strEmail) { this.arrEmail[0] =objEmail; }  //if primary address is blank, replace with new address
      else {
         //is new address already in array? if primary, abort and return; else remove array entry
         intNewEmailArrayPosition =this.isEmailPresent(objEmail.strEmail);
         if(intNewEmailArrayPosition === 0) { //address is already primary, skip
            this.arrEmail[0] =objEmail;
            boolSuccess =true;
         }
         else {
            if(intNewEmailArrayPosition > 0) { delete this.arrEmail[intNewEmailArrayPosition]; }
            objOldPrimaryEmail =this.arrEmail[0];
            this.arrEmail[0] =objEmail;
            this.addEmail(objOldPrimaryEmail);
            boolSuccess =true;
         }
      }
   }

   return boolSuccess;
}





function trim(strText) {
   var strTemp;

   if(strText) {
      strTemp ="" +strText;
      if(strText.replace) {
         strText =strText.replace(/^\s+|\s+$/g, "");
         strText =strText.replace(/&nbsp;/g, "");
      }
   }
   else {
      strText ="";
   }

   return strText;
}






try { checkIfLastToLoad(); } catch(error) {;}
